|
8 | 8 | import logging |
9 | 9 | import tomllib |
10 | 10 | from pathlib import Path |
| 11 | +from types import ModuleType |
11 | 12 | from typing import Any |
12 | 13 |
|
| 14 | +import mesonpy |
13 | 15 | from scikit_build_core import build as skbuild |
14 | 16 |
|
15 | 17 | from cppython.build.prepare import BuildPreparationResult, prepare_build |
@@ -40,10 +42,21 @@ def _is_meson_project() -> bool: |
40 | 42 | return True |
41 | 43 |
|
42 | 44 | # Fallback: check for meson.build file |
43 | | - if (source_dir / 'meson.build').exists(): |
44 | | - return True |
| 45 | + return (source_dir / 'meson.build').exists() |
45 | 46 |
|
46 | | - return False |
| 47 | + |
| 48 | +def _get_backend(is_meson: bool) -> ModuleType: |
| 49 | + """Get the appropriate backend module. |
| 50 | +
|
| 51 | + Args: |
| 52 | + is_meson: Whether to use meson-python instead of scikit-build-core |
| 53 | +
|
| 54 | + Returns: |
| 55 | + The backend module (mesonpy or scikit_build_core.build) |
| 56 | + """ |
| 57 | + if is_meson: |
| 58 | + return mesonpy |
| 59 | + return skbuild |
47 | 60 |
|
48 | 61 |
|
49 | 62 | def _inject_cmake_toolchain(config_settings: dict[str, Any] | None, toolchain_file: Path | None) -> dict[str, Any]: |
@@ -171,185 +184,63 @@ def _is_meson_build(result: BuildPreparationResult) -> bool: |
171 | 184 | # PEP 517 Hooks - dispatching to the appropriate backend after preparation |
172 | 185 |
|
173 | 186 |
|
174 | | -def get_requires_for_build_wheel( |
175 | | - config_settings: dict[str, Any] | None = None, |
176 | | -) -> list[str]: |
177 | | - """Get additional requirements for building a wheel. |
178 | | -
|
179 | | - Args: |
180 | | - config_settings: Build configuration settings |
181 | | -
|
182 | | - Returns: |
183 | | - List of additional requirements |
184 | | - """ |
185 | | - if _is_meson_project(): |
186 | | - import mesonpy |
187 | | - |
188 | | - return mesonpy.get_requires_for_build_wheel(config_settings) |
189 | | - return skbuild.get_requires_for_build_wheel(config_settings) |
190 | | - |
191 | | - |
192 | | -def get_requires_for_build_sdist( |
193 | | - config_settings: dict[str, Any] | None = None, |
194 | | -) -> list[str]: |
195 | | - """Get additional requirements for building an sdist. |
196 | | -
|
197 | | - Args: |
198 | | - config_settings: Build configuration settings |
199 | | -
|
200 | | - Returns: |
201 | | - List of additional requirements |
202 | | - """ |
203 | | - if _is_meson_project(): |
204 | | - import mesonpy |
| 187 | +def get_requires_for_build_wheel(config_settings: dict[str, Any] | None = None) -> list[str]: |
| 188 | + """Get additional requirements for building a wheel.""" |
| 189 | + return _get_backend(_is_meson_project()).get_requires_for_build_wheel(config_settings) |
205 | 190 |
|
206 | | - return mesonpy.get_requires_for_build_sdist(config_settings) |
207 | | - return skbuild.get_requires_for_build_sdist(config_settings) |
208 | 191 |
|
| 192 | +def get_requires_for_build_sdist(config_settings: dict[str, Any] | None = None) -> list[str]: |
| 193 | + """Get additional requirements for building an sdist.""" |
| 194 | + return _get_backend(_is_meson_project()).get_requires_for_build_sdist(config_settings) |
209 | 195 |
|
210 | | -def get_requires_for_build_editable( |
211 | | - config_settings: dict[str, Any] | None = None, |
212 | | -) -> list[str]: |
213 | | - """Get additional requirements for building an editable install. |
214 | 196 |
|
215 | | - Args: |
216 | | - config_settings: Build configuration settings |
217 | | -
|
218 | | - Returns: |
219 | | - List of additional requirements |
220 | | - """ |
221 | | - if _is_meson_project(): |
222 | | - import mesonpy |
223 | | - |
224 | | - return mesonpy.get_requires_for_build_editable(config_settings) |
225 | | - return skbuild.get_requires_for_build_editable(config_settings) |
| 197 | +def get_requires_for_build_editable(config_settings: dict[str, Any] | None = None) -> list[str]: |
| 198 | + """Get additional requirements for building an editable install.""" |
| 199 | + return _get_backend(_is_meson_project()).get_requires_for_build_editable(config_settings) |
226 | 200 |
|
227 | 201 |
|
228 | 202 | def build_wheel( |
229 | 203 | wheel_directory: str, |
230 | 204 | config_settings: dict[str, Any] | None = None, |
231 | 205 | metadata_directory: str | None = None, |
232 | 206 | ) -> str: |
233 | | - """Build a wheel from the source distribution. |
234 | | -
|
235 | | - This runs CPPython's provider workflow first to ensure C++ dependencies |
236 | | - are installed, then delegates to the appropriate build backend |
237 | | - (scikit-build-core for CMake, meson-python for Meson). |
238 | | -
|
239 | | - Args: |
240 | | - wheel_directory: Directory to place the built wheel |
241 | | - config_settings: Build configuration settings |
242 | | - metadata_directory: Directory containing wheel metadata |
243 | | -
|
244 | | - Returns: |
245 | | - The basename of the built wheel |
246 | | - """ |
| 207 | + """Build a wheel, running CPPython preparation first.""" |
247 | 208 | logger.info('CPPython: Starting wheel build') |
248 | | - |
249 | | - # Prepare CPPython and get updated settings |
250 | 209 | result, settings = _prepare_and_get_result(config_settings) |
251 | | - |
252 | | - # Delegate to the appropriate backend |
253 | | - if _is_meson_build(result): |
254 | | - import mesonpy |
255 | | - |
256 | | - return mesonpy.build_wheel(wheel_directory, settings, metadata_directory) |
257 | | - |
258 | | - return skbuild.build_wheel(wheel_directory, settings, metadata_directory) |
| 210 | + return _get_backend(_is_meson_build(result)).build_wheel(wheel_directory, settings, metadata_directory) |
259 | 211 |
|
260 | 212 |
|
261 | 213 | def build_sdist( |
262 | 214 | sdist_directory: str, |
263 | 215 | config_settings: dict[str, Any] | None = None, |
264 | 216 | ) -> str: |
265 | | - """Build a source distribution. |
266 | | -
|
267 | | - For sdist, we don't run the full CPPython workflow since the C++ dependencies |
268 | | - should be resolved at wheel build time, not sdist creation time. |
269 | | -
|
270 | | - Args: |
271 | | - sdist_directory: Directory to place the built sdist |
272 | | - config_settings: Build configuration settings |
273 | | -
|
274 | | - Returns: |
275 | | - The basename of the built sdist |
276 | | - """ |
| 217 | + """Build a source distribution (no CPPython workflow needed).""" |
277 | 218 | logger.info('CPPython: Starting sdist build') |
278 | | - |
279 | | - # Delegate to the appropriate backend |
280 | | - if _is_meson_project(): |
281 | | - import mesonpy |
282 | | - |
283 | | - return mesonpy.build_sdist(sdist_directory, config_settings) |
284 | | - return skbuild.build_sdist(sdist_directory, config_settings) |
| 219 | + return _get_backend(_is_meson_project()).build_sdist(sdist_directory, config_settings) |
285 | 220 |
|
286 | 221 |
|
287 | 222 | def build_editable( |
288 | 223 | wheel_directory: str, |
289 | 224 | config_settings: dict[str, Any] | None = None, |
290 | 225 | metadata_directory: str | None = None, |
291 | 226 | ) -> str: |
292 | | - """Build an editable wheel. |
293 | | -
|
294 | | - This runs CPPython's provider workflow first, similar to build_wheel. |
295 | | -
|
296 | | - Args: |
297 | | - wheel_directory: Directory to place the built wheel |
298 | | - config_settings: Build configuration settings |
299 | | - metadata_directory: Directory containing wheel metadata |
300 | | -
|
301 | | - Returns: |
302 | | - The basename of the built wheel |
303 | | - """ |
| 227 | + """Build an editable wheel, running CPPython preparation first.""" |
304 | 228 | logger.info('CPPython: Starting editable build') |
305 | | - |
306 | | - # Prepare CPPython and get updated settings |
307 | 229 | result, settings = _prepare_and_get_result(config_settings) |
308 | | - |
309 | | - # Delegate to the appropriate backend |
310 | | - if _is_meson_build(result): |
311 | | - import mesonpy |
312 | | - |
313 | | - return mesonpy.build_editable(wheel_directory, settings, metadata_directory) |
314 | | - |
315 | | - return skbuild.build_editable(wheel_directory, settings, metadata_directory) |
| 230 | + return _get_backend(_is_meson_build(result)).build_editable(wheel_directory, settings, metadata_directory) |
316 | 231 |
|
317 | 232 |
|
318 | 233 | def prepare_metadata_for_build_wheel( |
319 | 234 | metadata_directory: str, |
320 | 235 | config_settings: dict[str, Any] | None = None, |
321 | 236 | ) -> str: |
322 | | - """Prepare metadata for wheel build. |
323 | | -
|
324 | | - Args: |
325 | | - metadata_directory: Directory to place the metadata |
326 | | - config_settings: Build configuration settings |
327 | | -
|
328 | | - Returns: |
329 | | - The basename of the metadata directory |
330 | | - """ |
331 | | - if _is_meson_project(): |
332 | | - import mesonpy |
333 | | - |
334 | | - return mesonpy.prepare_metadata_for_build_wheel(metadata_directory, config_settings) |
335 | | - return skbuild.prepare_metadata_for_build_wheel(metadata_directory, config_settings) |
| 237 | + """Prepare metadata for wheel build.""" |
| 238 | + return _get_backend(_is_meson_project()).prepare_metadata_for_build_wheel(metadata_directory, config_settings) |
336 | 239 |
|
337 | 240 |
|
338 | 241 | def prepare_metadata_for_build_editable( |
339 | 242 | metadata_directory: str, |
340 | 243 | config_settings: dict[str, Any] | None = None, |
341 | 244 | ) -> str: |
342 | | - """Prepare metadata for editable build. |
343 | | -
|
344 | | - Args: |
345 | | - metadata_directory: Directory to place the metadata |
346 | | - config_settings: Build configuration settings |
347 | | -
|
348 | | - Returns: |
349 | | - The basename of the metadata directory |
350 | | - """ |
351 | | - if _is_meson_project(): |
352 | | - import mesonpy |
353 | | - |
354 | | - return mesonpy.prepare_metadata_for_build_editable(metadata_directory, config_settings) |
355 | | - return skbuild.prepare_metadata_for_build_editable(metadata_directory, config_settings) |
| 245 | + """Prepare metadata for editable build.""" |
| 246 | + return _get_backend(_is_meson_project()).prepare_metadata_for_build_editable(metadata_directory, config_settings) |
0 commit comments