FindPython.cmake
Go to the documentation of this file.
1 #.rst
2 # FindPython
3 # ----------
4 #
5 # Find Python components such as the interpreter, C libraries, or Python modules.
6 #
7 # This module uses find_package to look for these components using the
8 # specialized Find modules named FindPythonInterp.cmake, FindPythonLibs.cmake,
9 # and FindPythonModules.cmake. A Python_FIND_COMPONENTS list entry that does
10 # not match either "Interp" or "Libs" is assumed to be the name of a Python
11 # module to be searched. See the aforementioned Find modules for more details.
12 
13 #=============================================================================
14 # Copyright 2016 Andreas Schuh <andreas.schuh.84@gmail.com>
15 #
16 # Distributed under the OSI-approved BSD License (the "License");
17 # see accompanying file Copyright.txt for details.
18 #
19 # This software is distributed WITHOUT ANY WARRANTY; without even the
20 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 # See the License for more information.
22 #=============================================================================
23 # (To distribute this file outside of CMake, substitute the full
24 # License text for the above reference.)
25 
26 # ------------------------------------------------------------------------------
27 # Components to look for
29 set (_Python_FIND_Libs FALSE)
31 set (_Python_FIND_Modules_OPTIONAL)
32 set (_Python_FIND_Modules_REQUIRED)
33 
35  set (Python_FIND_COMPONENTS Interp)
36 endif ()
37 
38 foreach (_Python_COMPONENT IN LISTS Python_FIND_COMPONENTS)
39  if (_Python_COMPONENT MATCHES "^(Interp|Libs)$")
40  set (_Python_FIND_${_Python_COMPONENT} TRUE)
41  else ()
42  set (_Python_FIND_Modules TRUE)
43  if (Python_FIND_REQUIRED_${_Python_COMPONENT})
44  list (APPEND _Python_FIND_Modules_REQUIRED ${_Python_COMPONENT})
45  else ()
46  list (APPEND _Python_FIND_Modules_OPTIONAL ${_Python_COMPONENT})
47  endif ()
48  endif ()
49 endforeach ()
50 
51 # ------------------------------------------------------------------------------
52 # Verbose message
53 if (NOT Python_FIND_QUIETLY)
54  set(_Python_FIND_STATUS "Looking for Python [${Python_FIND_COMPONENTS}]")
55  if (NOT Python_FIND_REQUIRED)
56  set(_Python_FIND_STATUS "${_Python_FIND_STATUS} (optional)")
57  endif ()
58  message(STATUS "${_Python_FIND_STATUS}...")
59 endif ()
60 
61 # ------------------------------------------------------------------------------
62 # Look for Python components
63 set (_Python_REQUIRED_VARS)
64 
66  find_package (PythonInterp QUIET MODULE)
67  set (Python_Interp_FOUND ${PythonInterp_FOUND})
68  list (APPEND _Python_REQUIRED_VARS PYTHON_EXECUTABLE)
69 endif ()
70 
71 if (Python_FIND_Libs)
72  find_package (PythonLibs QUIET MODULE)
73  set (Python_Libs_FOUND ${PythonLibs_FOUND})
74  list (APPEND _Python_REQUIRED_VARS PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)
75 endif ()
76 
77 if (Python_FIND_Modules)
78  find_package (PythonModules QUIET MODULE
79  COMPONENTS ${Python_FIND_Modules_REQUIRED}
80  OPTIONAL_COMPONENTS ${Python_FIND_Modules_OPTIONAL}
81  )
82  foreach (_Python_COMPONENT IN LISTS _Python_FIND_Modules_REQUIRED _Python_FIND_Modules_OPTIONAL)
83  set (Python_${_Python_COMPONENT}_FOUND ${PythonModules_${_Python_COMPONENT}_FOUND})
84  endforeach ()
85 endif ()
86 
87 # ------------------------------------------------------------------------------
88 # Handle QUIET, REQUIRED, and [EXACT] VERSION arguments and set Python_FOUND
91 else ()
92  set (_Python_VERSION ${PYTHONLIBS_VERSION_STRING})
93 endif ()
94 
95 if (_Python_REQUIRED_VARS)
96  include(FindPackageHandleStandardArgs)
97  find_package_handle_standard_args(Python
98  REQUIRED_VARS ${_Python_REQUIRED_VARS}
99  VERSION_VAR _Python_VERSION
100  HANDLE_COMPONENTS
101  )
102 else ()
103  set (Python_FOUND 1)
104  set (PYTHON_FOUND 1)
105 endif ()
106 
107 # ------------------------------------------------------------------------------
108 # Verbose message
109 if (NOT Python_FIND_QUIETLY)
110  if (Python_FOUND)
111  if (_Python_VERSION)
112  message(STATUS "${_Python_FIND_STATUS}... - found v${_Python_VERSION}")
113  else ()
114  message(STATUS "${_Python_FIND_STATUS}... - found")
115  endif ()
116  else ()
117  message(STATUS "${_Python_FIND_STATUS}... - not found")
118  endif ()
119 endif ()
120 
121 # ------------------------------------------------------------------------------
122 # Unset local variables
123 unset (_Python_VERSION)
124 unset (_Python_FIND_STATUS)
125 unset (_Python_FIND_Interp)
126 unset (_Python_FIND_Libs)
127 unset (_Python_FIND_Modules)
128 unset (_Python_FIND_Modules_REQUIRED)
129 unset (_Python_FIND_Modules_OPTIONAL)
130 unset (_Python_COMPONENT)
131 unset (_Python_REQUIRED_VARS)
cmake Python_Interp_FOUND
cmake PYTHON_FOUND
cmake _Python_FIND_STATUS
cmake _Python_FIND_Libs
cmake _Python_FIND_Interp
cmake _Python_VERSION
cmake _Python_FIND_Modules
cmake Python_FOUND
macro find_package()
Overloaded find_package() command.
cmake Python_Libs_FOUND
cmake Python_FIND_COMPONENTS
cmake PYTHON_VERSION_STRING