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.
165 lines
7.2 KiB
C++
165 lines
7.2 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
#include <stdlib.h>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pure-data tests for ApplyStandardArgs and ArgQuery — no build/run, just
|
|
// confirms the documented arg parsing produces the documented mutations.
|
|
int main() {
|
|
// --debug + --target= + --march= + --mtune=
|
|
{
|
|
Configuration cfg;
|
|
cfg.target = "ignored";
|
|
std::array<std::string_view, 4> raw = {
|
|
"--debug", "--target=aarch64-linux-gnu", "--march=armv8-a", "--mtune=cortex-a72",
|
|
};
|
|
ApplyStandardArgs(cfg, raw);
|
|
Check(cfg.debug, "--debug should set cfg.debug");
|
|
Check(cfg.target == "aarch64-linux-gnu", "--target= should overwrite cfg.target");
|
|
Check(cfg.march == "armv8-a", "--march= should overwrite cfg.march");
|
|
Check(cfg.mtune == "cortex-a72", "--mtune= should overwrite cfg.mtune");
|
|
}
|
|
|
|
// --cuda-arch= is nvcc's -arch for cfg.cuda, as --march= is the CPU's,
|
|
// and it defaults to native as march does.
|
|
{
|
|
Configuration plain;
|
|
Check(plain.cudaArch == "native", "the GPU architecture defaults to native, like march");
|
|
Configuration cfg;
|
|
std::array<std::string_view, 1> raw = { "--cuda-arch=all-major" };
|
|
ApplyStandardArgs(cfg, raw);
|
|
Check(cfg.cudaArch == "all-major", "--cuda-arch= sets cfg.cudaArch");
|
|
Check(cfg.projectArgs.empty(), "--cuda-arch= is not a project arg");
|
|
}
|
|
{
|
|
// CRAFTER_BUILD_CUDA_ARCH sets it as CRAFTER_BUILD_MARCH sets march,
|
|
// and the command line wins over it.
|
|
setenv("CRAFTER_BUILD_CUDA_ARCH", "sm_86", 1);
|
|
Configuration fromEnv;
|
|
ApplyStandardArgs(fromEnv, {});
|
|
Check(fromEnv.cudaArch == "sm_86", "CRAFTER_BUILD_CUDA_ARCH sets cfg.cudaArch");
|
|
Configuration both;
|
|
std::array<std::string_view, 1> raw = { "--cuda-arch=sm_120" };
|
|
ApplyStandardArgs(both, raw);
|
|
Check(both.cudaArch == "sm_120", "--cuda-arch= wins over CRAFTER_BUILD_CUDA_ARCH");
|
|
unsetenv("CRAFTER_BUILD_CUDA_ARCH");
|
|
}
|
|
|
|
// --sysroot= sets cfg.sysroot (cross-compiles against a foreign root,
|
|
// e.g. an Alpine aarch64 tree; also picked up by the std.cppm lookup
|
|
// and the qemu test-runner's QEMU_LD_PREFIX).
|
|
{
|
|
Configuration cfg;
|
|
std::array<std::string_view, 2> raw = {
|
|
"--target=aarch64-alpine-linux-musl", "--sysroot=/opt/alpine-aarch64",
|
|
};
|
|
ApplyStandardArgs(cfg, raw);
|
|
Check(cfg.sysroot == "/opt/alpine-aarch64", "--sysroot= should set cfg.sysroot");
|
|
Check(cfg.target == "aarch64-alpine-linux-musl", "--target= combines with --sysroot=");
|
|
}
|
|
|
|
// --lib promotes Executable -> LibraryStatic; --shared then promotes to Dynamic.
|
|
{
|
|
Configuration cfg;
|
|
cfg.type = ConfigurationType::Executable;
|
|
std::array<std::string_view, 1> raw = { "--lib" };
|
|
ApplyStandardArgs(cfg, raw);
|
|
Check(cfg.type == ConfigurationType::LibraryStatic, "--lib promotes Exe -> Static");
|
|
}
|
|
{
|
|
Configuration cfg;
|
|
cfg.type = ConfigurationType::Executable;
|
|
// Order shouldn't matter — promotions chain in priority order.
|
|
std::array<std::string_view, 2> raw = { "--shared", "--lib" };
|
|
ApplyStandardArgs(cfg, raw);
|
|
Check(cfg.type == ConfigurationType::LibraryDynamic, "--lib + --shared lands on Dynamic regardless of order");
|
|
}
|
|
{
|
|
Configuration cfg;
|
|
cfg.type = ConfigurationType::Executable;
|
|
std::array<std::string_view, 1> raw = { "--shared" };
|
|
ApplyStandardArgs(cfg, raw);
|
|
Check(cfg.type == ConfigurationType::Executable, "--shared alone on Executable is a no-op");
|
|
}
|
|
{
|
|
// --packaging= is standard: it sets cfg.packaging and, its effect
|
|
// showing in VariantId already, stays out of projectArgs. Outside any
|
|
// project file, as here, it counts as the top-level project's.
|
|
Configuration flatpak;
|
|
std::array<std::string_view, 1> raw = { "--packaging=flatpak" };
|
|
ApplyStandardArgs(flatpak, raw);
|
|
Check(flatpak.packaging == Packaging::Flatpak, "--packaging=flatpak asks for a Flatpak");
|
|
Check(flatpak.projectArgs.empty(), "--packaging= is not a project arg");
|
|
Configuration none;
|
|
std::array<std::string_view, 1> rawNone = { "--packaging=none" };
|
|
ApplyStandardArgs(none, rawNone);
|
|
Check(none.packaging == Packaging::None, "--packaging=none asks for the executable as linked");
|
|
Configuration bad;
|
|
std::array<std::string_view, 1> rawBad = { "--packaging=snap" };
|
|
bool threw = false;
|
|
try {
|
|
ApplyStandardArgs(bad, rawBad);
|
|
} catch (const std::invalid_argument& e) {
|
|
threw = std::string_view(e.what()).contains("expected none or flatpak");
|
|
}
|
|
Check(threw, "an unknown --packaging= is an error naming the kinds");
|
|
}
|
|
|
|
// ArgQuery is returned over the same args span and exposes Has/Get.
|
|
{
|
|
Configuration cfg;
|
|
std::array<std::string_view, 3> raw = { "--timing", "--prefix=/opt/x", "extra" };
|
|
ArgQuery q = ApplyStandardArgs(cfg, raw);
|
|
Check(q.Has("--timing"), "ArgQuery::Has true for present flag");
|
|
Check(!q.Has("--missing"), "ArgQuery::Has false for absent flag");
|
|
auto prefix = q.Get("--prefix=");
|
|
Check(prefix.has_value(), "ArgQuery::Get returns a value for known prefix");
|
|
Check(prefix.value_or("") == "/opt/x", "ArgQuery::Get returns the substring after =");
|
|
Check(!q.Get("--other=").has_value(), "ArgQuery::Get returns nullopt for absent prefix");
|
|
}
|
|
|
|
// Flags ApplyStandardArgs doesn't itself interpret are collected into
|
|
// cfg.projectArgs, which feeds VariantId — a project flag that changes what
|
|
// gets compiled has to move the output directory with it.
|
|
{
|
|
Configuration cfg;
|
|
std::array<std::string_view, 5> raw = {
|
|
"--debug", "--no-webgpu", "--target=aarch64-linux-gnu", "--feature=fancy", "bare",
|
|
};
|
|
ApplyStandardArgs(cfg, raw);
|
|
std::vector<std::string> expected = { "--feature=fancy", "--no-webgpu", "bare" };
|
|
Check(cfg.projectArgs == expected, std::format("only unrecognised args are collected, sorted; got [{}]", std::format("{}", std::views::join_with(cfg.projectArgs, std::string(", ")) | std::ranges::to<std::string>())));
|
|
}
|
|
|
|
// Order and repetition must not perturb the set — otherwise the same build
|
|
// spelled two ways would land in two directories.
|
|
{
|
|
Configuration a;
|
|
Configuration b;
|
|
std::array<std::string_view, 2> forward = { "--alpha", "--beta" };
|
|
std::array<std::string_view, 3> reversed = { "--beta", "--alpha", "--beta" };
|
|
ApplyStandardArgs(a, forward);
|
|
ApplyStandardArgs(b, reversed);
|
|
Check(a.projectArgs == b.projectArgs, "projectArgs is order- and duplicate-insensitive");
|
|
}
|
|
|
|
if (Failures > 0) {
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|