FindPython2.cmake 17 KB

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