feat: let a project file return several configurations
A project.cpp described one configuration: CrafterBuildProject returns a Configuration by value, and anything else the project wanted built had to be among that one's dependencies. There it was built as part of that configuration's graph: under the check that a graph builds each module once, with its libraries handed up to whatever links that configuration, and with its tests and scripts out of reach of `test` and `run`. Jorijn asked for a project.cpp that can return several unrelated configurations which all get built, and suggested a separate entry point, so that no existing project file breaks. A project file now defines one of two functions. CrafterBuildProject is as it was. CrafterBuildConfigurations returns std::vector<Configuration*>: pointers to configurations the file keeps alive itself, or that GitProject and LocalProject gave it. The two have different names because the loader finds the entry point by its C symbol, which carries no return type, so one name with two return types would be called as whichever the loader guessed. They are pointers because a project file takes pointers between its configurations, for `dependencies` and for a test's, before it returns them, and a vector of values would move them. LoadConfigurations loads a project file with either entry point and returns the list. A CrafterBuildProject's Configuration comes back as a list of one, constructed by the call where the loader keeps it for the rest of the process; LoadProjectFromRoot, which did that for a sibling's, leaves it to LoadConfigurations, and the project cache holds the lists without owning a Configuration. The Configuration of the project crafter-build is pointed at is kept that long as well, where it was a local of Run(). LoadConfigurations throws for a file that defines both functions or neither, and for a CrafterBuildConfigurations that returns nothing, a null pointer or one configuration twice. It also throws for one that returns a configuration a project loaded through GitProject or LocalProject keeps to itself, which the loader now takes down when it loads that project's file: what the configurations the file returned led to through `dependencies` or through their tests, as far as the first configuration on each way that a loaded project's file had returned or another loaded project keeps. Returned from here, such a configuration would count as this file's own to `test` and `run`, and only the configurations a project file returns are rebased onto its directory. LoadProject stays, for a caller that wants a CrafterBuildProject's Configuration by value, and throws for a file with the other entry point. GitProject and LocalProject keep, per spec and args, the list a sibling's file returned, and return the first one unless the spec's new `configuration` names another by Configuration::name. Every configuration of the list is rebased onto the sibling's directory, except one that another project's file had returned before, which was rebased onto that project's. Specs that differ only in the name they pick share one load. A name the sibling does not have throws, listing the names it has, and a name two of its configurations share throws too. A `#pragma crafter dependency` gets the first. BuildAll builds the returned configurations in one pass. Each is built by Build(), as a project's one configuration is, the first on the calling thread and each other one on a thread of its own, and its result then goes into the record of the pass under its PcmDir, where Build() looks for a dependency. One that another of them is built on is built before that other starts, which then finds it in the record: started beside it, the other would not find it there yet and would build it a second time. A configuration is not put into the record before it is built, because a dependency of its own that is built into the same directory would be handed its unfinished build and wait for it, while it waits for the dependency. All of them are built to the end, and the errors come back in the order given, each message once; an exception a build throws comes back as an error that names the configuration, where Run() printed the exception's own text for a project's one configuration. Refused before anything is built: two that would be built into one directory, which would be two builds writing the same files at once; several that are each built on something of another's directory, so that none could be built first; and one that is built on something of its own directory when another is built on that directory as well, since the record holds one result for a directory and the other could be given either. One shortcut is in it. A wasm executable that declares wasmVariants builds what it is built on once more per variant, and says which variant by rewriting those configurations in place, at the end of its Build(). A library that two returned configurations share would change under the one still building its baseline, so with such an executable anywhere in the graph BuildAll builds the configurations one after the other, on the calling thread. Keeping them side by side takes a variant driver that does not rewrite configurations other builds read, which this commit does not attempt. Run() works on the list. A build builds all of it. `test` runs the tests, and `run` offers the scripts, of the ones in it that the project file made itself. One that another project's file returned to GitProject or LocalProject, and that this file returns in turn, is built with the rest, but its tests and scripts stay that project's, so `run` still reaches only the scripts of the project crafter-build was pointed at. What the list itself rules out Run() refuses once the project file has loaded, before any of the list is built and before AGENTS.md is written: for a build, `-r`, `install` and `bundle` whatever BuildAll refuses a list for, and for `test`, which builds its tests with Build() and never comes to BuildAll, two returned configurations of one directory. BuildAll makes its refusals again for whoever calls it. A script is handed the configuration it is declared on. `lint` and `format` walk from every returned configuration and keep, as before, the configurations in the project's directory: the returned ones first, in the order the file returned them, then what they depend on, so that of two returned ones that register a rule under one name the earlier one's runs. `-r` runs the executable when exactly one of them is one, and `install` and `bundle` export the one that is packaged as a Flatpak; with more than one candidate each refuses and names them, `-r` once everything is built and `install` and `bundle` before anything is. AGENTS.md takes the notes that the dependencyNotes of each returned configuration the file made itself selects among what that one is built against, and the file's pragma tooling and a returned configuration of another project go by the widest of those settings. There, a configuration is another project's when GitProject or LocalProject loaded it from another project file than the one whose AGENTS.md is written, two files being one when they resolve to one path, so UpdateAgentsMd still reads the setting of a Configuration one of the two returned when it is given that Configuration's own project file. For that, RunTests, RunLint, RunScript and UpdateAgentsMd each gained an overload that takes the list, and the one that takes a single Configuration calls it with a list of one. SetParentProject gained one too, and ParentLib searches the graph of every configuration it was given. BuildAll, RunTests and RunScript count a configuration the list names twice once. The tests of several configurations share what one configuration's had to itself: AddTest names a test's sources, tests/<name>, and its build directory after the test and its compile state, not after the configuration that declares it. Two configurations that each declare a test of one name, compiled alike, would have RunTests build both in that one directory, each with the dependencies of its own declaration. RunTests fails both without building either, and each failure names the configurations. It compares tests of different configurations only, so a project of one configuration is treated as it was. The --help text names the second entry point, says what -r, install and bundle do with several configurations, and speaks of the project file's entry point as what gets the project args, where it named CrafterBuildProject. AGENTS.template.md says that a project.cpp returns one configuration or several, that Run() works on the list and what a new verb has to decide about it, which configurations have their path rebased, and that the scripts of a returned configuration of another project cannot be run. Two of its notes named LoadProject as what loads a project.cpp for crafter-build, which Run(), GitProject and LocalProject no longer call, and speak of the load now. Its rule for the link flags of a test that loads a project file names the ways a test loads one, where it named LoadProject, and its entry for `:Script` speaks of the `scripts` of the configurations the driver is given, where it named `cfg.scripts`. Its entry for `:Platform` names LoadConfigurations beside LoadProject. That entry also said that `:Platform` holds all OS-specific code, which it did not before this range either, :Agents, :Progress, :Lint and :Clang including POSIX headers of their own, and no longer says so. The bug-prover lens's section names BuildAll's threads and LoadConfigurations. Not in this commit: a way to say which executable -r runs or which app install and bundle export when there are several, and a keyword that lets a `#pragma crafter dependency` name a configuration.
This commit is contained in:
parent
a7aa22220b
commit
4283a3ecd2
16 changed files with 986 additions and 241 deletions
|
|
@ -345,18 +345,60 @@ namespace {
|
|||
return DependencyNote{ .name = name, .text = std::move(note.text) };
|
||||
}
|
||||
|
||||
// The loaded projects `configs` lead to without passing through another
|
||||
// loaded project, in the order the configurations name them: one ring
|
||||
// further out. `walked` spans the whole collection, so each
|
||||
// Configuration is looked at once however many paths reach it.
|
||||
std::vector<const Configuration*> NextRing(std::span<const Configuration* const> configs, std::unordered_set<const Configuration*>& walked) {
|
||||
// Tells a configuration of another project from one of the project
|
||||
// whose AGENTS.md is written, the one of the project file it is made
|
||||
// with. GitProject and LocalProject hold the configurations of a project
|
||||
// file they loaded, so held or not does not tell the two apart: the
|
||||
// project whose notes are written may itself have been loaded through
|
||||
// one of them, by a program that then writes its AGENTS.md for it. Two
|
||||
// files are one project file when they resolve to one path, which is
|
||||
// how the loader tells project files apart: a hard link to the project
|
||||
// file in another directory is another project's. Each loaded project's
|
||||
// file is compared with the project file once and the answer kept, so a
|
||||
// configuration is the same thing every time one collection asks,
|
||||
// whatever happens to the files meanwhile.
|
||||
class OtherProjects {
|
||||
public:
|
||||
explicit OtherProjects(const fs::path& projectFile) {
|
||||
// Left empty when the file cannot be resolved, and then no
|
||||
// loaded project's file is it.
|
||||
std::error_code ec;
|
||||
projectFile_ = fs::canonical(projectFile, ec);
|
||||
if (ec) projectFile_.clear();
|
||||
}
|
||||
// The project `cfg` was loaded from when that is another one;
|
||||
// nullopt for a configuration the project's own file made.
|
||||
std::optional<LoadedProject> Of(const Configuration& cfg) {
|
||||
std::optional<LoadedProject> loaded = LoadedProjectOf(cfg);
|
||||
if (!loaded) return loaded;
|
||||
const auto [known, added] = same_.try_emplace(loaded->projectFile, false);
|
||||
if (added && !projectFile_.empty()) {
|
||||
std::error_code ec;
|
||||
const fs::path file = fs::canonical(loaded->projectFile, ec);
|
||||
known->second = !ec && file == projectFile_;
|
||||
}
|
||||
if (known->second) loaded.reset();
|
||||
return loaded;
|
||||
}
|
||||
private:
|
||||
// The project file, resolved.
|
||||
fs::path projectFile_;
|
||||
// By a loaded project's file: whether it is projectFile_.
|
||||
std::unordered_map<fs::path, bool> same_;
|
||||
};
|
||||
|
||||
// The other projects `configs` lead to without passing through another
|
||||
// of them, in the order the configurations name them: one ring further
|
||||
// out. `walked` spans the whole collection, so each Configuration is
|
||||
// looked at once however many paths reach it.
|
||||
std::vector<const Configuration*> NextRing(std::span<const Configuration* const> configs, std::unordered_set<const Configuration*>& walked, OtherProjects& others) {
|
||||
std::vector<const Configuration*> ring;
|
||||
std::vector<const Configuration*> pending(configs.rbegin(), configs.rend());
|
||||
while (!pending.empty()) {
|
||||
const Configuration* cfg = pending.back();
|
||||
pending.pop_back();
|
||||
if (!cfg || !walked.insert(cfg).second) continue;
|
||||
if (LoadedProjectOf(*cfg)) {
|
||||
if (others.Of(*cfg)) {
|
||||
ring.push_back(cfg);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -365,21 +407,72 @@ namespace {
|
|||
return ring;
|
||||
}
|
||||
|
||||
// The AGENTS.consumer.md of every project `project` depends on, as
|
||||
// project.dependencyNotes selects them, ring by ring: first the loaded
|
||||
// projects (the ones GitProject and LocalProject returned) that its
|
||||
// `#pragma crafter dependency` lines and its configurations name, in
|
||||
// that order, then the ones those depend on. Its own configurations, and
|
||||
// a dependency's internal ones, are only walked through. Direct stops
|
||||
// after the first ring.
|
||||
// The AGENTS.consumer.md of every project the project is built against,
|
||||
// as the dependencyNotes of `configurations`, the configurations its
|
||||
// project file returns, select them, ring by ring: first the other
|
||||
// projects (the ones GitProject and LocalProject loaded from another
|
||||
// project file) that its `#pragma crafter dependency` lines and its
|
||||
// configurations name, in that order, then the ones those depend on.
|
||||
// Its own configurations, and a dependency's internal ones, are only
|
||||
// walked through.
|
||||
//
|
||||
// Each configuration the file made itself selects for what it is built
|
||||
// against: None nothing, Direct the first ring from it, Transitive every
|
||||
// ring. Another project therefore has its own dependencies followed
|
||||
// when a Transitive configuration leads to it, at whatever depth, and
|
||||
// not when only Direct ones do. The pragma lines belong to the file and
|
||||
// not to one configuration, and a configuration the file loaded from
|
||||
// another project and returns is a project of the first ring itself,
|
||||
// with a setting that is that project's own: both go by the widest
|
||||
// setting among the configurations the file made, and by Transitive
|
||||
// when it made none.
|
||||
//
|
||||
// Never walked: the project's tests, since a plain build never touches
|
||||
// them and a test's dependency can point at the Configuration the project
|
||||
// file returned by value, which has since moved; and a dependency's own
|
||||
// pragma tooling, which is about how the dependency is built, not how it
|
||||
// is used, and is no `dependencies` edge to begin with.
|
||||
std::vector<DependencyNote> CollectNotes(const Configuration& project, const fs::path& projectFile, const fs::path& projectDir) {
|
||||
std::unordered_set<const Configuration*> walked = { &project };
|
||||
// them and a test's dependency can point at the Configuration a
|
||||
// CrafterBuildProject returned by value, which a caller of LoadProject
|
||||
// may have moved since; and a dependency's own pragma tooling, which is
|
||||
// about how the dependency is built, not how it is used, and is no
|
||||
// `dependencies` edge to begin with.
|
||||
std::vector<DependencyNote> CollectNotes(std::span<const Configuration* const> configurations, const fs::path& projectFile, const fs::path& projectDir) {
|
||||
OtherProjects others(projectFile);
|
||||
const auto made = [&](const Configuration* cfg) { return !others.Of(*cfg); };
|
||||
const auto asks = [&](DependencyNotes setting) {
|
||||
return std::ranges::any_of(configurations, [&](const Configuration* cfg) { return made(cfg) && cfg->dependencyNotes == setting; });
|
||||
};
|
||||
const bool transitive = asks(DependencyNotes::Transitive) || std::ranges::none_of(configurations, made);
|
||||
if (!transitive && !asks(DependencyNotes::Direct)) return {};
|
||||
|
||||
// Where the walk starts, and the starts that ask for every ring.
|
||||
std::vector<const Configuration*> start;
|
||||
std::vector<const Configuration*> deep;
|
||||
for (const Configuration* dep : PragmaDependencies(projectFile)) {
|
||||
start.push_back(dep);
|
||||
if (transitive) deep.push_back(dep);
|
||||
}
|
||||
for (const Configuration* cfg : configurations) {
|
||||
const DependencyNotes setting = made(cfg) ? cfg->dependencyNotes : transitive ? DependencyNotes::Transitive : DependencyNotes::Direct;
|
||||
if (setting == DependencyNotes::None) continue;
|
||||
start.push_back(cfg);
|
||||
if (setting == DependencyNotes::Transitive) deep.push_back(cfg);
|
||||
}
|
||||
// Every other project those lead to: the ones whose own
|
||||
// dependencies are followed. Worked out ahead of the walk below,
|
||||
// which meets each project once, at its nearest ring, and that can
|
||||
// be a ring a Direct configuration put it in.
|
||||
std::unordered_set<const Configuration*> followed;
|
||||
{
|
||||
std::unordered_set<const Configuration*> reached;
|
||||
for (std::vector<const Configuration*> ring = NextRing(deep, reached, others); !ring.empty();) {
|
||||
std::vector<const Configuration*> next;
|
||||
for (const Configuration* cfg : ring) {
|
||||
followed.insert(cfg);
|
||||
next.insert(next.end(), cfg->dependencies.begin(), cfg->dependencies.end());
|
||||
}
|
||||
ring = NextRing(next, reached, others);
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_set<const Configuration*> walked;
|
||||
// The project's own notes are for its consumers, so they never go
|
||||
// into its own AGENTS.md, whatever dependency they sit beside. Both
|
||||
// places count for a symlinked project file.
|
||||
|
|
@ -389,13 +482,11 @@ namespace {
|
|||
std::unordered_set<std::string> identities;
|
||||
std::vector<DependencyNote> notes;
|
||||
|
||||
std::vector<const Configuration*> start;
|
||||
for (const Configuration* dep : PragmaDependencies(projectFile)) start.push_back(dep);
|
||||
start.insert(start.end(), project.dependencies.begin(), project.dependencies.end());
|
||||
for (std::vector<const Configuration*> ring = NextRing(start, walked); !ring.empty();) {
|
||||
for (std::vector<const Configuration*> ring = NextRing(start, walked, others); !ring.empty();) {
|
||||
std::vector<const Configuration*> next;
|
||||
for (const Configuration* cfg : ring) {
|
||||
const std::optional<LoadedProject> loaded = LoadedProjectOf(*cfg);
|
||||
// Another project's, or NextRing had walked through it.
|
||||
const std::optional<LoadedProject> loaded = others.Of(*cfg);
|
||||
const fs::path file = loaded->projectFile.parent_path() / NotesFileName;
|
||||
// One library under two specs (a pin here, another revision
|
||||
// further down) is described once, as the ring nearest the
|
||||
|
|
@ -403,10 +494,9 @@ namespace {
|
|||
if (identities.insert(loaded->identity).second && files.insert(file).second) {
|
||||
if (std::optional<DependencyNote> note = ReadNote(file, loaded->name)) notes.push_back(std::move(*note));
|
||||
}
|
||||
next.insert(next.end(), cfg->dependencies.begin(), cfg->dependencies.end());
|
||||
if (followed.contains(cfg)) next.insert(next.end(), cfg->dependencies.begin(), cfg->dependencies.end());
|
||||
}
|
||||
if (project.dependencyNotes == DependencyNotes::Direct) break;
|
||||
ring = NextRing(next, walked);
|
||||
ring = NextRing(next, walked, others);
|
||||
}
|
||||
return notes;
|
||||
}
|
||||
|
|
@ -504,6 +594,11 @@ std::string Crafter::MergeDependencyNotes(std::string_view templateMd, std::span
|
|||
}
|
||||
|
||||
bool Crafter::UpdateAgentsMd(const Configuration& project, const fs::path& projectFile) {
|
||||
std::array<const Configuration*, 1> one = { &project };
|
||||
return UpdateAgentsMd(std::span<const Configuration* const>(one), projectFile);
|
||||
}
|
||||
|
||||
bool Crafter::UpdateAgentsMd(std::span<const Configuration* const> configurations, const fs::path& projectFile) {
|
||||
std::error_code ec;
|
||||
// Beside the project file as it was named, so a symlinked project.cpp
|
||||
// keeps the AGENTS.md beside the link.
|
||||
|
|
@ -527,7 +622,7 @@ bool Crafter::UpdateAgentsMd(const Configuration& project, const fs::path& proje
|
|||
return false;
|
||||
}
|
||||
|
||||
const std::vector<DependencyNote> notes = project.dependencyNotes == DependencyNotes::None ? std::vector<DependencyNote>() : CollectNotes(project, projectFile, projectDir);
|
||||
const std::vector<DependencyNote> notes = CollectNotes(configurations, projectFile, projectDir);
|
||||
std::string wanted;
|
||||
try {
|
||||
wanted = std::format("{}{}{}", GeneratedLine, LineEnd(templateMd), MergeDependencyNotes(templateMd, notes));
|
||||
|
|
|
|||
Loading…
Reference in a new issue