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.
This commit is contained in:
Jorijn van der Graaf 2026-11-27 16:16:38 +01:00
commit 090d8f6363
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 32 additions and 7 deletions

View file

@ -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<ScannedUnit> 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<std::string> 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