FindPython2.cmake 18 KB

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