Skip to content

Commit ce1a07e

Browse files
authored
fix: use classic extension handling unless otherwise requested (#2462)
* fix: use classic extension handling unless otherwise requested * fix: variable must be cached to be used externally
1 parent 0dbda6e commit ce1a07e

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

tools/pybind11NewTools.cmake

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,22 @@ if(PYBIND11_MASTER_PROJECT)
7171
endif()
7272

7373
# Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
74-
execute_process(COMMAND ${_Python}::Python -c "import sys; print(hasattr(sys, 'gettotalrefcount'))"
75-
OUTPUT_VARIABLE PYTHON_IS_DEBUG)
74+
execute_process(
75+
COMMAND "${${_Python}_EXECUTABLE}" "-c" "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
76+
RESULT_VARIABLE PYTHON_IS_DEBUG)
77+
78+
# Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
79+
# required for PyPy3 (as of 7.3.1)
80+
execute_process(
81+
COMMAND "${${_Python}_EXECUTABLE}" "-c"
82+
"from distutils import sysconfig; print(sysconfig.get_config_var('SO'))"
83+
OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
84+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
85+
86+
# This needs to be available for the pybind11_extension function
87+
set(PYTHON_MODULE_EXTENSION
88+
"${_PYTHON_MODULE_EXTENSION}"
89+
CACHE INTERNAL "")
7690

7791
# Python debug libraries expose slightly different objects before 3.8
7892
# https://docs.python.org/3.6/c-api/intro.html#debugging-builds
@@ -121,8 +135,11 @@ else()
121135
PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_link_helper)
122136
endif()
123137

138+
# WITHOUT_SOABI and WITH_SOABI will disable the custom extension handling used by pybind11.
139+
# WITH_SOABI is passed on to python_add_library.
124140
function(pybind11_add_module target_name)
125-
cmake_parse_arguments(PARSE_ARGV 1 ARG "STATIC;SHARED;MODULE;THIN_LTO;NO_EXTRAS" "" "")
141+
cmake_parse_arguments(PARSE_ARGV 1 ARG "STATIC;SHARED;MODULE;THIN_LTO;NO_EXTRAS;WITHOUT_SOABI"
142+
"" "")
126143

127144
if(ARG_ADD_LIBRARY_STATIC)
128145
set(type STATIC)
@@ -133,11 +150,11 @@ function(pybind11_add_module target_name)
133150
endif()
134151

135152
if("${_Python}" STREQUAL "Python")
136-
python_add_library(${target_name} ${type} WITH_SOABI ${ARG_UNPARSED_ARGUMENTS})
153+
python_add_library(${target_name} ${type} ${ARG_UNPARSED_ARGUMENTS})
137154
elseif("${_Python}" STREQUAL "Python3")
138-
python3_add_library(${target_name} ${type} WITH_SOABI ${ARG_UNPARSED_ARGUMENTS})
155+
python3_add_library(${target_name} ${type} ${ARG_UNPARSED_ARGUMENTS})
139156
elseif("${_Python}" STREQUAL "Python2")
140-
python2_add_library(${target_name} ${type} WITH_SOABI ${ARG_UNPARSED_ARGUMENTS})
157+
python2_add_library(${target_name} ${type} ${ARG_UNPARSED_ARGUMENTS})
141158
else()
142159
message(FATAL_ERROR "Cannot detect FindPython version: ${_Python}")
143160
endif()
@@ -161,6 +178,12 @@ function(pybind11_add_module target_name)
161178
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden"
162179
CUDA_VISIBILITY_PRESET "hidden")
163180

181+
# If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions
182+
if("${type}" STREQUAL "MODULE" AND (NOT ARG_WITHOUT_SOABI OR NOT "WITH_SOABI" IN_LIST
183+
ARG_UNPARSED_ARGUMENTS))
184+
pybind11_extension(${target_name})
185+
endif()
186+
164187
if(ARG_NO_EXTRAS)
165188
return()
166189
endif()
@@ -184,20 +207,7 @@ function(pybind11_add_module target_name)
184207
endfunction()
185208

186209
function(pybind11_extension name)
187-
set_property(TARGET ${name} PROPERTY PREFIX "")
188-
189-
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
190-
set_property(TARGET ${name} PROPERTY SUFFIX ".pyd")
191-
endif()
210+
# The extension is precomputed
211+
set_target_properties(${name} PROPERTIES PREFIX "" SUFFIX "${PYTHON_MODULE_EXTENSION}")
192212

193-
if(${_Python}_SOABI)
194-
get_property(
195-
suffix
196-
TARGET ${name}
197-
PROPERTY SUFFIX)
198-
if(NOT suffix)
199-
set(suffix "${CMAKE_SHARED_MODULE_SUFFIX}")
200-
endif()
201-
set_property(TARGET ${name} PROPERTY SUFFIX ".${${_Python}_SOABI}${suffix}")
202-
endif()
203213
endfunction()

0 commit comments

Comments
 (0)