273 lines
14 KiB
C++
273 lines
14 KiB
C++
|
|
// 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;
|
||
|
|
}
|