From 1ddba745cf19d4adf781b3888c5d4af0c73227fa Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sat, 14 Nov 2026 23:42:47 +0100 Subject: [PATCH] fix(wasm): resolve the WASI sysroot where it is read, not in Build() A wasm32 configuration that names no sysroot is built against the toolchain's WASI sysroot. ApplyStandardArgs stores that sysroot in cfg.sysroot, for a project file that calls it. For a configuration whose project file sets the target itself, Build() stored it on its way in. VariantId hashes the sysroot, so such a configuration had one BuildDir, BinDir and PcmDir until its build started and others from then on. A build pass records each dependency's build under its PcmDir, and a configuration asks for a dependency by the PcmDir it has at that moment. A wasm32 library that two configurations of one pass are built on was recorded under the first directory for the one that asked first. One that asked after the library's build had started asked under the second directory, found nothing there, and built the library again. Configuration::ResolvedSysroot now gives what cfg.sysroot comes to: the toolchain's WASI sysroot for a wasm32 configuration that names none, cfg.sysroot as it stands for every other, as ResolvedPackaging gives what `packaging` comes to. VariantId, the std module's flags, key and source, the compile and link commands and the check that refuses a wasm build without a usable sysroot read it, and Build() no longer stores the sysroot in the configuration. ApplyStandardArgs still does: what a project file reads in cfg.sysroot after the call stays what it was. The directories a build writes keep their names: a configuration that named no sysroot was built into the ones its VariantId named once Build() had stored it, which are the ones it names now. StdPcmFlags takes the toolchain, as StdPcmKey does, so that a VariantId asked for under a toolchain resolves the sysroot under that one. --- implementations/Crafter.Build-Clang.cpp | 51 ++++++++++------------ implementations/Crafter.Build-Platform.cpp | 8 ++-- interfaces/Crafter.Build-Clang.cppm | 31 ++++++++++--- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index d95625f..ae6d10b 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -249,14 +249,15 @@ void Configuration::GetInterfacesAndImplementations(std::span interfac } } -std::string Crafter::StdPcmFlags(const Configuration& config) { +std::string Crafter::StdPcmFlags(const Configuration& config, const Toolchain& toolchain) { if (config.target == "x86_64-w64-mingw32") { // -femulated-tls keeps PCM TLS access matching libstdc++'s emutls // definitions; a mismatch surfaces as undefined std::__once_callable. return std::format(" --target={} -march={} -mtune={} -femulated-tls -O3 -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier", config.target, config.march, config.mtune); } const bool isWasm = config.target.starts_with("wasm32"); - const std::string sysrootFlag = config.sysroot.empty() ? std::string() : std::format(" --sysroot={}", config.sysroot); + const std::string sysroot = config.ResolvedSysroot(toolchain); + const std::string sysrootFlag = sysroot.empty() ? std::string() : std::format(" --sysroot={}", sysroot); // wasm32 rejects -march, and the std module has to be compiled the way // the units importing it are, a variant pass's flags (e.g. // -mrelaxed-simd) included, or the BMI is not feature-compatible. @@ -265,8 +266,8 @@ std::string Crafter::StdPcmFlags(const Configuration& config) { // libc++ headers (/../include/c++/v1) above --sysroot, so the // PCM would mix host libc++ with the foreign libc. Force the sysroot's // copy. - if (!isWasm && !config.sysroot.empty()) { - archFlags += std::format(" -nostdinc++ -isystem {}/usr/include/c++/v1", config.sysroot); + if (!isWasm && !sysroot.empty()) { + archFlags += std::format(" -nostdinc++ -isystem {}/usr/include/c++/v1", sysroot); } return std::format(" --target={} -std=c++26{}{} -O3 -Wno-reserved-identifier -Wno-reserved-module-identifier", config.target, sysrootFlag, archFlags); } @@ -281,7 +282,7 @@ std::string Crafter::StdPcmKey(const Configuration& config, const Toolchain& too // releases sharing a cache, as the Flatpak's branches do, each time under // the PCMs built against the previous one, which clang crashes on rather // than rejecting. - std::string compiled = StdPcmFlags(config); + std::string compiled = StdPcmFlags(config, toolchain); // The base command's part: the toolchain's key covers the compiler and // libc++, and for the mingw target what clang finds mingw-w64 through is // the prefix, given whole or through a view (MingwSysroot), named here by @@ -335,8 +336,8 @@ CompileCommand Crafter::GetCompileCommand(const Configuration& config) { : std::format(" -march={} -mtune={}", config.march, config.mtune); out.command = std::format("{} --target={}{} -std=c++26 -D CRAFTER_BUILD_CONFIGURATION_TARGET=\\\"{}\\\" -D CRAFTER_BUILD_CONFIGURATION_TARGET_{} -fprebuilt-module-path={} -fprebuilt-module-path={}", GetBaseCommand(config), config.target, archFlags, editedTarget, editedTarget, out.stdPcmDir.string(), out.pcmDir.string()); - if (!config.sysroot.empty()) { - out.command += std::format(" --sysroot={}", config.sysroot); + if (const std::string sysroot = config.ResolvedSysroot(GetToolchain()); !sysroot.empty()) { + out.command += std::format(" --sysroot={}", sysroot); // Same reason as in BuildStdPcm: the driver ranks the host // toolchain's own libc++ headers (/../include/c++/v1) above // --sysroot. A unit that only does `import std;` never notices, but @@ -350,7 +351,7 @@ CompileCommand Crafter::GetCompileCommand(const Configuration& config) { // option in compileFlags) on sysroot builds. Not for mingw, which // uses libstdc++ and has no libc++ in its sysroot. if (!isWasm && config.target != "x86_64-w64-mingw32") { - out.command += std::format(" -nostdinc++ -isystem {}/usr/include/c++/v1 -Wno-unused-command-line-argument", config.sysroot); + out.command += std::format(" -nostdinc++ -isystem {}/usr/include/c++/v1 -Wno-unused-command-line-argument", sysroot); } } if (isWasm) { @@ -627,7 +628,7 @@ namespace { if (config.type == ConfigurationType::LibraryDynamic) { return std::format("{}: wasm32 shared libraries are not supported: the exception-enabled libc++ they would link has static archives only", config.name); } - const fs::path sysroot = config.sysroot; + const fs::path sysroot = config.ResolvedSysroot(GetToolchain()); if (!fs::is_directory(sysroot)) { return std::format("no WASI sysroot at {}: the Flatpak bundles one, and CRAFTER_BUILD_WASI_SYSROOT selects another", sysroot.string()); } @@ -811,17 +812,10 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map/usr/share/libc++/v1/. std::string stdCppm; if (isWasm) { - stdCppm = std::format("{}/share/libc++/v1/std.cppm", config.sysroot); + stdCppm = std::format("{}/share/libc++/v1/std.cppm", config.ResolvedSysroot(GetToolchain())); } else if (config.sysroot.empty()) { stdCppm = GetToolchain().stdCppm.string(); } else { @@ -968,7 +968,7 @@ std::string Crafter::BuildStdPcm(const Configuration& config, fs::path stdPcm) { // mtimes: an installed std.cppm can be dated at the epoch (OSTree), // and a libc++ update under the same clang changes the bytes but // not the path. - std::string command = std::format("{}{} --precompile {} -o {}", GetBaseCommand(config), StdPcmFlags(config), stdCppm, stdPcm.string()); + std::string command = std::format("{}{} --precompile {} -o {}", GetBaseCommand(config), StdPcmFlags(config, GetToolchain()), stdCppm, stdPcm.string()); std::string stamp = HashStamp(command + '\0' + FileBytes(stdCppm)); if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return ""; std::string result = RunBuildCommand(command); @@ -1003,7 +1003,7 @@ std::string Crafter::GetBaseCommand(const Configuration& config) { command += " -stdlib=libc++"; // A sysroot build (wasm, a cross rootfs) takes libc++ from the sysroot; // only host builds can be pointed at an explicit libc++ install. - if (config.sysroot.empty()) command += tc.LibcxxCompileFlags(); + if (config.ResolvedSysroot(tc).empty()) command += tc.LibcxxCompileFlags(); return command; } diff --git a/interfaces/Crafter.Build-Clang.cppm b/interfaces/Crafter.Build-Clang.cppm index c253079..0ae6ab9 100644 --- a/interfaces/Crafter.Build-Clang.cppm +++ b/interfaces/Crafter.Build-Clang.cppm @@ -662,7 +662,7 @@ export namespace Crafter { compileKey += '|'; compileKey += debug ? '1' : '0'; compileKey += '|'; - compileKey += sysroot; + compileKey += ResolvedSysroot(toolchain); for (const Define& d : defines) { compileKey += "|D:"; compileKey += d.name; @@ -748,6 +748,25 @@ export namespace Crafter { Packaging ResolvedPackaging() const { return type == ConfigurationType::Executable && packaging == Packaging::Flatpak ? Packaging::Flatpak : Packaging::None; } + // What `sysroot` comes to for this configuration under `toolchain`: + // the toolchain's WASI sysroot for a wasm32 target that names none, + // `sysroot` as it stands for every other configuration. The + // commands that compile and link a wasm32 configuration and the + // names of its directories read the sysroot through here, so the + // configuration is the same thing before its build as during it and + // after. Build() used to store the WASI sysroot in `sysroot` on its + // way in instead, and VariantId hashes the sysroot: a wasm32 + // configuration whose target was set without ApplyStandardArgs, + // which stores it when the project file is loaded, had one + // BuildDir, BinDir and PcmDir until its build started and others + // from then on. A build pass records a dependency's build under its + // PcmDir, so one that two configurations of the pass were built on + // was looked up under the first directory by the one that asked + // before its build had started and under the second by the one + // that asked after, and was built a second time. + std::string ResolvedSysroot(const Toolchain& toolchain) const { + return sysroot.empty() && target.starts_with("wasm32") ? toolchain.wasiSysroot.string() : sysroot; + } fs::path PcmDir() const { return type == ConfigurationType::Executable ? BuildDir() : BinDir(); } @@ -930,9 +949,9 @@ namespace Crafter { // compile command and of the std module's, which have to agree flag for // flag. std::string WasmCompileFlags(const Configuration& config); - // The flags BuildStdPcm compiles the std module with, after the base - // command and before the source, each with a leading space. StdPcmKey - // hashes the same string, so the command and the directory it writes to - // cannot disagree about what decides the BMI. - std::string StdPcmFlags(const Configuration& config); + // The flags BuildStdPcm compiles the std module with under `toolchain`, + // after the base command and before the source, each with a leading + // space. StdPcmKey hashes the same string, so the command and the + // directory it writes to cannot disagree about what decides the BMI. + std::string StdPcmFlags(const Configuration& config, const Toolchain& toolchain); }