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
|
|
@ -3,10 +3,7 @@
|
|||
|
||||
// Copied over lib/Widget.cppm mid-test to stand in for the interface edit from
|
||||
// issue #27: one extra data member, every signature and mangled name unchanged.
|
||||
// Kept as a .cppm.in so the module scanner never treats it as a source of its
|
||||
// own — and so the text does not have to live in a string literal inside the
|
||||
// test, where `export module Widget;` would make the test itself look like an
|
||||
// implementation unit of Widget.
|
||||
// Kept as a .cppm.in so that no build takes it for a source of its own.
|
||||
|
||||
export module Widget;
|
||||
import std;
|
||||
|
|
|
|||
|
|
@ -7,15 +7,17 @@ namespace fs = std::filesystem;
|
|||
using namespace Crafter;
|
||||
|
||||
// Adding a data member to a class in a module interface must rebuild every
|
||||
// object compiled against the old layout. The dangerous shape (issue #27) is a
|
||||
// object compiled against the old layout. The dangerous shape (issue #27) was a
|
||||
// consumer whose sources were scanned *before* its `dependencies` were assigned:
|
||||
// its `import <DepModule>;` matched nothing, so its object carried no staleness
|
||||
// edge to the interface, and a layout change rebuilt the library, relinked the
|
||||
// consumer, and produced a binary mixing both layouts with no error or warning.
|
||||
//
|
||||
// `AddTest` is exactly that shape — it scans tests/<name>/main.cpp and only then
|
||||
// `AddTest` is exactly that shape — it lists tests/<name>/main.cpp and only then
|
||||
// returns a builder whose .Dependencies() supplies the library — which is why
|
||||
// the original report saw the corruption in test executables specifically.
|
||||
// A build reads the sources now, once the dependencies are built, so the order
|
||||
// they were declared in no longer matters; the cases below hold it to that.
|
||||
|
||||
namespace {
|
||||
std::int32_t Failures = 0;
|
||||
|
|
@ -36,10 +38,7 @@ namespace {
|
|||
return staged;
|
||||
}
|
||||
|
||||
// Swap in the variant carrying the extra member. Comes from a file rather
|
||||
// than a string literal here: the module scanner reads raw source, so an
|
||||
// `export module Widget;` spelled inside this test would make the test look
|
||||
// like an implementation unit of Widget.
|
||||
// Swap in the variant carrying the extra member, kept beside the original.
|
||||
void GrowWidget(const fs::path& staged) {
|
||||
fs::copy_file(staged / "lib" / "Widget-grown.cppm.in", staged / "lib" / "Widget.cppm", fs::copy_options::overwrite_existing);
|
||||
// copy_file carries the source's mtime across, which would leave the
|
||||
|
|
@ -60,9 +59,9 @@ namespace {
|
|||
return lib;
|
||||
}
|
||||
|
||||
// Scan first, wire the dependency up afterwards — the ordering that used to
|
||||
// silently drop the staleness edge.
|
||||
Configuration MakeConsumerScannedBeforeDependencies(const fs::path& staged, Configuration* lib) {
|
||||
// Sources first, the dependency wired up afterwards — the ordering that
|
||||
// used to silently drop the staleness edge.
|
||||
Configuration MakeConsumerListedBeforeDependencies(const fs::path& staged, Configuration* lib) {
|
||||
Configuration app;
|
||||
app.path = staged;
|
||||
app.name = "widget-app";
|
||||
|
|
@ -94,45 +93,31 @@ namespace {
|
|||
|
||||
int main() {
|
||||
{
|
||||
// The scan leaves the unmatched import recorded rather than forgotten,
|
||||
// and ResolvePendingImports places it once the library is reachable.
|
||||
// The build places the consumer's import on the library's module,
|
||||
// although the library became a dependency after the consumer's
|
||||
// sources were listed.
|
||||
fs::path staged = StageFixture();
|
||||
std::unique_ptr<Configuration> lib = MakeLib(staged);
|
||||
Configuration app;
|
||||
app.path = staged;
|
||||
app.name = "widget-app";
|
||||
app.outputName = "widget-app";
|
||||
app.target = HostTarget();
|
||||
app.type = ConfigurationType::Executable;
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
|
||||
Check(app.implementations.size() == 1, "consumer has one implementation");
|
||||
if (app.implementations.size() == 1) {
|
||||
const Implementation& impl = app.implementations[0];
|
||||
Check(impl.externalModuleDependencies.empty(), "no external dep resolvable before dependencies are assigned");
|
||||
Check(std::ranges::find(impl.pendingImports, "Widget") != impl.pendingImports.end(), "unresolved 'import Widget;' is recorded as pending");
|
||||
|
||||
app.dependencies = { lib.get() };
|
||||
app.ResolvePendingImports();
|
||||
|
||||
Check(impl.externalModuleDependencies.size() == 1, "ResolvePendingImports adds the external module dep");
|
||||
if (impl.externalModuleDependencies.size() == 1) {
|
||||
Check(impl.externalModuleDependencies[0].first->name == "Widget", "external dep is the Widget module");
|
||||
Check(impl.externalModuleDependencies[0].second == lib->PcmDir() / "Widget.pcm", "external dep points at the library's BMI");
|
||||
Configuration app = MakeConsumerListedBeforeDependencies(staged, lib.get());
|
||||
if (BuildOk(app, "consumer with the dependency wired up last")) {
|
||||
Check(app.implementations.size() == 1, "consumer has one implementation");
|
||||
if (app.implementations.size() == 1) {
|
||||
const Implementation& impl = app.implementations[0];
|
||||
Check(impl.externalModuleDependencies.size() == 1, "the import of the library's module is one external module dep");
|
||||
if (impl.externalModuleDependencies.size() == 1) {
|
||||
Check(impl.externalModuleDependencies[0].first->name == "Widget", "external dep is the Widget module");
|
||||
Check(impl.externalModuleDependencies[0].second == lib->PcmDir() / "Widget.pcm", "external dep points at the library's BMI");
|
||||
}
|
||||
}
|
||||
// A second build makes the units anew: the edge is there once.
|
||||
if (BuildOk(app, "consumer built again")) {
|
||||
Check(app.implementations.size() == 1 && app.implementations[0].externalModuleDependencies.size() == 1, "a second build has the one edge again");
|
||||
}
|
||||
Check(std::ranges::find(impl.pendingImports, "Widget") == impl.pendingImports.end(), "resolved import is no longer pending");
|
||||
|
||||
// Idempotent: a second sweep must not duplicate the edge (Build
|
||||
// runs one unconditionally, on top of whatever callers already did).
|
||||
app.ResolvePendingImports();
|
||||
Check(impl.externalModuleDependencies.size() == 1, "ResolvePendingImports is idempotent");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// AddTest is the reported path: it scans the test source, then hands
|
||||
// AddTest is the reported path: it lists the test source, then hands
|
||||
// back a builder whose .Dependencies() names the library.
|
||||
fs::path staged = StageFixture();
|
||||
fs::create_directories(staged / "tests" / "Consumer");
|
||||
|
|
@ -146,28 +131,31 @@ int main() {
|
|||
app.target = HostTarget();
|
||||
app.type = ConfigurationType::Executable;
|
||||
// AddTest resolves tests/<name>/main against the cwd, which for a real
|
||||
// run is the directory holding project.cpp. Stand in the staged project
|
||||
// for the declaration so the fixture's test source is the one scanned.
|
||||
// run is the directory holding project.cpp, and the test's "./" path
|
||||
// is read against it when the test is built. Stand in the staged
|
||||
// project for both, so the fixture's test source is the one listed
|
||||
// and the test builds beside it.
|
||||
fs::path restore = fs::current_path();
|
||||
fs::current_path(staged);
|
||||
app.AddTest("Consumer").Dependencies({ lib.get() });
|
||||
fs::current_path(restore);
|
||||
|
||||
Check(app.tests.size() == 1, "one test declared");
|
||||
if (app.tests.size() == 1 && app.tests[0].config.implementations.size() == 1) {
|
||||
const Implementation& impl = app.tests[0].config.implementations[0];
|
||||
Check(impl.externalModuleDependencies.size() == 1, "AddTest(...).Dependencies() resolves the test's import of the library module");
|
||||
Check(impl.pendingImports.empty() || std::ranges::find(impl.pendingImports, "Widget") == impl.pendingImports.end(), "test's 'import Widget;' is no longer pending");
|
||||
if (app.tests.size() == 1 && BuildOk(app.tests[0].config, "test declared with AddTest")) {
|
||||
Check(app.tests[0].config.implementations.size() == 1, "the test has one implementation");
|
||||
if (app.tests[0].config.implementations.size() == 1) {
|
||||
const Implementation& impl = app.tests[0].config.implementations[0];
|
||||
Check(impl.externalModuleDependencies.size() == 1, "AddTest(...).Dependencies() gives the test's import of the library module its edge");
|
||||
}
|
||||
}
|
||||
fs::current_path(restore);
|
||||
}
|
||||
|
||||
{
|
||||
// End to end, and deliberately without calling ResolvePendingImports:
|
||||
// the guarantee under test is that Build() closes the window on its own,
|
||||
// for consumers that never knew they had to ask.
|
||||
// End to end: the guarantee under test is that a build of a consumer
|
||||
// declared in that order recompiles it, for consumers that never knew
|
||||
// the order mattered.
|
||||
fs::path staged = StageFixture();
|
||||
std::unique_ptr<Configuration> lib = MakeLib(staged);
|
||||
Configuration app = MakeConsumerScannedBeforeDependencies(staged, lib.get());
|
||||
Configuration app = MakeConsumerListedBeforeDependencies(staged, lib.get());
|
||||
fs::path binary = app.BinDir() / "widget-app";
|
||||
fs::path consumerObject = ObjectPath(app, staged / "main.cpp");
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue