Skip to content

Commit 63abec1

Browse files
authored
Default CMake to .venv Path (#132)
1 parent 84e2d02 commit 63abec1

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

cppython/plugins/conan/plugin.py

Lines changed: 22 additions & 8 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,7 @@ 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+
self._cmake_binary: str | None = None
5050

5151
@staticmethod
5252
def features(directory: Path) -> SupportedFeatures:
@@ -174,7 +174,7 @@ def _run_conan_install(self, conanfile_path: Path, update: bool, build_type: str
174174
command_args.extend(['-s', f'build_type={build_type}'])
175175

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

240240
raise NotSupportedError(f'Unsupported sync types: {consumer.sync_types()}')
241241

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

258272
return self._create_cmake_sync_data()
259273

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

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

0 commit comments

Comments
 (0)