test(build): sources of one file name, and where their objects go

SameNamedSources builds configurations whose sources share file names,
on a fresh copy of its fixture outside the checkout, and runs what they
link. `names` is an executable of an a/Thing.cpp and a b/Thing.cpp, the
C sources a/unit.c and b/unit.c, a b/Thing.c beside the b/Thing.cpp and
a Thing.cpp in a directory beside the configuration's own; its program
prints what each returns, the first time and again after a header that
only a/Thing.cpp includes has changed and after b/Thing.cpp and
b/unit.c have. `registry` is a debug executable of an a/Self.cpp and a
b/Self.cpp that register themselves when the program starts, which
prints that each did so once. `things` is a static library of
a/Thing.cpp, b/Thing.cpp, b/Thing.c and the two unit.c, and
`things-app` a program of main.cpp and ../shared/Thing.cpp built on
it, which prints what `names` does. `outside` is a static library of
one C source that lies outside its configuration's directory and is
listed relative to the working directory, built from a working
directory with a space in its name; `llvm-ar t` lists its object.

ObjectPath asks the rule for the objects of sources without building
anything: in the configuration's directory and in directories of it,
of one file name in two directories, a .cpp, a .c and a .cu of one
name in one directory, outside the directory, and under a directory of
the configuration's own that is named objects-outside and holds the
whole path of the source outside. No two of them get one object. A
path spelled with . and .., a relative source, and a source of a
configuration whose path ends in a separator or is ./ are each given
the object of the file they lead to, and a .cppm, a .h and a file
without an extension are refused.

CudaCompile gains a static library of its kernel.cu and a kernel.c
beside it, whose archive lists kernel_source.o and kernel_cuda.o.

Before the fix SameNamedSources failed at the link of `names`, with
duplicate symbols, and of `things-app`, with undefined ones, and its
`registry` printed that one of the two sources had registered twice
and the other not at all. `outside` built before the fix as well, its
object lying in the build directory then: it is there for the path
the fix gives that object.
This commit is contained in:
Jorijn van der Graaf 2026-11-19 11:02:39 +01:00
commit f9bd5333da
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
19 changed files with 456 additions and 4 deletions

View file

@ -0,0 +1,5 @@
/* SPDX-License-Identifier: LGPL-3.0-only
SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® */
/* A C source of kernel.cu's name, in its directory. */
int KernelInC(void) { return 1; }

View file

@ -28,11 +28,14 @@ using namespace Crafter;
// 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
// clang++ adds -lm to every C++ link. Another 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.
// the static archive. A last one is a static library of the .cu and a C source
// of its name beside it: the archive has to hold an object of each, where the
// two used to be compiled into one (issue #50). 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;
@ -259,6 +262,27 @@ int main() {
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));
}
{
// A C source of the .cu's name beside it, in a static library: each
// has an object of its own, and the archive lists the two. Both used
// to be compiled into kernel_source.o, each over the other (issue
// #50).
const fs::path twins = scratch / "twins";
fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", twins, fs::copy_options::recursive);
Configuration kernels;
kernels.path = twins;
kernels.name = "twins";
kernels.outputName = "twins";
kernels.type = ConfigurationType::LibraryStatic;
kernels.cFiles = { twins / "kernel" };
kernels.cuda = { twins / "kernel" };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const BuildResult built = Build(kernels, depResults, depMutex);
Check(built.result.empty(), std::format("a library of a kernel.c and a kernel.cu in one directory builds: {}", built.result));
const auto members = RunCommandWithTimeout(std::format("{} t {}", ShellQuote(GetToolchain().llvmAr.string()), ShellQuote(ExpectedOutput(kernels).string())), std::chrono::seconds(30));
Check(members.exitCode == 0 && members.output == "kernel_source.o\nkernel_cuda.o\n", std::format("with an object of each in its archive: llvm-ar t lists '{}' (exit {})", members.output, members.exitCode));
}
fs::remove_all(scratch, ec);
return Failures == 0 ? 0 : 1;

