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:
parent
1440e20231
commit
1ddba745cf
3 changed files with 51 additions and 39 deletions
|
|
@ -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") {
|
if (config.target == "x86_64-w64-mingw32") {
|
||||||
// -femulated-tls keeps PCM TLS access matching libstdc++'s emutls
|
// -femulated-tls keeps PCM TLS access matching libstdc++'s emutls
|
||||||
// definitions; a mismatch surfaces as undefined std::__once_callable.
|
// 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);
|
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 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
|
// 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.
|
// the units importing it are, a variant pass's flags (e.g.
|
||||||
// -mrelaxed-simd) included, or the BMI is not feature-compatible.
|
// -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
|
// libc++ headers (<install>/../include/c++/v1) above --sysroot, so the
|
||||||
// PCM would mix host libc++ with the foreign libc. Force the sysroot's
|
// PCM would mix host libc++ with the foreign libc. Force the sysroot's
|
||||||
// copy.
|
// copy.
|
||||||
if (!isWasm && !config.sysroot.empty()) {
|
if (!isWasm && !sysroot.empty()) {
|
||||||
archFlags += std::format(" -nostdinc++ -isystem {}/usr/include/c++/v1", config.sysroot);
|
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);
|
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
|
// 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
|
// the PCMs built against the previous one, which clang crashes on rather
|
||||||
// than rejecting.
|
// 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
|
// 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
|
// 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
|
// 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);
|
: 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());
|
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()) {
|
if (const std::string sysroot = config.ResolvedSysroot(GetToolchain()); !sysroot.empty()) {
|
||||||
out.command += std::format(" --sysroot={}", config.sysroot);
|
out.command += std::format(" --sysroot={}", sysroot);
|
||||||
// Same reason as in BuildStdPcm: the driver ranks the host
|
// Same reason as in BuildStdPcm: the driver ranks the host
|
||||||
// toolchain's own libc++ headers (<install>/../include/c++/v1) above
|
// toolchain's own libc++ headers (<install>/../include/c++/v1) above
|
||||||
// --sysroot. A unit that only does `import std;` never notices, but
|
// --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
|
// option in compileFlags) on sysroot builds. Not for mingw, which
|
||||||
// uses libstdc++ and has no libc++ in its sysroot.
|
// uses libstdc++ and has no libc++ in its sysroot.
|
||||||
if (!isWasm && config.target != "x86_64-w64-mingw32") {
|
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) {
|
if (isWasm) {
|
||||||
|
|
@ -627,7 +628,7 @@ namespace {
|
||||||
if (config.type == ConfigurationType::LibraryDynamic) {
|
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);
|
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)) {
|
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());
|
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, {}};
|
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
|
// 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.
|
// directory behind and starts no thread. A Flatpak asked of a target that
|
||||||
// A Flatpak asked of a target that cannot be one says so before anything
|
// cannot be one says so before anything the target itself would refuse:
|
||||||
// the target itself would refuse: that is the request to change.
|
// that is the request to change.
|
||||||
if (std::string refused = FlatpakBuildError(config); !refused.empty()) {
|
if (std::string refused = FlatpakBuildError(config); !refused.empty()) {
|
||||||
return {std::move(refused), false, {}};
|
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 cArchFlags = isWasm
|
||||||
? std::string()
|
? std::string()
|
||||||
: std::format(" -march={} -mtune={}", config.march, config.mtune);
|
: std::format(" -march={} -mtune={}", config.march, config.mtune);
|
||||||
if (!config.sysroot.empty()) {
|
if (const std::string resolved = config.ResolvedSysroot(GetToolchain()); !resolved.empty()) {
|
||||||
cArchFlags += std::format(" --sysroot={}", config.sysroot);
|
cArchFlags += std::format(" --sysroot={}", resolved);
|
||||||
} else if (config.target == "x86_64-w64-mingw32") {
|
} else if (config.target == "x86_64-w64-mingw32") {
|
||||||
// The C++ side gets it through GetBaseCommand.
|
// The C++ side gets it through GetBaseCommand.
|
||||||
if (const fs::path& sysroot = MingwSysroot(); !sysroot.empty()) cArchFlags += std::format(" --sysroot={}", sysroot.string());
|
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
|
// through the run path above instead, as it does anything else this -L
|
||||||
// lets the link take from that directory. mingw links libstdc++ and
|
// lets the link take from that directory. mingw links libstdc++ and
|
||||||
// every sysroot build (wasm included) brings its own libc++.
|
// 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();
|
linkExtras += flatpak ? GetToolchain().LibcxxSearchFlags() : GetToolchain().LibcxxLinkFlags();
|
||||||
}
|
}
|
||||||
// mingw uses libstdc++; C++26 std::print/format extras live in libstdc++exp.
|
// 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());
|
cfg.projectArgs.erase(std::ranges::unique(cfg.projectArgs).begin(), cfg.projectArgs.end());
|
||||||
if (sawLib && cfg.type == ConfigurationType::Executable) cfg.type = ConfigurationType::LibraryStatic;
|
if (sawLib && cfg.type == ConfigurationType::Executable) cfg.type = ConfigurationType::LibraryStatic;
|
||||||
if (sawShared && cfg.type == ConfigurationType::LibraryStatic) cfg.type = ConfigurationType::LibraryDynamic;
|
if (sawShared && cfg.type == ConfigurationType::LibraryStatic) cfg.type = ConfigurationType::LibraryDynamic;
|
||||||
// WASI sysroot autodetect, applied at config-load time so the VariantId
|
// The WASI sysroot a wasm32 configuration is built against when it
|
||||||
// includes it. (Build() also runs this once more for callers that bypassed
|
// names none, stored where the project file can read it and hand it to
|
||||||
// ApplyStandardArgs, but doing it here makes dep PcmDirs consistent
|
// other configurations it makes. No build depends on it being stored: a
|
||||||
// between the consumer's command-construction and the dep's own build.)
|
// configuration that never came through here resolves to the same
|
||||||
if (cfg.sysroot.empty() && cfg.target.starts_with("wasm32")) {
|
// directory (Configuration::ResolvedSysroot).
|
||||||
cfg.sysroot = GetToolchain().wasiSysroot.string();
|
cfg.sysroot = cfg.ResolvedSysroot(GetToolchain());
|
||||||
}
|
|
||||||
return ArgQuery{args};
|
return ArgQuery{args};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
// The std.cc → std.cppm copy uses a plain filename: held under the
|
||||||
// cache lock, no other builder writes this path concurrently.
|
// cache lock, no other builder writes this path concurrently.
|
||||||
fs::path stdCppm = stdPcm.parent_path() / "std.cppm";
|
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));
|
std::string stamp = HashStamp(command + '\0' + FileBytes(stdCc));
|
||||||
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
|
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
|
||||||
std::error_code copyEc;
|
std::error_code copyEc;
|
||||||
|
|
@ -952,7 +952,7 @@ std::string Crafter::BuildStdPcm(const Configuration& config, fs::path stdPcm) {
|
||||||
// <sysroot>/usr/share/libc++/v1/.
|
// <sysroot>/usr/share/libc++/v1/.
|
||||||
std::string stdCppm;
|
std::string stdCppm;
|
||||||
if (isWasm) {
|
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()) {
|
} else if (config.sysroot.empty()) {
|
||||||
stdCppm = GetToolchain().stdCppm.string();
|
stdCppm = GetToolchain().stdCppm.string();
|
||||||
} else {
|
} 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),
|
// mtimes: an installed std.cppm can be dated at the epoch (OSTree),
|
||||||
// and a libc++ update under the same clang changes the bytes but
|
// and a libc++ update under the same clang changes the bytes but
|
||||||
// not the path.
|
// 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));
|
std::string stamp = HashStamp(command + '\0' + FileBytes(stdCppm));
|
||||||
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
|
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
|
||||||
std::string result = RunBuildCommand(command);
|
std::string result = RunBuildCommand(command);
|
||||||
|
|
@ -1003,7 +1003,7 @@ std::string Crafter::GetBaseCommand(const Configuration& config) {
|
||||||
command += " -stdlib=libc++";
|
command += " -stdlib=libc++";
|
||||||
// A sysroot build (wasm, a cross rootfs) takes libc++ from the sysroot;
|
// A sysroot build (wasm, a cross rootfs) takes libc++ from the sysroot;
|
||||||
// only host builds can be pointed at an explicit libc++ install.
|
// 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;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -662,7 +662,7 @@ export namespace Crafter {
|
||||||
compileKey += '|';
|
compileKey += '|';
|
||||||
compileKey += debug ? '1' : '0';
|
compileKey += debug ? '1' : '0';
|
||||||
compileKey += '|';
|
compileKey += '|';
|
||||||
compileKey += sysroot;
|
compileKey += ResolvedSysroot(toolchain);
|
||||||
for (const Define& d : defines) {
|
for (const Define& d : defines) {
|
||||||
compileKey += "|D:";
|
compileKey += "|D:";
|
||||||
compileKey += d.name;
|
compileKey += d.name;
|
||||||
|
|
@ -748,6 +748,25 @@ export namespace Crafter {
|
||||||
Packaging ResolvedPackaging() const {
|
Packaging ResolvedPackaging() const {
|
||||||
return type == ConfigurationType::Executable && packaging == Packaging::Flatpak ? Packaging::Flatpak : Packaging::None;
|
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 {
|
fs::path PcmDir() const {
|
||||||
return type == ConfigurationType::Executable ? BuildDir() : BinDir();
|
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
|
// compile command and of the std module's, which have to agree flag for
|
||||||
// flag.
|
// flag.
|
||||||
std::string WasmCompileFlags(const Configuration& config);
|
std::string WasmCompileFlags(const Configuration& config);
|
||||||
// The flags BuildStdPcm compiles the std module with, after the base
|
// The flags BuildStdPcm compiles the std module with under `toolchain`,
|
||||||
// command and before the source, each with a leading space. StdPcmKey
|
// after the base command and before the source, each with a leading
|
||||||
// hashes the same string, so the command and the directory it writes to
|
// space. StdPcmKey hashes the same string, so the command and the
|
||||||
// cannot disagree about what decides the BMI.
|
// directory it writes to cannot disagree about what decides the BMI.
|
||||||
std::string StdPcmFlags(const Configuration& config);
|
std::string StdPcmFlags(const Configuration& config, const Toolchain& toolchain);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue