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.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 19:20:55 +01:00
commit 27031ed8e3
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
56 changed files with 2314 additions and 74 deletions

View file

@ -9,6 +9,7 @@ import :Implementation;
import :External;
import :Toolchain;
import :Agents;
import :Flatpak;
namespace fs = std::filesystem;
namespace Crafter {
@ -57,7 +58,9 @@ export namespace Crafter {
// loaded from at run time, its dependencies' included, and, once a
// shared library is linked, its own bin dir, for its consumers. An
// executable or shared library linked from them records each one as
// a run path.
// a run path, except an executable packaged as a Flatpak: its one run
// path is $ORIGIN/../lib, and WriteFlatpak copies what it needs from
// these directories into files/lib.
std::set<std::string> runPaths;
};
@ -89,6 +92,25 @@ export namespace Crafter {
LibraryDynamic,
};
// What an executable is delivered as. Read for executables only; a
// library is never packaged.
enum class Packaging {
// The executable as linked, for now; Flatpak is asked for by name.
Default,
// The files/ of a Flatpak in BinDir(), what becomes /app in the
// app's sandbox: the program and what it reads at run time in
// files/bin, the shared libraries it needs in files/lib, and its
// desktop entry, metainfo and icons in files/share. An x86_64 Linux
// (glibc) executable without a sysroot, built by the Flatpak
// crafter-build, since it links that runtime's glibc.
Flatpak,
// The executable as linked, with its files beside it in BinDir().
// Built by the Flatpak crafter-build, it needs the libc++,
// libc++abi and libunwind in /app/lib, so it runs inside that
// sandbox (-r, test, run scripts) and practically nowhere else.
None,
};
struct Test;
struct TestRunner {
@ -471,6 +493,11 @@ export namespace Crafter {
std::string sysroot;
bool debug = false;
ConfigurationType type = ConfigurationType::Executable;
// What the executable is delivered as (see Packaging).
// ApplyStandardArgs sets it from --packaging=none|flatpak.
Packaging packaging = Packaging::Default;
// What it is installed as when it is packaged as a Flatpak.
FlatpakApp flatpak;
std::vector<std::unique_ptr<Module>> interfaces;
std::vector<Implementation> implementations;
std::vector<fs::path> cFiles;
@ -661,6 +688,10 @@ export namespace Crafter {
}
compileKey += "|T:";
compileKey += toolchain.Key();
// A Flatpak lays its bin dir out unlike the executable as linked,
// so the two never share one. Only keyed for a Flatpak, so every
// other configuration keeps the directory it had.
if (ResolvedPackaging() == Packaging::Flatpak) compileKey += "|P:flatpak";
std::size_t configHash = std::hash<std::string>{}(compileKey);
return std::format("{}-{}-{}-{}-{:08x}", name, target, march, mtune, configHash);
}
@ -669,9 +700,16 @@ export namespace Crafter {
// Where an executable and the files it reads at run time land, and
// the directory `-r` runs a host executable in and serves a browser
// build from: its shaders, cfg.files and assets, and those its
// dependencies forward to it. For now that is the bin
// dir of every configuration.
fs::path ProgramDir() const { return BinDir(); }
// dependencies forward to it. A Flatpak keeps them in files/bin of
// its bin dir, which becomes /app/bin; every other configuration in
// its bin dir.
fs::path ProgramDir() const { return ResolvedPackaging() == Packaging::Flatpak ? BinDir() / "files" / "bin" : BinDir(); }
// What `packaging` comes to for this configuration: Flatpak for an
// executable that asks for one by name, None for every other
// configuration and for Default.
Packaging ResolvedPackaging() const {
return type == ConfigurationType::Executable && packaging == Packaging::Flatpak ? Packaging::Flatpak : Packaging::None;
}
fs::path PcmDir() const {
return type == ConfigurationType::Executable ? BuildDir() : BinDir();
}
@ -806,6 +844,11 @@ export namespace Crafter {
// --mtune=<value> cfg.mtune = <value>
// --lib cfg.type promoted Executable → LibraryStatic
// --shared cfg.type promoted LibraryStatic → LibraryDynamic
// --packaging=<kind> cfg.packaging = None or Flatpak (none, flatpak),
// in the project crafter-build was pointed at only:
// a dependency loaded with the same args keeps its
// executables as they are, since its consumers run
// them by path
// Promotions chain in priority order so `--lib --shared` lands on
// LibraryDynamic regardless of arg order; each is a no-op when the
// baseline doesn't match (e.g. --shared on an Executable, or --lib on a

View file

@ -0,0 +1,98 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Crafter.Build:Flatpak;
import std;
namespace fs = std::filesystem;
// An executable packaged as a Flatpak: what a project declares about it, and
// the files/ Build() lays out for it, which becomes /app in the app's
// sandbox. Everything here works on plain data; Build() gathers it from the
// Configuration, so this partition knows nothing of one.
export namespace Crafter {
// What an executable packaged as a Flatpak is installed as. Read for an
// executable whose Configuration::packaging makes it one.
struct FlatpakApp {
// The application ID, reverse-DNS. Empty means
// DefaultFlatpakAppId(outputName).
std::string appId;
// A desktop entry, installed as <appId>.desktop. Its Icon= has to be
// the app-id. Empty: none, and the app gets no menu entry.
fs::path desktopFile;
// AppStream metadata, installed as <appId>.metainfo.xml.
fs::path metainfo;
// The app's icons, each installed under the app-id: an .svg as
// scalable, a .png at its own size, which has to be square.
std::vector<fs::path> icons;
};
// local.crafterbuild.<outputName>: no published application uses the
// .local domain, which is reserved for mDNS, so an app installed under it
// cannot replace one. A character an application ID cannot hold becomes
// `_`, and a leading digit gets a `_` before it.
std::string DefaultFlatpakAppId(std::string_view outputName);
// Why `appId` is not a name Flatpak takes for an application; empty when
// it is one. build-export accepts an invalid one, and only build-bundle
// and an install refuse it, so the build checks.
std::string FlatpakAppIdError(std::string_view appId);
// Everything WriteFlatpakFiles needs, gathered by Build().
struct FlatpakFiles {
// BinDir(): files/ is written here.
fs::path root;
// A file in BuildDir() remembering what the previous write placed.
fs::path record;
std::string appId;
// The program's file name in files/bin: the app's command.
std::string command;
fs::path desktopFile;
fs::path metainfo;
std::vector<fs::path> icons;
// Where the toolchain's clang++ and llvm-readelf are: clang names the
// LLVM runtime libraries the program links, readelf what it needs.
fs::path clangxx;
fs::path readelf;
// Where the program's other shared libraries are found, in the order
// its link searches them: the directories of the shared libraries it
// is built on, which an unpackaged link records as run paths, then
// the toolchain's own lib dir (Toolchain::LibcxxLibDir), /app/lib in
// crafter-build's Flatpak.
std::vector<fs::path> libraryDirs;
// The top-level names the build itself places in files/bin: the
// program, its shaders, cfg.files and assets, and what its
// dependencies forward. Anything else there is not the build's.
std::set<std::string> programEntries;
};
// Lays out what becomes /app around the program Build() linked into
// files/bin: files/share with the desktop file, metainfo and icons under
// the app-id, and files/lib with the shared libraries the program needs
// that the runtime does not have. Those are the LLVM runtime libraries
// the toolchain links into every program, and whatever else of its
// NEEDED entries, and theirs, `libraryDirs` holds; a name found in
// neither is the runtime's. That is the files/ of a Flatpak build
// directory; its metadata and export/ are flatpak build-finish's to
// write, which decides what the sandbox's permissions mean and what the
// host sees of the app.
//
// The writer owns files/lib and files/share outright, and changes a file
// only when what it would write differs, so a build that changed nothing
// leaves every mtime alone. In files/bin it works on top-level entries,
// those `programEntries` names: it removes each that an earlier write
// recorded as placed by the build and this one does not, and reports
// those neither placed, files the program wrote itself under `-r`, which
// would ship with the app. Inside a directory the build copies there, of
// cfg.files or assets, it removes nothing, as the bin dir of an
// unpackaged build does not. Returns why it could not finish; empty when
// it did.
std::string WriteFlatpakFiles(const FlatpakFiles& files);
}
namespace Crafter {
// Adds `names` to the top-level entries of files/bin that the writer's
// record at `record` counts as the build's, for a build that copied
// into files/bin and failed before the writer ran: the next write then
// removes those it does not place, rather than take them for files the
// program wrote. Writes only what changes, and never throws, as Build()
// calls it on every way out.
void RecordFlatpakPlaced(const fs::path& record, const std::set<std::string>& names);
}

View file

@ -95,3 +95,14 @@ export namespace Crafter {
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);
}

View file

@ -18,6 +18,8 @@ export namespace Crafter {
fs::path clangxx;
fs::path clang;
fs::path llvmAr;
// What a packaged program's shared libraries are read with.
fs::path llvmReadelf;
// Where clang-c/Index.h lives when it is not on the default include
// path; empty otherwise.
fs::path llvmIncludeDir;
@ -53,6 +55,15 @@ export namespace Crafter {
// its mingw-w64 parts; any other prefix is the sysroot itself, and
// Windows libraries installed into its include/ and lib/ are found.
bool mingwPrefixIsSystem = false;
// The Flatpak runtime crafter-build runs on, from the
// runtime=runtime/<id>/<arch>/<branch> line of /.flatpak-info:
// "x86_64" and "26.08" in the Flatpak, both empty outside one. A
// program built here links that runtime's glibc, so an executable is
// only packaged as a Flatpak when they are set, and for that branch.
// CRAFTER_BUILD_FLATPAK_INFO names another file to read them from,
// for tests that package outside the Flatpak.
std::string flatpakArch;
std::string flatpakBranch;
// `clang++ -print-target-triple` and the first line of
// `clang++ --version`; both empty when clang++ could not be run.
std::string hostTriple;
@ -62,6 +73,12 @@ export namespace Crafter {
// Flags for compiling against libcxxDir; empty without one.
std::string LibcxxCompileFlags() const;
// Where libc++.so.1 is when clang would not find it itself: an
// explicit libcxxDir's lib, or <prefix>/lib of a toolchain outside
// /usr, /app/lib in crafter-build's Flatpak, which holds the rest of
// its LLVM as well. Empty for a toolchain in /usr, whose libraries
// are on the linker's and the loader's default paths.
fs::path LibcxxLibDir() const;
// Flags for linking against the libc++ the compiler will use: a -L
// and an rpath for <prefix>/lib when the prefix is not the system one
// (clang searches only its per-target runtime directory and the
@ -69,6 +86,9 @@ export namespace Crafter {
// the linker's nor the loader's default path), or for an explicit
// libcxxDir.
std::string LibcxxLinkFlags() const;
// LibcxxLinkFlags without the rpath: for a program that finds its
// copy of libc++ somewhere else at run time, as a Flatpak does.
std::string LibcxxSearchFlags() const;
// Names this toolchain for cache directory names: the clang version
// followed by a hash of what decides which headers and libraries a
// PCM is built against. Two toolchains with different keys never
@ -85,6 +105,9 @@ export namespace Crafter {
// Where the host's root shows inside a sandbox (/run/host in a
// Flatpak); empty outside one.
fs::path hostRoot;
// What /.flatpak-info holds, or the file CRAFTER_BUILD_FLATPAK_INFO
// names; empty outside a Flatpak.
std::string flatpakInfo;
};
// Pure path resolution: an env override wins, then a toolchain installed
// beside crafter-build (<crafterPrefix>/bin/clang++), then bare names on

View file

@ -3,6 +3,7 @@
export module Crafter.Build;
export import :Toolchain;
export import :Flatpak;
export import :Clang;
export import :Platform;
export import :Implementation;