Skip to content

Commit

Permalink
COMP: Updating to find_package(Python3)
Browse files Browse the repository at this point in the history
Now that cmake FindPython3 can be universally
used, prefer to only use that set of variable
names for identifying the python executables.

Intial procesing with manual cleanup by:

ITK/Utilities/Maintenance/FindPython3_ModernizeCMake.sh

Finding the correct python version should always respect the
Python3_ROOT_DIR variable provided during building.
  • Loading branch information
hjmjohnson committed Aug 8, 2023
1 parent 23c58db commit 22a225c
Show file tree
Hide file tree
Showing 111 changed files with 172 additions and 343 deletions.
11 changes: 6 additions & 5 deletions CMake/FindNumPy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
# all listed variables are TRUE

if(NOT NUMPY_FOUND)
find_package(PythonInterp)
find_package(PythonLibs)
find_package(Python3 COMPONENTS Interpreter Development)

if(PYTHON_EXECUTABLE)
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
if(Python3_EXECUTABLE)
execute_process(COMMAND "${Python3_EXECUTABLE}"
-c "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE _numpy_include_dir
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand All @@ -30,9 +29,11 @@ if(NOT NUMPY_FOUND)
endif()
endif()

set(_numpy_search_paths ${Python3_INCLUDE_DIRS})
list(prepend _numpy_search_paths "${_numpy_include_dir}")
find_path(NUMPY_INCLUDE_DIR
numpy/arrayobject.h
PATHS "${_numpy_include_dir}" "${PYTHON_INCLUDE_DIR}"
PATHS ${_numpy_search_paths}
PATH_SUFFIXES numpy/core/include
)

Expand Down
120 changes: 0 additions & 120 deletions CMake/FindPythonInterp.cmake

This file was deleted.

6 changes: 3 additions & 3 deletions CMake/FindSphinx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# SPHINX_FOUND

set(_python_paths )
find_package(PythonInterp)
if(PYTHON_EXECUTABLE)
get_filename_component(_python_dir "${PYTHON_EXECUTABLE}" DIRECTORY)
find_package(Python3 COMPONENTS Interpreter)
if(Python3_EXECUTABLE)
get_filename_component(_python_dir "${Python3_EXECUTABLE}" DIRECTORY)
list(APPEND _python_paths
"${_python_dir}"
"${_python_dir}/bin"
Expand Down
4 changes: 2 additions & 2 deletions CMake/ITKDoxygenTAG.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
set( ITKDoxygenTAG_DIR ${CMAKE_BINARY_DIR}/ITKDoxygenTAG )
set( ITKDoxygenTAG_TEMP_DIR ${CMAKE_BINARY_DIR}/ITKDoxygenTAG-TEMP )

find_package(PythonInterp REQUIRED)
find_package(Python3 COMPONENTS Interpreter REQUIRED)

add_custom_command( OUTPUT ${ITKDoxygenTAG_DIR}/InsightDoxygen.tag
COMMAND ${CMAKE_COMMAND} -DITKDoxygenTAG_TEMP_DIR="${ITKDoxygenTAG_TEMP_DIR}" -P ${CMAKE_CURRENT_SOURCE_DIR}/CMake/DownloadDoxygenTAG.cmake
COMMAND ${PYTHON_EXECUTABLE} "${ITKSphinxExamples_SOURCE_DIR}/Utilities/GUnzip.py" "${ITKDoxygenTAG_TEMP_DIR}/InsightDoxygen.tag.gz"
COMMAND ${Python3_EXECUTABLE} "${ITKSphinxExamples_SOURCE_DIR}/Utilities/GUnzip.py" "${ITKDoxygenTAG_TEMP_DIR}/InsightDoxygen.tag.gz"

COMMAND ${CMAKE_COMMAND} -E remove_directory "${ITKDoxygenTAG_DIR}"
COMMAND ${CMAKE_COMMAND} -E rename "${ITKDoxygenTAG_TEMP_DIR}" "${ITKDoxygenTAG_DIR}"
Expand Down
80 changes: 15 additions & 65 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,21 @@ foreach(link ${content_links})
endforeach()
ExternalData_Add_Target(ITKSphinxExamplesData)

if(${ITK_WRAP_PYTHON})
# Use the same Python interpreter as ITK to run the tests.
find_package(PythonInterp ${ITK_WRAP_PYTHON_VERSION} REQUIRED)
mark_as_advanced(PYTHON_EXECUTABLE)
set(PYTHON_REQUIRED_VERSION 3.7)
set(Python_ADDITIONAL_VERSIONS 3.12 3.11 3.10 3.9 3.8 3.7)
if(BUILD_TESTING OR ITK_BUILD_DOCUMENTATION OR ITK_WRAP_PYTHON)
if(ITK_WRAP_PYTHON)
# Use the same Python interpreter as ITK to run the tests.
find_package(Python3 ${ITK_WRAP_PYTHON_VERSION} EXACT COMPONENTS Interpreter REQUIRED)
mark_as_advanced(Python3_EXECUTABLE)
else()
find_package(Python3 ${PYTHON_REQUIRED_VERSION} COMPONENTS Interpreter)
endif()
endif()
find_package(PythonInterp)
if(PYTHON_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import itk"

if(Python3_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
# See if external ITK python packages are installed for this python environment
execute_process(COMMAND ${Python3_EXECUTABLE} -c "import itk"
RESULT_VARIABLE _have_itk_return_code
OUTPUT_QUIET
ERROR_QUIET
Expand All @@ -154,62 +161,6 @@ if(PYTHON_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
set(ITK_WRAP_PYTHON 1)
endif()
endif()
if(BUILD_TESTING OR ITK_BUILD_DOCUMENTATION OR ITK_WRAP_PYTHON)
# Prefer to use more robust FindPython3 module if greater than cmake 3.12.0
if("${CMAKE_VERSION}" VERSION_LESS_EQUAL "3.12.0")
# Use of PythonInterp and PythonLibs is deprecated since cmake version 3.12.0
# Only use deprecated mechanisms for older versions of cmake
set(Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6 3.5)
find_package(PythonInterp)
if(ITK_WRAP_PYTHON)
find_package(PythonLibs REQUIRED)
# check for version mismatch.
if(PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND
AND NOT(PYTHON_VERSION_STRING VERSION_EQUAL PYTHONLIBS_VERSION_STRING))
message(FATAL_ERROR "Python executable (\"${PYTHON_VERSION_STRING}\") and library (\"${PYTHONLIBS_VERSION_STRING}\") version mismatch.")
endif()
endif()
if(PYTHON_VERSION_STRING VERSION_LESS 3.5)
# if python version is less than 3.5, unset so that it appears that no python version is found.
# to emulate the same behavior as find(Python3 ..) from cmake 3.12.0+
unset(PYTHON_EXECUTABLE)
unset(PYTHONINTERP_FOUND)
unset(PYTHON_VERSION_STRING)
unset(PYTHON_INCLUDE_DIRS)
else()
## For forward compatibility with cmake 3.12.0 or greater, emulate variable names from FindPython3.cmake
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
set(Python3_Interpreter_FOUND ${PYTHONINTERP_FOUND})
set(Python3_VERSION ${PYTHON_VERSION_STRING})

set(Python3_Development_FOUND ${PYTHONLIBS_FOUND})
set(Python3_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
set(Python3_LIBRARIES ${PYTHON_LIBRARIES})
endif()
else()
if(ITK_WRAP_PYTHON)
if(DEFINED Python3_EXECUTABLE)
set(_specified_Python3_EXECUTABLE ${Python3_EXECUTABLE})
endif()
find_package(Python3 COMPONENTS Interpreter Development)
set(Python3_EXECUTABLE ${_specified_Python3_EXECUTABLE} CACHE INTERNAL
"Path to the Python interpreter" FORCE)
else()
find_package(Python3 COMPONENTS Interpreter)
endif()
if(NOT Python3_EXECUTABLE AND _Python3_EXECUTABLE)
set(Python3_EXECUTABLE ${_Python3_EXECUTABLE} CACHE INTERNAL
"Path to the Python interpreter" FORCE)
endif()
endif()

if(ITK_WRAP_PYTHON AND Python3_VERSION VERSION_LESS 3.5)
message(FATAL_ERROR "Python versions less than 3.5 are not supported for wrapping. Python version: \"${Python3_VERSION}\".")
endif()
if(ITK_WRAP_PYTHON AND NOT Python3_INCLUDE_DIRS)
message(FATAL_ERROR "Python version ${Python3_VERSION} development environment not found for wrapping.")
endif()
endif()

# Build the documentation?
option(BUILD_DOCUMENTATION "Build the examples documentation." OFF)
Expand Down Expand Up @@ -265,7 +216,6 @@ if(BUILD_DOCUMENTATION)
endif()

# Builds the documentation.
find_package(PythonInterp REQUIRED)
find_package(Sphinx REQUIRED)
find_program(PANDOC_BIN pandoc REQUIRED)

Expand All @@ -288,7 +238,7 @@ if(BUILD_DOCUMENTATION)
)
add_custom_command(TARGET ${sphinx_target_base_name}_html
POST_BUILD
COMMAND ${PYTHON_EXECUTABLE}
COMMAND ${Python3_EXECUTABLE}
${ITKSphinxExamples_SOURCE_DIR}/Utilities/DocumentationTarball.py
${SPHINX_DESTINATION}/html
${ITKSphinxExamples_BINARY_DIR}/ITKSphinxExamples-${ITKSphinxExamples_RELEASE_VERSION}-html
Expand Down
13 changes: 7 additions & 6 deletions Superbuild/External-Python.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
find_package(PythonInterp 3 REQUIRED)
set(MIN_PYTHON_VERSION 3.7)
find_package(Python3 ${MIN_PYTHON_VERSION} COMPONENTS Interpreter REQUIRED)

set(_itk_venv "${CMAKE_CURRENT_BINARY_DIR}/itkpython")
if(WIN32)
set(ITKPYTHON_EXECUTABLE "${_itk_venv}/python.exe" CACHE FILEPATH "Python executable with the ITK package installed" FORCE)
set(ITKPython3_EXECUTABLE "${_itk_venv}/python.exe" CACHE FILEPATH "Python executable with the ITK package installed" FORCE)
set(SPHINX_EXECUTABLE "${_itk_venv}/Scripts/sphinx-build.exe" CACHE FILEPATH "Sphinx executable" FORCE)
set(BLACK_EXECUTABLE "${_itk_venv}/Scripts/black.exe" CACHE FILEPATH "black executable" FORCE)
else()
set(ITKPYTHON_EXECUTABLE "${_itk_venv}/bin/python" CACHE FILEPATH "Python executable with the ITK package installed" FORCE)
set(ITKPython3_EXECUTABLE "${_itk_venv}/bin/python" CACHE FILEPATH "Python executable with the ITK package installed" FORCE)
set(SPHINX_EXECUTABLE "${_itk_venv}/bin/sphinx-build" CACHE FILEPATH "Sphinx executable" FORCE)
set(BLACK_EXECUTABLE "${_itk_venv}/bin/black" CACHE FILEPATH "black executable" FORCE)
endif()
Expand All @@ -15,8 +16,8 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/ITKBlackConfig.cmake.in"

ExternalProject_Add(ITKPython
DOWNLOAD_COMMAND ""
CONFIGURE_COMMAND ${PYTHON_EXECUTABLE} -m venv "${_itk_venv}"
BUILD_COMMAND ${ITKPYTHON_EXECUTABLE} -m pip install --upgrade pip
INSTALL_COMMAND ${ITKPYTHON_EXECUTABLE} -m pip install --ignore-installed itk>=5.4rc1 sphinx==4.4.0 docutils<0.18 traitlets==5.6.0 six black nbsphinx ipywidgets sphinx-contributors ipykernel matplotlib itkwidgets[lab,notebook]>=1.0a21 pydata-sphinx-theme
CONFIGURE_COMMAND ${Python3_EXECUTABLE} -m venv "${_itk_venv}"
BUILD_COMMAND ${ITKPython3_EXECUTABLE} -m pip install --upgrade pip
INSTALL_COMMAND ${ITKPython3_EXECUTABLE} -m pip install --ignore-installed itk>=5.4rc1 sphinx==4.4.0 docutils<0.18 traitlets==5.6.0 six black nbsphinx ipywidgets sphinx-contributors ipykernel matplotlib itkwidgets[lab,notebook]>=1.0a21 pydata-sphinx-theme
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/ITKBlackConfig.cmake
)
8 changes: 6 additions & 2 deletions Superbuild/External-VTK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if(NOT VTK_TAG)
set(VTK_TAG "v9.0.1")
endif()

set(PYTHON_EXECUTABLE "${ITKPYTHON_EXECUTABLE}")
set(Python3_EXECUTABLE "${ITKPython3_EXECUTABLE}")

ExternalProject_Add(VTK
GIT_REPOSITORY "https://gitlab.kitware.com/VTK/VTK.git"
Expand All @@ -23,7 +23,11 @@ ExternalProject_Add(VTK
-DModule_vtkImagingMath:BOOL=ON
-DVTK_WRAP_PYTHON:BOOL=ON
-DExternalData_OBJECT_STORES:STRING=${ExternalData_OBJECT_STORES}
"-DPython3_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}"
"-DPython3_EXECUTABLE:FILEPATH=${Python3_EXECUTABLE}"
-DVTK_USE_SYSTEM_ZLIB:BOOL=ON
"-DZLIB_ROOT:PATH=${ZLIB_ROOT}"
"-DZLIB_INCLUDE_DIR:PATH=${ZLIB_INCLUDE_DIR}"
"-DZLIB_LIBRARY:FILEPATH=${ZLIB_LIBRARY}"
INSTALL_COMMAND ""
LOG_DOWNLOAD 0
LOG_UPDATE 0
Expand Down
4 changes: 2 additions & 2 deletions Superbuild/Superbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ if(WIN32)
set(_use_python_default OFF)
endif()
option(ITKSphinxExamples_USE_WRAP_PYTHON "Enable downloads of the ITK Python wrapping to the superbuild" ${_use_python_default})
if(NOT EXISTS PYTHON_EXECUTABLE AND ITKSphinxExamples_USE_WRAP_PYTHON OR ITKSphinxExamples_USE_VTK)
if(NOT EXISTS Python3_EXECUTABLE AND ITKSphinxExamples_USE_WRAP_PYTHON OR ITKSphinxExamples_USE_VTK)
include(${CMAKE_SOURCE_DIR}/External-Python.cmake)
list(APPEND ITKSphinxExamples_DEPENDENCIES ITKPython)
set(_python_args "-DPYTHON_EXECUTABLE:FILEPATH=${ITKPYTHON_EXECUTABLE}")
set(_python_args "-DPython3_EXECUTABLE:FILEPATH=${ITKPython3_EXECUTABLE}")
endif()

if(NOT VTK_DIR AND ITKSphinxExamples_USE_VTK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ add_test(NAME {{ cookiecutter.example_name }}Test
)

if(ITK_WRAP_PYTHON)
find_package(PythonInterp REQUIRED)
find_package(Python3 COMPONENTS Interpreter REQUIRED)
string(REPLACE . "Python." output_image "${output_image}")
add_test(NAME {{ cookiecutter.example_name }}TestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
${input_image}
${output_image}
${test_options}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(input_image ${CMAKE_CURRENT_BINARY_DIR}/Slicer.png)

if(ITK_WRAP_PYTHON)
add_test(NAME ConvertNumPyArrayToitkImageTestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
${input_image}
SliceRevised.png
)
Expand Down
13 changes: 3 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@ if(Module_ITKVtkGlue OR ITKVtkGlue_LOADED)
endif()

if(BUILD_DOCUMENTATION)
if(NOT PYTHON_EXECUTABLE)
find_package(PythonInterp 3.6 REQUIRED)
if(NOT PYTHONINTERP_FOUND)
if(NOT Python3_EXECUTABLE)
find_package(Python3 ${PYTHON_REQUIRED_VERSION} COMPONENTS Interpreter REQUIRED)
if(NOT Python3_Interpreter_FOUND)
message(SEND_ERROR "Building the documentation requires Python")
else()
# Set here the proper version of Python required to be able to build
# tarballs for each example
set(PYTHON_REQUIRED_VERSION 3.6)
if("${PYTHON_VERSION_STRING}" VERSION_LESS ${PYTHON_REQUIRED_VERSION})
message(SEND_ERROR "Building the documentation requires Python >= ${PYTHON_REQUIRED_VERSION}: ${PYTHON_VERSION_STRING} found at ${PYTHON_EXECUTABLE}")
endif()
endif()
endif()
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Common/AddOffsetToIndex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ add_test(NAME AddOffsetToIndexTest

if(ITK_WRAP_PYTHON)
add_test(NAME AddOffsetToIndexTestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
)
endif()
Loading

0 comments on commit 22a225c

Please sign in to comment.