Crafter.Build/interfaces/Crafter.Build-Implementation.cppm

32 lines
1.5 KiB
Text
Raw 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
export module Crafter.Build:Implementation;
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;
export class Implementation {
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;
// See ModulePartition::pendingImports — imports this TU declared that
// no reachable Configuration provided when the source was scanned.
std::vector<std::string> pendingImports;
2025-10-31 16:50:47 +01:00
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
Implementation(fs::path&& path);
fix(build): name a source's object from its path The object of an implementation unit was named from its file's name alone, <build dir>/<name>_impl.o, and that of a C or CUDA source likewise, <name>_source.o. Two sources of one configuration with the same file name in different directories were therefore compiled into one object, each over the other, with one dependency file between them, and the link was given that object twice (issue #50). A build of an a/Thing.cpp and a b/Thing.cpp that each define a function failed at the link, with duplicate symbols of one of the two and no word of the other. A debug build of two such files that define nothing another file names succeeded, and its program had one file's code twice and the other's not at all. A .c and a .cu of one name shared an object in one directory as well. An object, with its dependency file beside it, is now named from where its source lies: under <build dir>/objects by the source's path from the configuration's directory, objects/a/Thing_impl.o for a/Thing.cpp, or, for a source outside that directory, under <build dir>/objects-outside by its whole path. A .cu's object ends in _cuda.o; a .cpp's still ends in _impl.o and a .c's in _source.o. Two sources then have one object only when their paths are one once . and .. are taken out of them: any other two differ by path or by ending in one of the two directories, and the two directories share nothing, whatever a project calls its own directories. They are directories of their own, and not the build directory itself, because that one holds files named after other things: an interface unit's object, the record of the link and the list of its objects and, for an executable, the BMIs. ObjectPath gives that name, exported beside ExpectedOutput for what looks at an object from outside Build(). Build() names each object by the same rule, once, as it goes over the sources, and the list it keeps of them for the link is what the check whether an object is newer than the output reads as well. Before, each put the name together for itself: Implementation::Check, Implementation::Schedule, and Build() where it compiles C and CUDA sources, lists the link's objects and checks whether to link again. Implementation::Check and Schedule are given the object, where they put its name together from the build directory. The job that compiles a source makes the directory of its object first, since the compiler makes none. The C compile quotes its source, dependency file and object for the shell, as the CUDA compile does: the object of a source outside the configuration's directory holds that source's whole path, and so directories the command did not name before, the working directory's among them where the source is listed relative to it. The progress line of the compile of a .cpp, .c or .cu names the source by the path its object is named from, "Compiling a/Thing.cpp" where it was "Compiling Thing.cpp" for both, and so does the error for a .cu without an nvcc. What else changes: every implementation unit and every C and CUDA source has its object in a new place, so an existing build directory compiles them once more. The objects under the old names stay where they are, unused; a static library's archive does not keep them, being written anew. Interface units are as they were: a BMI is found by its module's name, so two interface files of one name would have to be one module. A source that a configuration lists twice is also as it was, compiled twice into its one object, which the link is given twice. In IncrementalHeaderChange, IncrementalInterfaceChange, StdPcmRebuild and TransitiveInterfaceChange, the lines that spelled out an object's old name take it from ObjectPath. Fixes #50
2026-11-19 11:02:39 +01:00
// Whether `object`, the object Build() keeps for this unit
// (ObjectPath), has to be written again.
bool Check(const fs::path& object, const fs::path& pcmDir, fs::file_time_type sourceFloor = fs::file_time_type::min()) const;
// Adds the job that compiles this unit into `object` to `jobs`, after
// the BMIs of the stale interface units it imports
// (ModulePartition::Schedule, Module::Schedule), whose own objects go
// to `buildDir`. For a unit Check found stale. `shown` is the source
// as the job's progress line names it.
void Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, const fs::path& object, std::string_view shown) const;
2025-10-31 16:50:47 +01:00
};
}