b778c39beaf14bf9bdf94ef67dd18980cd593f71
[alexxy/gromacs.git] / cmake / gmxPythonDiscovery.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2020, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 # Perform Python installation discovery early and in one place, for consistency.
36 #
37 # Note: If necessary, the Python location can be hinted with Python3_ROOT_DIR
38 # For additional parameters affecting Python installation discovery, see
39 # https://cmake.org/cmake/help/latest/module/FindPython3.html#hints
40 if(FIND_PACKAGE_MESSAGE_DETAILS_Python3)
41     # Keep quiet on subsequent runs of cmake
42     set(Python3_FIND_QUIETLY ON)
43     set(PythonInterp_FIND_QUIETLY ON)
44 endif()
45 if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
46     if (NOT Python3_FIND_STRATEGY)
47         # If the user provides a hint for the Python installation with Python3_ROOT_DIR,
48         # prevent FindPython3 from overriding the choice with a newer Python version
49         # when CMP0094 is set to OLD.
50         set(Python3_FIND_STRATEGY LOCATION)
51     endif ()
52     if(NOT Python3_FIND_VIRTUALENV)
53         # We advocate using Python venvs to manage package availability, so by default
54         # we want to preferentially discover user-space software.
55         set(Python3_FIND_VIRTUALENV FIRST)
56     endif()
57 else()
58     if(NOT Python3_FIND_REGISTRY)
59         # We advocate using Python venvs to manage package availability, so by default
60         # we want to preferentially discover user-space software.
61         set(Python3_FIND_REGISTRY LAST)
62     endif()
63     # Make package discovery consistent with Unix behavior and our documented
64     # suggestions for installing dependencies.
65     set(CMAKE_FIND_FRAMEWORK LAST)
66 endif ()
67 if(GMX_PYTHON_PACKAGE)
68     find_package(Python3 3.6 COMPONENTS Interpreter Development)
69     if (NOT Python3_FOUND OR NOT Python3_Development_FOUND)
70         message(FATAL_ERROR "Could not locate Python development requirements. \
71                 Provide appropriate CMake hints or set GMX_PYTHON_PACKAGE=OFF")
72     endif ()
73 else()
74     find_package(Python3 3.6 COMPONENTS Interpreter)
75 endif()
76
77 # Provide hints for other Python detection that may occur later.
78 #
79 # Other components, such as pybind and googletest, may expect the
80 # PYTHON_EXECUTABLE variable from pre-3.12 FindPythonInterp.cmake.
81 if (Python3_Interpreter_FOUND)
82     set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE FILEPATH "Location hint for Python interpreter.")
83 endif ()
84 # We've already generated all of the output we need, even though other subcomponents
85 # may call find_package(PythonInterp) later on.
86 set(Python3_FIND_QUIETLY ON)
87 set(PythonInterp_FIND_QUIETLY ON)
88 # Older versions of FindPythonLibs and FindPythonInterp might not search for
89 # Python newer than 3.7 by default. (as of CMake 3.9)
90 # Note that this hint is not used by the newer FindPython module we rely on above.
91 set(Python_ADDITIONAL_VERSIONS 3.8 3.9 3.10)