Crafter.Build/tests/BuildAll/main.cpp

273 lines
14 KiB
C++
Raw Permalink Normal View History

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.
2026-11-15 02:16:07 +01:00
// 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;
}