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
|
|
@ -69,6 +69,15 @@ export namespace Crafter {
|
|||
// path is $ORIGIN/../lib, and WriteFlatpak copies what it needs from
|
||||
// these directories into files/lib.
|
||||
std::set<std::string> runPaths;
|
||||
// The configuration this is the build of: the one whose Build() ran.
|
||||
// A build pass builds a directory once, so of two configurations
|
||||
// that would be built into one, as the same project loaded along two
|
||||
// paths with its args in two orders is, only one is built, and only
|
||||
// that one has the modules the build read in the sources
|
||||
// (Configuration::interfaces). What is built on the other finds them
|
||||
// through this. Null where Build() stopped before it read the
|
||||
// sources.
|
||||
const struct Configuration* built = nullptr;
|
||||
};
|
||||
|
||||
struct Define {
|
||||
|
|
@ -509,6 +518,26 @@ export namespace Crafter {
|
|||
// sandbox permissions any configuration may declare for the
|
||||
// executables built on it.
|
||||
FlatpakApp flatpak;
|
||||
// The module interface units (.cppm) and the implementation units
|
||||
// (.cpp) of the configuration, as GetInterfacesAndImplementations
|
||||
// lists them: absolute, with their extensions.
|
||||
std::vector<fs::path> interfaceSources;
|
||||
std::vector<fs::path> implementationSources;
|
||||
// What those sources are, as Build() found them the last time it read
|
||||
// all of them without an error: each module whose primary interface
|
||||
// unit is among interfaceSources, with its partitions, and a unit for
|
||||
// each of implementationSources, with what each of them imports.
|
||||
// Build() takes what a source declares and imports from
|
||||
// clang-scan-deps, which preprocesses the source with the command it
|
||||
// is compiled with, so it reads them as the compiler does: one in a
|
||||
// string literal, in a comment or in a branch that #if leaves out is
|
||||
// not there. It reads them once the configuration's dependencies and
|
||||
// external dependencies are built, since the command takes their
|
||||
// include paths and a source may include their headers. Empty until a
|
||||
// Build() has read the sources. A pass builds a directory once, so a
|
||||
// configuration whose directory it built for another keeps what it
|
||||
// had, and what is built on it finds the modules of that build
|
||||
// through BuildResult::built.
|
||||
std::vector<std::unique_ptr<Module>> interfaces;
|
||||
std::vector<Implementation> implementations;
|
||||
std::vector<fs::path> cFiles;
|
||||
|
|
@ -602,16 +631,14 @@ export namespace Crafter {
|
|||
// read, for what that one is built against (see UpdateAgentsMd). Not
|
||||
// part of VariantId: the notes change nothing that is compiled.
|
||||
DependencyNotes dependencyNotes = DependencyNotes::Transitive;
|
||||
// Adds the units `interfaces` and `implementations` name to
|
||||
// interfaceSources and implementationSources. Each is named without
|
||||
// its extension, .cppm for an interface unit and .cpp for an
|
||||
// implementation unit, and resolved against `path` and the working
|
||||
// directory as they are at the call. Reads none of them: Build()
|
||||
// does, with the command the configuration has by then, its defines,
|
||||
// flags and dependencies set after this call included.
|
||||
void GetInterfacesAndImplementations(std::span<fs::path> interfaces, std::span<fs::path> implementations);
|
||||
// Retry the `import X;` names GetInterfacesAndImplementations could not
|
||||
// place, against the dependency DAG as it stands now. Sources are
|
||||
// scanned when they're declared, but `dependencies` is often assigned
|
||||
// afterwards (AddTest does exactly this), and an import that resolved to
|
||||
// nothing leaves no staleness edge — so a dependency's interface could
|
||||
// change and this Configuration's objects would be silently reused
|
||||
// against the new layout. Build() calls this before checking mtimes;
|
||||
// calling it again is harmless.
|
||||
void ResolvePendingImports();
|
||||
// Declare a test. Sources default to `tests/<name>/main.cpp` resolved
|
||||
// against this Configuration's path; target/march/mtune/sysroot/debug
|
||||
// and cudaArch are inherited from this Configuration so cross-arch
|
||||
|
|
|
|||
Loading…
Reference in a new issue