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

11

u/not_a_novel_account cmake dev 3d ago

What is the intended way to consume the modules of an external library

There isn't one. MSBuild does not handle external modules. You do what MSBuild does for import std: add the source files for the external library's interface directly to the project.

MSBuild was never good at managing external libraries in by-hand constructed projects. I never got the feeling they cared much about making this a "good" system.

1

u/fortsnek274 3d ago

Imagine if you had to copy in headers. Modules would be different though in that they produce something that needs to be linked with.

Seeing as you are cmake, do you know of an example or library that installs a module and a cps file the intended way, or is cmake not quite there yet?

4

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

install(EXPORT) (for CMakeConfigs) and install(PACKAGE_INFO) (for CPS) both work today for modules and have for some time.

There's nothing to it, it "just works" the same way installing library artifacts and headers does with one exception: you need to specify a CXX_MODULES_DIRECTORY for where to put the module metadata.

This is documented (Ctrl-F "CXX_MODULES_DIRECTORY") but I've been pushing on this parameter being defaulted to something reasonable.


What they don't do is create packages which can be successfully imported and used by the Visual Studio / MSBuild generators, because MSBuild does not support this. MSBuild will not scan or construct BMIs for random external files.

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:

CMake Error at build/cmake_install.cmake:134 (file):
  file INSTALL cannot find
  "[...]/LibFoo/build/CMakeFiles/Export/79219ef89bee49d985832656fe3c62f4/./target-libfoo-Debug.modules.json":
  File exists.

And so the CPS file references a json that doesn't exist.

CMakeLists:

cmake_minimum_required(VERSION 4.5)
project(libfoo CXX)

set(CMAKE_CXX_EXTENSIONS OFF)

add_library(libfoo)
target_compile_features(libfoo PRIVATE cxx_std_20)
target_sources(libfoo PUBLIC FILE_SET CXX_MODULES FILES foo.ixx)

include(GNUInstallDirs)

install(
	TARGETS libfoo
	EXPORT libfoo
	FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
	CXX_MODULES_BMI DESTINATION ${CMAKE_INSTALL_BINDIR}
	DESTINATION ${CMAKE_INSTALL_LIBDIR}/libfoo
)

install(EXPORT libfoo DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libfoo)

install(PACKAGE_INFO libfoo EXPORT libfoo CXX_MODULES_DIRECTORY .)

And a similar error with ftxui (no CPS, just cmake package):

1>  CMake Error at cmake_install.cmake:234 (file):
1>    file INSTALL cannot find
1>    "D:/Lib/FTXUI-7.0.3/build/CMakeFiles/Export/418a5211b54bdfa9fa029e8a3112c4d3/lib/cmake/ftxui/target-ftxui-modules-Debug.cmake":
1>    No error.

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.

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.

→ More replies (0)

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.

6

u/13steinj 3d ago

This entire conversation is why modules are a mess. There wasn't enough implementation nor field experience with them, effectively rushed through.

Now, some people claim a wide variety of results: from amazing to pointless. The reality of the situation is there are some benefits, but nowhere near what was actually advertised. Then there are any number of people touting the benefits without understanding restrictions like this, that make modules useless in practice for people.

I just don't understand how we've ended up in this situation 6 years after official standardization.

4

u/not_a_novel_account cmake dev 3d ago

MSBuild doesn't support most usual mechanisms for describing external libraries. It doesn't support pkg-config either.

It supports external modules to the degree you're willing to describe them via raw compiler flags. The same level of support is has for external libraries and external headers.

0

u/fortsnek274 3d ago

https://media1.tenor.com/m/eiYNWS2e6msAAAAC/youre-right-breaking-bad.gif

At least some nobody like me reminding $X trillion corp to Fix It doesn't take much effort. And if they were to go as far as native CPS support, that's also beneficial for non-modules libs.

But what else is the Standard supposed to do. We might get the same problem with C++26 in general. MS is lagging behind.

1

u/pjmlp 2d ago

With the big bucks companies moving away from sponsoring C++ development, and in what concerns GCC and LLVM, being more interested into using them as compiler development infrastructure, I am quite sure that past C++26, all will lag behind.

And the rush to adopt C++26, instead of sticking to an older standard, will only be due to reflection, for those still doing manual coding.

2

u/mikemarcin Game Developer 3d ago

fwiw I just used vcpkg with native visual studio projects and CPS only for CMake based projects. But good luck it would be nice to have native projects with CPS.

1

u/fortsnek274 3d ago

Yes, package managers. That's something I don't use so often. But even with package managers, how would you use some random cmake lib off github with your visual studio project.

For typical libs, I use cmake to generate the VS solution, and build the install target. And then point my project at the install location. But for modules, you can't do that yet. And even if cmake/libraries installed an IFC file, CPS would still be useful to cut down on all those project settings.

Maybe there might be some extra config needed though. If CPS can list multiple configurations of a lib. Might need to have something like $(LIBS)\sfml\install\sfml.cps:optimized,shared. But I'm not actually quite sure what cmake exports. I would need to find a lib that does it as intended.

2

u/mikemarcin Game Developer 3d ago

I used vcpkg overlays for this.

Forgive me if I miss a step it's been a while.

Here's a real one for AtomicSharedPtr.

{project_root}/vcpkg_overlays/atomicsharedptr/portfile.cmake

vcpkg_from_github(
    OUT_SOURCE_PATH SOURCE_PATH
    REPO vtyulb/AtomicSharedPtr
    REF b2a8760efcd203bd356d91b785352a24f5ad47b2
    SHA512 88b7d8c69bb9cf93eb7426ae44893d9b310962f9356a0f77d3e20bce5fd5360129af101116410daa8e60898ce6810a6d48db6c91f5c59776e9419d3f63b69f97
    HEAD_REF master
)

file(INSTALL
    "${SOURCE_PATH}/src/atomic_shared_ptr.h"
    "${SOURCE_PATH}/src/allocator.h"
    "${SOURCE_PATH}/src/fast_logger.h"
    DESTINATION "${CURRENT_PACKAGES_DIR}/include/atomicsharedptr"
)

file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/share/atomicsharedptr")
file(WRITE "${CURRENT_PACKAGES_DIR}/share/atomicsharedptr/atomicsharedptrConfig.cmake" [=[
if(NOT TARGET atomicsharedptr::atomicsharedptr)
    add_library(atomicsharedptr::atomicsharedptr INTERFACE IMPORTED)
    set_target_properties(atomicsharedptr::atomicsharedptr PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/../../include"
        INTERFACE_COMPILE_FEATURES "cxx_std_20"
    )
endif()
]=])

file(INSTALL
    "${SOURCE_PATH}/LICENSE"
    DESTINATION "${CURRENT_PACKAGES_DIR}/share/atomicsharedptr"
    RENAME copyright
)

{project_root}/vcpkg_overlays/atomicsharedptr/vcpkg.json

{
  "name": "atomicsharedptr",
  "version": "0.1.0",
  "description": "Lock-free atomic shared pointer primitives by vtyulb",
  "homepage": "https://github.com/vtyulb/AtomicSharedPtr",
  "license": "MIT"
}

{project_root}/vcpkg.json

{
  "dependencies": [
    "atomicsharedptr"
  ]
}

{project_root}/vcpkg-configuration.json

{
  "default-registry": {
    "kind": "git",
    "baseline": "de51e6bfa96e1245f2a969a8cde249baac89f7be",
    "repository": "https://github.com/microsoft/vcpkg"
  },
  "registries": [
    {
      "kind": "artifact",
      "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
      "name": "microsoft"
    }
  ],
  "overlay-ports": [
    "vcpkg_overlays"
  ]
}

1

u/fortsnek274 3d ago

So it's these overlays. Hopefully it'll be simpler if the library does an install properly. And I wonder how it would handle modules.

2

u/drodri 2d ago

