Skip to content

Commit

Permalink
COMP: Python Stable ABI support configuration improvements
Browse files Browse the repository at this point in the history
For Windows, `abi3` is not used in the binary suffix.

Use the Development.SABIModule with find_package Python3 to get the
corresponding library and link to it. Note that recent ITKPythonPackage
scripts and scikit-build-core are needed to set this up correctly,
especially on Windows.
  • Loading branch information
thewtex committed Apr 3, 2024
1 parent 7e4c0c5 commit 8da6c2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMake/ITKSetPython3Vars.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if(PYTHON_DEVELOPMENT_REQUIRED)
set(_specified_Python3_EXECUTABLE ${Python3_EXECUTABLE})
endif()
# set(Python3_FIND_REGISTRY LAST) # default is FIRST. Do we need/want this?
find_package(Python3 ${PYTHON_REQUIRED_VERSION} COMPONENTS Interpreter Development)
find_package(Python3 ${PYTHON_REQUIRED_VERSION} COMPONENTS Interpreter Development.Module ${SKBUILD_SABI_COMPONENT})
if(DEFINED _specified_Python3_EXECUTABLE)
set(Python3_EXECUTABLE
${_specified_Python3_EXECUTABLE}
Expand Down
21 changes: 12 additions & 9 deletions Wrapping/macro_files/itk_end_wrap_module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ ${DO_NOT_WAIT_FOR_THREADS_CALLS}

# build all the c++ files from this module in a common lib
if(NOT TARGET ${lib})
set(development_component "Development.Module")
if(ITK_USE_PYTHON_LIMITED_API)
set(development_component "Development.SABIModule")
endif()
find_package(Python3 COMPONENTS Interpreter ${development_component} REQUIRED)
add_library(${lib} MODULE ${cpp_file} ${ITK_WRAP_PYTHON_CXX_FILES} ${WRAPPER_LIBRARY_CXX_SOURCES})
set_target_properties(${lib} PROPERTIES PREFIX "_")

Expand All @@ -405,13 +410,7 @@ ${DO_NOT_WAIT_FOR_THREADS_CALLS}
endif()
# extension is not the same on windows
if(WIN32)
# normally need *.pyd
# python_d requires libraries named *_d.pyd
if (ITK_USE_PYTHON_LIMITED_API)
set_target_properties(${lib} PROPERTIES SUFFIX .abi3.pyd)
else()
set_target_properties(${lib} PROPERTIES SUFFIX .pyd)
endif()
set_target_properties(${lib} PROPERTIES SUFFIX .pyd)
set_target_properties(${lib} PROPERTIES DEBUG_POSTFIX "_d")

if(MSVC)
Expand All @@ -434,13 +433,17 @@ ${DO_NOT_WAIT_FOR_THREADS_CALLS}
unset(ipo_is_supported)
endif()

# Python Limited API
# Python Limited API / Stable ABI
if (ITK_USE_PYTHON_LIMITED_API)
target_compile_definitions(${lib} PUBLIC -DPy_LIMITED_API=0x03110000)
endif()
# Link the modules together
target_link_libraries(${lib} LINK_PUBLIC ${WRAPPER_LIBRARY_LINK_LIBRARIES})
itk_target_link_libraries_with_dynamic_lookup(${lib} LINK_PUBLIC ${Python3_LIBRARIES})
if (ITK_USE_PYTHON_LIMITED_API)
itk_target_link_libraries_with_dynamic_lookup(${lib} LINK_PUBLIC ${Python3_SABI_LIBRARIES})
else()
itk_target_link_libraries_with_dynamic_lookup(${lib} LINK_PUBLIC ${Python3_LIBRARIES})
endif()

if(USE_COMPILER_HIDDEN_VISIBILITY)
# Prefer to use target properties supported by newer cmake
Expand Down

0 comments on commit 8da6c2e

Please sign in to comment.