Skip to content

Commit

Permalink
feat: if for free-threading
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Jul 19, 2024
1 parent 9bc0d97 commit ea6c7f0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ features over classic Scikit-build:
soon)
- Experimental editable mode support, with optional experimental auto rebuilds
on import and optional in-place mode
- Supports WebAssembly (Emscripten/Pyodide).
- Supports free-threaded Python 3.13.
- Supports WebAssembly (Emscripten/[Pyodide](https://pyodide.org)).
- Supports [free-threaded Python 3.13](https://py-free-threading.github.io).

The following limitations are present compared to classic scikit-build:

Expand Down Expand Up @@ -117,7 +117,7 @@ the minimum to get started.
An example `CMakeLists.txt`:

```cmake
cmake_minimum_required(VERSION 3.15...3.27)
cmake_minimum_required(VERSION 3.15...3.30)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C)
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
Expand Down
6 changes: 5 additions & 1 deletion docs/overrides.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Example:

```toml
[[tool.scikit-build.overrides]]
if.python_version = ">=3.13"
if.python-version = ">=3.13"
wheel.cmake = false
```

Expand Down Expand Up @@ -84,6 +84,10 @@ A few sample values:
| Windows | ARM | `ARM64` |


### `abi-flags` (string)

A sorted list of the ABI flags. `t` is the free-threaded build.

### `platform-node` (string)

The value of `platform.node()`. This is generally your computer's name. Takes a
Expand Down
8 changes: 8 additions & 0 deletions src/scikit_build_core/builder/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from collections.abc import Mapping

__all__ = [
"get_abi_flags",
"get_cmake_platform",
"get_numpy_include_dir",
"get_python_include_dir",
Expand Down Expand Up @@ -184,6 +185,13 @@ def get_numpy_include_dir() -> Path | None:
return Path(np.get_include())


def get_abi_flags() -> str:
"""
Return the ABI flags for the current Python interpreter.
"""
return "".join(sorted(sysconfig.get_config_var("ABIFLAGS"))) or ""


def info_print(*, color: str = "") -> None:
"""
Print information about the Python environment.
Expand Down
4 changes: 4 additions & 0 deletions src/scikit_build_core/resources/scikit-build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@
"type": "boolean",
"description": "Whether the build is from an sdist."
},
"abi-flags": {
"type": "string",
"description": "A sorted string of the abi flags. Takes a regex."
},
"env": {
"type": "object",
"patternProperties": {
Expand Down
7 changes: 7 additions & 0 deletions src/scikit_build_core/settings/skbuild_read_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .. import __version__
from .._compat import tomllib
from .._logging import logger, rich_error, rich_print, rich_warning
from ..builder.sysconfig import get_abi_flags
from ..errors import CMakeConfigError
from .auto_cmake_version import find_min_cmake_version
from .auto_requires import get_min_requires
Expand Down Expand Up @@ -81,6 +82,7 @@ def override_match(
env: dict[str, str] | None = None,
state: str | None = None,
from_sdist: bool | None = None,
abi_flags: str | None = None,
) -> bool:
# This makes a matches list. A match is a string; if it's an empty string,
# then it did not match. If it's not empty, it did match, and the string
Expand Down Expand Up @@ -137,6 +139,11 @@ def override_match(
else:
matches.append("" if from_sdist else "not from sdist, no PKG-INFO")

if abi_flags is not None:
current_abi_flags = get_abi_flags()
match_msg = regex_match(current_abi_flags, abi_flags)
matches.append(match_msg)

if env:
for key, value in env.items():
if isinstance(value, bool):
Expand Down
4 changes: 4 additions & 0 deletions src/scikit_build_core/settings/skbuild_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def generate_skbuild_schema(tool_name: str = "scikit-build") -> dict[str, Any]:
"type": "boolean",
"description": "Whether the build is from an sdist.",
},
"abi-flags": {
"type": "string",
"description": "A sorted string of the abi flags. Takes a regex.",
},
"env": {
"type": "object",
"patternProperties": {
Expand Down
22 changes: 22 additions & 0 deletions tests/test_settings_overrides.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sysconfig
import typing
from textwrap import dedent

Expand Down Expand Up @@ -500,6 +501,7 @@ def test_skbuild_overrides_from_sdist(
tmp_path / ("from_sdist" if from_sdist else "not_from_sdist") / "pyproject.toml"
)
pyproject_toml.parent.mkdir(exist_ok=True)

pyproject_toml.write_text(
dedent(
"""\
Expand Down Expand Up @@ -530,3 +532,23 @@ def test_skbuild_overrides_from_sdist(

assert settings.wheel.cmake != from_sdist
assert settings.sdist.cmake == from_sdist


def test_free_threaded_override(tmp_path: Path):
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[tool.scikit-build]
wheel.cmake = false
[[tool.scikit-build.overrides]]
if.abi-flags = "t"
wheel.cmake = true
"""
)
)

settings_reader = SettingsReader.from_file(pyproject_toml, state="wheel")
settings = settings_reader.settings
assert settings.wheel.cmake == bool(sysconfig.get_config_var("Py_GIL_DISABLED"))

0 comments on commit ea6c7f0

Please sign in to comment.