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
61 lines
3.1 KiB
C++
61 lines
3.1 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module Crafter.Build:Implementation_impl;
|
|
import std;
|
|
import :Implementation;
|
|
import :Interface;
|
|
import :Jobs;
|
|
import :Platform;
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace Crafter {
|
|
Implementation::Implementation(fs::path&& path) : path(std::move(path)) {
|
|
|
|
}
|
|
bool Implementation::Check(const fs::path& object, const fs::path& pcmDir, fs::file_time_type sourceFloor) const {
|
|
std::string objPath = object.string();
|
|
std::string cppPath = std::format("{}.cpp", path.string());
|
|
// The depfile clang wrote beside the object lists the headers this TU
|
|
// included; nothing else in the configuration knows about them, since
|
|
// the source scanner only reads `import` lines.
|
|
if(!fs::exists(objPath) || std::max({fs::last_write_time(cppPath), sourceFloor, NewestPrerequisite(std::format("{}.d", objPath))}) >= fs::last_write_time(objPath)) {
|
|
return true;
|
|
}
|
|
fs::file_time_type objTime = fs::last_write_time(objPath);
|
|
for(ModulePartition* dependency : partitionDependencies) {
|
|
if(dependency->Check(pcmDir, sourceFloor)) return true;
|
|
}
|
|
for(Module* dependency : moduleDependencies) {
|
|
if(dependency->Check(pcmDir, sourceFloor)) return true;
|
|
}
|
|
for(const auto& [externalMod, externalPcmPath] : externalModuleDependencies) {
|
|
std::error_code ec;
|
|
fs::file_time_type pcmTime = fs::last_write_time(externalPcmPath, ec);
|
|
if (!ec && pcmTime >= objTime) return true;
|
|
}
|
|
return false;
|
|
}
|
|
void Implementation::Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, const fs::path& object, std::string_view shown) const {
|
|
std::vector<JobGroup::Job> after;
|
|
for(ModulePartition* dependency : partitionDependencies) {
|
|
if (const std::optional<JobGroup::Job> job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job);
|
|
}
|
|
for(Module* dependency : moduleDependencies) {
|
|
if (const std::optional<JobGroup::Job> job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job);
|
|
}
|
|
// -MD leaves <name>_impl.o.d beside the object for Check to read, and
|
|
// -fmodule-file-deps puts the BMIs the unit imported in it with the
|
|
// headers. The driver adds that for --precompile only, so a std
|
|
// module rebuilt in place, for a mingw-w64 or libc++ update under
|
|
// the same compiler, left the objects importing it as they were, and
|
|
// the next build only relinked them.
|
|
//
|
|
// The job makes the directory of its object, which is one for the
|
|
// source's own directory (ObjectPath): the compiler makes none.
|
|
jobs.Add(std::format("Compiling {}", shown), [directory = object.parent_path(), command = std::format("{0} {1}.cpp -c -MD -MF {2}.d -fmodule-file-deps -o {2}", clang, path.string(), object.string())] {
|
|
fs::create_directories(directory);
|
|
return RunBuildCommand(command);
|
|
}, after);
|
|
}
|
|
}
|