Skip to content

Commit 7222679

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
1 parent 8998362 commit 7222679

9 files changed

Lines changed: 38 additions & 44 deletions

CMake/ctkBlockCheckDependencies.cmake

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

9292
set(PythonQt_enabling_variable PYTHONQT_LIBRARIES)
93-
set(${PythonQt_enabling_variable}_INCLUDE_DIRS PYTHONQT_INCLUDE_DIR PYTHON_INCLUDE_DIRS)
93+
set(${PythonQt_enabling_variable}_INCLUDE_DIRS PYTHONQT_INCLUDE_DIR Python3_INCLUDE_DIRS)
9494
set(${PythonQt_enabling_variable}_FIND_PACKAGE_CMD PythonQt)
9595

9696
set(QtSOAP_enabling_variable QtSOAP_LIBRARIES)

CMake/ctkMacroBuildLibWrapper.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ 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)
99-
set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
98+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
99+
set(my_EXTRA_Python3_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
100100

101101
# Does a header having the expected filename exists ?
102102
string(REGEX REPLACE "^CTK" "ctk" lib_name_lc_ctk ${lib_name})
@@ -122,7 +122,7 @@ macro(ctkMacroBuildLibWrapper)
122122
list(APPEND KIT_PYTHONQT_SRCS ${DECORATOR_HEADER})
123123
endif()
124124
add_library(${lib_name}PythonQt ${MY_WRAPPER_LIBRARY_TYPE} ${KIT_PYTHONQT_SRCS})
125-
target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_PYTHON_LIBRARIES})
125+
target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_Python3_LIBRARIES})
126126
if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "STATIC")
127127
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
128128
set_target_properties(${lib_name}PythonQt PROPERTIES COMPILE_FLAGS "-fPIC")

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: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,16 @@ if(NOT DEFINED PYTHONQT_INSTALL_DIR)
7474
endforeach()
7575
endif()
7676

77-
# 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()
83-
endif()
84-
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)
77+
# CMakeLists.txt already required Python3 with Interpreter and Development
78+
# before reaching this block. A subsequent dependency (e.g. system VTK) may
79+
# call find_package(Python3) with a narrower component set, which resets
80+
# Python3_Development_FOUND. Force a fresh REQUIRED detection so the cache
81+
# carries Development.Module + Development.Embed alongside Interpreter.
82+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
9183

92-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
84+
# Bridge to legacy PYTHON_* names that downstream CTK CMake code still reads.
85+
set(PYTHON_INCLUDE_DIR "${Python3_INCLUDE_DIRS}")
86+
set(PYTHON_LIBRARY "${Python3_LIBRARY}")
9387

9488
set(revision_tag 7ef4c5ee066b8ef1e7dc609b14071d504f59d4ba) # patched-v4.0.1-2026-03-16-5cd9b581f
9589
if(${proj}_REVISION_TAG)
@@ -299,7 +293,7 @@ set(PythonQt_DIR ${PYTHONQT_INSTALL_DIR})
299293
mark_as_superbuild(
300294
VARS
301295
PYTHONQT_INSTALL_DIR:PATH
302-
PYTHON_EXECUTABLE:FILEPATH # FindPythonInterp expects PYTHON_EXECUTABLE variable to be defined
296+
Python3_EXECUTABLE:FILEPATH # FindPythonInterp expects Python3_EXECUTABLE variable to be defined
303297
PYTHON_INCLUDE_DIR:PATH # FindPythonQt expects PYTHON_INCLUDE_DIR variable to be defined
304298
PYTHON_INCLUDE_DIR2:PATH
305299
PYTHON_LIBRARY:FILEPATH # FindPythonQt expects PYTHON_LIBRARY variable to be defined

CMakeExternals/VTK.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ if(NOT DEFINED VTK_DIR AND NOT ${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})
6262

6363
if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
6464

65-
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
65+
set(Python3_EXECUTABLE ${Python3_EXECUTABLE})
6666
set(Python3_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
6767
set(Python3_LIBRARY ${PYTHON_LIBRARY})
6868
set(Python3_LIBRARY_DEBUG ${PYTHON_LIBRARY})
6969
set(Python3_LIBRARY_RELEASE ${PYTHON_LIBRARY})
7070
find_package(Python3 COMPONENTS Interpreter Development)
7171

72-
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
72+
ctkFunctionExtractOptimizedLibrary(Python3_LIBRARIES PYTHON_LIBRARY)
7373
list(APPEND additional_vtk_cmakevars
7474
# FindPythonInterp, FindPythonLibs
75-
-DPYTHON_EXECUTABLE:PATH=${PYTHON_EXECUTABLE}
75+
-DPython3_EXECUTABLE:PATH=${Python3_EXECUTABLE}
7676
-DPYTHON_INCLUDE_DIR:PATH=${PYTHON_INCLUDE_DIR}
7777
-DPYTHON_LIBRARY:FILEPATH=${PYTHON_LIBRARY}
7878
-DPYTHON_DEBUG_LIBRARIES:FILEPATH=${PYTHON_DEBUG_LIBRARIES}

CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -965,16 +965,16 @@ endif()
965965

966966
# Check if dependencies are satisfied
967967
if(CTK_LIB_Scripting/Python/Core)
968-
if(NOT PYTHONINTERP_FOUND)
969-
find_package(PythonInterp)
970-
if(NOT PYTHONINTERP_FOUND)
971-
message(FATAL_ERROR "PYTHON_EXECUTABLE variable should be set to build CTK_LIB_Scripting/Python")
968+
if(NOT Python3_Interpreter_FOUND)
969+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
970+
if(NOT Python3_Interpreter_FOUND)
971+
message(FATAL_ERROR "Python3_EXECUTABLE variable should be set to build CTK_LIB_Scripting/Python")
972972
endif()
973973
endif()
974-
if(NOT PYTHONLIBS_FOUND)
975-
find_package(PythonLibs)
976-
if(NOT PYTHONLIBS_FOUND)
977-
message(FATAL_ERROR "PYTHON_LIBRARIES and PYTHON_INCLUDE_DIRS should be set to build CTK_LIB_Scripting/Python")
974+
if(NOT Python3_Development_FOUND)
975+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
976+
if(NOT Python3_Development_FOUND)
977+
message(FATAL_ERROR "Python3_LIBRARIES and Python3_INCLUDE_DIRS should be set to build CTK_LIB_Scripting/Python")
978978
endif()
979979
endif()
980980
endif()

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(

0 commit comments

Comments
 (0)