Crafter.Build/tests/StandardArgs/main.cpp

139 lines
6 KiB
C++
Raw Permalink Normal View History

2026-07-23 01:24:42 +02:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2026-07-22 18:25:39 +02:00
import std;
import Crafter.Build;
using namespace Crafter;
namespace {
2026-07-23 01:24:42 +02:00
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
2026-07-23 01:24:42 +02:00
++Failures;
}
}
}
// Pure-data tests for ApplyStandardArgs and ArgQuery — no build/run, just
// confirms the documented arg parsing produces the documented mutations.
int main() {
// --debug + --target= + --march= + --mtune=
{
Configuration cfg;
cfg.target = "ignored";
std::array<std::string_view, 4> raw = {
"--debug", "--target=aarch64-linux-gnu", "--march=armv8-a", "--mtune=cortex-a72",
};
ApplyStandardArgs(cfg, raw);
Check(cfg.debug, "--debug should set cfg.debug");
Check(cfg.target == "aarch64-linux-gnu", "--target= should overwrite cfg.target");
Check(cfg.march == "armv8-a", "--march= should overwrite cfg.march");
Check(cfg.mtune == "cortex-a72", "--mtune= should overwrite cfg.mtune");
}
2026-07-22 18:25:39 +02:00
// --sysroot= sets cfg.sysroot (cross-compiles against a foreign root,
// e.g. an Alpine aarch64 tree; also picked up by the std.cppm lookup
// and the qemu test-runner's QEMU_LD_PREFIX).
{
Configuration cfg;
std::array<std::string_view, 2> raw = {
"--target=aarch64-alpine-linux-musl", "--sysroot=/opt/alpine-aarch64",
};
ApplyStandardArgs(cfg, raw);
Check(cfg.sysroot == "/opt/alpine-aarch64", "--sysroot= should set cfg.sysroot");
Check(cfg.target == "aarch64-alpine-linux-musl", "--target= combines with --sysroot=");
}
// --lib promotes Executable -> LibraryStatic; --shared then promotes to Dynamic.
{
Configuration cfg;
cfg.type = ConfigurationType::Executable;
std::array<std::string_view, 1> raw = { "--lib" };
ApplyStandardArgs(cfg, raw);
Check(cfg.type == ConfigurationType::LibraryStatic, "--lib promotes Exe -> Static");
}
{
Configuration cfg;
cfg.type = ConfigurationType::Executable;
// Order shouldn't matter — promotions chain in priority order.
std::array<std::string_view, 2> raw = { "--shared", "--lib" };
ApplyStandardArgs(cfg, raw);
Check(cfg.type == ConfigurationType::LibraryDynamic, "--lib + --shared lands on Dynamic regardless of order");
}
{
Configuration cfg;
cfg.type = ConfigurationType::Executable;
std::array<std::string_view, 1> raw = { "--shared" };
ApplyStandardArgs(cfg, raw);
Check(cfg.type == ConfigurationType::Executable, "--shared alone on Executable is a no-op");
}
feat: lay an executable out as the files/ of a Flatpak A Linux executable built by the Flatpak crafter-build links the libc++, libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs inside crafter-build's sandbox and practically nowhere else: a host needs glibc 2.38 or newer and all three LLVM libraries, which not even Arch has. A program built here is meant to run in a Flatpak, so it can now be laid out as one: with cfg.packaging = Packaging::Flatpak, or --packaging=flatpak where the project.cpp crafter-build was pointed at calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in the app's sandbox, and -r runs the program there against the libraries the app carries. The program is linked into files/bin, where its shaders, files and assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path, and the new :Flatpak partition writes the rest of files/: files/lib with the LLVM runtime libraries, located one by one through clang, and whatever else the program needs, read with llvm-readelf, that the directories of the shared libraries it is built on hold, the ones an unpackaged link records as run paths, or the toolchain's own lib dir, /app/lib in crafter-build's Flatpak, which the link searches too; and the desktop entry, metainfo and icons under the app-id in files/share, the entry refused when its Icon= is not the app-id or desktop-file-validate, where there is one, rejects it, and an icon that is not an .svg or a square .png. The metadata and export/ of a build directory are flatpak build-finish's to write, and crafter-build keeps no copy of its rules: what a permission means and what the host sees of an app change with flatpak's releases, and a copy would have to follow every one. The writer copies a file only when its content differs, compared a block at a time so that a large file costs no more memory than a small one, and writes one only when what it would write differs, never deciding by an mtime, which ostree sets to 0 for every file of /app, and works the program's libraries out on every write, which reads only their dynamic sections, since a library replaced in place, as by a toolchain update, can need others, which FlatpakLibraryChange checks; it prunes from files/bin each top-level entry an earlier build put there and this one does not, reporting those the program wrote there itself. What cannot be a Flatpak is refused before anything is built, saying why: a Windows or wasm executable, a build against a sysroot or for musl, another architecture than the runtime's, a build against CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and a build by a native crafter-build, which links the host's glibc: a native process knows no Flatpak runtime, from /.flatpak-info, unless CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one. Default stays the executable as linked for now, RunTests runs every test as linked, whatever its Configuration asks for, and RunLint works out its compile command so, and a forwarded --packaging= leaves a dependency's executables alone, since consumers run them by path. A dependency that packages its own executable gets its desktop file, metainfo and icons rebased by the loader as its cfg.files are, so they are its own whatever directory the consumer builds from. Only a Flatpak is keyed into VariantId, so every other configuration keeps its directory, but for one given --packaging=, which was a project arg, hashed into VariantId, and is a standard arg now, a dependency's included. The app-id and what a Flatpak is installed as are not keyed: they change files/share, never what is compiled. The asset name map, kept three times, twice in Build() and once for the browser runtime's files.json, is one function now, CompressedAssetName; CompressAsset, in Crafter.Build-Asset.cpp, keeps the list of the extensions it loads, which has to agree with it. Each copy a Flatpak build makes into the program's directory, of its shaders, assets, cfg.files and what its dependencies forward, records the top-level name it places there, the names of what a directory holds for one copied into it itself ("data/", "data/."), and those are what the writer keeps in files/bin; a build that fails after copying adds them to the writer's record all the same, so the next write removes what no build places any more. SharedLibraryRuns now also packages its program as a Flatpak, whose files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
{
// --packaging= is standard: it sets cfg.packaging and, its effect
// showing in VariantId already, stays out of projectArgs. Outside any
// project file, as here, it counts as the top-level project's.
Configuration flatpak;
std::array<std::string_view, 1> raw = { "--packaging=flatpak" };
ApplyStandardArgs(flatpak, raw);
Check(flatpak.packaging == Packaging::Flatpak, "--packaging=flatpak asks for a Flatpak");
Check(flatpak.projectArgs.empty(), "--packaging= is not a project arg");
Configuration none;
std::array<std::string_view, 1> rawNone = { "--packaging=none" };
ApplyStandardArgs(none, rawNone);
Check(none.packaging == Packaging::None, "--packaging=none asks for the executable as linked");
Configuration bad;
std::array<std::string_view, 1> rawBad = { "--packaging=snap" };
bool threw = false;
try {
ApplyStandardArgs(bad, rawBad);
} catch (const std::invalid_argument& e) {
threw = std::string_view(e.what()).contains("expected none or flatpak");
}
Check(threw, "an unknown --packaging= is an error naming the kinds");
}
// ArgQuery is returned over the same args span and exposes Has/Get.
{
Configuration cfg;
std::array<std::string_view, 3> raw = { "--timing", "--prefix=/opt/x", "extra" };
ArgQuery q = ApplyStandardArgs(cfg, raw);
Check(q.Has("--timing"), "ArgQuery::Has true for present flag");
Check(!q.Has("--missing"), "ArgQuery::Has false for absent flag");
auto prefix = q.Get("--prefix=");
Check(prefix.has_value(), "ArgQuery::Get returns a value for known prefix");
Check(prefix.value_or("") == "/opt/x", "ArgQuery::Get returns the substring after =");
Check(!q.Get("--other=").has_value(), "ArgQuery::Get returns nullopt for absent prefix");
}
fix: key the host PCM cache on source content, add clean, hash project args Three follow-ons to the stale-build report, all cases of an identity not capturing something that changes the output. The host PCM cache under <cache>/crafter.build/<target>-<march>/ is shared by every crafter-build on the machine, and freshness was a per-file mtime comparison. That cannot tell "this PCM is newer than my source" from "this PCM was built from different sources that happen to be newer", so a package install and a working checkout — or two checkouts of different versions — silently compiled their project.cpp against each other's declarations. Invalidation now keys on a stamp over the bytes of every module source, which also covers the case one file's mtime never could: the cached PCMs import each other, so a change to :Interface invalidates :Clang's PCM with Crafter.Build-Clang.cppm untouched. Project args ApplyStandardArgs does not itself interpret are now folded into VariantId. Such a flag typically decides what gets compiled or bundled — the report's example is --no-webgpu dropping entries from cfg.files — and without it both settings shared one bin dir and interleaved their outputs there, leaving a bundle matching neither. Sorted and deduplicated so flag order doesn't split the cache, and inherited by test Configurations. `crafter-build clean` removes the project's bin/ and build/ trees. It deliberately does not load project.cpp: cleaning is most often reached when something is already wrong, and a clean that first needs the project to compile is useless exactly then.
2026-07-30 17:43:09 +00:00
// Flags ApplyStandardArgs doesn't itself interpret are collected into
// cfg.projectArgs, which feeds VariantId — a project flag that changes what
// gets compiled has to move the output directory with it.
{
Configuration cfg;
std::array<std::string_view, 5> raw = {
"--debug", "--no-webgpu", "--target=aarch64-linux-gnu", "--feature=fancy", "bare",
};
ApplyStandardArgs(cfg, raw);
std::vector<std::string> expected = { "--feature=fancy", "--no-webgpu", "bare" };
Check(cfg.projectArgs == expected, std::format("only unrecognised args are collected, sorted; got [{}]", std::format("{}", std::views::join_with(cfg.projectArgs, std::string(", ")) | std::ranges::to<std::string>())));
}
// Order and repetition must not perturb the set — otherwise the same build
// spelled two ways would land in two directories.
{
Configuration a;
Configuration b;
std::array<std::string_view, 2> forward = { "--alpha", "--beta" };
std::array<std::string_view, 3> reversed = { "--beta", "--alpha", "--beta" };
ApplyStandardArgs(a, forward);
ApplyStandardArgs(b, reversed);
Check(a.projectArgs == b.projectArgs, "projectArgs is order- and duplicate-insensitive");
}
2026-07-23 01:24:42 +02:00
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}