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
|
|
@ -26,8 +26,9 @@ The execution contexts to enumerate, beside the charter's:
|
|||
jobs it was added after have finished; a `RunBuildCommand` waits for a
|
||||
worker when its caller is not one. Jobs are waited for by threads that
|
||||
are not workers, among them one that runs a `Build()` (its caller's, one
|
||||
of `RunTests`', one a `Build()` starts per dependency) and the one that
|
||||
runs `LoadProject`, for the compiles of the std module and of
|
||||
of `RunTests`', one of `BuildAll`'s, one a `Build()` starts per
|
||||
dependency) and the one that loads a project file (`LoadConfigurations`,
|
||||
`LoadProject`), for the compiles of the std module and of
|
||||
`project.cpp`. The threads a `Build()` starts per external dependency are
|
||||
not workers either and wait for no job: they run git and cmake. An
|
||||
executable's copies of what its dependencies forward run on the thread of
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ Guidance for coding agents and contributors working on crafter-build itself. The
|
|||
|
||||
## Project overview
|
||||
|
||||
crafter-build is a C++26 modules build system. A project is described by a `project.cpp` that returns a `Crafter::Configuration`; the tool compiles that file to a shared object, loads it in-process and builds what it describes. This repository is self-hosting: its own `project.cpp` builds crafter-build with crafter-build, and its tests link the library and drive `Build()` in-process.
|
||||
crafter-build is a C++26 modules build system. A project is described by a `project.cpp` that returns a `Crafter::Configuration`, or several of them; the tool compiles that file to a shared object, loads it in-process and builds what it describes. This repository is self-hosting: its own `project.cpp` builds crafter-build with crafter-build, and its tests link the library and drive `Build()` in-process.
|
||||
|
||||
| Path | What lives there |
|
||||
|---|---|
|
||||
|
|
@ -25,14 +25,14 @@ Partitions and what they own:
|
|||
|
||||
- `:Toolchain` resolves the compiler once per process (`GetToolchain`): clang++, clang, llvm-ar, llvm-readelf, libclang, libc++'s std module source and the default sysroots, from `CRAFTER_BUILD_LLVM_DIR`, else a toolchain installed beside crafter-build, else PATH. It also holds the version baked in at build time, and the arch and branch of the Flatpak runtime crafter-build runs on, from `/.flatpak-info` or the file `CRAFTER_BUILD_FLATPAK_INFO` names, and the nvcc `cfg.cuda` compiles with: `$CUDA_PATH/bin/nvcc`, else `bin/nvcc` under `/opt/cuda` or `/usr/local/cuda`, else the first in an absolute entry of PATH, which it searches itself, and `cudaRuntime`, the `lib64/libcudart_static.a` beside that nvcc's `bin` where there is one, which a configuration with CUDA sources links. In the Flatpak the host's `/usr` and `/etc` are under `/run/host`, and a link to an absolute path names a host path, so where the path of a toolkit is a link, it follows that link, and the link its target is in turn, each as the host's (`HostPath`); a link in a directory partway along that path, or inside the toolkit, is the kernel's to resolve, in the sandbox's root. `SysrootStdModule` picks the std module source for a sysroot build by the sysroot's libc++ release. `MingwSysroot` settles the mingw target's `--sysroot`: the mingw-w64 prefix itself, or, for a prefix that also holds a Linux system (the host's `/usr`, anything whose `include/` has `features.h`), a view in the cache of its mingw-w64 directories, since clang would search its `include/` and `lib/` too (issue #43). `MingwViewToPrefix` turns paths into the view back into paths into the prefix for messages.
|
||||
- `:Clang` holds `Configuration`, `Build()`, `VariantId()`, `ApplyStandardArgs`, `HostTarget`, the CLI entry point `Crafter::Run`, and the lint, test and script data types.
|
||||
- `:Platform` runs commands, implements `LoadProject` (compile and load `project.cpp`), the host PCM cache, `#pragma crafter dependency`, and all OS-specific code.
|
||||
- `:Platform` runs commands, implements `LoadConfigurations` and `LoadProject` (compile and load `project.cpp`), the host PCM cache and `#pragma crafter dependency`.
|
||||
- `:Jobs` is what build steps run on: `JobPool`, worker threads behind one queue, started as jobs need them up to a fixed number, with `JobPool::Process()` the pool of at most one worker per hardware thread that `Build()` and `RunBuildCommand` use, and `JobGroup`, the jobs of one configuration's build, each started once the jobs it was added after have finished, with the first error among them and what `Build()` reports to it through `Fail()`.
|
||||
- `:Interface` and `:Implementation` model module units, their staleness checks and the jobs that compile them (`Schedule`).
|
||||
- `:External` handles git + cmake dependencies, `GitProject` and `LocalProject`.
|
||||
- `:Test` holds `AddTest`, `RunTests` and `TestRunner`.
|
||||
- `:Lint` is the lint/format driver plus the libclang token and AST layer.
|
||||
- `:Script` is the driver behind `crafter-build run`: it looks a name up in `cfg.scripts`, calls the callback and turns what it returns into the exit code.
|
||||
- `:Agents` writes a project's AGENTS.md from its AGENTS.template.md: it walks the dependencies `cfg.dependencyNotes` selects, reads each one's `AGENTS.consumer.md` and merges them into the template's dependency notes section. Which Configurations belong to another project, and where it lives, comes from the loader (`LoadedProjectOf` in `:External`, `PragmaDependencies` in `:Platform`), because only the Configuration `GitProject` and `LocalProject` return has its `path` rebased.
|
||||
- `:Script` is the driver behind `crafter-build run`: it looks a name up in the `scripts` of the configurations it is given, calls the callback and turns what it returns into the exit code.
|
||||
- `:Agents` writes a project's AGENTS.md from its AGENTS.template.md: it walks the dependencies `cfg.dependencyNotes` selects, reads each one's `AGENTS.consumer.md` and merges them into the template's dependency notes section. Which Configurations belong to another project, and where it lives, comes from the loader (`LoadedProjectOf` in `:External`, `PragmaDependencies` in `:Platform`), because only the Configurations returned by a project file that `GitProject` or `LocalProject` loaded have their `path` rebased.
|
||||
- `:Progress` is console output. `:Shader` compiles shaders through glslang. `:Asset` is the hook into Crafter.Asset.
|
||||
- `:Flatpak` is an executable packaged as a Flatpak: `FlatpakApp` (app-id, runtime, permissions, desktop file, metainfo, icons, share entries, modules), the writer of its `files/`, which becomes `/app` in the app's sandbox, and what makes the app of that for `install` and `bundle`: the host's `flatpak build-finish`, which writes the metadata and `export/` by flatpak's own rules, then `flatpak build-export` into a repo, or flatpak-builder from a generated manifest when there are modules. It works on plain data; `Build()` and the `InstallFlatpakApp` and `BundleFlatpakApp` of `:Clang` gather it from the Configuration. The host's flatpak and flatpak-builder are reached through `RunHostFlatpak` and `RunHostFlatpakBuilder` in `:Platform`.
|
||||
|
||||
|
|
@ -51,7 +51,7 @@ Three things to know about this loop:
|
|||
|
||||
- `bin/crafter-build` is the bootstrap binary and the harness for every command below. The self-host build writes to `bin/crafter.build-exe-<variant>/crafter-build` and does not replace it.
|
||||
- Changes under `implementations/` are picked up by the self-host build and by the tests, which link the local library. Changes under `interfaces/` are not: the harness compiles against the copies in `share/crafter-build/` and the PCM cache built from them, so a new or changed declaration is invisible until you run `./build.sh` again. The symptom is `use of undeclared identifier` for the symbol you just added. A changed default member initialiser has no symptom at all: `project.cpp` compiles against the old one and quietly gets the old default, as `project.cpp`'s sibling loads do with a harness that predates `GitProjectSpec::args = ProjectArgs()`.
|
||||
- A new exported symbol that a `project.cpp` reaches on its default path also needs `./build.sh`: the old binary does not export it, and `LoadProject` fails with an undefined symbol.
|
||||
- A new exported symbol that a `project.cpp` reaches on its default path also needs `./build.sh`: the old binary does not export it, and loading `project.cpp` fails with an undefined symbol.
|
||||
|
||||
`./build.sh` clones and builds glslang on first run and takes several minutes; later runs are faster. `./bin/crafter-build clean` deletes `bin/` and `build/` without loading `project.cpp`. That takes `bin/crafter-build` itself with it, and `build/flatpak`, the state of the local Flatpak build (see "CI and publishing"): about 4 GB after a first run, and the next `run flatpak` rebuilds LLVM. Set `CRAFTER_BUILD_HOME` only when running a binary from outside this tree; `CRAFTER_BUILD_MARCH` and `CRAFTER_BUILD_MTUNE` default to `native`.
|
||||
|
||||
|
|
@ -81,7 +81,7 @@ Tests are declared, not discovered. Each one is a line in `project.cpp`:
|
|||
cfg.AddTest("IncrementalHeaderChange").Dependencies({ CrafterBuildLib.get() });
|
||||
```
|
||||
|
||||
The source is `tests/<Name>/main.cpp`. The test inherits target, march and the rest from the root configuration and links the local `crafter.build-lib`, so `import Crafter.Build;` resolves to the code in this checkout. A test that calls `LoadProject` also needs `.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl")`, as the existing ones show. The declarations sit in an `if (linuxTarget)` block that matches any `*-linux-gnu` or `*-linux-musl` target: the suite is Linux-host only today.
|
||||
The source is `tests/<Name>/main.cpp`. The test inherits target, march and the rest from the root configuration and links the local `crafter.build-lib`, so the engine it calls is the one built from this checkout. A test that loads a project file, through `LoadConfigurations`, `LoadProject`, `LocalProject`, `GitProject` or `Run()`, also needs `.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl")`, as the existing ones show. The declarations sit in an `if (linuxTarget)` block that matches any `*-linux-gnu` or `*-linux-musl` target: the suite is Linux-host only today.
|
||||
|
||||
A test is an ordinary `int main()`. Exit 0 passes, nonzero fails, 77 skips (for a missing tool, never for a failed assertion). Each runs in its own subprocess, in parallel, with a 60 s default timeout.
|
||||
|
||||
|
|
@ -159,7 +159,7 @@ These were decided deliberately. Do not undo them without the maintainer's say-s
|
|||
- `project.cpp` is compiled and loaded into the crafter-build process, and every `GitProject`, `#pragma crafter dependency` and `ExternalDependency` clones a repository and runs its build on the host. Treat a dependency URL as code you are choosing to run. This repo's own dependencies are sibling Catcrafts repos tracking their default branch on purpose; pin with `commit` when adding anything else, and whenever reproducibility matters.
|
||||
- Unpinned dependencies `git pull` on every invocation, so builds and tests need network. A missing or private repo surfaces as a clone failure before anything is built.
|
||||
- `--runner=cmd:<command>` executes the named wrapper around every test binary. Wrappers come from `--runner=` or `CRAFTER_BUILD_RUNNER_*`; do not hardcode hosts, wrappers or credentials in `project.cpp` or tests.
|
||||
- `crafter-build run <name>` executes whatever the script does, with your permissions. Only the scripts of the project crafter-build was pointed at can be run, never a dependency's; keep it that way.
|
||||
- `crafter-build run <name>` executes whatever the script does, with your permissions. Only the scripts of the project crafter-build was pointed at can be run, never a dependency's: `run` leaves out a configuration that another project's file returned and the project file returns in turn, and the load refuses one that another project keeps to itself (`KeptBy`); keep it that way.
|
||||
- The linter loads `libclang` at runtime by name. `CRAFTER_BUILD_LIBCLANG` pins an exact path and must fail loudly when wrong, never fall back to another library.
|
||||
- crafter-build's Flatpak has `--talk-name=org.freedesktop.Flatpak`, and the permission is the whole sandbox's: anything that runs there can run commands on the host through `flatpak-spawn --host`. That is every process of crafter-build's sandbox: crafter-build itself, every `project.cpp` it loads with its scripts, the builds of its dependencies and pragma dependencies, the programs `-r` and `test` start, and whatever `flatpak run --command=` and `flatpak enter` start in it, the debuggers of the README's Debugging section and the programs under them included. crafter-build's own code uses it only through `HostCommand` in `:Platform`, which builds the command lines of `RunHostFlatpak`, `RunHostFlatpakBuilder` and `HostFlatpakUnavailable`'s probes, for the host's `flatpak` and `flatpak-builder` behind `install` and `bundle`; add no other host command to crafter-build. `RunHostFlatpak` is exported for the tests, and gives a `project.cpp` nothing the sandbox does not. The trust model allows the permission because `--filesystem=host` already lets sandboxed code write `~/.bashrc`, `~/.config/autostart` and `~/.config/systemd/user`: it makes running something on the host immediate, not newly possible, for everything the sandbox runs.
|
||||
- CI creates a release for each release tag with the workflow's own token, pushes nothing to git, and signs the Flatpak repo with the key mounted from the runner host. Do not add secrets to the workflow or echo the environment in it.
|
||||
|
|
@ -198,8 +198,9 @@ Local builds are two scripts in `project.cpp`. Run them with the native binary:
|
|||
|
||||
- A new partition is listed in four places, in import order: `PARTITIONS` in `build.sh`, `CrafterBuildModules` in `Crafter.Build-Platform.cpp`, both arrays in `project.cpp`, and the umbrella `Crafter.Build.cppm`. Then `./build.sh`.
|
||||
- A `#pragma crafter dependency` target is rebuilt on every load on purpose: clang crashes rather than erroring when it imports a BMI whose own imported PCM was rebuilt underneath it, and the depfile check is what catches a regenerated host cache. Do not optimise the rebuild away.
|
||||
- A host-tooling library such as Crafter.LintRules may `import Crafter.Build;` without declaring it as a dependency, because `LoadProject` fills the cache it resolves from. Listing Crafter.Build in its `cfg.dependencies` would clone and build all of crafter-build for every consumer. Its test does link crafter-build, so that test is wired behind `IsTopLevelProject()`: without the guard, crafter-build loading the library as a dependency would load its own `project.cpp` again through the test's `GitProject` and hit the cycle guard.
|
||||
- A host-tooling library such as Crafter.LintRules may `import Crafter.Build;` without declaring it as a dependency, because loading a project file fills the cache it resolves from. Listing Crafter.Build in its `cfg.dependencies` would clone and build all of crafter-build for every consumer. Its test does link crafter-build, so that test is wired behind `IsTopLevelProject()`: without the guard, crafter-build loading the library as a dependency would load its own `project.cpp` again through the test's `GitProject` and hit the cycle guard.
|
||||
- `crafter-build clean` leaves a pragma dependency's build alone; it lives with the clone in the cache.
|
||||
- A project file returns one configuration from `CrafterBuildProject` or several from `CrafterBuildConfigurations`, and `Crafter::Run` works on the list `LoadConfigurations` gives it for both. A new verb has to say what it does with several, and whether it takes all of them or leaves out another project's: `test` and `run` leave out a configuration that another project's file returned and this one returns in turn, which `LoadedProjectOf` tells apart. The load has refused, before a verb sees the list, a returned configuration that a loaded project keeps to itself (`KeptBy`): one the loader took down when that project's file was loaded, among what the configurations the file returned led to through `dependencies` and `tests`, as far as the first configuration on each way that a loaded project's file had returned or another loaded project keeps.
|
||||
- The limit of as many steps at once as there are hardware threads holds for what runs on `JobPool::Process()`: the jobs `Build()` adds and the commands of `RunBuildCommand`. Among what runs outside it: an `ExternalDependency`'s `cmake --build`, which `BuildCMake` gives `--parallel <hardware threads>` of its own; the tests `RunTests` runs, `--jobs=` of them at once on threads of its own, while other tests may still be building; the Crafter.Build modules `EnsureCrafterBuildPcms` precompiles for the host cache, one after another; and the threads a step starts for itself, lld's for a link and, for an asset, those of the GDeflate compressor in Crafter.Asset, up to 31 for each stream it compresses. The pool is one process's: a self-test of this repository that builds a fixture does so in its own process, on a pool of its own.
|
||||
- An interface unit takes two compiler runs, one for its BMI and one for the object made from the BMI, and they are two jobs so that the units importing it start once the first is done. `Check` reads staleness off the BMI; the object is `Schedule`'s to look at, which adds the object job alone for a unit whose BMI is up to date and whose object is missing or older than the BMI. A build that fails between the two jobs leaves a unit like that, since no job starts after a failure.
|
||||
- The source scanner follows an `import` it reads in a string literal or under `#if 0` like any other (issue #45), so interface units can import each other by its account alone. `ImportCycleError` refuses such a round, naming the units, at the top of the `Build()` of the configuration whose units form it: before that configuration makes a directory or builds a dependency, though a configuration built on it has started its own build by then.
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -479,6 +479,21 @@ CompileCommand Crafter::GetCompileCommand(const Configuration& config) {
|
|||
}
|
||||
|
||||
namespace {
|
||||
// Two of `configurations` that would be built into one directory, as
|
||||
// the error that names them; empty when no two would. Built side by
|
||||
// side they are two builds writing the same files, and a pass that has
|
||||
// built one of them hands it to whatever is built on the other.
|
||||
std::string SameDirectoryError(std::span<Configuration* const> configurations) {
|
||||
std::unordered_map<fs::path, const Configuration*> builtInto;
|
||||
for (const Configuration* cfg : configurations) {
|
||||
const auto [known, added] = builtInto.try_emplace(fs::absolute(cfg->PcmDir()).lexically_normal(), cfg);
|
||||
if (!added && known->second != cfg) {
|
||||
return std::format("two configurations named {} would both be built into {}: give each a name of its own", cfg->name, known->first.string());
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// The outcome of `work` for `key` in this build pass. The first caller
|
||||
// runs it; every other caller, whether at the same time or later, waits
|
||||
// for that run and gets its result or its exception. The keys are the
|
||||
|
|
@ -1667,6 +1682,175 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
|
|||
return buildResult;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// How BuildAll builds the configurations of a list, or why it does not:
|
||||
// worked out from the list alone, before anything of it is built.
|
||||
struct BuildPlan {
|
||||
// Each of them once, however often the list names it.
|
||||
std::vector<Configuration*> roots;
|
||||
// For each of `roots`, the others it is built after, by their places
|
||||
// in `roots`.
|
||||
std::vector<std::vector<std::size_t>> waitsFor;
|
||||
// The places in `roots`, each after the ones it waits for.
|
||||
std::vector<std::size_t> order;
|
||||
// Whether a wasm executable that declares variants is among them or
|
||||
// among what they are built on, so that they are built one after the
|
||||
// other.
|
||||
bool rewrites = false;
|
||||
// Why the list is not built, as BuildAll returns it; empty when it
|
||||
// is.
|
||||
std::string refused;
|
||||
};
|
||||
|
||||
BuildPlan PlanBuild(std::span<Configuration* const> configurations) {
|
||||
BuildPlan plan;
|
||||
for (Configuration* cfg : configurations) {
|
||||
if (!std::ranges::contains(plan.roots, cfg)) plan.roots.push_back(cfg);
|
||||
}
|
||||
const std::span<Configuration* const> roots = plan.roots;
|
||||
const std::size_t count = roots.size();
|
||||
const auto directory = [](const Configuration& cfg) { return fs::absolute(cfg.PcmDir()).lexically_normal(); };
|
||||
|
||||
// The directory each of them is built into, and the directories of
|
||||
// everything it is built on. A wasm executable that declares
|
||||
// variants builds everything it is built on once more for each of
|
||||
// them, and says which variant by rewriting those configurations in
|
||||
// place (the end of Build()). A library that two of these share
|
||||
// would change under whichever is still building its baseline, so
|
||||
// with such a configuration anywhere among them they are built one
|
||||
// after the other.
|
||||
std::vector<fs::path> builtInto(count);
|
||||
std::vector<std::unordered_set<fs::path>> builtOn(count);
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
builtInto[index] = directory(*roots[index]);
|
||||
std::unordered_set<const Configuration*> seen = { roots[index] };
|
||||
std::vector<const Configuration*> pending = { roots[index] };
|
||||
while (!pending.empty()) {
|
||||
const Configuration& cfg = *pending.back();
|
||||
pending.pop_back();
|
||||
if (cfg.type == ConfigurationType::Executable && cfg.target.starts_with("wasm32") && !cfg.wasmVariants.empty()) plan.rewrites = true;
|
||||
for (const Configuration* dep : cfg.dependencies) {
|
||||
if (!seen.insert(dep).second) continue;
|
||||
builtOn[index].insert(directory(*dep));
|
||||
pending.push_back(dep);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
plan.refused = SameDirectoryError(roots);
|
||||
if (!plan.refused.empty()) return plan;
|
||||
|
||||
// One that another is built on, which is to say that something the
|
||||
// other is built on is built into its directory, is built before
|
||||
// the other starts: the other then finds it built, in the record of
|
||||
// the pass, instead of building it a second time beside this build
|
||||
// of it.
|
||||
plan.waitsFor.resize(count);
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
for (std::size_t other = 0; other < count; ++other) {
|
||||
if (other != index && builtOn[index].contains(builtInto[other])) plan.waitsFor[index].push_back(other);
|
||||
}
|
||||
}
|
||||
// One that is itself built on something of its own directory makes
|
||||
// two builds for that directory, its dependency's and its own, and
|
||||
// the record of the pass holds one result for a directory: another
|
||||
// of them that looks there for what it is built on would be given
|
||||
// one of the two, whichever it had asked for. By itself, or with no
|
||||
// other of them built on that directory, it builds as it does for
|
||||
// Build().
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
if (!builtOn[index].contains(builtInto[index])) continue;
|
||||
for (std::size_t other = 0; other < count; ++other) {
|
||||
if (other == index || !builtOn[other].contains(builtInto[index])) continue;
|
||||
plan.refused = std::format("{} and a configuration it is built on would both be built into {}, where {} looks for what it is built on: give each a name of its own", roots[index]->name, builtInto[index].string(), roots[other]->name);
|
||||
return plan;
|
||||
}
|
||||
}
|
||||
std::vector<bool> ordered(count, false);
|
||||
for (bool placed = true; placed;) {
|
||||
placed = false;
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
if (ordered[index] || !std::ranges::all_of(plan.waitsFor[index], [&](std::size_t other) { return ordered[other]; })) continue;
|
||||
ordered[index] = true;
|
||||
plan.order.push_back(index);
|
||||
placed = true;
|
||||
}
|
||||
}
|
||||
if (plan.order.size() != count) {
|
||||
std::string names;
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
if (ordered[index]) continue;
|
||||
if (!names.empty()) names += ", ";
|
||||
names += roots[index]->name;
|
||||
}
|
||||
plan.refused = std::format("{}: each of these is built on something that would be built into the directory of another of them, so none of them can be built first", names);
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Crafter::BuildAll(std::span<Configuration* const> configurations, std::unordered_map<fs::path, std::shared_future<BuildResult>>& depResults, std::mutex& depMutex) {
|
||||
const BuildPlan plan = PlanBuild(configurations);
|
||||
if (!plan.refused.empty()) return plan.refused;
|
||||
const std::size_t count = plan.roots.size();
|
||||
|
||||
std::vector<std::string> errors(count);
|
||||
const auto build = [&](std::size_t index) {
|
||||
Configuration& cfg = *plan.roots[index];
|
||||
// By Build() itself, as a project's one configuration always was,
|
||||
// and only then put into the record of the pass, where a
|
||||
// configuration that depends on this one looks for it, with what
|
||||
// the build returned or threw. Recorded while it is still being
|
||||
// built, it would be handed to a dependency of its own that is
|
||||
// built into the same directory, which would wait for the build
|
||||
// that is waiting for it. Such a dependency has left its own result
|
||||
// under that directory by now, and this one's takes its place: no
|
||||
// other of them is built on that directory, or the set was refused.
|
||||
std::promise<BuildResult> built;
|
||||
try {
|
||||
BuildResult result = Build(cfg, depResults, depMutex);
|
||||
errors[index] = result.result;
|
||||
built.set_value(std::move(result));
|
||||
} catch (const std::exception& e) {
|
||||
errors[index] = std::format("build of {} threw: {}", cfg.name, e.what());
|
||||
built.set_exception(std::current_exception());
|
||||
} catch (...) {
|
||||
errors[index] = std::format("build of {} threw something that is not a std::exception", cfg.name);
|
||||
built.set_exception(std::current_exception());
|
||||
}
|
||||
const std::lock_guard lock(depMutex);
|
||||
depResults.insert_or_assign(cfg.PcmDir(), built.get_future().share());
|
||||
};
|
||||
|
||||
if (plan.rewrites) {
|
||||
for (const std::size_t index : plan.order) build(index);
|
||||
} else {
|
||||
std::vector<std::promise<void>> finishes(count);
|
||||
std::vector<std::shared_future<void>> finished;
|
||||
finished.reserve(count);
|
||||
for (std::promise<void>& finish : finishes) finished.push_back(finish.get_future().share());
|
||||
const auto buildInTurn = [&](std::size_t index) {
|
||||
for (const std::size_t other : plan.waitsFor[index]) finished[other].wait();
|
||||
build(index);
|
||||
finishes[index].set_value();
|
||||
};
|
||||
// The first on this thread, so a project of one configuration is
|
||||
// built where it always was.
|
||||
std::vector<std::jthread> others;
|
||||
others.reserve(count);
|
||||
for (std::size_t index = 1; index < count; ++index) others.emplace_back(buildInTurn, index);
|
||||
if (count > 0) buildInTurn(0);
|
||||
}
|
||||
|
||||
std::string failed;
|
||||
for (auto error = errors.begin(); error != errors.end(); ++error) {
|
||||
if (error->empty() || std::ranges::contains(errors.begin(), error, *error)) continue;
|
||||
if (!failed.empty()) failed += '\n';
|
||||
failed += *error;
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
|
||||
namespace {
|
||||
FlatpakExport ExportOf(const Configuration& config) {
|
||||
FlatpakExport app;
|
||||
|
|
@ -1700,6 +1884,41 @@ namespace {
|
|||
return "";
|
||||
}
|
||||
|
||||
// The names of `configurations`, as a message lists them.
|
||||
std::string NamesOf(std::span<const Configuration* const> configurations) {
|
||||
std::string names;
|
||||
for (const Configuration* cfg : configurations) {
|
||||
if (!names.empty()) names += ", ";
|
||||
names += cfg->name;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
// What install or bundle exports of the configurations a project file
|
||||
// returns: the only one, or of several the one packaged as a Flatpak.
|
||||
struct ExportChoice {
|
||||
// Null when none of several is packaged, or more than one is.
|
||||
const Configuration* config = nullptr;
|
||||
// Why `config` cannot be exported, or why there is none; empty when
|
||||
// it can be.
|
||||
std::string refused;
|
||||
};
|
||||
|
||||
ExportChoice ChooseExport(std::span<Configuration* const> configurations, std::string_view verb) {
|
||||
std::vector<const Configuration*> packaged;
|
||||
for (const Configuration* cfg : configurations) {
|
||||
if (cfg->ResolvedPackaging() == Packaging::Flatpak) packaged.push_back(cfg);
|
||||
}
|
||||
if (packaged.size() > 1) {
|
||||
return { nullptr, std::format("crafter-build {0}: {1} are each packaged as a Flatpak, and {0} exports one app", verb, NamesOf(packaged)) };
|
||||
}
|
||||
if (packaged.empty() && configurations.size() > 1) {
|
||||
return { nullptr, std::format("crafter-build {0}: none of {1} is packaged as a Flatpak; set cfg.packaging = Packaging::Flatpak in project.cpp on the one to {0}", verb, NamesOf(configurations)) };
|
||||
}
|
||||
const Configuration* const config = packaged.empty() ? configurations.front() : packaged.front();
|
||||
return { config, ExportRefusal(*config, verb) };
|
||||
}
|
||||
|
||||
// A repo's path as the file:// URL of a remote: percent-encoded, since
|
||||
// `remote-modify --url=` stores what it is given as it is.
|
||||
std::string FileUrl(const fs::path& path) {
|
||||
|
|
@ -2151,7 +2370,9 @@ R"(Usage:
|
|||
Loads ./project.cpp (override with --project=<path>), compiles it to a shared
|
||||
object, and invokes its CrafterBuildProject() to obtain a Configuration that
|
||||
drives the build. Outputs land at bin/<name>-<target>-<march>/, intermediates
|
||||
at build/<name>-<target>-<march>/.
|
||||
at build/<name>-<target>-<march>/. A project of several configurations
|
||||
defines CrafterBuildConfigurations() instead, which returns them all and gets
|
||||
the same args, and every one of them is built.
|
||||
|
||||
A build runs at most as many of its steps at once as the machine has hardware
|
||||
threads: the compiles of its sources, its shaders and assets, and its links
|
||||
|
|
@ -2165,7 +2386,8 @@ Build options:
|
|||
one for the host in its own directory, a wasm32 one
|
||||
under wasmtime or wasmer, or one built for the
|
||||
browser served over HTTP from its directory.
|
||||
Libraries cannot be run.
|
||||
Libraries cannot be run, and of several
|
||||
configurations one has to be the only executable.
|
||||
-v, --verbose Verbose progress output.
|
||||
-q, --quiet Suppress progress output.
|
||||
--project=<path> Path to the project file (default: ./project.cpp).
|
||||
|
|
@ -2244,7 +2466,9 @@ Install and bundle (after the `install` or `bundle` subcommand):
|
|||
run the host's flatpak, inside crafter-build's Flatpak through
|
||||
flatpak-spawn --host, and refuse a project whose bin/ and build/ the host
|
||||
does not see where crafter-build does, as in the sandbox's own /tmp, /var,
|
||||
/usr, /etc and /app, which they ask the host before building.
|
||||
/usr, /etc and /app, which they ask the host before building. Of several
|
||||
configurations they export the one that is packaged as a Flatpak, and
|
||||
refuse before building when none is, or more than one.
|
||||
|
||||
Clean (after the `clean` subcommand):
|
||||
Removes bin/ and build/ next to the project file. Does not load project.cpp,
|
||||
|
|
@ -2263,14 +2487,14 @@ AGENTS.md:
|
|||
Direct, or None.
|
||||
|
||||
Project args:
|
||||
Any flag not consumed above is forwarded verbatim to CrafterBuildProject as
|
||||
part of its `args` span. Project-specific flags (e.g. --target=, custom
|
||||
feature toggles) live there. ApplyStandardArgs reads --debug, --target=,
|
||||
--march=, --mtune=, --cuda-arch=, --sysroot=, --lib, --shared and
|
||||
--packaging=none|flatpak, the last for the project crafter-build was pointed
|
||||
at only. Flags it does not itself interpret are folded into the variant
|
||||
hash, so switching one lands in its own bin/ and build/ directory instead of
|
||||
overwriting the other setting's.
|
||||
Any flag not consumed above is forwarded verbatim to the project file's
|
||||
entry point as part of its `args` span. Project-specific flags (e.g.
|
||||
--target=, custom feature toggles) live there. ApplyStandardArgs reads
|
||||
--debug, --target=, --march=, --mtune=, --cuda-arch=, --sysroot=, --lib,
|
||||
--shared and --packaging=none|flatpak, the last for the project
|
||||
crafter-build was pointed at only. Flags it does not itself interpret are
|
||||
folded into the variant hash, so switching one lands in its own bin/ and
|
||||
build/ directory instead of overwriting the other setting's.
|
||||
|
||||
Environment:
|
||||
CRAFTER_BUILD_MARCH Override -march (default: native).
|
||||
|
|
@ -2436,7 +2660,7 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Ahead of LoadProject on purpose — see CleanProject.
|
||||
// Ahead of the load on purpose — see CleanProject.
|
||||
if (verb == Verb::Clean) {
|
||||
std::vector<fs::path> removed = CleanProject(projectFile);
|
||||
if (removed.empty()) {
|
||||
|
|
@ -2447,19 +2671,31 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
Configuration config = LoadProject(projectFile, projectArgs);
|
||||
SetParentProject(&config);
|
||||
// What the project file describes: one configuration, or several
|
||||
// that are all built.
|
||||
const std::vector<Configuration*> configurations = LoadConfigurations(projectFile, projectArgs);
|
||||
// The ones the file made itself: every one but those that another
|
||||
// project's file returned to GitProject or LocalProject. One of
|
||||
// those, returned here in turn, is built with the rest, and -r,
|
||||
// install and bundle count it among them, but its tests and its
|
||||
// scripts stay that project's: `run` still reaches no script but
|
||||
// those of the project crafter-build was pointed at.
|
||||
std::vector<Configuration*> own;
|
||||
for (Configuration* cfg : configurations) {
|
||||
if (!LoadedProjectOf(*cfg)) own.push_back(cfg);
|
||||
}
|
||||
SetParentProject(configurations);
|
||||
|
||||
if (verb == Verb::Run) {
|
||||
scriptOpts.projectFile = fs::absolute(projectFile).lexically_normal();
|
||||
return RunScript(config, scriptOpts);
|
||||
return RunScript(own, scriptOpts);
|
||||
}
|
||||
|
||||
if (verb == Verb::Lint || verb == Verb::Format) {
|
||||
// lexically_normal: fs::absolute keeps a "./" component, which
|
||||
// would leak into displayed paths and the project-root derivation.
|
||||
lintOpts.projectFile = fs::absolute(projectFile).lexically_normal();
|
||||
LintSummary lintSummary = RunLint(config, lintOpts);
|
||||
LintSummary lintSummary = RunLint(configurations, lintOpts);
|
||||
if (verb == Verb::Lint) return lintSummary.Clean() ? 0 : 1;
|
||||
if (lintSummary.noRulesDefined || lintSummary.errors > 0) return 1;
|
||||
// Apply: formatting files is success (gofmt convention).
|
||||
|
|
@ -2469,51 +2705,76 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
|
|||
|
||||
// What install and bundle cannot export they refuse here, before
|
||||
// anything is built: a project that is not packaged as a Flatpak, or
|
||||
// a host flatpak out of reach, is known from the loaded project.
|
||||
// a host flatpak out of reach, is known from the loaded project, and
|
||||
// so is which of several configurations is the app.
|
||||
ExportChoice exported;
|
||||
if (verb == Verb::Install || verb == Verb::Bundle) {
|
||||
if (const std::string refused = ExportRefusal(config, verb == Verb::Install ? "install" : "bundle"); !refused.empty()) {
|
||||
std::println(std::cerr, "{}", refused);
|
||||
exported = ChooseExport(configurations, verb == Verb::Install ? "install" : "bundle");
|
||||
if (!exported.refused.empty()) {
|
||||
std::println(std::cerr, "{}", exported.refused);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// What the list itself rules out is refused here too, before any of
|
||||
// it is built and before AGENTS.md is written: two returned
|
||||
// configurations of one directory for every verb that builds, and
|
||||
// for the verbs that build the configurations themselves whatever
|
||||
// else BuildAll refuses a list for. `test` builds its tests with
|
||||
// Build() and never comes to BuildAll, which makes these refusals
|
||||
// again for whoever calls it.
|
||||
if (const std::string refused = verb == Verb::Test ? SameDirectoryError(configurations) : PlanBuild(configurations).refused; !refused.empty()) {
|
||||
Progress::Clear();
|
||||
std::println(std::cerr, "{}", refused);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Builds and test runs only: lint and `format --check` promise to
|
||||
// write nothing, `format` rewrites the project's sources and nothing
|
||||
// else, `run` is for chores and `test --list` only lists. Here,
|
||||
// before compiling, because loading has just fetched every
|
||||
// dependency, so the notes are current, and an agent fixing a build
|
||||
// that fails needs them.
|
||||
if (!testOpts.listOnly) UpdateAgentsMd(config, projectFile);
|
||||
if (!testOpts.listOnly) UpdateAgentsMd(configurations, projectFile);
|
||||
|
||||
if (verb == Verb::Test) {
|
||||
TestSummary summary = RunTests(config, testOpts, projectArgs);
|
||||
TestSummary summary = RunTests(own, testOpts, projectArgs);
|
||||
return summary.AllPassed() ? 0 : 1;
|
||||
}
|
||||
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult result = Build(config, depResults, depMutex);
|
||||
|
||||
if (!result.result.empty()) {
|
||||
if (const std::string failed = BuildAll(configurations, depResults, depMutex); !failed.empty()) {
|
||||
Progress::Clear();
|
||||
std::println(std::cerr, "{}", result.result);
|
||||
std::println(std::cerr, "{}", failed);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Progress::Finalize();
|
||||
|
||||
if (verb == Verb::Install || verb == Verb::Bundle) {
|
||||
const std::string failed = verb == Verb::Install ? InstallFlatpakApp(config) : BundleFlatpakApp(config);
|
||||
const std::string failed = verb == Verb::Install ? InstallFlatpakApp(*exported.config) : BundleFlatpakApp(*exported.config);
|
||||
if (failed.empty()) return 0;
|
||||
std::println(std::cerr, "{}", failed);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (runAfterBuild) {
|
||||
if (config.type != ConfigurationType::Executable) {
|
||||
std::vector<const Configuration*> executables;
|
||||
for (const Configuration* cfg : configurations) {
|
||||
if (cfg->type == ConfigurationType::Executable) executables.push_back(cfg);
|
||||
}
|
||||
if (executables.empty()) {
|
||||
std::println(std::cerr, "-r: cannot run a library");
|
||||
return 1;
|
||||
}
|
||||
// Everything is built by now, as without -r. Which one to run
|
||||
// would be a guess.
|
||||
if (executables.size() > 1) {
|
||||
std::println(std::cerr, "-r: {} are executables, and -r runs one", NamesOf(executables));
|
||||
return 1;
|
||||
}
|
||||
const Configuration& config = *executables.front();
|
||||
fs::path dir = config.ProgramDir();
|
||||
fs::path artifact = fs::absolute(ExpectedOutput(config));
|
||||
fs::path absDir = fs::absolute(dir);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -866,18 +866,28 @@ namespace {
|
|||
// whose path lies inside the project root — GitProject / external deps
|
||||
// live under the global cache and are foreign code: they contribute
|
||||
// neither rules nor files. Root-first order so the root's rules win the
|
||||
// by-name dedup.
|
||||
std::vector<Configuration*> CollectLocalConfigs(Configuration& root, const fs::path& projectRoot) {
|
||||
// by-name dedup. Of several roots, the configurations a project file
|
||||
// returns, every root comes first, in the order given, and what they
|
||||
// depend on after them: walked one root's graph after another, a root
|
||||
// that an earlier one depends on would come before the roots between
|
||||
// the two, and its rule would win over theirs.
|
||||
std::vector<Configuration*> CollectLocalConfigs(std::span<Configuration* const> roots, const fs::path& projectRoot) {
|
||||
std::vector<Configuration*> local;
|
||||
std::unordered_set<Configuration*> seen;
|
||||
std::function<void(Configuration*)> walk = [&](Configuration* c) {
|
||||
if (!seen.insert(c).second) return;
|
||||
if (PathInsideRoot(fs::absolute(c->path), projectRoot)) {
|
||||
local.push_back(c);
|
||||
}
|
||||
for (Configuration* dep : c->dependencies) walk(dep);
|
||||
const auto keep = [&](Configuration* c) {
|
||||
if (PathInsideRoot(fs::absolute(c->path), projectRoot)) local.push_back(c);
|
||||
};
|
||||
walk(&root);
|
||||
std::function<void(Configuration*)> below = [&](Configuration* c) {
|
||||
for (Configuration* dep : c->dependencies) {
|
||||
if (!seen.insert(dep).second) continue;
|
||||
keep(dep);
|
||||
below(dep);
|
||||
}
|
||||
};
|
||||
for (Configuration* root : roots) {
|
||||
if (seen.insert(root).second) keep(root);
|
||||
}
|
||||
for (Configuration* root : roots) below(root);
|
||||
return local;
|
||||
}
|
||||
|
||||
|
|
@ -1069,6 +1079,11 @@ void Configuration::AddAstLintRule(std::string name, std::function<void(LintCont
|
|||
}
|
||||
|
||||
LintSummary Crafter::RunLint(Configuration& projectCfg, const RunLintOptions& opts) {
|
||||
std::array<Configuration*, 1> one = { &projectCfg };
|
||||
return RunLint(std::span<Configuration* const>(one), opts);
|
||||
}
|
||||
|
||||
LintSummary Crafter::RunLint(std::span<Configuration* const> configurations, const RunLintOptions& opts) {
|
||||
LintSummary summary;
|
||||
|
||||
// libclang backs the lexer every rule reads through, so a failed load is
|
||||
|
|
@ -1081,10 +1096,10 @@ LintSummary Crafter::RunLint(Configuration& projectCfg, const RunLintOptions& op
|
|||
}
|
||||
|
||||
fs::path projectRoot = opts.projectFile.empty()
|
||||
? fs::absolute(projectCfg.path)
|
||||
? fs::absolute(configurations.empty() ? fs::path() : configurations.front()->path)
|
||||
: opts.projectFile.parent_path();
|
||||
|
||||
std::vector<Configuration*> localConfigs = CollectLocalConfigs(projectCfg, projectRoot);
|
||||
std::vector<Configuration*> localConfigs = CollectLocalConfigs(configurations, projectRoot);
|
||||
// The tests' compile commands are worked out as RunTests builds them.
|
||||
for (Configuration* c : localConfigs) UnpackageTests(*c);
|
||||
|
||||
|
|
@ -1133,7 +1148,7 @@ format` applies it to disk, and `crafter-build lint` reports where it would.
|
|||
CollectConfigSources(*c, files);
|
||||
for (const Test& t : c->tests) CollectConfigSources(t.config, files);
|
||||
}
|
||||
// project.cpp is not built by Build() at all — LoadProject compiles it
|
||||
// project.cpp is not built by Build() at all — its load compiles it
|
||||
// against the host PCM cache with its own flags, so it is owned by nothing
|
||||
// here and gets no compile command. Token rules still cover it.
|
||||
if (!opts.projectFile.empty()) files.emplace(opts.projectFile, nullptr);
|
||||
|
|
@ -1217,7 +1232,7 @@ format` applies it to disk, and `crafter-build lint` reports where it would.
|
|||
continue;
|
||||
}
|
||||
// No compile command means this file is not a translation unit
|
||||
// of the build graph — project.cpp, which LoadProject compiles
|
||||
// of the build graph — project.cpp, which its load compiles
|
||||
// with its own flags, or a header. A semantic rule does not
|
||||
// apply there, the same way a rule self-filters by extension,
|
||||
// so skip it quietly. That is a different thing from a file we
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,18 @@ namespace fs = std::filesystem;
|
|||
using namespace Crafter;
|
||||
|
||||
namespace {
|
||||
std::string JoinNames(std::span<const Script> scripts) {
|
||||
// A script and the configuration it is declared on, which is the
|
||||
// project its callback is handed.
|
||||
struct Declared {
|
||||
const Script* script;
|
||||
Configuration* owner;
|
||||
};
|
||||
|
||||
std::string JoinNames(std::span<const Declared> scripts) {
|
||||
std::string out;
|
||||
for (const Script& script : scripts) {
|
||||
for (const Declared& declared : scripts) {
|
||||
if (!out.empty()) out += ", ";
|
||||
out += script.name;
|
||||
out += declared.script->name;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
@ -29,10 +36,22 @@ std::int32_t ScriptContext::Run(std::string_view command) const {
|
|||
}
|
||||
|
||||
std::int32_t Crafter::RunScript(Configuration& projectCfg, const RunScriptOptions& opts) {
|
||||
std::array<Configuration*, 1> one = { &projectCfg };
|
||||
return RunScript(std::span<Configuration* const>(one), opts);
|
||||
}
|
||||
|
||||
std::int32_t Crafter::RunScript(std::span<Configuration* const> configurations, const RunScriptOptions& opts) {
|
||||
// Loading the project may have left an in-place status line behind.
|
||||
Progress::Clear();
|
||||
|
||||
const std::span<const Script> scripts = projectCfg.scripts;
|
||||
std::vector<Declared> scripts;
|
||||
std::unordered_set<const Configuration*> listed;
|
||||
for (Configuration* cfg : configurations) {
|
||||
// Once, however often the list names it: its scripts are not names
|
||||
// declared twice.
|
||||
if (!listed.insert(cfg).second) continue;
|
||||
for (const Script& script : cfg->scripts) scripts.push_back({ &script, cfg });
|
||||
}
|
||||
if (scripts.empty()) {
|
||||
std::println(std::cerr,
|
||||
R"msg(No scripts defined.
|
||||
|
|
@ -51,7 +70,8 @@ returned; ctx.args holds the arguments.)msg");
|
|||
// declaration is reported the first time anyone uses `run` and not only
|
||||
// when that one name is asked for.
|
||||
std::unordered_set<std::string_view> seen;
|
||||
for (const Script& script : scripts) {
|
||||
for (const Declared& declared : scripts) {
|
||||
const Script& script = *declared.script;
|
||||
if (script.name.empty() || script.name.starts_with('-')) {
|
||||
std::println(std::cerr, "script name '{}' cannot be run: a name must not be empty or start with '-'", script.name);
|
||||
return 1;
|
||||
|
|
@ -69,36 +89,37 @@ returned; ctx.args holds the arguments.)msg");
|
|||
}
|
||||
|
||||
if (opts.listOnly || !opts.named) {
|
||||
for (const Script& script : scripts) std::println("{}", script.name);
|
||||
for (const Declared& declared : scripts) std::println("{}", declared.script->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const auto found = std::ranges::find(scripts, opts.name, &Script::name);
|
||||
const auto found = std::ranges::find_if(scripts, [&](const Declared& declared) { return declared.script->name == opts.name; });
|
||||
if (found == scripts.end()) {
|
||||
std::println(std::cerr, "No script named '{}'. Scripts: {}", opts.name, JoinNames(scripts));
|
||||
return 1;
|
||||
}
|
||||
const Script& script = *found->script;
|
||||
|
||||
ScriptContext ctx{
|
||||
.project = projectCfg,
|
||||
.project = *found->owner,
|
||||
// lexically_normal: fs::absolute keeps a "./" component.
|
||||
.projectDir = opts.projectFile.empty() ? fs::absolute(projectCfg.path).lexically_normal() : opts.projectFile.parent_path(),
|
||||
.projectDir = opts.projectFile.empty() ? fs::absolute(found->owner->path).lexically_normal() : opts.projectFile.parent_path(),
|
||||
.args = opts.args,
|
||||
};
|
||||
try {
|
||||
const std::int32_t code = found->run(ctx);
|
||||
const std::int32_t code = script.run(ctx);
|
||||
if (code >= 0 && code <= 255) return code;
|
||||
std::println(std::cerr, "script '{}' returned {}, which is not an exit status (0..255); exiting with 1", found->name, code);
|
||||
std::println(std::cerr, "script '{}' returned {}, which is not an exit status (0..255); exiting with 1", script.name, code);
|
||||
return 1;
|
||||
} catch (const std::exception& e) {
|
||||
// Never let a script's exception unwind across the project.so
|
||||
// boundary and out of Run: report it as this script's failure.
|
||||
Progress::Clear();
|
||||
std::println(std::cerr, "script '{}' failed: {}", found->name, e.what());
|
||||
std::println(std::cerr, "script '{}' failed: {}", script.name, e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
Progress::Clear();
|
||||
std::println(std::cerr, "script '{}' failed: it threw something that isn't a std::exception", found->name);
|
||||
std::println(std::cerr, "script '{}' failed: it threw something that isn't a std::exception", script.name);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,7 +111,8 @@ namespace {
|
|||
}
|
||||
|
||||
namespace {
|
||||
std::atomic<Configuration*> ParentProject{nullptr};
|
||||
std::mutex ParentProjectsMutex;
|
||||
std::vector<Configuration*> ParentProjects;
|
||||
|
||||
Configuration* FindLibInTree(Configuration* root, std::string_view name, std::unordered_set<Configuration*>& seen) {
|
||||
if (!seen.insert(root).second) return nullptr;
|
||||
|
|
@ -124,17 +125,31 @@ namespace {
|
|||
}
|
||||
|
||||
void Crafter::SetParentProject(Configuration* parent) {
|
||||
ParentProject.store(parent);
|
||||
SetParentProject(parent ? std::span<Configuration* const>(&parent, 1) : std::span<Configuration* const>());
|
||||
}
|
||||
|
||||
void Crafter::SetParentProject(std::span<Configuration* const> parents) {
|
||||
const std::lock_guard lock(ParentProjectsMutex);
|
||||
ParentProjects.assign(parents.begin(), parents.end());
|
||||
}
|
||||
|
||||
Configuration* Crafter::ParentLib(std::string_view name) {
|
||||
Configuration* root = ParentProject.load();
|
||||
if (!root) {
|
||||
std::vector<Configuration*> roots;
|
||||
{
|
||||
const std::lock_guard lock(ParentProjectsMutex);
|
||||
roots = ParentProjects;
|
||||
}
|
||||
if (roots.empty()) {
|
||||
throw std::runtime_error(std::format("Crafter::ParentLib('{}'): no parent project set", name));
|
||||
}
|
||||
std::unordered_set<Configuration*> seen;
|
||||
if (auto found = FindLibInTree(root, name, seen)) return found;
|
||||
throw std::runtime_error(std::format("Crafter::ParentLib('{}'): not found in parent project '{}'", name, root->name));
|
||||
std::string names;
|
||||
for (Configuration* root : roots) {
|
||||
if (auto found = FindLibInTree(root, name, seen)) return found;
|
||||
if (!names.empty()) names += ", ";
|
||||
names += root->name;
|
||||
}
|
||||
throw std::runtime_error(std::format("Crafter::ParentLib('{}'): not found in parent project '{}'", name, names));
|
||||
}
|
||||
|
||||
TestRunner TestRunner::Local() {
|
||||
|
|
@ -449,9 +464,22 @@ void Crafter::UnpackageTests(Configuration& cfg) {
|
|||
}
|
||||
|
||||
TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs) {
|
||||
UnpackageTests(projectCfg);
|
||||
std::array<Configuration*, 1> one = { &projectCfg };
|
||||
return RunTests(std::span<Configuration* const>(one), opts, projectArgs);
|
||||
}
|
||||
|
||||
TestSummary Crafter::RunTests(std::span<Configuration* const> configurations, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs) {
|
||||
// Each of them once, however often the list names it: named twice, a
|
||||
// configuration would have each of its tests built twice at once.
|
||||
std::vector<Configuration*> distinct;
|
||||
for (Configuration* cfg : configurations) {
|
||||
if (!std::ranges::contains(distinct, cfg)) distinct.push_back(cfg);
|
||||
}
|
||||
if (distinct.size() != configurations.size()) return RunTests(distinct, opts, projectArgs);
|
||||
|
||||
for (Configuration* cfg : configurations) UnpackageTests(*cfg);
|
||||
// Multi-target sweep: when no --target= was given, the run covers every
|
||||
// distinct target declared across projectCfg.tests plus the host target.
|
||||
// distinct target declared across the tests plus the host target.
|
||||
// Lets a bare `crafter-build test` exercise cross-arch tests without the
|
||||
// user having to know which targets exist. An explicit --target=X
|
||||
// bypasses the sweep and runs that target only.
|
||||
|
|
@ -461,8 +489,10 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions&
|
|||
// the one shown and filtered on.
|
||||
std::map<std::string, std::string> sweep;
|
||||
sweep.emplace(NormalizeTarget(HostTarget()), HostTarget());
|
||||
for (const Test& t : projectCfg.tests) {
|
||||
if (!t.config.target.empty()) sweep.emplace(NormalizeTarget(t.config.target), t.config.target);
|
||||
for (const Configuration* cfg : configurations) {
|
||||
for (const Test& t : cfg->tests) {
|
||||
if (!t.config.target.empty()) sweep.emplace(NormalizeTarget(t.config.target), t.config.target);
|
||||
}
|
||||
}
|
||||
TestSummary aggregate;
|
||||
for (const auto& [normalized, target] : sweep) {
|
||||
|
|
@ -472,7 +502,7 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions&
|
|||
Progress::Clear();
|
||||
std::println("\n=== target: {} ===", target);
|
||||
}
|
||||
TestSummary s = RunTests(projectCfg, perTarget, projectArgs);
|
||||
TestSummary s = RunTests(configurations, perTarget, projectArgs);
|
||||
aggregate.passed += s.passed;
|
||||
aggregate.failed += s.failed;
|
||||
aggregate.crashed += s.crashed;
|
||||
|
|
@ -490,17 +520,22 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions&
|
|||
// time) because runnerOverride and FromEnv resolution depend on the
|
||||
// run's options, which the project.cpp doesn't know about.
|
||||
std::vector<Test*> filtered;
|
||||
filtered.reserve(projectCfg.tests.size());
|
||||
for (auto& test : projectCfg.tests) {
|
||||
if (!SameTarget(test.config.target, opts.targetFilter)) continue;
|
||||
if (!MatchAny(opts.globs, test.config.name)) continue;
|
||||
test.runner = TestRunner::FromEnv(test.config.target, TestRunner::ForTarget(test.config));
|
||||
if (opts.runnerOverride) {
|
||||
if (auto r = TestRunner::FromSpec(*opts.runnerOverride)) {
|
||||
test.runner = std::move(*r);
|
||||
// The configuration that declares each of them, whose path its log is
|
||||
// written under.
|
||||
std::vector<const Configuration*> owners;
|
||||
for (Configuration* cfg : configurations) {
|
||||
for (auto& test : cfg->tests) {
|
||||
if (!SameTarget(test.config.target, opts.targetFilter)) continue;
|
||||
if (!MatchAny(opts.globs, test.config.name)) continue;
|
||||
test.runner = TestRunner::FromEnv(test.config.target, TestRunner::ForTarget(test.config));
|
||||
if (opts.runnerOverride) {
|
||||
if (auto r = TestRunner::FromSpec(*opts.runnerOverride)) {
|
||||
test.runner = std::move(*r);
|
||||
}
|
||||
}
|
||||
filtered.push_back(&test);
|
||||
owners.push_back(cfg);
|
||||
}
|
||||
filtered.push_back(&test);
|
||||
}
|
||||
|
||||
if (opts.listOnly) {
|
||||
|
|
@ -515,6 +550,38 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions&
|
|||
return summary;
|
||||
}
|
||||
|
||||
// A test is built in a directory named after the test and what it is
|
||||
// compiled as, not after the configuration that declares it, and its
|
||||
// sources are tests/<name> whichever one does. Two configurations that
|
||||
// each declare a test of one name, compiled alike, would have the two
|
||||
// built in that one directory, at once when the run has the jobs for
|
||||
// it, each with the dependencies and link flags of its own declaration.
|
||||
// Neither is built: both fail, and say which configurations declare
|
||||
// them. Only tests of different configurations are looked at: a test
|
||||
// one configuration declares twice is as it was.
|
||||
std::vector<std::string> clashes(filtered.size());
|
||||
{
|
||||
std::unordered_map<fs::path, std::vector<std::size_t>> builtInto;
|
||||
for (std::size_t i = 0; i < filtered.size(); ++i) {
|
||||
builtInto[fs::absolute(filtered[i]->config.PcmDir()).lexically_normal()].push_back(i);
|
||||
}
|
||||
for (const auto& [directory, sharing] : builtInto) {
|
||||
std::vector<const Configuration*> declaring;
|
||||
for (const std::size_t i : sharing) {
|
||||
if (!std::ranges::contains(declaring, owners[i])) declaring.push_back(owners[i]);
|
||||
}
|
||||
if (declaring.size() < 2) continue;
|
||||
std::string names;
|
||||
for (const Configuration* cfg : declaring) {
|
||||
if (!names.empty()) names += ", ";
|
||||
names += cfg->name;
|
||||
}
|
||||
for (const std::size_t i : sharing) {
|
||||
clashes[i] = std::format("{} each declare a test named {}, which would be built for each of them in {}: give each test a name of its own", names, filtered[i]->config.name, directory.string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::int32_t jobs = opts.jobs > 0
|
||||
? opts.jobs
|
||||
: std::max(1u, std::thread::hardware_concurrency());
|
||||
|
|
@ -549,6 +616,18 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions&
|
|||
TestResult r;
|
||||
r.name = t.config.name;
|
||||
|
||||
if (!clashes[i].empty()) {
|
||||
r.outcome = TestOutcome::Fail;
|
||||
r.output = clashes[i];
|
||||
r.exitCode = -1;
|
||||
{
|
||||
std::lock_guard lk(printMutex);
|
||||
PrintResult(r, t.runner.name);
|
||||
}
|
||||
results[i] = std::move(r);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Declarative preconditions set via TestBuilder::Requires. Evaluated
|
||||
// before the build so a missing tool/file/env turns into a Skip without
|
||||
// paying the compile cost. Reports the first failure only — once one
|
||||
|
|
@ -632,7 +711,7 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions&
|
|||
}
|
||||
|
||||
if (r.outcome != TestOutcome::Pass && r.outcome != TestOutcome::Skipped && !r.output.empty()) {
|
||||
WriteLog(projectCfg.path, r.name, r.output);
|
||||
WriteLog(owners[i]->path, r.name, r.output);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ export namespace Crafter {
|
|||
// Write the AGENTS.md beside `projectFile` from the AGENTS.template.md
|
||||
// beside it, with the AGENTS.consumer.md of every dependency
|
||||
// project.dependencyNotes selects merged in. `project` is the
|
||||
// Configuration that file returned, after LoadProject: loading is what
|
||||
// Configuration that file returned, once it is loaded: loading is what
|
||||
// found the dependencies. Creates AGENTS.md when it is missing, and
|
||||
// replaces only one a build wrote: an AGENTS.md someone wrote is left
|
||||
// alone, with a warning. Without a template it writes nothing, and only
|
||||
|
|
@ -81,4 +81,17 @@ export namespace Crafter {
|
|||
// a dependency's unusable notes are warnings: the notes never fail a
|
||||
// build, and nothing is reported when nothing would have been written.
|
||||
bool UpdateAgentsMd(const Configuration& project, const fs::path& projectFile);
|
||||
// The same for a project file that returned several configurations.
|
||||
// Each one the file made itself selects, by its dependencyNotes, among
|
||||
// the projects that one is built against, and the notes are merged: a
|
||||
// dependency's are in when a configuration that asks for them leads to
|
||||
// it. The file's `#pragma crafter dependency` lines, and a
|
||||
// configuration it loaded from another project and returns, which
|
||||
// counts as a dependency here, go by the widest of those settings, and
|
||||
// by Transitive when the file made no configuration itself. A
|
||||
// configuration is another project's when GitProject or LocalProject
|
||||
// loaded it from another project file than `projectFile`, two files
|
||||
// being one when they resolve to one path: one they loaded from
|
||||
// `projectFile` itself is that file's own.
|
||||
bool UpdateAgentsMd(std::span<const Configuration* const> configurations, const fs::path& projectFile);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -440,9 +440,11 @@ export namespace Crafter {
|
|||
// What a Script's callback is handed when `crafter-build run <name>`
|
||||
// starts it.
|
||||
struct ScriptContext {
|
||||
// The Configuration project.cpp returned. Read the project through
|
||||
// this rather than capturing the local in CrafterBuildProject by
|
||||
// reference: that local is gone by the time a script runs.
|
||||
// The Configuration the script is declared on: the one project.cpp
|
||||
// returned, or of several the one whose `scripts` holds it. Read the
|
||||
// project through this rather than capturing a local of the entry
|
||||
// point by reference: the local need not be the object that the
|
||||
// entry point returned.
|
||||
Configuration& project;
|
||||
// Absolute directory of the project file. Commands run in the
|
||||
// directory crafter-build was started from, like everything else it
|
||||
|
|
@ -595,8 +597,10 @@ export namespace Crafter {
|
|||
// Crafter.Build:Agents):
|
||||
// cfg.dependencyNotes = DependencyNotes::Direct;
|
||||
// Only read on the Configuration project.cpp returns; a dependency's
|
||||
// setting decides nothing for its consumers. Not part of VariantId:
|
||||
// the notes change nothing that is compiled.
|
||||
// setting decides nothing for its consumers. Of several returned
|
||||
// configurations, each one the project file made itself has its own
|
||||
// read, for what that one is built against (see UpdateAgentsMd). Not
|
||||
// part of VariantId: the notes change nothing that is compiled.
|
||||
DependencyNotes dependencyNotes = DependencyNotes::Transitive;
|
||||
void GetInterfacesAndImplementations(std::span<fs::path> interfaces, std::span<fs::path> implementations);
|
||||
// Retry the `import X;` names GetInterfacesAndImplementations could not
|
||||
|
|
@ -867,6 +871,35 @@ export namespace Crafter {
|
|||
// checks everything again and pulls unpinned externals again.
|
||||
BuildResult Build(Configuration& config, std::unordered_map<fs::path, std::shared_future<BuildResult>>& depResults, std::mutex& depMutex);
|
||||
|
||||
// Builds every one of `configurations`, the configurations a project
|
||||
// file returns, in the pass `depResults` records. Each is built by
|
||||
// Build(), as a project's one configuration is, and its result then
|
||||
// goes into the record under its PcmDir, where Build() records a
|
||||
// dependency. What two of them depend on is built once, and so is one
|
||||
// that another of them is built on: it is built before the other
|
||||
// starts, and the other finds it in the record. The first is built on
|
||||
// the calling thread and each other one on a thread of its own, all of
|
||||
// them to the end: one that fails stops none of the others. Returns the
|
||||
// errors of the ones that failed, in the order given and each message
|
||||
// once, so a dependency that failed for three of them is reported once,
|
||||
// and an empty string when all were built. A configuration the list
|
||||
// names twice is built once.
|
||||
//
|
||||
// With a wasm executable that declares wasmVariants among them, or
|
||||
// among what they are built on, they are built one after the other on
|
||||
// the calling thread instead: its build rewrites the configurations it
|
||||
// is built on for each variant, which no other build may be reading.
|
||||
//
|
||||
// Refuses, before building anything, two of them that would be built
|
||||
// into one directory (same path, name, target and compile state), which
|
||||
// would be two builds writing the same files at once; a set of them in
|
||||
// which each is built on something that is built into the directory of
|
||||
// another, so that none could be built first; and one that is built on
|
||||
// something of its own directory when another of them is built on that
|
||||
// directory too, since the record holds one result for a directory and
|
||||
// the other could be given either.
|
||||
std::string BuildAll(std::span<Configuration* const> configurations, std::unordered_map<fs::path, std::shared_future<BuildResult>>& depResults, std::mutex& depMutex);
|
||||
|
||||
// Takes main's argv verbatim so the CLI entry point is a one-liner;
|
||||
// the shape is inherited from the language, not chosen here.
|
||||
// lint-disable-next-line no-char-pointer
|
||||
|
|
|
|||
|
|
@ -76,14 +76,14 @@ export namespace Crafter {
|
|||
// clone can still differ in includeDirs, libDirs and libs.
|
||||
ExternalBuildResult ExternalOutputs(const ExternalDependency& dep, std::string_view target);
|
||||
|
||||
// The args the project file now running its CrafterBuildProject was
|
||||
// loaded with: everything that function receives, --target=, --debug and
|
||||
// The args the project file now running its entry point was loaded
|
||||
// with: everything that function receives, --target=, --debug and
|
||||
// the other standard flags included. Not cfg.projectArgs, which holds
|
||||
// only what ApplyStandardArgs left uninterpreted: `.args =
|
||||
// cfg.projectArgs` would drop the target and the build type. Empty
|
||||
// everywhere else, because it reads the stack LoadProject keeps per
|
||||
// everywhere else, because it reads the stack the loader keeps per
|
||||
// thread, as IsTopLevelProject does: outside a project file, on any
|
||||
// thread but the one running it, once CrafterBuildProject has returned
|
||||
// thread but the one running it, once the entry point has returned
|
||||
// (a script runs later), and before it is called, which covers the
|
||||
// file's static initialisers and its `#pragma crafter dependency`
|
||||
// pre-pass. The specs below default their args to it. Defined beside
|
||||
|
|
@ -97,7 +97,7 @@ export namespace Crafter {
|
|||
// GitSource picks the revision: leave branch + commit empty for the
|
||||
// remote's default branch HEAD (sloppy but convenient), set branch to
|
||||
// track a branch tip, or set commit to pin to an immutable SHA. args are
|
||||
// what the dep's CrafterBuildProject receives. They default to this
|
||||
// what the dep's entry point receives. They default to this
|
||||
// project's own, read when the spec is made, so --target=, --march=,
|
||||
// --debug and the project's own flags reach every sibling down a chain
|
||||
// without being passed. Set them to forward something else: {} for
|
||||
|
|
@ -105,19 +105,28 @@ export namespace Crafter {
|
|||
// a flag only this project reads. Give such a list to every sibling:
|
||||
// Build() refuses a graph in which two paths load one project with two
|
||||
// different lists, since the program would end up with both builds.
|
||||
// configuration picks among the configurations of a project file that
|
||||
// returns several (CrafterBuildConfigurations), by Configuration::name.
|
||||
// Left empty it is the first one the file returns, so a project that
|
||||
// grows a second configuration puts it after the one its consumers
|
||||
// already get.
|
||||
struct GitProjectSpec {
|
||||
GitSource source;
|
||||
fs::path projectFile = "project.cpp";
|
||||
std::vector<std::string> args = ProjectArgs();
|
||||
std::string configuration;
|
||||
};
|
||||
|
||||
// Clones the spec's git URL into the external cache, recursively
|
||||
// LoadProject's the remote's project.cpp, and returns a stable
|
||||
// Configuration* you can drop into cfg.dependencies. The clone is keyed by
|
||||
// url+branch+commit+projectFile alone, so every arg list builds in the
|
||||
// same working tree, each into variant directories of its own. The
|
||||
// Configuration is owned by an internal cache for the lifetime of the
|
||||
// build, keyed by the clone and the args: identical specs share one.
|
||||
// Clones the spec's git URL into the external cache, loads the remote's
|
||||
// project.cpp in turn, and returns a stable Configuration* you can drop
|
||||
// into cfg.dependencies: the configuration the spec picks of the ones
|
||||
// that file describes. The clone is keyed by url+branch+commit+projectFile
|
||||
// alone, so every arg list builds in the same working tree, each into
|
||||
// variant directories of its own. What the file describes is kept in an
|
||||
// internal cache for the lifetime of the build, keyed by the clone and
|
||||
// the args: identical specs share one Configuration, and two that differ
|
||||
// only in `configuration` share one load of the file. Throws when the
|
||||
// file has no configuration of the name the spec asks for, or two.
|
||||
Configuration* GitProject(const GitProjectSpec& spec);
|
||||
|
||||
// Specification for a local sibling crafter-build project (e.g. an
|
||||
|
|
@ -126,12 +135,13 @@ export namespace Crafter {
|
|||
struct LocalProjectSpec {
|
||||
fs::path projectFile; // relative to cwd or absolute
|
||||
std::vector<std::string> args = ProjectArgs(); // as GitProjectSpec::args
|
||||
std::string configuration; // as GitProjectSpec::configuration
|
||||
};
|
||||
|
||||
// Same as GitProject but for a local on-disk project — no fetch, no
|
||||
// cache copy. Resolves projectFile to its canonical absolute path and
|
||||
// recursively LoadProject's it. Returns a stable Configuration* owned
|
||||
// by the same internal cache as GitProject.
|
||||
// loads it in turn. Returns a stable Configuration* kept by the same
|
||||
// internal cache as GitProject.
|
||||
Configuration* LocalProject(const LocalProjectSpec& spec);
|
||||
|
||||
// Clone (or update) the given git source into the shared external
|
||||
|
|
@ -147,11 +157,11 @@ export namespace Crafter {
|
|||
|
||||
namespace Crafter {
|
||||
// Where GitProject or LocalProject loaded a Configuration from. The
|
||||
// loader keeps this beside the cache that owns the Configuration, since
|
||||
// the Configuration itself cannot say: only the one they return has its
|
||||
// path rebased, and the configurations a dependency's project file makes
|
||||
// for itself keep a path such as "./", which resolves against the
|
||||
// consumer's directory.
|
||||
// loader keeps this beside the cache that holds the Configuration, since
|
||||
// the Configuration itself cannot say: only the ones its project file
|
||||
// returns have their paths rebased, and the configurations a
|
||||
// dependency's project file makes for itself and does not return keep a
|
||||
// path such as "./", which resolves against the consumer's directory.
|
||||
struct LoadedProject {
|
||||
fs::path projectFile; // absolute
|
||||
// What a consumer's AGENTS.md calls it: the repository's name for a
|
||||
|
|
@ -166,8 +176,20 @@ namespace Crafter {
|
|||
// Two specs that pin one library differently are one library.
|
||||
std::string identity;
|
||||
};
|
||||
// The LoadedProject behind a Configuration that GitProject or
|
||||
// LocalProject returned; nullopt for any other: the project crafter-build
|
||||
// was pointed at, and every configuration a project file makes itself.
|
||||
// The LoadedProject behind a Configuration that a project file GitProject
|
||||
// or LocalProject loaded returned, the ones the spec did not pick
|
||||
// included; nullopt for any other: what the project file crafter-build
|
||||
// was pointed at makes itself, and what a loaded project file makes for
|
||||
// itself and does not return.
|
||||
std::optional<LoadedProject> LoadedProjectOf(const Configuration& cfg);
|
||||
// The project that keeps `cfg` to itself: one GitProject or LocalProject
|
||||
// loaded whose project file did not return `cfg`, while a configuration
|
||||
// that file did return led to it when the file was loaded. A
|
||||
// configuration leads to what it is built on (`dependencies`) and to
|
||||
// the configurations of its tests (`tests`), and through each of those
|
||||
// to what that one leads to. nullopt for every other configuration: one
|
||||
// a loaded project file returned, and one that nothing a loaded project
|
||||
// file returned led to, such as what the project file crafter-build was
|
||||
// pointed at makes itself.
|
||||
std::optional<LoadedProject> KeptBy(const Configuration& cfg);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,8 @@ export namespace Crafter {
|
|||
// Absolute path of the loaded project.cpp. It is linted too, and its
|
||||
// parent directory is the project root that decides which dependency
|
||||
// Configurations contribute rules/files (GitProject / cache-dir deps
|
||||
// are foreign code and skipped). Empty => fall back to cfg.path.
|
||||
// are foreign code and skipped). Empty => fall back to cfg.path, of
|
||||
// several configurations the first one's.
|
||||
std::filesystem::path projectFile;
|
||||
};
|
||||
|
||||
|
|
@ -62,4 +63,10 @@ export namespace Crafter {
|
|||
// findings compiler-style (Report), would-change paths (Check), or
|
||||
// formatted paths (Apply), plus a summary line.
|
||||
LintSummary RunLint(Configuration& projectCfg, const RunLintOptions& opts);
|
||||
// The same for the several configurations a project file returns, as
|
||||
// one run over all of them: each is a root, and the roots come before
|
||||
// what any of them depends on, in the order given. A rule two of them
|
||||
// register under one name runs once, as the first of them in that order
|
||||
// registered it, and a source two of them reach is linted once.
|
||||
LintSummary RunLint(std::span<Configuration* const> configurations, const RunLintOptions& opts);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,17 +98,47 @@ namespace Crafter {
|
|||
// or the org.flatpak.Builder app where the host has none.
|
||||
std::int32_t RunHostFlatpakBuilder(std::string_view arguments);
|
||||
std::string GetBaseCommand(const Configuration& config);
|
||||
// The configurations `projectFile` describes, in the order it gives them.
|
||||
// Compiles the file into a shared object in the build/ beside it, unless
|
||||
// the one there is current, loads that into this process and calls the
|
||||
// file's entry point with `args`. A project file defines one of two:
|
||||
// extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args);
|
||||
// extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args);
|
||||
// The first is a project of one configuration, which comes back as a
|
||||
// list of one. The second is a project of several that need not have
|
||||
// anything to do with each other, such as a library and a tool, or the
|
||||
// programs of one repository; they come back as the file returned them.
|
||||
// The two names are how a loaded file says which it is: both are C
|
||||
// symbols, which carry no return type, so one name with two return types
|
||||
// would be called as whichever the loader guessed. Throws for a file
|
||||
// that defines neither or both, and for a CrafterBuildConfigurations
|
||||
// that returns nothing, a null pointer, one configuration twice, or one
|
||||
// that a project GitProject or LocalProject loaded keeps to itself
|
||||
// (KeptBy): every configuration in the list is one the file made or one
|
||||
// those two returned.
|
||||
//
|
||||
// The pointers are used for as long as the process runs. What a
|
||||
// CrafterBuildConfigurations returns is its project file's to keep that
|
||||
// long, at addresses that do not change: in a std::deque the file only
|
||||
// adds to, one new set per call, or what GitProject and LocalProject
|
||||
// gave it. What a CrafterBuildProject returns is kept here, constructed
|
||||
// by that call and never moved.
|
||||
export std::vector<Configuration*> LoadConfigurations(const fs::path& projectFile, std::span<const std::string_view> args);
|
||||
// What the CrafterBuildProject of `projectFile` returns, loaded as
|
||||
// LoadConfigurations loads it. Throws for a project file that defines
|
||||
// CrafterBuildConfigurations: the configurations that one returns stay
|
||||
// where the file keeps them and cannot be returned by value.
|
||||
export Configuration LoadProject(const fs::path& projectFile, std::span<const std::string_view> args);
|
||||
// A dependency the project file currently running its CrafterBuildProject
|
||||
// A dependency the project file currently running its entry point
|
||||
// declared with `#pragma crafter dependency`, so it can also be wired
|
||||
// into the build graph (a test that links the same module, say) without
|
||||
// spelling the source twice. `name` is the dependency's Configuration
|
||||
// name or any module it exports — whatever the project file `import`s.
|
||||
// Throws when no such dependency was declared, or when called from
|
||||
// anywhere other than inside LoadProject.
|
||||
// anywhere other than inside a project file's load.
|
||||
export Configuration* ProjectDependency(std::string_view name);
|
||||
// True while the project file crafter-build was pointed at runs its
|
||||
// CrafterBuildProject, and when no project file is running at all; false
|
||||
// entry point, and when no project file is running at all; false
|
||||
// inside a dependency's project file (a `#pragma crafter dependency`,
|
||||
// GitProject or LocalProject target). Wire anything only the project's
|
||||
// own build or test run needs behind it. The case that forced it: a
|
||||
|
|
@ -117,9 +147,9 @@ namespace Crafter {
|
|||
// project file in turn and the two recurse until the cycle guard throws.
|
||||
export bool IsTopLevelProject();
|
||||
// What the `#pragma crafter dependency` lines of `projectFile` resolved
|
||||
// to the last time LoadProject loaded it in this process, in file order;
|
||||
// to the last time it was loaded in this process, in file order;
|
||||
// empty when it has none or was never loaded. Unlike ProjectDependency
|
||||
// this still answers once LoadProject has returned, which is when the
|
||||
// this still answers once the load has returned, which is when the
|
||||
// dependency notes of the project crafter-build was pointed at are
|
||||
// gathered (Crafter.Build:Agents).
|
||||
std::vector<Configuration*> PragmaDependencies(const fs::path& projectFile);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ export namespace Crafter {
|
|||
// Print the script names instead of running one.
|
||||
bool listOnly = false;
|
||||
// Absolute path of the loaded project.cpp; its directory becomes
|
||||
// ScriptContext::projectDir. Empty => fall back to cfg.path.
|
||||
// ScriptContext::projectDir. Empty => fall back to the path of the
|
||||
// configuration the script is declared on.
|
||||
std::filesystem::path projectFile;
|
||||
};
|
||||
|
||||
|
|
@ -37,4 +38,10 @@ export namespace Crafter {
|
|||
// (a raw wait status passed along by mistake) would otherwise wrap to
|
||||
// success.
|
||||
std::int32_t RunScript(Configuration& projectCfg, const RunScriptOptions& opts);
|
||||
// The same over the scripts of several configurations, the ones a
|
||||
// project file returns, as one list in the order given: a name two of
|
||||
// them declare is a name declared twice, and a configuration the list
|
||||
// names twice counts once. The script's ScriptContext::project is the
|
||||
// configuration it is declared on.
|
||||
std::int32_t RunScript(std::span<Configuration* const> configurations, const RunScriptOptions& opts);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,15 +88,26 @@ export namespace Crafter {
|
|||
};
|
||||
|
||||
TestSummary RunTests(Configuration& projectCfg, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs = {});
|
||||
// The tests of several configurations, those a project file returns, as
|
||||
// one run: one sweep over the targets they declare, one summary, and
|
||||
// one build pass per target, so a library that tests of two of them link
|
||||
// is built once. The log a failed, crashed or timed-out test leaves
|
||||
// goes under the path of the configuration that declares it. A test
|
||||
// belongs to one configuration: where two of them each declare one that
|
||||
// would be built into the same directory, which two tests of one name
|
||||
// compiled alike are, both fail without being built and name the
|
||||
// configurations. A configuration the list names twice counts once.
|
||||
TestSummary RunTests(std::span<Configuration* const> configurations, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs = {});
|
||||
TestResult RunSingleTest(const Test& test, const std::filesystem::path& binary, std::chrono::seconds timeout);
|
||||
|
||||
// Parent-project access for test fixtures. Run() sets the pointer to the
|
||||
// root project's Configuration before discovery; fixtures call ParentLib()
|
||||
// to obtain a Configuration* for one of the parent's libraries (or the
|
||||
// parent itself) so they can declare it as a dependency without rebuilding
|
||||
// the lib config from scratch. Lookup is by Configuration::name and
|
||||
// recurses through the parent's dependency graph.
|
||||
// The configurations of the project crafter-build was pointed at, by
|
||||
// name. Run() hands SetParentProject what the project file returned,
|
||||
// once it has loaded. ParentLib() then finds a Configuration* by
|
||||
// Configuration::name among them and what they depend on, searching the
|
||||
// dependency graph of each in the order the file returned them, and
|
||||
// throws when none has that name or no project is set.
|
||||
void SetParentProject(Configuration* parent);
|
||||
void SetParentProject(std::span<Configuration* const> parents);
|
||||
Configuration* ParentLib(std::string_view name);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue