FindPython2.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. FindPython2
  5. -----------
  6. Find Python 2 interpreter, compiler and development environment (include
  7. directories and libraries).
  8. The following components are supported:
  9. * ``Interpreter``: search for Python 2 interpreter
  10. * ``Compiler``: search for Python 2 compiler. Only offered by IronPython.
  11. * ``Development``: search for development artifacts (include directories and
  12. libraries). This component includes two sub-components which can be specified
  13. independently:
  14. * ``Development.Module``: search for artifacts for Python 2 module
  15. developments.
  16. * ``Development.Embed``: search for artifacts for Python 2 embedding
  17. developments.
  18. * ``NumPy``: search for NumPy include directories.
  19. If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
  20. If component ``Development`` is specified, it implies sub-components
  21. ``Development.Module`` and ``Development.Embed``.
  22. To ensure consistent versions between components ``Interpreter``, ``Compiler``,
  23. ``Development`` (or one of its sub-components) and ``NumPy``, specify all
  24. components at the same time::
  25. find_package (Python2 COMPONENTS Interpreter Development)
  26. This module looks only for version 2 of Python. This module can be used
  27. concurrently with :module:`FindPython3` module to use both Python versions.
  28. The :module:`FindPython` module can be used if Python version does not matter
  29. for you.
  30. .. note::
  31. If components ``Interpreter`` and ``Development`` (or one of its
  32. sub-components) are both specified, this module search only for interpreter
  33. with same platform architecture as the one defined by ``CMake``
  34. configuration. This contraint does not apply if only ``Interpreter``
  35. component is specified.
  36. Imported Targets
  37. ^^^^^^^^^^^^^^^^
  38. This module defines the following :ref:`Imported Targets <Imported Targets>`
  39. (when :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  40. ``Python2::Interpreter``
  41. Python 2 interpreter. Target defined if component ``Interpreter`` is found.
  42. ``Python2::Compiler``
  43. Python 2 compiler. Target defined if component ``Compiler`` is found.
  44. ``Python2::Module``
  45. Python 2 library for Python module. Target defined if component
  46. ``Development.Module`` is found.
  47. ``Python2::Python``
  48. Python 2 library for Python embedding. Target defined if component
  49. ``Development.Embed`` is found.
  50. ``Python2::NumPy``
  51. NumPy library for Python 2. Target defined if component ``NumPy`` is found.
  52. Result Variables
  53. ^^^^^^^^^^^^^^^^
  54. This module will set the following variables in your project
  55. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  56. ``Python2_FOUND``
  57. System has the Python 2 requested components.
  58. ``Python2_Interpreter_FOUND``
  59. System has the Python 2 interpreter.
  60. ``Python2_EXECUTABLE``
  61. Path to the Python 2 interpreter.
  62. ``Python2_INTERPRETER_ID``
  63. A short string unique to the interpreter. Possible values include:
  64. * Python
  65. * ActivePython
  66. * Anaconda
  67. * Canopy
  68. * IronPython
  69. * PyPy
  70. ``Python2_STDLIB``
  71. Standard platform independent installation directory.
  72. Information returned by
  73. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``
  74. or else ``sysconfig.get_path('stdlib')``.
  75. ``Python2_STDARCH``
  76. Standard platform dependent installation directory.
  77. Information returned by
  78. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``
  79. or else ``sysconfig.get_path('platstdlib')``.
  80. ``Python2_SITELIB``
  81. Third-party platform independent installation directory.
  82. Information returned by
  83. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``
  84. or else ``sysconfig.get_path('purelib')``.
  85. ``Python2_SITEARCH``
  86. Third-party platform dependent installation directory.
  87. Information returned by
  88. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``
  89. or else ``sysconfig.get_path('platlib')``.
  90. ``Python2_Compiler_FOUND``
  91. System has the Python 2 compiler.
  92. ``Python2_COMPILER``
  93. Path to the Python 2 compiler. Only offered by IronPython.
  94. ``Python2_COMPILER_ID``
  95. A short string unique to the compiler. Possible values include:
  96. * IronPython
  97. ``Python2_Development_FOUND``
  98. System has the Python 2 development artifacts.
  99. ``Python2_Development.Module_FOUND``
  100. System has the Python 2 development artifacts for Python module.
  101. ``Python2_Development.Embed_FOUND``
  102. System has the Python 2 development artifacts for Python embedding.
  103. ``Python2_INCLUDE_DIRS``
  104. The Python 2 include directories.
  105. ``Python2_LIBRARIES``
  106. The Python 2 libraries.
  107. ``Python2_LIBRARY_DIRS``
  108. The Python 2 library directories.
  109. ``Python2_RUNTIME_LIBRARY_DIRS``
  110. The Python 2 runtime library directories.
  111. ``Python2_VERSION``
  112. Python 2 version.
  113. ``Python2_VERSION_MAJOR``
  114. Python 2 major version.
  115. ``Python2_VERSION_MINOR``
  116. Python 2 minor version.
  117. ``Python2_VERSION_PATCH``
  118. Python 2 patch version.
  119. ``Python2_PyPy_VERSION``
  120. Python 2 PyPy version.
  121. ``Python2_NumPy_FOUND``
  122. System has the NumPy.
  123. ``Python2_NumPy_INCLUDE_DIRS``
  124. The NumPy include directries.
  125. ``Python2_NumPy_VERSION``
  126. The NumPy version.
  127. Hints
  128. ^^^^^
  129. ``Python2_ROOT_DIR``
  130. Define the root directory of a Python 2 installation.
  131. ``Python2_USE_STATIC_LIBS``
  132. * If not defined, search for shared libraries and static libraries in that
  133. order.
  134. * If set to TRUE, search **only** for static libraries.
  135. * If set to FALSE, search **only** for shared libraries.
  136. ``Python2_FIND_STRATEGY``
  137. This variable defines how lookup will be done.
  138. The ``Python2_FIND_STRATEGY`` variable can be set to one of the following:
  139. * ``VERSION``: Try to find the most recent version in all specified
  140. locations.
  141. This is the default if policy :policy:`CMP0094` is undefined or set to
  142. ``OLD``.
  143. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  144. constraints is founded.
  145. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  146. ``Python2_FIND_REGISTRY``
  147. On Windows the ``Python2_FIND_REGISTRY`` variable determine the order
  148. of preference between registry and environment variables.
  149. the ``Python2_FIND_REGISTRY`` variable can be set to one of the following:
  150. * ``FIRST``: Try to use registry before environment variables.
  151. This is the default.
  152. * ``LAST``: Try to use registry after environment variables.
  153. * ``NEVER``: Never try to use registry.
  154. ``Python2_FIND_FRAMEWORK``
  155. On macOS the ``Python2_FIND_FRAMEWORK`` variable determine the order of
  156. preference between Apple-style and unix-style package components.
  157. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  158. variable.
  159. .. note::
  160. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  161. If ``Python2_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  162. variable will be used, if any.
  163. ``Python2_FIND_VIRTUALENV``
  164. This variable defines the handling of virtual environments managed by
  165. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  166. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  167. takes precedence over ``Python2_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  168. variables. The ``Python2_FIND_VIRTUALENV`` variable can be set to one of the
  169. following:
  170. * ``FIRST``: The virtual environment is used before any other standard
  171. paths to look-up for the interpreter. This is the default.
  172. * ``ONLY``: Only the virtual environment is used to look-up for the
  173. interpreter.
  174. * ``STANDARD``: The virtual environment is not used to look-up for the
  175. interpreter but environment variable ``PATH`` is always considered.
  176. In this case, variable ``Python2_FIND_REGISTRY`` (Windows) or
  177. ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  178. ``NEVER`` to select preferably the interpreter from the virtual
  179. environment.
  180. .. note::
  181. If the component ``Development`` is requested, it is **strongly**
  182. recommended to also include the component ``Interpreter`` to get expected
  183. result.
  184. ``Python2_FIND_IMPLEMENTATIONS``
  185. This variable defines, in an ordered list, the different implementations
  186. which will be searched. The ``Python2_FIND_IMPLEMENTATIONS`` variable can
  187. hold the following values:
  188. * ``CPython``: this is the standard implementation. Various products, like
  189. ``Anaconda`` or ``ActivePython``, rely on this implementation.
  190. * ``IronPython``: This implementation use the ``CSharp`` language for
  191. ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
  192. See `IronPython <http://ironpython.net>`_.
  193. * ``PyPy``: This implementation use ``RPython`` language and
  194. ``RPython translation toolchain`` to produce the python interpreter.
  195. See `PyPy <https://www.pypy.org>`_.
  196. The default value is the list: ``CPython``, ``IronPython``.
  197. .. note::
  198. This hint has the lowest priority of all hints, so even if, for example,
  199. you specify ``IronPython`` first and ``CPython`` in second, a python
  200. product based on ``CPython`` can be selected because, for example with
  201. ``Python2_FIND_STRATEGY=LOCATION``, each location will be search first for
  202. ``IronPython`` and second for ``CPython``.
  203. Artifacts Specification
  204. ^^^^^^^^^^^^^^^^^^^^^^^
  205. To solve special cases, it is possible to specify directly the artifacts by
  206. setting the following variables:
  207. ``Python2_EXECUTABLE``
  208. The path to the interpreter.
  209. ``Python2_COMPILER``
  210. The path to the compiler.
  211. ``Python2_LIBRARY``
  212. The path to the library. It will be used to compute the
  213. variables ``Python2_LIBRARIES``, ``Python2_LIBRAY_DIRS`` and
  214. ``Python2_RUNTIME_LIBRARY_DIRS``.
  215. ``Python2_INCLUDE_DIR``
  216. The path to the directory of the ``Python`` headers. It will be used to
  217. compute the variable ``Python2_INCLUDE_DIRS``.
  218. ``Python2_NumPy_INCLUDE_DIR``
  219. The path to the directory of the ``NumPy`` headers. It will be used to
  220. compute the variable ``Python2_NumPy_INCLUDE_DIRS``.
  221. .. note::
  222. All paths must be absolute. Any artifact specified with a relative path
  223. will be ignored.
  224. .. note::
  225. When an artifact is specified, all ``HINTS`` will be ignored and no search
  226. will be performed for this artifact.
  227. If more than one artifact is specified, it is the user's responsability to
  228. ensure the consistency of the various artifacts.
  229. By default, this module supports multiple calls in different directories of a
  230. project with different version/component requirements while providing correct
  231. and consistent results for each call. To support this behavior, ``CMake`` cache
  232. is not used in the traditional way which can be problematic for interactive
  233. specification. So, to enable also interactive specification, module behavior
  234. can be controled with the following variable:
  235. ``Python2_ARTIFACTS_INTERACTIVE``
  236. Selects the behavior of the module. This is a boolean variable:
  237. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  238. specification variables so that users can edit them interactively.
  239. This disables support for multiple version/component requirements.
  240. * If set to ``FALSE`` or undefined: Enable multiple version/component
  241. requirements.
  242. Commands
  243. ^^^^^^^^
  244. This module defines the command ``Python2_add_library`` (when
  245. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  246. :command:`add_library` and adds a dependency to target ``Python2::Python`` or,
  247. when library type is ``MODULE``, to target ``Python2::Module`` and takes care
  248. of Python module naming rules::
  249. Python2_add_library (<name> [STATIC | SHARED | MODULE]
  250. <source1> [<source2> ...])
  251. If library type is not specified, ``MODULE`` is assumed.
  252. #]=======================================================================]
  253. set (_PYTHON_PREFIX Python2)
  254. set (_Python2_REQUIRED_VERSION_MAJOR 2)
  255. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  256. if (COMMAND __Python2_add_library)
  257. macro (Python2_add_library)
  258. __Python2_add_library (Python2 ${ARGV})
  259. endmacro()
  260. endif()
  261. unset (_PYTHON_PREFIX)