r/cpp • • 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.

6 Upvotes

28 comments sorted by

View all comments

Show parent comment

2

u/not_a_novel_account cmake dev 3d ago edited 3d ago

If cmake builds the module, then it should also install the IFC/BMI to go along with the lib file

No it shouldn't. We're deprecating that option anyway. It's low on my todo list because it has exactly zero consumers and no impact on CPS.


Your example works for me:

$ cmake --version                       
cmake version 4.5.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ cmake -GNinja -B build
-- The CXX compiler identification is MSVC 19.52.37029.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/.../cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.7s)
-- Generating done (0.0s)
-- Build files have been written to: C:/.../build
$ cmake --build build
[1/7] Scanning "C:\...\modules\std.compat.ixx" for CXX dependencies
std.compat.ixx
[2/7] Scanning "C:\...\modules\std.ixx" for CXX dependencies
std.ixx
[4/7] Scanning C:\...\foo.ixx for CXX dependencies
foo.ixx
[6/7] Building CXX object CMakeFiles\libfoo.dir\foo.ixx.obj
foo.ixx
[7/7] Linking CXX static library libfoo.lib
$ cmake --install build --prefix install                     
-- Install configuration: "Debug"
-- Installing: C:/.../install/lib/libfoo.lib
-- Installing: C:/.../install/include/foo.ixx
-- Installing: C:/.../install/lib/libfoo/foo.ifc
-- Installing: C:/.../install/lib/cmake/libfoo/libfoo.cmake
-- Installing: C:/.../install/lib/cmake/libfoo/libfoo-debug.cmake
-- Installing: C:/.../install/cps/libfoo/libfoo.cps
-- Installing: C:/.../install/cps/libfoo/libfoo@debug.cps
-- Installing: C:/.../install/cps/libfoo/target-libfoo-Debug.modules.json

EDIT: Oooooh you're trying to use MSBuild to produce a CPS file. That's a bug. It's broken. It's not going to be fixed any time soon. The .modules.json is produced by the scan step, which MSBuild doesn't have. CMake + MSBuild is effectively preview support of modules, it doesn't support 95% of CMake's modules features.

2

u/fortsnek274 3d ago

Oooooh you're trying to use MSBuild to produce a CPS file. That's a bug. It's broken.

Oh, that's good to know isn't it. Would be nice if cmake stopped me from doing that!

Well, I've got ninja handy and that seems to work just fine.

No it shouldn't. We're deprecating that option anyway.

Trying with FTXUI, I see now:

CMake Error in CMakeLists.txt:
  Target "ftxui-modules@synth_0" contains C++ modules intended for BMI-only
  compilation.  This is not yet supported by the Visual Studio generator.

So cmake gets a static lib built for the module interface when building the library, and then users of the module only build the IFC/BMI so that you don't have a case where you've got two libs: one built by the importer and one referenced by the CPS.

So that is what MSBuild will have to do if it natively supported CPS.

1

u/not_a_novel_account cmake dev 3d ago

I want to rip out the claim of support for modules on MSBuild entirely.

The problem is CMake supported a very, very limited set of module operations out the gate. This extremely limited set happened to line up with what could be supported by MSBuild, so claiming "CMake supports modules with MSBuild" was kinda-sorta true, as true as it was everywhere else.

Personally I think this was a mistake.

Because as new features and broader support was added, nothing works on MSBuild. Ninja is the only real CMake generator for those interested in cutting-edge new features, everything else is rapidly falling behind and exists for legacy users who can't use Ninja (and aren't interetested in new behavior anyway).

It would be better if module builds under MSBuild just failed, rather than crossing our fingers the two things it can do correctly happen to be what you want.

1

u/fortsnek274 3d ago

The future of C++: MSBuild just never got along with CMake, and so I can't use the Open Project button if the lib exports a module. It's good for at least browsing the code. Or should I be braving VS's built-in CMake support with flaky modules intellisense.

1

u/not_a_novel_account cmake dev 3d ago

VS's built-in CMake support is excellent, first-class.

The problem is MS invented the Language Server Protocol, then subsequently disregarded it for modules because EDG isn't there yet.

If you're not married to VS, clice is the new mature LS implementation for C++ (and C++-like languages) and works in all dev environments that let you bring-your-own-language-server (which is all of them except VS, VSC works fine).

Clice consumes CMake-generated compile databases and modules flawlessly in my experience.

2

u/fortsnek274 3d ago

Yes, increasingly, I see that everybody else has it better than VS users. Maybe the switchover to CLion may happen one day.

2

u/delta_p_delta_x 2d ago

If you want to stick to VS, use ReSharper for modules. Works quite well in my opinion.

1

u/GabrielDosReis 1d ago

I want to rip out the claim of support for modules on MSBuild entirely.

That support was very useful for getting some projects started. I know I leaned into that support when the Ninja part was still trailing.

Because as new features and broader support was added, nothing works on MSBuild.

Living with history is hard. People don't want to wait for the train to be perfect before jumping on it (and in fact, as engineers, we want people to jump on the train early so we get feedback on scaling issues). Once people are on the train and they feel it is moving just right for them, completeness arguments become difficult if they never exercised completeness and are unaffected by it.

1

u/not_a_novel_account cmake dev 1d ago

MSBuild is obviously capable of describing arbitrary operations. We can make it work. But people expect "native" integration, and if I just make the module support a bunch of opaque commands that MSBuild runs then nothing in VS understands the codebase anymore and the intellisense breaks.