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.
This commit is contained in:
Jorijn van der Graaf 2026-11-14 23:42:47 +01:00
commit 1ddba745cf
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 51 additions and 39 deletions

View file

@ -249,14 +249,15 @@ void Configuration::GetInterfacesAndImplementations(std::span<fs::path> 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 (<install>/../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 (<install>/../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<fs::path, s
return {std::move(cycle), false, {}};
}
// Auto-detect the WASI sysroot before any compile step runs so BuildStdPcm
// and the main compile command see the same value. Covers all wasm32-*
// triples (wasi, wasip1, wasip2, ...); the sysroot's per-triple subdirs
// handle the differences.
if (config.sysroot.empty() && config.target.starts_with("wasm32")) {
config.sysroot = GetToolchain().wasiSysroot.string();
}
// As early as the duplicate-module check, so a refused build leaves no
// directory behind and starts no thread, but after the sysroot is known.
// A Flatpak asked of a target that cannot be one says so before anything
// the target itself would refuse: that is the request to change.
// directory behind and starts no thread. A Flatpak asked of a target that
// cannot be one says so before anything the target itself would refuse:
// that is the request to change.
if (std::string refused = FlatpakBuildError(config); !refused.empty()) {
return {std::move(refused), false, {}};
}
@ -1151,8 +1145,8 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
std::string cArchFlags = isWasm
? std::string()
: std::format(" -march={} -mtune={}", config.march, config.mtune);
if (!config.sysroot.empty()) {
cArchFlags += std::format(" --sysroot={}", config.sysroot);
if (const std::string resolved = config.ResolvedSysroot(GetToolchain()); !resolved.empty()) {
cArchFlags += std::format(" --sysroot={}", resolved);
} else if (config.target == "x86_64-w64-mingw32") {
// The C++ side gets it through GetBaseCommand.
if (const fs::path& sysroot = MingwSysroot(); !sysroot.empty()) cArchFlags += std::format(" --sysroot={}", sysroot.string());
@ -1407,7 +1401,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// through the run path above instead, as it does anything else this -L
// lets the link take from that directory. mingw links libstdc++ and
// every sysroot build (wasm included) brings its own libc++.
if (config.target != "x86_64-w64-mingw32" && config.sysroot.empty()) {
if (config.target != "x86_64-w64-mingw32" && config.ResolvedSysroot(GetToolchain()).empty()) {
linkExtras += flatpak ? GetToolchain().LibcxxSearchFlags() : GetToolchain().LibcxxLinkFlags();
}
// mingw uses libstdc++; C++26 std::print/format extras live in libstdc++exp.
@ -2081,13 +2075,12 @@ ArgQuery Crafter::ApplyStandardArgs(Configuration& cfg, std::span<const std::str
cfg.projectArgs.erase(std::ranges::unique(cfg.projectArgs).begin(), cfg.projectArgs.end());
if (sawLib && cfg.type == ConfigurationType::Executable) cfg.type = ConfigurationType::LibraryStatic;
if (sawShared && cfg.type == ConfigurationType::LibraryStatic) cfg.type = ConfigurationType::LibraryDynamic;
// WASI sysroot autodetect, applied at config-load time so the VariantId
// includes it. (Build() also runs this once more for callers that bypassed
// ApplyStandardArgs, but doing it here makes dep PcmDirs consistent
// between the consumer's command-construction and the dep's own build.)
if (cfg.sysroot.empty() && cfg.target.starts_with("wasm32")) {
cfg.sysroot = GetToolchain().wasiSysroot.string();
}
// The WASI sysroot a wasm32 configuration is built against when it
// names none, stored where the project file can read it and hand it to
// other configurations it makes. No build depends on it being stored: a
// configuration that never came through here resolves to the same
// directory (Configuration::ResolvedSysroot).
cfg.sysroot = cfg.ResolvedSysroot(GetToolchain());
return ArgQuery{args};
}

View file

@ -934,7 +934,7 @@ std::string Crafter::BuildStdPcm(const Configuration& config, fs::path stdPcm) {
// The std.cc → std.cppm copy uses a plain filename: held under the
// cache lock, no other builder writes this path concurrently.
fs::path stdCppm = stdPcm.parent_path() / "std.cppm";
std::string command = std::format("{}{} --precompile {} -o {}", GetBaseCommand(config), StdPcmFlags(config), stdCppm.string(), stdPcm.string());
std::string command = std::format("{}{} --precompile {} -o {}", GetBaseCommand(config), StdPcmFlags(config, GetToolchain()), stdCppm.string(), stdPcm.string());
std::string stamp = HashStamp(command + '\0' + FileBytes(stdCc));
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
std::error_code copyEc;
@ -952,7 +952,7 @@ std::string Crafter::BuildStdPcm(const Configuration& config, fs::path stdPcm) {
// <sysroot>/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;
}