Crafter.Build/tests/FlatpakTestsRaw/main.cpp
Jorijn van der Graaf 27031ed8e3
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-08 22:48:42 +01:00

78 lines
3.3 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;
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
}
// A test is never packaged, whatever its Configuration asks for: the runner
// starts the binary itself. Saying so in AddTest alone would miss a test
// from AddMarchVariants, or one a project.cpp puts into cfg.tests itself:
// it would follow its own packaging, Default only while that means the
// executable as linked, and Packaging::Flatpak would be packaged, or, by a
// native crafter-build, refused. So RunTests runs every test as linked,
// and so does RunLint work out its compile command: the test's own module
// PCMs are where its VariantId puts them, and the Flatpak variant, which
// nothing builds, has none, so linting it with a rule that reads the AST
// used to fail the run for want of them, after any number of test runs.
int main() {
// AddTest resolves tests/<name>/main.cpp against the working directory,
// and the test's outputs land below it, in the fixture's own bin/ and
// build/.
fs::current_path(fs::current_path() / "tests" / "FlatpakTestsRaw" / "fixture");
std::error_code ec;
fs::remove_all("bin", ec);
fs::remove_all("build", ec);
Configuration project;
project.path = "./";
project.name = "flatpak-tests-raw";
project.outputName = "flatpak-tests-raw";
project.target = HostTarget();
{
std::array<fs::path, 1> ifaces = { "tests/hello/Hello" };
project.AddTest("hello", ifaces);
}
project.tests.back().config.packaging = Packaging::Flatpak;
RunTestsOptions opts;
opts.jobs = 1;
const TestSummary summary = RunTests(project, opts);
Check(summary.results.size() == 1, std::format("one test ran, got {}", summary.results.size()));
if (!summary.results.empty()) {
const TestResult& result = summary.results.front();
Check(result.outcome == TestOutcome::Pass, std::format("the test passes (exit {}): {}", result.exitCode, result.output));
Check(result.output.contains("hello from a test"), "the output is the program's own");
}
const Configuration& test = project.tests.back().config;
Check(test.ResolvedPackaging() == Packaging::None, "the test is built as linked");
Check(!fs::exists(test.BinDir() / "files") && fs::is_regular_file(test.BinDir() / "hello"), "its bin directory holds the program, and no Flatpak files/");
// As `crafter-build lint` loads the project afresh: the test asks for a
// Flatpak again, and main.cpp, which imports Hello, parses against the
// PCMs the test run built.
project.tests.back().config.packaging = Packaging::Flatpak;
std::size_t parsed = 0;
project.AddAstLintRule("parses", [&parsed](LintContext&) { ++parsed; });
const LintSummary lint = RunLint(project, RunLintOptions{});
Check(lint.errors == 0 && parsed == 2, std::format("lint parses the test's two files against those PCMs ({} errors, {} parsed)", lint.errors, parsed));
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}