docs: a project file can return several configurations

The README's opening and the start of "Writing a project.cpp" say that a
project file returns one configuration or several, and so does the
description in the Flatpak's metainfo. The README has a "Several
configurations" section in "Writing a project.cpp":
CrafterBuildConfigurations with an example, how the configurations it
returns have to be kept, and what a build, test, run, lint, format, -r,
install and bundle do with several. "Dependencies" says how a GitProject
or LocalProject spec picks one of a sibling's configurations, and that a
`#pragma crafter dependency` gets the first; its sentences on the args a
sibling gets, on where to make a spec and on IsTopLevelProject() speak
of a project file's entry point, where they named CrafterBuildProject.
"Dependency notes for agents" says how the dependencyNotes of several
configurations combine, and which runs leave AGENTS.md alone now that a
list of configurations can be refused before a build starts: its
sentence on that said that a build that fails still has the notes, and
named an `install` or `bundle` refused before building as the one
refusal that writes none. The table of ScriptContext says that a
script's `project` is the configuration it is declared on.
This commit is contained in:
Jorijn van der Graaf 2026-11-15 02:16:07 +01:00
commit 53485c160b
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 66 additions and 11 deletions

View file

@ -1,6 +1,6 @@
# Crafter Build # Crafter Build
A C++26 modules build system. Project descriptions are written in C++ with no JSON, no Lua, no embedded DSL. You write a `project.cpp` that constructs a `Configuration` and returns it. Crafter Build compiles, loads, and executes it. A C++26 modules build system. Project descriptions are written in C++ with no JSON, no Lua, no embedded DSL. You write a `project.cpp` that constructs a `Configuration`, or several, and returns them. Crafter Build compiles, loads, and executes it.
## Install ## Install
@ -18,7 +18,7 @@ Releases are published to the `stable` branch that the commands above install, a
## Writing a project.cpp ## Writing a project.cpp
Crafter Build loads a `project.cpp` from the current directory. The file exports one function that returns a populated `Configuration`: Crafter Build loads a `project.cpp` from the current directory. The file exports one function: `CrafterBuildProject`, which returns a populated `Configuration`, or `CrafterBuildConfigurations` for [several](#several-configurations):
```cpp ```cpp
import std; import std;
@ -47,6 +47,50 @@ crafter-build orders a configuration's units by the `import` lines of its source
A build runs at most as many of its steps at once as the machine has hardware threads, however many sources are stale: the compiles of its sources, its shaders and assets, and its links and archives, counted together for the project and the dependencies it is built on. An `ExternalDependency`'s CMake build is not among them and is given that many jobs of its own, and the tests `crafter-build test` runs are counted by `--jobs=`. A step counts as one whatever threads it starts for itself: a link runs on lld's, and an asset is compressed on threads its compressor starts. A build runs at most as many of its steps at once as the machine has hardware threads, however many sources are stale: the compiles of its sources, its shaders and assets, and its links and archives, counted together for the project and the dependencies it is built on. An `ExternalDependency`'s CMake build is not among them and is given that many jobs of its own, and the tests `crafter-build test` runs are counted by `--jobs=`. A step counts as one whatever threads it starts for itself: a link runs on lld's, and an asset is compressed on threads its compressor starts.
### Several configurations
A project that builds more than one thing, such as a library and the tool that comes with it or the programs of one repository, returns all of them from `CrafterBuildConfigurations` instead. They need not have anything to do with each other, and `crafter-build` builds every one of them:
```cpp
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args) {
static std::deque<Configuration> owned;
Configuration& lib = owned.emplace_back();
lib.path = "./";
lib.name = "mylib";
lib.outputName = "mylib";
lib.type = ConfigurationType::LibraryStatic;
std::array<fs::path, 1> libIfaces = { "interfaces/MyLib" };
std::array<fs::path, 1> libImpls = { "implementations/MyLib" };
lib.GetInterfacesAndImplementations(libIfaces, libImpls);
Configuration& tool = owned.emplace_back();
tool.path = "./";
tool.name = "mytool";
tool.outputName = "mytool";
tool.type = ConfigurationType::Executable;
tool.dependencies = { &lib };
std::array<fs::path, 0> toolIfaces = {};
std::array<fs::path, 1> toolImpls = { "implementations/main" };
tool.GetInterfacesAndImplementations(toolIfaces, toolImpls);
return { &lib, &tool };
}
```
A project file has one of the two functions, never both. `CrafterBuildConfigurations` gets the same args as `CrafterBuildProject`, and `ProjectArgs()` and `IsTopLevelProject()` answer the same inside it. It returns pointers, so the configurations have to stay where they are for as long as crafter-build runs. A `std::deque` that the file only adds to keeps them there, with a new set for every call; a `std::vector` would move them when it grows, and a single static `Configuration` would be filled in again by every load of the file. A pointer that `GitProject` or `LocalProject` returned can be in the list as well, which is how one file builds a set of sibling projects.
With several configurations:
- **A build** builds all of them in one pass, so what two of them depend on is built once. One that fails does not stop the others, and the build then fails with the errors of the ones that failed.
- **`test`** runs the tests of every one the file made itself, in one run. By default a test is built from `tests/<Name>/` into `build/<Name>-<variant>/` and `bin/<Name>-<variant>/`, whichever configuration declares it, so a test belongs to one of them: where two each declare a test that would be built into the same directories, both tests fail without being built and name the two configurations. Give each test a name of its own.
- **`run`** offers the scripts of every one the file made itself: a script gets the configuration it is declared on as `ctx.project`, and a name two of them declare is an error.
- **`lint`** and **`format`** take the rules and the sources of every one that lies in the project's directory, and a rule two of them register under one name runs once.
- **`-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 to choose from they stop and name them, as `install` and `bundle` do when none is packaged: `-r` once everything is built, `install` and `bundle` before anything is.
- **A configuration of another project**, which the file got from `GitProject` or `LocalProject` and returns, is built with the rest and counts for `-r`, `install` and `bundle`. Its tests and its scripts stay that project's.
- **Two that would be built into the same directory**, as two with the same `path` and `name` and no other difference are, are refused by a build, `test`, `-r`, `install` and `bundle`, before anything is built. Give each a `name` of its own. A build, `-r`, `install` and `bundle` refuse two more sets before they build anything: configurations 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.
- **`cfg.dependencyNotes`** is read on every one the file made itself (see [Dependency notes for agents](#dependency-notes-for-agents)).
## Flatpak output ## Flatpak output
A program crafter-build's Flatpak links for Linux without a sysroot takes the libc++, libc++abi and libunwind of its LLVM and the glibc of its runtime, so outside the Flatpak it starts only on a host that has those libraries and a glibc with every symbol version they and the program reference, 2.38 or newer for the bundled libc++. A build lays the program out as a Flatpak of its own when it is packaged: A program crafter-build's Flatpak links for Linux without a sysroot takes the libc++, libc++abi and libunwind of its LLVM and the glibc of its runtime, so outside the Flatpak it starts only on a host that has those libraries and a glibc with every symbol version they and the program reference, 2.38 or newer for the bundled libc++. A build lays the program out as a Flatpak of its own when it is packaged:
@ -136,13 +180,21 @@ Configuration* sibling = LocalProject({ .projectFile = "../Crafter.Math/project.
cfg.dependencies = { math }; cfg.dependencies = { math };
``` ```
The sibling's `CrafterBuildProject` gets the args this `project.cpp` was loaded with (`ProjectArgs()`), so `--target=`, `--march=`, `--debug` and the project's own flags reach every sibling down the chain. That is all of them: `cfg.projectArgs` holds only what `ApplyStandardArgs` did not interpret, and passing it on would drop the target and the build type. Set `.args` to send something else: `{}` for nothing, or a list built from the project's args, such as one without a flag only this project reads. Build such a list once and give it to every sibling. A project that two paths load with different lists would end up in the program twice, so the build stops with an error that names both paths. Releases up to 2.2.0 forward nothing when `.args` is left out. The sibling's entry point gets the args this `project.cpp` was loaded with (`ProjectArgs()`), so `--target=`, `--march=`, `--debug` and the project's own flags reach every sibling down the chain. That is all of them: `cfg.projectArgs` holds only what `ApplyStandardArgs` did not interpret, and passing it on would drop the target and the build type. Set `.args` to send something else: `{}` for nothing, or a list built from the project's args, such as one without a flag only this project reads. Build such a list once and give it to every sibling. A project that two paths load with different lists would end up in the program twice, so the build stops with an error that names both paths. Releases up to 2.2.0 forward nothing when `.args` is left out.
The default is read when the spec is made, so make it inside `CrafterBuildProject`, on the thread that calls it. A spec made at namespace scope, on a thread of your own or in a script forwards nothing. The default is read when the spec is made, so make it inside the entry point, `CrafterBuildProject` or `CrafterBuildConfigurations`, on the thread that calls it. A spec made at namespace scope, on a thread of your own or in a script forwards nothing.
Clones share the external cache and identical specs share one `Configuration`. An unpinned source is `git pull`ed on every invocation, set `.commit` for reproducible, offline-after-first-fetch builds. Clones share the external cache and identical specs share one `Configuration`. An unpinned source is `git pull`ed on every invocation, set `.commit` for reproducible, offline-after-first-fetch builds.
A project loaded this way runs its `CrafterBuildProject` as a dependency, and `IsTopLevelProject()` is `false` while it does (`true` in the project crafter-build was pointed at, and outside any project file). Wire test-only dependencies behind it: a library whose test links crafter-build itself would otherwise load crafter-build's `project.cpp` from inside crafter-build and recurse. A sibling whose project file returns [several configurations](#several-configurations) gives its first one, or the one `.configuration` names by its `name`:
```cpp
Configuration* packer = LocalProject({ .projectFile = "../tools/project.cpp", .configuration = "packer" });
```
Only the configuration a consumer names is built for it, with what that one depends on, and specs that differ only in `.configuration` share one load of the sibling. A name the sibling does not have is an error that lists the ones it has, and a name two of its configurations share is an error too. A library that comes to return a second configuration puts it after the first, so that a consumer that names none keeps getting the one it had. Its consumers then need a crafter-build that has `CrafterBuildConfigurations`: an older one loads only a `CrafterBuildProject`.
A project loaded this way runs its entry point as a dependency, and `IsTopLevelProject()` is `false` while it does (`true` in the project crafter-build was pointed at, and outside any project file). Wire test-only dependencies behind it: a library whose test links crafter-build itself would otherwise load crafter-build's `project.cpp` from inside crafter-build and recurse.
### Dependencies of project.cpp itself ### Dependencies of project.cpp itself
@ -159,7 +211,7 @@ import Crafter.LintRules;
#pragma crafter dependency "<source>" [branch "<name>" | commit "<sha>"] [project "<file>"] #pragma crafter dependency "<source>" [branch "<name>" | commit "<sha>"] [project "<file>"]
``` ```
Such a dependency is built for the host, whatever the project targets, and its `project.cpp` gets none of the project's args. Such a dependency is built for the host, whatever the project targets, and its `project.cpp` gets none of the project's args. Of a project that returns [several configurations](#several-configurations), the dependency is the first.
### Dependency notes for agents ### Dependency notes for agents
@ -195,7 +247,7 @@ What a build then writes:
- The section goes after the template's text and a blank line. To have it elsewhere, put the begin and end marker lines in the template where it should go: whatever they enclose is replaced. A template moved over from a committed `AGENTS.md` has them where its section was, around notes that can be deleted. - The section goes after the template's text and a blank line. To have it elsewhere, put the begin and end marker lines in the template where it should go: whatever they enclose is replaced. A template moved over from a committed `AGENTS.md` has them where its section was, around notes that can be deleted.
- `AGENTS.md` is written only when its text changes, in place, so a `CLAUDE.md` linked to it, its ownership and a read-only `AGENTS.md` are respected. An edit made in it is gone after the next build, and a fresh clone has none until its first build. The template is never written. - `AGENTS.md` is written only when its text changes, in place, so a `CLAUDE.md` linked to it, its ownership and a read-only `AGENTS.md` are respected. An edit made in it is gone after the next build, and a fresh clone has none until its first build. The template is never written.
- An `AGENTS.md` that no build wrote, such as a committed one still next to a new template, is never replaced: each build warns instead, until it is deleted. Without a template, `AGENTS.md` is never touched. One that still holds a section from crafter-build 3.0.0, which kept the notes in a committed `AGENTS.md`, gets a warning: move it over as above, or delete the section. - An `AGENTS.md` that no build wrote, such as a committed one still next to a new template, is never replaced: each build warns instead, until it is deleted. Without a template, `AGENTS.md` is never touched. One that still holds a section from crafter-build 3.0.0, which kept the notes in a committed `AGENTS.md`, gets a warning: move it over as above, or delete the section.
- The update runs once `project.cpp` has loaded and before the project compiles, so a build that fails still has the notes. A `project.cpp` that fails to load gets no update. `test --list`, `lint`, `format`, `run` and `clean` leave the file alone, and so does an `install` or `bundle` refused before building. - In a build or a test run that is not refused before it starts, the update runs once `project.cpp` has loaded and before the project compiles, so a build that fails while it compiles still has the notes. It does not run for a `project.cpp` that fails to load, for `test --list`, `lint`, `format`, `run` and `clean`, or for a verb that is refused before it starts a build: an `install` or `bundle` of a project that is not packaged as a Flatpak, or for which crafter-build cannot run the host's flatpak, and a build, `test`, `install` or `bundle` of [several configurations](#several-configurations) that are refused as a set.
- The section follows what the build loaded: a dependency that only some flags pull in (a `--target=`, a flag of the project's own) comes and goes with them. An unpinned dependency's notes follow its default branch, as its code does, so the section can change on any build. Pin a `commit` to decide when. - The section follows what the build loaded: a dependency that only some flags pull in (a `--target=`, a flag of the project's own) comes and goes with them. An unpinned dependency's notes follow its default branch, as its code does, so the section can change on any build. Pin a `commit` to decide when.
- A library reached through two specs, pinned at one revision here and another further down, is described once, as the nearer one has it. - A library reached through two specs, pinned at one revision here and another further down, is described once, as the nearer one has it.
- When there are notes to write, unbalanced markers in the template leave `AGENTS.md` as it is, with a warning. A notes file that would break the section (a code block it never closes, a marker line of its own) is left out, also with a warning. Markers quoted inside a fenced code block don't count. - When there are notes to write, unbalanced markers in the template leave `AGENTS.md` as it is, with a warning. A notes file that would break the section (a code block it never closes, a marker line of its own) is left out, also with a warning. Markers quoted inside a fenced code block don't count.
@ -210,6 +262,8 @@ What a build then writes:
Three things never count: the project's own `AGENTS.consumer.md`; its tests' dependencies, since a plain build never looks at the tests; and a dependency's `#pragma crafter dependency` tooling, which is about how the dependency is built, not how it is used. Three things never count: the project's own `AGENTS.consumer.md`; its tests' dependencies, since a plain build never looks at the tests; and a dependency's `#pragma crafter dependency` tooling, which is about how the dependency is built, not how it is used.
In a project of [several configurations](#several-configurations), the `dependencyNotes` of each one the file made itself selects among what that one is built against, and a dependency's notes are in when a configuration that asks for them leads to it: what a `Direct` configuration names is followed further when a `Transitive` one reaches it too. The project's own `#pragma crafter dependency` tooling, and a configuration of another project that the file returns, which counts as a dependency here, go by the widest setting among the configurations the file made itself.
## CUDA ## CUDA
`cfg.cuda` lists CUDA sources, named without `.cu`. A relative name is read from the directory crafter-build runs in, except in a project loaded through `GitProject` or `LocalProject`, whose relative names crafter-build rebases onto that project's directory: `cfg.cuda` lists CUDA sources, named without `.cu`. A relative name is read from the directory crafter-build runs in, except in a project loaded through `GitProject` or `LocalProject`, whose relative names crafter-build rebases onto that project's directory:
@ -427,7 +481,7 @@ cfg.scripts.push_back({"package", [](Crafter::ScriptContext& ctx) {
| Member | Meaning | | Member | Meaning |
|---|---| |---|---|
| `project` | the `Configuration` that `project.cpp` returned. Read the project through this; a `cfg` captured by reference is gone by the time a script runs | | `project` | 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 through a `cfg` captured by reference |
| `projectDir` | absolute directory of the project file | | `projectDir` | absolute directory of the project file |
| `args` | everything typed after the script's name, untouched | | `args` | everything typed after the script's name, untouched |
| `Run(command)` | run a shell command with the terminal attached and return its exit code | | `Run(command)` | run a shell command with the terminal attached and return its exit code |

View file

@ -12,9 +12,10 @@
<description> <description>
<p> <p>
A build system for C++26 modules. Project descriptions are C++: a A build system for C++26 modules. Project descriptions are C++: a
project.cpp constructs a Configuration and returns it, and crafter-build project.cpp constructs a Configuration, or several, and returns them,
compiles, loads and executes it. The Flatpak carries its own LLVM, so and crafter-build compiles, loads and executes it. The Flatpak carries
every machine builds with the same clang, lld, libc++ and WASI sysroot. its own LLVM, so every machine builds with the same clang, lld, libc++
and WASI sysroot.
</p> </p>
</description> </description>
<url type="homepage">https://forgejo.catcrafts.net/Catcrafts/Crafter.Build</url> <url type="homepage">https://forgejo.catcrafts.net/Catcrafts/Crafter.Build</url>