From 090d8f6363813a30df1edfb3c3d08cc761906bc4 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Fri, 27 Nov 2026 16:16:38 +0100 Subject: [PATCH] fix(build): scan sources with the compiler where it is installed clang-scan-deps, which reads what a .cppm or .cpp declares and imports since 6.4.2, builds a clang driver of its own from the compile command it is given, and that driver looks for clang's own headers, and in a build for the host whose command names no libc++ directory for libc++'s too, from the directory of the command's first word: the toolchain's clangxx, looked up on PATH where it is a bare name, and taken as it stands where it is a link, whatever the command says of canonical prefixes. Where clangxx is a link from another directory, as a CRAFTER_BUILD_LLVM_DIR whose bin/clang++ links into another installation makes it, and the command's last prefix flag is not -no-canonical-prefixes, that directory need not lead to either, and the scan of a source that includes a header of the C++ standard library then failed, and the build with it, on "'stddef.h' file not found" or on the header it included, though the compile, which takes itself to be where the link leads, found them. It is the parse of issue #46 over again, in the scan. The scan is now given the compile command with Toolchain::InstalledClangxx(command) as its first word, the compiler where clang started with that command takes itself to be, as lint's parse is given it: in the directory its --version names, or, for a command whose last prefix flag is -no-canonical-prefixes, in the directory of clangxx as written, from which such a compile looks and from which the scan of it looked before. That directory is also where the driver looks for its default configuration files, clang++.cfg among them, so for any other command the scan now reads the ones beside the compiler, which the compile reads, where it read any beside a link. What is kept of a scan still records the compile command itself, which decides when a source is scanned again: what the source is compiled with has not changed. AGENTS.template.md now says which of the two commands is which. --- AGENTS.template.md | 2 +- implementations/Crafter.Build-Clang.cpp | 37 +++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/AGENTS.template.md b/AGENTS.template.md index 364a336..5e1c58a 100644 --- a/AGENTS.template.md +++ b/AGENTS.template.md @@ -206,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 ` 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. -- `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. +- `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 compile command it was taken for and what clang-scan-deps wrote, with a dependency file beside it, and taken again when that command differs, when the source or a file the dependency file lists is newer, and when it cannot be read; clang-scan-deps is given that command with its first word made the compiler where clang started with that command takes itself to be: the directory it is installed in, or, for a command whose last of `-canonical-prefixes` and `-no-canonical-prefixes` is `-no-canonical-prefixes`, that of the compiler as written (`ScannedAs`, `Toolchain::InstalledClangxx`). 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 (`/.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. diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index 3b367e3..aa6c813 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -299,9 +299,10 @@ namespace { } // The scan of a source that Build() keeps at `scan`, when it holds for - // `command`: it was taken with that command, after the source and every - // file the source included were last written (NewestPrerequisite of - // its dependency file). nullopt when it does not, and when it cannot be + // `command`: it was taken for that command, which clang-scan-deps was + // given as ScannedAs makes it, after the source and every file the + // source included were last written (NewestPrerequisite of its + // dependency file). nullopt when it does not, and when it cannot be // read; the source is then scanned again. std::optional CurrentScan(const fs::path& scan, std::string_view command) { std::error_code ec; @@ -1137,6 +1138,29 @@ namespace { return quoted; } + // `command`, a compile command of GetCompileCommand's, with its first + // word, the toolchain's clangxx, made + // Toolchain::InstalledClangxx(command), the compiler where clang started + // with that command takes itself to be. clang-scan-deps runs a clang + // driver of its own over the command after its --, and that driver looks + // for clang's own headers, and in a build for the host whose command names + // no libc++ directory for libc++'s too, from the directory of the + // command's first word, found on PATH where it is a bare name, without + // following a link to where the compiler is, whatever the command says of + // canonical prefixes: with a clangxx reached through a link from another + // directory, as CRAFTER_BUILD_LLVM_DIR may name one, a source that + // includes a header of the C++ standard library failed its scan where + // those headers were not to be found from the link's directory, though its + // compile, which takes itself to be where the link leads unless the + // command's last prefix flag is -no-canonical-prefixes, found the header. + // A command that does not start with clangxx is left as it is. + std::string ScannedAs(std::string_view command) { + const Toolchain& tc = GetToolchain(); + const std::string compiler = tc.clangxx.string(); + if (!command.starts_with(compiler)) return std::string(command); + return std::format("{}{}", ShellQuote(tc.InstalledClangxx(command).string()), command.substr(compiler.size())); + } + // Has `scanner`, clang-scan-deps, read `source` as `command` compiles it, // and keeps what it found at `scan` for CurrentScan: a JSON object of the // command and of the P1689 description the scanner wrote, with the @@ -1152,7 +1176,7 @@ namespace { // -w: what the preprocessor warns of, the compile of the source // reports. The command's own output is named `scan`, so that the // dependency file is the rule for the scan. - const std::string failed = RunBuildCommand(std::format("{} -format=p1689 -o {} -- {} -w {} -c -MD -MF {} -o {}", ShellQuote(scanner.string()), ShellQuote(description.string()), command, ShellQuote(source.string()), ShellQuote(depfile.string()), ShellQuote(scan.string()))); + const std::string failed = RunBuildCommand(std::format("{} -format=p1689 -o {} -- {} -w {} -c -MD -MF {} -o {}", ShellQuote(scanner.string()), ShellQuote(description.string()), ScannedAs(command), ShellQuote(source.string()), ShellQuote(depfile.string()), ShellQuote(scan.string()))); const std::optional written = ReadWholeFile(description); fs::remove(description, ec); if (!failed.empty()) { @@ -1190,8 +1214,9 @@ namespace { } // Reads what each interface and implementation source of `config` - // declares and imports, with `command`, the command they are compiled - // with, and gives the configuration the modules and units that makes. A + // declares and imports, as `command`, the command they are compiled + // with, compiles them (ScanSource), and gives the configuration the + // modules and units that makes. A // source whose scan is current (CurrentScan) is not read again; the // others are scanned at once, each a job on the workers of the process. // Returns why it could not, empty when it could; the configuration then