Skip to content

Commit 9ead4fc

Browse files
committed
Default CMake Binary to VEnv
1 parent e8d7326 commit 9ead4fc

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

cppython/plugins/conan/plugin.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import os
9+
import shutil
910
from logging import Logger, getLogger
1011
from pathlib import Path
1112
from typing import Any
@@ -45,8 +46,8 @@ def __init__(
4546

4647
self._ensure_default_profiles()
4748

48-
# Initialize cmake_binary with system default. It may be overridden during sync.
49-
self._cmake_binary = 'cmake'
49+
# Initialize cmake_binary by resolving venv cmake. It may be overridden during sync.
50+
self._cmake_binary = self._resolve_cmake_binary(None)
5051

5152
@staticmethod
5253
def features(directory: Path) -> SupportedFeatures:
@@ -174,7 +175,7 @@ def _run_conan_install(self, conanfile_path: Path, update: bool, build_type: str
174175
command_args.extend(['-s', f'build_type={build_type}'])
175176

176177
# Add cmake binary configuration if specified
177-
if self._cmake_binary and self._cmake_binary != 'cmake':
178+
if self._cmake_binary:
178179
# Quote the path if it contains spaces
179180
cmake_path = f'"{self._cmake_binary}"' if ' ' in self._cmake_binary else self._cmake_binary
180181
command_args.extend(['-c', f'tools.cmake:cmake_program={cmake_path}'])
@@ -239,6 +240,23 @@ def sync_data(self, consumer: SyncConsumer) -> SyncData:
239240

240241
raise NotSupportedError(f'Unsupported sync types: {consumer.sync_types()}')
241242

243+
@staticmethod
244+
def _resolve_cmake_binary(cmake_path: Path | str | None) -> str | None:
245+
"""Resolve the cmake binary path.
246+
247+
If an explicit path is provided, use it. Otherwise, try to find cmake
248+
in the current Python environment (venv) to ensure we use the same
249+
cmake version for all operations including dependency builds.
250+
251+
Args:
252+
cmake_path: Explicit cmake path, or None to auto-detect
253+
254+
Returns:
255+
Resolved cmake path as string, or None if not found
256+
"""
257+
resolved = cmake_path or shutil.which('cmake')
258+
return str(Path(resolved).resolve()) if resolved else None
259+
242260
def _sync_with_cmake(self, consumer: SyncConsumer) -> CMakeSyncData:
243261
"""Synchronize with CMake generator and create sync data.
244262
@@ -251,9 +269,8 @@ def _sync_with_cmake(self, consumer: SyncConsumer) -> CMakeSyncData:
251269
# Extract cmake_binary from CMakeGenerator if available
252270
if isinstance(consumer, CMakeGenerator) and not os.environ.get('CMAKE_BINARY'):
253271
# Only override if not already set from environment variable
254-
# Convert Path to string, or use 'cmake' if None
255272
cmake_path = consumer.data.cmake_binary
256-
self._cmake_binary = str(cmake_path) if cmake_path else 'cmake'
273+
self._cmake_binary = self._resolve_cmake_binary(cmake_path)
257274

258275
return self._create_cmake_sync_data()
259276

@@ -301,7 +318,7 @@ def publish(self) -> None:
301318
command_args.extend(['-c', 'tools.build:skip_test=True'])
302319

303320
# Add cmake binary configuration if specified
304-
if self._cmake_binary and self._cmake_binary != 'cmake':
321+
if self._cmake_binary:
305322
# Quote the path if it contains spaces
306323
cmake_path = f'"{self._cmake_binary}"' if ' ' in self._cmake_binary else self._cmake_binary
307324
command_args.extend(['-c', f'tools.cmake:cmake_program={cmake_path}'])

0 commit comments

Comments
 (0)