Crafter.Build/implementations/Crafter.Build-Implementation.cpp

57 lines
3 KiB
C++
Raw Permalink Normal View History

2026-07-23 01:24:42 +02:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2025-10-31 16:50:47 +01:00
module Crafter.Build:Implementation_impl;
import std;
import :Implementation;
2026-04-27 07:04:42 +02:00
import :Interface;
feat(build): run a build's steps on one worker per hardware thread Build() started a thread for every stale shader, asset, C, CUDA and C++ unit, and Module::Compile one more for every stale partition, and nothing bounded how many of them ran their step at once. Jorijn asked for the amount of parallelism to be limited by the amount of threads instead of just starting one per file; the threads are taken here to be the machine's hardware threads. The new :Jobs partition holds the two pieces. JobPool is worker threads behind one queue, started as jobs need them up to a fixed number, and JobPool::Process() is the pool of the process: at most a worker per hardware thread, one where std::thread::hardware_concurrency does not know. JobGroup is the jobs of one build on a pool. A job starts once the jobs it was added after have finished; the first job to fail keeps the ones that have not started from starting, and Wait() returns its error. No job waits for another job, and a JobGroup made on a worker of its own pool throws: a worker that waited for a job still in the queue would hold the thread that job needs. Build() adds what it did on those threads to a JobGroup instead, and the group's first error replaces the buildCancelled and buildError pair the threads set. The module units no longer block on the `compiled` flag of the units they import: Schedule() replaces Compile() on Module, ModulePartition and Implementation and adds a unit's jobs after the jobs that write the BMIs it imports. An interface unit is two jobs, its BMI and then its object from the BMI, so the units importing it start when the first is done, as they did when one thread ran both commands and raised the flag in between. A primary interface now waits for the BMIs of its partitions and no longer for their objects. Each of the two jobs has a progress line, "Precompiling X.cppm" and "Compiling X.cppm" for every interface unit, where a module and its partitions shared one "Compiling interface X". RunBuildCommand runs its command on a worker of the process's pool: on the calling thread when that is one, otherwise it waits for one. A link, an archive, the std module's precompile and project.cpp's compile are steps under the limit that way. The threads Build() starts per dependency and per external dependency stay: one of the first runs the dependency's own Build(), or waits for the thread that does, and one of the second runs git and cmake. With the BMI and the object as two jobs, a build that fails between them has written the BMI and skips the object, and Check, which reads staleness off the BMI, would call the unit built from then on. Schedule() therefore adds the object job alone for a unit whose BMI is up to date and whose object is missing or older than the BMI. A build interrupted between the two commands, or one whose second command failed, left the same behind before, and it stayed: the link took the object of the source before the edit, or failed on the missing one. ImportCycleError is now what keeps Schedule() from following a round of imports without end. The limit is on steps. Outside it, as AGENTS.template.md lists: an ExternalDependency's cmake --build, which keeps its own --parallel of as many jobs as there are hardware threads; the tests RunTests runs, --jobs= of them at once as before; the Crafter.Build modules EnsureCrafterBuildPcms precompiles one after another; and the threads a step starts for itself, which lld does for a link and Crafter.Asset's GDeflate compressor for an asset. The pool is one process's. The --help text says what a build runs at once and that test --jobs= counts tests. ConcurrentDependencyReset read the `compiled` flag, which is gone. It asserts on `checked` and `scheduled` instead, the state a reset from another build's thread would clear now, with a unit scheduled twice as the result, and the comment in its fixture says what the implementation unit comes after now. TransitiveInterfaceChange's comment names Schedule() where it named Compile() as what honours a partition's imports. Two comments in :Platform that spoke of worker threads, a word that now means the pool's, name the threads they mean. The bug-prover lens's section said shaders compile on a thread of their own each, and describes the pool now.
2026-11-13 17:59:55 +01:00
import :Jobs;
2026-04-27 07:04:42 +02:00
import :Platform;
2025-10-31 16:50:47 +01:00
namespace fs = std::filesystem;
namespace Crafter {
Implementation::Implementation(fs::path&& path) : path(std::move(path)) {
}
2026-04-27 07:04:42 +02:00
bool Implementation::Check(const fs::path& buildDir, const fs::path& pcmDir, fs::file_time_type sourceFloor) const {
2026-07-23 01:24:42 +02:00
std::string objPath = std::format("{}_impl.o", (buildDir/path.filename()).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)) {
2025-10-31 16:50:47 +01:00
return true;
}
2026-04-27 07:04:42 +02:00
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;
2025-10-31 16:50:47 +01:00
}
feat(build): run a build's steps on one worker per hardware thread Build() started a thread for every stale shader, asset, C, CUDA and C++ unit, and Module::Compile one more for every stale partition, and nothing bounded how many of them ran their step at once. Jorijn asked for the amount of parallelism to be limited by the amount of threads instead of just starting one per file; the threads are taken here to be the machine's hardware threads. The new :Jobs partition holds the two pieces. JobPool is worker threads behind one queue, started as jobs need them up to a fixed number, and JobPool::Process() is the pool of the process: at most a worker per hardware thread, one where std::thread::hardware_concurrency does not know. JobGroup is the jobs of one build on a pool. A job starts once the jobs it was added after have finished; the first job to fail keeps the ones that have not started from starting, and Wait() returns its error. No job waits for another job, and a JobGroup made on a worker of its own pool throws: a worker that waited for a job still in the queue would hold the thread that job needs. Build() adds what it did on those threads to a JobGroup instead, and the group's first error replaces the buildCancelled and buildError pair the threads set. The module units no longer block on the `compiled` flag of the units they import: Schedule() replaces Compile() on Module, ModulePartition and Implementation and adds a unit's jobs after the jobs that write the BMIs it imports. An interface unit is two jobs, its BMI and then its object from the BMI, so the units importing it start when the first is done, as they did when one thread ran both commands and raised the flag in between. A primary interface now waits for the BMIs of its partitions and no longer for their objects. Each of the two jobs has a progress line, "Precompiling X.cppm" and "Compiling X.cppm" for every interface unit, where a module and its partitions shared one "Compiling interface X". RunBuildCommand runs its command on a worker of the process's pool: on the calling thread when that is one, otherwise it waits for one. A link, an archive, the std module's precompile and project.cpp's compile are steps under the limit that way. The threads Build() starts per dependency and per external dependency stay: one of the first runs the dependency's own Build(), or waits for the thread that does, and one of the second runs git and cmake. With the BMI and the object as two jobs, a build that fails between them has written the BMI and skips the object, and Check, which reads staleness off the BMI, would call the unit built from then on. Schedule() therefore adds the object job alone for a unit whose BMI is up to date and whose object is missing or older than the BMI. A build interrupted between the two commands, or one whose second command failed, left the same behind before, and it stayed: the link took the object of the source before the edit, or failed on the missing one. ImportCycleError is now what keeps Schedule() from following a round of imports without end. The limit is on steps. Outside it, as AGENTS.template.md lists: an ExternalDependency's cmake --build, which keeps its own --parallel of as many jobs as there are hardware threads; the tests RunTests runs, --jobs= of them at once as before; the Crafter.Build modules EnsureCrafterBuildPcms precompiles one after another; and the threads a step starts for itself, which lld does for a link and Crafter.Asset's GDeflate compressor for an asset. The pool is one process's. The --help text says what a build runs at once and that test --jobs= counts tests. ConcurrentDependencyReset read the `compiled` flag, which is gone. It asserts on `checked` and `scheduled` instead, the state a reset from another build's thread would clear now, with a unit scheduled twice as the result, and the comment in its fixture says what the implementation unit comes after now. TransitiveInterfaceChange's comment names Schedule() where it named Compile() as what honours a partition's imports. Two comments in :Platform that spoke of worker threads, a word that now means the pool's, name the threads they mean. The bug-prover lens's section said shaders compile on a thread of their own each, and describes the pool now.
2026-11-13 17:59:55 +01:00
void Implementation::Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) const {
std::vector<JobGroup::Job> after;
2025-10-31 16:50:47 +01:00
for(ModulePartition* dependency : partitionDependencies) {
feat(build): run a build's steps on one worker per hardware thread Build() started a thread for every stale shader, asset, C, CUDA and C++ unit, and Module::Compile one more for every stale partition, and nothing bounded how many of them ran their step at once. Jorijn asked for the amount of parallelism to be limited by the amount of threads instead of just starting one per file; the threads are taken here to be the machine's hardware threads. The new :Jobs partition holds the two pieces. JobPool is worker threads behind one queue, started as jobs need them up to a fixed number, and JobPool::Process() is the pool of the process: at most a worker per hardware thread, one where std::thread::hardware_concurrency does not know. JobGroup is the jobs of one build on a pool. A job starts once the jobs it was added after have finished; the first job to fail keeps the ones that have not started from starting, and Wait() returns its error. No job waits for another job, and a JobGroup made on a worker of its own pool throws: a worker that waited for a job still in the queue would hold the thread that job needs. Build() adds what it did on those threads to a JobGroup instead, and the group's first error replaces the buildCancelled and buildError pair the threads set. The module units no longer block on the `compiled` flag of the units they import: Schedule() replaces Compile() on Module, ModulePartition and Implementation and adds a unit's jobs after the jobs that write the BMIs it imports. An interface unit is two jobs, its BMI and then its object from the BMI, so the units importing it start when the first is done, as they did when one thread ran both commands and raised the flag in between. A primary interface now waits for the BMIs of its partitions and no longer for their objects. Each of the two jobs has a progress line, "Precompiling X.cppm" and "Compiling X.cppm" for every interface unit, where a module and its partitions shared one "Compiling interface X". RunBuildCommand runs its command on a worker of the process's pool: on the calling thread when that is one, otherwise it waits for one. A link, an archive, the std module's precompile and project.cpp's compile are steps under the limit that way. The threads Build() starts per dependency and per external dependency stay: one of the first runs the dependency's own Build(), or waits for the thread that does, and one of the second runs git and cmake. With the BMI and the object as two jobs, a build that fails between them has written the BMI and skips the object, and Check, which reads staleness off the BMI, would call the unit built from then on. Schedule() therefore adds the object job alone for a unit whose BMI is up to date and whose object is missing or older than the BMI. A build interrupted between the two commands, or one whose second command failed, left the same behind before, and it stayed: the link took the object of the source before the edit, or failed on the missing one. ImportCycleError is now what keeps Schedule() from following a round of imports without end. The limit is on steps. Outside it, as AGENTS.template.md lists: an ExternalDependency's cmake --build, which keeps its own --parallel of as many jobs as there are hardware threads; the tests RunTests runs, --jobs= of them at once as before; the Crafter.Build modules EnsureCrafterBuildPcms precompiles one after another; and the threads a step starts for itself, which lld does for a link and Crafter.Asset's GDeflate compressor for an asset. The pool is one process's. The --help text says what a build runs at once and that test --jobs= counts tests. ConcurrentDependencyReset read the `compiled` flag, which is gone. It asserts on `checked` and `scheduled` instead, the state a reset from another build's thread would clear now, with a unit scheduled twice as the result, and the comment in its fixture says what the implementation unit comes after now. TransitiveInterfaceChange's comment names Schedule() where it named Compile() as what honours a partition's imports. Two comments in :Platform that spoke of worker threads, a word that now means the pool's, name the threads they mean. The bug-prover lens's section said shaders compile on a thread of their own each, and describes the pool now.
2026-11-13 17:59:55 +01:00
if (const std::optional<JobGroup::Job> job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job);
2025-10-31 16:50:47 +01:00
}
for(Module* dependency : moduleDependencies) {
feat(build): run a build's steps on one worker per hardware thread Build() started a thread for every stale shader, asset, C, CUDA and C++ unit, and Module::Compile one more for every stale partition, and nothing bounded how many of them ran their step at once. Jorijn asked for the amount of parallelism to be limited by the amount of threads instead of just starting one per file; the threads are taken here to be the machine's hardware threads. The new :Jobs partition holds the two pieces. JobPool is worker threads behind one queue, started as jobs need them up to a fixed number, and JobPool::Process() is the pool of the process: at most a worker per hardware thread, one where std::thread::hardware_concurrency does not know. JobGroup is the jobs of one build on a pool. A job starts once the jobs it was added after have finished; the first job to fail keeps the ones that have not started from starting, and Wait() returns its error. No job waits for another job, and a JobGroup made on a worker of its own pool throws: a worker that waited for a job still in the queue would hold the thread that job needs. Build() adds what it did on those threads to a JobGroup instead, and the group's first error replaces the buildCancelled and buildError pair the threads set. The module units no longer block on the `compiled` flag of the units they import: Schedule() replaces Compile() on Module, ModulePartition and Implementation and adds a unit's jobs after the jobs that write the BMIs it imports. An interface unit is two jobs, its BMI and then its object from the BMI, so the units importing it start when the first is done, as they did when one thread ran both commands and raised the flag in between. A primary interface now waits for the BMIs of its partitions and no longer for their objects. Each of the two jobs has a progress line, "Precompiling X.cppm" and "Compiling X.cppm" for every interface unit, where a module and its partitions shared one "Compiling interface X". RunBuildCommand runs its command on a worker of the process's pool: on the calling thread when that is one, otherwise it waits for one. A link, an archive, the std module's precompile and project.cpp's compile are steps under the limit that way. The threads Build() starts per dependency and per external dependency stay: one of the first runs the dependency's own Build(), or waits for the thread that does, and one of the second runs git and cmake. With the BMI and the object as two jobs, a build that fails between them has written the BMI and skips the object, and Check, which reads staleness off the BMI, would call the unit built from then on. Schedule() therefore adds the object job alone for a unit whose BMI is up to date and whose object is missing or older than the BMI. A build interrupted between the two commands, or one whose second command failed, left the same behind before, and it stayed: the link took the object of the source before the edit, or failed on the missing one. ImportCycleError is now what keeps Schedule() from following a round of imports without end. The limit is on steps. Outside it, as AGENTS.template.md lists: an ExternalDependency's cmake --build, which keeps its own --parallel of as many jobs as there are hardware threads; the tests RunTests runs, --jobs= of them at once as before; the Crafter.Build modules EnsureCrafterBuildPcms precompiles one after another; and the threads a step starts for itself, which lld does for a link and Crafter.Asset's GDeflate compressor for an asset. The pool is one process's. The --help text says what a build runs at once and that test --jobs= counts tests. ConcurrentDependencyReset read the `compiled` flag, which is gone. It asserts on `checked` and `scheduled` instead, the state a reset from another build's thread would clear now, with a unit scheduled twice as the result, and the comment in its fixture says what the implementation unit comes after now. TransitiveInterfaceChange's comment names Schedule() where it named Compile() as what honours a partition's imports. Two comments in :Platform that spoke of worker threads, a word that now means the pool's, name the threads they mean. The bug-prover lens's section said shaders compile on a thread of their own each, and describes the pool now.
2026-11-13 17:59:55 +01:00
if (const std::optional<JobGroup::Job> job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job);
2025-10-31 16:50:47 +01:00
}
// -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.
feat(build): run a build's steps on one worker per hardware thread Build() started a thread for every stale shader, asset, C, CUDA and C++ unit, and Module::Compile one more for every stale partition, and nothing bounded how many of them ran their step at once. Jorijn asked for the amount of parallelism to be limited by the amount of threads instead of just starting one per file; the threads are taken here to be the machine's hardware threads. The new :Jobs partition holds the two pieces. JobPool is worker threads behind one queue, started as jobs need them up to a fixed number, and JobPool::Process() is the pool of the process: at most a worker per hardware thread, one where std::thread::hardware_concurrency does not know. JobGroup is the jobs of one build on a pool. A job starts once the jobs it was added after have finished; the first job to fail keeps the ones that have not started from starting, and Wait() returns its error. No job waits for another job, and a JobGroup made on a worker of its own pool throws: a worker that waited for a job still in the queue would hold the thread that job needs. Build() adds what it did on those threads to a JobGroup instead, and the group's first error replaces the buildCancelled and buildError pair the threads set. The module units no longer block on the `compiled` flag of the units they import: Schedule() replaces Compile() on Module, ModulePartition and Implementation and adds a unit's jobs after the jobs that write the BMIs it imports. An interface unit is two jobs, its BMI and then its object from the BMI, so the units importing it start when the first is done, as they did when one thread ran both commands and raised the flag in between. A primary interface now waits for the BMIs of its partitions and no longer for their objects. Each of the two jobs has a progress line, "Precompiling X.cppm" and "Compiling X.cppm" for every interface unit, where a module and its partitions shared one "Compiling interface X". RunBuildCommand runs its command on a worker of the process's pool: on the calling thread when that is one, otherwise it waits for one. A link, an archive, the std module's precompile and project.cpp's compile are steps under the limit that way. The threads Build() starts per dependency and per external dependency stay: one of the first runs the dependency's own Build(), or waits for the thread that does, and one of the second runs git and cmake. With the BMI and the object as two jobs, a build that fails between them has written the BMI and skips the object, and Check, which reads staleness off the BMI, would call the unit built from then on. Schedule() therefore adds the object job alone for a unit whose BMI is up to date and whose object is missing or older than the BMI. A build interrupted between the two commands, or one whose second command failed, left the same behind before, and it stayed: the link took the object of the source before the edit, or failed on the missing one. ImportCycleError is now what keeps Schedule() from following a round of imports without end. The limit is on steps. Outside it, as AGENTS.template.md lists: an ExternalDependency's cmake --build, which keeps its own --parallel of as many jobs as there are hardware threads; the tests RunTests runs, --jobs= of them at once as before; the Crafter.Build modules EnsureCrafterBuildPcms precompiles one after another; and the threads a step starts for itself, which lld does for a link and Crafter.Asset's GDeflate compressor for an asset. The pool is one process's. The --help text says what a build runs at once and that test --jobs= counts tests. ConcurrentDependencyReset read the `compiled` flag, which is gone. It asserts on `checked` and `scheduled` instead, the state a reset from another build's thread would clear now, with a unit scheduled twice as the result, and the comment in its fixture says what the implementation unit comes after now. TransitiveInterfaceChange's comment names Schedule() where it named Compile() as what honours a partition's imports. Two comments in :Platform that spoke of worker threads, a word that now means the pool's, name the threads they mean. The bug-prover lens's section said shaders compile on a thread of their own each, and describes the pool now.
2026-11-13 17:59:55 +01:00
jobs.Add(std::format("Compiling {}.cpp", path.filename().string()), [command = std::format("{0} {1}.cpp -c -MD -MF {2}_impl.o.d -fmodule-file-deps -o {2}_impl.o", clang, path.string(), (buildDir/path.filename()).string())] {
return RunBuildCommand(command);
}, after);
2025-10-31 16:50:47 +01:00
}
}