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

0 Upvotes

9 comments sorted by

•

u/not_a_novel_account cmake dev 2h 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.

•

u/fortsnek274 2h 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?

•

u/not_a_novel_account cmake dev 1h ago edited 1h 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.

•

u/fortsnek274 34m 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/mikemarcin Game Developer 4h 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.

•

u/fortsnek274 3h 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.

•

u/mikemarcin Game Developer 3h 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"
  ]
}

•

u/fortsnek274 2h 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.

•

u/pjmlp 3h 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.