test: several configurations from one project file

BuildAll builds configurations the test makes itself, on a fresh copy of
its fixture outside the checkout. A library, a program built on it and
an unrelated program all come out built, each in the record of the pass
under its PcmDir, and a list that names one configuration twice builds
it from nothing, what the build before left of it being removed first,
and leaves it in the record of the pass once. A library built on another
library of its own name, path and type, so that the two are built into
one directory, builds, with both archives in that directory; returned
beside a program that is built on it, the pair is refused, both by name,
before the program is built or anything is recorded. Two libraries that
are each built on a configuration of the other's directory are refused
by name, and nothing of them is built or recorded. A wasm32 library that
names no sysroot and a program for the host built on it leave the
library in the record once, under the directory it has after the pass,
and its refusal comes back once; it is refused for its -fno-exceptions,
so the case needs no WASI sysroot. Of two programs built on a library
that does not compile and a third that has nothing to do with it, the
third is built, the two are not, and the library's error comes back
once. Two configurations that would be built into one directory are
refused, and neither is built. With a wasm executable that declares a
variant among them, a program of one source is linked no earlier than
the program and library given before it; the wasm one is refused for its
-fno-exceptions before it builds anything, so the case needs no WASI
sysroot.

ProjectConfigurations loads fixture project files staged outside the
checkout. A file with CrafterBuildConfigurations comes back as the three
configurations it returns, with the pointer the file took from the
second to the first intact, and as two when its args say so; LoadProject
refuses it. A file with CrafterBuildProject comes back as a list of one
whose test still depends on that Configuration, and that test builds and
passes. A file with both entry points, one with neither, and ones whose
CrafterBuildConfigurations returns nothing, a null pointer or one
configuration twice are refused, each with a message that says which.
Then crafter-build's verbs, through Run() and RunTests, on the file of
three: a build builds all three; -r refuses to pick between its two
executables, and runs the one executable there is when the file returns
two configurations; RunTests runs the tests of the first and the third
configuration in one run, and when the first declares the third's test
as well, that test fails for both, naming the two, and is not built,
while the first's own test passes, and a list that names the first
configuration twice runs its test once; with a fourth configuration of
the third's name and path, a test run and a build are each refused for
the two that would be built into one directory, and neither writes the
AGENTS.md that a build which is not refused writes from the template the
test puts beside the project file; `run` reaches a script of the first
and of the third configuration and hands each the configuration that
declares it; install refuses the project while none of the three is
packaged, bundle refuses it when two are, and with one packaged
executable beside the library bundle does not take the library for the
app.

SiblingConfigurations loads such a file as another project's sibling. A
spec without `configuration` gets the first, a spec that names one gets
that one, and the three specs share one load: the dependency of the
second is the first as LocalProject returned it. All three are rebased
onto the sibling's directory. A name the sibling does not have throws
with the names it has, and a name that two configurations of another
fixture share throws too, while a spec without a name still gets the
first of those two. Building the second builds the first with it and
leaves the third unbuilt. A file that returns something a sibling keeps
to itself is refused when it is loaded, the configuration and the
sibling named: the library the sibling's program is built on, the
configuration of that program's test, and a helper only that test is
built on, none of which the sibling's file returns. A project that
returns the sibling's third configuration beside one of its own builds
both, cannot run the sibling's script and runs its own, and its test run
leaves the sibling's test alone.

DependencyNotes gains a project of two configurations and a fixture,
outer, that builds against lib: two Direct configurations give what each
of them names; a project a Direct one names is followed when a
Transitive one leads to it; a configuration under None keeps out what
only it is built against; with both under None, AGENTS.md is the
template's text; and a returned configuration of another project is one
of the projects the notes come from, followed further only under
Transitive, which is also what a file that made no configuration gets.
For outer's own AGENTS.md, written from the Configuration LocalProject
returned and outer's project file, outer's own setting selects: lib
under Direct, lib and base under Transitive, nothing under None.
Script runs a script of each of two configurations, each handed the
configuration that declares it, refuses a name both declare without
running anything, and, given a list that names one configuration twice,
runs the script asked for, which a name declared twice would have kept
it from. Lint formats the sources of two configurations in one run,
counts a rule both register as one, and runs a rule only one of them
registers over the sources of both. Of two returned configurations that
register a rule under one name the earlier one's runs, over the sources
of all three, though the configuration returned ahead of both depends
on the later one.
This commit is contained in:
Jorijn van der Graaf 2026-11-15 02:16:07 +01:00
commit 5795b666f5
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
42 changed files with 1427 additions and 5 deletions

View file

