Require pybind 2.6 from environment for gmxapi Python package extension module.
[alexxy/gromacs.git] / cmake / gmxPythonDiscovery.cmake
index 21a1743f664743b7c7f943478ad5dec3cfbfd3f6..c6adf6954222c11fcb98c4fcf790cedec4be35c7 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2020, by the GROMACS development team, led by
+# Copyright (c) 2020,2021, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
 # the research papers on the package. Check out http://www.gromacs.org.
 
 # Perform Python installation discovery early and in one place, for consistency.
-#
+
+if($<VERSION_GREATER_EQUAL:${CMAKE_VERSION},"3.18">)
+    # With embedded packages in this repository, the module-scoped default behavior of
+    # FindPython3 version/component requirements and artifact specification is likely
+    # confusing (at best) or leading to bad logic (at worst).
+    # https://cmake.org/cmake/help/latest/module/FindPython3.html#artifacts-specification
+    option(Python3_ARTIFACTS_INTERACTIVE TRUE
+           "Make artifacts specification global and cached.")
+elseif(NOT FIND_PACKAGE_MESSAGE_DETAILS_Python3)
+    # On first run, check whether the user has triggered the behavior described above.
+    if (Python3_EXECUTABLE
+            OR Python3_COMPILER
+            OR Python3_DOTNET_LAUNCHER
+            OR Python3_LIBRARY
+            OR Python3_INCLUDE_DIR
+            OR Python3_NumPy_INCLUDE_DIR)
+        message(WARNING
+                "Specifying FindPython3 artifacts may complicate consistent Python detection "
+                "in projects bundled with GROMACS. See "
+                "https://cmake.org/cmake/help/latest/module/FindPython3.html#artifacts-specification"
+                )
+    endif ()
+endif()
+
 # Note: If necessary, the Python location can be hinted with Python3_ROOT_DIR
 # For additional parameters affecting Python installation discovery, see
 # https://cmake.org/cmake/help/latest/module/FindPython3.html#hints
@@ -42,23 +65,25 @@ 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)
-# 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(GMX_PYTHON_PACKAGE)
-    find_package(Python3 3.6 COMPONENTS Interpreter Development)
-    if (NOT Python3_FOUND OR NOT Python3_Development_FOUND)
-        message(FATAL_ERROR "Could not locate Python development requirements. \
-                Provide appropriate CMake hints or set GMX_PYTHON_PACKAGE=OFF")
-    endif ()
-else()
-    find_package(Python3 3.6 COMPONENTS Interpreter)
+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()
+find_package(Python3 3.7 COMPONENTS Interpreter Development)
+if (GMX_PYTHON_PACKAGE AND (NOT Python3_FOUND OR NOT Python3_Development_FOUND))
+    message(FATAL_ERROR "Could not locate Python development requirements. \
+            Provide appropriate CMake hints or set GMX_PYTHON_PACKAGE=OFF")
+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 +93,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)