Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/cmakelists.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ related to making Python extension modules.

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

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

```cmake
if(NOT "${SKBUILD_SABI_COMPONENT}" STREQUAL "")
python_add_library(some_ext MODULE WITH_SOABI USE_SABI 3.11 ...)
if(NOT "${SKBUILD_SABI_VERSION}" STREQUAL "")
python_add_library(some_ext MODULE WITH_SOABI USE_SABI ${SKBUILD_SABI_VERSION} ...)
else()
python_add_library(some_ext MODULE WITH_SOABI ...)
endif()
```

If you have a lot of libraries, you can conditionally save these two items into
a variable with `set(USE_SABI ${SKBUILD_SABI_VERSION})` and use it in all your
`python_add_library` calls.

This will define `Py_LIMITED_API` for you. If you want to support building
directly from CMake, you need to protect this for Python version,
`Python_INTERPRETER_ID STREQUAL Python`, and free-threading Python 3.13+ doesn't
Expand Down
8 changes: 8 additions & 0 deletions src/scikit_build_core/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ def configure(
"Development.SABIModule" if limited_api else ""
)

# Allow users to detect the version requested in settings
py_api = self.settings.wheel.py_api
cache_config["SKBUILD_SABI_VERSION"] = (
"{py_api[2]}.{py_api[3:]}"
if limited_api and py_api.startswith("cp")
else ""
)

if cache_entries:
cache_config.update(cache_entries)

Expand Down
12 changes: 10 additions & 2 deletions tests/packages/abi3_pyproject_ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ find_package(
COMPONENTS Interpreter Development.Module ${SKBUILD_SABI_COMPONENT}
REQUIRED)

if(NOT "${SKBUILD_SABI_COMPONENT}" STREQUAL "")
python_add_library(abi3_example MODULE abi3_example.c WITH_SOABI USE_SABI 3.7)
if(NOT "${SKBUILD_SABI_VERSION}" STREQUAL "")
python_add_library(abi3_example MODULE abi3_example.c WITH_SOABI USE_SABI
${SKBUILD_SABI_VERSION})

if(NOT SKBUILD_SABI_VERSION STREQUAL "3.7")
message(
FATAL_ERROR
"TEST FAILED: SKBUILD_SABI_VERSION (${SKBUILD_SABI_VERSION}) is not 3.7"
)
endif()
else()
python_add_library(abi3_example MODULE abi3_example.c WITH_SOABI)
endif()
Expand Down
Loading