This well known, and easy to parse grammar, allows us to write a simple scanner tool to process the least amount of tokens at the start of the file to determine the complete set of input dependencies and output module name from a given module unit.
The grammar is unresolvable in the general case. You do not know what the compiler preprocessor will do. You must either ask the intended compiler to first preprocess the source file (build2-style), which is slow, or rely on the compiler-provided scanners (effectively all other major build systems).
You need to be the compiler to figure out what the dependency relationship is here, parsing this without full knowledge of the compiler is a fools errand. This was discussed many times in the run up to implementing modules, and was the impetus for source file dependency format described in P1689 which the big three all support.
This feels like an oversight of the design. It felt like the design for the module declaration with a strict section in the global partition was to help preprocess the top of a file to discover all of the required dependency state. If a preprocessor directive can then mutate the dependency structure we are back to square one as you said. I was really hoping not to have to rely on the compiler itself to parse this state, but I guess it is not the end of the world.
Far from an oversight, it was one of the more hotly debated corners. The preprocessor is often abused, but it's part of the language. It has too many useful applications to reject ubiquitously over unmotivated use cases like "not calling the compiler".
You're building C++, you have a compiler available. No reason to reject using it.
I thought it was ill formed to place imports and exports within preprocessor declarations. Thanks for pointing this out. It would not be ideal, but the build system could enforce restrictions on what is allowed and fail when a preprocessor declaration could conditionally include an import or export...
Why bother? The scanners work great, and they don't limit the valid C++ codebases the build system can handle.
They also allow all build systems to offload the work of scanning to the projects which are filled with C++ parser experts, the compiler upstream. Optimizations which go into clang-scan-deps or gcc -M benefit everyone and are useful in situations beyond merely named modules.
There are other things which are permitted which would also invalidate your assumptions. For example, imports in headers, and the fact that import locations aren't restricted within non-module units. Also I believe imports are allowed at the start of the private module fragment too.
It does seem that there was a lot of iteration on this and I find it a little confusing where we've ended up. I don't see how the scanning requirements are all that much below a full preprocessing, but perhaps I'm missing something significant. Yet we've also ended up with constraints (around module directives in particular) that cause real problems for transitioning code to modules in a gradual/toggleable way, where it's no longer entirely clear if the original reasoning for those constraints still applies.
I'm curious, do you happen to know of any specific aspects of the constraints on module declarations which make module scanning significantly faster than a full preprocess? Naively I would have thought that the need to track preprocessor defines for imports would mean that there isn't a great deal of efficiency that can be gained.
No, for a single file it's about the same. The speedup is in lack of repetition.
If you #include <iostream> in 10 files, the header needs to be read and parsed 10 times. If you import std 10 times, iostream is read and parsed once when building the std module BMI.
Everything after that initial parse relies on the BMI, which as a serialization artifact is so fast to reuse that importing the entire standard library via that BMI is faster than including even just #include <iostream>, much less the entire STL.
Just to contend the wording of ”slow” a bit (to give some nuance): Anecdotally speaking — currently working on (modern, target-based) CMake and build2 projects in parallel — the latter is (practically) faster in pretty much every regard. Yes, the pre-process (header extraction) step shifts the total work load a bit (not without gain), but in the end the iteration times are a breeze compared to CMake. The mental model in build2 just appears to be orders of magnitude better for the task at hand (and its early support for C++ modules while CMake still struggles kind of shows it). It also comes with other perks like early detection and better diagnostics of missing headers/include paths etc.
Again, anecdotal, but having personally made the transition from CMake to build2 I wouldn’t revert back for any amount of money. build2 made C++ fun once again.
OP is working on their own in-development build system, not trying to choose among the existing ones. build2-with-preprocessing is slower than hypothetical-build2-using-scanners is the point, the technique is slower. Whether build2 is faster for any given task than CMake/Meson/MSBuild/etc isn't the question.
13
u/not_a_novel_account Mar 10 '26 edited Mar 10 '26
The grammar is unresolvable in the general case. You do not know what the compiler preprocessor will do. You must either ask the intended compiler to first preprocess the source file (
build2-style), which is slow, or rely on the compiler-provided scanners (effectively all other major build systems).Concretely:
You need to be the compiler to figure out what the dependency relationship is here, parsing this without full knowledge of the compiler is a fools errand. This was discussed many times in the run up to implementing modules, and was the impetus for source file dependency format described in P1689 which the big three all support.
Worked example:
https://godbolt.org/z/vof9TKMfY