RunCommand merged stderr into stdout, dropped pclose's status and returned the text; every compile, link and archive site then read "printed something" as "failed". A warning is printing something, so a translation unit that warned failed the build — but only on the run that actually recompiled it, since warnings aren't re-emitted for an object that's already up to date. The same unchanged source therefore passed or failed depending on the state of the build tree: flaky-looking tests locally, and a cold CI checkout surfacing every latent warning in a project at once as unrelated failures. RunCommand is gone, replaced by RunBuildCommand: it goes through RunCommandChecked, returns "" when the command exited 0 (so every caller's `if (!result.empty())` error path is unchanged) and hands any warnings to the new Progress::Diagnostic instead of to the error path. That also closes the quiet half of the bug — a compiler killed by the OOM killer prints nothing, so it used to read as success and leave the build carrying on with a missing object; it now reports the signal that killed it. Warnings are now shown rather than swallowed, which they weren't in either direction before: invisible on an incremental build, fatal on a cold one. Failing on them stays a project's choice, via -Werror in compileFlags.
230 lines
10 KiB
C++
230 lines
10 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module Crafter.Build:Interface_impl;
|
|
import std;
|
|
import :Interface;
|
|
import :Platform;
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace Crafter {
|
|
ModulePartition::ModulePartition(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
|
|
|
|
bool ModulePartition::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) {
|
|
if(!checked) {
|
|
checked = true;
|
|
std::string pcmPath = std::format("{}.pcm", (pcmDir/path.filename()).generic_string());
|
|
std::string cppmPath = std::format("{}.cppm", path.generic_string());
|
|
// NewestPrerequisite covers the headers this partition #includes —
|
|
// they are inputs to the BMI just as much as the .cppm is, and the
|
|
// .cppm's own mtime says nothing about them. It only ever bounds
|
|
// *this* artifact, so it goes in the comparison below and not into
|
|
// the `sourceFloor` handed to the recursive Checks: each of those
|
|
// reads its own depfile.
|
|
if(fs::exists(pcmPath) && std::max({fs::last_write_time(cppmPath), sourceFloor, NewestPrerequisite(std::format("{}.d", pcmPath))}) < fs::last_write_time(pcmPath)) {
|
|
fs::file_time_type pcmTime = fs::last_write_time(pcmPath);
|
|
for(ModulePartition* dependency : partitionDependencies) {
|
|
if(dependency->Check(pcmDir, sourceFloor)) {
|
|
needsRecompiling = true;
|
|
return true;
|
|
}
|
|
}
|
|
for(Module* dependency : moduleDependencies) {
|
|
if(dependency->Check(pcmDir, sourceFloor)) {
|
|
needsRecompiling = true;
|
|
return true;
|
|
}
|
|
}
|
|
for(const auto& [externalMod, externalPcmPath] : externalModuleDependencies) {
|
|
std::error_code ec;
|
|
fs::file_time_type t = fs::last_write_time(externalPcmPath, ec);
|
|
if (!ec && t >= pcmTime) {
|
|
needsRecompiling = true;
|
|
return true;
|
|
}
|
|
}
|
|
needsRecompiling = false;
|
|
compiled.store(true);
|
|
// Nothing waits on `compiled` until the compile threads start,
|
|
// which is after every Check has run — but notify anyway so the
|
|
// flag is never left set without a wake-up behind it.
|
|
compiled.notify_all();
|
|
return false;
|
|
} else {
|
|
needsRecompiling = true;
|
|
return true;
|
|
}
|
|
} else {
|
|
return needsRecompiling;
|
|
}
|
|
}
|
|
|
|
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError) {
|
|
for(ModulePartition* dependency : partitionDependencies) {
|
|
if(!dependency->compiled.load()) {
|
|
dependency->compiled.wait(false);
|
|
}
|
|
}
|
|
for(Module* dependency : moduleDependencies) {
|
|
if(!dependency->compiled.load()) {
|
|
dependency->compiled.wait(false);
|
|
}
|
|
}
|
|
|
|
if (buildCancelled.load(std::memory_order_relaxed)) {
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
return;
|
|
}
|
|
|
|
// -MD records every header the preamble pulled in, next to the BMI as
|
|
// <name>.pcm.d, so the next Check can see an edit to one of them.
|
|
std::string result = RunBuildCommand(std::format("{0} {1}.cppm --precompile -MD -MF {2}.pcm.d -o {2}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
|
|
|
|
if (!result.empty()) {
|
|
bool expected = false;
|
|
if (buildCancelled.compare_exchange_strong(expected, true)) {
|
|
buildError = std::move(result);
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
return;
|
|
}
|
|
}
|
|
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
|
|
result = RunBuildCommand(std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
|
|
|
|
if (!result.empty()) {
|
|
bool expected = false;
|
|
if (buildCancelled.compare_exchange_strong(expected, true)) {
|
|
buildError = std::move(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
|
|
|
|
bool Module::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) {
|
|
if(!checked) {
|
|
checked = true;
|
|
std::string pcmPath = std::format("{}.pcm", (pcmDir/path.filename()).generic_string());
|
|
std::string cppmPath = std::format("{}.cppm", path.generic_string());
|
|
// See ModulePartition::Check — the depfile clang wrote beside the BMI
|
|
// is the only record of which headers this interface unit included.
|
|
if(fs::exists(pcmPath) && std::max({fs::last_write_time(cppmPath), sourceFloor, NewestPrerequisite(std::format("{}.d", pcmPath))}) < fs::last_write_time(pcmPath)) {
|
|
fs::file_time_type pcmTime = fs::last_write_time(pcmPath);
|
|
// Every partition gets Check()ed even once one is known stale:
|
|
// Compile() drives partition rebuilds off their own
|
|
// needsRecompiling flags, so short-circuiting here would leave
|
|
// the later ones unevaluated and silently unbuilt.
|
|
bool depCheck = false;
|
|
for(std::unique_ptr<ModulePartition>& partition : partitions) {
|
|
if(partition->Check(pcmDir, sourceFloor)) {
|
|
depCheck = true;
|
|
}
|
|
}
|
|
// Modules this interface unit imports directly. Local ones share
|
|
// our pcmDir and are resolved recursively; external ones are
|
|
// compared by PCM mtime, their owning Configuration having
|
|
// already finished building by the time we run.
|
|
for(Module* dependency : moduleDependencies) {
|
|
if(dependency->Check(pcmDir, sourceFloor)) {
|
|
depCheck = true;
|
|
}
|
|
}
|
|
if(!depCheck) {
|
|
for(const auto& [externalMod, externalPcmPath] : externalModuleDependencies) {
|
|
std::error_code ec;
|
|
fs::file_time_type t = fs::last_write_time(externalPcmPath, ec);
|
|
if (!ec && t >= pcmTime) {
|
|
depCheck = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if(depCheck) {
|
|
needsRecompiling = true;
|
|
return true;
|
|
} else {
|
|
needsRecompiling = false;
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
return false;
|
|
}
|
|
} else {
|
|
// Already known stale, but the dependencies still need
|
|
// evaluating: an unchecked local module would never get built,
|
|
// and Compile() below waits on its `compiled` flag.
|
|
for(std::unique_ptr<ModulePartition>& partition : partitions) {
|
|
partition->Check(pcmDir, sourceFloor);
|
|
}
|
|
for(Module* dependency : moduleDependencies) {
|
|
dependency->Check(pcmDir, sourceFloor);
|
|
}
|
|
needsRecompiling = true;
|
|
return true;
|
|
}
|
|
} else {
|
|
return needsRecompiling;
|
|
}
|
|
}
|
|
|
|
void Module::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError) {
|
|
// A sibling module in the same Configuration that this interface unit
|
|
// imports has to have its PCM on disk before we precompile against it.
|
|
// Safe to block here: Build() Checks every interface before it spawns
|
|
// any compile thread, so a dependency that needs no rebuild already has
|
|
// `compiled` set and this returns immediately.
|
|
for(Module* dependency : moduleDependencies) {
|
|
if(!dependency->compiled.load()) {
|
|
dependency->compiled.wait(false);
|
|
}
|
|
}
|
|
|
|
std::vector<std::thread> threads;
|
|
threads.reserve(partitions.size());
|
|
for(std::unique_ptr<ModulePartition>& part : partitions) {
|
|
if(part->needsRecompiling) {
|
|
threads.emplace_back(&ModulePartition::Compile, part.get(), clang, pcmDir, buildDir, std::ref(buildCancelled), std::ref(buildError));
|
|
}
|
|
}
|
|
|
|
for(std::thread& thread : threads){
|
|
thread.join();
|
|
}
|
|
|
|
if (buildCancelled.load(std::memory_order_relaxed)) {
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
return;
|
|
}
|
|
|
|
// -MD records every header the preamble pulled in, next to the BMI as
|
|
// <name>.pcm.d, so the next Check can see an edit to one of them.
|
|
std::string result = RunBuildCommand(std::format("{0} {1}.cppm --precompile -MD -MF {2}.pcm.d -o {2}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
|
|
|
|
if (!result.empty()) {
|
|
bool expected = false;
|
|
if (buildCancelled.compare_exchange_strong(expected, true)) {
|
|
buildError = std::move(result);
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
return;
|
|
}
|
|
}
|
|
|
|
compiled.store(true);
|
|
compiled.notify_all();
|
|
|
|
result = RunBuildCommand(std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
|
|
|
|
if (!result.empty()) {
|
|
bool expected = false;
|
|
if (buildCancelled.compare_exchange_strong(expected, true)) {
|
|
buildError = std::move(result);
|
|
}
|
|
}
|
|
}
|
|
}
|