From fc45cd9bbf886653f84c3efa1485875fa6dd9daf Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Tue, 3 Mar 2026 07:03:01 -0500 Subject: [PATCH 1/3] COMP: Fix Python3::Module target not found on Windows with limited API When ITK_USE_PYTHON_LIMITED_API is ON (Python >= 3.11), the second find_package(Python3) call only requested Development.SABIModule, never creating the Python3::Module target. ITKCommon unconditionally referenced Python3::Module, causing a CMake generate-time error. - Always request Development.Module alongside Development.SABIModule so the Python3::Module target is created regardless of limited API setting - Select Python3::SABIModule or Python3::Module in ITKCommon based on ITK_USE_PYTHON_LIMITED_API for architecturally correct linking - Extend Python3_ROOT_DIR inference to Windows (was Unix/Apple only) so FindPython3 locates the correct installation from virtualenvs --- CMake/ITKSetPython3Vars.cmake | 26 ++++++-------------------- Modules/Core/Common/src/CMakeLists.txt | 9 +++++++-- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/CMake/ITKSetPython3Vars.cmake b/CMake/ITKSetPython3Vars.cmake index 4a396cc07a6..061fde1476e 100644 --- a/CMake/ITKSetPython3Vars.cmake +++ b/CMake/ITKSetPython3Vars.cmake @@ -109,6 +109,7 @@ else() set( _python_find_components Interpreter + Development.Module Development.SABIModule ) else() @@ -195,26 +196,11 @@ else() endif() # If a specific Python3_EXECUTABLE is provided by the user, try to infer - # the corresponding Python3_ROOT_DIR for Unix/macOS/Linux so CMake's - # FindPython3 locates the matching installation or virtual environment. - # This is especially important for virtualenv/venv/conda environments. - if( - DEFINED - Python3_EXECUTABLE - AND - NOT - DEFINED - Python3_ROOT_DIR - AND - ( - UNIX - OR - APPLE - ) - AND - NOT - WIN32 - ) + # the corresponding Python3_ROOT_DIR so CMake's FindPython3 locates the + # matching installation or virtual environment. + # This is especially important for virtualenv/venv/conda environments + # and on Windows where FindPython3 may otherwise pick the wrong installation. + if(DEFINED Python3_EXECUTABLE AND NOT DEFINED Python3_ROOT_DIR) # First, try sys.prefix from the provided interpreter (works for venv/conda) execute_process( COMMAND diff --git a/Modules/Core/Common/src/CMakeLists.txt b/Modules/Core/Common/src/CMakeLists.txt index 59f4d0a2c9f..8203d971c8e 100644 --- a/Modules/Core/Common/src/CMakeLists.txt +++ b/Modules/Core/Common/src/CMakeLists.txt @@ -159,12 +159,17 @@ set( ) if(ITK_WRAP_PYTHON) + if(ITK_USE_PYTHON_LIMITED_API AND TARGET Python3::SABIModule) + set(_itk_python_target Python3::SABIModule) + else() + set(_itk_python_target Python3::Module) + endif() list(APPEND ITKCommon_SRCS itkPyCommand.cxx) set_source_files_properties( itkPyCommand.cxx PROPERTIES INCLUDE_DIRECTORIES - "$" + "$" ) endif() @@ -255,7 +260,7 @@ if(UNIX) endif() endif() if(ITK_WRAP_PYTHON) - target_link_libraries(ITKCommon PRIVATE Python3::Module) + target_link_libraries(ITKCommon PRIVATE ${_itk_python_target}) endif() if(ITK_USE_TBB) From 1d0305e73e138280993d3b102b19f31bb1477919 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Mon, 2 Mar 2026 08:38:44 -0500 Subject: [PATCH 2/3] COMP: Add target_link_directories Python3_LIBRARY_DIRS On Windows, we need to specify the link directory for python311.lib. This is for building the ITKPythonPackage after recent target-based build system and CMake find python changes. --- Modules/Core/Common/src/CMakeLists.txt | 3 +++ Wrapping/macro_files/itk_end_wrap_module.cmake | 1 + 2 files changed, 4 insertions(+) diff --git a/Modules/Core/Common/src/CMakeLists.txt b/Modules/Core/Common/src/CMakeLists.txt index 8203d971c8e..22573b3ea70 100644 --- a/Modules/Core/Common/src/CMakeLists.txt +++ b/Modules/Core/Common/src/CMakeLists.txt @@ -261,6 +261,9 @@ if(UNIX) endif() if(ITK_WRAP_PYTHON) target_link_libraries(ITKCommon PRIVATE ${_itk_python_target}) + if(MSVC) + target_link_directories(ITKCommon PRIVATE ${Python3_LIBRARY_DIRS}) + endif() endif() if(ITK_USE_TBB) diff --git a/Wrapping/macro_files/itk_end_wrap_module.cmake b/Wrapping/macro_files/itk_end_wrap_module.cmake index bb7288d3760..da96eccacb5 100644 --- a/Wrapping/macro_files/itk_end_wrap_module.cmake +++ b/Wrapping/macro_files/itk_end_wrap_module.cmake @@ -535,6 +535,7 @@ ${DO_NOT_WAIT_FOR_THREADS_CALLS} "/wd4244" ) endif() + target_link_directories(${lib} PUBLIC ${Python3_LIBRARY_DIRS}) endif() # Link the modules together From 77cecb1e163cb57433e912eb4715bd570f264a93 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Thu, 5 Mar 2026 13:31:39 -0500 Subject: [PATCH 3/3] COMP: Suppress deprecated warnings in MSVC wrapping builds Add /wd4996 to MSVC compile flags for SWIG-generated Python wrapper targets. This suppresses C4996 warnings triggered by deprecated VariableLengthVector::AllocateElements calls in auto-generated wrapper code. GCC/Clang wrapping builds already suppress all warnings with -w. --- Wrapping/macro_files/itk_end_wrap_module.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Wrapping/macro_files/itk_end_wrap_module.cmake b/Wrapping/macro_files/itk_end_wrap_module.cmake index da96eccacb5..bfb53dd9796 100644 --- a/Wrapping/macro_files/itk_end_wrap_module.cmake +++ b/Wrapping/macro_files/itk_end_wrap_module.cmake @@ -527,12 +527,13 @@ ${DO_NOT_WAIT_FOR_THREADS_CALLS} ) else() if(MSVC) - # Disables 'conversion from 'type1' to 'type2', possible loss of data warnings + # /wd4244: Disables 'conversion from 'type1' to 'type2', possible loss of data warnings + # /wd4996: Disables deprecated declaration warnings in generated wrapper code set_target_properties( ${lib} PROPERTIES COMPILE_FLAGS - "/wd4244" + "/wd4244 /wd4996" ) endif() target_link_directories(${lib} PUBLIC ${Python3_LIBRARY_DIRS})