Skip to content

Commit a00ce48

Browse files
committed
CMakeConfigDeps
1 parent 8f51d29 commit a00ce48

File tree

8 files changed

+56
-19
lines changed

8 files changed

+56
-19
lines changed

cppython/build/backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ def _prepare_and_get_result(
162162

163163
if result.sync_data is not None:
164164
if isinstance(result.sync_data, CMakeSyncData):
165-
settings = _inject_cmake_toolchain(config_settings, result.sync_data.toolchain_file)
165+
settings = _inject_cmake_toolchain(settings, result.sync_data.toolchain_file)
166166
elif isinstance(result.sync_data, MesonSyncData):
167-
settings = _inject_meson_files(config_settings, result.sync_data.native_file, result.sync_data.cross_file)
167+
settings = _inject_meson_files(settings, result.sync_data.native_file, result.sync_data.cross_file)
168168

169169
return result, settings
170170

cppython/plugins/conan/builder.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ def _create_conanfile(
8383
) -> None:
8484
"""Creates a conanfile.py file that inherits from CPPython base."""
8585
class_name = name.replace('-', '_').title().replace('_', '')
86-
content = f'''import os
87-
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain
86+
content = f'''from conan.tools.cmake import CMake, CMakeConfigDeps, CMakeToolchain
8887
from conan.tools.files import copy
8988
89+
import os
90+
9091
from conanfile_base import CPPythonBase
9192
9293
@@ -125,7 +126,7 @@ def layout(self):
125126
super().layout() # Get CPPython managed layout
126127
127128
def generate(self):
128-
deps = CMakeDeps(self)
129+
deps = CMakeConfigDeps(self)
129130
deps.generate()
130131
tc = CMakeToolchain(self)
131132
tc.user_presets_path = None
@@ -142,9 +143,11 @@ def package(self):
142143
143144
def package_info(self):
144145
# Use native CMake config files to preserve FILE_SET information for C++ modules
145-
# This tells CMakeDeps to skip generating files and use the package's native config
146+
# This tells CMakeConfigDeps to skip generating files and use the package's native config
146147
self.cpp_info.set_property("cmake_find_mode", "none")
147-
self.cpp_info.builddirs = ["."]
148+
# Point CMakeConfigDeps to the directory containing the native config files
149+
# so conan_cmakedeps_paths.cmake populates the search paths for find_package()
150+
self.cpp_info.builddirs.append(os.path.join("lib", "cmake", self.name))
148151
149152
def export_sources(self):
150153
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)

