fix(build): name a source's object from its path

The object of an implementation unit was named from its file's name
alone, <build dir>/<name>_impl.o, and that of a C or CUDA source
likewise, <name>_source.o. Two sources of one configuration with the
same file name in different directories were therefore compiled into
one object, each over the other, with one dependency file between
them, and the link was given that object twice (issue #50). A build of
an a/Thing.cpp and a b/Thing.cpp that each define a function failed at
the link, with duplicate symbols of one of the two and no word of the
other. A debug build of two such files that define nothing another
file names succeeded, and its program had one file's code twice and
the other's not at all. A .c and a .cu of one name shared an object in
one directory as well.

An object, with its dependency file beside it, is now named from where
its source lies: under <build dir>/objects by the source's path from
the configuration's directory, objects/a/Thing_impl.o for a/Thing.cpp,
or, for a source outside that directory, under
<build dir>/objects-outside by its whole path. A .cu's object ends in
_cuda.o; a .cpp's still ends in _impl.o and a .c's in _source.o. Two
sources then have one object only when their paths are one once . and
.. are taken out of them: any other two differ by path or by ending in
one of the two directories, and the two directories share nothing,
whatever a project calls its own directories. They are directories of
their own, and not the build directory itself, because that one holds
files named after other things: an interface unit's object, the record
of the link and the list of its objects and, for an executable, the
BMIs.

ObjectPath gives that name, exported beside ExpectedOutput for what
looks at an object from outside Build(). Build() names each object by
the same rule, once, as it goes over the sources, and the list it
keeps of them for the link is what the check whether an object is
newer than the output reads as well. Before, each put the name
together for itself: Implementation::Check, Implementation::Schedule,
and Build() where it compiles C and CUDA sources, lists the link's
objects and checks whether to link again. Implementation::Check and
Schedule are given the object, where they put its name together from
the build directory. The job that compiles a source makes the
directory of its object first, since the compiler makes none. The C
compile quotes its source, dependency file and object for the shell,
as the CUDA compile does: the object of a source outside the
configuration's directory holds that source's whole path, and so
directories the command did not name before, the working directory's
among them where the source is listed relative to it.

The progress line of the compile of a .cpp, .c or .cu names the source
by the path its object is named from, "Compiling a/Thing.cpp" where it
was "Compiling Thing.cpp" for both, and so does the error for a .cu
without an nvcc.

What else changes: every implementation unit and every C and CUDA
source has its object in a new place, so an existing build directory
compiles them once more. The objects under the old names stay where
they are, unused; a static library's archive does not keep them, being
written anew. Interface units are as they were: a BMI is found by its
module's name, so two interface files of one name would have to be one
module. A source that a configuration lists twice is also as it was,
compiled twice into its one object, which the link is given twice.

In IncrementalHeaderChange, IncrementalInterfaceChange, StdPcmRebuild
and TransitiveInterfaceChange, the lines that spelled out an object's
old name take it from ObjectPath.

Fixes #50
This commit is contained in:
Jorijn van der Graaf 2026-11-19 11:02:39 +01:00
commit 7f8089474b
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
8 changed files with 162 additions and 70 deletions

View file

@ -320,6 +320,73 @@ fs::path Crafter::ExpectedOutput(const Configuration& config) {
return mingwTarget ? dir / std::format("{}.dll", config.outputName) : dir / std::format("lib{}.so", config.outputName);
}
namespace {
// `path` resolved against the working directory, with no `.` or `..` in
// it and no separator at its end.
fs::path WithoutSeparator(fs::path path) {
path = fs::absolute(path).lexically_normal();
return path.has_filename() ? path : path.parent_path();
}
// What the name of a source's object ends in, by the source's
// extension: one ending for each kind of source, so a .c beside a .cu
// of its name has an object of its own. Empty for an extension Build()
// compiles no source of.
std::string_view ObjectSuffix(std::string_view extension) {
if (extension == ".cpp") return "_impl.o";
if (extension == ".c") return "_source.o";
if (extension == ".cu") return "_cuda.o";
return "";
}
// ObjectPath's rule for the sources of one configuration, with what it
// takes from the configuration worked out once for all of them.
class SourceLayout {
public:
SourceLayout(const fs::path& directory, const fs::path& buildDir) : directory(WithoutSeparator(directory)), inside(buildDir / "objects"), outside(buildDir / "objects-outside") {}
// The object of the source a configuration lists as `listed`, which
// is its path without its extension, `extension`.
fs::path Object(const fs::path& listed, std::string_view extension) const {
fs::path object = fs::absolute(listed).lexically_normal();
object += ObjectSuffix(extension);
const fs::path within = Within(object);
return within.empty() ? outside / object.relative_path() : inside / within;
}
// That source as the progress line of its compile names it: by its
// path from the configuration's directory, or by its whole path
// when it lies outside that directory.
std::string Shown(const fs::path& listed, std::string_view extension) const {
fs::path source = fs::absolute(listed).lexically_normal();
source += extension;
const fs::path within = Within(source);
return within.empty() ? source.string() : within.string();
}
private:
// The path of `resolved` from the configuration's directory; empty
// when it does not lie in that directory.
fs::path Within(const fs::path& resolved) const {
const fs::path relative = resolved.lexically_relative(directory);
if (relative.empty() || relative == "." || *relative.begin() == "..") return {};
return relative;
}
fs::path directory;
fs::path inside;
fs::path outside;
};
}
fs::path Crafter::ObjectPath(const Configuration& config, const fs::path& source) {
const std::string extension = source.extension().string();
if (ObjectSuffix(extension).empty()) throw std::invalid_argument(std::format("{} is not a .cpp, .c or .cu source", source.string()));
fs::path listed = source;
listed.replace_extension();
return SourceLayout(config.path, config.BuildDir()).Object(listed, extension);
}
CompileCommand Crafter::GetCompileCommand(const Configuration& config) {
CompileCommand out;
out.stdPcmDir = StdPcmDir(config);
@ -884,6 +951,9 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
}
fs::path buildDir = config.BuildDir();
// Where the object of each .cpp, .c and .cu of this configuration goes,
// and what its compile is called (ObjectPath).
const SourceLayout layout(config.path, buildDir);
// Everything this configuration produces lands here: a library's archive
// and module PCMs, an executable and the files it reads at run time.
fs::path outputDir = config.ProgramDir();
@ -893,13 +963,9 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// follows outputDir in it, or, for a directory copied into outputDir
// itself ("data/", "data/."), the names of what that directory holds.
std::set<std::string> placed = {ExpectedOutput(config).filename().string()};
const auto withoutSeparator = [](fs::path path) {
path = fs::absolute(path).lexically_normal();
return path.has_filename() ? path : path.parent_path();
};
const fs::path outputRoot = withoutSeparator(outputDir);
const fs::path outputRoot = WithoutSeparator(outputDir);
const auto place = [&](const fs::path& destination, const fs::path& source) {
const fs::path normal = withoutSeparator(destination);
const fs::path normal = WithoutSeparator(destination);
if (normal != outputRoot) {
placed.insert(normal.lexically_relative(outputRoot).begin()->string());
return;
@ -1222,22 +1288,36 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
cArchFlags += WasmCompileFlags(config);
}
for (const fs::path& cFile : config.cFiles) {
const std::string objPath = std::format("{}_source.o", (buildDir / cFile.filename()).string());
objects.emplace_back(objPath);
const fs::path object = layout.Object(cFile, ".c");
objects.push_back(object);
const std::string objPath = object.string();
const std::string srcPath = std::format("{}.c", cFile.string());
// C sources carry their dependencies entirely in #includes, so the
// depfile the compile below writes is the whole record of what this
// object was built from beyond the .c itself.
if (!fs::exists(objPath) || (fs::exists(srcPath) && fs::last_write_time(srcPath) > fs::last_write_time(objPath)) || NewestPrerequisite(std::format("{}.d", objPath)) > fs::last_write_time(objPath)) {
jobs.Add(std::format("Compiling {}.c", cFile.filename().string()), [command = std::format("{8} {0}.c --target={1}{2} -O3{3} -c{4}{5}{6} -MD -MF {7}_source.o.d -o {7}_source.o", cFile.string(), config.target, cArchFlags, ltoCompileFlags, includeFlags, defineFlags, userFlags, (buildDir / cFile.filename()).string(), GetToolchain().clang.string())] {
// The job makes the directory of its object, which is one for
// the source's own directory: the compiler makes none.
//
// The source, the dependency file and the object are quoted
// for the shell, as the CUDA compile below quotes its own. The
// object of a source outside the configuration's directory is
// named by that source's whole path, and so holds directories
// this command did not name while an object lay in the build
// directory itself: the working directory's, where the source
// is listed relative to it. A build from a directory with a
// space in its name would have stopped here.
jobs.Add(std::format("Compiling {}", layout.Shown(cFile, ".c")), [directory = object.parent_path(), command = std::format("{8} {0} --target={1}{2} -O3{3} -c{4}{5}{6} -MD -MF {7} -o {9}", ShellQuote(srcPath), config.target, cArchFlags, ltoCompileFlags, includeFlags, defineFlags, userFlags, ShellQuote(std::format("{}.d", objPath)), GetToolchain().clang.string(), ShellQuote(objPath))] {
fs::create_directories(directory);
return RunBuildCommand(command);
});
}
}
for (const fs::path& cFile : config.cuda) {
const std::string objPath = std::format("{}_source.o", (buildDir / cFile.filename()).string());
objects.emplace_back(objPath);
const fs::path object = layout.Object(cFile, ".cu");
objects.push_back(object);
const std::string objPath = object.string();
const std::string srcPath = std::format("{}.cu", cFile.string());
// nvcc writes the depfile clang writes for a C source (-MD -MF): the
// .cu and every header it read, the toolkit's and the system's among
@ -1249,10 +1329,11 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
if (!fs::exists(objPath) || (fs::exists(srcPath) && fs::last_write_time(srcPath) > fs::last_write_time(objPath)) || NewestPrerequisite(std::format("{}.d", objPath)) > fs::last_write_time(objPath)) {
const Toolchain& toolchain = GetToolchain();
if (toolchain.nvcc.empty()) {
jobs.Fail(std::format("cannot compile {}.cu: {}", cFile.filename().string(), toolchain.nvccError));
jobs.Fail(std::format("cannot compile {}: {}", layout.Shown(cFile, ".cu"), toolchain.nvccError));
break;
}
jobs.Add(std::format("Compiling {}.cu", cFile.filename().string()), [command = std::format("{} {} -c -MD -MF {} -o {} -O3 -arch={}", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(std::format("{}.d", objPath)), ShellQuote(objPath), ShellQuote(config.cudaArch))] {
jobs.Add(std::format("Compiling {}", layout.Shown(cFile, ".cu")), [directory = object.parent_path(), command = std::format("{} {} -c -MD -MF {} -o {} -O3 -arch={}", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(std::format("{}.d", objPath)), ShellQuote(objPath), ShellQuote(config.cudaArch))] {
fs::create_directories(directory);
return RunBuildCommand(command);
});
}
@ -1424,11 +1505,12 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
}
for(Implementation& implementation : config.implementations) {
if(implementation.Check(buildDir, pcmDir, externalFloor)) {
const fs::path object = layout.Object(implementation.path, ".cpp");
if(implementation.Check(object, pcmDir, externalFloor)) {
buildResult.repack = true;
implementation.Schedule(jobs, command, pcmDir, buildDir);
implementation.Schedule(jobs, command, pcmDir, buildDir, object, layout.Shown(implementation.path, ".cpp"));
}
objects.push_back(buildDir / std::format("{}_impl.o", implementation.path.filename().string()));
objects.push_back(object);
}
if (std::string failed = jobs.Wait(); !failed.empty()) {
@ -1524,48 +1606,20 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// after compile but before link): on the next run the .cpp is
// already ≤ .o so Implementation/Module Check returns false and
// nothing else would notice the archive is stale.
//
// Every object of the step is looked at, by the name the step
// itself is given. C and CUDA objects are archive members too,
// and their compile loop above is the only thing that knows it
// rebuilt one — it sets no repack flag, so without this a .c
// edit recompiled the object and then linked nothing.
if (!buildResult.repack) {
auto objNewer = [&](const fs::path& obj) {
for (const fs::path& object : objects) {
std::error_code ec;
auto t = fs::last_write_time(obj, ec);
return !ec && t > consumerMtime;
};
for (const std::unique_ptr<Module>& iface : config.interfaces) {
if (objNewer(buildDir / std::format("{}.o", iface->path.filename().string()))) {
const fs::file_time_type written = fs::last_write_time(object, ec);
if (!ec && written > consumerMtime) {
buildResult.repack = true;
break;
}
bool partHit = false;
for (const std::unique_ptr<ModulePartition>& part : iface->partitions) {
if (objNewer(buildDir / std::format("{}.o", part->path.filename().string()))) {
partHit = true;
break;
}
}
if (partHit) { buildResult.repack = true; break; }
}
if (!buildResult.repack) {
for (const Implementation& impl : config.implementations) {
if (objNewer(buildDir / std::format("{}_impl.o", impl.path.filename().string()))) {
buildResult.repack = true;
break;
}
}
}
// C and CUDA objects are archive members too, and their compile
// loop above is the only thing that knows it rebuilt one — it
// sets no repack flag, so without this a .c edit recompiled the
// object and then linked nothing.
if (!buildResult.repack) {
auto sourceObjNewer = [&](const std::vector<fs::path>& sources) {
for (const fs::path& source : sources) {
if (objNewer(buildDir / std::format("{}_source.o", source.filename().string()))) return true;
}
return false;
};
if (sourceObjNewer(config.cFiles) || sourceObjNewer(config.cuda)) {
buildResult.repack = true;
}
}
}
}