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.
108 lines
5 KiB
C++
108 lines
5 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
export module Crafter.Build:Test;
|
|
import std;
|
|
import :Clang;
|
|
|
|
export namespace Crafter {
|
|
// One row in a SIMD-march fan-out: a single Test variant compiles the
|
|
// shared source with `-march=<march> -mtune=<mtune>`. Passed by span to
|
|
// Configuration::AddMarchVariants — see Crafter.Math/project.cpp.
|
|
struct MarchTier {
|
|
std::string march;
|
|
std::string mtune;
|
|
};
|
|
|
|
// Fluent helper returned by Configuration::AddTest. Holds a back-pointer
|
|
// to the parent Configuration and the index of the just-pushed Test, so
|
|
// mutations survive any vector reallocation that subsequent AddTest
|
|
// calls might trigger. Each setter returns `*this` to support chaining.
|
|
//
|
|
// Note: source files (interfaces + implementations) are fixed at
|
|
// AddTest-time — pass them via the AddTest overload that takes
|
|
// interfaces. The builder only mutates compile state, deps, runtime
|
|
// args, and requires; it deliberately does NOT expose Sources() to
|
|
// avoid the double-`GetInterfacesAndImplementations` footgun (calling
|
|
// it twice would re-parse the same .cppm files into duplicate Module
|
|
// entries).
|
|
struct TestBuilder {
|
|
Configuration* parent;
|
|
std::size_t index;
|
|
|
|
Test& Ref() const { return parent->tests[index]; }
|
|
|
|
// Override the path the test's sources resolve against. Defaults to
|
|
// "./" (project root, where tests/<name>/main.cpp lives). Override
|
|
// only when the test source layout sits under a subdir.
|
|
TestBuilder& Path(std::filesystem::path p);
|
|
TestBuilder& Target(std::string t);
|
|
TestBuilder& March(std::string m);
|
|
TestBuilder& Mtune(std::string m);
|
|
TestBuilder& Sysroot(std::filesystem::path s);
|
|
TestBuilder& Debug(bool d = true);
|
|
TestBuilder& Define(std::string name, std::string value = "");
|
|
TestBuilder& Timeout(std::chrono::seconds s);
|
|
// Replaces any previously-set args.
|
|
TestBuilder& Args(std::vector<std::string> a);
|
|
// Appends one require entry ("tool:foo" / "file:..." / "env:VAR").
|
|
TestBuilder& Requires(std::string r);
|
|
// Replaces the dependency list. Pointers must outlive the build.
|
|
TestBuilder& Dependencies(std::vector<Configuration*> deps);
|
|
// Append a single linker flag (-l..., -L..., etc.).
|
|
TestBuilder& LinkFlag(std::string f);
|
|
// Append a single compile flag.
|
|
TestBuilder& CompileFlag(std::string f);
|
|
};
|
|
|
|
struct RunTestsOptions {
|
|
std::vector<std::string> globs;
|
|
std::int32_t jobs = 0;
|
|
std::optional<std::chrono::seconds> timeoutOverride;
|
|
bool listOnly = false;
|
|
// Single-target run: only tests whose Configuration::target matches
|
|
// are included. Empty (default) = run every distinct target declared
|
|
// across discovered tests, plus the host target. Set from --target=...
|
|
// (when omitted, the harness sweeps all declared targets so cross-arch
|
|
// tests run by default without the user having to know which targets
|
|
// exist).
|
|
std::string targetFilter;
|
|
// CLI override for --runner=<spec>: applies to every test in the run.
|
|
// Target scoping is unnecessary because targetFilter ensures the run
|
|
// contains only one target's tests.
|
|
std::optional<std::string> runnerOverride;
|
|
};
|
|
|
|
struct TestSummary {
|
|
std::int32_t passed = 0;
|
|
std::int32_t failed = 0;
|
|
std::int32_t crashed = 0;
|
|
std::int32_t timedOut = 0;
|
|
std::int32_t skipped = 0;
|
|
std::vector<TestResult> results;
|
|
bool AllPassed() const { return failed == 0 && crashed == 0 && timedOut == 0; }
|
|
};
|
|
|
|
TestSummary RunTests(Configuration& projectCfg, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs = {});
|
|
TestResult RunSingleTest(const Test& test, const std::filesystem::path& binary, std::chrono::seconds timeout);
|
|
|
|
// Parent-project access for test fixtures. Run() sets the pointer to the
|
|
// root project's Configuration before discovery; fixtures call ParentLib()
|
|
// to obtain a Configuration* for one of the parent's libraries (or the
|
|
// parent itself) so they can declare it as a dependency without rebuilding
|
|
// the lib config from scratch. Lookup is by Configuration::name and
|
|
// recurses through the parent's dependency graph.
|
|
void SetParentProject(Configuration* parent);
|
|
Configuration* ParentLib(std::string_view name);
|
|
}
|
|
|
|
namespace Crafter {
|
|
// A test is never packaged, whatever its Configuration asks for: the
|
|
// runner starts the binary itself. Sets every test of `cfg` so, for one
|
|
// from AddTest or AddMarchVariants and one a project.cpp put into
|
|
// cfg.tests itself alike. Every reader of cfg.tests that builds a test
|
|
// or works out its compile command, RunTests and RunLint, calls it
|
|
// first, so that they agree on the test's VariantId, and so on where its
|
|
// module PCMs are.
|
|
void UnpackageTests(Configuration& cfg);
|
|
}
|