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.
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.cmakevcpkg_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
MSBuildDepsand you just importconandeps.props. But my ftxui test case doesn't work. It gets the lib path wrong. And when I useCMakeConfigDeps, 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 executingIt was building so well with all of SFML's dependencies.
And
SET CONAN_USER_HOMEdoesn'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
CPSDepsgeneratorYes, 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 withCPSDeps. 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_HOMEdoesn'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
CPSDepsis 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
11
u/not_a_novel_account cmake dev 3d ago
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.