// 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, the // files/ Build() lays out for it, which becomes /app in the app's sandbox, // and what `crafter-build install` and `bundle` hand the host's flatpak to // make the app of it. Everything here works on plain data; Build() and those // verbs gather it from the Configuration, so this partition knows nothing of // one. export namespace Crafter { // The runtime an executable packaged as a Flatpak runs on. Its branch is // always the one crafter-build itself runs on: the program is linked // against that Sdk's glibc, which the Platform of the same branch carries // as well, and an older runtime's glibc would not load it. enum class FlatpakRuntime { // org.freedesktop.Platform: what a program needs to run. Platform, // org.freedesktop.Sdk, for a program that runs tools at run time that // the Platform does not have, git for one. Sdk, }; // A file or directory of the project installed under /app/share. struct FlatpakShareEntry { // A file, or a directory: every regular file in it, at its path // inside it, leaving out empty directories and links to directories. fs::path source; // Where under /app/share, relative to it: "crafter-review/process", // "licenses/net.example.App/LICENSE". fs::path target; }; // What an executable packaged as a Flatpak is installed as. Read for an // executable whose Configuration::packaging makes it one, except // `permissions`, which every configuration may declare: a library's // reach each executable built on it. struct FlatpakApp { // The application ID, reverse-DNS. Empty means // DefaultFlatpakAppId(outputName). std::string appId; FlatpakRuntime runtime = FlatpakRuntime::Platform; // The sandbox's permissions, spelled as Flatpak's finish-args are: // "--socket=wayland", "--device=dri", "--filesystem=xdg-documents:ro". // An executable gets those of its dependencies first and its own // last, and flatpak takes a later one over an earlier one for the // same socket, device, path or bus name, so "--nosocket=x11" takes // back what a library granted. `crafter-build install` and `bundle` // print the resulting list, naming where each came from, and hand // it to the host's flatpak build-finish, which refuses one it does // not know. The build itself does not read them. std::vector permissions; // A desktop entry, installed as .desktop. Its Icon= has to be // the app-id, and desktop-file-validate, where there is one, has to // pass it, as one a share entry places under that name has to. Empty: // the app has none but what a share entry places. fs::path desktopFile; // AppStream metadata, installed as .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 icons; // Files the program reads that belong in /app/share rather than next // to it, where cfg.files goes: data a program finds at /../share/, licences. They are copied into // files/share, kept there as long as they are listed, and gone from // it once they are not; files/share is the writer's, and anything // else in it is removed. -r runs the program from files/bin, so the // same ../share lookup finds them there. Any file may be one, a // second desktop entry or an add-on's metainfo among them: flatpak // build-finish exports what it exports of them by its own rules, as // for any app's, and build-export refuses an icon that is not a // square image or a D-Bus service that is not a key file, and warns // of the app's own desktop entry where desktop-file-validate rejects // it. None may go where another file goes, a field's included, and a // directory may neither hold what the build writes, which it would // copy into itself, nor lie in it, the build's own output: files/, // and the app's directory, FlatpakAppDir(), where install and bundle // put another copy of files/ and the exported app, however their // links lead. std::vector share; // flatpak-builder module files, in Flatpak's own format (JSON or // YAML), for what the app needs in /app besides the program: a // runtime it starts, a tool it runs, data built from source. With // any, `crafter-build install` and `bundle` have flatpak-builder make // the app from a manifest that lists them and copies the program in // last, so the program is not compiled again. crafter-build only // names them; flatpak-builder reads them. std::vector modules; }; // local.crafterbuild.: 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 icons; std::vector share; // FlatpakAppDir(): build-dir/files there is files/ again once install // or bundle has run, so no share entry may copy from it. fs::path appDir; // 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 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 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 the `share` entries at the paths they name, 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& names); // For :Clang's install and bundle alone, kept out of the API a // project.cpp compiles against, so that the names cannot meet one of its // own under `using namespace Crafter`. // A permission and the configuration that declared it. struct FlatpakPermission { std::string argument; std::string source; }; // What `crafter-build install` and `bundle` hand the host's flatpak, // gathered from the Configuration like FlatpakFiles. Paths are absolute: // the host runs the commands from its /. struct FlatpakExport { // BinDir()/files, as WriteFlatpakFiles left it. fs::path files; // /build/flatpak-apps/: the repo, the build // directory the app is finished in, and with modules the manifest // and flatpak-builder's state. One per app rather than per variant, // because the state is flatpak-builder's cache, and a variant-keyed // path would build every module again after each compiler update. fs::path work; std::string appId; std::string command; FlatpakRuntime runtime = FlatpakRuntime::Platform; // The arch and branch of the runtime crafter-build runs on, from its // /.flatpak-info: the app's runtime and Sdk are at the same. std::string arch; std::string branch; // The executable's dependencies' first, its own last. std::vector permissions; std::vector modules; }; // The manifest flatpak-builder makes an app with modules from: the // app-id, runtime, sdk, command and finish-args, the project's modules, // and last a module that copies files/ into /app as it is. std::string FlatpakManifestText(const FlatpakExport& app); // Makes the app on the host and puts it into work/repo. Without modules, // the host's flatpak build-finish finishes work/build-dir, made afresh // each time from a metadata naming the app, its runtime and the Sdk and // a copy of files/, hard links where they can be made: it writes the // command and the sandbox's permissions into the metadata, and export/, // by flatpak's own rules, and refuses a permission it does not know. // flatpak build-export then puts that into the repo. With modules, // flatpak-builder makes the app from FlatpakManifestText, written to // work/.json, and runs build-finish itself. Either way the // permissions are printed first, with where each came from, so a // refusal can be traced to the configuration that declared it. // Returns why it could not; empty when it did. HostFlatpakUnavailable is // the caller's to check first, and the lock on work/ that // InstallFlatpakApp and BundleFlatpakApp take is the caller's to hold: // every variant of the app exports into the one repo. std::string ExportFlatpak(const FlatpakExport& app); }