nvcc ran with -arch=sm_89 for every build, which is code for that one architecture and its PTX, whatever GPU the program was for. Jorijn asked for the architecture to be configurable, and native when nothing sets it, as the CPU's is. cfg.cudaArch is nvcc's -arch now, and it defaults to "native": nvcc compiles for the GPUs it sees, and where it sees none it warns and uses its own default architecture. Like -march=native it is for a program that runs where it is built, since it carries no PTX, and `crafter-build bundle` warns of a bundle whose CUDA code, in the program or a library it is built on, is compiled for native, as it warns of -march=native. ApplyStandardArgs sets it from --cuda-arch= and CRAFTER_BUILD_CUDA_ARCH as it sets march from --march= and CRAFTER_BUILD_MARCH; AddTest and AddMarchVariants give their tests the parent's, as they give them its sysroot and debug flag; and ForceHostTooling gives host tooling the host's, as it gives it the host's march, since project.so runs it on the machine that loads it. A .cu is compiled again only when it is newer than its object, so the architecture keys VariantId, and so does the nvcc that compiles it, by what it is rather than where: Toolchain::nvccStamp, its canonical path, size and time, read without a process, changes when CUDA_PATH names another toolkit and when an update replaces the one at the same path, as a package does at /opt/cuda. native is keyed as well, by Toolchain::cudaNativeKey: the NVIDIA display devices sysfs lists, by PCI address and device ID, and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER, which choose and number them, each empty value apart from an unset one, read without a process. It is crafter-build's reading of the machine, not nvcc's. The word alone would keep an object compiled for a GPU the machine no longer has, after a GPU change or on another machine that shares the checkout, and that object carries no PTX for the GPU it has now, where sm_89's carried its PTX. Only a configuration with CUDA sources is keyed, so every other one keeps the directory it had, but for one built with --cuda-arch=, which was a project argument, hashed as one, and is crafter-build's own now.
296 lines
15 KiB
C++
296 lines
15 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
import std;
|
|
import Crafter.Build;
|
|
using namespace Crafter;
|
|
|
|
namespace {
|
|
std::int32_t Failures = 0;
|
|
|
|
void Check(bool cond, std::string_view msg) {
|
|
if (!cond) {
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
|
++Failures;
|
|
}
|
|
}
|
|
|
|
Configuration MakeBase() {
|
|
Configuration cfg;
|
|
cfg.path = "/tmp/variant-id-test";
|
|
cfg.name = "vt";
|
|
cfg.outputName = "vt";
|
|
cfg.target = "x86_64-pc-linux-gnu";
|
|
cfg.march = "native";
|
|
cfg.mtune = "native";
|
|
cfg.type = ConfigurationType::Executable;
|
|
return cfg;
|
|
}
|
|
}
|
|
|
|
// VariantId is the cache key for build outputs. Two Configurations that
|
|
// differ only in something that affects codegen must produce different
|
|
// VariantId / BuildDir / BinDir, otherwise their .o files collide.
|
|
int main() {
|
|
{
|
|
// Baseline against itself: VariantId is deterministic.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
Check(a.VariantId() == b.VariantId(), "identical configs have identical VariantId");
|
|
Check(a.BuildDir() == b.BuildDir(), "identical configs have identical BuildDir");
|
|
}
|
|
{
|
|
// type differs -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.type = ConfigurationType::LibraryStatic;
|
|
Check(a.VariantId() != b.VariantId(), "type change perturbs VariantId");
|
|
}
|
|
{
|
|
// A Flatpak lays its bin dir out unlike the executable as linked, so
|
|
// the two get directories of their own. Only a Flatpak is keyed:
|
|
// Default and None, and a library whatever it asks for, keep the
|
|
// directory they had, and what the app is installed as changes
|
|
// files/share and what install and bundle make of it, never what is
|
|
// compiled.
|
|
Configuration plain = MakeBase();
|
|
Configuration none = MakeBase();
|
|
none.packaging = Packaging::None;
|
|
Configuration flatpak = MakeBase();
|
|
flatpak.packaging = Packaging::Flatpak;
|
|
Configuration described = MakeBase();
|
|
described.packaging = Packaging::Flatpak;
|
|
described.flatpak.appId = "net.example.Described";
|
|
described.flatpak.desktopFile = "app.desktop";
|
|
described.flatpak.icons = {"icon.svg"};
|
|
described.flatpak.runtime = FlatpakRuntime::Sdk;
|
|
described.flatpak.permissions = {"--socket=wayland"};
|
|
described.flatpak.modules = {"packaging/extra.json"};
|
|
described.flatpak.share = {{"data", "app/data"}};
|
|
Configuration library = MakeBase();
|
|
library.type = ConfigurationType::LibraryStatic;
|
|
Configuration flatpakLibrary = MakeBase();
|
|
flatpakLibrary.type = ConfigurationType::LibraryStatic;
|
|
flatpakLibrary.packaging = Packaging::Flatpak;
|
|
Check(plain.VariantId() == none.VariantId(), "Default and None share a directory");
|
|
Check(plain.VariantId() != flatpak.VariantId(), "a Flatpak gets a directory of its own");
|
|
Check(flatpak.VariantId() == described.VariantId(), "the app-id, desktop entry, icons, runtime, permissions, modules and share entries do not move the directory");
|
|
Check(library.VariantId() == flatpakLibrary.VariantId(), "a library ignores packaging");
|
|
Check(flatpak.ProgramDir() == flatpak.BinDir() / "files" / "bin" && plain.ProgramDir() == plain.BinDir(), "a Flatpak's program lives in files/bin, everything else's in the bin dir");
|
|
}
|
|
{
|
|
// debug differs -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.debug = true;
|
|
Check(a.VariantId() != b.VariantId(), "debug change perturbs VariantId");
|
|
}
|
|
{
|
|
// sysroot differs -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.sysroot = "/opt/sysroot";
|
|
Check(a.VariantId() != b.VariantId(), "sysroot change perturbs VariantId");
|
|
}
|
|
{
|
|
// defines differ -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.defines.push_back({"FOO", "1"});
|
|
Check(a.VariantId() != b.VariantId(), "define added perturbs VariantId");
|
|
}
|
|
{
|
|
// Same define name, different value -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
a.defines.push_back({"FOO", "1"});
|
|
Configuration b = MakeBase();
|
|
b.defines.push_back({"FOO", "2"});
|
|
Check(a.VariantId() != b.VariantId(), "define value change perturbs VariantId");
|
|
}
|
|
{
|
|
// compileFlags differ -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.compileFlags.push_back("-fno-omit-frame-pointer");
|
|
Check(a.VariantId() != b.VariantId(), "compile flag added perturbs VariantId");
|
|
}
|
|
{
|
|
// target differs -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.target = "aarch64-linux-gnu";
|
|
Check(a.VariantId() != b.VariantId(), "target change perturbs VariantId");
|
|
}
|
|
{
|
|
// march differs -> VariantId differs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.march = "x86-64-v3";
|
|
Check(a.VariantId() != b.VariantId(), "march change perturbs VariantId");
|
|
}
|
|
{
|
|
// The GPU architecture decides what a .cu compiles into, and it need
|
|
// not touch the files whose times decide whether a .cu is compiled
|
|
// again, so it keys the directory of a configuration with CUDA
|
|
// sources, and only of one.
|
|
Configuration a = MakeBase();
|
|
a.cuda = { "/tmp/variant-id-test/kernel" };
|
|
Configuration b = MakeBase();
|
|
b.cuda = a.cuda;
|
|
b.cudaArch = "sm_89";
|
|
Check(a.VariantId() != b.VariantId(), "cudaArch change perturbs the VariantId of a configuration with CUDA sources");
|
|
Configuration c = MakeBase();
|
|
Configuration d = MakeBase();
|
|
d.cudaArch = "sm_89";
|
|
Check(c.VariantId() == d.VariantId(), "without CUDA sources, cudaArch leaves VariantId alone");
|
|
// So does the nvcc that compiles them, by what it is: CUDA_PATH can
|
|
// name another toolkit, and an update can replace one at its path,
|
|
// and the old one's objects must not pass for the new one's.
|
|
const Toolchain one = GetToolchain();
|
|
Toolchain other = one;
|
|
other.nvccStamp = "/opt/other-cuda/bin/nvcc|1|1";
|
|
Toolchain updated = other;
|
|
updated.nvccStamp = "/opt/other-cuda/bin/nvcc|2|2";
|
|
Check(a.VariantId(one) != a.VariantId(other), "another nvcc perturbs the VariantId of a configuration with CUDA sources");
|
|
Check(a.VariantId(other) != a.VariantId(updated), "so does an nvcc updated at the same path");
|
|
Check(c.VariantId(one) == c.VariantId(other), "without CUDA sources, the nvcc leaves VariantId alone");
|
|
// native is what nvcc finds on the machine, so the GPUs it finds key
|
|
// it as well, and only it.
|
|
Toolchain first = one;
|
|
first.cudaNativeKey = "0x2684|";
|
|
Toolchain swapped = one;
|
|
swapped.cudaNativeKey = "0x2b85|";
|
|
Check(a.VariantId(first) != a.VariantId(swapped), "another GPU perturbs the VariantId of a configuration with CUDA sources for native");
|
|
Check(b.VariantId(first) == b.VariantId(swapped), "but not for a named architecture");
|
|
Check(c.VariantId(first) == c.VariantId(swapped), "nor without CUDA sources");
|
|
}
|
|
{
|
|
// A project flag the framework can't interpret still decides what gets
|
|
// compiled and bundled, so it has to key into VariantId. Otherwise both
|
|
// settings share one bin dir and interleave their outputs there — an
|
|
// index.html from one alongside a script from the other.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.projectArgs.push_back("--no-webgpu");
|
|
Check(a.VariantId() != b.VariantId(), "project arg perturbs VariantId");
|
|
Check(a.BinDir() != b.BinDir(), "project arg yields a distinct BinDir");
|
|
}
|
|
{
|
|
// Distinct project args must not collide with each other either.
|
|
Configuration a = MakeBase();
|
|
a.projectArgs.push_back("--no-webgpu");
|
|
Configuration b = MakeBase();
|
|
b.projectArgs.push_back("--no-audio");
|
|
Check(a.VariantId() != b.VariantId(), "different project args yield different VariantId");
|
|
}
|
|
{
|
|
// PcmDir() differs between Executable (in BuildDir) and Library
|
|
// (in BinDir) — Library PCMs land in the installable bin dir so
|
|
// downstream consumers can find them.
|
|
Configuration exe = MakeBase();
|
|
Configuration lib = MakeBase();
|
|
lib.type = ConfigurationType::LibraryStatic;
|
|
Check(exe.PcmDir() == exe.BuildDir(), "Executable PcmDir == BuildDir");
|
|
Check(lib.PcmDir() == lib.BinDir(), "Library PcmDir == BinDir");
|
|
}
|
|
{
|
|
// VariantId is embedded in the path so distinct ids produce distinct dirs.
|
|
Configuration a = MakeBase();
|
|
Configuration b = MakeBase();
|
|
b.debug = true;
|
|
Check(a.BuildDir() != b.BuildDir(), "different VariantId yields different BuildDir");
|
|
Check(a.BinDir() != b.BinDir(), "different VariantId yields different BinDir");
|
|
}
|
|
{
|
|
// The compiler is compile state as much as any flag: a PCM can only
|
|
// be read by the clang release that wrote it, and nothing in a
|
|
// project's sources changes when the compiler does. With the
|
|
// toolchain left out of the id, a project built with clang 22 kept
|
|
// its PCMs after crafter-build moved to clang 23; the build after
|
|
// the update found nothing stale, and the first edit failed with
|
|
// "module file ... uses an older format that is no longer supported"
|
|
// until the project was cleaned by hand.
|
|
Configuration cfg = MakeBase();
|
|
Toolchain older;
|
|
older.clangxx = "/app/bin/clang++";
|
|
older.clangVersion = "clang version 22.1.8";
|
|
Toolchain newer = older;
|
|
newer.clangVersion = "clang version 23.1.2";
|
|
Check(cfg.VariantId(older) != cfg.VariantId(newer), "a compiler update at the same path perturbs VariantId");
|
|
Check(cfg.VariantId(older) == cfg.VariantId(older), "VariantId under one toolchain is deterministic");
|
|
|
|
// Two installs of one release are still two toolchains: their
|
|
// libc++ need not be the same build.
|
|
Toolchain elsewhere = older;
|
|
elsewhere.clangxx = "/usr/bin/clang++";
|
|
Check(cfg.VariantId(older) != cfg.VariantId(elsewhere), "the same release installed elsewhere perturbs VariantId");
|
|
|
|
Check(cfg.VariantId() == cfg.VariantId(GetToolchain()), "VariantId() is the variant under the running toolchain");
|
|
Check(cfg.BuildDir().filename() == cfg.VariantId(GetToolchain()), "BuildDir is named for the running toolchain's variant");
|
|
}
|
|
{
|
|
// The mingw-w64 prefix decides what a mingw build compiles against.
|
|
// Before issue #43 it was clang's sysroot as is, which put the host's
|
|
// headers on the include path, and with the prefix left out of the
|
|
// id, the release that fixed that reused the objects built against
|
|
// them: the next build only relinked, and failed to link as before.
|
|
Configuration mingw = MakeBase();
|
|
mingw.target = "x86_64-w64-mingw32";
|
|
Toolchain discovered;
|
|
discovered.clangxx = "/app/bin/clang++";
|
|
Toolchain hostUsr = discovered;
|
|
hostUsr.mingwPrefix = "/run/host/usr";
|
|
Toolchain optMingw = discovered;
|
|
optMingw.mingwPrefix = "/opt/mingw";
|
|
Check(mingw.VariantId(discovered) != mingw.VariantId(hostUsr), "a mingw prefix perturbs a mingw configuration's VariantId");
|
|
Check(mingw.VariantId(hostUsr) != mingw.VariantId(optMingw), "two mingw prefixes are two variants");
|
|
Configuration host = MakeBase();
|
|
Check(host.VariantId(discovered) == host.VariantId(hostUsr), "the mingw prefix leaves other targets' variants alone");
|
|
|
|
// The std module's directory is keyed the same way, and so is a
|
|
// prefix given whole against one reached through a view.
|
|
Check(StdPcmKey(mingw, discovered) != StdPcmKey(mingw, hostUsr), "a mingw prefix moves the mingw std module");
|
|
Check(StdPcmKey(mingw, hostUsr) != StdPcmKey(mingw, optMingw), "two mingw prefixes are two std modules");
|
|
Toolchain hostUsrWhole = hostUsr;
|
|
hostUsrWhole.mingwPrefixIsSystem = !hostUsr.mingwPrefixIsSystem;
|
|
Check(StdPcmKey(mingw, hostUsr) != StdPcmKey(mingw, hostUsrWhole), "a prefix given whole and one shown through a view are two std modules");
|
|
Check(mingw.VariantId(hostUsr) != mingw.VariantId(hostUsrWhole), "and two variants, through the std module");
|
|
Check(StdPcmKey(host, discovered) == StdPcmKey(host, hostUsr), "the mingw prefix leaves other targets' std modules alone");
|
|
}
|
|
{
|
|
// Everything the std module is compiled with keys its directory, not
|
|
// only target and march. -mtune was left out, so a project built
|
|
// with -mtune=generic shared one std PCM with project.cpp's
|
|
// -mtune=native, and the two rebuilt it in turn on every run, under
|
|
// every module built against it; two sysroots did the same.
|
|
Toolchain tc;
|
|
tc.clangxx = "/app/bin/clang++";
|
|
tc.clangVersion = "clang version 23.1.2";
|
|
Configuration native = MakeBase();
|
|
Configuration generic = MakeBase();
|
|
generic.mtune = "generic";
|
|
Check(StdPcmKey(native, tc) != StdPcmKey(generic, tc), "-mtune keys the std module");
|
|
Configuration sysrootA = MakeBase();
|
|
sysrootA.sysroot = "/opt/sysroot-a";
|
|
Configuration sysrootB = MakeBase();
|
|
sysrootB.sysroot = "/opt/sysroot-b";
|
|
Check(StdPcmKey(sysrootA, tc) != StdPcmKey(sysrootB, tc) && StdPcmKey(sysrootA, tc) != StdPcmKey(native, tc), "the sysroot keys the std module");
|
|
Configuration wasm = MakeBase();
|
|
wasm.target = "wasm32-wasip1";
|
|
wasm.sysroot = "/opt/wasi-sysroot";
|
|
Configuration relaxed = MakeBase();
|
|
relaxed.target = wasm.target;
|
|
relaxed.sysroot = wasm.sysroot;
|
|
relaxed.wasmVariantFlags = {"-mrelaxed-simd"};
|
|
Check(StdPcmKey(wasm, tc) != StdPcmKey(relaxed, tc), "a wasm variant pass's flags key the std module");
|
|
Check(StdPcmKey(native, tc) == StdPcmKey(MakeBase(), tc), "one configuration, one std module");
|
|
Check(StdPcmKey(native, tc).starts_with(std::format("{}-{}-{}-", native.target, native.march, tc.Key())), std::format("the directory still says target, march and toolchain (got '{}')", StdPcmKey(native, tc)));
|
|
Check(native.VariantId(tc) != generic.VariantId(tc) && sysrootA.VariantId(tc) != sysrootB.VariantId(tc), "a build moves with its std module");
|
|
}
|
|
|
|
if (Failures > 0) {
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|