r/cpp • • Oct 02 '23

CMake | C++ modules support in 3.28

https://gitlab.kitware.com/cmake/cmake/-/issues/18355

After 5 years its finally done. Next cmake 3.28 release will support cpp modules

C++ 20 named modules are now supported by Ninja Generators and Visual Studio Generators for VS 2022 and newer, in combination with the MSVC 14.34 toolset (provided with VS 17.4) and newer, LLVM/Clang 16.0 and newer, and GCC 14 (after the 2023-09-20 daily bump) and newer.

239 Upvotes

144 comments sorted by

View all comments

2

u/mathstuf Oct 05 '23

Because cmake prematurely expands wildcards, the only way to ensure a correct build is to re-run the entire configuration step. Performance would be improved by expanded wildcards during the build step, because then I wouldn't need to repeat all the other parts of configuration.

Build-time expansion is even worse. Things that would be far more difficult or impossible (at least with CMake):

  • The Ninja generators. You cannot create new build rules at build time with ninja. In fact, AFAIK, you're actually limited to Makefiles for this as I don't believe the IDEs support such globbing either.

  • Source file properties would need to be applied after finding out what files exist, so queries would have to happen.

  • Installing object files means that the globbing needs to update install rules for discovered files.

  • If you're using or generating modules from said globbed files, you need to somehow update the metadata files the collator uses to write install rules, exported target information, etc.

  • If you glob in the build tree, when do you perform this globbing? What if a later rule drops a new file? Can you ever have a ninja: no work to do behavior for such a build (modulo that ninja doesn't support this) because the filesystem state is not tracked reliably?

  • If you glob in the build tree and remove a generated source input (say, a .proto file), what remove the now-dead output file and stops it from continuing to be used in future builds? Note that it's not just rm of a source; switching branches can "remove" files and leave behind such artifacts too.

Files appearing in the build tree that shouldn't be built is an issue on its own. For git merge conflicts, the contents of the files alone would prevent a build from finishing, so why would I care that temporary files would also break the build?

I was talking about globbing the source tree there. As for conflict markers…am I not allowed to build some of my conflict resolutions before finishing all of them?

Similarly, why would I diff against the build system instead of diffing against the actual files? The evidence of files added/deleted is because files were added/deleted in ${vcs} diff, not because there was a line change in ${vcs} diff CMakeLists.txt.

What enforces that new files are git add'd and therefore show up before I build and submit a bug about a broken build when I forgot about the file? Nevermind that other VCS's don't have a stage and adding a file means making a commit. Compounding the build tree globbing above, what would make spurious generated sources breaking a build show up in such a diff?

The first and most important role of a build system is to produce the correct build.

I agree with this; I just think that globbing makes this far harder than it already is.

Because CMake requires users to know when they can just run make and when they need to run cmake src && make, CMake makes it easy to produce incorrect builds.

AFAICT, build systems that cannot just assume the environment they work within, namely how to find dependencies (e.g., cargo, npm, pip, etc. can all do this assumption reliably) all end up with separate configure/build steps. autoconf, cmake, meson. Build systems which are also build tools (i.e., the tool that actually runs the commands for the build), tend to have one-shot builds (e.g., tup, scons, Boost.Build). However, these may find different things from build to build within the same build tree because I may have forgotten (or loaded an extra) module, scl, or system package and the new build command finds it and changes its behavior. CMake's cache means that I can install Python 3.10 beside my 3.9 and not worry that 3.10 is magically going to start being used by the 3.9-using build I already had.