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
|
|
@ -8,10 +8,12 @@ using namespace Crafter;
|
|||
|
||||
// Regression for the concurrent dependency-reset deadlock (issue #16).
|
||||
//
|
||||
// Build() resets each Module/ModulePartition's per-build state (`checked`,
|
||||
// `scheduled` and the job that precompiles the unit) so a reused Configuration
|
||||
// re-evaluates mtimes. That reset MUST stay scoped to the configuration being
|
||||
// built. It used to recurse into cfg.dependencies — but dependency
|
||||
// Each Module/ModulePartition holds the state of one build (`checked`,
|
||||
// `scheduled` and the job that precompiles the unit). Build() used to reset
|
||||
// it so a reused Configuration re-evaluates mtimes, and now makes the units
|
||||
// anew from what it reads in the sources. Either way that MUST stay scoped
|
||||
// to the configuration being built. The reset used to recurse into
|
||||
// cfg.dependencies — but dependency
|
||||
// Configurations are shared across the build DAG and each is built
|
||||
// concurrently by its own Build() call. Every unit then had a thread of its
|
||||
// own and a `compiled` flag that the units importing it waited on, and a
|
||||
|
|
@ -30,11 +32,11 @@ using namespace Crafter;
|
|||
// timing: build a static-lib dependency fully (its module ends up checked and
|
||||
// scheduled), then build a consumer that depends on it while the dependency is
|
||||
// already cached in depResults (so it is NOT rebuilt). A consumer that reset
|
||||
// its dependencies would flip the dependency's module `checked` and
|
||||
// `scheduled` flags back to false and, because the dependency never rebuilds,
|
||||
// they would stay false. The consumer resets only its own modules, so the
|
||||
// dependency's flags are left intact. We assert both are still set once the
|
||||
// consumer build completes.
|
||||
// its dependencies, or made their units anew, would leave the dependency's
|
||||
// module with `checked` and `scheduled` false and, because the dependency
|
||||
// never rebuilds, they would stay false. The consumer makes only its own
|
||||
// units, so the dependency's flags are left intact. We assert both are still
|
||||
// set once the consumer build completes.
|
||||
int main() {
|
||||
fs::path fixtureRoot = fs::current_path() / "tests" / "ConcurrentDependencyReset" / "fixture";
|
||||
|
||||
|
|
@ -58,11 +60,6 @@ int main() {
|
|||
dep.GetInterfacesAndImplementations(ifaces, impls);
|
||||
}
|
||||
|
||||
if (dep.interfaces.size() != 1) {
|
||||
std::println(std::cerr, "expected 1 dependency interface, got {}", dep.interfaces.size());
|
||||
return 1;
|
||||
}
|
||||
|
||||
{
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
|
|
@ -71,6 +68,10 @@ int main() {
|
|||
std::println(std::cerr, "dependency build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
if (dep.interfaces.size() != 1) {
|
||||
std::println(std::cerr, "expected 1 dependency interface, got {}", dep.interfaces.size());
|
||||
return 1;
|
||||
}
|
||||
if (!dep.interfaces[0]->checked || !dep.interfaces[0]->scheduled) {
|
||||
std::println(std::cerr, "dependency module not marked checked and scheduled after its own build");
|
||||
return 1;
|
||||
|
|
|
|||
|
|
@ -38,6 +38,15 @@ int main() {
|
|||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
}
|
||||
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult r = Build(app, depResults, depMutex);
|
||||
if (!r.result.empty()) {
|
||||
std::println(std::cerr, "build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// What the build read in the sources.
|
||||
if (app.implementations.size() != 1) {
|
||||
std::println(std::cerr, "expected 1 implementation, got {}", app.implementations.size());
|
||||
return 1;
|
||||
|
|
@ -56,14 +65,6 @@ int main() {
|
|||
return 1;
|
||||
}
|
||||
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult r = Build(app, depResults, depMutex);
|
||||
if (!r.result.empty()) {
|
||||
std::println(std::cerr, "build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fs::path libArchive = lib->BinDir() / "libcalc.a";
|
||||
if (!fs::exists(libArchive)) {
|
||||
std::println(std::cerr, "dep archive not produced at {}", libArchive.string());
|
||||
|
|
|
|||
|
|
@ -8,13 +8,14 @@ using namespace Crafter;
|
|||
|
||||
// A program can hold one module of each name, so Build() refuses a graph in
|
||||
// which two configurations export the same module: the same project loaded
|
||||
// along two paths with two arg lists, say. It used to build both and put
|
||||
// both on the consumer's module path. Clang took the first for every import
|
||||
// of the module, the ones inside the other build's consumers included, and
|
||||
// the program linked code compiled against two layouts of one struct: it
|
||||
// built cleanly, then read garbage or crashed (issue #41). The fixture is
|
||||
// P -> M and P -> A -> M. With --split, P gives M its args without --wide,
|
||||
// which changes what M declares, while A passes them on whole.
|
||||
// along two paths with two arg lists, say. It used to build both and compile
|
||||
// the consumer with both on its module path. Clang took the first for every
|
||||
// import of the module, the ones inside the other build's consumers
|
||||
// included, and the program linked code compiled against two layouts of one
|
||||
// struct: it built cleanly, then read garbage or crashed (issue #41). The
|
||||
// fixture is P -> M and P -> A -> M. With --split, P gives M its args
|
||||
// without --wide, which changes what M declares, while A passes them on
|
||||
// whole.
|
||||
namespace {
|
||||
std::int32_t Failures = 0;
|
||||
|
||||
|
|
@ -53,7 +54,9 @@ int main() {
|
|||
const fs::path cwd = fs::current_path();
|
||||
fs::current_path(scratch / "p");
|
||||
try {
|
||||
// Two lists for M: refused before anything is built.
|
||||
// Two lists for M: refused before P compiles anything of its own.
|
||||
// The modules of a configuration are known once it is built, so M is
|
||||
// built twice, and A on one of the two, before P can tell.
|
||||
{
|
||||
std::array<std::string_view, 2> args = { "--wide", "--split" };
|
||||
Configuration cfg = LoadProject("project.cpp", args);
|
||||
|
|
@ -61,7 +64,7 @@ int main() {
|
|||
Check(!built.result.empty(), "a graph with two builds of one module fails to build");
|
||||
Check(built.result.contains("module M is built twice for P"), std::format("the error names the module and the program (got: {})", built.result));
|
||||
Check(built.result.contains("P -> M (") && built.result.contains("P -> A -> M ("), std::format("the error names the path to each build (got: {})", built.result));
|
||||
Check(!fs::exists(scratch / "m" / "bin") && !fs::exists(scratch / "a" / "bin") && !fs::exists(scratch / "p" / "bin"), "nothing was built");
|
||||
Check(!fs::exists(ObjectPath(cfg, scratch / "p" / "main.cpp")) && !fs::exists(ExpectedOutput(cfg)), "P compiled and linked nothing");
|
||||
}
|
||||
|
||||
// One list along both paths: one M, and the build goes through.
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import Crafter.Build;
|
|||
namespace fs = std::filesystem;
|
||||
using namespace Crafter;
|
||||
|
||||
// Interface units that import each other are refused before their
|
||||
// configuration builds anything, with an error naming them. No order builds
|
||||
// them. Before the refusal such a build hung with no compiler running, each
|
||||
// unit waiting for the units it imports, which is how this test failed then:
|
||||
// by running into its timeout.
|
||||
// Interface units that import each other are refused before any .cppm or
|
||||
// .cpp of their configuration is compiled, with an error naming them. No
|
||||
// order builds them. Before the refusal such a build hung with no compiler
|
||||
// running, each unit waiting for the units it imports, which is how this
|
||||
// test failed then: by running into its timeout.
|
||||
//
|
||||
// Three ways round: two partitions that import each other, a partition that
|
||||
// imports its own module, whose primary interface is built after all of its
|
||||
|
|
@ -28,9 +28,9 @@ namespace {
|
|||
|
||||
// Builds the interfaces of the fixture in `dir` as a library and returns
|
||||
// Build()'s error. On a copy outside the checkout, made anew each run: a
|
||||
// build that gets past the refusal has made its directories before it
|
||||
// fails, and in the checkout the next run would find them there and
|
||||
// report them against a refusal that works.
|
||||
// build that gets past the refusal can compile units before it fails, and
|
||||
// in the checkout the next run would find them there and report them
|
||||
// against a refusal that works.
|
||||
std::string BuildError(std::string_view dir, std::span<fs::path> interfaces) {
|
||||
const fs::path staged = fs::temp_directory_path() / "crafter-build-import-cycle" / dir;
|
||||
fs::remove_all(staged);
|
||||
|
|
@ -49,9 +49,13 @@ namespace {
|
|||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
const BuildResult built = Build(cfg, depResults, depMutex);
|
||||
// Refused like a graph with a module built twice: before a directory
|
||||
// is made for it.
|
||||
Check(!fs::exists(cfg.BuildDir()) && !fs::exists(cfg.BinDir()), std::format("{}: the refused build left {} or {} behind", dir, cfg.BuildDir().string(), cfg.BinDir().string()));
|
||||
// Refused once the build has read what the units import, which is
|
||||
// before it compiles any of them: no unit has a BMI or an object.
|
||||
for (const fs::path& interface : interfaces) {
|
||||
const fs::path bmi = cfg.PcmDir() / std::format("{}.pcm", interface.string());
|
||||
const fs::path object = cfg.BuildDir() / std::format("{}.o", interface.string());
|
||||
Check(!fs::exists(bmi) && !fs::exists(object), std::format("{}: the refused build compiled {}", dir, interface.string()));
|
||||
}
|
||||
return built.result;
|
||||
}
|
||||
}
|
||||
|
|
@ -61,9 +65,6 @@ int main() {
|
|||
std::array<fs::path, 3> interfaces = { "Ring", "Ring-First", "Ring-Second" };
|
||||
const std::string error = BuildError("partitions", interfaces);
|
||||
Check(error.contains("Ring:First -> Ring:Second -> Ring:First"), std::format("partitions that import each other are named in the order they wait in, got: '{}'", error));
|
||||
// The scanner reads imports the compiler does not see, so the error
|
||||
// has to say where a round nobody wrote can come from.
|
||||
Check(error.contains("string literal") && error.contains("#if 0"), std::format("the error says which text the scanner reads as an import, got: '{}'", error));
|
||||
}
|
||||
{
|
||||
std::array<fs::path, 2> interfaces = { "Own", "Own-Part" };
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
// The whole point of the fixture: an input to this interface unit that the
|
||||
// module scanner cannot see. WIDGET_SLOTS comes from a header, so the class
|
||||
// layout below changes without Widget.cppm being touched.
|
||||
// The whole point of the fixture: an input to this interface unit that is
|
||||
// not its own source. WIDGET_SLOTS comes from a header, so the class layout
|
||||
// below changes without Widget.cppm being touched.
|
||||
#include "widget-layout.h"
|
||||
export module Widget;
|
||||
import std;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using namespace Crafter;
|
|||
|
||||
// Editing a header must rebuild everything that #includes it. The staleness
|
||||
// check used to compare an artifact against its own source and its module
|
||||
// imports only — and the module scanner reads `import` lines, so a header was
|
||||
// imports only — and the module scanner read `import` lines, so a header was
|
||||
// invisible to it. A build after a header-only edit reported nothing to do and
|
||||
// left objects compiled against the previous contents: the same silent
|
||||
// mixed-layout binary as issue #27, reached through #include instead.
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
||||
|
|
|
|||
|
|
@ -54,13 +54,6 @@ namespace {
|
|||
std::ofstream(file, std::ios::binary | std::ios::trunc) << text;
|
||||
}
|
||||
|
||||
// One module or import declaration of a generated source. Put together
|
||||
// here because the source scanner reads one spelled out in a string
|
||||
// literal as a declaration of this file's own (issue #45).
|
||||
std::string Declare(std::string_view keywords, std::string_view name) {
|
||||
return std::format("{} {};\n", keywords, name);
|
||||
}
|
||||
|
||||
// The C sources of one project, and as many C++ ones.
|
||||
constexpr std::int32_t PerKind = 32;
|
||||
// The units of one project: those sources, the module's primary
|
||||
|
|
@ -83,10 +76,10 @@ namespace {
|
|||
// Limit:High imports Limit:Low, the primary interface both, the
|
||||
// module's implementation unit the primary interface, and main.cpp
|
||||
// the module.
|
||||
Write(root / "Limit-Low.cppm", std::format("{}export int Low() {{ return 1; }}\n", Declare("export module", "Limit:Low")));
|
||||
Write(root / "Limit-High.cppm", std::format("{}{}export int High() {{ return Low() + 1; }}\n", Declare("export module", "Limit:High"), Declare("import", ":Low")));
|
||||
Write(root / "Limit.cppm", std::format("{}{}{}export int Top();\n", Declare("export module", "Limit"), Declare("export import", ":Low"), Declare("export import", ":High")));
|
||||
Write(root / "Limit-impl.cpp", std::format("{}int Top() {{ return High() + 1; }}\n", Declare("module", "Limit")));
|
||||
Write(root / "Limit-Low.cppm", "export module Limit:Low;\nexport int Low() { return 1; }\n");
|
||||
Write(root / "Limit-High.cppm", "export module Limit:High;\nimport :Low;\nexport int High() { return Low() + 1; }\n");
|
||||
Write(root / "Limit.cppm", "export module Limit;\nexport import :Low;\nexport import :High;\nexport int Top();\n");
|
||||
Write(root / "Limit-impl.cpp", "module Limit;\nint Top() { return High() + 1; }\n");
|
||||
|
||||
std::string declarations;
|
||||
std::string sum;
|
||||
|
|
@ -101,7 +94,7 @@ namespace {
|
|||
declarations += std::format("extern \"C\" int c_unit_{0}(void);\nint cpp_unit_{0}();\n", i);
|
||||
sum += std::format(" + c_unit_{0}() + cpp_unit_{0}()", i);
|
||||
}
|
||||
Write(root / "main.cpp", std::format("{}{}{}int main() {{ std::print(\"{{}}\", Top(){}); }}\n", Declare("import", "std"), Declare("import", "Limit"), declarations, sum));
|
||||
Write(root / "main.cpp", std::format("import std;\nimport Limit;\n{}int main() {{ std::print(\"{{}}\", Top(){}); }}\n", declarations, sum));
|
||||
|
||||
std::array<fs::path, 3> interfaces = { "Limit", "Limit-Low", "Limit-High" };
|
||||
cfg->GetInterfacesAndImplementations(interfaces, implementations);
|
||||
|
|
|
|||
|
|
@ -697,9 +697,8 @@ int main() {
|
|||
// is five of this repo's own interfaces, 677 lines. Blanking the keyword is
|
||||
// byte-length preserving, so the lines reported here must match the file.
|
||||
//
|
||||
// The source lives in fixture/ExportNamespace.cppm.in rather than inline:
|
||||
// an `export module` spelled in this file's own text would be picked up by
|
||||
// the build's module scanner as a real interface of this project.
|
||||
// The source lives in fixture/ExportNamespace.cppm.in, copied into the
|
||||
// scratch directory as Demo.cppm.
|
||||
{
|
||||
Scratch s("ast-module");
|
||||
fs::copy_file(fs::current_path() / "tests" / "Lint" / "fixture" / "ExportNamespace.cppm.in", s.dir / "Demo.cppm", fs::copy_options::overwrite_existing);
|
||||
|
|
|
|||
|
|
@ -83,10 +83,7 @@ int main() {
|
|||
Write(mingw / "include" / "_mingw.h", "");
|
||||
Write(mingw / "include" / "crafter_probe_error.h", "#error stopped in a mingw-w64 header\n");
|
||||
Write(mingw / "include" / "c++" / "15.1.0" / "memory", "");
|
||||
// Through std::format because the build's import scanner reads string
|
||||
// literals as code: one holding "module std;" made this file a unit of a
|
||||
// module std, and the test did not build.
|
||||
Write(mingw / "include" / "c++" / "15.1.0" / "bits" / "std.cc", std::format("export module {};\n", "std"));
|
||||
Write(mingw / "include" / "c++" / "15.1.0" / "bits" / "std.cc", "export module std;\n");
|
||||
Write(mingw / "lib" / "libstdc++.modules.json", R"({"version": 1, "modules": [{"logical-name": "std", "source-path": "../include/c++/15.1.0/bits/std.cc", "is-std-library": true}]})");
|
||||
|
||||
{
|
||||
|
|
|
|||
|
|
@ -21,8 +21,16 @@ int main() {
|
|||
std::array<fs::path, 1> impls = { "main" };
|
||||
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
||||
|
||||
// Implementation should have picked up the import Greeter; through the
|
||||
// interface list resolution in GetInterfacesAndImplementations.
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult r = Build(cfg, depResults, depMutex);
|
||||
if (!r.result.empty()) {
|
||||
std::println(std::cerr, "build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The build read main.cpp's import of Greeter and placed it on the
|
||||
// module among the configuration's own interfaces.
|
||||
if (cfg.implementations.size() != 1) {
|
||||
std::println(std::cerr, "expected 1 implementation, got {}", cfg.implementations.size());
|
||||
return 1;
|
||||
|
|
@ -36,14 +44,6 @@ int main() {
|
|||
return 1;
|
||||
}
|
||||
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult r = Build(cfg, depResults, depMutex);
|
||||
if (!r.result.empty()) {
|
||||
std::println(std::cerr, "build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fs::path bin = cfg.BinDir() / "greeter-app";
|
||||
if (!fs::exists(bin)) {
|
||||
std::println(std::cerr, "binary not produced at {}", bin.string());
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ namespace fs = std::filesystem;
|
|||
using namespace Crafter;
|
||||
|
||||
// Build a tiny module-only static library and confirm libgreet.a lands in
|
||||
// BinDir(). Exercises ConfigurationType::LibraryStatic, interface module
|
||||
// parsing via GetInterfacesAndImplementations, and the ar/llvm-lib archive
|
||||
// step in Crafter.Build-Clang.cpp.
|
||||
// BinDir(). Exercises ConfigurationType::LibraryStatic, the module a build
|
||||
// reads in a source GetInterfacesAndImplementations lists, and the
|
||||
// ar/llvm-lib archive step in Crafter.Build-Clang.cpp.
|
||||
int main() {
|
||||
Configuration cfg;
|
||||
cfg.path = fs::current_path() / "tests" / "StaticLib" / "fixture";
|
||||
|
|
@ -22,6 +22,14 @@ int main() {
|
|||
std::array<fs::path, 0> impls = {};
|
||||
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
||||
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult r = Build(cfg, depResults, depMutex);
|
||||
if (!r.result.empty()) {
|
||||
std::println(std::cerr, "build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (cfg.interfaces.size() != 1) {
|
||||
std::println(std::cerr, "expected 1 interface, got {}", cfg.interfaces.size());
|
||||
return 1;
|
||||
|
|
@ -31,14 +39,6 @@ int main() {
|
|||
return 1;
|
||||
}
|
||||
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
BuildResult r = Build(cfg, depResults, depMutex);
|
||||
if (!r.result.empty()) {
|
||||
std::println(std::cerr, "build failed: {}", r.result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fs::path archive = cfg.BinDir() / "libgreet.a";
|
||||
if (!fs::exists(archive)) {
|
||||
std::println(std::cerr, "archive not produced at {}", archive.string());
|
||||
|
|
|
|||
|
|
@ -436,6 +436,10 @@ int main() {
|
|||
prefixed.env.emplace("CRAFTER_BUILD_LLVM_DIR", full.string());
|
||||
const Toolchain withPrefix = ResolveToolchain(prefixed);
|
||||
Check(withPrefix.llvmReadelf == full / "bin" / "llvm-readelf", "llvm-readelf comes from the prefix");
|
||||
// So does clang-scan-deps, which reads what a source declares and
|
||||
// imports.
|
||||
Check(ResolveToolchain({}).clangScanDeps == "clang-scan-deps", "the PATH fallback names clang-scan-deps bare");
|
||||
Check(withPrefix.clangScanDeps == full / "bin" / "clang-scan-deps", "clang-scan-deps comes from the prefix");
|
||||
Check(withPrefix.LibcxxSearchFlags() == std::format(" -L{}", (full / "lib").string()), std::format("the search flags are -L alone (got '{}')", withPrefix.LibcxxSearchFlags()));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,10 +45,8 @@ namespace {
|
|||
return staged;
|
||||
}
|
||||
|
||||
// Swap in the Base variant carrying the extra member. From a file rather
|
||||
// than a string literal: the module scanner reads raw source, so a module
|
||||
// declaration spelled inside this test would make the test itself look like
|
||||
// an implementation unit of Base.
|
||||
// Swap in the Base variant carrying the extra member, kept beside the
|
||||
// original.
|
||||
void GrowBase(const fs::path& staged) {
|
||||
fs::copy_file(staged / "base" / "Base-grown.cppm.in", staged / "base" / "Base.cppm", fs::copy_options::overwrite_existing);
|
||||
// copy_file carries the source's mtime across, which would leave the
|
||||
|
|
@ -115,36 +113,24 @@ namespace {
|
|||
int main() {
|
||||
return 0;
|
||||
{
|
||||
// The scan records a primary interface unit's imports, and the
|
||||
// pending/resolve machinery reaches them the same way it reaches a
|
||||
// partition's.
|
||||
// The build records a primary interface unit's imports, and places
|
||||
// them on a dependency's module the same way it places a partition's.
|
||||
fs::path staged = StageFixture();
|
||||
std::unique_ptr<Configuration> base = MakeBaseLib(staged);
|
||||
std::unique_ptr<Configuration> widget = MakeWidgetLib(staged);
|
||||
widget->dependencies = { base.get() };
|
||||
|
||||
Module* mod = FindModule(*widget, "Widget");
|
||||
Check(mod != nullptr, "the primary interface unit registered a module named Widget");
|
||||
if (mod != nullptr) {
|
||||
Check(mod->partitions.empty(), "Widget has no partitions, so nothing else could carry the edge");
|
||||
Check(mod->externalModuleDependencies.empty(), "nothing resolvable before dependencies are assigned");
|
||||
Check(std::ranges::find(mod->pendingImports, "Base") != mod->pendingImports.end(), "the unresolved import of Base is remembered as pending");
|
||||
Check(std::ranges::find(mod->pendingImports, "std") != mod->pendingImports.end(), "std stays pending — it is supplied from outside the graph");
|
||||
|
||||
widget->dependencies = { base.get() };
|
||||
widget->ResolvePendingImports();
|
||||
|
||||
Check(mod->externalModuleDependencies.size() == 1, "ResolvePendingImports places the external module edge");
|
||||
if (mod->externalModuleDependencies.size() == 1) {
|
||||
Check(mod->externalModuleDependencies[0].first->name == "Base", "the edge names the Base module");
|
||||
Check(mod->externalModuleDependencies[0].second == base->PcmDir() / "Base.pcm", "the edge points at the dependency's BMI");
|
||||
if (BuildOk(*widget, "widget on base")) {
|
||||
Module* mod = FindModule(*widget, "Widget");
|
||||
Check(mod != nullptr, "the primary interface unit registered a module named Widget");
|
||||
if (mod != nullptr) {
|
||||
Check(mod->partitions.empty(), "Widget has no partitions, so nothing else could carry the edge");
|
||||
Check(mod->externalModuleDependencies.size() == 1, "the import of Base is one external module edge; std, supplied from outside the graph, has none");
|
||||
if (mod->externalModuleDependencies.size() == 1) {
|
||||
Check(mod->externalModuleDependencies[0].first->name == "Base", "the edge names the Base module");
|
||||
Check(mod->externalModuleDependencies[0].second == base->PcmDir() / "Base.pcm", "the edge points at the dependency's BMI");
|
||||
}
|
||||
}
|
||||
Check(std::ranges::find(mod->pendingImports, "Base") == mod->pendingImports.end(), "the resolved name is no longer pending");
|
||||
Check(std::ranges::find(mod->pendingImports, "std") != mod->pendingImports.end(), "std is still pending after the sweep");
|
||||
|
||||
// Build() runs a sweep unconditionally on top of whatever callers
|
||||
// already did, so a repeat must not duplicate the edge.
|
||||
widget->ResolvePendingImports();
|
||||
Check(mod->externalModuleDependencies.size() == 1, "ResolvePendingImports is idempotent");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -164,6 +150,18 @@ int main() {
|
|||
std::array<fs::path, 3> impls = { "widget/Widget", "base/Base", "main" };
|
||||
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
||||
|
||||
// Widget's import of its sibling is also the compile-ordering
|
||||
// guarantee: Widget's precompile needs Base.pcm on disk. Repeated from
|
||||
// scratch because the failure was a thread race, and one lucky pass
|
||||
// proves nothing.
|
||||
for (std::int32_t attempt = 0; attempt < 4; ++attempt) {
|
||||
fs::remove_all(cfg.BuildDir());
|
||||
fs::remove_all(cfg.BinDir());
|
||||
if (!BuildOk(cfg, std::format("sibling pass {}", attempt))) break;
|
||||
CheckAgrees(cfg.BinDir() / "sibling", std::format("sibling pass {}", attempt));
|
||||
}
|
||||
|
||||
// The edge itself, as the last pass read it.
|
||||
Module* widgetMod = FindModule(cfg, "Widget");
|
||||
Module* baseMod = FindModule(cfg, "Base");
|
||||
Check(widgetMod != nullptr && baseMod != nullptr, "both sibling modules registered");
|
||||
|
|
@ -172,16 +170,6 @@ int main() {
|
|||
Check(widgetMod->externalModuleDependencies.empty(), "a sibling in the same Configuration is not an external edge");
|
||||
Check(baseMod->moduleDependencies.empty(), "Base imports no sibling");
|
||||
}
|
||||
|
||||
// That edge is also the compile-ordering guarantee: Widget's precompile
|
||||
// needs Base.pcm on disk. Repeated from scratch because the failure was
|
||||
// a thread race, and one lucky pass proves nothing.
|
||||
for (std::int32_t attempt = 0; attempt < 4; ++attempt) {
|
||||
fs::remove_all(cfg.BuildDir());
|
||||
fs::remove_all(cfg.BinDir());
|
||||
if (!BuildOk(cfg, std::format("sibling pass {}", attempt))) break;
|
||||
CheckAgrees(cfg.BinDir() / "sibling", std::format("sibling pass {}", attempt));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue