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.
173 lines
9.7 KiB
C++
173 lines
9.7 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
export module Crafter.Build:Toolchain;
|
|
import std;
|
|
namespace fs = std::filesystem;
|
|
|
|
export namespace Crafter {
|
|
// The compiler crafter-build drives and what sits next to it, resolved
|
|
// once per process by GetToolchain(). Every clang++, clang and llvm-ar
|
|
// the tool spawns, the libclang the linter dlopens, the std module
|
|
// source and the default sysroots come from here, so a toolchain
|
|
// installed anywhere other than /usr (the Flatpak carries its own under
|
|
// /app) needs no per-call-site knowledge.
|
|
struct Toolchain {
|
|
// LLVM install prefix. Empty when the tools are bare names on PATH.
|
|
fs::path prefix;
|
|
fs::path clangxx;
|
|
fs::path clang;
|
|
fs::path llvmAr;
|
|
// What a packaged program's shared libraries are read with.
|
|
fs::path llvmReadelf;
|
|
// Where clang-c/Index.h lives when it is not on the default include
|
|
// path; empty otherwise.
|
|
fs::path llvmIncludeDir;
|
|
// The exact libclang to dlopen; nullopt means search the usual names.
|
|
std::optional<fs::path> libclang;
|
|
// A libc++ install other than the compiler's own
|
|
// (CRAFTER_BUILD_LIBCXX_DIR); empty means clang finds its own.
|
|
fs::path libcxxDir;
|
|
// libc++'s std module source for host builds.
|
|
fs::path stdCppm;
|
|
// The _LIBCPP_VERSION of the libc++ stdCppm belongs to (220108 for
|
|
// 22.1.8); 0 when its <__config> could not be read.
|
|
std::int32_t libcxxVersion = 0;
|
|
// Std module sources for libc++ releases other than the toolchain's
|
|
// own, one <major>/std.cppm each, for sysroots that carry libc++
|
|
// without them: <crafter prefix>/share/crafter-build/libc++-modules.
|
|
// Empty when the crafter prefix is unknown; need not exist.
|
|
fs::path stdModulesDir;
|
|
fs::path wasiSysroot;
|
|
// The mingw-w64 install prefix, the directory that holds
|
|
// x86_64-w64-mingw32/ and lib/gcc/x86_64-w64-mingw32/ (/usr for a
|
|
// distro package), absolute and lexically normal. Empty means clang's
|
|
// own discovery, which follows the mingw gcc on PATH. Set by
|
|
// CRAFTER_MINGW_DIR, else the host's /usr when running sandboxed and
|
|
// it has mingw-w64 (the sandbox's own /usr never does). What the
|
|
// mingw target passes as --sysroot is MingwSysroot(), not this:
|
|
// see mingwPrefixIsSystem.
|
|
fs::path mingwPrefix;
|
|
// The prefix holds a Linux system as well as mingw-w64: the host's
|
|
// /usr in the sandbox, or a prefix whose include/ has a C library's
|
|
// features.h, as /usr has. clang searches a mingw sysroot's include/
|
|
// and lib/, so such a prefix is shown to it only through a view of
|
|
// its mingw-w64 parts; any other prefix is the sysroot itself, and
|
|
// Windows libraries installed into its include/ and lib/ are found.
|
|
bool mingwPrefixIsSystem = false;
|
|
// The Flatpak runtime crafter-build runs on, from the
|
|
// runtime=runtime/<id>/<arch>/<branch> line of /.flatpak-info:
|
|
// "x86_64" and "26.08" in the Flatpak, both empty outside one. A
|
|
// program built here links that runtime's glibc, so an executable is
|
|
// only packaged as a Flatpak when they are set, and for that branch.
|
|
// CRAFTER_BUILD_FLATPAK_INFO names another file to read them from,
|
|
// for tests that package outside the Flatpak.
|
|
std::string flatpakArch;
|
|
std::string flatpakBranch;
|
|
// `clang++ -print-target-triple` and the first line of
|
|
// `clang++ --version`; both empty when clang++ could not be run.
|
|
std::string hostTriple;
|
|
std::string clangVersion;
|
|
// Why clang++ could not be resolved or run; empty when it could.
|
|
std::string error;
|
|
|
|
// Flags for compiling against libcxxDir; empty without one.
|
|
std::string LibcxxCompileFlags() const;
|
|
// Where libc++.so.1 is when clang would not find it itself: an
|
|
// explicit libcxxDir's lib, or <prefix>/lib of a toolchain outside
|
|
// /usr, /app/lib in crafter-build's Flatpak, which holds the rest of
|
|
// its LLVM as well. Empty for a toolchain in /usr, whose libraries
|
|
// are on the linker's and the loader's default paths.
|
|
fs::path LibcxxLibDir() const;
|
|
// Flags for linking against the libc++ the compiler will use: a -L
|
|
// and an rpath for <prefix>/lib when the prefix is not the system one
|
|
// (clang searches only its per-target runtime directory and the
|
|
// system paths, and a bundled toolchain's libc++.so.1 is on neither
|
|
// the linker's nor the loader's default path), or for an explicit
|
|
// libcxxDir.
|
|
std::string LibcxxLinkFlags() const;
|
|
// LibcxxLinkFlags without the rpath: for a program that finds its
|
|
// copy of libc++ somewhere else at run time, as a Flatpak does.
|
|
std::string LibcxxSearchFlags() const;
|
|
// Names this toolchain for cache directory names: the clang version
|
|
// followed by a hash of what decides which headers and libraries a
|
|
// PCM is built against. Two toolchains with different keys never
|
|
// share a PCM.
|
|
std::string Key() const;
|
|
};
|
|
|
|
// What ResolveToolchain reads, so a test can feed it a staged layout.
|
|
struct ToolchainInputs {
|
|
// The CRAFTER_* variables, present only when set and non-empty.
|
|
std::map<std::string, std::string, std::less<>> env;
|
|
// The <prefix> of <prefix>/share/crafter-build; empty when unknown.
|
|
fs::path crafterPrefix;
|
|
// Where the host's root shows inside a sandbox (/run/host in a
|
|
// Flatpak); empty outside one.
|
|
fs::path hostRoot;
|
|
// What /.flatpak-info holds, or the file CRAFTER_BUILD_FLATPAK_INFO
|
|
// names; empty outside a Flatpak.
|
|
std::string flatpakInfo;
|
|
};
|
|
// Pure path resolution: an env override wins, then a toolchain installed
|
|
// beside crafter-build (<crafterPrefix>/bin/clang++), then bare names on
|
|
// PATH. Never runs a process, so hostTriple and clangVersion stay empty.
|
|
Toolchain ResolveToolchain(const ToolchainInputs& inputs);
|
|
// ResolveToolchain over the real environment plus the two clang probes,
|
|
// computed once and cached. Never throws.
|
|
const Toolchain& GetToolchain();
|
|
// GetToolchain(), or a std::runtime_error carrying its error.
|
|
const Toolchain& RequireToolchain();
|
|
// Makes, in the first of `parents` that can hold one, a directory that
|
|
// shows clang only the mingw-w64 parts of `prefix`: links named
|
|
// x86_64-w64-mingw32, lib/gcc and lib64/gcc to the same paths under
|
|
// `prefix`, at <parent>/mingw-sysroot/<hash of prefix>, and returns it.
|
|
// A view already there is reused; one another process makes at the same
|
|
// moment is as good as one made here. Throws when no parent can hold
|
|
// one, with the reason for each.
|
|
fs::path MakeMingwSysroot(const fs::path& prefix, std::span<const fs::path> parents);
|
|
// The --sysroot of every compile, link and CMake configure for the mingw
|
|
// target, settled on the first call of a process: empty without a
|
|
// mingwPrefix, the prefix itself for one that holds mingw-w64 alone, and
|
|
// for a system prefix the view MakeMingwSysroot makes in crafter-build's
|
|
// cache, or in $XDG_RUNTIME_DIR/crafter.build when the cache's
|
|
// filesystem takes no symbolic links. A system prefix cannot be the
|
|
// sysroot: clang takes a mingw sysroot for a tree that holds mingw-w64
|
|
// alone and searches its include/ and lib/ as well, which for /usr are
|
|
// the Linux system's own. A Windows build then saw glibc's headers, and
|
|
// libstdc++, finding <sys/single_threaded.h>, had shared_ptr read
|
|
// __libc_single_threaded, which nothing on Windows defines (issue #43).
|
|
// Throws when the view cannot be made.
|
|
const fs::path& MingwSysroot();
|
|
// `text` with every path into the view MingwSysroot() made written as
|
|
// the same path into the prefix it shows, so that compiler messages name
|
|
// a header where the user installed it rather than in a hashed cache
|
|
// directory; `text` unchanged when no view was made.
|
|
std::string MingwViewToPrefix(std::string text);
|
|
// _LIBCPP_VERSION as the <__config> in `includeDir` (an include/c++/v1)
|
|
// defines it, XXYYZZ; 0 when the file is missing or has no such line.
|
|
std::int32_t LibcxxVersion(const fs::path& includeDir);
|
|
// "23.1.2" for 230102.
|
|
std::string LibcxxVersionString(std::int32_t version);
|
|
|
|
struct StdModuleSource {
|
|
fs::path stdCppm;
|
|
// One line saying why no source fits; empty when stdCppm is set.
|
|
std::string error;
|
|
};
|
|
// The std module source to precompile for a native (non-wasm) build
|
|
// against `sysroot`. The module source is a list of using-declarations
|
|
// for names the headers of the same libc++ release declare, so one from
|
|
// another release fails on every name that release added or dropped. In
|
|
// order: the sysroot's own <sysroot>/usr/share/libc++/v1/std.cppm, the
|
|
// toolchain's when both are the same major release, the copy for the
|
|
// sysroot's release under toolchain.stdModulesDir. Not every distro
|
|
// packages the module sources with its libc++ (Alpine does not), which
|
|
// is why the sysroot's own copy cannot be the only answer. When the
|
|
// sysroot's release cannot be read, the toolchain's source is returned
|
|
// and the compile is the check.
|
|
StdModuleSource SysrootStdModule(const Toolchain& toolchain, const fs::path& sysroot);
|
|
// The version baked in when crafter-build was built; "0.0.0-dev" for a
|
|
// build that was given none.
|
|
std::string_view CrafterBuildVersion();
|
|
}
|