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
|
|
@ -313,7 +313,7 @@ namespace {
|
|||
std::string Crafter::RunBuildCommand(std::string_view cmd) {
|
||||
// A job is on a worker already and runs the command where it is. A link,
|
||||
// or the precompile of the std module, comes from the thread of a Build()
|
||||
// or a LoadProject and waits here for its turn among the jobs.
|
||||
// or of a project file's load and waits here for its turn among the jobs.
|
||||
std::string result;
|
||||
JobPool::Process().Run([&result, cmd] { result = RunBuildStep(cmd); });
|
||||
return result;
|
||||
|
|
@ -328,7 +328,7 @@ std::string Crafter::RunBuildCommand(std::string_view cmd) {
|
|||
// file, so this is a directive next to the `import` it enables — where
|
||||
// `#include` lives. Each one is fetched, built for the host, and put on
|
||||
// project.so's module path and link line before project.cpp is compiled. The
|
||||
// dependency's own project.cpp goes through LoadProject, so its pragmas
|
||||
// dependency's own project.cpp is loaded the same way, so its pragmas
|
||||
// resolve recursively.
|
||||
namespace {
|
||||
struct PragmaDependency {
|
||||
|
|
@ -429,18 +429,18 @@ namespace {
|
|||
|
||||
struct ProjectFileFrame {
|
||||
fs::path project;
|
||||
// What LoadProject was given, for ProjectArgs(), and only once
|
||||
// CrafterBuildProject is about to be called (see LoadProject). A view
|
||||
// is enough: the caller's storage outlives LoadProject, and the frame
|
||||
// is popped before LoadProject returns.
|
||||
// What the load was given, for ProjectArgs(), and only once the
|
||||
// entry point is about to be called (see OpenProjectFile). A view
|
||||
// is enough: the caller's storage outlives the load, and the frame
|
||||
// is popped before the load returns.
|
||||
std::span<const std::string_view> args;
|
||||
std::vector<Configuration*> dependencies;
|
||||
};
|
||||
|
||||
// LoadProject nests: a dependency's project.cpp loads inside its
|
||||
// consumer's pre-pass, and GitProject inside CrafterBuildProject nests
|
||||
// again. Several threads may also load at once (ConcurrentCacheRace), so
|
||||
// the stack is per thread. ProjectDependency() reads the top frame.
|
||||
// Loads nest: a dependency's project.cpp loads inside its consumer's
|
||||
// pre-pass, and GitProject inside an entry point nests again. Several
|
||||
// threads may also load at once (ConcurrentCacheRace), so the stack is
|
||||
// per thread. ProjectDependency() reads the top frame.
|
||||
thread_local std::vector<ProjectFileFrame> LoadingFrames;
|
||||
|
||||
// A frame's dependencies, kept past the frame for PragmaDependencies().
|
||||
|
|
@ -450,7 +450,7 @@ namespace {
|
|||
std::mutex PragmaDependenciesMutex;
|
||||
std::unordered_map<fs::path, std::vector<Configuration*>> PragmaDependenciesByProject;
|
||||
|
||||
// Held for the whole of one LoadProject call. Pushing a file that is
|
||||
// Held for the whole of one load of a project file. Pushing one that is
|
||||
// already on the stack is a cycle, which used to recurse forever.
|
||||
class ProjectFileGuard {
|
||||
public:
|
||||
|
|
@ -491,7 +491,7 @@ namespace {
|
|||
|
||||
// A pre-dependency is host tooling: project.so is loaded into the running
|
||||
// crafter-build, so the dependency is built for the host and against the
|
||||
// same PCM cache LoadProject populates — the hostConfig LoadProject itself
|
||||
// same PCM cache a load populates — the hostConfig the load itself
|
||||
// compiles project.so with, so the dependency's std/Crafter.Build PCMs
|
||||
// land in the cache dir project.so is built against. Recursive, because
|
||||
// the dependency's project.cpp is loaded with no arguments and so passes
|
||||
|
|
@ -523,7 +523,7 @@ namespace {
|
|||
// Every dependency's BuildResult.libs, verbatim, then its runtimeLibs,
|
||||
// the CUDA runtime, then a run path per directory of its runPaths
|
||||
// (RunPathFlags), as Build() links a program: the shared libraries a
|
||||
// pragma dependency is built on are found there when LoadProject
|
||||
// pragma dependency is built on are found there when the load
|
||||
// dlopens project.so.
|
||||
std::string linkFlags;
|
||||
fs::file_time_type newestArtifact = fs::file_time_type::min();
|
||||
|
|
@ -548,7 +548,7 @@ namespace {
|
|||
|
||||
for (const PragmaDependency& pragma : pragmas) {
|
||||
// Neither spec sets args, and the tool still gets none of the
|
||||
// consumer's: this pre-pass runs before LoadProject hands the
|
||||
// consumer's: this pre-pass runs before the load hands the
|
||||
// frame its args, so ProjectArgs() is empty. Given the consumer's
|
||||
// --target=, the tool's project.cpp would pick sources and flags
|
||||
// for the wrong triple, and its flags would split the tool's
|
||||
|
|
@ -566,7 +566,7 @@ namespace {
|
|||
cfg = GitProject(spec);
|
||||
} else {
|
||||
if (!pragma.branch.empty() || !pragma.commit.empty()) PragmaError(absProject, pragma.line, "'branch' and 'commit' only apply to a git source");
|
||||
// Relative to the project file, not the cwd: LoadProject runs
|
||||
// Relative to the project file, not the cwd: the load runs
|
||||
// from wherever the consumer was invoked.
|
||||
fs::path local = (absProject.parent_path() / pragma.source).lexically_normal();
|
||||
if (fs::is_directory(local)) {
|
||||
|
|
@ -1067,10 +1067,32 @@ namespace {
|
|||
}
|
||||
}
|
||||
|
||||
Configuration Crafter::LoadProject(const fs::path& projectFile, std::span<const std::string_view> args) {
|
||||
RequireToolchain();
|
||||
fs::path absProject = fs::canonical(projectFile);
|
||||
ProjectFileGuard guard(absProject);
|
||||
namespace {
|
||||
using ProjectFn = Configuration (*)(std::span<const std::string_view>);
|
||||
using ConfigurationsFn = std::vector<Configuration*> (*)(std::span<const std::string_view>);
|
||||
|
||||
// The entry point of a loaded project file: one of the two is set.
|
||||
struct ProjectEntry {
|
||||
ProjectFn one = nullptr;
|
||||
ConfigurationsFn several = nullptr;
|
||||
};
|
||||
|
||||
// Every Configuration a CrafterBuildProject returned to
|
||||
// LoadConfigurations, kept for the rest of the process like the ones a
|
||||
// CrafterBuildConfigurations returns, which its project file keeps:
|
||||
// whoever holds a pointer LoadConfigurations gave out, the project cache
|
||||
// or a consumer's `dependencies`, holds it that long.
|
||||
std::mutex ReturnedConfigurationsMutex;
|
||||
std::vector<std::unique_ptr<Configuration>> ReturnedConfigurations;
|
||||
}
|
||||
|
||||
// Compiles the project file `guard` holds into project.so, unless the one
|
||||
// there is current, loads it and finds its entry point. The frame holds
|
||||
// `args` once this returns, for the call the caller makes next.
|
||||
static ProjectEntry OpenProjectFile(ProjectFileGuard& guard, std::span<const std::string_view> args) {
|
||||
// A copy: the loads of the file's dependencies push frames of their own,
|
||||
// which can move this one.
|
||||
const fs::path absProject = guard.Frame().project;
|
||||
fs::path buildDir = absProject.parent_path() / "build";
|
||||
if (!fs::exists(buildDir)) {
|
||||
fs::create_directories(buildDir);
|
||||
|
|
@ -1154,11 +1176,20 @@ Configuration Crafter::LoadProject(const fs::path& projectFile, std::span<const
|
|||
throw std::runtime_error(std::format("Failed to load project {}: {}", soPath.string(), dlerror()));
|
||||
}
|
||||
|
||||
using ProjectFn = Configuration (*)(std::span<const std::string_view>);
|
||||
dlerror();
|
||||
auto fn = reinterpret_cast<ProjectFn>(dlsym(handle, "CrafterBuildProject"));
|
||||
if (const char* err = dlerror()) {
|
||||
throw std::runtime_error(std::format("CrafterBuildProject not found in {}: {}", soPath.string(), err));
|
||||
// Looked up in project.so and the libraries it links, which is where
|
||||
// dlsym searches for a handle: every project file this process has
|
||||
// loaded exports an entry point under one of these two names, and none
|
||||
// of the others is found here.
|
||||
ProjectEntry entry;
|
||||
entry.one = reinterpret_cast<ProjectFn>(dlsym(handle, "CrafterBuildProject"));
|
||||
entry.several = reinterpret_cast<ConfigurationsFn>(dlsym(handle, "CrafterBuildConfigurations"));
|
||||
if (!entry.one && !entry.several) {
|
||||
throw std::runtime_error(std::format("{} defines neither CrafterBuildProject nor CrafterBuildConfigurations (looked in {})", absProject.string(), soPath.string()));
|
||||
}
|
||||
// Calling the first would leave what the second returns unbuilt, and
|
||||
// the other way around, without a word.
|
||||
if (entry.one && entry.several) {
|
||||
throw std::runtime_error(std::format("{} defines both CrafterBuildProject and CrafterBuildConfigurations; a project file has one entry point", absProject.string()));
|
||||
}
|
||||
|
||||
// The args only for the call itself, unlike the dependencies. A static
|
||||
|
|
@ -1168,5 +1199,67 @@ Configuration Crafter::LoadProject(const fs::path& projectFile, std::span<const
|
|||
// Now it gets none, whichever load ran it. The pragma pre-pass above
|
||||
// also ran on this frame and relies on the args not being there yet.
|
||||
guard.Frame().args = args;
|
||||
return fn(args);
|
||||
return entry;
|
||||
}
|
||||
|
||||
Configuration Crafter::LoadProject(const fs::path& projectFile, std::span<const std::string_view> args) {
|
||||
RequireToolchain();
|
||||
const fs::path absProject = fs::canonical(projectFile);
|
||||
ProjectFileGuard guard(absProject);
|
||||
const ProjectEntry entry = OpenProjectFile(guard, args);
|
||||
if (!entry.one) {
|
||||
throw std::runtime_error(std::format("{} defines CrafterBuildConfigurations, which LoadProject cannot return by value; load it with LoadConfigurations", absProject.string()));
|
||||
}
|
||||
return entry.one(args);
|
||||
}
|
||||
|
||||
std::vector<Configuration*> Crafter::LoadConfigurations(const fs::path& projectFile, std::span<const std::string_view> args) {
|
||||
RequireToolchain();
|
||||
const fs::path absProject = fs::canonical(projectFile);
|
||||
ProjectFileGuard guard(absProject);
|
||||
const ProjectEntry entry = OpenProjectFile(guard, args);
|
||||
if (entry.one) {
|
||||
// The call's return value is the initialiser of the new-expression,
|
||||
// so the Configuration is constructed where it stays and is never
|
||||
// moved; std::make_unique would move a temporary into place. 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.
|
||||
std::unique_ptr<Configuration> returned(new Configuration(entry.one(args)));
|
||||
Configuration* const configuration = returned.get();
|
||||
const std::lock_guard lock(ReturnedConfigurationsMutex);
|
||||
ReturnedConfigurations.push_back(std::move(returned));
|
||||
return { configuration };
|
||||
}
|
||||
|
||||
std::vector<Configuration*> configurations = entry.several(args);
|
||||
if (configurations.empty()) {
|
||||
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned no configuration", absProject.string()));
|
||||
}
|
||||
std::unordered_set<const Configuration*> seen;
|
||||
for (const Configuration* configuration : configurations) {
|
||||
if (!configuration) {
|
||||
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned a null pointer", absProject.string()));
|
||||
}
|
||||
// Most often a pasted `&lib` where `&tool` was meant, which would
|
||||
// leave `tool` unbuilt without a word.
|
||||
if (!seen.insert(configuration).second) {
|
||||
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned {} twice", absProject.string(), configuration->name));
|
||||
}
|
||||
// One that a project GitProject or LocalProject loaded keeps to
|
||||
// itself, taken out of the `dependencies` or the `tests` of what
|
||||
// that project's file returned. Returned from here it would count
|
||||
// as one this file
|
||||
// made: `test` would run its tests and `run` offer its scripts,
|
||||
// which are that project's. And only the configurations a project
|
||||
// file returns are rebased onto its directory, so this one would be
|
||||
// built wherever its relative `path` leads from here.
|
||||
if (const std::optional<LoadedProject> keeper = KeptBy(*configuration)) {
|
||||
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned {}, which {} keeps to itself: a project file returns the configurations it makes and the ones GitProject or LocalProject returned", absProject.string(), configuration->name, keeper->name));
|
||||
}
|
||||
}
|
||||
return configurations;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue