Crafter.Build/tests/VariantId/main.cpp

296 lines
15 KiB
C++
Raw Normal View History

2026-07-23 01:24:42 +02:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
namespace {
2026-07-23 01:24:42 +02:00
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
2026-07-23 01:24:42 +02:00
++Failures;
}
}
Configuration MakeBase() {
Configuration cfg;
cfg.path = "/tmp/variant-id-test";
cfg.name = "vt";
cfg.outputName = "vt";
cfg.target = "x86_64-pc-linux-gnu";
cfg.march = "native";
cfg.mtune = "native";
cfg.type = ConfigurationType::Executable;
return cfg;
}
}
// VariantId is the cache key for build outputs. Two Configurations that
// differ only in something that affects codegen must produce different
// VariantId / BuildDir / BinDir, otherwise their .o files collide.
int main() {
{
// Baseline against itself: VariantId is deterministic.
Configuration a = MakeBase();
Configuration b = MakeBase();
Check(a.VariantId() == b.VariantId(), "identical configs have identical VariantId");
Check(a.BuildDir() == b.BuildDir(), "identical configs have identical BuildDir");
}
{
// type differs -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.type = ConfigurationType::LibraryStatic;
Check(a.VariantId() != b.VariantId(), "type change perturbs VariantId");
}
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 Flatpak lays its bin dir out unlike the executable as linked, so
// the two get directories of their own. Only a Flatpak is keyed:
// Default and None, and a library whatever it asks for, keep the
// directory they had, and what the app is installed as changes
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
// files/share and what install and bundle make of it, never what is
// compiled.
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
Configuration plain = MakeBase();
Configuration none = MakeBase();
none.packaging = Packaging::None;
Configuration flatpak = MakeBase();
flatpak.packaging = Packaging::Flatpak;
Configuration described = MakeBase();
described.packaging = Packaging::Flatpak;
described.flatpak.appId = "net.example.Described";
described.flatpak.desktopFile = "app.desktop";
described.flatpak.icons = {"icon.svg"};
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
described.flatpak.runtime = FlatpakRuntime::Sdk;
described.flatpak.permissions = {"--socket=wayland"};
described.flatpak.modules = {"packaging/extra.json"};
feat: install a Flatpak's data under /app/share A program packaged as a Flatpak could only have its files beside it in files/bin, where cfg.files goes. One that finds its data at <its directory>/../share/<name>, as Crafter.Review finds its process/ directory, had no way to get it there: files/share belongs to the writer, which removes anything it did not put there, and a module of flatpak-builder's would have placed it only in the installed app, never under -r. cfg.flatpak.share lists files and directories of the project and the path under /app/share each goes to. The writer copies them into files/share with the desktop entry, metainfo and icons, keeps them while they are listed and removes them once they are not, as it does those. -r runs the program from files/bin, so the same ../share lookup finds them there. What is exported of them is flatpak build-finish's to decide, by its own rules, as for any app's files: the app's D-Bus services, MIME types, search providers and KRunner plugins, which have no field of their own, are placed like any other file. A library's are not passed on to the programs built on it, since data a library needs at run time already goes through cfg.files. A dependency that packages its own executable has the sources rebased by the loader, as its cfg.files are. Share entries change files/share and not what is compiled, so they are not keyed into VariantId, which its test checks. Every file bound for files/share, the desktop entry, metainfo and icons included, now goes through one placement check, which refuses two files for one path, a field's file among them, and a file where another needs a directory, naming both. A target that is absolute or leaves share/ is refused too. Any other place is open to a share entry, a second desktop entry, an add-on's metainfo and any icon included, as for any app's files: flatpak build-export refuses an exported icon that is not a square image and a D-Bus service that is not a key file, warns of the app's own desktop entry where desktop-file-validate rejects it or its Icon= is not the app's, and exports a second desktop entry, a MIME package or metainfo as it is. The app's own desktop entry, applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's checks whichever placed it: its Icon= and desktop-file-validate are keyed to that place now, not to the field. A source directory that holds what the build writes is refused as well, since it would be copied into itself, growing each time the app is built or exported: files/, and the app's directory, where install and bundle put another copy of files/ and the exported app, compared as written and where links lead, so that the project's own directory is refused where its bin/ is a link elsewhere too. So is one that lies in them, the build's own output, which the next build, install or bundle writes again. The loader leaves an empty source empty, as it does an empty desktop file. FlatpakShareLarge holds the writer to comparing an entry with its copy at constant memory, which the program's data, gigabytes of it in a game, needs on every build: it rewrites a files/ holding a 32 MiB entry, with nothing changed, in a child process whose address space may grow by 16 MiB, with a stand-in llvm-readelf, since the real one, which a write starts for the libraries, would not fit under that limit, and a change in the entry's last byte, in the short block a comparison reads last, is still copied.
2026-11-02 20:04:14 +01:00
described.flatpak.share = {{"data", "app/data"}};
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
Configuration library = MakeBase();
library.type = ConfigurationType::LibraryStatic;
Configuration flatpakLibrary = MakeBase();
flatpakLibrary.type = ConfigurationType::LibraryStatic;
flatpakLibrary.packaging = Packaging::Flatpak;
Check(plain.VariantId() == none.VariantId(), "Default and None share a directory");
Check(plain.VariantId() != flatpak.VariantId(), "a Flatpak gets a directory of its own");
feat: install a Flatpak's data under /app/share A program packaged as a Flatpak could only have its files beside it in files/bin, where cfg.files goes. One that finds its data at <its directory>/../share/<name>, as Crafter.Review finds its process/ directory, had no way to get it there: files/share belongs to the writer, which removes anything it did not put there, and a module of flatpak-builder's would have placed it only in the installed app, never under -r. cfg.flatpak.share lists files and directories of the project and the path under /app/share each goes to. The writer copies them into files/share with the desktop entry, metainfo and icons, keeps them while they are listed and removes them once they are not, as it does those. -r runs the program from files/bin, so the same ../share lookup finds them there. What is exported of them is flatpak build-finish's to decide, by its own rules, as for any app's files: the app's D-Bus services, MIME types, search providers and KRunner plugins, which have no field of their own, are placed like any other file. A library's are not passed on to the programs built on it, since data a library needs at run time already goes through cfg.files. A dependency that packages its own executable has the sources rebased by the loader, as its cfg.files are. Share entries change files/share and not what is compiled, so they are not keyed into VariantId, which its test checks. Every file bound for files/share, the desktop entry, metainfo and icons included, now goes through one placement check, which refuses two files for one path, a field's file among them, and a file where another needs a directory, naming both. A target that is absolute or leaves share/ is refused too. Any other place is open to a share entry, a second desktop entry, an add-on's metainfo and any icon included, as for any app's files: flatpak build-export refuses an exported icon that is not a square image and a D-Bus service that is not a key file, warns of the app's own desktop entry where desktop-file-validate rejects it or its Icon= is not the app's, and exports a second desktop entry, a MIME package or metainfo as it is. The app's own desktop entry, applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's checks whichever placed it: its Icon= and desktop-file-validate are keyed to that place now, not to the field. A source directory that holds what the build writes is refused as well, since it would be copied into itself, growing each time the app is built or exported: files/, and the app's directory, where install and bundle put another copy of files/ and the exported app, compared as written and where links lead, so that the project's own directory is refused where its bin/ is a link elsewhere too. So is one that lies in them, the build's own output, which the next build, install or bundle writes again. The loader leaves an empty source empty, as it does an empty desktop file. FlatpakShareLarge holds the writer to comparing an entry with its copy at constant memory, which the program's data, gigabytes of it in a game, needs on every build: it rewrites a files/ holding a 32 MiB entry, with nothing changed, in a child process whose address space may grow by 16 MiB, with a stand-in llvm-readelf, since the real one, which a write starts for the libraries, would not fit under that limit, and a change in the entry's last byte, in the short block a comparison reads last, is still copied.
2026-11-02 20:04:14 +01:00
Check(flatpak.VariantId() == described.VariantId(), "the app-id, desktop entry, icons, runtime, permissions, modules and share entries do not move the directory");
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
Check(library.VariantId() == flatpakLibrary.VariantId(), "a library ignores packaging");
Check(flatpak.ProgramDir() == flatpak.BinDir() / "files" / "bin" && plain.ProgramDir() == plain.BinDir(), "a Flatpak's program lives in files/bin, everything else's in the bin dir");
}
{
// debug differs -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.debug = true;
Check(a.VariantId() != b.VariantId(), "debug change perturbs VariantId");
}
{
// sysroot differs -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.sysroot = "/opt/sysroot";
Check(a.VariantId() != b.VariantId(), "sysroot change perturbs VariantId");
}
{
// defines differ -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.defines.push_back({"FOO", "1"});
Check(a.VariantId() != b.VariantId(), "define added perturbs VariantId");
}
{
// Same define name, different value -> VariantId differs.
Configuration a = MakeBase();
a.defines.push_back({"FOO", "1"});
Configuration b = MakeBase();
b.defines.push_back({"FOO", "2"});
Check(a.VariantId() != b.VariantId(), "define value change perturbs VariantId");
}
{
// compileFlags differ -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.compileFlags.push_back("-fno-omit-frame-pointer");
Check(a.VariantId() != b.VariantId(), "compile flag added perturbs VariantId");
}
{
// target differs -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.target = "aarch64-linux-gnu";
Check(a.VariantId() != b.VariantId(), "target change perturbs VariantId");
}
{
// march differs -> VariantId differs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.march = "x86-64-v3";
Check(a.VariantId() != b.VariantId(), "march change perturbs VariantId");
}
feat(cuda): make the GPU architecture configurable, native by default nvcc ran with -arch=sm_89 for every build, which is code for that one architecture and its PTX, whatever GPU the program was for. Jorijn asked for the architecture to be configurable, and native when nothing sets it, as the CPU's is. cfg.cudaArch is nvcc's -arch now, and it defaults to "native": nvcc compiles for the GPUs it sees, and where it sees none it warns and uses its own default architecture. Like -march=native it is for a program that runs where it is built, since it carries no PTX, and `crafter-build bundle` warns of a bundle whose CUDA code, in the program or a library it is built on, is compiled for native, as it warns of -march=native. ApplyStandardArgs sets it from --cuda-arch= and CRAFTER_BUILD_CUDA_ARCH as it sets march from --march= and CRAFTER_BUILD_MARCH; AddTest and AddMarchVariants give their tests the parent's, as they give them its sysroot and debug flag; and ForceHostTooling gives host tooling the host's, as it gives it the host's march, since project.so runs it on the machine that loads it. A .cu is compiled again only when it is newer than its object, so the architecture keys VariantId, and so does the nvcc that compiles it, by what it is rather than where: Toolchain::nvccStamp, its canonical path, size and time, read without a process, changes when CUDA_PATH names another toolkit and when an update replaces the one at the same path, as a package does at /opt/cuda. native is keyed as well, by Toolchain::cudaNativeKey: the NVIDIA display devices sysfs lists, by PCI address and device ID, and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER, which choose and number them, each empty value apart from an unset one, read without a process. It is crafter-build's reading of the machine, not nvcc's. The word alone would keep an object compiled for a GPU the machine no longer has, after a GPU change or on another machine that shares the checkout, and that object carries no PTX for the GPU it has now, where sm_89's carried its PTX. Only a configuration with CUDA sources is keyed, so every other one keeps the directory it had, but for one built with --cuda-arch=, which was a project argument, hashed as one, and is crafter-build's own now.
2026-11-11 19:34:52 +01:00
{
// The GPU architecture decides what a .cu compiles into, and it need
// not touch the files whose times decide whether a .cu is compiled
// again, so it keys the directory of a configuration with CUDA
// sources, and only of one.
Configuration a = MakeBase();
a.cuda = { "/tmp/variant-id-test/kernel" };
Configuration b = MakeBase();
b.cuda = a.cuda;
b.cudaArch = "sm_89";
Check(a.VariantId() != b.VariantId(), "cudaArch change perturbs the VariantId of a configuration with CUDA sources");
Configuration c = MakeBase();
Configuration d = MakeBase();
d.cudaArch = "sm_89";
Check(c.VariantId() == d.VariantId(), "without CUDA sources, cudaArch leaves VariantId alone");
// So does the nvcc that compiles them, by what it is: CUDA_PATH can
// name another toolkit, and an update can replace one at its path,
// and the old one's objects must not pass for the new one's.
const Toolchain one = GetToolchain();
Toolchain other = one;
other.nvccStamp = "/opt/other-cuda/bin/nvcc|1|1";
Toolchain updated = other;
updated.nvccStamp = "/opt/other-cuda/bin/nvcc|2|2";
Check(a.VariantId(one) != a.VariantId(other), "another nvcc perturbs the VariantId of a configuration with CUDA sources");
Check(a.VariantId(other) != a.VariantId(updated), "so does an nvcc updated at the same path");
Check(c.VariantId(one) == c.VariantId(other), "without CUDA sources, the nvcc leaves VariantId alone");
// native is what nvcc finds on the machine, so the GPUs it finds key
// it as well, and only it.
Toolchain first = one;
first.cudaNativeKey = "0x2684|";
Toolchain swapped = one;
swapped.cudaNativeKey = "0x2b85|";
Check(a.VariantId(first) != a.VariantId(swapped), "another GPU perturbs the VariantId of a configuration with CUDA sources for native");
Check(b.VariantId(first) == b.VariantId(swapped), "but not for a named architecture");
Check(c.VariantId(first) == c.VariantId(swapped), "nor without CUDA sources");
}
fix: key the host PCM cache on source content, add clean, hash project args Three follow-ons to the stale-build report, all cases of an identity not capturing something that changes the output. The host PCM cache under <cache>/crafter.build/<target>-<march>/ is shared by every crafter-build on the machine, and freshness was a per-file mtime comparison. That cannot tell "this PCM is newer than my source" from "this PCM was built from different sources that happen to be newer", so a package install and a working checkout — or two checkouts of different versions — silently compiled their project.cpp against each other's declarations. Invalidation now keys on a stamp over the bytes of every module source, which also covers the case one file's mtime never could: the cached PCMs import each other, so a change to :Interface invalidates :Clang's PCM with Crafter.Build-Clang.cppm untouched. Project args ApplyStandardArgs does not itself interpret are now folded into VariantId. Such a flag typically decides what gets compiled or bundled — the report's example is --no-webgpu dropping entries from cfg.files — and without it both settings shared one bin dir and interleaved their outputs there, leaving a bundle matching neither. Sorted and deduplicated so flag order doesn't split the cache, and inherited by test Configurations. `crafter-build clean` removes the project's bin/ and build/ trees. It deliberately does not load project.cpp: cleaning is most often reached when something is already wrong, and a clean that first needs the project to compile is useless exactly then.
2026-07-30 17:43:09 +00:00
{
// A project flag the framework can't interpret still decides what gets
// compiled and bundled, so it has to key into VariantId. Otherwise both
// settings share one bin dir and interleave their outputs there — an
// index.html from one alongside a script from the other.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.projectArgs.push_back("--no-webgpu");
Check(a.VariantId() != b.VariantId(), "project arg perturbs VariantId");
Check(a.BinDir() != b.BinDir(), "project arg yields a distinct BinDir");
}
{
// Distinct project args must not collide with each other either.
Configuration a = MakeBase();
a.projectArgs.push_back("--no-webgpu");
Configuration b = MakeBase();
b.projectArgs.push_back("--no-audio");
Check(a.VariantId() != b.VariantId(), "different project args yield different VariantId");
}
{
// PcmDir() differs between Executable (in BuildDir) and Library
// (in BinDir) — Library PCMs land in the installable bin dir so
// downstream consumers can find them.
Configuration exe = MakeBase();
Configuration lib = MakeBase();
lib.type = ConfigurationType::LibraryStatic;
Check(exe.PcmDir() == exe.BuildDir(), "Executable PcmDir == BuildDir");
Check(lib.PcmDir() == lib.BinDir(), "Library PcmDir == BinDir");
}
{
// VariantId is embedded in the path so distinct ids produce distinct dirs.
Configuration a = MakeBase();
Configuration b = MakeBase();
b.debug = true;
Check(a.BuildDir() != b.BuildDir(), "different VariantId yields different BuildDir");
Check(a.BinDir() != b.BinDir(), "different VariantId yields different BinDir");
}
{
// The compiler is compile state as much as any flag: a PCM can only
// be read by the clang release that wrote it, and nothing in a
// project's sources changes when the compiler does. With the
// toolchain left out of the id, a project built with clang 22 kept
// its PCMs after crafter-build moved to clang 23; the build after
// the update found nothing stale, and the first edit failed with
// "module file ... uses an older format that is no longer supported"
// until the project was cleaned by hand.
Configuration cfg = MakeBase();
Toolchain older;
older.clangxx = "/app/bin/clang++";
older.clangVersion = "clang version 22.1.8";
Toolchain newer = older;
newer.clangVersion = "clang version 23.1.2";
Check(cfg.VariantId(older) != cfg.VariantId(newer), "a compiler update at the same path perturbs VariantId");
Check(cfg.VariantId(older) == cfg.VariantId(older), "VariantId under one toolchain is deterministic");
// Two installs of one release are still two toolchains: their
// libc++ need not be the same build.
Toolchain elsewhere = older;
elsewhere.clangxx = "/usr/bin/clang++";
Check(cfg.VariantId(older) != cfg.VariantId(elsewhere), "the same release installed elsewhere perturbs VariantId");
Check(cfg.VariantId() == cfg.VariantId(GetToolchain()), "VariantId() is the variant under the running toolchain");
Check(cfg.BuildDir().filename() == cfg.VariantId(GetToolchain()), "BuildDir is named for the running toolchain's variant");
}
{
// The mingw-w64 prefix decides what a mingw build compiles against.
// Before issue #43 it was clang's sysroot as is, which put the host's
// headers on the include path, and with the prefix left out of the
// id, the release that fixed that reused the objects built against
// them: the next build only relinked, and failed to link as before.
Configuration mingw = MakeBase();
mingw.target = "x86_64-w64-mingw32";
Toolchain discovered;
discovered.clangxx = "/app/bin/clang++";
Toolchain hostUsr = discovered;
hostUsr.mingwPrefix = "/run/host/usr";
Toolchain optMingw = discovered;
optMingw.mingwPrefix = "/opt/mingw";
Check(mingw.VariantId(discovered) != mingw.VariantId(hostUsr), "a mingw prefix perturbs a mingw configuration's VariantId");
Check(mingw.VariantId(hostUsr) != mingw.VariantId(optMingw), "two mingw prefixes are two variants");
Configuration host = MakeBase();
Check(host.VariantId(discovered) == host.VariantId(hostUsr), "the mingw prefix leaves other targets' variants alone");
// The std module's directory is keyed the same way, and so is a
// prefix given whole against one reached through a view.
Check(StdPcmKey(mingw, discovered) != StdPcmKey(mingw, hostUsr), "a mingw prefix moves the mingw std module");
Check(StdPcmKey(mingw, hostUsr) != StdPcmKey(mingw, optMingw), "two mingw prefixes are two std modules");
Toolchain hostUsrWhole = hostUsr;
hostUsrWhole.mingwPrefixIsSystem = !hostUsr.mingwPrefixIsSystem;
Check(StdPcmKey(mingw, hostUsr) != StdPcmKey(mingw, hostUsrWhole), "a prefix given whole and one shown through a view are two std modules");
Check(mingw.VariantId(hostUsr) != mingw.VariantId(hostUsrWhole), "and two variants, through the std module");
Check(StdPcmKey(host, discovered) == StdPcmKey(host, hostUsr), "the mingw prefix leaves other targets' std modules alone");
}
{
// Everything the std module is compiled with keys its directory, not
// only target and march. -mtune was left out, so a project built
// with -mtune=generic shared one std PCM with project.cpp's
// -mtune=native, and the two rebuilt it in turn on every run, under
// every module built against it; two sysroots did the same.
Toolchain tc;
tc.clangxx = "/app/bin/clang++";
tc.clangVersion = "clang version 23.1.2";
Configuration native = MakeBase();
Configuration generic = MakeBase();
generic.mtune = "generic";
Check(StdPcmKey(native, tc) != StdPcmKey(generic, tc), "-mtune keys the std module");
Configuration sysrootA = MakeBase();
sysrootA.sysroot = "/opt/sysroot-a";
Configuration sysrootB = MakeBase();
sysrootB.sysroot = "/opt/sysroot-b";
Check(StdPcmKey(sysrootA, tc) != StdPcmKey(sysrootB, tc) && StdPcmKey(sysrootA, tc) != StdPcmKey(native, tc), "the sysroot keys the std module");
Configuration wasm = MakeBase();
wasm.target = "wasm32-wasip1";
wasm.sysroot = "/opt/wasi-sysroot";
Configuration relaxed = MakeBase();
relaxed.target = wasm.target;
relaxed.sysroot = wasm.sysroot;
relaxed.wasmVariantFlags = {"-mrelaxed-simd"};
Check(StdPcmKey(wasm, tc) != StdPcmKey(relaxed, tc), "a wasm variant pass's flags key the std module");
Check(StdPcmKey(native, tc) == StdPcmKey(MakeBase(), tc), "one configuration, one std module");
Check(StdPcmKey(native, tc).starts_with(std::format("{}-{}-{}-", native.target, native.march, tc.Key())), std::format("the directory still says target, march and toolchain (got '{}')", StdPcmKey(native, tc)));
Check(native.VariantId(tc) != generic.VariantId(tc) && sysrootA.VariantId(tc) != sysrootB.VariantId(tc), "a build moves with its std module");
}
2026-07-23 01:24:42 +02:00
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}