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:
parent
7283610128
commit
27031ed8e3
56 changed files with 2314 additions and 74 deletions
162
tests/FlatpakIncremental/main.cpp
Normal file
162
tests/FlatpakIncremental/main.cpp
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ReadFile(const fs::path& path) {
|
||||
std::ifstream in(path, std::ios::binary);
|
||||
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
std::string BuildIt(Configuration& cfg) {
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
return Build(cfg, depResults, depMutex).result;
|
||||
}
|
||||
}
|
||||
|
||||
// What a Flatpak's files/ holds is what the build puts there, and it is
|
||||
// kept so without rewriting what did not change. A cfg.files entry
|
||||
// dropped since goes from files/bin, which ships; a file the program wrote
|
||||
// there itself stays, since the build cannot know what it is (the build
|
||||
// reports it instead). A renamed app-id leaves no desktop entry or icon
|
||||
// under the old name behind in files/share. What a directory
|
||||
// written with a trailing separator or as "dir/." held, which the build
|
||||
// copies flat, goes with the entry, and so does a program renamed in place,
|
||||
// under its old name: neither may stay, taken for files the program wrote,
|
||||
// nor may what a build that then failed copied there. And a library is
|
||||
// copied again when its copy no longer matches the source, whatever the
|
||||
// mtimes say: every file of an ostree deployment, the Flatpak's /app among
|
||||
// them, has mtime 0, so a newer libc++ never looks newer than its copy.
|
||||
int main() {
|
||||
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-incremental";
|
||||
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);
|
||||
if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") {
|
||||
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple);
|
||||
return 77;
|
||||
}
|
||||
if (!GetToolchain().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 fixture = fs::current_path() / "tests" / "FlatpakIncremental" / "fixture";
|
||||
fs::remove_all(fixture / "bin", ec);
|
||||
fs::remove_all(fixture / "build", ec);
|
||||
Configuration app;
|
||||
app.path = fixture;
|
||||
app.name = "incremental-app";
|
||||
app.outputName = "incremental";
|
||||
app.target = HostTarget();
|
||||
app.packaging = Packaging::Flatpak;
|
||||
app.files = { fixture / "kept.txt", fixture / "dropped.txt" };
|
||||
app.flatpak.appId = "net.example.Incremental";
|
||||
app.flatpak.desktopFile = fixture / "app.desktop";
|
||||
app.flatpak.icons = { fixture / "icon.svg" };
|
||||
{
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
}
|
||||
if (const std::string failed = BuildIt(app); !failed.empty()) {
|
||||
std::println(std::cerr, "FAIL: build failed: {}", failed);
|
||||
return 1;
|
||||
}
|
||||
const fs::path root = app.BinDir();
|
||||
const fs::path bin = root / "files" / "bin";
|
||||
Check(fs::exists(bin / "kept.txt") && fs::exists(bin / "dropped.txt"), "both files are placed");
|
||||
|
||||
app.files = { fixture / "kept.txt" };
|
||||
Check(BuildIt(app).empty(), "the build without dropped.txt succeeds");
|
||||
Check(!fs::exists(bin / "dropped.txt"), "a dropped cfg.files entry goes from files/bin");
|
||||
Check(fs::exists(bin / "kept.txt") && fs::exists(bin / "incremental"), "what is still placed stays");
|
||||
|
||||
std::ofstream(bin / "written.bin") << "written by the program";
|
||||
Check(BuildIt(app).empty(), "the build after the program wrote a file succeeds");
|
||||
Check(fs::exists(bin / "written.bin"), "a file the program wrote stays");
|
||||
|
||||
app.flatpak.appId = "net.example.Renamed";
|
||||
Check(BuildIt(app).empty(), "the build under the new app-id succeeds");
|
||||
const fs::path share = root / "files" / "share";
|
||||
Check(fs::exists(share / "applications" / "net.example.Renamed.desktop") && !fs::exists(share / "applications" / "net.example.Incremental.desktop"), "the desktop entry is renamed");
|
||||
Check(fs::exists(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Renamed.svg") && !fs::exists(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Incremental.svg"), "the icon is renamed");
|
||||
|
||||
// A directory written with a trailing separator, "flat/", or as
|
||||
// "flat/.", has no filename() of its own, and the build copies what it
|
||||
// holds straight into files/bin, an asset directory's files as they
|
||||
// are: those are the names it placed, and once the entries are dropped
|
||||
// they go, rather than stay as files the program wrote.
|
||||
const fs::path flat = scratch / "flat";
|
||||
fs::create_directories(flat / "inner");
|
||||
std::ofstream(flat / "flat.txt") << "flat\n";
|
||||
std::ofstream(flat / "inner" / "nested.txt") << "nested\n";
|
||||
const fs::path flatAssets = scratch / "flat-assets";
|
||||
fs::create_directories(flatAssets);
|
||||
std::ofstream(flatAssets / "mod.json") << "{}\n";
|
||||
for (const fs::path& spelling : { fs::path(""), fs::path(".") }) {
|
||||
app.files = { fixture / "kept.txt", flat / spelling };
|
||||
app.assets = { flatAssets / spelling };
|
||||
const std::string written = (flat / spelling).string();
|
||||
Check(BuildIt(app).empty(), std::format("the build with directories written as {} succeeds", written));
|
||||
Check(fs::exists(bin / "flat.txt") && fs::exists(bin / "inner" / "nested.txt") && fs::exists(bin / "mod.json"), std::format("what they hold is placed straight in files/bin, written as {}", written));
|
||||
app.files = { fixture / "kept.txt" };
|
||||
app.assets = {};
|
||||
Check(BuildIt(app).empty(), "the build without them succeeds");
|
||||
Check(!fs::exists(bin / "flat.txt") && !fs::exists(bin / "inner") && !fs::exists(bin / "mod.json"), std::format("and what they placed goes, written as {}", written));
|
||||
}
|
||||
|
||||
// A program renamed in place: outputName is not in VariantId(), so the
|
||||
// bin dir stays, and the old program goes as one the last build
|
||||
// placed, rather than stay as a file the program wrote.
|
||||
app.outputName = "renamed";
|
||||
Check(BuildIt(app).empty(), "the build of the renamed program succeeds");
|
||||
Check(fs::exists(bin / "renamed") && !fs::exists(bin / "incremental"), "files/bin holds the program under its new name, and not under the old");
|
||||
|
||||
// A build that fails after copying into files/bin, here at its link,
|
||||
// placed what it copied all the same: once a build places it no more, it
|
||||
// goes, rather than stay as a file the program wrote.
|
||||
std::ofstream(scratch / "failed.txt") << "failed\n";
|
||||
app.files = { fixture / "kept.txt", scratch / "failed.txt" };
|
||||
app.linkFlags = { "-lcrafter-build-missing" };
|
||||
Check(!BuildIt(app).empty(), "a build whose link fails fails");
|
||||
Check(fs::exists(bin / "failed.txt"), "after copying its files into files/bin");
|
||||
app.files = { fixture / "kept.txt" };
|
||||
app.linkFlags = {};
|
||||
Check(BuildIt(app).empty(), "the build after it succeeds");
|
||||
Check(!fs::exists(bin / "failed.txt"), "and what the failed build placed goes");
|
||||
|
||||
// A copy that no longer matches its source, under the mtime it had.
|
||||
const fs::path copy = root / "files" / "lib" / "libc++.so.1";
|
||||
const std::string good = ReadFile(copy);
|
||||
const fs::file_time_type stamp = fs::last_write_time(copy);
|
||||
std::ofstream(copy, std::ios::binary | std::ios::trunc) << std::string(good.size(), 'x');
|
||||
fs::last_write_time(copy, stamp);
|
||||
Check(BuildIt(app).empty(), "the build after the copy went stale succeeds");
|
||||
Check(ReadFile(copy) == good, "a library whose copy differs from its source is copied again, mtimes unchanged or not");
|
||||
|
||||
fs::remove_all(scratch, ec);
|
||||
if (Failures > 0) {
|
||||
std::println(std::cerr, "{} assertions failed", Failures);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue