test(wasm): a configuration is the same with its WASI sysroot named or not

WasmSysrootKey makes two wasm32 configurations that differ in one thing:
one names no sysroot and the other names the toolchain's WASI sysroot.
They have to be one configuration: the same VariantId, PcmDir and
BuildDir, the same std module key, and the same compile command, which
names the WASI sysroot. A third, with a sysroot of its own, has another
VariantId.

It then builds two programs for the host in one pass, each on the same
wasm32 library, which names no sysroot. The library carries
-fno-exceptions, which a wasm build is refused for before it makes a
directory, so the case does not depend on the machine having a WASI
sysroot. The library's PcmDir has to be the same after the first
program's build as before it, and the pass has to hold the library
once.

Against the engine as it was before the fix the test fails on each of
the three: the two configurations have different VariantIds, the
library's PcmDir changes with the first build, and the pass holds the
library twice.
This commit is contained in:
Jorijn van der Graaf 2026-11-14 23:42:47 +01:00
commit a7aa22220b
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 109 additions and 0 deletions

View file

@ -308,6 +308,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
cfg.AddTest("WasmExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmExceptions").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasiSysrootWithoutExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasiSysrootWithoutExceptions").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmSysrootKey").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("RunSingleTestExit").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("RunSingleTestExit").Dependencies({ CrafterBuildLib.get() });
// LoadProject dlopens the synthesized project.so, which references // LoadProject dlopens the synthesized project.so, which references
// Crafter:: symbols (HostTarget, Configuration ctors) that have to be // Crafter:: symbols (HostTarget, Configuration ctors) that have to be

View file

@ -0,0 +1,108 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// A wasm32 configuration that names no sysroot is built against the
// toolchain's WASI sysroot, and it is the same configuration whether its
// `sysroot` says so or not: the same directories, the same compile command.
//
// Build() used to store the WASI sysroot in the configuration as it started
// building it, and the sysroot is part of what names a configuration's
// directories. A wasm32 configuration whose project file set its target
// without ApplyStandardArgs, which stores the sysroot at load time, so had
// one PcmDir before its build and another once it had started. A build pass
// records a dependency's build under its PcmDir: a library that two programs
// of one pass were built on was recorded under the first directory for the
// program that asked first, not found under the second by the program that
// asked next, and built again.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
// A wasm32 library without sources that names no sysroot.
Configuration MakeLibrary(const fs::path& dir, std::string_view name) {
Configuration cfg;
cfg.path = dir;
cfg.name = name;
cfg.outputName = name;
cfg.target = "wasm32-wasip1";
cfg.type = ConfigurationType::LibraryStatic;
return cfg;
}
// A program for the host without sources, built on `library`.
Configuration MakeProgram(const fs::path& dir, std::string_view name, Configuration& library) {
Configuration cfg;
cfg.path = dir;
cfg.name = name;
cfg.outputName = name;
cfg.target = HostTarget();
cfg.type = ConfigurationType::Executable;
cfg.dependencies = { &library };
return cfg;
}
}
int main() {
const fs::path scratch = fs::temp_directory_path() / "crafter-build-wasm-sysroot-key";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::create_directories(scratch);
const Toolchain& toolchain = GetToolchain();
// Named or not, the WASI sysroot makes one configuration.
{
const Configuration unnamed = MakeLibrary(scratch, "keyed");
Configuration named = MakeLibrary(scratch, "keyed");
named.sysroot = toolchain.wasiSysroot.string();
Check(unnamed.VariantId() == named.VariantId(), std::format("a wasm32 configuration that names no sysroot is the one that names the toolchain's WASI sysroot ({} and {})", unnamed.VariantId(), named.VariantId()));
Check(unnamed.PcmDir() == named.PcmDir() && unnamed.BuildDir() == named.BuildDir(), "and is built into the same directories");
Check(StdPcmKey(unnamed, toolchain) == StdPcmKey(named, toolchain), "against the same std module");
Check(GetCompileCommand(unnamed).command == GetCompileCommand(named).command, std::format("with the same compile command:\n{}\n{}", GetCompileCommand(unnamed).command, GetCompileCommand(named).command));
Check(GetCompileCommand(unnamed).command.contains(std::format("--sysroot={}", toolchain.wasiSysroot.string())), "which names the WASI sysroot");
Configuration other = MakeLibrary(scratch, "keyed");
other.sysroot = (scratch / "another-sysroot").string();
Check(other.VariantId() != unnamed.VariantId(), "a wasm32 configuration with a sysroot of its own is still another configuration");
}
// Building it leaves it where it was, so a pass that two programs ask
// for it in builds it once. The library has a flag no wasm build
// accepts: its build is refused on every machine, with or without a
// WASI sysroot, before it makes a directory, and a refused build is
// recorded in the pass like any other.
{
Configuration library = MakeLibrary(scratch, "shared");
library.compileFlags.push_back("-fno-exceptions");
Configuration first = MakeProgram(scratch, "first", library);
Configuration second = MakeProgram(scratch, "second", library);
const fs::path before = library.PcmDir();
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const BuildResult one = Build(first, depResults, depMutex);
Check(one.result.contains("-fno-exceptions"), std::format("the first program's build asks for the library, whose build is refused: {}", one.result));
Check(library.PcmDir() == before, std::format("a build leaves a configuration's directories where they were ({} before, {} after)", before.string(), library.PcmDir().string()));
const BuildResult two = Build(second, depResults, depMutex);
Check(two.result == one.result, "the second program gets the same answer");
const auto builds = std::ranges::count_if(depResults, [](const auto& entry) { return entry.first.filename().string().starts_with("shared-wasm32"); });
Check(builds == 1, std::format("one pass builds the library once for both programs (recorded {} times)", builds));
}
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}