114
tests/ObjectPath/main.cpp Normal file
View file

@ -0,0 +1,114 @@
// 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;
// ObjectPath names the object of a source from where the source lies, so
// that two sources have one object only when their paths, with . and ..
// taken out of them, are one: by its path from the configuration's
// directory under BuildDir()/objects, by its whole path under
// BuildDir()/objects-outside when it lies outside that directory, and with
// an ending of its own for a .cpp, a .c and a .cu. Named from the file name
// alone, a/Thing.cpp and b/Thing.cpp shared an object (issue #50), and so
// did a Thing.c and a Thing.cu in one directory.
//
// Nothing is built here: the rule is asked for the object of sources that
// need not exist, which is how the CUDA ending is checked on a machine
// without nvcc, and the tree of outside sources against a directory of the
// configuration's own that is named like it. SameNamedSources builds
// configurations with such sources.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
Configuration Named(const fs::path& path) {
Configuration cfg;
cfg.path = path;
cfg.name = "paths";
cfg.outputName = "paths";
cfg.target = HostTarget();
return cfg;
}
}
int main() {
// Never made: the rule reads no file.
const fs::path root = fs::temp_directory_path() / "crafter-build-object-path";
const Configuration cfg = Named(root / "project");
const fs::path inside = cfg.BuildDir() / "objects";
const fs::path outside = cfg.BuildDir() / "objects-outside";
// Every source asked about below with the object it was given, to see
// at the end that no two of them were given one.
std::vector<std::pair<fs::path, fs::path>> given;
const auto expect = [&](const fs::path& source, const fs::path& object, std::string_view what) {
const fs::path got = ObjectPath(cfg, source);
Check(got == object, std::format("{}: {} has the object {}, expected {}", what, source.string(), got.string(), object.string()));
given.emplace_back(source, got);
};
expect(cfg.path / "main.cpp", inside / "main_impl.o", "a source in the configuration's directory itself");
expect(cfg.path / "a" / "Thing.cpp", inside / "a" / "Thing_impl.o", "a source in a directory of it");
expect(cfg.path / "b" / "Thing.cpp", inside / "b" / "Thing_impl.o", "a source of the same file name in another directory");
expect(cfg.path / "a" / "Thing.c", inside / "a" / "Thing_source.o", "a C source beside a C++ one of its name");
expect(cfg.path / "a" / "Thing.cu", inside / "a" / "Thing_cuda.o", "a CUDA source beside a C one of its name");
expect(cfg.path / "implementations" / "Crafter.Build-Clang.cpp", inside / "implementations" / "Crafter.Build-Clang_impl.o", "a file name with a dot in it");
// Outside the configuration's directory: by the whole path, in a tree
// of its own.
const fs::path shared = root / "shared" / "Thing.cpp";
expect(shared, outside / (root / "shared" / "Thing_impl.o").relative_path(), "a source beside the configuration's directory");
expect(root / "project-more" / "Thing.cpp", outside / (root / "project-more" / "Thing_impl.o").relative_path(), "a source in a directory whose name begins with the configuration's");
// A directory of the configuration's own named like that tree, with
// the whole path of the outside source in it: an object of the first
// tree all the same.
const fs::path mimic = cfg.path / "objects-outside" / shared.relative_path();
expect(mimic, inside / "objects-outside" / (root / "shared" / "Thing_impl.o").relative_path(), "a source under a directory of the configuration's named objects-outside");
for (std::size_t i = 0; i < given.size(); ++i) {
for (std::size_t j = i + 1; j < given.size(); ++j) {
Check(given[i].second != given[j].second, std::format("{} and {} have one object, {}", given[i].first.string(), given[j].first.string(), given[i].second.string()));
}
}
// One source however its path is spelled.
Check(ObjectPath(cfg, cfg.path / "b" / ".." / "a" / "." / "Thing.cpp") == inside / "a" / "Thing_impl.o", "a path with . and .. in it names the source it leads to");
Check(ObjectPath(cfg, cfg.path / ".." / "shared" / "Thing.cpp") == ObjectPath(cfg, shared), "a path that leaves the configuration's directory through .. names the source outside it");
Check(ObjectPath(cfg, fs::path("sources") / "Thing.c") == ObjectPath(cfg, fs::current_path() / "sources" / "Thing.c"), "a relative source is taken against the working directory");
// The configuration's directory however it is spelled: with a
// separator at its end, or relative as a project file's "./" is.
const Configuration trailing = Named(root / "project" / "");
Check(ObjectPath(trailing, root / "project" / "a" / "Thing.cpp") == trailing.BuildDir() / "objects" / "a" / "Thing_impl.o", std::format("a configuration whose path ends in a separator: {}", ObjectPath(trailing, root / "project" / "a" / "Thing.cpp").string()));
const Configuration relative = Named("./");
Check(ObjectPath(relative, fs::path("lib") / "unit.c") == relative.BuildDir() / "objects" / "lib" / "unit_source.o", std::format("a relative source of a configuration whose path is ./: {}", ObjectPath(relative, fs::path("lib") / "unit.c").string()));
Check(ObjectPath(relative, fs::current_path() / "lib" / "unit.c") == relative.BuildDir() / "objects" / "lib" / "unit_source.o", "and the same source by its whole path");
// An interface unit's object is not ObjectPath's to name, nor is any
// other file's.
for (const std::string_view name : {"Thing.cppm", "Thing.h", "Thing"}) {
bool refused = false;
try {
ObjectPath(cfg, cfg.path / name);
} catch (const std::invalid_argument&) {
refused = true;
}
Check(refused, std::format("{} is refused: not a .cpp, .c or .cu", name));
}
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}

