@@ -154,6 +154,62 @@ if(PYTHON_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
154154 set (ITK_WRAP_PYTHON 1)
155155 endif ()
156156endif ()
157+ if (BUILD_TESTING OR ITK_BUILD_DOCUMENTATION OR ITK_WRAP_PYTHON)
158+ # Prefer to use more robust FindPython3 module if greater than cmake 3.12.0
159+ if ("${CMAKE_VERSION} " VERSION_LESS_EQUAL "3.12.0" )
160+ # Use of PythonInterp and PythonLibs is deprecated since cmake version 3.12.0
161+ # Only use deprecated mechanisms for older versions of cmake
162+ set (Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6 3.5)
163+ find_package (PythonInterp)
164+ if (ITK_WRAP_PYTHON)
165+ find_package (PythonLibs REQUIRED)
166+ # check for version mismatch.
167+ if (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND
168+ AND NOT (PYTHON_VERSION_STRING VERSION_EQUAL PYTHONLIBS_VERSION_STRING))
169+ message (FATAL_ERROR "Python executable (\" ${PYTHON_VERSION_STRING} \" ) and library (\" ${PYTHONLIBS_VERSION_STRING} \" ) version mismatch." )
170+ endif ()
171+ endif ()
172+ if (PYTHON_VERSION_STRING VERSION_LESS 3.5)
173+ # if python version is less than 3.5, unset so that it appears that no python version is found.
174+ # to emulate the same behavior as find(Python3 ..) from cmake 3.12.0+
175+ unset (PYTHON_EXECUTABLE)
176+ unset (PYTHONINTERP_FOUND)
177+ unset (PYTHON_VERSION_STRING)
178+ unset (PYTHON_INCLUDE_DIRS)
179+ else ()
180+ ## For forward compatibility with cmake 3.12.0 or greater, emulate variable names from FindPython3.cmake
181+ set (Python3_EXECUTABLE ${PYTHON_EXECUTABLE} )
182+ set (Python3_Interpreter_FOUND ${PYTHONINTERP_FOUND} )
183+ set (Python3_VERSION ${PYTHON_VERSION_STRING} )
184+
185+ set (Python3_Development_FOUND ${PYTHONLIBS_FOUND} )
186+ set (Python3_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} )
187+ set (Python3_LIBRARIES ${PYTHON_LIBRARIES} )
188+ endif ()
189+ else ()
190+ if (ITK_WRAP_PYTHON)
191+ if (DEFINED Python3_EXECUTABLE)
192+ set (_specified_Python3_EXECUTABLE ${Python3_EXECUTABLE} )
193+ endif ()
194+ find_package (Python3 COMPONENTS Interpreter Development)
195+ set (Python3_EXECUTABLE ${_specified_Python3_EXECUTABLE} CACHE INTERNAL
196+ "Path to the Python interpreter" FORCE)
197+ else ()
198+ find_package (Python3 COMPONENTS Interpreter)
199+ endif ()
200+ if (NOT Python3_EXECUTABLE AND _Python3_EXECUTABLE)
201+ set (Python3_EXECUTABLE ${_Python3_EXECUTABLE} CACHE INTERNAL
202+ "Path to the Python interpreter" FORCE)
203+ endif ()
204+ endif ()
205+
206+ if (ITK_WRAP_PYTHON AND Python3_VERSION VERSION_LESS 3.5)
207+ message (FATAL_ERROR "Python versions less than 3.5 are not supported for wrapping. Python version: \" ${Python3_VERSION} \" ." )
208+ endif ()
209+ if (ITK_WRAP_PYTHON AND NOT Python3_INCLUDE_DIRS)
210+ message (FATAL_ERROR "Python version ${Python3_VERSION} development environment not found for wrapping." )
211+ endif ()
212+ endif ()
157213
158214# Build the documentation?
159215option (BUILD_DOCUMENTATION "Build the examples documentation." OFF )
0 commit comments