diff --git a/project.cpp b/project.cpp index 2aceae7..1452eb6 100644 --- a/project.cpp +++ b/project.cpp @@ -308,6 +308,7 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("WasmExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasiSysrootWithoutExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("WasmSysrootKey").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("RunSingleTestExit").Dependencies({ CrafterBuildLib.get() }); // LoadProject dlopens the synthesized project.so, which references // Crafter:: symbols (HostTarget, Configuration ctors) that have to be diff --git a/tests/WasmSysrootKey/main.cpp b/tests/WasmSysrootKey/main.cpp new file mode 100644 index 0000000..afe6abc --- /dev/null +++ b/tests/WasmSysrootKey/main.cpp @@ -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> 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; +}