Crafter.Build/interfaces/Crafter.Build-Flatpak.cppm

187 lines
10 KiB
Text
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®
export module Crafter.Build:Flatpak;
import std;
namespace fs = std::filesystem;
feat: install and bundle a Flatpak through the host's flatpak An executable packaged as a Flatpak came out as the files/ of one, which only flatpak build-finish makes an app of, and getting it installed or onto another machine took flatpak commands on the host, typed by hand. Two verbs do it where the host's flatpak can be reached: `crafter-build install` builds the project and installs its app for this user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a single file for `flatpak install --user` that names Flathub's repository for the runtime. Inside crafter-build's Flatpak they need the org.freedesktop.Flatpak permission, which its manifest does not grant in this commit. Both put the app into the project's repo first, in build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than per variant, since flatpak-builder's state there is its cache. Every variant exports into it, so the export and the install or bundle made from it hold a lock there, and another crafter-build exporting meanwhile waits rather than leave this one installing or bundling its program. The user remote an install re-points is every checkout's that builds the app-id, so re-pointing it and installing from it hold a second lock, which every crafter-build installing there takes, natively or in the Flatpak, whatever its XDG_CACHE_HOME: in ~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's own installation, which flatpak hides from the sandbox behind a tmpfs of the sandbox's own, and in the directory of a private one, FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that directory with the host. Without modules, the host's flatpak build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh each time from a metadata naming the app, its runtime and the Sdk and a tree of hard links to the build's files/, copies where a link cannot be made, across filesystems or on one without links, vfat or exFAT: it writes the command, the sandbox's permissions and export/ by flatpak's own rules, those of whichever flatpak the host runs, and refuses a permission it does not know, and flatpak build-export puts the result into the repo. When cfg.flatpak.modules names flatpak-builder module files for what the app needs in /app besides the program, flatpak-builder does both, from a manifest that lists them and copies the built program in last, so nothing is compiled twice. That manifest keeps share/locale in the app ("separate-locales": false): flatpak-builder would split it into a .Locale extension by default, which a bundle, holding the app alone, leaves out. The loader rebases a dependency's module files as it does its cfg.files. install uses a user remote named after the app-id, re-pointed at the repo each time, and says how to remove both. The app's tree in build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a module's files too, is left out of the include walk with every other build/, so the headers a module installed reach no compile. cfg.flatpak gets the runtime, the Platform or the Sdk at the branch crafter-build runs on, and the sandbox's permissions, finish-args as Flatpak spells them, which any configuration may declare: an executable gets its dependencies' first and its own last, so it can take one back. The build reads neither, and a change to either links nothing. install and bundle print the permissions, with where each came from, and hand them to build-finish, the one judge of what they mean, so a refusal can be traced to the configuration that declared it. FlatpakPermissions checks the order everywhere, and that a change to one neither moves the build to another directory nor links anything, and, where the host's flatpak runs, what build-finish makes of them and that it refuses a socket it does not have; FlatpakBundle that build-finish exports the desktop entry and the icon, and that the build's own files/ is left as it was; VariantId's test has the runtime, permissions and modules move no directory. The host's flatpak and flatpak-builder are reached only through HostCommand in :Platform, which builds the command lines of RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak, started in the host's / and passing FLATPAK_USER_DIR on, and the command itself natively. flatpak-spawn would otherwise start a command in crafter-build's own working directory, which the host need not have, and from /app, say, every host command would fail. HostFlatpakUnavailable says why the host cannot be reached: no org.freedesktop.Flatpak permission, no session bus (CI's build sandbox has neither), a host that does not see the project's bin/ and build/ where crafter-build does, as it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or where a link leads into one, or no flatpak natively. That, and an executable that is not packaged as a Flatpak, install and bundle refuse as soon as the project is loaded, before anything is built; FlatpakVerbRefusal shows the second. The host is asked rather than the sandbox's paths modelled, which an override sharing a directory inside the sandbox's own would defeat: its flatpak is started in a directory just made in each of bin/ and build/ under a name no other has, which it enters only where it sees that very directory, and the directory is removed again. --help gets a section on both, and its Clean section says the repo an install points at goes with build/. FlatpakModules runs flatpak-builder only with a flatpak-builder program on the host, crafter-build falling back to the org.flatpak.Builder app, whose `flatpak run` would write ~/.var/app in the real home, and with the Platform and Sdk it builds the app with. The library's install function is InstallFlatpakApp because project.cpp files define an InstallFlatpak script of their own under `using namespace Crafter`, this repository's included, and a library InstallFlatpak would make the name an overload set, which the scripts table's std::function cannot be built from; BundleFlatpakApp is named to match. JsonQuote writes the manifest's strings, and variants.json's, which escaped quotes and backslashes only. FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission, which only :Clang uses, stay out of the API a project.cpp compiles against, as RecordFlatpakPlaced does, so their names cannot meet a project's own under `using namespace Crafter`; ExportFlatpak's comment names the app's lock, which its callers hold, beside HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// 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.
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
export namespace Crafter {
feat: install and bundle a Flatpak through the host's flatpak An executable packaged as a Flatpak came out as the files/ of one, which only flatpak build-finish makes an app of, and getting it installed or onto another machine took flatpak commands on the host, typed by hand. Two verbs do it where the host's flatpak can be reached: `crafter-build install` builds the project and installs its app for this user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a single file for `flatpak install --user` that names Flathub's repository for the runtime. Inside crafter-build's Flatpak they need the org.freedesktop.Flatpak permission, which its manifest does not grant in this commit. Both put the app into the project's repo first, in build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than per variant, since flatpak-builder's state there is its cache. Every variant exports into it, so the export and the install or bundle made from it hold a lock there, and another crafter-build exporting meanwhile waits rather than leave this one installing or bundling its program. The user remote an install re-points is every checkout's that builds the app-id, so re-pointing it and installing from it hold a second lock, which every crafter-build installing there takes, natively or in the Flatpak, whatever its XDG_CACHE_HOME: in ~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's own installation, which flatpak hides from the sandbox behind a tmpfs of the sandbox's own, and in the directory of a private one, FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that directory with the host. Without modules, the host's flatpak build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh each time from a metadata naming the app, its runtime and the Sdk and a tree of hard links to the build's files/, copies where a link cannot be made, across filesystems or on one without links, vfat or exFAT: it writes the command, the sandbox's permissions and export/ by flatpak's own rules, those of whichever flatpak the host runs, and refuses a permission it does not know, and flatpak build-export puts the result into the repo. When cfg.flatpak.modules names flatpak-builder module files for what the app needs in /app besides the program, flatpak-builder does both, from a manifest that lists them and copies the built program in last, so nothing is compiled twice. That manifest keeps share/locale in the app ("separate-locales": false): flatpak-builder would split it into a .Locale extension by default, which a bundle, holding the app alone, leaves out. The loader rebases a dependency's module files as it does its cfg.files. install uses a user remote named after the app-id, re-pointed at the repo each time, and says how to remove both. The app's tree in build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a module's files too, is left out of the include walk with every other build/, so the headers a module installed reach no compile. cfg.flatpak gets the runtime, the Platform or the Sdk at the branch crafter-build runs on, and the sandbox's permissions, finish-args as Flatpak spells them, which any configuration may declare: an executable gets its dependencies' first and its own last, so it can take one back. The build reads neither, and a change to either links nothing. install and bundle print the permissions, with where each came from, and hand them to build-finish, the one judge of what they mean, so a refusal can be traced to the configuration that declared it. FlatpakPermissions checks the order everywhere, and that a change to one neither moves the build to another directory nor links anything, and, where the host's flatpak runs, what build-finish makes of them and that it refuses a socket it does not have; FlatpakBundle that build-finish exports the desktop entry and the icon, and that the build's own files/ is left as it was; VariantId's test has the runtime, permissions and modules move no directory. The host's flatpak and flatpak-builder are reached only through HostCommand in :Platform, which builds the command lines of RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak, started in the host's / and passing FLATPAK_USER_DIR on, and the command itself natively. flatpak-spawn would otherwise start a command in crafter-build's own working directory, which the host need not have, and from /app, say, every host command would fail. HostFlatpakUnavailable says why the host cannot be reached: no org.freedesktop.Flatpak permission, no session bus (CI's build sandbox has neither), a host that does not see the project's bin/ and build/ where crafter-build does, as it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or where a link leads into one, or no flatpak natively. That, and an executable that is not packaged as a Flatpak, install and bundle refuse as soon as the project is loaded, before anything is built; FlatpakVerbRefusal shows the second. The host is asked rather than the sandbox's paths modelled, which an override sharing a directory inside the sandbox's own would defeat: its flatpak is started in a directory just made in each of bin/ and build/ under a name no other has, which it enters only where it sees that very directory, and the directory is removed again. --help gets a section on both, and its Clean section says the repo an install points at goes with build/. FlatpakModules runs flatpak-builder only with a flatpak-builder program on the host, crafter-build falling back to the org.flatpak.Builder app, whose `flatpak run` would write ~/.var/app in the real home, and with the Platform and Sdk it builds the app with. The library's install function is InstallFlatpakApp because project.cpp files define an InstallFlatpak script of their own under `using namespace Crafter`, this repository's included, and a library InstallFlatpak would make the name an overload set, which the scripts table's std::function cannot be built from; BundleFlatpakApp is named to match. JsonQuote writes the manifest's strings, and variants.json's, which escaped quotes and backslashes only. FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission, which only :Clang uses, stay out of the API a project.cpp compiles against, as RecordFlatpakPlaced does, so their names cannot meet a project's own under `using namespace Crafter`; ExportFlatpak's comment names the app's lock, which its callers hold, beside HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// 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,
};
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
// What an executable packaged as a Flatpak is installed as. Read for an
feat: install and bundle a Flatpak through the host's flatpak An executable packaged as a Flatpak came out as the files/ of one, which only flatpak build-finish makes an app of, and getting it installed or onto another machine took flatpak commands on the host, typed by hand. Two verbs do it where the host's flatpak can be reached: `crafter-build install` builds the project and installs its app for this user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a single file for `flatpak install --user` that names Flathub's repository for the runtime. Inside crafter-build's Flatpak they need the org.freedesktop.Flatpak permission, which its manifest does not grant in this commit. Both put the app into the project's repo first, in build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than per variant, since flatpak-builder's state there is its cache. Every variant exports into it, so the export and the install or bundle made from it hold a lock there, and another crafter-build exporting meanwhile waits rather than leave this one installing or bundling its program. The user remote an install re-points is every checkout's that builds the app-id, so re-pointing it and installing from it hold a second lock, which every crafter-build installing there takes, natively or in the Flatpak, whatever its XDG_CACHE_HOME: in ~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's own installation, which flatpak hides from the sandbox behind a tmpfs of the sandbox's own, and in the directory of a private one, FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that directory with the host. Without modules, the host's flatpak build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh each time from a metadata naming the app, its runtime and the Sdk and a tree of hard links to the build's files/, copies where a link cannot be made, across filesystems or on one without links, vfat or exFAT: it writes the command, the sandbox's permissions and export/ by flatpak's own rules, those of whichever flatpak the host runs, and refuses a permission it does not know, and flatpak build-export puts the result into the repo. When cfg.flatpak.modules names flatpak-builder module files for what the app needs in /app besides the program, flatpak-builder does both, from a manifest that lists them and copies the built program in last, so nothing is compiled twice. That manifest keeps share/locale in the app ("separate-locales": false): flatpak-builder would split it into a .Locale extension by default, which a bundle, holding the app alone, leaves out. The loader rebases a dependency's module files as it does its cfg.files. install uses a user remote named after the app-id, re-pointed at the repo each time, and says how to remove both. The app's tree in build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a module's files too, is left out of the include walk with every other build/, so the headers a module installed reach no compile. cfg.flatpak gets the runtime, the Platform or the Sdk at the branch crafter-build runs on, and the sandbox's permissions, finish-args as Flatpak spells them, which any configuration may declare: an executable gets its dependencies' first and its own last, so it can take one back. The build reads neither, and a change to either links nothing. install and bundle print the permissions, with where each came from, and hand them to build-finish, the one judge of what they mean, so a refusal can be traced to the configuration that declared it. FlatpakPermissions checks the order everywhere, and that a change to one neither moves the build to another directory nor links anything, and, where the host's flatpak runs, what build-finish makes of them and that it refuses a socket it does not have; FlatpakBundle that build-finish exports the desktop entry and the icon, and that the build's own files/ is left as it was; VariantId's test has the runtime, permissions and modules move no directory. The host's flatpak and flatpak-builder are reached only through HostCommand in :Platform, which builds the command lines of RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak, started in the host's / and passing FLATPAK_USER_DIR on, and the command itself natively. flatpak-spawn would otherwise start a command in crafter-build's own working directory, which the host need not have, and from /app, say, every host command would fail. HostFlatpakUnavailable says why the host cannot be reached: no org.freedesktop.Flatpak permission, no session bus (CI's build sandbox has neither), a host that does not see the project's bin/ and build/ where crafter-build does, as it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or where a link leads into one, or no flatpak natively. That, and an executable that is not packaged as a Flatpak, install and bundle refuse as soon as the project is loaded, before anything is built; FlatpakVerbRefusal shows the second. The host is asked rather than the sandbox's paths modelled, which an override sharing a directory inside the sandbox's own would defeat: its flatpak is started in a directory just made in each of bin/ and build/ under a name no other has, which it enters only where it sees that very directory, and the directory is removed again. --help gets a section on both, and its Clean section says the repo an install points at goes with build/. FlatpakModules runs flatpak-builder only with a flatpak-builder program on the host, crafter-build falling back to the org.flatpak.Builder app, whose `flatpak run` would write ~/.var/app in the real home, and with the Platform and Sdk it builds the app with. The library's install function is InstallFlatpakApp because project.cpp files define an InstallFlatpak script of their own under `using namespace Crafter`, this repository's included, and a library InstallFlatpak would make the name an overload set, which the scripts table's std::function cannot be built from; BundleFlatpakApp is named to match. JsonQuote writes the manifest's strings, and variants.json's, which escaped quotes and backslashes only. FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission, which only :Clang uses, stay out of the API a project.cpp compiles against, as RecordFlatpakPlaced does, so their names cannot meet a project's own under `using namespace Crafter`; ExportFlatpak's comment names the app's lock, which its callers hold, beside HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// executable whose Configuration::packaging makes it one, except
// `permissions`, which every configuration may declare: a library's
// reach each executable built on it.
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
struct FlatpakApp {
// The application ID, reverse-DNS. Empty means
// DefaultFlatpakAppId(outputName).
std::string appId;
feat: install and bundle a Flatpak through the host's flatpak An executable packaged as a Flatpak came out as the files/ of one, which only flatpak build-finish makes an app of, and getting it installed or onto another machine took flatpak commands on the host, typed by hand. Two verbs do it where the host's flatpak can be reached: `crafter-build install` builds the project and installs its app for this user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a single file for `flatpak install --user` that names Flathub's repository for the runtime. Inside crafter-build's Flatpak they need the org.freedesktop.Flatpak permission, which its manifest does not grant in this commit. Both put the app into the project's repo first, in build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than per variant, since flatpak-builder's state there is its cache. Every variant exports into it, so the export and the install or bundle made from it hold a lock there, and another crafter-build exporting meanwhile waits rather than leave this one installing or bundling its program. The user remote an install re-points is every checkout's that builds the app-id, so re-pointing it and installing from it hold a second lock, which every crafter-build installing there takes, natively or in the Flatpak, whatever its XDG_CACHE_HOME: in ~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's own installation, which flatpak hides from the sandbox behind a tmpfs of the sandbox's own, and in the directory of a private one, FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that directory with the host. Without modules, the host's flatpak build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh each time from a metadata naming the app, its runtime and the Sdk and a tree of hard links to the build's files/, copies where a link cannot be made, across filesystems or on one without links, vfat or exFAT: it writes the command, the sandbox's permissions and export/ by flatpak's own rules, those of whichever flatpak the host runs, and refuses a permission it does not know, and flatpak build-export puts the result into the repo. When cfg.flatpak.modules names flatpak-builder module files for what the app needs in /app besides the program, flatpak-builder does both, from a manifest that lists them and copies the built program in last, so nothing is compiled twice. That manifest keeps share/locale in the app ("separate-locales": false): flatpak-builder would split it into a .Locale extension by default, which a bundle, holding the app alone, leaves out. The loader rebases a dependency's module files as it does its cfg.files. install uses a user remote named after the app-id, re-pointed at the repo each time, and says how to remove both. The app's tree in build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a module's files too, is left out of the include walk with every other build/, so the headers a module installed reach no compile. cfg.flatpak gets the runtime, the Platform or the Sdk at the branch crafter-build runs on, and the sandbox's permissions, finish-args as Flatpak spells them, which any configuration may declare: an executable gets its dependencies' first and its own last, so it can take one back. The build reads neither, and a change to either links nothing. install and bundle print the permissions, with where each came from, and hand them to build-finish, the one judge of what they mean, so a refusal can be traced to the configuration that declared it. FlatpakPermissions checks the order everywhere, and that a change to one neither moves the build to another directory nor links anything, and, where the host's flatpak runs, what build-finish makes of them and that it refuses a socket it does not have; FlatpakBundle that build-finish exports the desktop entry and the icon, and that the build's own files/ is left as it was; VariantId's test has the runtime, permissions and modules move no directory. The host's flatpak and flatpak-builder are reached only through HostCommand in :Platform, which builds the command lines of RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak, started in the host's / and passing FLATPAK_USER_DIR on, and the command itself natively. flatpak-spawn would otherwise start a command in crafter-build's own working directory, which the host need not have, and from /app, say, every host command would fail. HostFlatpakUnavailable says why the host cannot be reached: no org.freedesktop.Flatpak permission, no session bus (CI's build sandbox has neither), a host that does not see the project's bin/ and build/ where crafter-build does, as it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or where a link leads into one, or no flatpak natively. That, and an executable that is not packaged as a Flatpak, install and bundle refuse as soon as the project is loaded, before anything is built; FlatpakVerbRefusal shows the second. The host is asked rather than the sandbox's paths modelled, which an override sharing a directory inside the sandbox's own would defeat: its flatpak is started in a directory just made in each of bin/ and build/ under a name no other has, which it enters only where it sees that very directory, and the directory is removed again. --help gets a section on both, and its Clean section says the repo an install points at goes with build/. FlatpakModules runs flatpak-builder only with a flatpak-builder program on the host, crafter-build falling back to the org.flatpak.Builder app, whose `flatpak run` would write ~/.var/app in the real home, and with the Platform and Sdk it builds the app with. The library's install function is InstallFlatpakApp because project.cpp files define an InstallFlatpak script of their own under `using namespace Crafter`, this repository's included, and a library InstallFlatpak would make the name an overload set, which the scripts table's std::function cannot be built from; BundleFlatpakApp is named to match. JsonQuote writes the manifest's strings, and variants.json's, which escaped quotes and backslashes only. FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission, which only :Clang uses, stay out of the API a project.cpp compiles against, as RecordFlatpakPlaced does, so their names cannot meet a project's own under `using namespace Crafter`; ExportFlatpak's comment names the app's lock, which its callers hold, beside HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
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<std::string> permissions;
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
// 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;
feat: install and bundle a Flatpak through the host's flatpak An executable packaged as a Flatpak came out as the files/ of one, which only flatpak build-finish makes an app of, and getting it installed or onto another machine took flatpak commands on the host, typed by hand. Two verbs do it where the host's flatpak can be reached: `crafter-build install` builds the project and installs its app for this user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a single file for `flatpak install --user` that names Flathub's repository for the runtime. Inside crafter-build's Flatpak they need the org.freedesktop.Flatpak permission, which its manifest does not grant in this commit. Both put the app into the project's repo first, in build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than per variant, since flatpak-builder's state there is its cache. Every variant exports into it, so the export and the install or bundle made from it hold a lock there, and another crafter-build exporting meanwhile waits rather than leave this one installing or bundling its program. The user remote an install re-points is every checkout's that builds the app-id, so re-pointing it and installing from it hold a second lock, which every crafter-build installing there takes, natively or in the Flatpak, whatever its XDG_CACHE_HOME: in ~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's own installation, which flatpak hides from the sandbox behind a tmpfs of the sandbox's own, and in the directory of a private one, FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that directory with the host. Without modules, the host's flatpak build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh each time from a metadata naming the app, its runtime and the Sdk and a tree of hard links to the build's files/, copies where a link cannot be made, across filesystems or on one without links, vfat or exFAT: it writes the command, the sandbox's permissions and export/ by flatpak's own rules, those of whichever flatpak the host runs, and refuses a permission it does not know, and flatpak build-export puts the result into the repo. When cfg.flatpak.modules names flatpak-builder module files for what the app needs in /app besides the program, flatpak-builder does both, from a manifest that lists them and copies the built program in last, so nothing is compiled twice. That manifest keeps share/locale in the app ("separate-locales": false): flatpak-builder would split it into a .Locale extension by default, which a bundle, holding the app alone, leaves out. The loader rebases a dependency's module files as it does its cfg.files. install uses a user remote named after the app-id, re-pointed at the repo each time, and says how to remove both. The app's tree in build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a module's files too, is left out of the include walk with every other build/, so the headers a module installed reach no compile. cfg.flatpak gets the runtime, the Platform or the Sdk at the branch crafter-build runs on, and the sandbox's permissions, finish-args as Flatpak spells them, which any configuration may declare: an executable gets its dependencies' first and its own last, so it can take one back. The build reads neither, and a change to either links nothing. install and bundle print the permissions, with where each came from, and hand them to build-finish, the one judge of what they mean, so a refusal can be traced to the configuration that declared it. FlatpakPermissions checks the order everywhere, and that a change to one neither moves the build to another directory nor links anything, and, where the host's flatpak runs, what build-finish makes of them and that it refuses a socket it does not have; FlatpakBundle that build-finish exports the desktop entry and the icon, and that the build's own files/ is left as it was; VariantId's test has the runtime, permissions and modules move no directory. The host's flatpak and flatpak-builder are reached only through HostCommand in :Platform, which builds the command lines of RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak, started in the host's / and passing FLATPAK_USER_DIR on, and the command itself natively. flatpak-spawn would otherwise start a command in crafter-build's own working directory, which the host need not have, and from /app, say, every host command would fail. HostFlatpakUnavailable says why the host cannot be reached: no org.freedesktop.Flatpak permission, no session bus (CI's build sandbox has neither), a host that does not see the project's bin/ and build/ where crafter-build does, as it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or where a link leads into one, or no flatpak natively. That, and an executable that is not packaged as a Flatpak, install and bundle refuse as soon as the project is loaded, before anything is built; FlatpakVerbRefusal shows the second. The host is asked rather than the sandbox's paths modelled, which an override sharing a directory inside the sandbox's own would defeat: its flatpak is started in a directory just made in each of bin/ and build/ under a name no other has, which it enters only where it sees that very directory, and the directory is removed again. --help gets a section on both, and its Clean section says the repo an install points at goes with build/. FlatpakModules runs flatpak-builder only with a flatpak-builder program on the host, crafter-build falling back to the org.flatpak.Builder app, whose `flatpak run` would write ~/.var/app in the real home, and with the Platform and Sdk it builds the app with. The library's install function is InstallFlatpakApp because project.cpp files define an InstallFlatpak script of their own under `using namespace Crafter`, this repository's included, and a library InstallFlatpak would make the name an overload set, which the scripts table's std::function cannot be built from; BundleFlatpakApp is named to match. JsonQuote writes the manifest's strings, and variants.json's, which escaped quotes and backslashes only. FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission, which only :Clang uses, stay out of the API a project.cpp compiles against, as RecordFlatpakPlaced does, so their names cannot meet a project's own under `using namespace Crafter`; ExportFlatpak's comment names the app's lock, which its callers hold, beside HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// 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<fs::path> modules;
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
};
// 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);
feat: install and bundle a Flatpak through the host's flatpak An executable packaged as a Flatpak came out as the files/ of one, which only flatpak build-finish makes an app of, and getting it installed or onto another machine took flatpak commands on the host, typed by hand. Two verbs do it where the host's flatpak can be reached: `crafter-build install` builds the project and installs its app for this user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a single file for `flatpak install --user` that names Flathub's repository for the runtime. Inside crafter-build's Flatpak they need the org.freedesktop.Flatpak permission, which its manifest does not grant in this commit. Both put the app into the project's repo first, in build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than per variant, since flatpak-builder's state there is its cache. Every variant exports into it, so the export and the install or bundle made from it hold a lock there, and another crafter-build exporting meanwhile waits rather than leave this one installing or bundling its program. The user remote an install re-points is every checkout's that builds the app-id, so re-pointing it and installing from it hold a second lock, which every crafter-build installing there takes, natively or in the Flatpak, whatever its XDG_CACHE_HOME: in ~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's own installation, which flatpak hides from the sandbox behind a tmpfs of the sandbox's own, and in the directory of a private one, FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that directory with the host. Without modules, the host's flatpak build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh each time from a metadata naming the app, its runtime and the Sdk and a tree of hard links to the build's files/, copies where a link cannot be made, across filesystems or on one without links, vfat or exFAT: it writes the command, the sandbox's permissions and export/ by flatpak's own rules, those of whichever flatpak the host runs, and refuses a permission it does not know, and flatpak build-export puts the result into the repo. When cfg.flatpak.modules names flatpak-builder module files for what the app needs in /app besides the program, flatpak-builder does both, from a manifest that lists them and copies the built program in last, so nothing is compiled twice. That manifest keeps share/locale in the app ("separate-locales": false): flatpak-builder would split it into a .Locale extension by default, which a bundle, holding the app alone, leaves out. The loader rebases a dependency's module files as it does its cfg.files. install uses a user remote named after the app-id, re-pointed at the repo each time, and says how to remove both. The app's tree in build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a module's files too, is left out of the include walk with every other build/, so the headers a module installed reach no compile. cfg.flatpak gets the runtime, the Platform or the Sdk at the branch crafter-build runs on, and the sandbox's permissions, finish-args as Flatpak spells them, which any configuration may declare: an executable gets its dependencies' first and its own last, so it can take one back. The build reads neither, and a change to either links nothing. install and bundle print the permissions, with where each came from, and hand them to build-finish, the one judge of what they mean, so a refusal can be traced to the configuration that declared it. FlatpakPermissions checks the order everywhere, and that a change to one neither moves the build to another directory nor links anything, and, where the host's flatpak runs, what build-finish makes of them and that it refuses a socket it does not have; FlatpakBundle that build-finish exports the desktop entry and the icon, and that the build's own files/ is left as it was; VariantId's test has the runtime, permissions and modules move no directory. The host's flatpak and flatpak-builder are reached only through HostCommand in :Platform, which builds the command lines of RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak, started in the host's / and passing FLATPAK_USER_DIR on, and the command itself natively. flatpak-spawn would otherwise start a command in crafter-build's own working directory, which the host need not have, and from /app, say, every host command would fail. HostFlatpakUnavailable says why the host cannot be reached: no org.freedesktop.Flatpak permission, no session bus (CI's build sandbox has neither), a host that does not see the project's bin/ and build/ where crafter-build does, as it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or where a link leads into one, or no flatpak natively. That, and an executable that is not packaged as a Flatpak, install and bundle refuse as soon as the project is loaded, before anything is built; FlatpakVerbRefusal shows the second. The host is asked rather than the sandbox's paths modelled, which an override sharing a directory inside the sandbox's own would defeat: its flatpak is started in a directory just made in each of bin/ and build/ under a name no other has, which it enters only where it sees that very directory, and the directory is removed again. --help gets a section on both, and its Clean section says the repo an install points at goes with build/. FlatpakModules runs flatpak-builder only with a flatpak-builder program on the host, crafter-build falling back to the org.flatpak.Builder app, whose `flatpak run` would write ~/.var/app in the real home, and with the Platform and Sdk it builds the app with. The library's install function is InstallFlatpakApp because project.cpp files define an InstallFlatpak script of their own under `using namespace Crafter`, this repository's included, and a library InstallFlatpak would make the name an overload set, which the scripts table's std::function cannot be built from; BundleFlatpakApp is named to match. JsonQuote writes the manifest's strings, and variants.json's, which escaped quotes and backslashes only. FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission, which only :Clang uses, stay out of the API a project.cpp compiles against, as RecordFlatpakPlaced does, so their names cannot meet a project's own under `using namespace Crafter`; ExportFlatpak's comment names the app's lock, which its callers hold, beside HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// 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;
// <project>/build/flatpak-apps/<app-id>: 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<FlatpakPermission> permissions;
std::vector<fs::path> 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/<app-id>.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);
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
}