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

@ -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);
}