FindPython(3): WITH_SOABI produces incorrect binary name on Windows with Debug builds of CPython
For context of what I'm working on: I'm looking to make Conan's CPython package seamlessly integrate with FindPython(3) (https://github.com/conan-io/conan-center-index/pull/23394), however I believe this is not Conan specific.
Firstly, in order to detect the Debug libs at all on Windows, I set Python(3)_EXECUTABLE
and Python(3)_LIBRARY
(probably unrelated: it's difficult to detect debug builds as a whole, specifying Python(3)_LIBRARY
directly was the only way I could find). Everything more or less works as expected from there, except for when setting WITH_SOABI
.
I can provide context as needed, but I think the only thing that matters other than what I've said so far is the following in CMake:
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
python3_add_library(spam WITH_SOABI module.c)
In release mode, I get the following module name: spam.cp311-win_amd64.pyd
(looks normal, and is importable)
In debug mode, I get the following module name: spam._d.cp311-win_amd64.pyd.pyd
(incorrect, not importable)
If I print the values of Python(3)_SOABI
, I get _d.cp311-win_amd64.pyd
in debug mode and cp311-win_amd64
in release. These are the same as sysconfig.get_config_var('EXT_SUFFIX')
(per the CMake documentation).
It's possible this is partly a CPython issue, since one contains the .pyd
suffix (plus a .
), the other does not, but I did find this in FindPython/support.cmake (https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/FindPython/Support.cmake?ref_type=heads#L4113):
get_property (suffix TARGET ${name} PROPERTY SUFFIX)
if (NOT suffix)
set (suffix "${CMAKE_SHARED_MODULE_SUFFIX}")
endif()
set_property (TARGET ${name} PROPERTY SUFFIX ".${${prefix}_SOABI}${suffix}")
The middle 3 lines don't matter for Windows, since suffix
should already be set to pyd
. I think part of the problem is that it assumes we place a .
immediately before the given suffix, whereas in debug mode it's expecting the _d
to come before the .
. Additionally, the pyd
suffix is being applied twice, but that is probably(?) the fault of CPython.
Normally to properly set the _d
expected by debug builds I would do something like (assuming debug build implies a debug Python):
if(MSVC)
set_target_properties(spam PROPERTIES DEBUG_POSTFIX "_d")
endif()
I would expect WITH_SOABI
to apply this correctly (without the debug == debug assumption).
Any insight would be appreciated!