View file

@ -0,0 +1,6 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
#include "limit.h"
int A() { return LIMIT; }

View file

@ -0,0 +1,4 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
#define LIMIT 100

View file

@ -0,0 +1,4 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
#define LIMIT 1

View file

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-3.0-only
SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® */
int AUnit(void) { return 4; }

View file

@ -0,0 +1,4 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
int B() { return 200; }

View file

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-3.0-only
SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® */
int BThingInC(void) { return 16; }

View file

@ -0,0 +1,4 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
int B() { return 2; }

View file

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-3.0-only
SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® */
int BUnit(void) { return 800; }

View file

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-3.0-only
SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® */
int BUnit(void) { return 8; }

View file

@ -0,0 +1,18 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int A();
int B();
int Shared();
extern "C" int AUnit(void);
extern "C" int BUnit(void);
extern "C" int BThingInC(void);
// Prints what each of the other sources returns, a/Thing.cpp's first: every
// one of them has to be in the program, and as it stands now.
int main() {
std::printf("%d %d %d %d %d %d", A(), B(), AUnit(), BUnit(), BThingInC(), Shared());
return 0;
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
void Register(int source);
// Nothing in this file is named by another: it is in the program for what it
// does when the program starts.
namespace {
const int registered = (Register(1), 0);
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
void Register(int source);
// Nothing in this file is named by another: it is in the program for what it
// does when the program starts.
namespace {
const int registered = (Register(2), 0);
}

View file

@ -0,0 +1,19 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
namespace {
// How often each source has registered: [1] is a/Self.cpp's count and
// [2] is b/Self.cpp's.
int Registered[3];
}
// Called by a/Self.cpp with 1 and by b/Self.cpp with 2, as the program starts.
void Register(int source) { ++Registered[source]; }
// Prints the two counts: "1 1" when each of the two is in the program once.
int main() {
std::printf("%d %d", Registered[1], Registered[2]);
return 0;
}

View file

@ -0,0 +1,4 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
int Shared() { return 32; }

View file

@ -0,0 +1,208 @@
// 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;
// Two sources of one configuration may have the same file name in different
// directories: each has an object of its own. An object used to be named
// from its source's file name alone, so a/Thing.cpp and b/Thing.cpp were
// compiled into one Thing_impl.o, each over the other, and the link was
// given that object twice (issue #50). A build of sources that define what
// another file calls failed at the link, with duplicate symbols of one of
// them and no word of the other. A debug build of sources that define
// nothing another file names, and are in the program for what they do when
// it starts, succeeded: the program had one of them twice and the other not
// at all.
//
// The fixture has both. `names` is an executable of a/Thing.cpp and
// b/Thing.cpp, the C sources a/unit.c and b/unit.c, a b/Thing.c beside the
// b/Thing.cpp, and a Thing.cpp in a directory beside the configuration's
// own; its program prints what each of them returns. A header only
// a/Thing.cpp includes is then changed, and after that b/Thing.cpp and
// b/unit.c: each time the program has to print the new values of the
// sources that changed and the values the others had. `registry` is a debug
// executable of a/Self.cpp and b/Self.cpp, which register themselves when
// the program starts, and prints how often each did. `things` is a static
// library of a/Thing.cpp, b/Thing.cpp, b/Thing.c and the two unit.c, whose
// archive then holds members of one name, and `things-app` a program of
// main.cpp and ../shared/Thing.cpp built on it.
//
// The object of a source outside its configuration's directory is named by
// the source's whole path, so that path holds every directory above the
// source. `outside`, which the test makes last, is a static library of one
// such C source, listed relative to the working directory and built from a
// working directory with a space in its name: the object's path then holds
// the space, where neither the source as listed nor the build directory
// does, and has to reach the compiler and the archive as one path.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
// The fixture is changed during the run, so it is a copy outside the
// checkout that is built.
fs::path StageFixture() {
const fs::path source = fs::current_path() / "tests" / "SameNamedSources" / "fixture";
const fs::path staged = fs::temp_directory_path() / "crafter-build-same-named-sources";
fs::remove_all(staged);
fs::copy(source, staged, fs::copy_options::recursive);
return staged;
}
// Puts <name>-grown<extension>.in in the place of `file`. The other
// text is a file of the fixture rather than a string here, so that the
// one under the source's name is the only one a build sees.
void Grow(const fs::path& file) {
const fs::path grown = file.parent_path() / std::format("{}-grown{}.in", file.stem().string(), file.extension().string());
fs::copy_file(grown, file, fs::copy_options::overwrite_existing);
// copy_file gives the copy the time of what it copied, which would
// leave the changed file older than the object built from it.
fs::last_write_time(file, fs::file_time_type::clock::now());
}
// The C sources of `names`, without their extensions and whole: cFiles
// are read against the working directory, not against cfg.path.
std::vector<fs::path> CSources(const fs::path& staged) {
return { staged / "names" / "a" / "unit", staged / "names" / "b" / "unit", staged / "names" / "b" / "Thing" };
}
// The issue's first case, and more of the kind: every source defines
// something main.cpp calls. ../shared/Thing lies outside the
// configuration's directory.
Configuration MakeNames(const fs::path& staged) {
Configuration cfg;
cfg.path = staged / "names";
cfg.name = "names";
cfg.outputName = "names";
cfg.target = HostTarget();
cfg.type = ConfigurationType::Executable;
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 4> impls = { "main", "a/Thing", "b/Thing", "../shared/Thing" };
cfg.GetInterfacesAndImplementations(ifaces, impls);
cfg.cFiles = CSources(staged);
return cfg;
}
// The issue's second case: a debug build of sources nothing names.
Configuration MakeRegistry(const fs::path& staged) {
Configuration cfg;
cfg.path = staged / "registry";
cfg.name = "registry";
cfg.outputName = "registry";
cfg.target = HostTarget();
cfg.debug = true;
cfg.type = ConfigurationType::Executable;
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 3> impls = { "main", "a/Self", "b/Self" };
cfg.GetInterfacesAndImplementations(ifaces, impls);
return cfg;
}
// a/Thing.cpp, b/Thing.cpp and the C sources of `names` as a static
// library.
std::unique_ptr<Configuration> MakeThings(const fs::path& staged) {
std::unique_ptr<Configuration> lib = std::make_unique<Configuration>();
lib->path = staged / "names";
lib->name = "things";
lib->outputName = "things";
lib->target = HostTarget();
lib->type = ConfigurationType::LibraryStatic;
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 2> impls = { "a/Thing", "b/Thing" };
lib->GetInterfacesAndImplementations(ifaces, impls);
lib->cFiles = CSources(staged);
return lib;
}
Configuration MakeThingsApp(const fs::path& staged, Configuration* things) {
Configuration cfg;
cfg.path = staged / "names";
cfg.name = "things-app";
cfg.outputName = "things-app";
cfg.target = HostTarget();
cfg.type = ConfigurationType::Executable;
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 2> impls = { "main", "../shared/Thing" };
cfg.GetInterfacesAndImplementations(ifaces, impls);
cfg.dependencies = { things };
return cfg;
}
bool BuildOk(Configuration& cfg, std::string_view label) {
// A fresh record for each pass: one that holds a configuration's
// build already would hand it to the next pass unbuilt.
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const BuildResult r = Build(cfg, depResults, depMutex);
if (!r.result.empty()) {
std::println(std::cerr, "FAIL: {} failed: {}", label, r.result);
++Failures;
return false;
}
return true;
}
void CheckPrints(const Configuration& cfg, std::string_view expected, std::string_view label) {
const auto r = RunCommandWithTimeout(ShellQuote(ExpectedOutput(cfg).string()), std::chrono::seconds(30));
Check(r.exitCode == 0 && r.output == expected, std::format("{}: the program printed '{}' (exit {}), expected '{}'", label, r.output, r.exitCode, expected));
}
}
int main() {
const fs::path staged = StageFixture();
// A() B() AUnit() BUnit() BThingInC() Shared(), see the fixture's
// names/main.cpp.
Configuration names = MakeNames(staged);
if (BuildOk(names, "the build of names")) CheckPrints(names, "1 2 4 8 16 32", "names");
Grow(staged / "names" / "a" / "limit.h");
if (BuildOk(names, "the build of names after a header of a/Thing.cpp changed")) CheckPrints(names, "100 2 4 8 16 32", "names after a header of a/Thing.cpp changed");
Grow(staged / "names" / "b" / "Thing.cpp");
Grow(staged / "names" / "b" / "unit.c");
if (BuildOk(names, "the build of names after b/Thing.cpp and b/unit.c changed")) CheckPrints(names, "100 200 4 800 16 32", "names after b/Thing.cpp and b/unit.c changed");
// How often a/Self.cpp registered, then b/Self.cpp.
Configuration registry = MakeRegistry(staged);
if (BuildOk(registry, "the debug build of registry")) CheckPrints(registry, "1 1", "registry");
// The sources as they are after the changes above.
const std::unique_ptr<Configuration> things = MakeThings(staged);
Configuration app = MakeThingsApp(staged, things.get());
if (BuildOk(app, "the build of things-app on the static library things")) CheckPrints(app, "100 200 4 800 16 32", "things-app");
// Last, because it changes the working directory of the test: a
// relative cfg.path and a relative C source are read against it.
const fs::path spaced = staged / "with space";
fs::create_directories(spaced / "app");
fs::create_directories(spaced / "shared");
std::ofstream(spaced / "shared" / "unit.c") << "int Outside(void) { return 64; }\n";
fs::current_path(spaced / "app");
Configuration outside;
outside.path = "./";
outside.name = "outside";
outside.outputName = "outside";
outside.target = HostTarget();
outside.type = ConfigurationType::LibraryStatic;
outside.cFiles = { "../shared/unit" };
if (BuildOk(outside, "the build of outside, from a working directory with a space in its name")) {
const auto members = RunCommandWithTimeout(std::format("{} t {}", ShellQuote(GetToolchain().llvmAr.string()), ShellQuote(ExpectedOutput(outside).string())), std::chrono::seconds(30));
Check(members.exitCode == 0 && members.output == "unit_source.o\n", std::format("outside: llvm-ar t lists '{}' (exit {}), expected the object of ../shared/unit.c", members.output, members.exitCode));
}
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}