Swapping a configuration's dependency keeps objects compiled against the old one's headers: no unit's staleness check looks at its compile command #51

Open
opened 2026-11-27 09:24:36 +00:00 by jorijnvdgraaf · 0 comments

Summary

Whether a C++ unit is compiled again is decided from file times alone: its source, the newest artifact of the configuration's external dependencies, the prerequisites in its own dependency file and the BMIs it imports. The command it was compiled with is never compared. A configuration gets a build directory of its own only for what VariantId() hashes, and VariantId() does not hash cfg.dependencies. The compile command does take from each dependency its directory, every include directory under it (-I) and its PCM directory (-fprebuilt-module-path).

So a configuration built on another dependency in the same build directory keeps every object that is still newer than its source, the BMIs it imports and what its dependency file lists, which names the old dependency's headers. Where a header of the same name says something else in the new dependency, the program is linked from objects compiled against the old one, and nothing is printed.

The link does compare its command: it is recorded with the objects it reads in <output>.link and run again when either differs. Since 6.4.2 a source's scan does too (CurrentScan), so after such a swap the module graph is read for the new dependency while the objects stay as they were compiled for the old one.

Line numbers refer to 578a1da (6.4.2). Reproduced with 6.4.1 (the stable Flatpak) and with 578a1da built natively.

Reproduce

tests/SourceScan/fixture/switch has two libraries, on and off, each with a switch.h that defines WITH_EXTRA, 1 in on and 0 in off. The app's main.cpp includes <switch.h>; under WITH_EXTRA it imports the app's own module Extra and returns 40, otherwise it returns 2. Copy that directory, add the project.cpp below, which builds the app on the library SWITCH_LIB names, and run the program after each build:

SWITCH_LIB=on crafter-build     # the program exits 40
SWITCH_LIB=off crafter-build    # the program still exits 40
rm -rf app/build app/bin
SWITCH_LIB=off crafter-build    # the program exits 2

The second build, with 578a1da:

[1/2] Building std PCM (x86_64-pc-linux-gnu-native)
[2/2] Copying files for scan-switch
[3/3] Copying files for scan-off
[4/4] Building std PCM (x86_64-pc-linux-gnu-native)
[5/5] Scanning Off.cppm
[6/7] Precompiling Off.cppm
[7/7] Compiling Off.cppm
[8/8] Linking scan-off
[9/10] Scanning Extra.cppm
[10/10] Scanning main.cpp
[11/11] Linking scan-switch
Built 11 steps in 156ms

main.cpp is scanned again, because its command changed, and is not compiled. With 6.4.1 the second build ends in [7/7] Linking scan-off and [8/8] Linking scan-switch, and its program exits 40 as well.

project.cpp
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;

extern "C" Configuration CrafterBuildProject(std::span<const std::string_view>) {
    static auto on = std::make_unique<Configuration>();
    on->path = "./on/";
    on->name = "scan-on";
    on->outputName = "scan-on";
    on->type = ConfigurationType::LibraryStatic;
    {
        std::array<fs::path, 1> ifaces = { "On" };
        std::array<fs::path, 0> impls = {};
        on->GetInterfacesAndImplementations(ifaces, impls);
    }
    static auto off = std::make_unique<Configuration>();
    off->path = "./off/";
    off->name = "scan-off";
    off->outputName = "scan-off";
    off->type = ConfigurationType::LibraryStatic;
    {
        std::array<fs::path, 1> ifaces = { "Off" };
        std::array<fs::path, 0> impls = {};
        off->GetInterfacesAndImplementations(ifaces, impls);
    }
    Configuration app;
    app.path = "./app/";
    app.name = "scan-switch";
    app.outputName = "scan-switch";
    app.type = ConfigurationType::Executable;
    const char* which = std::getenv("SWITCH_LIB");
    app.dependencies = { which != nullptr && std::string_view(which) == "off" ? off.get() : on.get() };
    std::array<fs::path, 1> ifaces = { "Extra" };
    std::array<fs::path, 1> impls = { "main" };
    app.GetInterfacesAndImplementations(ifaces, impls);
    return app;
}

Where

  • implementations/Crafter.Build-Implementation.cpp:16, Implementation::Check: an object is stale when it is missing or not newer than its .cpp, sourceFloor and the newest prerequisite of its dependency file (:22), or than a BMI it imports.
  • implementations/Crafter.Build-Interface.cpp:57 and :121, ModulePartition::Check and Module::Check: the same rule for a BMI and its .pcm.d.
  • implementations/Crafter.Build-Clang.cpp:1590: a C source's object, by its source and its dependency file alone.
  • interfaces/Crafter.Build-Clang.cppm:690, VariantId(): type, debug, sysroot, defines, compile flags, the wasm variant and baseline flags, the std PCM key, project args and the toolchain. Not the dependencies.
  • implementations/Crafter.Build-Clang.cpp:725-744, in GetCompileCommand: what each dependency, and each of its own dependencies, adds to the command.
  • implementations/Crafter.Build-Clang.cpp:2019-2030: the link's record of its command and objects. :306, CurrentScan: a scan holds only for the command it was taken with.

Proposal

Do for each unit what the link and the scan already do: keep the command it was compiled with beside its object or BMI, and count it stale when the command differs. That takes in every input of the command, not only the dependencies, and leaves VariantId() as it is. Hashing the dependencies into VariantId() instead would give each set of dependencies a build directory of its own, with a full build after every swap, and would still miss whatever else reaches the command without being hashed.

Not looked at

External dependencies (cfg.externalDependencies), whose include flags reach the command in the same way and are not hashed either; CUDA sources; whether anything else reaches the command without being in VariantId().

## Summary Whether a C++ unit is compiled again is decided from file times alone: its source, the newest artifact of the configuration's external dependencies, the prerequisites in its own dependency file and the BMIs it imports. The command it was compiled with is never compared. A configuration gets a build directory of its own only for what `VariantId()` hashes, and `VariantId()` does not hash `cfg.dependencies`. The compile command does take from each dependency its directory, every `include` directory under it (`-I`) and its PCM directory (`-fprebuilt-module-path`). So a configuration built on another dependency in the same build directory keeps every object that is still newer than its source, the BMIs it imports and what its dependency file lists, which names the old dependency's headers. Where a header of the same name says something else in the new dependency, the program is linked from objects compiled against the old one, and nothing is printed. The link does compare its command: it is recorded with the objects it reads in `<output>.link` and run again when either differs. Since 6.4.2 a source's scan does too (`CurrentScan`), so after such a swap the module graph is read for the new dependency while the objects stay as they were compiled for the old one. Line numbers refer to `578a1da` (6.4.2). Reproduced with 6.4.1 (the stable Flatpak) and with `578a1da` built natively. ## Reproduce `tests/SourceScan/fixture/switch` has two libraries, `on` and `off`, each with a `switch.h` that defines `WITH_EXTRA`, 1 in `on` and 0 in `off`. The app's `main.cpp` includes `<switch.h>`; under `WITH_EXTRA` it imports the app's own module `Extra` and returns 40, otherwise it returns 2. Copy that directory, add the `project.cpp` below, which builds the app on the library `SWITCH_LIB` names, and run the program after each build: ``` SWITCH_LIB=on crafter-build # the program exits 40 SWITCH_LIB=off crafter-build # the program still exits 40 rm -rf app/build app/bin SWITCH_LIB=off crafter-build # the program exits 2 ``` The second build, with `578a1da`: ``` [1/2] Building std PCM (x86_64-pc-linux-gnu-native) [2/2] Copying files for scan-switch [3/3] Copying files for scan-off [4/4] Building std PCM (x86_64-pc-linux-gnu-native) [5/5] Scanning Off.cppm [6/7] Precompiling Off.cppm [7/7] Compiling Off.cppm [8/8] Linking scan-off [9/10] Scanning Extra.cppm [10/10] Scanning main.cpp [11/11] Linking scan-switch Built 11 steps in 156ms ``` `main.cpp` is scanned again, because its command changed, and is not compiled. With 6.4.1 the second build ends in `[7/7] Linking scan-off` and `[8/8] Linking scan-switch`, and its program exits 40 as well. <details> <summary>project.cpp</summary> ```cpp import std; import Crafter.Build; namespace fs = std::filesystem; using namespace Crafter; extern "C" Configuration CrafterBuildProject(std::span<const std::string_view>) { static auto on = std::make_unique<Configuration>(); on->path = "./on/"; on->name = "scan-on"; on->outputName = "scan-on"; on->type = ConfigurationType::LibraryStatic; { std::array<fs::path, 1> ifaces = { "On" }; std::array<fs::path, 0> impls = {}; on->GetInterfacesAndImplementations(ifaces, impls); } static auto off = std::make_unique<Configuration>(); off->path = "./off/"; off->name = "scan-off"; off->outputName = "scan-off"; off->type = ConfigurationType::LibraryStatic; { std::array<fs::path, 1> ifaces = { "Off" }; std::array<fs::path, 0> impls = {}; off->GetInterfacesAndImplementations(ifaces, impls); } Configuration app; app.path = "./app/"; app.name = "scan-switch"; app.outputName = "scan-switch"; app.type = ConfigurationType::Executable; const char* which = std::getenv("SWITCH_LIB"); app.dependencies = { which != nullptr && std::string_view(which) == "off" ? off.get() : on.get() }; std::array<fs::path, 1> ifaces = { "Extra" }; std::array<fs::path, 1> impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); return app; } ``` </details> ## Where - `implementations/Crafter.Build-Implementation.cpp:16`, `Implementation::Check`: an object is stale when it is missing or not newer than its `.cpp`, `sourceFloor` and the newest prerequisite of its dependency file (`:22`), or than a BMI it imports. - `implementations/Crafter.Build-Interface.cpp:57` and `:121`, `ModulePartition::Check` and `Module::Check`: the same rule for a BMI and its `.pcm.d`. - `implementations/Crafter.Build-Clang.cpp:1590`: a C source's object, by its source and its dependency file alone. - `interfaces/Crafter.Build-Clang.cppm:690`, `VariantId()`: type, debug, sysroot, defines, compile flags, the wasm variant and baseline flags, the std PCM key, project args and the toolchain. Not the dependencies. - `implementations/Crafter.Build-Clang.cpp:725-744`, in `GetCompileCommand`: what each dependency, and each of its own dependencies, adds to the command. - `implementations/Crafter.Build-Clang.cpp:2019-2030`: the link's record of its command and objects. `:306`, `CurrentScan`: a scan holds only for the command it was taken with. ## Proposal Do for each unit what the link and the scan already do: keep the command it was compiled with beside its object or BMI, and count it stale when the command differs. That takes in every input of the command, not only the dependencies, and leaves `VariantId()` as it is. Hashing the dependencies into `VariantId()` instead would give each set of dependencies a build directory of its own, with a full build after every swap, and would still miss whatever else reaches the command without being hashed. ## Not looked at External dependencies (`cfg.externalDependencies`), whose include flags reach the command in the same way and are not hashed either; CUDA sources; whether anything else reaches the command without being in `VariantId()`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Build#51
No description provided.