test(cuda): build a .cu without link flags, and through a library
CudaCompile's builds name no CUDA library now, but for the last. The toolkit it names through CUDA_PATH has a lib64 beside its nvcc with the real toolkit's libcudart_static.a, so the executable links only if crafter-build links the runtime, and the recorded link names that archive. Another build puts the .cu in a static library and the C++ unit in an executable built on it, which links only if the library hands the runtime on. That lib64 also holds a libcheckpoint.so and a libm.so that are no libraries, as Arch's lib64 holds a real libcheckpoint.so and a libOpenCL.so: another build links an executable with CUDA sources on a library of the project's named checkpoint, and clang++ adds -lm to every C++ link, so a link that searched the toolkit's lib64 would stop on one of them wherever its -L stood. The last names -L<lib64> -lcudart, as a project with CUDA sources had to, and has that -lcudart ahead of the static archive, so the shared runtime is the one the linker takes. The test skips where the real toolkit's lib64 holds no static runtime, since crafter-build then links none. Before crafter-build linked the runtime, the link failed on an undefined CUDA runtime symbol. ToolchainResolve checks where the runtime is looked for, on staged toolkits, so that it runs in CI: the archive in the lib64 beside nvcc's bin, none where that lib64 holds none, none where it links to itself, which a look that threw would have stopped the test on, and none without an nvcc. CudaCompile is the only check of the runtime link itself, and it skips without such a toolkit, so AGENTS.template.md's note on when to run it names the link as well as the compile, and gives the lookup to ToolchainResolve.
This commit is contained in:
parent
22ea97406d
commit
99653194ba
4 changed files with 179 additions and 18 deletions
|
|
@ -20,9 +20,19 @@ using namespace Crafter;
|
|||
// a directory of its own, then once more with nothing changed, which compiles
|
||||
// nothing, and once its header is newer than its object, which compiles it
|
||||
// again: nvcc's depfile is the record of what the .cu includes, and before
|
||||
// nvcc wrote one, a changed header left the old object in place. Where nvcc
|
||||
// is looked for is ToolchainResolve's to check; this is the build that uses
|
||||
// it. Skips without a CUDA toolkit, which CI does not have.
|
||||
// nvcc wrote one, a changed header left the old object in place. Those builds
|
||||
// name no CUDA library: the configuration links the runtime of the toolkit
|
||||
// itself, the static archive in its lib64 by its path. Another build puts the
|
||||
// .cu in a static library and the C++ unit in an executable built on it, which
|
||||
// links only if the library hands the runtime on. That lib64 also holds a
|
||||
// libcheckpoint.so and a libm.so that are no libraries, so a link that
|
||||
// searched it would stop on one: an executable with CUDA sources built on a
|
||||
// library of the project's named checkpoint has to take the project's, and
|
||||
// clang++ adds -lm to every C++ link. A last build names the shared runtime
|
||||
// itself, as a project with CUDA sources had to, and has its -lcudart ahead of
|
||||
// the static archive. Where nvcc is looked for, and the runtime beside it, is
|
||||
// ToolchainResolve's to check; this is the build that uses them. Skips without
|
||||
// a CUDA toolkit whose lib64 holds the static runtime; CI has none.
|
||||
namespace {
|
||||
std::int32_t Failures = 0;
|
||||
|
||||
|
|
@ -53,6 +63,11 @@ namespace {
|
|||
fs::permissions(file, fs::perms::owner_exec, fs::perm_options::add);
|
||||
}
|
||||
|
||||
std::string Read(const fs::path& file) {
|
||||
std::ifstream in(file, std::ios::binary);
|
||||
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
std::vector<std::string> Lines(const fs::path& file) {
|
||||
std::vector<std::string> lines;
|
||||
std::ifstream in(file);
|
||||
|
|
@ -63,6 +78,13 @@ namespace {
|
|||
std::string Joined(const std::vector<std::string>& lines) {
|
||||
return std::views::join_with(lines, std::string(" | ")) | std::ranges::to<std::string>();
|
||||
}
|
||||
|
||||
struct Built {
|
||||
std::string error;
|
||||
fs::path binary;
|
||||
// The link command the build recorded, beside its objects.
|
||||
fs::path linkRecord;
|
||||
};
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
@ -72,6 +94,14 @@ int main() {
|
|||
return 77;
|
||||
}
|
||||
const fs::path nvcc = real.nvcc;
|
||||
// The static runtime crafter-build links, from the lib64 beside nvcc's
|
||||
// bin. A toolkit without one there gets none linked, and the runtime link
|
||||
// has nothing to show.
|
||||
const fs::path realArchive = real.cudaRuntime;
|
||||
if (realArchive.empty()) {
|
||||
std::println("SKIP: no libcudart_static.a in the lib64 beside {}, so crafter-build links no CUDA runtime for this toolkit", nvcc.parent_path().string());
|
||||
return 77;
|
||||
}
|
||||
|
||||
const fs::path scratch = fs::temp_directory_path() / "crafter-build-cuda-compile";
|
||||
std::error_code ec;
|
||||
|
|
@ -80,9 +110,23 @@ int main() {
|
|||
const fs::path project = scratch / "project";
|
||||
fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", project, fs::copy_options::recursive);
|
||||
|
||||
// The toolkit CUDA_PATH names: an nvcc that writes down how it was run and
|
||||
// runs the real one, and a lib64 with the real toolkit's static runtime
|
||||
// and a libcheckpoint.so and a libm.so that are no libraries at all. A
|
||||
// toolkit's lib64 can hold a library named like a project's own (Arch's
|
||||
// holds a libcheckpoint.so) or like one the link takes from the system (a
|
||||
// libOpenCL.so), and a link that searched it would take that one: the
|
||||
// libm.so stands for those, as clang++ adds -lm to every C++ link.
|
||||
const fs::path log = scratch / "nvcc.log";
|
||||
const fs::path toolkit = scratch / "toolkit";
|
||||
WriteProgram(toolkit / "bin" / "nvcc", std::format("#!/bin/sh\nprintf '%s\\n' \"$*\" >> {}\nexec {} \"$@\"\n", ShellQuote(log.string()), ShellQuote(nvcc.string())));
|
||||
fs::create_directories(toolkit / "lib64");
|
||||
fs::create_symlink(realArchive, toolkit / "lib64" / "libcudart_static.a");
|
||||
std::ofstream(toolkit / "lib64" / "libcheckpoint.so") << "not a library\n";
|
||||
std::ofstream(toolkit / "lib64" / "libm.so") << "not a library\n";
|
||||
// What a link names the runtime by: the archive in the toolkit's lib64, by
|
||||
// its path.
|
||||
const std::string runtime = (toolkit / "lib64" / "libcudart_static.a").string();
|
||||
const fs::path decoy = scratch / "decoy";
|
||||
WriteProgram(decoy / "nvcc", "#!/bin/sh\necho 'the nvcc on PATH ran' >&2\nexit 1\n");
|
||||
// Before anything resolves the toolchain, which reads the environment
|
||||
|
|
@ -91,8 +135,6 @@ int main() {
|
|||
const char* path = std::getenv("PATH");
|
||||
setenv("PATH", std::format("{}:{}", decoy.string(), path ? path : "").c_str(), 1);
|
||||
|
||||
// The CUDA runtime the .cu calls into, from the real toolkit.
|
||||
const std::string runtimeDir = std::format("-L{}", (nvcc.parent_path().parent_path() / "lib64").string());
|
||||
const auto build = [&](std::optional<std::string> arch) {
|
||||
Configuration app;
|
||||
app.path = project;
|
||||
|
|
@ -104,34 +146,120 @@ int main() {
|
|||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
app.cuda = { project / "kernel" };
|
||||
app.linkFlags = { runtimeDir, "-lcudart" };
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
const BuildResult result = Build(app, depResults, depMutex);
|
||||
return std::pair{ result.result, app.BinDir() / "cuda" };
|
||||
return Built{ result.result, app.BinDir() / "cuda", app.BuildDir() / "cuda.link" };
|
||||
};
|
||||
|
||||
const auto [native, nativeBinary] = build(std::nullopt);
|
||||
Check(native.empty(), std::format("the build with the default architecture succeeds: {}", native));
|
||||
Check(fs::exists(nativeBinary), "it links the executable");
|
||||
const Built native = build(std::nullopt);
|
||||
Check(native.error.empty(), std::format("the build with the default architecture succeeds: {}", native.error));
|
||||
Check(fs::exists(native.binary), "it links the executable");
|
||||
const std::string record = Read(native.linkRecord);
|
||||
Check(record.contains(runtime), std::format("with the static runtime from the toolkit's lib64, though no link flag names it (link: {})", record));
|
||||
std::vector<std::string> runs = Lines(log);
|
||||
Check(runs.size() == 1 && runs.front().contains("-arch=native"), std::format("CUDA_PATH's nvcc compiles the .cu once, with -arch=native (ran: {})", Joined(runs)));
|
||||
|
||||
const auto [other, otherBinary] = build("sm_86");
|
||||
Check(other.empty(), std::format("the build for sm_86 succeeds: {}", other));
|
||||
const Built other = build("sm_86");
|
||||
Check(other.error.empty(), std::format("the build for sm_86 succeeds: {}", other.error));
|
||||
runs = Lines(log);
|
||||
Check(runs.size() == 2 && runs.back().contains("-arch=sm_86"), std::format("another architecture compiles the unchanged .cu again, for it (ran: {})", Joined(runs)));
|
||||
Check(fs::exists(otherBinary) && otherBinary.parent_path() != nativeBinary.parent_path(), "in a directory of its own");
|
||||
Check(fs::exists(other.binary) && other.binary.parent_path() != native.binary.parent_path(), "in a directory of its own");
|
||||
|
||||
const auto [again, againBinary] = build("sm_86");
|
||||
Check(again.empty() && Lines(log).size() == 2, std::format("an unchanged build compiles nothing (ran: {})", Joined(Lines(log))));
|
||||
const Built again = build("sm_86");
|
||||
Check(again.error.empty() && Lines(log).size() == 2, std::format("an unchanged build compiles nothing (ran: {})", Joined(Lines(log))));
|
||||
|
||||
fs::last_write_time(project / "kernel.h", fs::file_time_type::clock::now());
|
||||
const auto [touched, touchedBinary] = build("sm_86");
|
||||
Check(touched.empty(), std::format("the build after the header changed succeeds: {}", touched));
|
||||
const Built touched = build("sm_86");
|
||||
Check(touched.error.empty(), std::format("the build after the header changed succeeds: {}", touched.error));
|
||||
runs = Lines(log);
|
||||
Check(runs.size() == 3 && runs.back().contains("-arch=sm_86"), std::format("a changed header compiles the .cu again (ran: {})", Joined(runs)));
|
||||
|
||||
{
|
||||
// The .cu in a static library, the C++ unit in an executable built on
|
||||
// it: neither names a CUDA library, and the executable's link has the
|
||||
// runtime only from what the library hands on.
|
||||
const fs::path split = scratch / "split";
|
||||
fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", split, fs::copy_options::recursive);
|
||||
Configuration kernels;
|
||||
kernels.path = split;
|
||||
kernels.name = "kernels";
|
||||
kernels.outputName = "kernels";
|
||||
kernels.type = ConfigurationType::LibraryStatic;
|
||||
kernels.cuda = { split / "kernel" };
|
||||
Configuration program;
|
||||
program.path = split;
|
||||
program.name = "program";
|
||||
program.outputName = "program";
|
||||
program.type = ConfigurationType::Executable;
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
program.GetInterfacesAndImplementations(ifaces, impls);
|
||||
program.dependencies = { &kernels };
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
const BuildResult built = Build(program, depResults, depMutex);
|
||||
Check(built.result.empty(), std::format("an executable built on a library with CUDA sources links: {}", built.result));
|
||||
Check(Read(program.BuildDir() / "program.link").contains(runtime), "with the runtime the library hands on");
|
||||
}
|
||||
{
|
||||
// An executable with CUDA sources built on a library of the
|
||||
// project's named like one in the toolkit's lib64: the linker has to
|
||||
// take the project's, and stops on the toolkit's file if it searches
|
||||
// the toolkit's lib64 first.
|
||||
const fs::path named = scratch / "named";
|
||||
fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", named, fs::copy_options::recursive);
|
||||
Configuration checkpoint;
|
||||
checkpoint.path = named;
|
||||
checkpoint.name = "checkpoint";
|
||||
checkpoint.outputName = "checkpoint";
|
||||
checkpoint.type = ConfigurationType::LibraryStatic;
|
||||
{
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "checkpoint" };
|
||||
checkpoint.GetInterfacesAndImplementations(ifaces, impls);
|
||||
}
|
||||
Configuration app;
|
||||
app.path = named;
|
||||
app.name = "named";
|
||||
app.outputName = "named";
|
||||
app.type = ConfigurationType::Executable;
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
app.cuda = { named / "kernel" };
|
||||
app.dependencies = { &checkpoint };
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
const BuildResult built = Build(app, depResults, depMutex);
|
||||
Check(built.result.empty(), std::format("an executable with CUDA sources links a library of its project's named like one in the toolkit's lib64: {}", built.result));
|
||||
}
|
||||
{
|
||||
// A project that names the shared runtime itself, as every project.cpp
|
||||
// with CUDA sources had to: its -lcudart comes ahead of the static
|
||||
// archive on the link line, so the linker takes the shared runtime.
|
||||
const fs::path own = scratch / "own";
|
||||
fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", own, fs::copy_options::recursive);
|
||||
Configuration app;
|
||||
app.path = own;
|
||||
app.name = "own";
|
||||
app.outputName = "own";
|
||||
app.type = ConfigurationType::Executable;
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
app.cuda = { own / "kernel" };
|
||||
app.linkFlags = { std::format("-L{}", ShellQuote(realArchive.parent_path().string())), "-lcudart" };
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
const BuildResult built = Build(app, depResults, depMutex);
|
||||
Check(built.result.empty(), std::format("a build that names -lcudart itself links: {}", built.result));
|
||||
const std::string link = Read(app.BuildDir() / "own.link");
|
||||
const std::size_t shared = link.find(" -lcudart ");
|
||||
const std::size_t archive = link.find(runtime);
|
||||
Check(shared != std::string::npos && archive != std::string::npos && shared < archive, std::format("with its -lcudart ahead of the static runtime (link: {})", link));
|
||||
}
|
||||
|
||||
fs::remove_all(scratch, ec);
|
||||
return Failures == 0 ? 0 : 1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue