FindPython.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. ``Python::Interpreter``
  25. Python interpreter. Target defined if component ``Interpreter`` is found.
  26. ``Python::Compiler``
  27. Python compiler. Target defined if component ``Compiler`` is found.
  28. ``Python::Python``
  29. Python library. Target defined if component ``Development`` is found.
  30. Result Variables
  31. ^^^^^^^^^^^^^^^^
  32. This module will set the following variables in your project
  33. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  34. ``Python_FOUND``
  35. System has the Python requested components.
  36. ``Python_Interpreter_FOUND``
  37. System has the Python interpreter.
  38. ``Python_EXECUTABLE``
  39. Path to the Python interpreter.
  40. ``Python_INTERPRETER_ID``
  41. A short string unique to the interpreter. Possible values include:
  42. * Python
  43. * ActivePython
  44. * Anaconda
  45. * Canopy
  46. * IronPython
  47. ``Python_STDLIB``
  48. Standard platform independent installation directory.
  49. Information returned by
  50. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
  51. ``Python_STDARCH``
  52. Standard platform dependent installation directory.
  53. Information returned by
  54. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
  55. ``Python_SITELIB``
  56. Third-party platform independent installation directory.
  57. Information returned by
  58. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
  59. ``Python_SITEARCH``
  60. Third-party platform dependent installation directory.
  61. Information returned by
  62. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
  63. ``Python_Compiler_FOUND``
  64. System has the Python compiler.
  65. ``Python_COMPILER``
  66. Path to the Python compiler. Only offered by IronPython.
  67. ``Python_COMPILER_ID``
  68. A short string unique to the compiler. Possible values include:
  69. * IronPython
  70. ``Python_Development_FOUND``
  71. System has the Python development artifacts.
  72. ``Python_INCLUDE_DIRS``
  73. The Python include directories.
  74. ``Python_LIBRARIES``
  75. The Python libraries.
  76. ``Python_LIBRARY_DIRS``
  77. The Python library directories.
  78. ``Python_RUNTIME_LIBRARY_DIRS``
  79. The Python runtime library directories.
  80. ``Python_VERSION``
  81. Python version.
  82. ``Python_VERSION_MAJOR``
  83. Python major version.
  84. ``Python_VERSION_MINOR``
  85. Python minor version.
  86. ``Python_VERSION_PATCH``
  87. Python patch version.
  88. Hints
  89. ^^^^^
  90. ``Python_ROOT_DIR``
  91. Define the root directory of a Python installation.
  92. ``Python_USE_STATIC_LIBS``
  93. * If not defined, search for shared libraries and static libraries in that
  94. order.
  95. * If set to TRUE, search **only** for static libraries.
  96. * If set to FALSE, search **only** for shared libraries.
  97. Commands
  98. ^^^^^^^^
  99. This module defines the command ``Python_add_library`` which have the same
  100. semantic as :command:`add_library` but take care of Python module naming rules
  101. (only applied if library is of type ``MODULE``) and add dependency to target
  102. ``Python::Python``::
  103. Python_add_library (my_module MODULE src1.cpp)
  104. If library type is not specified, ``MODULE`` is assumed.
  105. #]=======================================================================]
  106. set (_PYTHON_PREFIX Python)
  107. if (DEFINED Python_FIND_VERSION)
  108. set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  109. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  110. else()
  111. # iterate over versions in quiet and NOT required modes to avoid multiple
  112. # "Found" messages and prematurally failure.
  113. set (_Python_QUIETLY ${Python_FIND_QUIETLY})
  114. set (_Python_REQUIRED ${Python_FIND_REQUIRED})
  115. set (Python_FIND_QUIETLY TRUE)
  116. set (Python_FIND_REQUIRED FALSE)
  117. foreach (_Python_REQUIRED_VERSION_MAJOR IN ITEMS 3 2)
  118. set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
  119. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  120. if (Python_FOUND)
  121. break()
  122. endif()
  123. # clean-up some CACHE variables to ensure look-up restart from scratch
  124. foreach (_Python_ITEM IN ITEMS EXECUTABLE COMPILER
  125. LIBRARY_RELEASE RUNTIME_LIBRARY_RELEASE
  126. LIBRARY_DEBUG RUNTIME_LIBRARY_DEBUG
  127. INCLUDE_DIR)
  128. unset (Python_${_Python_ITEM} CACHE)
  129. endforeach()
  130. endforeach()
  131. unset (Python_FIND_VERSION)
  132. set (Python_FIND_QUIETLY ${_Python_QUIETLY})
  133. set (Python_FIND_REQUIRED ${_Python_REQUIRED})
  134. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  135. # call again validation command to get "Found" or error message
  136. find_package_handle_standard_args (Python HANDLE_COMPONENTS
  137. REQUIRED_VARS ${_Python_REQUIRED_VARS}
  138. VERSION_VAR Python_VERSION)
  139. endif()
  140. endif()
  141. if (COMMAND __Python_add_library)
  142. macro (Python_add_library)
  143. __Python_add_library (Python ${ARGV})
  144. endmacro()
  145. endif()
  146. unset (_PYTHON_PREFIX)