docs: the Flatpak output in the README and --help

A section on packaging a program as a Flatpak: how to ask for it, what
its bin directory holds and what flatpak build-finish adds to it, what
cfg.flatpak describes, what cannot be a Flatpak and why, and -march for
other machines. It also says where a program finds its files once
installed, which is not the working directory -r gives it. --help lists
the standard flags with --packaging= among them, says how -r runs a
program, one for the host in its own directory, a wasm32 one under
wasmtime or wasmer and one built for the browser served from its
directory, and names CRAFTER_BUILD_FLATPAK_INFO. The package script
example tars ProgramDir(), the program and its files.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 19:26:32 +01:00
commit 194af206cc
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 53 additions and 9 deletions

View file

@ -14,7 +14,7 @@ alias crafter-build='flatpak run --die-with-parent net.catcrafts.CrafterBuild'
`--die-with-parent` ends the sandbox with the `flatpak run` that started it. Without the flag, killing only the `flatpak run` process leaves the sandbox running: a build carries on writing into `bin/` and `build/`, and a program started with `-r` keeps running. With it, bubblewrap kills everything in the sandbox with SIGKILL when the `flatpak run` process dies or when that process's parent does. For `crafter-build …` or `crafter-build … &` typed at a bash or zsh prompt, that parent is the shell, so the sandbox ends with the shell, even after a `disown`. Ctrl-C reaches the program either way.
Releases are published to the `stable` branch that the commands above install, and every release also attaches a single-file bundle on the Forgejo releases page (`flatpak install --user crafter-build-<version>.flatpak`). The sandbox sees the whole host filesystem, so projects can live anywhere. Binaries built for the host link the bundled `libc++.so.1` through an rpath into the Flatpak: they run inside it (`-r`, the test runner), and are not meant to be copied out onto the host.
Releases are published to the `stable` branch that the commands above install, and every release also attaches a single-file bundle on the Forgejo releases page (`flatpak install --user crafter-build-<version>.flatpak`). The sandbox sees the host's own files where it shares them, the home, `/mnt`, `/opt`, `/srv` and `/run/media` among them, so a project can live there; it has its own `/tmp`, `/var`, `/usr`, `/etc` and `/app`. Binaries built for the host link the bundled `libc++.so.1` through an rpath into the Flatpak: they run inside it (`-r`, the test runner), and are not meant to be copied out onto the host. To run a program outside crafter-build's sandbox, package it as a Flatpak of its own ([Flatpak output](#flatpak-output)).
## Writing a project.cpp
@ -41,7 +41,43 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
}
```
Run `crafter-build` in that directory, outputs land at `bin/myapp-<target>-<march>-<mtune>-<hash>/myapp` and intermediates at `build/myapp-<target>-<march>-<mtune>-<hash>/`. The hash covers everything that changes what is compiled (build type, defines, flags, sysroot, the compiler itself), so two such builds never share a directory. After a compiler update, a new crafter-build release included, the first build starts a fresh directory and recompiles. `crafter-build clean` removes the ones left behind. Add `-r` to run the produced executable straight after a successful build.
Run `crafter-build` in that directory, outputs land at `bin/myapp-<target>-<march>-<mtune>-<hash>/myapp` and intermediates at `build/myapp-<target>-<march>-<mtune>-<hash>/`. The hash covers everything that changes what is compiled (build type, defines, flags, sysroot, the compiler itself), so two such builds never share a directory. After a compiler update, a new crafter-build release included, the first build starts a fresh directory and recompiles. `crafter-build clean` removes the ones left behind. Add `-r` to run the produced executable straight after a successful build, in its own directory, where its shaders, `cfg.files` and assets are; a wasm32 one runs under wasmtime, or wasmer where there is no wasmtime, with no directory given to it, and one built for the browser (`EnableWasiBrowserRuntime`) is served over HTTP from that directory instead.
## Flatpak output
A program crafter-build's Flatpak links for Linux without a sysroot takes the libc++, libc++abi and libunwind of its LLVM and the glibc of its runtime, so outside the Flatpak it starts only on a host that has those libraries and a glibc with every symbol version they and the program reference, 2.38 or newer for the bundled libc++. A build lays the program out as a Flatpak of its own when it is packaged:
```cpp
cfg.packaging = Packaging::Flatpak; // or, for one build: --packaging=flatpak, where project.cpp calls ApplyStandardArgs
```
Its bin directory then holds `files/`, what becomes `/app` inside the app's sandbox:
```
bin/myapp-<variant>/
└── files/
├── bin/ myapp and the files it reads: shaders, cfg.files, assets
├── lib/ libc++, libc++abi, libunwind, and the shared libraries it links from its dependencies and from crafter-build's /app/lib
└── share/ the desktop entry, metainfo and icons, under the app-id
```
`-r` runs `files/bin/myapp` from that directory, inside crafter-build's sandbox, against the libraries in `files/lib`, the ones the app carries. A Flatpak build directory holds two things more: `metadata`, naming the app, its runtime and its sandbox's permissions, and `export/`, what the host sees of the app, such as its desktop entry and icons. `flatpak build-finish` adds the command and the permissions to a metadata that names the app and its runtime, and writes `export/`, by flatpak's own rules.
Installed, the program starts in the directory it is started from, when its permissions let it see that directory, and in `$HOME` otherwise. A program that reads its own files therefore finds them next to its executable (`/proc/self/exe`) rather than in the working directory, which is only its own under `-r`.
What the app is installed as is `cfg.flatpak`; a hello world sets none of it:
```cpp
cfg.flatpak.appId = "net.example.MyGame"; // default: local.crafterbuild.<outputName>
cfg.flatpak.desktopFile = "packaging/MyGame.desktop"; // installed as <appId>.desktop; its Icon= is the app-id
cfg.flatpak.metainfo = "packaging/MyGame.metainfo.xml";
cfg.flatpak.icons = { "packaging/icon.svg" }; // an .svg is scalable, a square .png goes under its size
```
- **What cannot be a Flatpak** fails the build saying why: Windows and wasm targets, which keep their `.exe` and `.wasm`; a build against a sysroot, which links that sysroot's libraries and not the runtime's, or one for musl, a C library no runtime has; an architecture other than the runtime's; a build against `CRAFTER_BUILD_LIBCXX_DIR`, since a Flatpak carries the toolchain's own libc++, built against the runtime's glibc; and a build by a native crafter-build, whose programs link the host's glibc, unless `CRAFTER_BUILD_FLATPAK_INFO`, which the tests set, names a runtime. A dependency's executables stay as linked even when `--packaging=flatpak` reaches their project, since their consumers run them by path, and tests are never packaged. A dependency that sets `cfg.packaging` in its own `project.cpp` is packaged, and the files its `cfg.flatpak` names are found in its own directory, as its `cfg.files` are.
- **For other machines**, build with `--march=x86-64-v3` rather than the default `native`, which can crash with SIGILL on an older CPU. Pass it on the command line or in `CRAFTER_BUILD_MARCH`: either reaches every configuration of the program that `ApplyStandardArgs` is called on, a sibling's whose `project.cpp` calls it included, while CMake externals are compiled without `-march`, and a `#pragma crafter dependency`, tooling built for `project.so` alone, with `-march=native`.
Without packaging (`Packaging::None`, and for now `Default`), a program is the executable as linked: it runs inside crafter-build's sandbox (`-r`, `test`, `run` scripts), and on a host only where the host has what it links.
## Dependencies
@ -207,7 +243,7 @@ For `--target=x86_64-w64-mingw32` (Windows from Linux), the pipeline cross-compi
For `--target=wasm32-wasip1`, the WASI sysroot is the one beside the toolchain (the Flatpak bundles one) unless `CRAFTER_BUILD_WASI_SYSROOT` names another, and tests run under `wasmtime`. C++ exceptions work as on the other targets: every unit and the std module are compiled with `-fwasm-exceptions` in the standardized form of WebAssembly exception handling (exnref), against the exception-enabled libc++ in the sysroot's `lib/<triple>/eh/`, and `-lunwind` is linked automatically. A sysroot without that libc++ (wasi-sdk before 33) stops the build with one line saying so. The result runs on engines with that proposal: Chrome 137, Firefox 131 and Safari 18.4 or newer, and the wasmtime the Flatpak bundles. A page from `EnableWasiBrowserRuntime` checks first and names what an older browser lacks in the console, instead of failing to compile the module. An exception that escapes `main` leaves the program as an uncaught WebAssembly exception (wasmtime reports `thrown Wasm exception` and exits 1, a page gets a `WebAssembly.Exception`) without calling `std::terminate`, so a `std::set_terminate` handler does not run and nothing prints the exception's type or `what()`: wasm exception handling unwinds in a single phase, and nothing finds out that no handler is left until the stack is gone. Catch it in `main` to report it.
A wasm build refuses three things before it starts, each with a message that says why: `-flto` in `compileFlags` or `linkFlags` (LTO code generation drops the exception handling, [WebAssembly/wasi-sdk#629](https://github.com/WebAssembly/wasi-sdk/issues/629)), `-fno-exceptions` (the std module is built with exceptions, and a unit without them cannot import it) and a shared library (the exception-enabled libc++ is static archives only). `setjmp` and `longjmp` work in C and C++ sources, with `-lsetjmp` linked automatically like `-lunwind`; a `longjmp` across a `noexcept` function ends the program, because it is a wasm exception and the function's terminate handler catches it. Outside the Flatpak, a wasmtime older than 47 has exception handling off and needs `-W exceptions=y`, and a `wasm-opt` on `PATH` from a binaryen that cannot read the standardized encoding breaks release links, since clang runs it on optimized wasm links when it finds one.
A wasm build refuses four things before it starts, each with a message that says why: `-flto` in `compileFlags` or `linkFlags` (LTO code generation drops the exception handling, [WebAssembly/wasi-sdk#629](https://github.com/WebAssembly/wasi-sdk/issues/629)), `-fno-exceptions` (the std module is built with exceptions, and a unit without them cannot import it), a shared library (the exception-enabled libc++ is static archives only) and packaging as a Flatpak (a wasm build stays a `.wasm`; see [Flatpak output](#flatpak-output)). `setjmp` and `longjmp` work in C and C++ sources, with `-lsetjmp` linked automatically like `-lunwind`; a `longjmp` across a `noexcept` function ends the program, because it is a wasm exception and the function's terminate handler catches it. Outside the Flatpak, a wasmtime older than 47 has exception handling off and needs `-W exceptions=y`, and a `wasm-opt` on `PATH` from a binaryen that cannot read the standardized encoding breaks release links, since clang runs it on optimized wasm links when it finds one.
In a page from `EnableWasiBrowserRuntime`, `exit` and `_Exit` end the program as they would anywhere else: none of its code runs on the way out, no destructor and no `catch (...)`, its status is in `window.crafter_wasi.exitCode`, and bridges that call into it on their own are expected to stop. A program that hands the thread to the browser for good and goes on serving calls, as Crafter.Graphics's `Window::StartSync()` does, parks instead of exiting. Parking never returns, runs nothing of the program either, and leaves `main`'s locals valid for the calls that follow:
@ -344,7 +380,7 @@ The callback returns the exit status, and `crafter-build run` exits with it. `ct
cfg.scripts.push_back({"package", [](Crafter::ScriptContext& ctx) {
const std::string dist = Crafter::ShellQuote((ctx.projectDir / "dist").string());
if (const std::int32_t code = ctx.Run(std::format("mkdir -p {}", dist)); code != 0) return code;
return ctx.Run(std::format("tar -czf {}/myapp.tar.gz -C {} .", dist, Crafter::ShellQuote(ctx.project.BinDir().string())));
return ctx.Run(std::format("tar -czf {}/myapp.tar.gz -C {} .", dist, Crafter::ShellQuote(ctx.project.ProgramDir().string())));
}});
```