Skip to content

Commit 5891867

Browse files
lpapp-foundrypre-commit-ci[bot]henryiii
authored
fix(cmake): support DEBUG_POSTFIX correctly (#4761)
* cmake: split extension Into suffix and debug postfix. Pybind11 is currently treating both as suffix, which is problematic when something else defines the DEBUG_POSTFIX because they will be concatenated. pybind11_extension sets SUFFIX to _d.something and if DEBUG_POSTFIX is set to _d. _d + _d.something = _d_d.something The issue has been reported at: #4699 * style: pre-commit fixes * fix(cmake): support postfix for old FindPythonInterp mode too Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 4fb111b commit 5891867

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

tools/FindPythonLibsNew.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,20 @@ _pybind11_get_if_undef(_PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
188188
_pybind11_get_if_undef(_PYTHON_VALUES 1 PYTHON_PREFIX)
189189
_pybind11_get_if_undef(_PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
190190
_pybind11_get_if_undef(_PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
191-
_pybind11_get_if_undef(_PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
192191
_pybind11_get_if_undef(_PYTHON_VALUES 5 PYTHON_IS_DEBUG)
193192
_pybind11_get_if_undef(_PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
194193
_pybind11_get_if_undef(_PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
195194
_pybind11_get_if_undef(_PYTHON_VALUES 8 PYTHON_LIBDIR)
196195
_pybind11_get_if_undef(_PYTHON_VALUES 9 PYTHON_MULTIARCH)
197196

197+
list(GET _PYTHON_VALUES 4 _PYTHON_MODULE_EXT_SUFFIX)
198+
if(PYBIND11_PYTHONLIBS_OVERWRITE OR NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX)
199+
get_filename_component(PYTHON_MODULE_DEBUG_POSTFIX "${_PYTHON_MODULE_EXT_SUFFIX}" NAME_WE)
200+
endif()
201+
if(PYBIND11_PYTHONLIBS_OVERWRITE OR NOT DEFINED PYTHON_MODULE_EXTENSION)
202+
get_filename_component(PYTHON_MODULE_EXTENSION "${_PYTHON_MODULE_EXT_SUFFIX}" EXT)
203+
endif()
204+
198205
# Make sure the Python has the same pointer-size as the chosen compiler
199206
# Skip if CMAKE_SIZEOF_VOID_P is not defined
200207
# This should be skipped for (non-Apple) cross-compiles (like EMSCRIPTEN)

tools/pybind11NewTools.cmake

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,36 @@ endif()
9595

9696
# Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
9797
# required for PyPy3 (as of 7.3.1)
98-
if(NOT DEFINED PYTHON_MODULE_EXTENSION)
98+
if(NOT DEFINED PYTHON_MODULE_EXTENSION OR NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX)
9999
execute_process(
100100
COMMAND
101101
"${${_Python}_EXECUTABLE}" "-c"
102102
"import sys, importlib; s = importlib.import_module('distutils.sysconfig' if sys.version_info < (3, 10) else 'sysconfig'); print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))"
103-
OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
104-
ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR
103+
OUTPUT_VARIABLE _PYTHON_MODULE_EXT_SUFFIX
104+
ERROR_VARIABLE _PYTHON_MODULE_EXT_SUFFIX_ERR
105105
OUTPUT_STRIP_TRAILING_WHITESPACE)
106106

107-
if(_PYTHON_MODULE_EXTENSION STREQUAL "")
107+
if(_PYTHON_MODULE_EXT_SUFFIX STREQUAL "")
108108
message(
109109
FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'"
110-
"package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}")
110+
"package is not installed. Full error message:\n${_PYTHON_MODULE_EXT_SUFFIX_ERR}"
111+
)
111112
endif()
112113

113114
# This needs to be available for the pybind11_extension function
114-
set(PYTHON_MODULE_EXTENSION
115-
"${_PYTHON_MODULE_EXTENSION}"
116-
CACHE INTERNAL "")
115+
if(NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX)
116+
get_filename_component(_PYTHON_MODULE_DEBUG_POSTFIX "${_PYTHON_MODULE_EXT_SUFFIX}" NAME_WE)
117+
set(PYTHON_MODULE_DEBUG_POSTFIX
118+
"${_PYTHON_MODULE_DEBUG_POSTFIX}"
119+
CACHE INTERNAL "")
120+
endif()
121+
122+
if(NOT DEFINED PYTHON_MODULE_EXTENSION)
123+
get_filename_component(_PYTHON_MODULE_EXTENSION "${_PYTHON_MODULE_EXT_SUFFIX}" EXT)
124+
set(PYTHON_MODULE_EXTENSION
125+
"${_PYTHON_MODULE_EXTENSION}"
126+
CACHE INTERNAL "")
127+
endif()
117128
endif()
118129

119130
# Python debug libraries expose slightly different objects before 3.8
@@ -253,6 +264,9 @@ endfunction()
253264

254265
function(pybind11_extension name)
255266
# The extension is precomputed
256-
set_target_properties(${name} PROPERTIES PREFIX "" SUFFIX "${PYTHON_MODULE_EXTENSION}")
257-
267+
set_target_properties(
268+
${name}
269+
PROPERTIES PREFIX ""
270+
DEBUG_POSTFIX "${PYTHON_MODULE_DEBUG_POSTFIX}"
271+
SUFFIX "${PYTHON_MODULE_EXTENSION}")
258272
endfunction()

tools/pybind11Tools.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ _pybind11_promote_to_cache(PYTHON_INCLUDE_DIRS)
6565
_pybind11_promote_to_cache(PYTHON_LIBRARIES)
6666
_pybind11_promote_to_cache(PYTHON_MODULE_PREFIX)
6767
_pybind11_promote_to_cache(PYTHON_MODULE_EXTENSION)
68+
_pybind11_promote_to_cache(PYTHON_MODULE_DEBUG_POSTFIX)
6869
_pybind11_promote_to_cache(PYTHON_VERSION_MAJOR)
6970
_pybind11_promote_to_cache(PYTHON_VERSION_MINOR)
7071
_pybind11_promote_to_cache(PYTHON_VERSION)
@@ -148,8 +149,11 @@ endif()
148149

149150
function(pybind11_extension name)
150151
# The prefix and extension are provided by FindPythonLibsNew.cmake
151-
set_target_properties(${name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
152-
SUFFIX "${PYTHON_MODULE_EXTENSION}")
152+
set_target_properties(
153+
${name}
154+
PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
155+
DEBUG_POSTFIX "${PYTHON_MODULE_DEBUG_POSTFIX}"
156+
SUFFIX "${PYTHON_MODULE_EXTENSION}")
153157
endfunction()
154158

155159
# Build a Python extension module:

0 commit comments

Comments
 (0)