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

@ -0,0 +1,14 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
// A function of libedit, called only on a path that never runs, so that the
// link records the library.
extern "C" void* history_init();
int main(int argc, char**) {
if (argc > 100) history_init();
std::println("linked against libedit");
return 0;
}

View file

@ -0,0 +1,125 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
#include <stdlib.h>
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;
}
}
// The NEEDED entries of an ELF file, by the toolchain's llvm-readelf.
std::set<std::string> Needed(const fs::path& readelf, const fs::path& file) {
const auto r = RunCommandWithTimeout(std::format("{} --needed-libs {}", ShellQuote(readelf.string()), ShellQuote(file.string())), std::chrono::seconds(30));
std::set<std::string> names;
bool inList = false;
std::istringstream lines(r.output);
for (std::string line; std::getline(lines, line);) {
const std::size_t first = line.find_first_not_of(" \t");
const std::string t = first == std::string::npos ? std::string() : line.substr(first);
if (!inList) {
inList = t.starts_with("NeededLibraries [");
continue;
}
if (t == "]") break;
if (!t.empty()) names.insert(t);
}
return names;
}
}
// A program packaged as a Flatpak carries what its link takes from the
// toolchain's own lib dir, which the Flatpak link searches with -L. In
// crafter-build's Flatpak that is /app/lib, which holds libclang, libLLVM,
// liblldb and libedit beside the LLVM runtime libraries, none of them a
// runtime's. The writer used to look only in the directories of the shared
// libraries the program is built on and took any other name for the
// runtime's, so a program linked against one of those started under -r,
// where crafter-build's own /app/lib is on the loader's path, and failed
// once installed. The fixture links libedit. Skips where the toolchain is in
// /usr, whose lib dir is the system's, and where its lib dir has no libedit.
int main() {
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-toolchain-library";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::create_directories(scratch);
const fs::path info = scratch / "flatpak-info";
std::ofstream(info) << "[Application]\nname=net.catcrafts.CrafterBuild\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n";
setenv("CRAFTER_BUILD_FLATPAK_INFO", info.string().c_str(), 1);
const Toolchain& tc = GetToolchain();
if (NormalizeTarget(tc.hostTriple) != "x86_64-linux-gnu") {
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", tc.hostTriple);
return 77;
}
if (!tc.libcxxDir.empty()) {
std::println(std::cerr, "SKIP: CRAFTER_BUILD_LIBCXX_DIR is set, and a build against it is no Flatpak");
return 77;
}
const fs::path toolchainLib = tc.LibcxxLibDir();
if (toolchainLib.empty()) {
std::println(std::cerr, "SKIP: the toolchain is in /usr, whose libraries are the system's");
return 77;
}
if (!fs::exists(toolchainLib / "libedit.so")) {
std::println(std::cerr, "SKIP: the toolchain's lib dir, {}, has no libedit.so", toolchainLib.string());
return 77;
}
const fs::path fixture = fs::current_path() / "tests" / "FlatpakToolchainLibrary" / "fixture";
fs::remove_all(fixture / "bin", ec);
fs::remove_all(fixture / "build", ec);
Configuration app;
app.path = fixture;
app.name = "toolchain-library-app";
app.outputName = "edit";
app.target = HostTarget();
app.packaging = Packaging::Flatpak;
app.flatpak.appId = "net.example.ToolchainLibrary";
app.linkFlags = { "-ledit" };
{
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 1> impls = { "main" };
app.GetInterfacesAndImplementations(ifaces, impls);
}
{
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
if (const BuildResult r = Build(app, depResults, depMutex); !r.result.empty()) {
std::println(std::cerr, "FAIL: build failed: {}", r.result);
return 1;
}
}
const fs::path lib = app.BinDir() / "files" / "lib";
const std::set<std::string> needed = Needed(tc.llvmReadelf, ExpectedOutput(app));
const auto edit = std::ranges::find_if(needed, [](const std::string& name) { return name.starts_with("libedit.so"); });
Check(edit != needed.end(), std::format("the program needs libedit (NEEDED: {})", needed));
if (edit != needed.end()) {
Check(fs::exists(toolchainLib / *edit), std::format("{} is the toolchain's, in {}", *edit, toolchainLib.string()));
Check(fs::is_regular_file(lib / *edit), std::format("{} is carried in files/lib", *edit));
}
// Whatever the program and the libraries it carries need from the
// toolchain's lib dir is carried too.
std::set<std::string> all = needed;
for (const fs::directory_entry& entry : fs::directory_iterator(lib)) all.merge(Needed(tc.llvmReadelf, entry.path()));
for (const std::string& name : all) {
if (fs::exists(toolchainLib / name)) Check(fs::exists(lib / name), std::format("{}, from the toolchain's lib dir, is carried in files/lib", name));
}
fs::remove_all(scratch, ec);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}