Skip to content

Commit 146fd75

Browse files
authored
fix: ninja wasn't being used if present (#310)
Also adds a way to test downstream packages manually. --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent dbb1a0f commit 146fd75

File tree

8 files changed

+103
-5
lines changed

8 files changed

+103
-5
lines changed

docs/examples/getting_started/fortran/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.15...3.26)
1+
cmake_minimum_required(VERSION 3.17.2...3.26)
22
project(${SKBUILD_PROJECT_NAME} LANGUAGES C Fortran)
33

44
find_package(

docs/examples/getting_started/fortran/pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ build-backend = "scikit_build_core.build"
66
name = "example"
77
version = "0.0.1"
88
dependencies = ["numpy"]
9+
10+
[tool.scikit-build]
11+
ninja.minimum-version = "1.10"
12+
cmake.minimum-version = "3.17.2"

noxfile.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,75 @@ def test_doc_examples(session: nox.Session, example: str) -> None:
114114
session.chdir(f"docs/examples/getting_started/{example}")
115115
session.install(".", "--config-settings=cmake.verbose=true")
116116
session.run("python", "../test.py")
117+
118+
119+
@nox.session(reuse_venv=True)
120+
def downstream(session: nox.Session) -> None:
121+
"""
122+
Build a downstream project.
123+
"""
124+
125+
# If running in manylinux:
126+
# docker run --rm -v $PWD:/sk -w /sk -t quay.io/pypa/manylinux2014_x86_64:latest \
127+
# pipx run --system-site-packages nox -s downstream -- https://github.com/...
128+
# (requires tomli, so allowing access to system-site-packages)
129+
130+
if sys.version_info < (3, 11):
131+
import tomli as tomllib
132+
else:
133+
import tomllib
134+
135+
parser = argparse.ArgumentParser()
136+
parser.add_argument("project", help="A project to build")
137+
parser.add_argument("--subdir", help="A subdirectory to build")
138+
args, remaining = parser.parse_known_args(session.posargs)
139+
140+
tmp_dir = Path(session.create_tmp())
141+
proj_dir = tmp_dir / "_".join(args.project.split("/"))
142+
143+
session.install("build", "hatch-vcs", "hatchling")
144+
session.install(".[pyproject]", "--no-build-isolation")
145+
146+
if proj_dir.is_dir():
147+
session.chdir(proj_dir)
148+
session.run("git", "pull", external=True)
149+
else:
150+
session.run(
151+
"git",
152+
"clone",
153+
args.project,
154+
*remaining,
155+
proj_dir,
156+
"--recurse-submodules",
157+
external=True,
158+
)
159+
session.chdir(proj_dir)
160+
161+
# Read and strip requirements
162+
pyproject_toml = Path("pyproject.toml")
163+
with pyproject_toml.open("rb") as f:
164+
pyproject = tomllib.load(f)
165+
requires = [
166+
x
167+
for x in pyproject["build-system"]["requires"]
168+
if "scikit-build-core" not in x.replace("_", "-")
169+
]
170+
if not shutil.which("ninja"):
171+
requires.append("ninja")
172+
if not shutil.which("cmake"):
173+
requires.append("cmake")
174+
if requires:
175+
session.install(*requires)
176+
177+
if args.subdir:
178+
session.chdir(args.subdir)
179+
180+
session.run(
181+
"python",
182+
"-m",
183+
"build",
184+
"--no-isolation",
185+
"--skip-dependency-check",
186+
"--wheel",
187+
".",
188+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ exclude = []
237237

238238
[tool.ruff.per-file-ignores]
239239
"tests/**" = ["T20"]
240-
"noxfile.py" = ["T20"]
240+
"noxfile.py" = ["T20", "TID251"]
241241
"src/scikit_build_core/resources/*.py" = ["PTH", "ARG002"]
242242
"src/scikit_build_core/_compat/**.py" = ["TID251"]
243243
"tests/conftest.py" = ["TID251"]

src/scikit_build_core/build/wheel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ def _build_wheel_impl(
182182

183183
generator = builder.config.env.get(
184184
"CMAKE_GENERATOR",
185-
"MSVC" if sysconfig.get_platform().startswith("win") else "Unknown",
185+
"MSVC"
186+
if sysconfig.get_platform().startswith("win")
187+
else "Default generator",
186188
)
187189
rich_print(
188190
f"[green]***[/green] [bold]Building project with [blue]{generator}[/blue]..."

src/scikit_build_core/builder/generator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ def set_environment_for_gen(
8080
"win"
8181
):
8282
# Non-MSVC Windows platforms require Ninja
83-
env.setdefault("CMAKE_GENERATOR", "Ninja")
83+
default = "Ninja"
84+
85+
# Try Ninja if it is available, even if make is CMake default
86+
if default == "Unix Makefiles":
87+
default = "Ninja"
8488

8589
if env.get("CMAKE_GENERATOR", default or "Ninja") == "Ninja":
8690
min_ninja = Version(ninja_settings.minimum_version)

tests/packages/fortran_example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# setup project ###
2-
cmake_minimum_required(VERSION 3.15...3.24)
2+
cmake_minimum_required(VERSION 3.17.2...3.24)
33

44
project(
55
fibby

tests/test_fortran.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@
55
from pathlib import Path
66

77
import pytest
8+
from packaging.version import Version
89

910
from scikit_build_core.build import build_wheel
11+
from scikit_build_core.program_search import (
12+
best_program,
13+
get_cmake_programs,
14+
get_ninja_programs,
15+
)
1016

1117
np = pytest.importorskip("numpy")
1218

1319
DIR = Path(__file__).parent.resolve()
1420
FORTRAN_EXAMPLE = DIR / "packages/fortran_example"
1521

1622

23+
cmake_info = best_program(get_cmake_programs(), minimum_version=Version("3.17.2"))
24+
ninja_info = best_program(get_ninja_programs(), minimum_version=Version("1.10"))
25+
26+
1727
@pytest.mark.compile()
1828
@pytest.mark.configure()
1929
@pytest.mark.fortran()
@@ -22,6 +32,12 @@
2232
sysconfig.get_platform().startswith("win"),
2333
reason="No reasonable Fortran compiler for MSVC",
2434
)
35+
@pytest.mark.skipif(
36+
cmake_info is None, reason="CMake needs to be 3.17.2+ to support Fortran with Ninja"
37+
)
38+
@pytest.mark.skipif(
39+
ninja_info is None, reason="Ninja needs to be 1.10+ to support Fortran with CMake"
40+
)
2541
def test_pep517_wheel(tmp_path, monkeypatch, virtualenv):
2642
dist = tmp_path / "dist"
2743
dist.mkdir()

0 commit comments

Comments
 (0)