In crafter-build's Flatpak, whose compiler keeps libc++ in /app and whose /usr has none, a file that includes a header of the C++ standard library could not be parsed for the lint rules that read its AST in a build like that of #46: the file built, and `crafter-build lint` then ran none of those rules on it, printed "'algorithm' file not found" and exited 1. libclang parses through a clang driver of its own. In a build for the host whose compile command names no libc++ directory, as #46's names none, where a sysroot or CRAFTER_BUILD_LIBCXX_DIR would name one, that driver looks for libc++'s headers under ../include from the directory of its argv[0], then under /usr/local/include and /usr/include. ParseAst gave libclang the compile command without its compiler, and clang_parseTranslationUnit names argv[0] plain "clang", in no directory. So the parse of such a build, natively as in the Flatpak, took the first libc++ it found under ../include from the working directory, /usr/local/include and /usr/include, whichever compiler the build used. The -resource-dir ParseAst passes is for clang's own headers and changes nothing for libc++. The compile command's argv[0] would not do on its own: a clang++ from PATH is the bare name clang++, in no directory either, and a clang++ reached through a link into another directory takes itself to be where the link leads, as clang finds its installation from the file it runs as. The compiler says where that is, on the InstalledDir line of `clang++ --version`, which GetToolchain() already runs for clangVersion. Toolchain::installedDir keeps it, InstalledClangxx(command) names the compiler in it, and ParseAst passes that as argv[0], through clang_parseTranslationUnit2FullArgv, which takes argv[0] from its caller. With argv[0] there, the parse finds libc++ where the compile does, whatever the working directory. A compile command can have the compiler look from elsewhere: with -no-canonical-prefixes, where the last of it and -canonical-prefixes is that one, clang takes itself to be at clangxx as written, or, where no file is there, as is usual for a bare name, at the one PATH gives, a link not followed, and `clang++ -no-canonical-prefixes --version` names that directory. GetToolchain() runs that too, Toolchain::installedDirAsWritten keeps what it names, and InstalledClangxx(command) names the compiler there for a command whose last prefix flag is -no-canonical-prefixes, as clang's own driver reads them. argv[0] also gives libclang's driver its mode, by its file name, and the configuration files it reads by default, from its directory. The parse now runs in the g++ mode the compile runs in, where it ran in clang's gcc mode, and reads the default configuration files beside the compiler that the compile reads, clang++.cfg among them; before, it read none from there, as the driver skips the directory of an argv[0] that has none. -resource-dir stays, for a libclang of another release than the compiler, whose driver would derive the directory from argv[0] for its own release. The comment on it said libclang found clang's own headers relative to its own install path; its driver derived them from argv[0], as it does libc++'s. Fixes #46
253 lines
15 KiB
C++
253 lines
15 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, llvm-ar and
|
|
// clang-scan-deps 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;
|
|
// What reads the module a C++ source declares and the modules it
|
|
// imports, by preprocessing it with its compile command (Build).
|
|
fs::path clangScanDeps;
|
|
// 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 nvcc that compiles cfg.cuda: <CUDA_PATH>/bin/nvcc when CUDA_PATH
|
|
// is set, else bin/nvcc of the first toolkit that has one among
|
|
// ToolchainInputs::cudaRoots, else the first nvcc in an absolute entry
|
|
// of PATH. In a sandbox, a toolkit is looked up as the host has it:
|
|
// under hostRoot where it is in the host's /usr or /etc, and, where
|
|
// its path is a link, through that link and the link its target is in
|
|
// turn, each taken as the host's, wherever they point. Empty when
|
|
// there is none, and nvccError says where it was looked for; only a
|
|
// build with a .cu to compile reports that, so no other build fails
|
|
// for want of a CUDA toolkit.
|
|
fs::path nvcc;
|
|
std::string nvccError;
|
|
// What that nvcc is, for VariantId: its canonical path, size and
|
|
// modification time, which change when the toolkit is updated or
|
|
// replaced at the same path, as the path alone does not. Empty
|
|
// without an nvcc.
|
|
std::string nvccStamp;
|
|
// What -arch=native compiles for on this machine, for VariantId: the
|
|
// NVIDIA display devices the PCI bus lists, by address and device ID,
|
|
// and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER, which choose among
|
|
// them and number them, each unset apart from empty. Empty without an
|
|
// nvcc.
|
|
std::string cudaNativeKey;
|
|
// The static CUDA runtime a configuration with CUDA sources links:
|
|
// libcudart_static.a in the lib64 beside nvcc's bin. Empty without an
|
|
// nvcc, and where that lib64 holds none, as where a distribution keeps
|
|
// the toolkit's libraries in its own directories, or cannot be looked
|
|
// into: then nothing is linked, and cfg.linkFlags names the runtime.
|
|
fs::path cudaRuntime;
|
|
// 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;
|
|
// The directory clang++ is installed in, from the InstalledDir line
|
|
// of that `clang++ --version`. clang names the directory of the file
|
|
// it runs from, so for a clang++ found on PATH, or reached through a
|
|
// link into another directory, it is where the compiler really is,
|
|
// the directory its driver looks for headers and configuration files
|
|
// from (InstalledClangxx). Empty when clang++ could not be run or
|
|
// named no directory.
|
|
fs::path installedDir;
|
|
// The same line of `clang++ -no-canonical-prefixes --version`. With
|
|
// that flag clang names the directory of clang++ as written, or, where
|
|
// no file is there, as is usual for a bare name, of the one PATH
|
|
// gives, a link not followed, and a compile whose last prefix flag is
|
|
// that one looks from there. Empty as installedDir is.
|
|
fs::path installedDirAsWritten;
|
|
// Why clang++ could not be resolved or run; empty when it could.
|
|
std::string error;
|
|
|
|
// clangxx as a program that runs a clang driver of its own over
|
|
// `command`, a compile command that starts with clangxx, has to be
|
|
// given it: the same file name in the directory clang started with
|
|
// that command takes itself to be in, installedDirAsWritten where the
|
|
// last of -canonical-prefixes and -no-canonical-prefixes in the
|
|
// command is -no-canonical-prefixes, as clang's own driver decides it,
|
|
// and installedDir otherwise. That driver takes the directory of the
|
|
// compiler it is given as written, without following a link to where
|
|
// the compiler is, and looks from there for what the compiler's own
|
|
// driver looks for from its directory: clang's own headers where it is
|
|
// not told where they are, libc++'s in a build for the host whose
|
|
// command names no libc++ directory, and its default configuration
|
|
// files. clangxx itself where that directory is empty.
|
|
fs::path InstalledClangxx(std::string_view command) const;
|
|
// 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, CUDA_PATH and PATH, 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;
|
|
// Where a CUDA toolkit is installed when CUDA_PATH names none, in the
|
|
// order they are tried and as the host names them: GetToolchain()
|
|
// passes /opt/cuda, Arch's, and /usr/local/cuda, NVIDIA's installers'.
|
|
std::vector<fs::path> cudaRoots;
|
|
// The PCI devices sysfs lists, one directory each with its vendor,
|
|
// device and class: GetToolchain() passes /sys/bus/pci/devices, which
|
|
// the sandbox shows too. What -arch=native compiles for is keyed by
|
|
// the NVIDIA display devices among them.
|
|
fs::path pciDevices;
|
|
// CUDA_VISIBLE_DEVICES, which chooses the GPUs -arch=native compiles
|
|
// for, when it is set: set but empty hides them all, which is not the
|
|
// same as unset, so it is kept apart from env, which holds non-empty
|
|
// values alone.
|
|
std::optional<std::string> cudaVisibleDevices;
|
|
// CUDA_DEVICE_ORDER, when it is set: it decides which GPU a number in
|
|
// CUDA_VISIBLE_DEVICES means, by PCI address or by speed.
|
|
std::optional<std::string> cudaDeviceOrder;
|
|
};
|
|
// 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.
|
|
// nvcc is not left to the shell's PATH lookup but searched for here,
|
|
// PATH included, so a build that cannot find one can say where it looked.
|
|
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();
|
|
}
|