FindPython.cmake 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPython
  5. ----------
  6. Find Python interpreter, compiler and development environment (include
  7. directories and libraries).
  8. Three components are supported:
  9. * ``Interpreter``: search for Python interpreter.
  10. * ``Compiler``: search for Python compiler. Only offered by IronPython.
  11. * ``Development``: search for development artifacts (include directories and
  12. libraries).
  13. If no ``COMPONENTS`` is specified, ``Interpreter`` is assumed.
  14. To ensure consistent versions between components ``Interpreter``, ``Compiler``
  15. and ``Development``, specify all components at the same time::
  16. find_package (Python COMPONENTS Interpreter Development)
  17. This module looks preferably for version 3 of Python. If not found, version 2
  18. is searched.
  19. To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
  20. :module:`FindPython2` modules rather than this one.
  21. Imported Targets
  22. ^^^^^^^^^^^^^^^^
  23. This module defines the following :ref:`Imported Targets <Imported Targets>`
  24. (when :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  25. ``Python::Interpreter``
  26. Python interpreter. Target defined if component ``Interpreter`` is found.
  27. ``Python::Compiler``
  28. Python compiler. Target defined if component ``Compiler`` is found.
  29. ``Python::Python``
  30. Python library. Target defined if component ``Development`` is found.
  31. Result Variables
  32. ^^^^^^^^^^^^^^^^
  33. This module will set the following variables in your project
  34. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  35. ``Python_FOUND``
  36. System has the Python requested components.
  37. ``Python_Interpreter_FOUND``
  38. System has the Python interpreter.
  39. ``Python_EXECUTABLE``
  40. Path to the Python interpreter.
  41. ``Python_INTERPRETER_ID``
  42. A short string unique to the interpreter. Possible values include:
  43. * Python
  44. * ActivePython
  45. * Anaconda
  46. * Canopy
  47. * IronPython
  48. ``Python_STDLIB``
  49. Standard platform independent installation directory.
  50. Information returned by
  51. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
  52. ``Python_STDARCH``
  53. Standard platform dependent installation directory.
  54. Information returned by
  55. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
  56. ``Python_SITELIB``
  57. Third-party platform independent installation directory.
  58. Information returned by
  59. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
  60. ``Python_SITEARCH``
  61. Third-party platform dependent installation directory.
  62. Information returned by
  63. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
  64. ``Python_Compiler_FOUND``
  65. System has the Python compiler.
  66. ``Python_COMPILER``
  67. Path to the Python compiler. Only offered by IronPython.
  68. ``Python_COMPILER_ID``
  69. A short string unique to the compiler. Possible values include:
  70. * IronPython
  71. ``Python_Development_FOUND``
  72. System has the Python development artifacts.
  73. ``Python_INCLUDE_DIRS``
  74. The Python include directories.
  75. ``Python_LIBRARIES``
  76. The Python libraries.
  77. ``Python_LIBRARY_DIRS``
  78. The Python library directories.
  79. ``Python_RUNTIME_LIBRARY_DIRS``
  80. The Python runtime library directories.
  81. ``Python_VERSION``
  82. Python version.
  83. ``Python_VERSION_MAJOR``
  84. Python major version.
  85. ``Python_VERSION_MINOR``
  86. Python minor version.
  87. ``Python_VERSION_PATCH``
  88. Python patch version.
  89. Hints
  90. ^^^^^
  91. ``Python_ROOT_DIR``
  92. Define the root directory of a Python installation.
  93. ``Python_USE_STATIC_LIBS``
  94. * If not defined, search for shared libraries and static libraries in that
  95. order.
  96. * If set to TRUE, search **only** for static libraries.
  97. * If set to FALSE, search **only** for shared libraries.
  98. ``Python_FIND_REGISTRY``
  99. On Windows the ``Python_FIND_REGISTRY`` variable determine the order
  100. of preference between registry and environment variables.
  101. the ``Python_FIND_REGISTRY`` variable can be set to empty or one of the
  102. following:
  103. * ``FIRST``: Try to use registry before environment variables.
  104. This is the default.
  105. * ``LAST``: Try to use registry after environment variables.
  106. * ``NEVER``: Never try to use registry.
  107. ``CMAKE_FIND_FRAMEWORK``
  108. On OS X the :variable:`CMAKE_FIND_FRAMEWORK` variable determine the order of
  109. preference between Apple-style and unix-style package components.
  110. .. note::
  111. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  112. .. note::
  113. If a Python virtual environment is configured, set variable
  114. ``Python_FIND_REGISTRY`` (Windows) or ``CMAKE_FIND_FRAMEWORK`` (macOS) with
  115. value ``LAST`` or ``NEVER`` to select it preferably.
  116. Commands
  117. ^^^^^^^^
  118. This module defines the command ``Python_add_library`` (when
  119. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  120. :command:`add_library`, but takes care of Python module naming rules
  121. (only applied if library is of type ``MODULE``), and adds a dependency to target
  122. ``Python::Python``::
  123. Python_add_library (my_module MODULE src1.cpp)
  124. If library type is not specified, ``MODULE`` is assumed.
  125. #]=======================================================================]
  126. set (_PYTHON_PREFIX Python)
  127. if (DEFINED Python_FIND_VERSION)
  128. set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  129. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  130. else()
  131. # iterate over versions in quiet and NOT required modes to avoid multiple
  132. # "Found" messages and prematurally failure.
  133. set (_Python_QUIETLY ${Python_FIND_QUIETLY})
  134. set (_Python_REQUIRED ${Python_FIND_REQUIRED})
  135. set (Python_FIND_QUIETLY TRUE)
  136. set (Python_FIND_REQUIRED FALSE)
  137. set (_Python_REQUIRED_VERSIONS 3 2)
  138. set (_Python_REQUIRED_VERSION_LAST 2)
  139. foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
  140. set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
  141. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  142. if (Python_FOUND OR
  143. _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
  144. break()
  145. endif()
  146. # clean-up some CACHE variables to ensure look-up restart from scratch
  147. foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
  148. unset (${_Python_ITEM} CACHE)
  149. endforeach()
  150. endforeach()
  151. unset (Python_FIND_VERSION)
  152. set (Python_FIND_QUIETLY ${_Python_QUIETLY})
  153. set (Python_FIND_REQUIRED ${_Python_REQUIRED})
  154. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  155. # call again validation command to get "Found" or error message
  156. find_package_handle_standard_args (Python HANDLE_COMPONENTS
  157. REQUIRED_VARS ${_Python_REQUIRED_VARS}
  158. VERSION_VAR Python_VERSION)
  159. endif()
  160. endif()
  161. if (COMMAND __Python_add_library)
  162. macro (Python_add_library)
  163. __Python_add_library (Python ${ARGV})
  164. endmacro()
  165. endif()
  166. unset (_PYTHON_PREFIX)