fix(build): read what a source declares and imports with clang-scan-deps
GetInterfacesAndImplementations found the module declarations and the imports of a configuration's sources by searching their text with regular expressions, after deleting the comments that fit on one line. The expressions skipped neither string literals nor comments that span lines, knew nothing of the preprocessor, and wanted one space after `import` and none before its semicolon (#45). An implementation source that held "export module std;" in a string literal was taken for a unit of a module std, and listing it threw "Module std not found not found". An import in a comment that spans lines or under #if 0 was followed. `import X;` and `import X ;` were missed, so their unit had no edge to the module it imports and could be compiled before that module's BMI was written. What a .cppm or .cpp declares and imports now comes from clang-scan-deps -format=p1689, which preprocesses the source with the configuration's C++ compile command, the one Build() compiles every unit of it with: strings, comments, spacing, macros and #if come out as they do for the compiler. GetInterfacesAndImplementations only lists the sources, in the new interfaceSources and implementationSources. Build() reads them (ReadSources) once the configuration's dependencies and external dependencies are built: the command takes their include paths, and a source may include headers of an external dependency, which are not there before it is fetched. From what it read, Build() makes the configuration's modules and units, its `interfaces` and `implementations`, and places each import on a module of the configuration or of what it is built on, whose own Build() has read its sources by then. It makes those of its own configuration alone, as the reset of the units' state did before it. A build pass builds a directory once, for whichever configuration of it asks first, and the loader can make two configurations of one directory: one project loaded along two paths with its args in two orders, which it keeps apart and VariantId, sorting the args, does not. Only the one that was built has the modules that build read, so the result of a build now names the configuration it ran on (BuildResult::built), and a dependency's modules are looked up through the pass's record of its directory. 6.4.1 had every configuration's modules from the listing and took such a graph for one with two builds of a module, which it refused; it is now built, once, and the units of both sides have their edges. A scan is kept in the build directory as an object is, in scans/ or, for a source outside cfg.path, in scans-outside/, as JSON of the command it was taken with and of what clang-scan-deps wrote, with the dependency file clang-scan-deps wrote beside it, which lists the source and every file it included. A build scans a source again unless its scan is newer than every file that dependency file lists, was taken with the same command and can be read, so a build with nothing changed runs no clang-scan-deps. Both files are written beside their places and renamed into them, as the archive and the list of objects are, for another crafter-build that may be building the same clone. A configuration's modules are known only once its build has read its sources, so the refusals of a module that two configurations of the graph build (DuplicateModuleError, #41) and of interface units that import each other (ImportCycleError) now come there: once the dependencies are built, before any .cppm or .cpp of the configuration is compiled. They came before the configuration made a directory or built a dependency. The contributor guide names clang-scan-deps among the prerequisites and among what :Toolchain resolves, has a test look at the modules of a configuration only after building it, and has the gotcha for the new order. ResolvePendingImports and the pendingImports of the unit classes go. They placed the imports that a scan at listing time could not, once the dependencies were wired up, and with the imports placed when the dependencies are built there is nothing pending. The tests that looked at a configuration's modules and units before building now look after it (DependencyLink, ModuleInterface, StaticLib, ConcurrentDependencyReset and the disabled TransitiveInterfaceChange), IncrementalInterfaceChange checks the edge that the build of a consumer gives it when its dependency was assigned after its sources were listed, and ImportCycle and DuplicateModule check that the refused configuration compiled nothing of its own. The workarounds for the old scanner are not needed: MingwSysroot writes its stand-in std module as a plain literal again, JobLimit writes the declarations of the sources it generates as plain literals, and Lint's comment on its fixture file no longer gives the scanner as the reason for it. The messages of the regex code stay where they still apply. The one for a source that declares a unit of a module the configuration does not have loses its doubled "not found", and applies to an implementation source that declares one, such as an implementation partition (`module X:P;`). An implementation unit written `module X;` declares no unit for clang-scan-deps but imports X, and an import needs no module of the configuration: the compile reports a module no one builds. A .cppm that declares an implementation partition is built as a partition of its module, where it was refused as declaring no module. The Toolchain gains clangScanDeps, resolved as llvm-ar is: in the bin/ of the LLVM prefix, else by name on PATH. The Flatpak has /app/bin/clang-scan-deps. Fixes #45
This commit is contained in:
parent
a99647ecff
commit
3c8f4ddb54
28 changed files with 851 additions and 521 deletions
|
|
@ -23,7 +23,7 @@ crafter-build is a C++26 modules build system. A project is described by a `proj
|
|||
|
||||
Partitions and what they own:
|
||||
|
||||
- `:Toolchain` resolves the compiler once per process (`GetToolchain`): clang++, clang, llvm-ar, llvm-readelf, libclang, libc++'s std module source and the default sysroots, from `CRAFTER_BUILD_LLVM_DIR`, else a toolchain installed beside crafter-build, else PATH. It also holds the version baked in at build time, and the arch and branch of the Flatpak runtime crafter-build runs on, from `/.flatpak-info` or the file `CRAFTER_BUILD_FLATPAK_INFO` names, and the nvcc `cfg.cuda` compiles with: `$CUDA_PATH/bin/nvcc`, else `bin/nvcc` under `/opt/cuda` or `/usr/local/cuda`, else the first in an absolute entry of PATH, which it searches itself, and `cudaRuntime`, the `lib64/libcudart_static.a` beside that nvcc's `bin` where there is one, which a configuration with CUDA sources links. In the Flatpak the host's `/usr` and `/etc` are under `/run/host`, and a link to an absolute path names a host path, so where the path of a toolkit is a link, it follows that link, and the link its target is in turn, each as the host's (`HostPath`); a link in a directory partway along that path, or inside the toolkit, is the kernel's to resolve, in the sandbox's root. `SysrootStdModule` picks the std module source for a sysroot build by the sysroot's libc++ release. `MingwSysroot` settles the mingw target's `--sysroot`: the mingw-w64 prefix itself, or, for a prefix that also holds a Linux system (the host's `/usr`, anything whose `include/` has `features.h`), a view in the cache of its mingw-w64 directories, since clang would search its `include/` and `lib/` too (issue #43). `MingwViewToPrefix` turns paths into the view back into paths into the prefix for messages.
|
||||
- `:Toolchain` resolves the compiler once per process (`GetToolchain`): clang++, clang, llvm-ar, llvm-readelf, clang-scan-deps, libclang, libc++'s std module source and the default sysroots, from `CRAFTER_BUILD_LLVM_DIR`, else a toolchain installed beside crafter-build, else PATH. It also holds the version baked in at build time, and the arch and branch of the Flatpak runtime crafter-build runs on, from `/.flatpak-info` or the file `CRAFTER_BUILD_FLATPAK_INFO` names, and the nvcc `cfg.cuda` compiles with: `$CUDA_PATH/bin/nvcc`, else `bin/nvcc` under `/opt/cuda` or `/usr/local/cuda`, else the first in an absolute entry of PATH, which it searches itself, and `cudaRuntime`, the `lib64/libcudart_static.a` beside that nvcc's `bin` where there is one, which a configuration with CUDA sources links. In the Flatpak the host's `/usr` and `/etc` are under `/run/host`, and a link to an absolute path names a host path, so where the path of a toolkit is a link, it follows that link, and the link its target is in turn, each as the host's (`HostPath`); a link in a directory partway along that path, or inside the toolkit, is the kernel's to resolve, in the sandbox's root. `SysrootStdModule` picks the std module source for a sysroot build by the sysroot's libc++ release. `MingwSysroot` settles the mingw target's `--sysroot`: the mingw-w64 prefix itself, or, for a prefix that also holds a Linux system (the host's `/usr`, anything whose `include/` has `features.h`), a view in the cache of its mingw-w64 directories, since clang would search its `include/` and `lib/` too (issue #43). `MingwViewToPrefix` turns paths into the view back into paths into the prefix for messages.
|
||||
- `:Clang` holds `Configuration`, `Build()`, `VariantId()`, `ApplyStandardArgs`, `HostTarget`, the CLI entry point `Crafter::Run`, and the lint, test and script data types.
|
||||
- `:Platform` runs commands, implements `LoadConfigurations` and `LoadProject` (compile and load `project.cpp`), the host PCM cache and `#pragma crafter dependency`.
|
||||
- `:Jobs` is what build steps run on: `JobPool`, worker threads behind one queue, started as jobs need them up to a fixed number, with `JobPool::Process()` the pool of at most one worker per hardware thread that `Build()` and `RunBuildCommand` use, and `JobGroup`, the jobs of one configuration's build, each started once the jobs it was added after have finished, with the first error among them and what `Build()` reports to it through `Fail()`.
|
||||
|
|
@ -40,7 +40,7 @@ Partitions and what they own:
|
|||
|
||||
The shipped artifact is the Flatpak (see "CI and publishing"); the loop below is the native development loop.
|
||||
|
||||
Prerequisites on Linux: `clang` (with `libclang`, which the AST lint rules load at runtime), `lld`, LLVM's `llvm-ar` and `llvm-readelf`, `libc++` with its std module source (`share/libc++/v1/std.cppm`), `cmake`, `git`. `crafter-build --version` prints which compiler was resolved; `CRAFTER_BUILD_LLVM_DIR` points it at another LLVM prefix, and `build.sh` honours the same variable. Network access to `forgejo.catcrafts.net` and `github.com` is needed: loading `project.cpp` fetches Crafter.Math, Crafter.Asset and Crafter.LintRules into `~/.cache/crafter.build/external/`, and the bootstrap clones glslang. Those sources are unpinned, so every invocation runs `git pull` on them.
|
||||
Prerequisites on Linux: `clang` (with `libclang`, which the AST lint rules load at runtime, and `clang-scan-deps`, which a build reads what each C++ source declares and imports with), `lld`, LLVM's `llvm-ar` and `llvm-readelf`, `libc++` with its std module source (`share/libc++/v1/std.cppm`), `cmake`, `git`. `crafter-build --version` prints which compiler was resolved; `CRAFTER_BUILD_LLVM_DIR` points it at another LLVM prefix, and `build.sh` honours the same variable. Network access to `forgejo.catcrafts.net` and `github.com` is needed: loading `project.cpp` fetches Crafter.Math, Crafter.Asset and Crafter.LintRules into `~/.cache/crafter.build/external/`, and the bootstrap clones glslang. Those sources are unpinned, so every invocation runs `git pull` on them.
|
||||
|
||||
```bash
|
||||
./build.sh # bootstrap: bin/crafter-build, lib/libcrafter-build.a, share/crafter-build/
|
||||
|
|
@ -93,6 +93,7 @@ Conventions the existing tests follow:
|
|||
- Fixtures under `tests/<Name>/fixture/` are inputs. A test that edits or touches files first stages a copy under `fs::temp_directory_path() / "crafter-build-<name>"` and removes it on the next run. Variant sources are checked in as `*.in` files and copied over the original inside the staged copy.
|
||||
- Use a fresh `depResults` map per build pass: the map memoises each configuration's result, and each external dependency's fetch and build, for one pass, so reusing it skips rebuilds and pulls you meant to observe. `Build()` itself is safe to call repeatedly on the same `Configuration`.
|
||||
- Set `cfg.path` before calling `GetInterfacesAndImplementations`, and spell `cFiles` as absolute paths: both resolve against the working directory, not the configuration.
|
||||
- `GetInterfacesAndImplementations` only lists the sources. A configuration's `interfaces` and `implementations`, its modules and units with what each imports, are made by `Build()` from what it reads in them, so a test that looks at them builds first.
|
||||
- A test that needs the host's flatpak goes through `RunHostFlatpak`, skips (77) with the reason `HostFlatpakUnavailable` gives (flatpak-builder's build sandbox, where CI runs, has no way out), and installs only into a private `FLATPAK_USER_DIR` under its fixture's `build/`, as `FlatpakInstall` does. It never runs an installed app: `flatpak run` makes `~/.var/app/<app-id>` in the real home.
|
||||
- A test that packages a Flatpak stages the runtime: it writes a `.flatpak-info` whose `[Application]` group has a `runtime=runtime/org.freedesktop.Sdk/x86_64/26.08` line, as the parser takes only that group's, and points `CRAFTER_BUILD_FLATPAK_INFO` at it with `setenv`, first thing in `main`, since the toolchain is read once per process. That works natively and wins over the sandbox's own file in the Flatpak; `FlatpakNative` stages an empty one for the native case.
|
||||
|
||||
|
|
@ -205,7 +206,7 @@ Local builds are two scripts in `project.cpp`. Run them with the native binary:
|
|||
- A project file returns one configuration from `CrafterBuildProject` or several from `CrafterBuildConfigurations`, and `Crafter::Run` works on the list `LoadConfigurations` gives it for both. A new verb has to say what it does with several, and whether it takes all of them or leaves out another project's: `test` and `run` leave out a configuration that another project's file returned and this one returns in turn, which `LoadedProjectOf` tells apart. The load has refused, before a verb sees the list, a returned configuration that a loaded project keeps to itself (`KeptBy`): one the loader took down when that project's file was loaded, among what the configurations the file returned led to through `dependencies` and `tests`, as far as the first configuration on each way that a loaded project's file had returned or another loaded project keeps.
|
||||
- The limit of as many steps at once as there are hardware threads holds for what runs on `JobPool::Process()`: the jobs `Build()` adds and the commands of `RunBuildCommand`. Among what runs outside it: an `ExternalDependency`'s `cmake --build`, which `BuildCMake` gives `--parallel <hardware threads>` of its own; the tests `RunTests` runs, `--jobs=` of them at once on threads of its own, while other tests may still be building; the Crafter.Build modules `EnsureCrafterBuildPcms` precompiles for the host cache, one after another; and the threads a step starts for itself, lld's for a link and, for an asset, those of the GDeflate compressor in Crafter.Asset, up to 31 for each stream it compresses. The pool is one process's: a self-test of this repository that builds a fixture does so in its own process, on a pool of its own.
|
||||
- An interface unit takes two compiler runs, one for its BMI and one for the object made from the BMI, and they are two jobs so that the units importing it start once the first is done. `Check` reads staleness off the BMI; the object is `Schedule`'s to look at, which 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 that fails between the two jobs leaves a unit like that, since no job starts after a failure.
|
||||
- The source scanner follows an `import` it reads in a string literal or under `#if 0` like any other (issue #45), so interface units can import each other by its account alone. `ImportCycleError` refuses such a round, naming the units, at the top of the `Build()` of the configuration whose units form it: before that configuration makes a directory or builds a dependency, though a configuration built on it has started its own build by then.
|
||||
- `Build()` reads what each `.cppm` and `.cpp` declares and imports with clang-scan-deps (`ReadSources` in `Crafter.Build-Clang.cpp`) once the configuration's dependencies and external dependencies are built: the compile command takes their include paths, and a source may include their headers. Until then nothing knows a configuration's modules, so `DuplicateModuleError` and `ImportCycleError` refuse a graph there, after the dependencies are built and before any `.cppm` or `.cpp` of the configuration is compiled; its `.c` and `.cu` sources, which compile while the dependencies are built, may have been by then. A scan is kept in the build directory as an object is, in `scans/` or, for a source outside `cfg.path`, in `scans-outside/`, as JSON holding the command it was taken with and what clang-scan-deps wrote, with a dependency file beside it, and taken again when the command differs, when the source or a file the dependency file lists is newer, and when it cannot be read. A `Build()` makes its own configuration's modules and units, never a dependency's: those are the dependency's own `Build()`'s to make, and the builds of other configurations built on it read them, through the pass record (`BuildResult::built`): a pass builds a directory once, so of two configurations of one directory, as the loader makes of one project loaded with its args in two orders, only the one it built has the modules of that build.
|
||||
- Units see a std module rebuilt in place, because their depfiles list the BMIs they import (`-fmodule-file-deps`), but not one that moved to another directory: their old depfiles still name the old one. So anything that changes how the std module is compiled goes into `StdPcmFlags`, which `BuildStdPcm` compiles with and `StdPcmKey` hashes. `VariantId()` hashes `StdPcmKey`, so the build moves with it. A flag added to the command anywhere else leaves both directories where they are.
|
||||
- A CMake external whose recorded configure inputs (`<build>/.crafter-options`) differ from this build's is built again from an empty build directory, not reconfigured in place: CMake keeps what an old compiler or `--sysroot` compiled, and the check results it cached.
|
||||
- The bootstrap binary is compiled without `CRAFTER_BUILD_HAS_ASSET`, so `cfg.assets` only works in the self-hosted binary. What an asset is compressed to is decided in one place, `CompressedAssetName` in `Crafter.Build-Clang.cpp`, which an executable's own assets, the ones its dependencies forward and the browser runtime's `files.json` all use; a Flatpak's `files/bin` keeps what those copies record as placed. `CompressAsset` in `Crafter.Build-Asset.cpp` does the compressing and has to load every extension that function maps.
|
||||
|
|
|
|||
Loading…
Reference in a new issue