Improve gmxPythonDiscovery.cmake
authorM. Eric Irrgang <mei2n@virginia.edu>
Wed, 7 Oct 2020 13:28:58 +0000 (13:28 +0000)
committerM. Eric Irrgang <mei2n@virginia.edu>
Wed, 7 Oct 2020 13:28:58 +0000 (13:28 +0000)
* Do a better job of urging CMake 3.15+ to use the requested Python
  installation when a user specifies Python3_ROOT_DIR.
* Adopt another CMake 3.15 feature: Python3_FIND_VIRTUALENV is much more
  readable than the older alternatives.
* Allow FindPython inputs to be set before entering the module (such as
  with `-D` command line arguments) by checking before setting.
* Clarify which parts of this CMake module apply to the main FindPython
  based detection and which parts exist to provide hints to other
  embedded / downstream detection.

Refs #3720

cmake/gmxPythonDiscovery.cmake

index d5bcbaf78457b1c894c1a4440a742146f6a0a096..b778c39beaf14bf9bdf94ef67dd18980cd593f71 100644 (file)
@@ -42,14 +42,28 @@ if(FIND_PACKAGE_MESSAGE_DETAILS_Python3)
     set(Python3_FIND_QUIETLY ON)
     set(PythonInterp_FIND_QUIETLY ON)
 endif()
-# Older CMake versions might not search for Python newer than 3.7.
-set(Python_ADDITIONAL_VERSIONS 3.8 3.9 3.10)
-# We advocate using Python venvs to manage package availability, so by default
-# we want to preferentially discover user-space software.
-set(Python3_FIND_REGISTRY LAST)
-# Make package discovery consistent with Unix behavior and our documented
-# suggestions for installing dependencies.
-set(CMAKE_FIND_FRAMEWORK LAST)
+if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
+    if (NOT Python3_FIND_STRATEGY)
+        # If the user provides a hint for the Python installation with Python3_ROOT_DIR,
+        # prevent FindPython3 from overriding the choice with a newer Python version
+        # when CMP0094 is set to OLD.
+        set(Python3_FIND_STRATEGY LOCATION)
+    endif ()
+    if(NOT Python3_FIND_VIRTUALENV)
+        # We advocate using Python venvs to manage package availability, so by default
+        # we want to preferentially discover user-space software.
+        set(Python3_FIND_VIRTUALENV FIRST)
+    endif()
+else()
+    if(NOT Python3_FIND_REGISTRY)
+        # We advocate using Python venvs to manage package availability, so by default
+        # we want to preferentially discover user-space software.
+        set(Python3_FIND_REGISTRY LAST)
+    endif()
+    # Make package discovery consistent with Unix behavior and our documented
+    # suggestions for installing dependencies.
+    set(CMAKE_FIND_FRAMEWORK LAST)
+endif ()
 if(GMX_PYTHON_PACKAGE)
     find_package(Python3 3.6 COMPONENTS Interpreter Development)
     if (NOT Python3_FOUND OR NOT Python3_Development_FOUND)
@@ -59,6 +73,9 @@ if(GMX_PYTHON_PACKAGE)
 else()
     find_package(Python3 3.6 COMPONENTS Interpreter)
 endif()
+
+# Provide hints for other Python detection that may occur later.
+#
 # Other components, such as pybind and googletest, may expect the
 # PYTHON_EXECUTABLE variable from pre-3.12 FindPythonInterp.cmake.
 if (Python3_Interpreter_FOUND)
@@ -68,3 +85,7 @@ endif ()
 # may call find_package(PythonInterp) later on.
 set(Python3_FIND_QUIETLY ON)
 set(PythonInterp_FIND_QUIETLY ON)
+# Older versions of FindPythonLibs and FindPythonInterp might not search for
+# Python newer than 3.7 by default. (as of CMake 3.9)
+# Note that this hint is not used by the newer FindPython module we rely on above.
+set(Python_ADDITIONAL_VERSIONS 3.8 3.9 3.10)