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
|
|
@ -336,40 +336,33 @@ ExternalBuildResult Crafter::ExternalOutputs(const ExternalDependency& dep, std:
|
|||
|
||||
namespace {
|
||||
std::mutex ProjectCacheMutex;
|
||||
std::unordered_map<std::string, std::unique_ptr<Configuration>> ProjectCache;
|
||||
// Keyed by the Configurations ProjectCache owns, which live as long as
|
||||
// the process does, so the pointers never dangle. Guarded by
|
||||
// ProjectCacheMutex as well.
|
||||
// What each project file loaded here returned, in the file's order, by
|
||||
// spec and args. An entry is never changed or removed once it is in.
|
||||
std::unordered_map<std::string, std::vector<Configuration*>> ProjectCache;
|
||||
// Keyed by the Configurations ProjectCache holds, which live as long as
|
||||
// the process does (see LoadConfigurations), so the pointers never
|
||||
// dangle. Guarded by ProjectCacheMutex as well.
|
||||
std::unordered_map<const Configuration*, LoadedProject> LoadedProjects;
|
||||
// The configurations a loaded project keeps to itself, with the project:
|
||||
// what the configurations its file returned led to when the file was
|
||||
// loaded and the file did not return. A configuration leads to what it
|
||||
// is built on and to the configurations of its tests, and each of those
|
||||
// leads on in the same way. Taken down at that moment, since a consumer
|
||||
// may add to the `dependencies` of a configuration it was given, and
|
||||
// what it adds is its own. Guarded by ProjectCacheMutex as well.
|
||||
std::unordered_map<const Configuration*, LoadedProject> KeptConfigurations;
|
||||
|
||||
// Run the dep's project.cpp from its own directory so its relative paths
|
||||
// (cfg.path = "./", interfaces/, lib/, ...) resolve against the dep
|
||||
// root, then rebase cfg.path and all other relative path collections to
|
||||
// absolute so Build() can run from any cwd without misresolving them.
|
||||
// The mutex is NOT held across LoadProject — the dep may itself call
|
||||
// GitProject/LocalProject, and a non-recursive lock would deadlock.
|
||||
//
|
||||
// The Configuration is made on the heap with the call as its
|
||||
// initialiser, so it is constructed where the cache keeps it and is
|
||||
// never moved. A project file takes pointers to the Configuration it
|
||||
// returns, for a test that links the library it describes
|
||||
// (cfg.AddTest(...).Dependencies({ &cfg })), and they are pointers to
|
||||
// this object when the compiler constructed the local the file returns
|
||||
// in place of the return value, as clang does for a function that
|
||||
// returns one local. Assigned to a local here and moved into the cache
|
||||
// from there, the Configuration left every such pointer behind.
|
||||
std::unique_ptr<Configuration> LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span<const std::string_view> args) {
|
||||
fs::path savedCwd = fs::current_path();
|
||||
std::unique_ptr<Configuration> made;
|
||||
fs::current_path(depRoot);
|
||||
try {
|
||||
made.reset(new Configuration(LoadProject(projectPath, args)));
|
||||
} catch (...) {
|
||||
fs::current_path(savedCwd);
|
||||
throw;
|
||||
}
|
||||
fs::current_path(savedCwd);
|
||||
Configuration& cfg = *made;
|
||||
// What the cache holds under `fullHash`, or nullptr.
|
||||
const std::vector<Configuration*>* CachedProject(const std::string& fullHash) {
|
||||
const std::lock_guard lock(ProjectCacheMutex);
|
||||
const auto it = ProjectCache.find(fullHash);
|
||||
return it == ProjectCache.end() ? nullptr : &it->second;
|
||||
}
|
||||
|
||||
// cfg.path and every other relative path collection of `cfg`, made
|
||||
// absolute against `depRoot`, so Build() can run from any cwd without
|
||||
// misresolving them.
|
||||
void RebaseOnto(Configuration& cfg, const fs::path& depRoot) {
|
||||
cfg.path = fs::absolute(depRoot / cfg.path).lexically_normal();
|
||||
auto abs = [&](const fs::path& p) -> fs::path {
|
||||
return p.is_absolute() ? p : fs::absolute(depRoot / p).lexically_normal();
|
||||
|
|
@ -389,21 +382,81 @@ namespace {
|
|||
for (auto& entry : cfg.flatpak.share) {
|
||||
if (!entry.source.empty()) entry.source = abs(entry.source);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
// Into ProjectCache under `fullHash`, with `loaded` recorded beside it.
|
||||
// When another thread stored the same spec first, its Configuration
|
||||
// wins and `cfg` is dropped.
|
||||
Configuration* StoreProject(std::string fullHash, std::unique_ptr<Configuration> cfg, LoadedProject loaded) {
|
||||
std::lock_guard lock(ProjectCacheMutex);
|
||||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||||
return it->second.get();
|
||||
// Run the dep's project.cpp from its own directory so its relative paths
|
||||
// (cfg.path = "./", interfaces/, lib/, ...) resolve against the dep
|
||||
// root, then rebase every configuration it returns onto that root.
|
||||
// The mutex is NOT held across the load — the dep may itself call
|
||||
// GitProject/LocalProject, and a non-recursive lock would deadlock.
|
||||
std::vector<Configuration*> LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span<const std::string_view> args) {
|
||||
fs::path savedCwd = fs::current_path();
|
||||
std::vector<Configuration*> configurations;
|
||||
fs::current_path(depRoot);
|
||||
try {
|
||||
configurations = LoadConfigurations(projectPath, args);
|
||||
} catch (...) {
|
||||
fs::current_path(savedCwd);
|
||||
throw;
|
||||
}
|
||||
Configuration* ptr = cfg.get();
|
||||
ProjectCache.emplace(std::move(fullHash), std::move(cfg));
|
||||
LoadedProjects.emplace(ptr, std::move(loaded));
|
||||
return ptr;
|
||||
fs::current_path(savedCwd);
|
||||
for (Configuration* cfg : configurations) {
|
||||
// One the file loaded from another project file and returns
|
||||
// itself was rebased onto that project's root when it was
|
||||
// loaded, and other consumers may be reading it by now.
|
||||
if (!LoadedProjectOf(*cfg)) RebaseOnto(*cfg, depRoot);
|
||||
}
|
||||
return configurations;
|
||||
}
|
||||
|
||||
// Into ProjectCache under `fullHash`, with `loaded` recorded beside each
|
||||
// configuration that is not another project's already, and beside what
|
||||
// those are built on and the file kept to itself. When another thread
|
||||
// stored the same spec first, what it stored wins.
|
||||
const std::vector<Configuration*>& StoreProject(std::string fullHash, std::vector<Configuration*> configurations, const LoadedProject& loaded) {
|
||||
const std::lock_guard lock(ProjectCacheMutex);
|
||||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
// What this file returned, and then what each of those leads to:
|
||||
// what it is built on and the configurations of its tests. One that
|
||||
// another project's file returned is that project's, and so is what
|
||||
// is found from there.
|
||||
std::vector<const Configuration*> pending;
|
||||
const auto leadsTo = [&pending](const Configuration& cfg) {
|
||||
pending.insert(pending.end(), cfg.dependencies.begin(), cfg.dependencies.end());
|
||||
for (const Test& test : cfg.tests) pending.push_back(&test.config);
|
||||
};
|
||||
for (const Configuration* cfg : configurations) {
|
||||
if (LoadedProjects.try_emplace(cfg, loaded).second) leadsTo(*cfg);
|
||||
}
|
||||
while (!pending.empty()) {
|
||||
const Configuration* kept = pending.back();
|
||||
pending.pop_back();
|
||||
if (!kept || LoadedProjects.contains(kept) || !KeptConfigurations.try_emplace(kept, loaded).second) continue;
|
||||
leadsTo(*kept);
|
||||
}
|
||||
return ProjectCache.emplace(std::move(fullHash), std::move(configurations)).first->second;
|
||||
}
|
||||
|
||||
// The one of `configurations` a spec asks for by `name`: the first for
|
||||
// an empty name. `what` names the spec in the error for a name that none
|
||||
// has, or that two have.
|
||||
Configuration* PickConfiguration(std::span<Configuration* const> configurations, std::string_view name, std::string_view what) {
|
||||
if (name.empty()) return configurations.front();
|
||||
Configuration* picked = nullptr;
|
||||
for (Configuration* cfg : configurations) {
|
||||
if (cfg->name != name) continue;
|
||||
if (picked) throw std::runtime_error(std::format("{}: two of its configurations are named '{}', so the name picks neither", what, name));
|
||||
picked = cfg;
|
||||
}
|
||||
if (picked) return picked;
|
||||
std::string names;
|
||||
for (const Configuration* cfg : configurations) {
|
||||
if (!names.empty()) names += ", ";
|
||||
names += cfg->name;
|
||||
}
|
||||
throw std::runtime_error(std::format("{}: no configuration named '{}' (it has: {})", what, name, names));
|
||||
}
|
||||
|
||||
// `url` as it names a repository: without user info, which is where a
|
||||
|
|
@ -425,10 +478,11 @@ namespace {
|
|||
Configuration* Crafter::GitProject(const GitProjectSpec& spec) {
|
||||
// Two hashes: the clone is shared across specs that point at the same
|
||||
// source revision (forwarded args don't change what gets cloned, just
|
||||
// how the dep's project.cpp interprets them at runtime), while the
|
||||
// in-memory Configuration is keyed by the full spec so each (args)
|
||||
// permutation gets its own Configuration object, and host tooling one
|
||||
// apart from the target's even for the same args.
|
||||
// how the dep's project.cpp interprets them at runtime), while what the
|
||||
// file returned is keyed by the args as well, so each (args)
|
||||
// permutation gets its own Configuration objects, and host tooling ones
|
||||
// apart from the target's even for the same args. Which configuration
|
||||
// the spec picks is in neither: it chooses among what one load returned.
|
||||
std::string srcKey = std::format("git|{}|{}|{}|{}", spec.source.url, spec.source.branch, spec.source.commit, spec.projectFile.string());
|
||||
std::string srcHash = std::format("{:016x}", std::hash<std::string>{}(srcKey));
|
||||
|
||||
|
|
@ -438,12 +492,10 @@ Configuration* Crafter::GitProject(const GitProjectSpec& spec) {
|
|||
fullKey += a;
|
||||
}
|
||||
std::string fullHash = std::format("{:016x}", std::hash<std::string>{}(fullKey));
|
||||
const std::string what = std::format("GitProject({})", spec.source.url);
|
||||
|
||||
{
|
||||
std::lock_guard lock(ProjectCacheMutex);
|
||||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||||
return it->second.get();
|
||||
}
|
||||
if (const std::vector<Configuration*>* cached = CachedProject(fullHash)) {
|
||||
return PickConfiguration(*cached, spec.configuration, what);
|
||||
}
|
||||
|
||||
std::string name = DeriveName(spec.source);
|
||||
|
|
@ -471,15 +523,15 @@ Configuration* Crafter::GitProject(const GitProjectSpec& spec) {
|
|||
}
|
||||
|
||||
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
||||
std::unique_ptr<Configuration> cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews);
|
||||
std::vector<Configuration*> configurations = LoadProjectFromRoot(projectPath, cloneDir, argViews);
|
||||
|
||||
const fs::path projectInRepo = spec.projectFile.lexically_normal();
|
||||
const fs::path below = projectInRepo.parent_path();
|
||||
return StoreProject(std::move(fullHash), std::move(cfg), {
|
||||
return PickConfiguration(StoreProject(std::move(fullHash), std::move(configurations), {
|
||||
.projectFile = projectPath.lexically_normal(),
|
||||
.name = below.empty() ? name : std::format("{}/{}", name, below.generic_string()),
|
||||
.identity = std::format("git|{}|{}", RepositoryKey(spec.source.url), projectInRepo.generic_string()),
|
||||
});
|
||||
}), spec.configuration, what);
|
||||
}
|
||||
|
||||
fs::path Crafter::GitFetch(const GitSource& source) {
|
||||
|
|
@ -522,23 +574,21 @@ Configuration* Crafter::LocalProject(const LocalProjectSpec& spec) {
|
|||
fullKey += a;
|
||||
}
|
||||
std::string fullHash = std::format("{:016x}", std::hash<std::string>{}(fullKey));
|
||||
const std::string what = std::format("LocalProject({})", canonical.string());
|
||||
|
||||
{
|
||||
std::lock_guard lock(ProjectCacheMutex);
|
||||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||||
return it->second.get();
|
||||
}
|
||||
if (const std::vector<Configuration*>* cached = CachedProject(fullHash)) {
|
||||
return PickConfiguration(*cached, spec.configuration, what);
|
||||
}
|
||||
|
||||
fs::path depRoot = canonical.parent_path();
|
||||
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
||||
std::unique_ptr<Configuration> cfg = LoadProjectFromRoot(canonical, depRoot, argViews);
|
||||
std::vector<Configuration*> configurations = LoadProjectFromRoot(canonical, depRoot, argViews);
|
||||
|
||||
return StoreProject(std::move(fullHash), std::move(cfg), {
|
||||
return PickConfiguration(StoreProject(std::move(fullHash), std::move(configurations), {
|
||||
.projectFile = canonical,
|
||||
.name = depRoot.filename().string(),
|
||||
.identity = std::format("local|{}", canonical.string()),
|
||||
});
|
||||
}), spec.configuration, what);
|
||||
}
|
||||
|
||||
std::optional<LoadedProject> Crafter::LoadedProjectOf(const Configuration& cfg) {
|
||||
|
|
@ -546,3 +596,9 @@ std::optional<LoadedProject> Crafter::LoadedProjectOf(const Configuration& cfg)
|
|||
if (auto it = LoadedProjects.find(&cfg); it != LoadedProjects.end()) return it->second;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<LoadedProject> Crafter::KeptBy(const Configuration& cfg) {
|
||||
const std::lock_guard lock(ProjectCacheMutex);
|
||||
if (const auto it = KeptConfigurations.find(&cfg); it != KeptConfigurations.end()) return it->second;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue