Skip to content

Commit 2fa66ec

Browse files
committed
COMP: Update to find_package(Python3)
The FindPythonInterp and FindPythonLibs modules are removed. These modules have been deprecated since CMake 3.12. CMake 3.27 and above prefer to not provide the modules. This policy provides compatibility for projects that have not been ported away from them. Projects using the FindPythonInterp and/or FindPythonLibs modules should be updated to use one of their replacements: FindPython3 FindPython2 FindPython Both the Interpreter and Development checks called find_package(Python3 COMPONENTS Interpreter Development REQUIRED) independently. Combine into a single call since find_package(Python3) should request all needed components at once to ensure version consistency. The old bridging code set Python3_ hints from PYTHON_ variables that are no longer populated after the FindPython3 migration. The VTK8 path was also passing Python3_EXECUTABLE under the wrong variable name. Fix by using find_package(Python3) results directly: - VTK8 path: pass Python3_EXECUTABLE, Python3_INCLUDE_DIRS, and the extracted PYTHON_LIBRARY under the old PYTHON_ variable names - VTK9 path: pass the same values under Python3_ hint names The superbuild export was missing Python3_INCLUDE_DIRS, Python3_LIBRARIES, and PYTHON_EXECUTABLE. Add the modern Python3_ variables so downstream superbuild projects using FindPython3 receive them, and re-add PYTHON_EXECUTABLE for legacy project compatibility. The default FindPython3 strategy (VERSION) searches all locations and picks the newest Python3, which can find the wrong Python in multi-version environments or when a virtualenv is active. Set LOCATION strategy to stop at the first version found, respecting PATH order and virtual environments.
1 parent 935c4e0 commit 2fa66ec

10 files changed

Lines changed: 52 additions & 61 deletions

File tree

CMake/ctkBlockCheckDependencies.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ set(${Log4Qt_enabling_variable}_INCLUDE_DIRS Log4Qt_INCLUDE_DIRS)
7575
set(${Log4Qt_enabling_variable}_FIND_PACKAGE_CMD Log4Qt)
7676

7777
set(PythonQt_enabling_variable PYTHONQT_LIBRARIES)
78-
set(${PythonQt_enabling_variable}_INCLUDE_DIRS PYTHONQT_INCLUDE_DIR PYTHON_INCLUDE_DIRS)
78+
set(${PythonQt_enabling_variable}_INCLUDE_DIRS PYTHONQT_INCLUDE_DIR Python3_INCLUDE_DIRS)
7979
set(${PythonQt_enabling_variable}_FIND_PACKAGE_CMD PythonQt)
8080

8181
set(QtSOAP_enabling_variable QtSOAP_LIBRARIES)

CMake/ctkMacroBuildLibWrapper.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ macro(ctkMacroBuildLibWrapper)
9595
# Since the PythonQt decorator depends on PythonQt, Python and VTK, let's link against
9696
# these ones to avoid complaints of MSVC
9797
# Note: "LINK_DIRECTORIES" has to be invoked before "ADD_LIBRARY"
98-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
98+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
9999
set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
100100

101101
# Does a header having the expected filename exists ?

CMake/ctkMacroCompilePythonScript.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ function(_ctk_add_compile_python_directories_target target keep_only_pyc)
164164
endif()
165165
endforeach()
166166

167-
if(NOT PYTHONINTERP_FOUND)
168-
find_package(PythonInterp REQUIRED)
167+
if(NOT Python3_Interpreter_FOUND)
168+
find_package(Python3 COMPONENTS Interpreter REQUIRED)
169169
endif()
170-
if(NOT PYTHONLIBS_FOUND)
171-
find_package(PythonLibs REQUIRED)
170+
if(NOT Python3_Development_FOUND)
171+
find_package(Python3 COMPONENTS Development REQUIRED)
172172
endif()
173173

174174
# Extract python lib path
175-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
175+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
176176
get_filename_component(PYTHON_LIBRARY_PATH "${PYTHON_LIBRARY}" PATH)
177177

178178
# Configure cmake script associated with the custom command

CMake/ctkMacroWrapPythonQt.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
6262
# TODO: this find package seems not to work when called form a superbuild, but the call is needed
6363
# in general to find the python interpreter. In CTK, the toplevel CMakeLists.txt does the find
6464
# package so this is a no-op. Other uses of this file may need to have this call so it is still enabled.
65-
if(NOT PYTHONINTERP_FOUND)
66-
find_package(PythonInterp)
67-
if(NOT PYTHONINTERP_FOUND)
68-
message(FATAL_ERROR "PYTHON_EXECUTABLE not specified or inexistent when calling ctkMacroWrapPythonQt")
65+
if(NOT Python3_Interpreter_FOUND)
66+
find_package(Python3 COMPONENTS Interpreter)
67+
if(NOT Python3_Interpreter_FOUND)
68+
message(FATAL_ERROR "Python3_EXECUTABLE not specified or inexistent when calling ctkMacroWrapPythonQt")
6969
endif()
7070
endif()
7171

@@ -157,7 +157,7 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
157157
DEPENDS
158158
${SOURCES_TO_WRAP}
159159
${CTK_CMAKE_DIR}/ctkWrapPythonQt.py
160-
COMMAND ${PYTHON_EXECUTABLE} ${CTK_CMAKE_DIR}/ctkWrapPythonQt.py
160+
COMMAND ${Python3_EXECUTABLE} ${CTK_CMAKE_DIR}/ctkWrapPythonQt.py
161161
--target=${TARGET}
162162
--namespace=${WRAPPING_NAMESPACE}
163163
--output-dir=${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir} ${extra_args}
@@ -188,6 +188,6 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
188188
if(NOT PYTHONQT_FOUND)
189189
message(FATAL_ERROR "error: PythonQt package is required to build ${TARGET}PythonQt")
190190
endif()
191-
include_directories(${PYTHON_INCLUDE_DIRS} ${PYTHONQT_INCLUDE_DIR})
191+
include_directories(${Python3_INCLUDE_DIRS} ${PYTHONQT_INCLUDE_DIR})
192192

193193
endmacro()

CMake/ctk_compile_python_scripts.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ else()
9595
endif()
9696

9797
execute_process(
98-
COMMAND "@PYTHON_EXECUTABLE@" "@compile_all_script@"
98+
COMMAND "@Python3_EXECUTABLE@" "@compile_all_script@"
9999
RESULT_VARIABLE result_var
100100
)
101101
if(NOT result_var STREQUAL 0)

CMakeExternals/PythonQt.cmake

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,19 @@ if(NOT DEFINED PYTHONQT_INSTALL_DIR)
7575
endif()
7676

7777
# Python is required
78-
if(NOT PYTHONLIBS_FOUND)
79-
find_package(PythonLibs)
80-
if(NOT PYTHONLIBS_FOUND)
81-
message(FATAL_ERROR "error: Python is required to build ${PROJECT_NAME}")
82-
endif()
78+
if(NOT Python3_Development_FOUND)
79+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
8380
endif()
8481

85-
# Variable expected by FindPython3 CMake module
86-
set(Python3_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
87-
set(Python3_LIBRARY ${PYTHON_LIBRARY})
88-
set(Python3_LIBRARY_DEBUG ${PYTHON_LIBRARY})
89-
set(Python3_LIBRARY_RELEASE ${PYTHON_LIBRARY})
90-
find_package(Python3 COMPONENTS Development REQUIRED)
82+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
9183

92-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
84+
# Bridge Python3_ results to legacy PYTHON_ variables needed by
85+
# FindPythonQt and downstream superbuild projects
86+
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
87+
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIRS})
88+
set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
89+
set(PYTHON_LIBRARY ${PYTHON_LIBRARY}) # already extracted above
90+
set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
9391

9492
set(revision_tag ec7e1416e17fae645f3b99a8df4533719e17cbf6) # patched-v3.6.1-2025-12-22-469f01f6a
9593
if(${proj}_REVISION_TAG)
@@ -299,10 +297,15 @@ set(PythonQt_DIR ${PYTHONQT_INSTALL_DIR})
299297
mark_as_superbuild(
300298
VARS
301299
PYTHONQT_INSTALL_DIR:PATH
302-
PYTHON_EXECUTABLE:FILEPATH # FindPythonInterp expects PYTHON_EXECUTABLE variable to be defined
303-
PYTHON_INCLUDE_DIR:PATH # FindPythonQt expects PYTHON_INCLUDE_DIR variable to be defined
300+
# FindPython3 variables (modern)
301+
Python3_EXECUTABLE:FILEPATH
302+
Python3_INCLUDE_DIRS:PATH
303+
Python3_LIBRARIES:FILEPATH
304+
# Legacy variables needed by FindPythonQt and older projects
305+
PYTHON_EXECUTABLE:FILEPATH
306+
PYTHON_INCLUDE_DIR:PATH
304307
PYTHON_INCLUDE_DIR2:PATH
305-
PYTHON_LIBRARY:FILEPATH # FindPythonQt expects PYTHON_LIBRARY variable to be defined
308+
PYTHON_LIBRARY:FILEPATH
306309
LABELS "FIND_PACKAGE_VARS"
307310
)
308311
mark_as_superbuild(

CMakeExternals/VTK.cmake

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,23 @@ if(NOT DEFINED VTK_DIR AND NOT ${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})
6767

6868
if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
6969

70-
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
71-
set(Python3_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
72-
set(Python3_LIBRARY ${PYTHON_LIBRARY})
73-
set(Python3_LIBRARY_DEBUG ${PYTHON_LIBRARY})
74-
set(Python3_LIBRARY_RELEASE ${PYTHON_LIBRARY})
75-
find_package(Python3 COMPONENTS Interpreter Development)
76-
77-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
70+
if(NOT Python3_Development_FOUND)
71+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
72+
endif()
73+
74+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
75+
76+
# VTK8 and earlier use the old FindPythonInterp/FindPythonLibs variable names
7877
list(APPEND additional_vtk_cmakevars
79-
# FindPythonInterp, FindPythonLibs
80-
-DPYTHON_EXECUTABLE:PATH=${PYTHON_EXECUTABLE}
81-
-DPYTHON_INCLUDE_DIR:PATH=${PYTHON_INCLUDE_DIR}
78+
-DPYTHON_EXECUTABLE:PATH=${Python3_EXECUTABLE}
79+
-DPYTHON_INCLUDE_DIR:PATH=${Python3_INCLUDE_DIRS}
8280
-DPYTHON_LIBRARY:FILEPATH=${PYTHON_LIBRARY}
83-
-DPYTHON_DEBUG_LIBRARIES:FILEPATH=${PYTHON_DEBUG_LIBRARIES}
8481
)
85-
# VTK9
82+
# VTK9+ uses FindPython3 hint variables
8683
list(APPEND additional_vtk9_cmakevars
87-
# FindPython3
88-
-DPython3_INCLUDE_DIR:PATH=${Python3_INCLUDE_DIR}
89-
-DPython3_LIBRARY:FILEPATH=${Python3_LIBRARY}
90-
-DPython3_LIBRARY_DEBUG:FILEPATH=${Python3_LIBRARY}
91-
-DPython3_LIBRARY_RELEASE:FILEPATH=${Python3_LIBRARY}
9284
-DPython3_EXECUTABLE:FILEPATH=${Python3_EXECUTABLE}
85+
-DPython3_INCLUDE_DIR:PATH=${Python3_INCLUDE_DIRS}
86+
-DPython3_LIBRARY:FILEPATH=${PYTHON_LIBRARY}
9387
)
9488
endif()
9589

CMakeLists.txt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -944,17 +944,11 @@ endif()
944944

945945
# Check if dependencies are satisfied
946946
if(CTK_LIB_Scripting/Python/Core)
947-
if(NOT PYTHONINTERP_FOUND)
948-
find_package(PythonInterp)
949-
if(NOT PYTHONINTERP_FOUND)
950-
message(FATAL_ERROR "PYTHON_EXECUTABLE variable should be set to build CTK_LIB_Scripting/Python")
951-
endif()
952-
endif()
953-
if(NOT PYTHONLIBS_FOUND)
954-
find_package(PythonLibs)
955-
if(NOT PYTHONLIBS_FOUND)
956-
message(FATAL_ERROR "PYTHON_LIBRARIES and PYTHON_INCLUDE_DIRS should be set to build CTK_LIB_Scripting/Python")
957-
endif()
947+
if(NOT Python3_Interpreter_FOUND OR NOT Python3_Development_FOUND)
948+
# Use LOCATION strategy to respect PATH order and virtual environments
949+
# rather than VERSION which picks the newest Python found anywhere.
950+
set(Python3_FIND_STRATEGY LOCATION)
951+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
958952
endif()
959953
endif()
960954

Libs/Scripting/Python/Core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ configure_file(
3636

3737
# Target libraries - See CMake/ctkFunctionGetTargetLibraries.cmake
3838
# The following macro will read the target libraries from the file 'target_libraries.cmake'
39-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
39+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
4040
ctkFunctionGetTargetLibraries(KIT_target_libraries)
4141

4242
ctkMacroBuildLib(

Libs/Visualization/VTK/Core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ endif()
9898

9999
if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
100100
include_directories(
101-
${PYTHON_INCLUDE_DIRS}
101+
${Python3_INCLUDE_DIRS}
102102
${PYTHONQT_INCLUDE_DIR}
103103
)
104104
endif()
@@ -192,7 +192,7 @@ endif()
192192
if(${VTK_VERSION} VERSION_LESS "8.90")
193193

194194
if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
195-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
195+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
196196
list(APPEND VTK_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
197197
if(${VTK_VERSION_MAJOR} GREATER 5)
198198
list(APPEND VTK_LIBRARIES vtkWrappingPythonCore)

0 commit comments

Comments
 (0)