Skip to content

Commit 241c63b

Browse files
authored
feat: SKBUILD_SABI_VERSION (#962)
1 parent 3c67119 commit 241c63b

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

docs/cmakelists.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ related to making Python extension modules.
3636

3737
If you are making a Limited API / Stable ABI package, you'll need the
3838
`Development.SABIModule` component instead (CMake 3.26+). You can use the
39-
`SKBUILD_SABI_COMPONENT` variable to check to see if it was requested.
39+
`SKBUILD_SABI_COMPONENT` variable to check to see if it was requested. You can
40+
get the version requested with `${SKBUILD_SABI_VERSION}`.
4041

4142
<!-- prettier-ignore-start -->
4243
:::{warning}
@@ -142,13 +143,25 @@ When defining your module, if you only support the Stable ABI after some point,
142143
you should use (for example for 3.11):
143144

144145
```cmake
145-
if(NOT "${SKBUILD_SABI_COMPONENT}" STREQUAL "")
146-
python_add_library(some_ext MODULE WITH_SOABI USE_SABI 3.11 ...)
146+
if(NOT "${SKBUILD_SABI_VERSION}" STREQUAL "")
147+
python_add_library(some_ext MODULE WITH_SOABI USE_SABI ${SKBUILD_SABI_VERSION} ...)
147148
else()
148149
python_add_library(some_ext MODULE WITH_SOABI ...)
149150
endif()
150151
```
151152

153+
If you have a lot of libraries, you can conditionally save these two items into
154+
a variable with `set(USE_SABI USE_SABI ${SKBUILD_SABI_VERSION})` and use it in
155+
all your `python_add_library` calls:
156+
157+
```
158+
if(NOT "${SKBUILD_SABI_VERSION}" STREQUAL "")
159+
set(USE_SABI "USE_SABI ${SKBUILD_SABI_VERSION}")
160+
endif()
161+
162+
python_add_library(some_ext MODULE WITH_SOABI ${USE_SABI} ...)
163+
```
164+
152165
This will define `Py_LIMITED_API` for you. If you want to support building
153166
directly from CMake, you need to protect this for Python version,
154167
`Python_INTERPRETER_ID STREQUAL Python`, and free-threading Python 3.13+ doesn't

src/scikit_build_core/builder/builder.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ def configure(
233233
"Development.SABIModule" if limited_api else ""
234234
)
235235

236+
# Allow users to detect the version requested in settings
237+
py_api = self.settings.wheel.py_api
238+
cache_config["SKBUILD_SABI_VERSION"] = (
239+
f"{py_api[2]}.{py_api[3:]}"
240+
if limited_api and py_api.startswith("cp")
241+
else ""
242+
)
243+
236244
if cache_entries:
237245
cache_config.update(cache_entries)
238246

tests/packages/abi3_pyproject_ext/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ find_package(
1010
COMPONENTS Interpreter Development.Module ${SKBUILD_SABI_COMPONENT}
1111
REQUIRED)
1212

13-
if(NOT "${SKBUILD_SABI_COMPONENT}" STREQUAL "")
14-
python_add_library(abi3_example MODULE abi3_example.c WITH_SOABI USE_SABI 3.7)
13+
if(NOT "${SKBUILD_SABI_VERSION}" STREQUAL "")
14+
python_add_library(abi3_example MODULE abi3_example.c WITH_SOABI USE_SABI
15+
${SKBUILD_SABI_VERSION})
16+
17+
if(NOT SKBUILD_SABI_VERSION STREQUAL "3.7")
18+
message(
19+
FATAL_ERROR
20+
"TEST FAILED: SKBUILD_SABI_VERSION (${SKBUILD_SABI_VERSION}) is not 3.7"
21+
)
22+
endif()
1523
else()
1624
python_add_library(abi3_example MODULE abi3_example.c WITH_SOABI)
1725
endif()

0 commit comments

Comments
 (0)