11"""Construction of Conan data"""
22
3+ import shutil
34from pathlib import Path
45
56from pydantic import DirectoryPath
67
7- from cppython .plugins .conan .schema import ConanDependency
8+ from cppython .plugins .conan .schema import ConanDependency , ConanfileGenerationData
89
910
1011class Builder :
@@ -19,8 +20,16 @@ def _create_base_conanfile(
1920 base_file : Path ,
2021 dependencies : list [ConanDependency ],
2122 dependency_groups : dict [str , list [ConanDependency ]],
23+ cmake_binary : Path | None = None ,
2224 ) -> None :
23- """Creates a conanfile_base.py with CPPython managed dependencies."""
25+ """Creates a conanfile_base.py with CPPython managed dependencies.
26+
27+ Args:
28+ base_file: Path to write the base conanfile
29+ dependencies: List of main dependencies
30+ dependency_groups: Dictionary of dependency groups (e.g., 'test')
31+ cmake_binary: Optional path to CMake binary to use
32+ """
2433 test_dependencies = dependency_groups .get ('test' , [])
2534
2635 # Generate requirements method content
@@ -37,6 +46,16 @@ def _create_base_conanfile(
3746 '\n ' .join (test_requires_lines ) if test_requires_lines else ' pass # No test requirements'
3847 )
3948
49+ # Generate configure method content for cmake_program if specified
50+ if cmake_binary :
51+ # Use forward slashes for cross-platform compatibility in Conan
52+ cmake_path_str = str (cmake_binary .resolve ()).replace ('\\ ' , '/' )
53+ configure_content = f''' def configure(self):
54+ """CPPython managed configuration."""
55+ self.conf.define("tools.cmake:cmake_program", "{ cmake_path_str } ")'''
56+ else :
57+ configure_content = ''
58+
4059 content = f'''"""CPPython managed base ConanFile.
4160
4261This file is auto-generated by CPPython. Do not edit manually.
@@ -48,6 +67,7 @@ def _create_base_conanfile(
4867
4968class CPPythonBase(ConanFile):
5069 """Base ConanFile with CPPython managed dependencies."""
70+ { configure_content }
5171
5272 def requirements(self):
5373 """CPPython managed requirements."""
@@ -135,23 +155,36 @@ def export_sources(self):
135155 def generate_conanfile (
136156 self ,
137157 directory : DirectoryPath ,
138- dependencies : list [ConanDependency ],
139- dependency_groups : dict [str , list [ConanDependency ]],
140- name : str ,
141- version : str ,
158+ data : ConanfileGenerationData ,
142159 ) -> None :
143160 """Generate conanfile.py and conanfile_base.py for the project.
144161
145162 Always generates the base conanfile with managed dependencies.
146163 Only creates conanfile.py if it doesn't exist (never modifies existing files).
164+
165+ Args:
166+ directory: The project directory
167+ data: Generation data containing dependencies, project info, and cmake binary path.
168+ If cmake_binary is not provided, attempts to find cmake in the current
169+ Python environment.
147170 """
148171 directory .mkdir (parents = True , exist_ok = True )
149172
173+ # Resolve cmake binary path
174+ resolved_cmake : Path | None = None
175+ if data .cmake_binary and data .cmake_binary != 'cmake' :
176+ resolved_cmake = Path (data .cmake_binary ).resolve ()
177+ else :
178+ # Try to find cmake in the current Python environment (venv)
179+ cmake_path = shutil .which ('cmake' )
180+ if cmake_path :
181+ resolved_cmake = Path (cmake_path ).resolve ()
182+
150183 # Always regenerate the base conanfile with managed dependencies
151184 base_file = directory / 'conanfile_base.py'
152- self ._create_base_conanfile (base_file , dependencies , dependency_groups )
185+ self ._create_base_conanfile (base_file , data . dependencies , data . dependency_groups , resolved_cmake )
153186
154187 # Only create conanfile.py if it doesn't exist
155188 conan_file = directory / self ._filename
156189 if not conan_file .exists ():
157- self ._create_conanfile (conan_file , name , version )
190+ self ._create_conanfile (conan_file , data . name , data . version )
0 commit comments