Crafter.Build/tests/FlatpakDesktop/main.cpp

157 lines
8 KiB
C++
Raw Normal View History

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-01 19:20:55 +01:00
// 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;
}
// A PNG's signature and IHDR, which is all the size is read from.
void WritePng(const fs::path& path, std::uint32_t width, std::uint32_t height) {
std::string bytes = "\x89PNG\r\n\x1a\n";
const auto bigEndian = [&](std::uint32_t value) {
for (std::int32_t shift = 24; shift >= 0; shift -= 8) bytes.push_back(static_cast<char>((value >> shift) & 0xff));
};
bigEndian(13);
bytes += "IHDR";
bigEndian(width);
bigEndian(height);
bytes += std::string("\x08\x06\x00\x00\x00", 5);
std::ofstream(path, std::ios::binary) << bytes;
}
}
// A desktop entry, AppStream metainfo and icons are installed under the
// app-id in files/share, where flatpak build-finish finds them to export
// to the host: the entry as share/applications/<app-id>.desktop, the
// metainfo as share/metainfo/<app-id>.metainfo.xml, an .svg as the
// scalable icon and a .png in the directory of its own size. What would
// make the app fail to install, or show no icon, is refused at build time:
// an Icon= other than the app-id, spelled with spaces around '=' or not, a
// png that is not square, an icon that is neither format, an entry
// desktop-file-validate rejects (where it exists), on the write after a
// failed one as well, an app-id flatpak does not accept. A project that
// names no app-id gets local.crafterbuild.<name>.
int main() {
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-desktop";
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);
Check(DefaultFlatpakAppId("hello") == "local.crafterbuild.hello", "the default app-id");
Check(DefaultFlatpakAppId("my-app") == "local.crafterbuild.my-app", "a dash may stay in the last segment");
Check(DefaultFlatpakAppId("3d.viewer") == "local.crafterbuild._3d_viewer", std::format("a dot and a leading digit are made safe: {}", DefaultFlatpakAppId("3d.viewer")));
Check(FlatpakAppIdError(DefaultFlatpakAppId("3d.viewer")).empty(), "and the result is valid");
Check(FlatpakAppIdError("org.example.My-App").empty(), "a valid app-id");
for (const std::string_view bad : {"org.example", "org.3d.App", "org.my-company.App", "org..App", "org.example.App!", ""}) {
Check(!FlatpakAppIdError(bad).empty(), std::format("'{}' is refused", bad));
}
Check(!FlatpakAppIdError(std::format("org.example.{}", std::string(250, 'a'))).empty(), "an app-id over 255 characters is refused");
if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") {
// A skip must never cover for a failure above.
if (Failures > 0) return 1;
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple);
return 77;
}
if (!GetToolchain().libcxxDir.empty()) {
// A skip must never cover for a failure above.
if (Failures > 0) return 1;
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" / "FlatpakDesktop" / "fixture";
fs::remove_all(fixture / "bin", ec);
fs::remove_all(fixture / "build", ec);
WritePng(scratch / "icon-512.png", 512, 512);
WritePng(scratch / "wide.png", 512, 256);
Configuration app;
app.path = fixture;
app.name = "desktop-app";
app.outputName = "desktop";
app.target = HostTarget();
app.packaging = Packaging::Flatpak;
app.flatpak.appId = "net.example.Desktop";
app.flatpak.desktopFile = fixture / "app.desktop";
app.flatpak.metainfo = fixture / "app.metainfo.xml";
app.flatpak.icons = { fixture / "icon.svg", scratch / "icon-512.png" };
{
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 share = root / "files" / "share";
Check(ReadFile(share / "applications" / "net.example.Desktop.desktop") == ReadFile(fixture / "app.desktop"), "the desktop entry is installed under the app-id");
Check(ReadFile(share / "metainfo" / "net.example.Desktop.metainfo.xml") == ReadFile(fixture / "app.metainfo.xml"), "the metainfo is installed under the app-id");
Check(fs::exists(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Desktop.svg"), "an .svg is the scalable icon");
Check(fs::exists(share / "icons" / "hicolor" / "512x512" / "apps" / "net.example.Desktop.png"), "a .png goes under its own size");
app.flatpak.desktopFile = fixture / "wrong-icon.desktop";
Check(BuildIt(app).contains("has to be the app-id"), "an Icon= other than the app-id is refused");
// Spelled with spaces around '=', which the Desktop Entry spec allows and
// every reader of the entry ignores.
std::ofstream(scratch / "spaced-icon.desktop") << "[Desktop Entry]\nType=Application\nName=Desktop\nExec=desktop\nIcon = desktop\n";
app.flatpak.desktopFile = scratch / "spaced-icon.desktop";
Check(BuildIt(app).contains("has to be the app-id"), "so is one spelled 'Icon = desktop'");
app.flatpak.desktopFile = fixture / "app.desktop";
app.flatpak.icons = { scratch / "wide.png" };
Check(BuildIt(app).contains("has to be square"), "a png that is not square is refused");
app.flatpak.icons = { fixture / "app.desktop" };
Check(BuildIt(app).contains("has to be an .svg or a .png"), "an icon that is neither is refused");
app.flatpak.icons = { fixture / "icon.svg" };
if (std::system("command -v desktop-file-validate > /dev/null 2>&1") == 0) {
app.flatpak.desktopFile = fixture / "invalid.desktop";
Check(BuildIt(app).contains("does not pass desktop-file-validate"), "an entry desktop-file-validate rejects is refused");
// A write that copies the entry and then fails, here for a metainfo
// that does not exist, leaves it in files/share unchanged for the
// next write, which copies nothing and has to check it all the same.
app.flatpak.metainfo = scratch / "missing.metainfo.xml";
Check(BuildIt(app).contains("missing.metainfo.xml"), "a write that copies the entry and then fails is refused for what failed");
app.flatpak.metainfo = fixture / "app.metainfo.xml";
Check(BuildIt(app).contains("does not pass desktop-file-validate"), "and the next build refuses the entry it left unchecked");
app.flatpak.desktopFile = fixture / "app.desktop";
}
app.flatpak.appId = "Desktop";
Check(BuildIt(app).contains("is not a Flatpak application ID"), "an invalid app-id is refused");
fs::remove_all(scratch, ec);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}