cppython/plugins/conan/plugin.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ def _run_conan_install(self, conanfile_path: Path, update: bool, build_type: str
176176
# Add build type setting if specified
177177
if build_type:
178178
command_args.extend(['-s', f'build_type={build_type}'])
179+
# Enable CMakeConfigDeps (the modern CMake config-mode generator)
180+
command_args.extend(['-c', 'tools.cmake.cmakedeps:new=will_break_next'])
181+
182+
# Enable 'import std;' support by providing the experimental UUID in the toolchain
183+
# The UUID must be in the toolchain file (before try_compile block) so compiler
184+
# detection can create __CMAKE::CXX23 for projects using 'import std;'
185+
command_args.extend(
186+
[
187+
'-c',
188+
'tools.cmake.cmaketoolchain:extra_variables={'
189+
"'CMAKE_EXPERIMENTAL_CXX_IMPORT_STD': 'd0edc3af-4c50-42ea-a356-e2862fe7a444'"
190+
'}',
191+
]
192+
)
179193

180194
# Add cmake binary configuration if specified
181195
if self._cmake_binary:
@@ -380,6 +394,30 @@ def _run_conan_create(self, conanfile_path: Path, build_type: str, logger: Logge
380394
command_args.extend(['-c', 'tools.graph:skip_test=True'])
381395
command_args.extend(['-c', 'tools.build:skip_test=True'])
382396

397+
# Enable CMakeConfigDeps (the modern CMake config-mode generator)
398+
command_args.extend(['-c', 'tools.cmake.cmakedeps:new=will_break_next'])
399+
400+
# Force Ninja Multi-Config generator for C++ module support
401+
# The Visual Studio generator does not support BMI-only compilation
402+
# needed for consuming C++ modules across package boundaries
403+
command_args.extend(['-c', 'tools.cmake.cmaketoolchain:generator=Ninja Multi-Config'])
404+
405+
# Enable 'import std;' support in the CMake toolchain
406+
# CMAKE_EXPERIMENTAL_CXX_IMPORT_STD must be in the toolchain file (before
407+
# the try_compile block) so compiler detection can create __CMAKE::CXX23.
408+
# Note: CMAKE_CXX_MODULE_STD must NOT be in the toolchain because it would
409+
# cause ABI detection try_compile to fail (chicken-and-egg with __CMAKE::CXX23).
410+
# The UUID is specific to the CMake version and will need updating
411+
# when the CMake version changes until import std graduates from experimental.
412+
command_args.extend(
413+
[
414+
'-c',
415+
'tools.cmake.cmaketoolchain:extra_variables={'
416+
"'CMAKE_EXPERIMENTAL_CXX_IMPORT_STD': 'd0edc3af-4c50-42ea-a356-e2862fe7a444'"
417+
'}',
418+
]
419+
)
420+
383421
# Add build type setting
384422
command_args.extend(['-s', f'build_type={build_type}'])
385423

docs/plugins/conan/integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ You can customize this file for package metadata like name, version, and setting
2727
If you have an existing `conanfile.py`, back it up and run `cppython install` to generate both files. Then update your conanfile to inherit from `CPPythonBase`.
2828

2929
```python
30-
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
30+
from conan.tools.cmake import CMake, CMakeConfigDeps, CMakeToolchain, cmake_layout
3131
from conanfile_base import CPPythonBase # Import the base class
3232

3333

examples/conan_cmake/library/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ install(
4646
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mathutils
4747
)
4848

49-
# Create empty include directory to satisfy CMake's exported target requirements
50-
# (module-only library with no headers, but CMake expects the directory to exist)
51-
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
52-
5349
# Export and package configuration
5450
install(
5551
EXPORT mathutilsTargets

examples/conan_cmake/library/test_package/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
cmake_minimum_required(VERSION 4.0)
22

3-
# Enable std module support for MSVC - MUST be before project()
4-
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
5-
63
project(MathUtilsConsumer LANGUAGES CXX)
74

8-
set(CMAKE_CXX_STANDARD 23)
9-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
# Enable 'import std;' support (requires __CMAKE::CXX23 from compiler detection)
106
set(CMAKE_CXX_MODULE_STD ON)
117

128
find_package(mathutils REQUIRED)

examples/conan_cmake/library/test_package/conanfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from conan import ConanFile
66
from conan.tools.build import can_run
7-
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
7+
from conan.tools.cmake import CMake, CMakeConfigDeps, CMakeToolchain, cmake_layout
88

99

1010
class MathUtilsTestConan(ConanFile):
@@ -22,7 +22,7 @@ def layout(self):
2222

2323
def generate(self):
2424
"""Generate CMake dependencies and toolchain."""
25-
deps = CMakeDeps(self)
25+
deps = CMakeConfigDeps(self)
2626
deps.generate()
2727
tc = CMakeToolchain(self)
2828
tc.generate()

tests/integration/examples/test_conan_cmake.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def test_extension(example_runner: CliRunner) -> None:
185185
# This test uses the cppython.build backend which wraps scikit-build-core
186186
# The build backend automatically runs CPPython's provider workflow
187187

188+
# Install C++ dependencies first (creates generators/ with conan_toolchain.cmake)
189+
project = TestConanCMake._create_project()
190+
project.install()
191+
188192
# Create dist directory for the wheel
189193
dist_path = Path('dist')
190194
dist_path.mkdir(exist_ok=True)

0 commit comments

Comments
 (0)