1
0

FindPython2.cmake 16 KB

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