Skip to content

Commit

Permalink
fix: ninja wasn't being used if present (#310)
Browse files Browse the repository at this point in the history
Also adds a way to test downstream packages manually.

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii authored May 11, 2023
1 parent dbb1a0f commit 146fd75
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/examples/getting_started/fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15...3.26)
cmake_minimum_required(VERSION 3.17.2...3.26)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C Fortran)

find_package(
Expand Down
4 changes: 4 additions & 0 deletions docs/examples/getting_started/fortran/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ build-backend = "scikit_build_core.build"
name = "example"
version = "0.0.1"
dependencies = ["numpy"]

[tool.scikit-build]
ninja.minimum-version = "1.10"
cmake.minimum-version = "3.17.2"
72 changes: 72 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,75 @@ def test_doc_examples(session: nox.Session, example: str) -> None:
session.chdir(f"docs/examples/getting_started/{example}")
session.install(".", "--config-settings=cmake.verbose=true")
session.run("python", "../test.py")


@nox.session(reuse_venv=True)
def downstream(session: nox.Session) -> None:
"""
Build a downstream project.
"""

# If running in manylinux:
# docker run --rm -v $PWD:/sk -w /sk -t quay.io/pypa/manylinux2014_x86_64:latest \
# pipx run --system-site-packages nox -s downstream -- https://github.com/...
# (requires tomli, so allowing access to system-site-packages)

if sys.version_info < (3, 11):
import tomli as tomllib
else:
import tomllib

parser = argparse.ArgumentParser()
parser.add_argument("project", help="A project to build")
parser.add_argument("--subdir", help="A subdirectory to build")
args, remaining = parser.parse_known_args(session.posargs)

tmp_dir = Path(session.create_tmp())
proj_dir = tmp_dir / "_".join(args.project.split("/"))

session.install("build", "hatch-vcs", "hatchling")
session.install(".[pyproject]", "--no-build-isolation")

if proj_dir.is_dir():
session.chdir(proj_dir)
session.run("git", "pull", external=True)
else:
session.run(
"git",
"clone",
args.project,
*remaining,
proj_dir,
"--recurse-submodules",
external=True,
)
session.chdir(proj_dir)

# Read and strip requirements
pyproject_toml = Path("pyproject.toml")
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)
requires = [
x
for x in pyproject["build-system"]["requires"]
if "scikit-build-core" not in x.replace("_", "-")
]
if not shutil.which("ninja"):
requires.append("ninja")
if not shutil.which("cmake"):
requires.append("cmake")
if requires:
session.install(*requires)

if args.subdir:
session.chdir(args.subdir)

session.run(
"python",
"-m",
"build",
"--no-isolation",
"--skip-dependency-check",
"--wheel",
".",
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ exclude = []

[tool.ruff.per-file-ignores]
"tests/**" = ["T20"]
"noxfile.py" = ["T20"]
"noxfile.py" = ["T20", "TID251"]
"src/scikit_build_core/resources/*.py" = ["PTH", "ARG002"]
"src/scikit_build_core/_compat/**.py" = ["TID251"]
"tests/conftest.py" = ["TID251"]
Expand Down
4 changes: 3 additions & 1 deletion src/scikit_build_core/build/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ def _build_wheel_impl(

generator = builder.config.env.get(
"CMAKE_GENERATOR",
"MSVC" if sysconfig.get_platform().startswith("win") else "Unknown",
"MSVC"
if sysconfig.get_platform().startswith("win")
else "Default generator",
)
rich_print(
f"[green]***[/green] [bold]Building project with [blue]{generator}[/blue]..."
Expand Down
6 changes: 5 additions & 1 deletion src/scikit_build_core/builder/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def set_environment_for_gen(
"win"
):
# Non-MSVC Windows platforms require Ninja
env.setdefault("CMAKE_GENERATOR", "Ninja")
default = "Ninja"

# Try Ninja if it is available, even if make is CMake default
if default == "Unix Makefiles":
default = "Ninja"

if env.get("CMAKE_GENERATOR", default or "Ninja") == "Ninja":
min_ninja = Version(ninja_settings.minimum_version)
Expand Down
2 changes: 1 addition & 1 deletion tests/packages/fortran_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# setup project ###
cmake_minimum_required(VERSION 3.15...3.24)
cmake_minimum_required(VERSION 3.17.2...3.24)

project(
fibby
Expand Down
16 changes: 16 additions & 0 deletions tests/test_fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
from pathlib import Path

import pytest
from packaging.version import Version

from scikit_build_core.build import build_wheel
from scikit_build_core.program_search import (
best_program,
get_cmake_programs,
get_ninja_programs,
)

np = pytest.importorskip("numpy")

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


cmake_info = best_program(get_cmake_programs(), minimum_version=Version("3.17.2"))
ninja_info = best_program(get_ninja_programs(), minimum_version=Version("1.10"))


@pytest.mark.compile()
@pytest.mark.configure()
@pytest.mark.fortran()
Expand All @@ -22,6 +32,12 @@
sysconfig.get_platform().startswith("win"),
reason="No reasonable Fortran compiler for MSVC",
)
@pytest.mark.skipif(
cmake_info is None, reason="CMake needs to be 3.17.2+ to support Fortran with Ninja"
)
@pytest.mark.skipif(
ninja_info is None, reason="Ninja needs to be 1.10+ to support Fortran with CMake"
)
def test_pep517_wheel(tmp_path, monkeypatch, virtualenv):
dist = tmp_path / "dist"
dist.mkdir()
Expand Down

0 comments on commit 146fd75

Please sign in to comment.