FindPython2.cmake 14 KB

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