Crafter.Build/interfaces/Crafter.Build-Interface.cppm

85 lines
4.4 KiB
Text
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
2026-04-23 01:57:25 +02:00
export module Crafter.Build:Interface;
2025-10-31 16:50:47 +01:00
import std;
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;
2025-10-31 16:50:47 +01:00
namespace fs = std::filesystem;
namespace Crafter {
export class Module;
export class ModulePartition {
public:
std::vector<Module*> moduleDependencies;
std::vector<ModulePartition*> partitionDependencies;
2026-04-27 07:04:42 +02:00
std::vector<std::pair<Module*, fs::path>> externalModuleDependencies;
// Names from `import X;` that matched neither a module in this
// Configuration nor one reachable through its dependencies at the time
// the source was scanned. Retried by ResolvePendingImports (see
// Crafter.Build:Clang) so a dependency wired up after
// GetInterfacesAndImplementations still produces a staleness edge.
// Names that never resolve (`std`, module-mapped externals) stay here.
std::vector<std::string> pendingImports;
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
// The state of one Build(), which clears it before it checks: whether
// Check has run, what it found, whether Schedule has added this
// unit's jobs, and the one among them that writes its BMI, which is
// the job the units importing it come after.
fix: track what a primary module interface imports A primary module interface unit — `export module Widget;`, no partitions — recorded nothing about what it imported. GetInterfacesAndImplementations registered the Module and then erased the file from the scan list, so the import pass only ever saw partitions, and Module had no vectors to hold an edge anyway. Two consequences, both reported as issue #26: Module::Check consulted only its own .cppm and its partitions. A data member added to an imported module left Widget.pcm, Widget.o and every consumer object untouched while the imported library rebuilt and both binaries relinked — one executable holding two class layouts, no diagnostic, and a crash somewhere unrelated. Wiping build/ was the only cure, so `crafter-build test` could not be trusted straight after an interface edit. Module::Compile waited on nothing. Two modules in one Configuration compile on concurrent threads, so a primary interface importing a sibling was a coin flip between working and "module 'Base' not found". Partitions never had either problem — they carry the same three vectors and Check/Compile honour them — which is why the gap only surfaced on a module whose interface is one flat unit. Module now carries moduleDependencies, externalModuleDependencies and pendingImports with the same meanings as on ModulePartition; primary units stay in the scan list so their imports land there; Check sees through them; Compile orders itself behind a local sibling; and ResolvePendingImports sweeps them so an edge survives dependencies being wired up afterwards. Build() now Checks every interface before spawning any compile thread — the `compiled` flag a waiter blocks on is raised either by a Compile that runs or by the Check that decides none is needed, so a Check still pending while another module's thread waits would have hung the build. Resolves #26 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-30 18:19:04 +00:00
bool needsRecompiling = false;
2025-10-31 16:50:47 +01:00
bool checked = false;
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
bool scheduled = false;
std::optional<JobGroup::Job> precompileJob;
2025-10-31 16:50:47 +01:00
std::string name;
fs::path path;
refactor: drop Windows host support crafter-build is moving to a single Flatpak distribution, and there is no Windows host in that picture: no build.cmd, no MSVC or mingw host build, no DLL plus launcher. Delete every host-Windows path instead of leaving it to rot unverified: the cmd.exe command runner and quoting, the LOCALAPPDATA cache, the MSVC and mingw-host LoadProject variants with their LIBCXX_DIR and CRAFTER_MINGW_DIR plumbing, the winsock port probe, the LoadLibrary libclang loader, the console-width and isatty shims, and the preprocessor guards around the Linux bodies. The x86_64-w64-mingw32 cross-compile target stays. It is target logic (output names, -femulated-tls, the libstdc++ link extras, copying a dependency DLL next to a mingw exe, wine as the runner) and did not depend on any of the removed code. The msvc target goes with the host that was the only thing able to use it. Three target-path warts fixed on the way: ExpectedOutput named a mingw static library <name>.lib while the archive step wrote lib<name>.a, so those libraries relinked on every run; the import lib is no longer copied next to a mingw exe, since the dependency's own bin dir is already on the link line; and llvm-ar now writes every archive, not only the LTO ones, so Debug and Release archives are made the same way. CRAFTER_API and Crafter.Build-Api.h existed only for the DLL boundary. Nothing downstream used them, so they are removed rather than kept as an empty macro nobody can verify. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-02 21:56:38 +02:00
ModulePartition(std::string&& name, fs::path&& path);
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
// Whether this unit's BMI has to be written again. The object made
// from the BMI is not Check's to judge: see Schedule.
refactor: drop Windows host support crafter-build is moving to a single Flatpak distribution, and there is no Windows host in that picture: no build.cmd, no MSVC or mingw host build, no DLL plus launcher. Delete every host-Windows path instead of leaving it to rot unverified: the cmd.exe command runner and quoting, the LOCALAPPDATA cache, the MSVC and mingw-host LoadProject variants with their LIBCXX_DIR and CRAFTER_MINGW_DIR plumbing, the winsock port probe, the LoadLibrary libclang loader, the console-width and isatty shims, and the preprocessor guards around the Linux bodies. The x86_64-w64-mingw32 cross-compile target stays. It is target logic (output names, -femulated-tls, the libstdc++ link extras, copying a dependency DLL next to a mingw exe, wine as the runner) and did not depend on any of the removed code. The msvc target goes with the host that was the only thing able to use it. Three target-path warts fixed on the way: ExpectedOutput named a mingw static library <name>.lib while the archive step wrote lib<name>.a, so those libraries relinked on every run; the import lib is no longer copied next to a mingw exe, since the dependency's own bin dir is already on the link line; and llvm-ar now writes every archive, not only the LTO ones, so Debug and Release archives are made the same way. CRAFTER_API and Crafter.Build-Api.h existed only for the DLL boundary. Nothing downstream used them, so they are removed rather than kept as an empty macro nobody can verify. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-02 21:56:38 +02:00
bool Check(const fs::path& pcmDir, fs::file_time_type sourceFloor = fs::file_time_type::min());
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
// Adds the jobs this unit needs to `jobs`, once however often it is
// called, and returns the one that writes its BMI, if there is one.
//
// A unit Check found stale gets two, one for each run of `clang`: the
// BMI, after the BMIs of the stale units this one imports, which are
// scheduled here as well, and then the object from that BMI, which
// only the link waits for, so that the units importing this one start
// when the first is done.
//
// A unit whose BMI is up to date gets the second job alone when its
// object is missing or older than the BMI. A build that fails or is
// interrupted between the two leaves a unit like that, and by its BMI
// alone every build after it would call the unit built: the link then
// took the object of the source before the edit, or found none.
//
// Every unit this one imports must have been checked: Build() checks
// all of a configuration's interfaces before it schedules any.
std::optional<JobGroup::Job> Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir);
2025-10-31 16:50:47 +01:00
};
export class Module {
public:
fix: track what a primary module interface imports A primary module interface unit — `export module Widget;`, no partitions — recorded nothing about what it imported. GetInterfacesAndImplementations registered the Module and then erased the file from the scan list, so the import pass only ever saw partitions, and Module had no vectors to hold an edge anyway. Two consequences, both reported as issue #26: Module::Check consulted only its own .cppm and its partitions. A data member added to an imported module left Widget.pcm, Widget.o and every consumer object untouched while the imported library rebuilt and both binaries relinked — one executable holding two class layouts, no diagnostic, and a crash somewhere unrelated. Wiping build/ was the only cure, so `crafter-build test` could not be trusted straight after an interface edit. Module::Compile waited on nothing. Two modules in one Configuration compile on concurrent threads, so a primary interface importing a sibling was a coin flip between working and "module 'Base' not found". Partitions never had either problem — they carry the same three vectors and Check/Compile honour them — which is why the gap only surfaced on a module whose interface is one flat unit. Module now carries moduleDependencies, externalModuleDependencies and pendingImports with the same meanings as on ModulePartition; primary units stay in the scan list so their imports land there; Check sees through them; Compile orders itself behind a local sibling; and ResolvePendingImports sweeps them so an edge survives dependencies being wired up afterwards. Build() now Checks every interface before spawning any compile thread — the `compiled` flag a waiter blocks on is raised either by a Compile that runs or by the Check that decides none is needed, so a Check still pending while another module's thread waits would have hung the build. Resolves #26 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-30 18:19:04 +00:00
// A primary module interface unit imports things too — `import Base;`
// in `export module Widget;` is as much a layout dependency as the
// same line in a partition. Recorded on the same three vectors, with
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
// the same meanings, so Check() can see through them and Schedule()
fix: track what a primary module interface imports A primary module interface unit — `export module Widget;`, no partitions — recorded nothing about what it imported. GetInterfacesAndImplementations registered the Module and then erased the file from the scan list, so the import pass only ever saw partitions, and Module had no vectors to hold an edge anyway. Two consequences, both reported as issue #26: Module::Check consulted only its own .cppm and its partitions. A data member added to an imported module left Widget.pcm, Widget.o and every consumer object untouched while the imported library rebuilt and both binaries relinked — one executable holding two class layouts, no diagnostic, and a crash somewhere unrelated. Wiping build/ was the only cure, so `crafter-build test` could not be trusted straight after an interface edit. Module::Compile waited on nothing. Two modules in one Configuration compile on concurrent threads, so a primary interface importing a sibling was a coin flip between working and "module 'Base' not found". Partitions never had either problem — they carry the same three vectors and Check/Compile honour them — which is why the gap only surfaced on a module whose interface is one flat unit. Module now carries moduleDependencies, externalModuleDependencies and pendingImports with the same meanings as on ModulePartition; primary units stay in the scan list so their imports land there; Check sees through them; Compile orders itself behind a local sibling; and ResolvePendingImports sweeps them so an edge survives dependencies being wired up afterwards. Build() now Checks every interface before spawning any compile thread — the `compiled` flag a waiter blocks on is raised either by a Compile that runs or by the Check that decides none is needed, so a Check still pending while another module's thread waits would have hung the build. Resolves #26 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-30 18:19:04 +00:00
// can order itself behind a sibling module in the same Configuration
// (issue #26).
std::vector<Module*> moduleDependencies;
std::vector<std::pair<Module*, fs::path>> externalModuleDependencies;
std::vector<std::string> pendingImports;
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
// As in ModulePartition.
fix: track what a primary module interface imports A primary module interface unit — `export module Widget;`, no partitions — recorded nothing about what it imported. GetInterfacesAndImplementations registered the Module and then erased the file from the scan list, so the import pass only ever saw partitions, and Module had no vectors to hold an edge anyway. Two consequences, both reported as issue #26: Module::Check consulted only its own .cppm and its partitions. A data member added to an imported module left Widget.pcm, Widget.o and every consumer object untouched while the imported library rebuilt and both binaries relinked — one executable holding two class layouts, no diagnostic, and a crash somewhere unrelated. Wiping build/ was the only cure, so `crafter-build test` could not be trusted straight after an interface edit. Module::Compile waited on nothing. Two modules in one Configuration compile on concurrent threads, so a primary interface importing a sibling was a coin flip between working and "module 'Base' not found". Partitions never had either problem — they carry the same three vectors and Check/Compile honour them — which is why the gap only surfaced on a module whose interface is one flat unit. Module now carries moduleDependencies, externalModuleDependencies and pendingImports with the same meanings as on ModulePartition; primary units stay in the scan list so their imports land there; Check sees through them; Compile orders itself behind a local sibling; and ResolvePendingImports sweeps them so an edge survives dependencies being wired up afterwards. Build() now Checks every interface before spawning any compile thread — the `compiled` flag a waiter blocks on is raised either by a Compile that runs or by the Check that decides none is needed, so a Check still pending while another module's thread waits would have hung the build. Resolves #26 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-30 18:19:04 +00:00
bool needsRecompiling = false;
2025-10-31 16:50:47 +01:00
bool checked = false;
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
bool scheduled = false;
std::optional<JobGroup::Job> precompileJob;
2025-10-31 16:50:47 +01:00
std::vector<std::unique_ptr<ModulePartition>> partitions;
std::string name;
fs::path path;
refactor: drop Windows host support crafter-build is moving to a single Flatpak distribution, and there is no Windows host in that picture: no build.cmd, no MSVC or mingw host build, no DLL plus launcher. Delete every host-Windows path instead of leaving it to rot unverified: the cmd.exe command runner and quoting, the LOCALAPPDATA cache, the MSVC and mingw-host LoadProject variants with their LIBCXX_DIR and CRAFTER_MINGW_DIR plumbing, the winsock port probe, the LoadLibrary libclang loader, the console-width and isatty shims, and the preprocessor guards around the Linux bodies. The x86_64-w64-mingw32 cross-compile target stays. It is target logic (output names, -femulated-tls, the libstdc++ link extras, copying a dependency DLL next to a mingw exe, wine as the runner) and did not depend on any of the removed code. The msvc target goes with the host that was the only thing able to use it. Three target-path warts fixed on the way: ExpectedOutput named a mingw static library <name>.lib while the archive step wrote lib<name>.a, so those libraries relinked on every run; the import lib is no longer copied next to a mingw exe, since the dependency's own bin dir is already on the link line; and llvm-ar now writes every archive, not only the LTO ones, so Debug and Release archives are made the same way. CRAFTER_API and Crafter.Build-Api.h existed only for the DLL boundary. Nothing downstream used them, so they are removed rather than kept as an empty macro nobody can verify. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-02 21:56:38 +02:00
Module(std::string&& name, fs::path&& path);
bool Check(const fs::path& pcmDir, fs::file_time_type sourceFloor = fs::file_time_type::min());
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
// As ModulePartition::Schedule, for the primary interface, and
// schedules every partition of the module with it. The primary
// interface comes after every stale partition of the module,
// whichever of them it imports, and after the stale sibling modules
// it imports.
std::optional<JobGroup::Job> Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir);
2025-10-31 16:50:47 +01:00
};
}