FindPython2.cmake 14 KB

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