Skip to content

Commit 2bf6d74

Browse files
authored
CMake build system: use FindPython's suffix computation if possible (#226)
1 parent badea4e commit 2bf6d74

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

cmake/nanobind-config.cmake

+35-19
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,57 @@ if (NOT TARGET Python::Module)
55
endif()
66

77
# Determine the right suffix for ordinary and stable ABI extensions.
8-
# Python_SOSABI is guaranteed to be available in CMake 3.26+, and it may
9-
# also be available as part of backported FindPython in scikit-build-core
10-
if (DEFINED Python_SOSABI)
11-
if (WIN32)
12-
set(NB_SUFFIX_EXT ".pyd")
13-
else()
14-
set(NB_SUFFIX_EXT "${CMAKE_SHARED_MODULE_SUFFIX}")
15-
endif()
168

17-
set(NB_SUFFIX ".${Python_SOABI}${NB_SUFFIX_EXT}")
9+
# We always need to know the extension
10+
if(WIN32)
11+
set(NB_SUFFIX_EXT ".pyd")
12+
else()
13+
set(NB_SUFFIX_EXT "${CMAKE_SHARED_MODULE_SUFFIX}")
14+
endif()
15+
16+
# This was added in CMake 3.17+, also available earlier in scikit-build-core.
17+
# PyPy sets an invalid SOABI (platform missing), causing older FindPythons to
18+
# report an incorrect value. Only use it if it looks correct (X-X-X form).
19+
if(DEFINED Python_SOABI AND "${Python_SOABI}" MATCHES ".+-.+-.+")
20+
set(NB_SUFFIX ".${Python_SOABI}${NB_SUFFIX_EXT}")
21+
endif()
1822

19-
if (Python_SOSABI STREQUAL "")
23+
# Python_SOSABI is guaranteed to be available in CMake 3.26+, and it may
24+
# also be available as part of backported FindPython in scikit-build-core.
25+
if(DEFINED Python_SOSABI)
26+
if(Python_SOSABI STREQUAL "")
2027
set(NB_SUFFIX_S "${NB_SUFFIX_EXT}")
2128
else()
2229
set(NB_SUFFIX_S ".${Python_SOSABI}${NB_SUFFIX_EXT}")
2330
endif()
24-
else()
25-
# Query Python directly to get the right suffix
31+
endif()
32+
33+
# If either suffix is missing, call Python to compute it
34+
if(NOT DEFINED NB_SUFFIX OR NOT DEFINED NB_SUFFIX_S)
35+
# Query Python directly to get the right suffix.
2636
execute_process(
2737
COMMAND "${Python_EXECUTABLE}" "-c"
2838
"import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"
2939
RESULT_VARIABLE NB_SUFFIX_RET
30-
OUTPUT_VARIABLE NB_SUFFIX
40+
OUTPUT_VARIABLE EXT_SUFFIX
3141
OUTPUT_STRIP_TRAILING_WHITESPACE)
3242

33-
if (NB_SUFFIX_RET AND NOT NB_SUFFIX_RET EQUAL 0)
43+
if(NB_SUFFIX_RET AND NOT NB_SUFFIX_RET EQUAL 0)
3444
message(FATAL_ERROR "nanobind: Python sysconfig query to "
3545
"find 'EXT_SUFFIX' property failed!")
3646
endif()
3747

38-
get_filename_component(NB_SUFFIX_EXT "${NB_SUFFIX}" LAST_EXT)
39-
if (WIN32)
40-
set(NB_SUFFIX_S "${NB_SUFFIX_EXT}")
41-
else()
42-
set(NB_SUFFIX_S ".abi3${NB_SUFFIX_EXT}")
48+
if(NOT DEFINED NB_SUFFIX)
49+
set(NB_SUFFIX "${EXT_SUFFIX}")
50+
endif()
51+
52+
if(NOT DEFINED NB_SUFFIX_S)
53+
get_filename_component(NB_SUFFIX_EXT "${EXT_SUFFIX}" LAST_EXT)
54+
if(WIN32)
55+
set(NB_SUFFIX_S "${NB_SUFFIX_EXT}")
56+
else()
57+
set(NB_SUFFIX_S ".abi3${NB_SUFFIX_EXT}")
58+
endif()
4359
endif()
4460
endif()
4561

0 commit comments

Comments
 (0)