Skip to content

Commit

Permalink
Pass CMAKE_MAKE_PROGRAM if Ninja from Python venv is used
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed May 23, 2024
1 parent 9cf46e3 commit 86dc912
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/py_build_cmake/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import contextlib
import logging
import os
import platform
import shutil
import tempfile
from pathlib import Path
Expand Down Expand Up @@ -434,6 +436,17 @@ def python_tag_to_cmake(x: str | None):
if btype: # -D CMAKE_BUILD_TYPE={type}
options["CMAKE_BUILD_TYPE:STRING"] = btype

# Check if we need to pass the Ninja path to CMake
generator: str | None = cmake_cfg.get("generator")
make_program: Path | None = None
if generator is not None and "ninja" in generator.lower():
with contextlib.suppress(ImportError):
import ninja # type: ignore[import-not-found]

make_program = Path(ninja.BIN_DIR) / "ninja"
if platform.system() == "Windows":
make_program = make_program.with_suffix(".exe")

# CMake options
return CMaker(
cmake_settings=CMakeSettings(
Expand All @@ -452,7 +465,8 @@ def python_tag_to_cmake(x: str | None):
options=options,
args=cmake_cfg.get("args", []),
preset=cmake_cfg.get("preset"),
generator=cmake_cfg.get("generator"),
generator=generator,
make_program=make_program,
cross_compiling=cross_compiling,
**cross_opts,
),
Expand Down
13 changes: 13 additions & 0 deletions src/py_build_cmake/commands/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CMakeConfigureSettings:
args: list[str]
preset: str | None
generator: str | None
make_program: Path | None
cross_compiling: bool
toolchain_file: Path | None
python_prefix: Path | None
Expand Down Expand Up @@ -200,6 +201,17 @@ def get_configure_options_install(self) -> list[Option]:
]
return []

def get_configure_options_make(self) -> list[Option]:
"""Sets CMAKE_MAKE_PROGRAM."""
if self.conf_settings.make_program:
opt = Option(
"CMAKE_MAKE_PROGRAM",
self.conf_settings.make_program.as_posix(),
"FILEPATH",
)
return [opt]
return []

def get_configure_options_toolchain(self) -> list[str]:
"""Sets CMAKE_TOOLCHAIN_FILE."""
return (
Expand All @@ -223,6 +235,7 @@ def get_preload_options(self) -> list[Option]:
"""Get the list of options set in the CMake pre-load script (-C)."""
return (
self.get_configure_options_package()
+ self.get_configure_options_make()
+ self.get_configure_options_python()
+ self.get_configure_options_install()
)
Expand Down

0 comments on commit 86dc912

Please sign in to comment.