r/cpp • u/fortsnek274 • 3d ago
RFC: Using external modules or CPS with Visual Studio
Seeking feedback on this suggestion I'll hopefully make to devcom. I barely know what I'm doing, so I probably missed something. Do things work the way I think they do...
Visual Studio/MSBuild should have a way to easily consume modules and/or CPS from external libraries
What is the intended way to consume the modules of an external library? I'm not sure. With header-based libs, you just add to the include and lib paths, and away you go. Maybe stick that stuff in a props file. But for external modules? Are you supposed to reference a pre-built IFC directly?
Instead, external module sources could be discovered and built as needed by the project system. So, external modules would retain the familiar user experience of headers. You just point the project at module sources and it should Just Work.
It would also mean that the library's CMake script would not need a separate compilation of modules to a static library or IFC. A binary distribution probably shouldn't contain an IFC anyway as they may be so much more compiler-dependent than ABI.
Method 1
Add a "additional module source directories" project setting. The build system will lookup all cppm/ixx files in that directory to satisfy imports.
Method 2
Go all-in on the Common Package Specification. Add a "CPS files" project setting. The build system will collect and maybe cache information from listed CPS files to resolve includes and imports, and add additional compile settings. CPS files may use https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3286r0.pdf to list modules.
And the build system can automatically link the libraries listed in those CPS files. Maybe only if a header or module from them is used.
The advantage of this method is that it's a single setting for a library. You'd only need to list the CPS in project settings, and nothing else. The CPS provides the include paths, the libs, preprocessor defs, and module sources.
Method 3
Dedicated external CPS project type. Like method 2, but the project would make available the listed CPS packages, their includes, libs, modules, and preprocessor defs, to referencing projects through the project reference system. Such a project would also act as a central place to put the compiled modules rather than possibly duplicating them among referencing projects.
Or some other method. In essence, external modules should be easy to use, and CPS files should be the future, as maybe exported by CMake. So the project system should handle that stuff in some way.
Also, the project reference system should try to reuse compiled modules if possible, just like with import std.
View all comments
Show parent comment
1
u/fortsnek274 3d ago
And is there an example that works. I've got my little test lib, and for some reason, it installs the cps file, but I get:
And so the CPS file references a json that doesn't exist.
CMakeLists:
And a similar error with ftxui (no CPS, just cmake package):
Latest cmake 4.5.1.
And yes, I certainly don't expect it to work with VS. Hence, adding support. And maybe if MSBuild supported CPS, that would be how vcpkg exposes packages to MSBuild.
But seeing as cmake will build a lib for a module, then it wouldn't be used if MSBuild builds the module itself from sources. So who builds the module. This C++ ecosystem problem. If cmake builds the module, then it should also install the IFC/BMI to go along with the lib file. But would that unnecessarily tie a binary release of a lib to a specific compiler version or specific compiler flags. Or, should the module be built with the importer's flags and cmake should avoid building the lib itself. Just a bare source file install with a CPS/JSON. Like a header-only lib.