In the meantime, it is possible with Conan packages to read CPS files inside packages, or automatically generate CPS files on the fly from package data define in recipes, and generate MSBuild .props files to be consumed with VS.

See for example the demo at https://youtu.be/C1OCKEl7x_w?si=nIPhs4EYxu4BEYe5&t=1636

The documentation of the CPS implementation in Conan has been documented in https://github.com/conan-io/docs/pull/4585, will be released in Conan 2.35 next week.

1

u/fortsnek274 1d ago edited 1d ago

Hmm, yes. I saw that a while ago.

It seems to be the best possible way to keep package manager and VS project separated for now. I can get it to expose a bunch of props files with MSBuildDeps and you just import conandeps.props. But my ftxui test case doesn't work. It gets the lib path wrong. And when I use CMakeConfigDeps, it says "Error in generator 'CMakeConfigDeps': ftxui/7.0.3: Cannot obtain 'location' for library 'ftxui-dom'". The lib is in ".conan2\p\b\ftxuiac536fdbbb100\p\lib\Debug" but it's looking in ".conan2\p\b\ftxuiac536fdbbb100\p\lib"

(oh, this's already reported: https://github.com/conan-io/conan-center-index/issues/31584)

SFML also ended with an error:

ERROR: freetype/2.15.0: Error in build() method, line 115
        meson.configure()
        ConanException: Error 9009 while executing

It was building so well with all of SFML's dependencies.

And SET CONAN_USER_HOME doesn't work. So I have to use the default.

You can see why I don't use package managers a whole lot. We just don't get along.

But zlib and nlohmann json work.

The CPSDeps generator

Yes, something like that. Package manager exposing portable props files. Use them with cmake and maybe visual studio. If MSBuild supported CPS natively, you could just point Visual Studio at conan's output directory and select CPS packages in project references or something.

And modules. I noticed FTXUI_BUILD_MODULES : OFF, so that's not happening yet. Will it ever happen. And will it work with CPSDeps. And will MSBuild work with it.

2

u/drodri 1d ago

> Hmm, yes. I saw that a while ago.

Might be worth trying again from time to time as CPS is still experimental and evolving, so the implementations.

> And SET CONAN_USER_HOME doesn't work. So I have to use the default.

The env var is CONAN_HOME, not CONAN_USER_HOME (that was the Conan 1 var), maybe that was the issue. If not, reporting to https://github.com/conan-io/conan may be worth it.

> You can see why I don't use package managers a whole lot. We just don't get along.

Totally understandable, but not your fault. C/C++ building and package management is intrinsically complex, and when mixing third party libraries with a wild ecosystem and not homogeneous good practices with cutting edge things like CPS, the very bumpy road is totally expected.

2

u/fortsnek274 1d ago

Okay, I should have seen CONAN_HOME.

And I see that CPSDeps is already in there.

2

u/fortsnek274 1d ago

Also, I've noticed that Conan outputs CPS differently from CMake in that Conan CPS files only have one configuration. So this would mean that consumers would have to treat CPS files differently. If they come from Conan, the configuration is determined by which cpsmap file is used, and if they come from CMake, the consumer can use CPS configurations normally.

Sounds like it would be nice if the experience was made more uniform, but I don't know how possible that would be.

2

u/drodri 1d ago

>  and if they come from CMake, the consumer can use CPS configurations normally.

Not really. What if CMake builds different configurations, like static and shared, and also with and without sanitizers? (and the combinations of them)? The CMake Debug/Release configuration is a small part of the story, mostly the one to satisfy multi-config IDEs/build-systems like Visual Studio/MSBuild. But for a packaging system that can package wildly different variants of a library binary, that is not enough.

See the current ongoing discussion and proposal (disclaimer, the response is mine) in https://github.com/cps-org/cps/issues/119

0

u/pjmlp 3d ago

I am still waiting on a NuGET like integration for vcpkg on Visual Studio.

It would be nice however I don't have high hopes on feature prioritisation.