@ -310,13 +310,13 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmSysrootKey").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("RunSingleTestExit").Dependencies({ CrafterBuildLib.get() });
// LoadProject dlopens the synthesized project.so, which references
// Crafter:: symbols (HostTarget, Configuration ctors) that have to be
// visible from the test exe — same wiring crafter-build itself uses
// for project.so.
// Loading a project file dlopens the project.so compiled from it,
// which references Crafter:: symbols (HostTarget, Configuration
// ctors) that have to be visible from the test exe — same wiring
// crafter-build itself uses for project.so.
cfg.AddTest("ConcurrentCacheRace").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
// Same LoadProject wiring as ConcurrentCacheRace above.
// Same wiring as ConcurrentCacheRace above.
cfg.AddTest("HostCacheSourceStamp").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("HostCacheToolchainKey").Dependencies({ CrafterBuildLib.get() })
@ -337,10 +337,15 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("SiblingOwnPointers").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("ProjectConfigurations").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("SiblingConfigurations").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("ConcurrentDependencyReset").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("JobGroup").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("JobLimit").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("ImportCycle").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("BuildAll").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("Lint").Dependencies({ CrafterBuildLib.get() });
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Broken;
import std;
// Does not compile: the library two of the test's programs are built on.
export std::int32_t Value() {
return Undeclared;
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Broken;
int main() {
return Value();
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("good");
return 0;
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Greeter;
import std;
export namespace Greeter {
std::string Greet(std::string_view name) {
return std::format("hello {}", name);
}
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Greeter;
int main() {
std::println("{}", Greeter::Greet("world"));
return 0;
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Broken;
int main() {
return Value();
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("tool");
return 0;
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Lower;
import std;
// Built by a configuration with the name, path and type of the one that
// builds Upper, which is built on this one.
export std::int32_t Lower() {
return 1;
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Upper;
import std;
import Lower;
export std::int32_t Upper() {
return Lower() + 1;
}

273
tests/BuildAll/main.cpp Normal file
View file

@ -0,0 +1,273 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// BuildAll builds the configurations a project file returns, which need not
// have anything to do with each other, in one pass: every one of them, and
// what two of them share once.
//
// What it guards against is a configuration that goes missing or is built
// wrongly without a word: left unbuilt because another one failed, or
// because it shares an output directory with another and was taken for it;
// built twice at once because another one depends on it, each build
// writing what the other reads; and built at once with a wasm executable
// that declares variants, which rewrites the configurations it is built on
// while it builds. And against a build that never ends: a configuration
// waiting for a build of its own directory that is waiting for it.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
std::size_t Count(std::string_view text, std::string_view needle) {
std::size_t count = 0;
for (std::size_t pos = text.find(needle); pos != std::string_view::npos; pos = text.find(needle, pos + needle.size())) ++count;
return count;
}
// A library made of `dir`/`interface`.cppm.
void MakeLibrary(Configuration& cfg, const fs::path& dir, std::string_view name, std::string_view interface) {
cfg.path = dir;
cfg.name = name;
cfg.outputName = name;
cfg.target = HostTarget();
cfg.type = ConfigurationType::LibraryStatic;
std::array<fs::path, 1> interfaces = { interface };
std::array<fs::path, 0> implementations = {};
cfg.GetInterfacesAndImplementations(interfaces, implementations);
}
// A program made of `dir`/main.cpp.
void MakeProgram(Configuration& cfg, const fs::path& dir, std::string_view name) {
cfg.path = dir;
cfg.name = name;
cfg.outputName = name;
cfg.target = HostTarget();
cfg.type = ConfigurationType::Executable;
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "main" };
cfg.GetInterfacesAndImplementations(interfaces, implementations);
}
}
int main() {
// Staged: the cases below compare what a build left behind, so each run
// starts from sources alone.
const fs::path scratch = fs::temp_directory_path() / "crafter-build-build-all";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::copy(fs::current_path() / "tests" / "BuildAll" / "fixture", scratch, fs::copy_options::recursive);
// Every one of them is built and left in the record of the pass, where
// a configuration that depends on it looks for it. hello depends on
// greeter, and greeter built from here as well as through hello was two
// builds of it at once, one of which archived the objects while the
// other was still writing them.
{
Configuration greeter;
MakeLibrary(greeter, scratch / "greeter", "greeter", "Greeter");
Configuration hello;
hello.dependencies = { &greeter };
MakeProgram(hello, scratch / "hello", "hello");
Configuration tool;
MakeProgram(tool, scratch / "tool", "tool");
std::array<Configuration*, 3> all = { &greeter, &hello, &tool };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(all, depResults, depMutex);
Check(failed.empty(), std::format("a library, a program built on it and an unrelated program build: {}", failed));
for (const Configuration* cfg : all) {
Check(fs::exists(ExpectedOutput(*cfg)), std::format("{} was built", cfg->name));
Check(depResults.contains(cfg->PcmDir()), std::format("{} is in the record of the pass, where a configuration that depends on it finds its build", cfg->name));
}
// A list that names one of them twice builds it like any other:
// once, from nothing here, since what the build above left of it is
// removed first.
fs::remove_all(tool.BinDir(), ec);
fs::remove_all(tool.BuildDir(), ec);
std::array<Configuration*, 2> twice = { &tool, &tool };
std::unordered_map<fs::path, std::shared_future<BuildResult>> twiceResults;
Check(BuildAll(twice, twiceResults, depMutex).empty() && fs::exists(ExpectedOutput(tool)), "a configuration the list names twice is built");
Check(twiceResults.size() == 1 && twiceResults.contains(tool.PcmDir()), std::format("and is in the record of the pass once ({} entries)", twiceResults.size()));
}
// A configuration built on another of its own name, path and type: the
// two are built into one directory, the dependency first, as they were
// when a project had one configuration and Build() was all there was.
// In the record of the pass while it is still being built, the first
// would be what the record hands its dependency, which would wait for
// it while it waits for the dependency: a build that never ends.
{
Configuration lower;
MakeLibrary(lower, scratch / "twins", "twins", "Lower");
lower.outputName = "lower";
Configuration upper;
upper.dependencies = { &lower };
MakeLibrary(upper, scratch / "twins", "twins", "Upper");
std::array<Configuration*, 1> one = { &upper };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(one, depResults, depMutex);
Check(failed.empty(), std::format("a configuration built on another of the same directory builds: {}", failed));
Check(upper.PcmDir() == lower.PcmDir() && fs::exists(ExpectedOutput(upper)) && fs::exists(ExpectedOutput(lower)), "both into that one directory");
// With a program built on the upper one returned beside it, that
// directory holds two builds and the record of the pass one result:
// the program would be handed the lower one's, and be linked without
// the library it names.
Configuration program;
program.dependencies = { &upper };
MakeProgram(program, scratch / "good", "on-twins");
std::array<Configuration*, 2> both = { &program, &upper };
std::unordered_map<fs::path, std::shared_future<BuildResult>> bothResults;
const std::string refused = BuildAll(both, bothResults, depMutex);
Check(refused.contains("twins and a configuration it is built on would both be built into") && refused.contains("where on-twins looks"), std::format("a configuration built on that one, returned beside it, is refused, both by name: {}", refused));
Check(bothResults.empty() && !fs::exists(program.BuildDir()) && !fs::exists(program.BinDir()), "and refused before anything is built");
}
// Two that are each built on something of the other's directory cannot
// be put in an order, and are refused before either is built. Nothing
// here has a source: the refusal comes first.
{
const auto named = [&](Configuration& cfg, std::string_view name) {
cfg.path = scratch / "crossed";
cfg.name = name;
cfg.outputName = name;
cfg.target = HostTarget();
cfg.type = ConfigurationType::LibraryStatic;
};
Configuration left;
Configuration right;
Configuration leftAgain;
Configuration rightAgain;
named(left, "left");
named(right, "right");
named(leftAgain, "left");
named(rightAgain, "right");
left.dependencies = { &rightAgain };
right.dependencies = { &leftAgain };
std::array<Configuration*, 2> crossed = { &left, &right };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string refused = BuildAll(crossed, depResults, depMutex);
Check(refused.contains("left, right") && refused.contains("none of them can be built first"), std::format("two configurations that each wait for the other's directory are refused, by name: {}", refused));
Check(depResults.empty() && !fs::exists(scratch / "crossed" / "bin") && !fs::exists(scratch / "crossed" / "build"), "and refused before anything is built");
}
// A wasm32 library whose target is set here and not by
// ApplyStandardArgs, so that it names no sysroot, and a program built on
// it: the program finds the library's build in the record, under the
// directory the library has before its build and after it alike. The
// library has a flag no wasm build accepts, so its build is refused on
// every machine, with a WASI sysroot or without, and a refused build is
// recorded like any other. The program is one for the host, whose own
// build gets as far as asking for the library wherever the test runs.
{
Configuration library;
MakeLibrary(library, scratch / "greeter", "wasm-greeter", "Greeter");
library.target = "wasm32-wasip1";
library.compileFlags.push_back("-fno-exceptions");
Configuration program;
program.dependencies = { &library };
MakeProgram(program, scratch / "hello", "on-wasm-greeter");
std::array<Configuration*, 2> all = { &program, &library };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(all, depResults, depMutex);
Check(Count(failed, "wasm-greeter: -fno-exceptions") == 1, std::format("the library's refusal comes back once, for the two of them: {}", failed));
const auto recorded = std::ranges::count_if(depResults, [](const auto& entry) { return entry.first.filename().string().starts_with("wasm-greeter-"); });
Check(recorded == 1 && depResults.contains(library.PcmDir()), std::format("and the library is in the record of the pass once, under the directory it has now ({} times)", recorded));
}
// One that fails stops none of the others, and what failed is said
// once: two programs built on a library that does not compile, and a
// third that has nothing to do with it.
{
Configuration broken;
MakeLibrary(broken, scratch / "broken", "broken", "Broken");
Configuration first;
first.dependencies = { &broken };
MakeProgram(first, scratch / "first", "first");
Configuration second;
second.dependencies = { &broken };
MakeProgram(second, scratch / "second", "second");
Configuration good;
MakeProgram(good, scratch / "good", "good");
std::array<Configuration*, 3> all = { &first, &second, &good };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(all, depResults, depMutex);
Check(failed.contains("Undeclared"), std::format("the library's compile error comes back: {}", failed));
Check(Count(failed, "error:") == 1, std::format("a dependency that failed for two configurations is reported once: {}", failed));
Check(fs::exists(ExpectedOutput(good)), "a configuration that has nothing to do with the failure is still built");
Check(!fs::exists(ExpectedOutput(first)) && !fs::exists(ExpectedOutput(second)), "the two built on the broken library are not");
// Two configurations in one directory would be one piece of work to
// the pass, which would build the first and hand the second its
// result.
Configuration twin;
MakeProgram(twin, scratch / "good", "good");
std::array<Configuration*, 2> clash = { &good, &twin };
fs::remove_all(good.BinDir(), ec);
fs::remove_all(good.BuildDir(), ec);
std::unordered_map<fs::path, std::shared_future<BuildResult>> clashResults;
const std::string refused = BuildAll(clash, clashResults, depMutex);
Check(refused.contains("would both be built into"), std::format("two configurations that build into one directory are refused: {}", refused));
Check(!fs::exists(good.BinDir()), "and refused before either is built");
}
// A wasm executable with variants rewrites the configurations it is
// built on, once for each variant, so with one among them the
// configurations are built one after the other: built at once, a
// library two of them share would change under the other's build. The
// slow one comes first here, a program and the library it is built on,
// and the quick one, a single source, is only linked once the slow one
// is. The wasm one is refused before it builds anything, on every
// machine, for its -fno-exceptions, so no wasm build is waited for.
{
Configuration library;
MakeLibrary(library, scratch / "greeter", "ordered-greeter", "Greeter");
Configuration slow;
slow.dependencies = { &library };
MakeProgram(slow, scratch / "hello", "ordered-slow");
Configuration quick;
MakeProgram(quick, scratch / "good", "ordered-quick");
Configuration variants;
MakeProgram(variants, scratch / "good", "ordered-variants");
variants.target = "wasm32-wasip1";
variants.compileFlags.push_back("-fno-exceptions");
EnableWasiRelaxedSimdVariant(variants);
std::array<Configuration*, 3> all = { &slow, &quick, &variants };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(all, depResults, depMutex);
Check(failed.contains("-fno-exceptions"), std::format("the wasm configuration is the one that is refused: {}", failed));
const fs::file_time_type slowBuilt = fs::last_write_time(ExpectedOutput(slow), ec);
Check(!ec, "the slow configuration is built");
const fs::file_time_type quickBuilt = fs::last_write_time(ExpectedOutput(quick), ec);
Check(!ec && quickBuilt >= slowBuilt, "with a wasm executable that declares variants among them, a configuration is built once the one before it is");
}
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}

View file

@ -0,0 +1,3 @@
## outer
outer wraps lib.

View file

@ -0,0 +1,21 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// A project that builds against lib, and so against base one ring further
// out. app does not name it: the test loads it for the cases in which a
// project has several configurations, where it is what a second
// configuration names.
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view>) {
Configuration cfg;
cfg.path = "./";
cfg.name = "outer";
cfg.outputName = "outer";
cfg.type = ConfigurationType::LibraryStatic;
// Loaded from outer/, so this resolves against it.
cfg.dependencies = { LocalProject({ .projectFile = "../lib/project.cpp" }) };
return cfg;
}

View file

@ -294,6 +294,86 @@ int main() {
Check(Read(agents) == generated, "under None, AGENTS.md holds the template's text alone");
cfg.dependencyNotes = DependencyNotes::Transitive;
// A project file that returns several configurations: each one's setting
// selects among what that one is built against, and the notes are
// merged. `second` stands in for a second configuration of app's project
// file, and outer is a project that builds against lib, and so against
// base one ring further out.
{
Configuration* outer = nullptr;
try {
outer = LocalProject({ .projectFile = scratch / "outer" / "project.cpp" });
} catch (const std::exception& e) {
std::println(std::cerr, "FAIL: loading outer threw: {}", e.what());
return 1;
}
Configuration* const baseProject = cfg.dependencies[0]->dependencies[0];
Configuration second;
std::array<const Configuration*, 2> both = { &cfg, &second };
// The projects the section names, in its order, once AGENTS.md is
// written for `configurations`.
const auto named = [&](std::span<const Configuration* const> configurations) {
(void)UpdateAgentsMd(configurations, projectFile);
const std::string now = Read(agents);
constexpr std::string_view from = "<!-- from ";
std::vector<std::string> names;
for (std::size_t pos = now.find(from); pos != std::string::npos; pos = now.find(from, pos + 1)) {
names.push_back(now.substr(pos + from.size(), now.find(" -->", pos) - pos - from.size()));
}
return names;
};
using Names = std::vector<std::string>;
cfg.dependencyNotes = DependencyNotes::Direct;
second.dependencies = { outer };
second.dependencyNotes = DependencyNotes::Direct;
Check(named(both) == Names{"tool", "lib", "outer"}, std::format("two Direct configurations give what each of them names (got {})", named(both)));
// lib is in the first ring through app, which asks for no more. The
// second configuration reaches it through outer and asks for
// everything, so what lib builds against is in.
second.dependencyNotes = DependencyNotes::Transitive;
Check(named(both) == Names{"tool", "lib", "outer", "base"}, std::format("a project a Direct configuration names is followed when a Transitive one leads to it (got {})", named(both)));
cfg.dependencyNotes = DependencyNotes::None;
second.dependencies = { baseProject };
Check(named(both) == Names{"tool", "base"}, std::format("a configuration under None keeps out what only it is built against (got {})", named(both)));
second.dependencyNotes = DependencyNotes::None;
Check(named(both).empty() && Read(agents) == generated, "with every configuration under None, AGENTS.md holds the template's text alone");
// A configuration the file loaded from another project and returns
// is one of the projects it is built against, and its own setting
// counts for nothing here: it goes by the widest of the others.
std::array<const Configuration*, 2> withOuter = { &cfg, outer };
cfg.dependencyNotes = DependencyNotes::Direct;
Check(named(withOuter) == Names{"tool", "lib", "outer"}, std::format("a returned configuration of another project is in the first ring, and under Direct no more of it (got {})", named(withOuter)));
cfg.dependencyNotes = DependencyNotes::Transitive;
Check(named(withOuter) == Names{"tool", "lib", "outer", "base"}, std::format("under Transitive what it builds against follows (got {})", named(withOuter)));
std::array<const Configuration*, 1> onlyOuter = { outer };
Check(named(onlyOuter) == Names{"tool", "outer", "lib", "base"}, std::format("a file that made no configuration itself gets everything (got {})", named(onlyOuter)));
// The same Configuration, for outer's own AGENTS.md: outer's project
// file made it, so here its setting is the one that selects, though
// a loader holds it. Told apart by being held, it was another
// project's configuration to its own file, and got everything.
const fs::path outerFile = scratch / "outer" / "project.cpp";
const fs::path outerAgents = scratch / "outer" / "AGENTS.md";
Write(scratch / "outer" / "AGENTS.template.md", templateText);
const auto outerNamed = [&](DependencyNotes setting) {
outer->dependencyNotes = setting;
(void)UpdateAgentsMd(*outer, outerFile);
const std::string now = Read(outerAgents);
constexpr std::string_view from = "<!-- from ";
Names names;
for (std::size_t pos = now.find(from); pos != std::string::npos; pos = now.find(from, pos + 1)) {
names.push_back(now.substr(pos + from.size(), now.find(" -->", pos) - pos - from.size()));
}
return names;
};
Check(outerNamed(DependencyNotes::Direct) == Names{"lib"}, std::format("a project loaded by another gets what its own Direct selects in its own AGENTS.md (got {})", outerNamed(DependencyNotes::Direct)));
Check(outerNamed(DependencyNotes::Transitive) == Names{"lib", "base"}, std::format("and what its Transitive does (got {})", outerNamed(DependencyNotes::Transitive)));
Check(outerNamed(DependencyNotes::None).empty() && Read(outerAgents) == generated, "and under None the template's text alone");
outer->dependencyNotes = DependencyNotes::Transitive;
}
// Linked to the template, AGENTS.md would have every build write the
// file that is committed.
fs::remove(agents);

View file

@ -246,6 +246,52 @@ int main() {
Check(s.Read("a") == "hello \n", "Check mode never writes");
}
// The configurations a project file returns: one run over the rules and
// the sources of all of them. The second lies inside the first's
// directory, which is the project root when no project file is given.
{
Scratch first("several");
Scratch second("several/second");
first.Write("a", "one \n");
second.Write("b", "two \n");
Configuration one = first.Config({"a"});
Configuration two = second.Config({"b"});
AddTrimRule(one);
AddTrimRule(two);
std::int32_t noted = 0;
two.AddLintRule("note", [&noted](LintContext&) { ++noted; });
std::array<Configuration*, 2> both = { &one, &two };
LintSummary sum = RunLint(both, Mode(LintMode::Apply));
Check(first.Read("a") == "one\n" && second.Read("b") == "two\n", "several: the sources of every configuration are formatted");
Check(sum.rulesRun == 2, "several: a rule two configurations register under one name is one rule");
Check(noted == 2, "several: a rule one configuration registers runs over the sources of all");
}
// A rule two of the returned configurations register under one name is
// the one the first of them registered, in the order the file returned
// them, whatever depends on the other: app depends on tools, so a walk
// of one configuration's graph after another's meets tools before
// `other` and would run its rule.
{
Scratch appDir("order");
Scratch otherDir("order/other");
Scratch toolsDir("order/tools");
appDir.Write("a", "app\n");
otherDir.Write("b", "other\n");
toolsDir.Write("c", "tools\n");
Configuration app = appDir.Config({"a"});
Configuration other = otherDir.Config({"b"});
Configuration tools = toolsDir.Config({"c"});
app.dependencies = { &tools };
std::atomic<std::int32_t> otherRan = 0;
std::atomic<std::int32_t> toolsRan = 0;
other.AddLintRule("pick", [&otherRan](LintContext&) { ++otherRan; });
tools.AddLintRule("pick", [&toolsRan](LintContext&) { ++toolsRan; });
std::array<Configuration*, 3> roots = { &app, &other, &tools };
const LintSummary sum = RunLint(roots, Mode(LintMode::Report));
Check(sum.rulesRun == 1 && otherRan == 3 && toolsRan == 0, std::format("several: the rule of the first configuration that registers the name runs, over every source (rules {}, the first's {} times, the later one's {} times)", sum.rulesRun, otherRan.load(), toolsRan.load()));
}
// Chaining: rule2 sees rule1's output; both attributed in Report mode.
{
Scratch s("chain");

View file

@ -0,0 +1,16 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Both entry points: whichever the loader called, what the other returns
// would go unbuilt.
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view>) {
return {};
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
return {};
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Returns nothing to build.
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
return {};
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// No entry point: the function has a name the loader does not look for.
extern "C" Configuration CrafterBuildProjects(std::span<const std::string_view>) {
return {};
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Returns a pointer to nothing.
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
return { nullptr };
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Greeter;
import std;
export namespace Greeter {
std::string Greet(std::string_view name) {
return std::format("hello {}", name);
}
}

View file

@ -0,0 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Greeter;
// -r runs this in its own directory, where it leaves what it would have
// printed for the test to find.
int main() {
std::ofstream("hello.ran") << Greeter::Greet("world");
return 0;
}

View file

@ -0,0 +1,94 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// A project of three configurations: a library, a program built on it, and
// a tool that shares nothing with either. The library and the tool each
// declare a test and a script. --no-tool leaves the tool out, which leaves
// one executable for -r to run. --packaged packages both programs as
// Flatpaks, for install and bundle to choose between, and makes them wasm
// builds, which a build refuses to package wherever the test runs: nothing
// is ever exported, let alone installed. --twin-test has the library
// declare the tool's test as well, which a test run refuses to build.
// --twin adds a fourth configuration of the tool's name and path, which
// would be built into the tool's directory.
namespace {
// One new set per call, at addresses that never change: whoever loads
// this file keeps the pointers it returns.
std::deque<Configuration> Owned;
// Leaves `<script>.ran` in the project's directory, holding the name of
// the configuration the script was handed.
std::int32_t Stamp(const ScriptContext& ctx, std::string_view script) {
std::ofstream(ctx.projectDir / std::format("{}.ran", script)) << ctx.project.name;
return 0;
}
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args) {
Configuration& greeter = Owned.emplace_back();
greeter.path = "./";
greeter.name = "greeter";
greeter.outputName = "greeter";
greeter.type = ConfigurationType::LibraryStatic;
{
std::array<fs::path, 1> interfaces = { "greeter/Greeter" };
std::array<fs::path, 0> implementations = {};
greeter.GetInterfacesAndImplementations(interfaces, implementations);
}
greeter.AddTest("GreeterTest").Dependencies({ &greeter });
if (std::ranges::contains(args, std::string_view("--twin-test"))) greeter.AddTest("ToolTest");
greeter.scripts.push_back({"greet", [](ScriptContext& ctx) { return Stamp(ctx, "greet"); }});
Configuration& hello = Owned.emplace_back();
hello.path = "./";
hello.name = "hello";
hello.outputName = "hello";
hello.type = ConfigurationType::Executable;
hello.dependencies = { &greeter };
{
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "hello/main" };
hello.GetInterfacesAndImplementations(interfaces, implementations);
}
const bool packaged = std::ranges::contains(args, std::string_view("--packaged"));
if (packaged) {
hello.packaging = Packaging::Flatpak;
hello.target = "wasm32-wasip1";
}
if (std::ranges::contains(args, std::string_view("--no-tool"))) return { &greeter, &hello };
Configuration& tool = Owned.emplace_back();
tool.path = "./";
tool.name = "tool";
tool.outputName = "tool";
tool.type = ConfigurationType::Executable;
{
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "tool/main" };
tool.GetInterfacesAndImplementations(interfaces, implementations);
}
tool.AddTest("ToolTest");
tool.scripts.push_back({"stamp", [](ScriptContext& ctx) { return Stamp(ctx, "stamp"); }});
if (packaged) {
tool.packaging = Packaging::Flatpak;
tool.target = "wasm32-wasip1";
}
if (std::ranges::contains(args, std::string_view("--twin"))) {
Configuration& twin = Owned.emplace_back();
twin.path = "./";
twin.name = "tool";
twin.outputName = "tool-twin";
twin.type = ConfigurationType::Executable;
return { &greeter, &hello, &tool, &twin };
}
return { &greeter, &hello, &tool };
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Greeter;
int main() {
return Greeter::Greet("test") == "hello test" ? 0 : 1;
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
// Leaves a file where it ran: the directory of the project whose test run
// started it.
int main() {
std::ofstream("ToolTest.ran") << "ran";
return 0;
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("tool");
return 0;
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Single;
import std;
export namespace Single {
std::int32_t Answer() {
return 42;
}
}

View file

@ -0,0 +1,22 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// The form with one configuration: a library whose test links it, so the
// test's dependency is a pointer to the Configuration this function returns.
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view>) {
Configuration cfg;
cfg.path = "./";
cfg.name = "single";
cfg.outputName = "single";
cfg.type = ConfigurationType::LibraryStatic;
std::array<fs::path, 1> interfaces = { "Single" };
std::array<fs::path, 0> implementations = {};
cfg.GetInterfacesAndImplementations(interfaces, implementations);
cfg.AddTest("SelfTest").Dependencies({ &cfg });
return cfg;
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Single;
int main() {
return Single::Answer() == 42 ? 0 : 1;
}

View file

@ -0,0 +1,17 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Returns one configuration twice, as a pasted line does.
namespace {
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& same = Owned.emplace_back();
same.name = "same";
return { &same, &same };
}

View file

@ -0,0 +1,228 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// A project file describes one configuration, returned by value from
// CrafterBuildProject, or several that need not have anything to do with
// each other, returned as pointers from CrafterBuildConfigurations, and
// crafter-build's verbs act on every one of them.
//
// Before the second entry point a project had one configuration, and
// whatever else it wanted built had to be made a dependency of that one.
// What this guards against is one of several going missing without a word:
// left out because the file returned it twice by mistake, or because a verb
// only looks at the first, so that its tests never run and its scripts
// cannot be reached; a verb that needs one of them picking one by guessing;
// and a test that two of them declare being built for both at once, in the
// one directory a test of that name has. And the form with one
// configuration keeps what it relied on all along: a pointer the file took
// to the Configuration it returns, which a test that links the project's
// library holds, still points at it once the file is loaded.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
std::string Read(const fs::path& file) {
std::ifstream in(file, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
}
// What `call` throws, or "" when it returns.
std::string Thrown(const std::function<void()>& call) {
try {
call();
} catch (const std::exception& e) {
return e.what();
}
return "";
}
// Run() as the command line reaches it, and what it wrote to stderr.
struct Ran {
std::int32_t status = 0;
std::string err;
};
Ran RunWith(std::vector<std::string> args) {
std::vector<char*> argv;
for (std::string& arg : args) argv.push_back(arg.data());
std::ostringstream err;
std::streambuf* const saved = std::cerr.rdbuf(err.rdbuf());
const std::int32_t status = Run(static_cast<std::int32_t>(argv.size()), argv.data());
std::cerr.rdbuf(saved);
return { status, err.str() };
}
std::vector<std::string> Names(std::span<Configuration* const> configurations) {
std::vector<std::string> names;
for (const Configuration* cfg : configurations) names.push_back(cfg->name);
return names;
}
}
int main() {
const fs::path installedHome = GetCrafterBuildHome();
if (!fs::exists(installedHome / "Crafter.Build.cppm")) {
std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string());
return 77;
}
// Staged because every load drops a build/ tree next to its project
// file.
const fs::path home = fs::current_path();
const fs::path scratch = fs::temp_directory_path() / "crafter-build-project-configurations";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::copy(home / "tests" / "ProjectConfigurations" / "fixture", scratch, fs::copy_options::recursive);
std::array<std::string_view, 0> noArgs = {};
std::array<std::string_view, 1> noTool = { "--no-tool" };
// The loader: what each entry point comes back as. From the project's
// own directory, as crafter-build runs it: the fixtures name their
// sources relative to it.
fs::current_path(scratch / "several");
std::vector<Configuration*> several;
try {
several = LoadConfigurations("project.cpp", noArgs);
} catch (const std::exception& e) {
std::println(std::cerr, "FAIL: loading a project of several configurations threw: {}", e.what());
return 1;
}
Check(Names(several) == std::vector<std::string>{"greeter", "hello", "tool"}, std::format("CrafterBuildConfigurations comes back as the file returned it (got {})", Names(several)));
// Every case below reads the three of them.
if (several.size() != 3) return 1;
Check(several[1]->dependencies == std::vector<Configuration*>{several[0]}, "the pointers the file took between its configurations are the ones it returned");
Check(Names(LoadConfigurations("project.cpp", noTool)) == std::vector<std::string>{"greeter", "hello"}, "the file decides what it returns from its args");
Check(Thrown([&] { (void)LoadProject("project.cpp", noArgs); }).contains("LoadConfigurations"), "LoadProject refuses a file of several configurations and names the function that loads it");
fs::current_path(scratch / "single");
{
const std::vector<Configuration*> single = LoadConfigurations("project.cpp", noArgs);
Check(Names(single) == std::vector<std::string>{"single"}, "CrafterBuildProject comes back as a list of one");
// The file handed its test `&cfg`. Moved after the call returned
// it, the Configuration would leave that pointer behind.
Check(single.size() == 1 && single[0]->tests.size() == 1 && single[0]->tests[0].config.dependencies == std::vector<Configuration*>{single[0]}, "a pointer the file took to the Configuration it returns points at the one that is loaded");
// And the test builds through that pointer.
RunTestsOptions opts;
opts.targetFilter = HostTarget();
const TestSummary summary = RunTests(single, opts);
Check(summary.passed == 1 && summary.AllPassed(), std::format("a test that links the project's own library through that pointer builds and passes ({} passed, {} failed, {} crashed)", summary.passed, summary.failed, summary.crashed));
Check(LoadProject("project.cpp", noArgs).name == "single", "LoadProject still returns a CrafterBuildProject's Configuration by value");
}
// A file the loader cannot make sense of says so when it is loaded. By
// now this process holds project files with each entry point, so these
// also show that one file's entry point is never taken for another's.
for (const auto& [project, expected] : std::initializer_list<std::pair<std::string_view, std::string_view>>{
{"both", "defines both CrafterBuildProject and CrafterBuildConfigurations"},
{"neither", "defines neither CrafterBuildProject nor CrafterBuildConfigurations"},
{"empty", "returned no configuration"},
{"null", "returned a null pointer"},
{"twice", "returned same twice"},
}) {
const std::string thrown = Thrown([&] { (void)LoadConfigurations(scratch / project / "project.cpp", noArgs); });
Check(thrown.contains(expected), std::format("{}/project.cpp is refused, saying '{}' (got '{}')", project, expected, thrown));
}
// The verbs, over the project crafter-build is pointed at. A build
// builds every one of them.
fs::current_path(scratch / "several");
Check(RunWith({"crafter-build", "-q"}).status == 0, "a build of the project succeeds");
for (const Configuration* cfg : several) {
Check(fs::exists(ExpectedOutput(*cfg)), std::format("the build built {}", cfg->name));
}
{
const Ran run = RunWith({"crafter-build", "-q", "-r"});
Check(run.status == 1 && run.err.contains("hello, tool are executables"), std::format("-r runs one program and says which two it will not pick between (exit {}): {}", run.status, run.err));
}
{
// -r leaves this process in the program's directory.
const fs::path ran = fs::absolute(several[1]->ProgramDir()) / "hello.ran";
fs::remove(ran, ec);
const Ran run = RunWith({"crafter-build", "-q", "--no-tool", "-r"});
fs::current_path(scratch / "several");
Check(run.status == 0 && Read(ran) == "hello world", std::format("with one executable among the configurations, -r runs it (exit {}): {}", run.status, run.err));
}
{
// The library declares the tool's test as well: one tests/ToolTest
// and one directory to build it in, for two configurations. Built
// for both at once, that directory would end up holding either.
std::array<std::string_view, 1> twinTest = { "--twin-test" };
const std::vector<Configuration*> twins = LoadConfigurations("project.cpp", twinTest);
RunTestsOptions opts;
opts.targetFilter = HostTarget();
const TestSummary summary = RunTests(twins, opts);
std::int32_t refused = 0;
for (const TestResult& result : summary.results) {
if (result.name == "ToolTest" && result.outcome == TestOutcome::Fail && result.output.contains("greeter, tool each declare a test named ToolTest")) ++refused;
}
Check(refused == 2 && summary.failed == 2 && summary.passed == 1, std::format("a test two configurations declare fails for both, naming them, and the library's own test still passes ({} refused, {} failed, {} passed)", refused, summary.failed, summary.passed));
Check(twins.size() == 3 && twins[2]->tests.size() == 1 && !fs::exists(ExpectedOutput(twins[2]->tests[0].config)), "and it is built for neither");
}
{
RunTestsOptions opts;
opts.targetFilter = HostTarget();
const TestSummary summary = RunTests(several, opts);
std::vector<std::string> ran;
for (const TestResult& result : summary.results) ran.push_back(result.name);
std::ranges::sort(ran);
Check(ran == std::vector<std::string>{"GreeterTest", "ToolTest"} && summary.passed == 2, std::format("the tests of every configuration run in one run (ran {}, {} passed)", ran, summary.passed));
// Named twice in the list, a configuration's tests would each be
// built twice at once.
std::array<Configuration*, 2> same = { several[0], several[0] };
const TestSummary once = RunTests(same, opts);
Check(once.results.size() == 1 && once.passed == 1, std::format("a list that names one configuration twice runs its tests once ({} results, {} passed)", once.results.size(), once.passed));
}
{
// Two returned configurations of one directory are refused by a
// test run as by a build, though a test run never builds the
// configurations themselves: a test built on the one would be
// handed the other's build.
// Refused from the loaded project alone, so before anything is
// built or written: with a template beside the project file, a
// build or a test run that gets as far as building writes AGENTS.md.
const fs::path templateFile = scratch / "several" / "AGENTS.template.md";
std::ofstream(templateFile) << "# several\n";
const Ran test = RunWith({"crafter-build", "-q", "--twin", "test"});
Check(test.status == 1 && test.err.contains("two configurations named tool would both be built into"), std::format("a test run refuses two configurations that would be built into one directory (exit {}): {}", test.status, test.err));
const Ran build = RunWith({"crafter-build", "-q", "--twin"});
Check(build.status == 1 && build.err.contains("two configurations named tool would both be built into"), std::format("and so does a build (exit {}): {}", build.status, build.err));
Check(!fs::exists(scratch / "several" / "AGENTS.md"), "and neither has written AGENTS.md");
Check(RunWith({"crafter-build", "-q"}).status == 0 && fs::exists(scratch / "several" / "AGENTS.md"), "which a build that is not refused writes");
fs::remove(templateFile, ec);
fs::remove(scratch / "several" / "AGENTS.md", ec);
}
Check(RunWith({"crafter-build", "-q", "run", "stamp"}).status == 0 && Read(scratch / "several" / "stamp.ran") == "tool", "a script of the third configuration runs, handed the configuration that declares it");
Check(RunWith({"crafter-build", "-q", "run", "greet"}).status == 0 && Read(scratch / "several" / "greet.ran") == "greeter", "and so does one of the first");
// install and bundle export one app: the configuration packaged as a
// Flatpak. The fixture's packaged programs are wasm builds, which no
// build packages, so the one case that gets past the choice ends there
// or, where the host's flatpak is out of reach, before it.
{
const Ran none = RunWith({"crafter-build", "-q", "install"});
Check(none.status == 1 && none.err.contains("none of greeter, hello, tool is packaged as a Flatpak"), std::format("install refuses a project none of whose configurations is packaged (exit {}): {}", none.status, none.err));
const Ran two = RunWith({"crafter-build", "-q", "--packaged", "bundle"});
Check(two.status == 1 && two.err.contains("hello, tool are each packaged as a Flatpak"), std::format("bundle refuses to pick between two packaged configurations (exit {}): {}", two.status, two.err));
const Ran one = RunWith({"crafter-build", "-q", "--packaged", "--no-tool", "bundle"});
Check(one.status == 1 && !one.err.contains("none of") && !one.err.contains("are each packaged") && !one.err.contains("greeter is not packaged"), std::format("with one packaged configuration among them, bundle takes that one (exit {}): {}", one.status, one.err));
}
fs::current_path(home);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}

View file

@ -107,6 +107,38 @@ int main() {
Check(calls == 0, "an unknown name runs no script");
}
// The configurations a project file returns: their scripts are one list,
// each handed the configuration that declares it, and a name two of
// them declare is a name declared twice.
{
Configuration first;
Configuration second;
const Configuration* seenProject = nullptr;
first.scripts.push_back({"one", [&](ScriptContext& ctx) {
seenProject = &ctx.project;
return 1;
}});
second.scripts.push_back({"two", [&](ScriptContext& ctx) {
seenProject = &ctx.project;
return 2;
}});
std::array<Configuration*, 2> both = { &first, &second };
Check(RunScript(both, {.name = "two", .named = true}) == 2 && seenProject == &second, "a script of the second configuration runs, handed that configuration");
Check(RunScript(both, {.name = "one", .named = true}) == 1 && seenProject == &first, "and one of the first is handed the first");
// Named twice in the list, the first would declare "one" twice,
// which is refused with the status the script itself returns: only
// the script having run tells the two apart.
std::array<Configuration*, 2> same = { &first, &first };
seenProject = nullptr;
Check(RunScript(same, {.name = "one", .named = true}) == 1 && seenProject == &first, "a list that names one configuration twice declares its scripts once, and the script runs");
seenProject = nullptr;
second.scripts.push_back({"one", [&](ScriptContext& ctx) {
seenProject = &ctx.project;
return 0;
}});
Check(RunScript(both, {.name = "two", .named = true}) == 1 && seenProject == nullptr, "a name two configurations declare is refused, and nothing runs");
}
// The exit status passes through whole, and only an exit status does.
Check(RunReturning(0) == 0, "0 stays 0");
Check(RunReturning(1) == 1, "1 stays 1");

View file

@ -0,0 +1,20 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Returns what the project beside it keeps to itself, which the load
// refuses: the library keeper's program is built on, or with --test the
// configuration of the program's test, or with --test-dependency the helper
// only that test is built on. keeper's own file returns none of the three.
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args) {
// The test loads this file from digger/, so the path resolves against
// it. No args, so that keeper is loaded once whatever this file was
// given.
Configuration* program = LocalProject({ .projectFile = "../keeper/project.cpp", .args = {} });
if (std::ranges::contains(args, std::string_view("--test"))) return { &program->tests[0].config };
if (std::ranges::contains(args, std::string_view("--test-dependency"))) return { program->tests[0].config.dependencies[1] };
return { program->dependencies[0] };
}

View file

@ -0,0 +1,39 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// A project that returns one of the configurations it makes: the program.
// It keeps to itself the library the program is built on, the program's
// test, and a helper only that test is built on. None has a source: the
// test loads this file and builds nothing of it.
namespace {
// One new set per call, at addresses that never change.
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& kept = Owned.emplace_back();
kept.path = "./";
kept.name = "kept";
kept.outputName = "kept";
kept.type = ConfigurationType::LibraryStatic;
Configuration& helper = Owned.emplace_back();
helper.path = "./";
helper.name = "helper";
helper.outputName = "helper";
helper.type = ConfigurationType::LibraryStatic;
Configuration& program = Owned.emplace_back();
program.path = "./";
program.name = "program";
program.outputName = "program";
program.type = ConfigurationType::Executable;
program.dependencies = { &kept };
program.AddTest("KeptTest").Dependencies({ &program, &helper });
return { &program };
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Greeter;
import std;
export namespace Greeter {
std::string Greet(std::string_view name) {
return std::format("hello {}", name);
}
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Greeter;
int main() {
std::println("{}", Greeter::Greet("world"));
return 0;
}

View file

@ -0,0 +1,60 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// The project other projects load: a library, a program built on it, and a
// tool that shares nothing with either and declares a test and a script.
// Its `path` is relative, as a project file writes it, so a consumer only
// builds it into this directory once the loader has rebased it.
namespace {
// One new set per call, at addresses that never change: whoever loads
// this file keeps the pointers it returns.
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& greeter = Owned.emplace_back();
greeter.path = "./";
greeter.name = "greeter";
greeter.outputName = "greeter";
greeter.type = ConfigurationType::LibraryStatic;
{
std::array<fs::path, 1> interfaces = { "greeter/Greeter" };
std::array<fs::path, 0> implementations = {};
greeter.GetInterfacesAndImplementations(interfaces, implementations);
}
Configuration& hello = Owned.emplace_back();
hello.path = "./";
hello.name = "hello";
hello.outputName = "hello";
hello.type = ConfigurationType::Executable;
hello.dependencies = { &greeter };
{
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "hello/main" };
hello.GetInterfacesAndImplementations(interfaces, implementations);
}
Configuration& tool = Owned.emplace_back();
tool.path = "./";
tool.name = "tool";
tool.outputName = "tool";
tool.type = ConfigurationType::Executable;
{
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "tool/main" };
tool.GetInterfacesAndImplementations(interfaces, implementations);
}
tool.AddTest("ToolTest");
tool.scripts.push_back({"stamp", [](ScriptContext& ctx) {
std::ofstream(ctx.projectDir / "stamp.ran") << ctx.project.name;
return 0;
}});
return { &greeter, &hello, &tool };
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
// Leaves a file where it ran: the directory of the project whose test run
// started it.
int main() {
std::ofstream("ToolTest.ran") << "ran";
return 0;
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("tool");
return 0;
}

View file

@ -0,0 +1,24 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Two configurations under one name, which a consumer cannot pick between
// by it. Their output names tell the test which of them it was handed.
namespace {
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& first = Owned.emplace_back();
first.path = "./first/";
first.name = "twin";
first.outputName = "first";
Configuration& second = Owned.emplace_back();
second.path = "./second/";
second.name = "twin";
second.outputName = "second";
return { &first, &second };
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("umbrella");
return 0;
}

View file

@ -0,0 +1,34 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// A project that returns a configuration of another project beside one of
// its own: the tool of ../sibling, picked by name, and a program made here.
// The tool is built with the program, and its test and its script stay the
// other project's.
namespace {
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration* tool = LocalProject({ .projectFile = "../sibling/project.cpp", .configuration = "tool" });
Configuration& app = Owned.emplace_back();
app.path = "./";
app.name = "umbrella";
app.outputName = "umbrella";
app.type = ConfigurationType::Executable;
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "main" };
app.GetInterfacesAndImplementations(interfaces, implementations);
app.scripts.push_back({"own", [](ScriptContext& ctx) {
std::ofstream(ctx.projectDir / "own.ran") << ctx.project.name;
return 0;
}});
return { tool, &app };
}

View file

@ -0,0 +1,165 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// A project file that returns several configurations, loaded by another
// project: LocalProject and GitProject give the first one unless the spec
// names one, and a project file may return what they gave it beside
// configurations of its own.
//
// What this guards against is a consumer that gets the wrong thing: another
// configuration than it asked for; one whose sources resolve against the
// consumer's directory, because only the first was rebased; a second load
// of the sibling for every name asked, which leaves a consumer of two of
// its configurations with two builds of what they share; or everything the
// sibling describes built when one part of it was asked for. And against a
// sibling's configuration becoming part of the project that returns it: its
// script run as the project's, its test in the project's test run, whether
// the sibling's file returned it or kept it to itself.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
std::string Read(const fs::path& file) {
std::ifstream in(file, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
}
// What `call` throws, or "" when it returns.
std::string Thrown(const std::function<void()>& call) {
try {
call();
} catch (const std::exception& e) {
return e.what();
}
return "";
}
// Run() as the command line reaches it, and what it wrote to stderr.
struct Ran {
std::int32_t status = 0;
std::string err;
};
Ran RunWith(std::vector<std::string> args) {
std::vector<char*> argv;
for (std::string& arg : args) argv.push_back(arg.data());
std::ostringstream err;
std::streambuf* const saved = std::cerr.rdbuf(err.rdbuf());
const std::int32_t status = Run(static_cast<std::int32_t>(argv.size()), argv.data());
std::cerr.rdbuf(saved);
return { status, err.str() };
}
}
int main() {
const fs::path installedHome = GetCrafterBuildHome();
if (!fs::exists(installedHome / "Crafter.Build.cppm")) {
std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string());
return 77;
}
// Staged because every load drops a build/ tree next to its project
// file.
const fs::path home = fs::current_path();
const fs::path scratch = fs::temp_directory_path() / "crafter-build-sibling-configurations";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::copy(home / "tests" / "SiblingConfigurations" / "fixture", scratch, fs::copy_options::recursive);
// The first configuration unless the spec names one.
const fs::path sibling = scratch / "sibling" / "project.cpp";
Configuration* greeter = nullptr;
Configuration* hello = nullptr;
Configuration* tool = nullptr;
try {
greeter = LocalProject({ .projectFile = sibling });
hello = LocalProject({ .projectFile = sibling, .configuration = "hello" });
tool = LocalProject({ .projectFile = sibling, .configuration = "tool" });
} catch (const std::exception& e) {
std::println(std::cerr, "FAIL: loading the sibling threw: {}", e.what());
return 1;
}
Check(greeter->name == "greeter", std::format("a spec without a name gets the first configuration (got {})", greeter->name));
Check(hello->name == "hello" && tool->name == "tool", "a spec that names one gets that one");
Check(hello->dependencies == std::vector<Configuration*>{greeter}, "specs that differ only in the configuration they name share one load of the file");
for (const Configuration* cfg : {greeter, hello, tool}) {
Check(cfg->path.is_absolute() && fs::equivalent(cfg->path, scratch / "sibling", ec), std::format("{} is rebased onto its project's directory, like every configuration the file returns (path {})", cfg->name, cfg->path.string()));
}
{
const std::string thrown = Thrown([&] { (void)LocalProject({ .projectFile = sibling, .configuration = "nope" }); });
Check(thrown.contains("no configuration named 'nope'") && thrown.contains("greeter, hello, tool"), std::format("a name the project does not have is refused, listing the ones it has (got '{}')", thrown));
const fs::path twins = scratch / "twins" / "project.cpp";
const std::string ambiguous = Thrown([&] { (void)LocalProject({ .projectFile = twins, .configuration = "twin" }); });
Check(ambiguous.contains("two of its configurations are named 'twin'"), std::format("a name two configurations share picks neither (got '{}')", ambiguous));
Check(LocalProject({ .projectFile = twins })->outputName == "first", "without a name the first of the two is still the one a consumer gets");
}
// A consumer builds what it names and what that is built on.
{
std::array<Configuration*, 1> named = { hello };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(named, depResults, depMutex);
Check(failed.empty(), std::format("a sibling's second configuration builds: {}", failed));
Check(fs::exists(ExpectedOutput(*hello)) && fs::exists(ExpectedOutput(*greeter)), "with the library it is built on");
Check(!fs::exists(ExpectedOutput(*tool)), "and the configuration nobody named stays unbuilt");
}
// A configuration a sibling keeps to itself, one its project file does
// not return, is not another project's to return. It is not rebased,
// and returned it would count as the returning project's own, tests and
// scripts with it. digger returns what keeper's program leads to: the
// library it is built on, the configuration of its test, and a helper
// only that test is built on.
fs::current_path(scratch / "digger");
for (const auto& [arg, kept] : std::initializer_list<std::pair<std::string_view, std::string_view>>{
{"", "kept"},
{"--test", "KeptTest"},
{"--test-dependency", "helper"},
}) {
std::vector<std::string_view> args;
if (!arg.empty()) args.push_back(arg);
const std::string thrown = Thrown([&] { (void)LoadConfigurations("project.cpp", args); });
Check(thrown.contains(std::format("returned {}, which keeper keeps to itself", kept)), std::format("a file that returns {}, which its sibling keeps to itself, is refused, naming the two (got '{}')", kept, thrown));
}
// A file that returns another project's configuration beside its own:
// it is built, and everything else about it stays that project's. From
// the project's own directory, as crafter-build runs it.
fs::current_path(scratch / "umbrella");
Check(RunWith({"crafter-build", "-q"}).status == 0, "a project that returns a sibling's configuration builds");
{
std::array<std::string_view, 0> noArgs = {};
const std::vector<Configuration*> umbrella = LoadConfigurations("project.cpp", noArgs);
Check(umbrella.size() == 2 && umbrella[0] == tool, "the sibling's configuration is the one every spec for it gets");
Check(fs::exists(ExpectedOutput(*tool)), "the returned sibling's configuration is built");
Check(umbrella.size() == 2 && fs::exists(ExpectedOutput(*umbrella[1])), "and so is the project's own");
}
{
const Ran run = RunWith({"crafter-build", "-q", "run", "stamp"});
Check(run.status == 1 && run.err.contains("No script named 'stamp'"), std::format("the sibling's script is not the project's to run (exit {}): {}", run.status, run.err));
Check(!fs::exists(scratch / "sibling" / "stamp.ran") && !fs::exists(scratch / "umbrella" / "stamp.ran"), "and it did not run");
Check(RunWith({"crafter-build", "-q", "run", "own"}).status == 0 && Read(scratch / "umbrella" / "own.ran") == "umbrella", "the project's own script runs");
}
Check(RunWith({"crafter-build", "-q", "test"}).status == 0, "a test run of the project succeeds");
Check(!fs::exists(scratch / "umbrella" / "ToolTest.ran") && !fs::exists(scratch / "sibling" / "ToolTest.ran"), "and leaves the sibling's test alone");
fs::current_path(home);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}