FindPython2.cmake 18 KB

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