FindPython2.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. The following components are supported:
  10. * ``Interpreter``: search for Python 2 interpreter
  11. * ``Compiler``: search for Python 2 compiler. Only offered by IronPython.
  12. * ``Development``: search for development artifacts (include directories and
  13. libraries). This component includes two sub-components which can be specified
  14. independently:
  15. * ``Development.Module``: search for artifacts for Python 2 module
  16. developments.
  17. * ``Development.Embed``: search for artifacts for Python 2 embedding
  18. developments.
  19. * ``NumPy``: search for NumPy include directories.
  20. If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
  21. If component ``Development`` is specified, it implies sub-components
  22. ``Development.Module`` and ``Development.Embed``.
  23. To ensure consistent versions between components ``Interpreter``, ``Compiler``,
  24. ``Development`` (or one of its sub-components) and ``NumPy``, specify all
  25. components at the same time::
  26. find_package (Python2 COMPONENTS Interpreter Development)
  27. This module looks only for version 2 of Python. This module can be used
  28. concurrently with :module:`FindPython3` module to use both Python versions.
  29. The :module:`FindPython` module can be used if Python version does not matter
  30. for you.
  31. .. note::
  32. If components ``Interpreter`` and ``Development`` (or one of its
  33. sub-components) are both specified, this module search only for interpreter
  34. with same platform architecture as the one defined by ``CMake``
  35. configuration. This constraint does not apply if only ``Interpreter``
  36. component is specified.
  37. Imported Targets
  38. ^^^^^^^^^^^^^^^^
  39. This module defines the following :ref:`Imported Targets <Imported Targets>`
  40. (when :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  41. ``Python2::Interpreter``
  42. Python 2 interpreter. Target defined if component ``Interpreter`` is found.
  43. ``Python2::Compiler``
  44. Python 2 compiler. Target defined if component ``Compiler`` is found.
  45. ``Python2::Module``
  46. Python 2 library for Python module. Target defined if component
  47. ``Development.Module`` is found.
  48. ``Python2::Python``
  49. Python 2 library for Python embedding. Target defined if component
  50. ``Development.Embed`` is found.
  51. ``Python2::NumPy``
  52. NumPy library for Python 2. Target defined if component ``NumPy`` is found.
  53. Result Variables
  54. ^^^^^^^^^^^^^^^^
  55. This module will set the following variables in your project
  56. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  57. ``Python2_FOUND``
  58. System has the Python 2 requested components.
  59. ``Python2_Interpreter_FOUND``
  60. System has the Python 2 interpreter.
  61. ``Python2_EXECUTABLE``
  62. Path to the Python 2 interpreter.
  63. ``Python2_INTERPRETER_ID``
  64. A short string unique to the interpreter. Possible values include:
  65. * Python
  66. * ActivePython
  67. * Anaconda
  68. * Canopy
  69. * IronPython
  70. * PyPy
  71. ``Python2_STDLIB``
  72. Standard platform independent installation directory.
  73. Information returned by
  74. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``
  75. or else ``sysconfig.get_path('stdlib')``.
  76. ``Python2_STDARCH``
  77. Standard platform dependent installation directory.
  78. Information returned by
  79. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``
  80. or else ``sysconfig.get_path('platstdlib')``.
  81. ``Python2_SITELIB``
  82. Third-party platform independent installation directory.
  83. Information returned by
  84. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``
  85. or else ``sysconfig.get_path('purelib')``.
  86. ``Python2_SITEARCH``
  87. Third-party platform dependent installation directory.
  88. Information returned by
  89. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``
  90. or else ``sysconfig.get_path('platlib')``.
  91. ``Python2_Compiler_FOUND``
  92. System has the Python 2 compiler.
  93. ``Python2_COMPILER``
  94. Path to the Python 2 compiler. Only offered by IronPython.
  95. ``Python2_COMPILER_ID``
  96. A short string unique to the compiler. Possible values include:
  97. * IronPython
  98. ``Python2_DOTNET_LAUNCHER``
  99. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  100. ``Python2_Development_FOUND``
  101. System has the Python 2 development artifacts.
  102. ``Python2_Development.Module_FOUND``
  103. System has the Python 2 development artifacts for Python module.
  104. ``Python2_Development.Embed_FOUND``
  105. System has the Python 2 development artifacts for Python embedding.
  106. ``Python2_INCLUDE_DIRS``
  107. The Python 2 include directories.
  108. ``Python2_LIBRARIES``
  109. The Python 2 libraries.
  110. ``Python2_LIBRARY_DIRS``
  111. The Python 2 library directories.
  112. ``Python2_RUNTIME_LIBRARY_DIRS``
  113. The Python 2 runtime library directories.
  114. ``Python2_VERSION``
  115. Python 2 version.
  116. ``Python2_VERSION_MAJOR``
  117. Python 2 major version.
  118. ``Python2_VERSION_MINOR``
  119. Python 2 minor version.
  120. ``Python2_VERSION_PATCH``
  121. Python 2 patch version.
  122. ``Python2_PyPy_VERSION``
  123. Python 2 PyPy version.
  124. ``Python2_NumPy_FOUND``
  125. System has the NumPy.
  126. ``Python2_NumPy_INCLUDE_DIRS``
  127. The NumPy include directories.
  128. ``Python2_NumPy_VERSION``
  129. The NumPy version.
  130. Hints
  131. ^^^^^
  132. ``Python2_ROOT_DIR``
  133. Define the root directory of a Python 2 installation.
  134. ``Python2_USE_STATIC_LIBS``
  135. * If not defined, search for shared libraries and static libraries in that
  136. order.
  137. * If set to TRUE, search **only** for static libraries.
  138. * If set to FALSE, search **only** for shared libraries.
  139. ``Python2_FIND_STRATEGY``
  140. This variable defines how lookup will be done.
  141. The ``Python2_FIND_STRATEGY`` variable can be set to one of the following:
  142. * ``VERSION``: Try to find the most recent version in all specified
  143. locations.
  144. This is the default if policy :policy:`CMP0094` is undefined or set to
  145. ``OLD``.
  146. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  147. constraints is founded.
  148. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  149. ``Python2_FIND_REGISTRY``
  150. On Windows the ``Python2_FIND_REGISTRY`` variable determine the order
  151. of preference between registry and environment variables.
  152. the ``Python2_FIND_REGISTRY`` variable can be set to one of the following:
  153. * ``FIRST``: Try to use registry before environment variables.
  154. This is the default.
  155. * ``LAST``: Try to use registry after environment variables.
  156. * ``NEVER``: Never try to use registry.
  157. ``Python2_FIND_FRAMEWORK``
  158. On macOS the ``Python2_FIND_FRAMEWORK`` variable determine the order of
  159. preference between Apple-style and unix-style package components.
  160. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  161. variable.
  162. .. note::
  163. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  164. If ``Python2_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  165. variable will be used, if any.
  166. ``Python2_FIND_VIRTUALENV``
  167. This variable defines the handling of virtual environments managed by
  168. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  169. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  170. takes precedence over ``Python2_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  171. variables. The ``Python2_FIND_VIRTUALENV`` variable can be set to one of the
  172. following:
  173. * ``FIRST``: The virtual environment is used before any other standard
  174. paths to look-up for the interpreter. This is the default.
  175. * ``ONLY``: Only the virtual environment is used to look-up for the
  176. interpreter.
  177. * ``STANDARD``: The virtual environment is not used to look-up for the
  178. interpreter but environment variable ``PATH`` is always considered.
  179. In this case, variable ``Python2_FIND_REGISTRY`` (Windows) or
  180. ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  181. ``NEVER`` to select preferably the interpreter from the virtual
  182. environment.
  183. .. note::
  184. If the component ``Development`` is requested, it is **strongly**
  185. recommended to also include the component ``Interpreter`` to get expected
  186. result.
  187. ``Python2_FIND_IMPLEMENTATIONS``
  188. This variable defines, in an ordered list, the different implementations
  189. which will be searched. The ``Python2_FIND_IMPLEMENTATIONS`` variable can
  190. hold the following values:
  191. * ``CPython``: this is the standard implementation. Various products, like
  192. ``Anaconda`` or ``ActivePython``, rely on this implementation.
  193. * ``IronPython``: This implementation use the ``CSharp`` language for
  194. ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
  195. See `IronPython <http://ironpython.net>`_.
  196. * ``PyPy``: This implementation use ``RPython`` language and
  197. ``RPython translation toolchain`` to produce the python interpreter.
  198. See `PyPy <https://www.pypy.org>`_.
  199. The default value is:
  200. * Windows platform: ``CPython``, ``IronPython``
  201. * Other platforms: ``CPython``
  202. .. note::
  203. This hint has the lowest priority of all hints, so even if, for example,
  204. you specify ``IronPython`` first and ``CPython`` in second, a python
  205. product based on ``CPython`` can be selected because, for example with
  206. ``Python2_FIND_STRATEGY=LOCATION``, each location will be search first for
  207. ``IronPython`` and second for ``CPython``.
  208. .. note::
  209. When ``IronPython`` is specified, on platforms other than ``Windows``, the
  210. ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available
  211. through the ``PATH`` variable.
  212. Artifacts Specification
  213. ^^^^^^^^^^^^^^^^^^^^^^^
  214. To solve special cases, it is possible to specify directly the artifacts by
  215. setting the following variables:
  216. ``Python2_EXECUTABLE``
  217. The path to the interpreter.
  218. ``Python2_COMPILER``
  219. The path to the compiler.
  220. ``Python2_DOTNET_LAUNCHER``
  221. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  222. ``Python2_LIBRARY``
  223. The path to the library. It will be used to compute the
  224. variables ``Python2_LIBRARIES``, ``Python2_LIBRARY_DIRS`` and
  225. ``Python2_RUNTIME_LIBRARY_DIRS``.
  226. ``Python2_INCLUDE_DIR``
  227. The path to the directory of the ``Python`` headers. It will be used to
  228. compute the variable ``Python2_INCLUDE_DIRS``.
  229. ``Python2_NumPy_INCLUDE_DIR``
  230. The path to the directory of the ``NumPy`` headers. It will be used to
  231. compute the variable ``Python2_NumPy_INCLUDE_DIRS``.
  232. .. note::
  233. All paths must be absolute. Any artifact specified with a relative path
  234. will be ignored.
  235. .. note::
  236. When an artifact is specified, all ``HINTS`` will be ignored and no search
  237. will be performed for this artifact.
  238. If more than one artifact is specified, it is the user's responsibility to
  239. ensure the consistency of the various artifacts.
  240. By default, this module supports multiple calls in different directories of a
  241. project with different version/component requirements while providing correct
  242. and consistent results for each call. To support this behavior, ``CMake`` cache
  243. is not used in the traditional way which can be problematic for interactive
  244. specification. So, to enable also interactive specification, module behavior
  245. can be controlled with the following variable:
  246. ``Python2_ARTIFACTS_INTERACTIVE``
  247. Selects the behavior of the module. This is a boolean variable:
  248. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  249. specification variables so that users can edit them interactively.
  250. This disables support for multiple version/component requirements.
  251. * If set to ``FALSE`` or undefined: Enable multiple version/component
  252. requirements.
  253. Commands
  254. ^^^^^^^^
  255. This module defines the command ``Python2_add_library`` (when
  256. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  257. :command:`add_library` and adds a dependency to target ``Python2::Python`` or,
  258. when library type is ``MODULE``, to target ``Python2::Module`` and takes care
  259. of Python module naming rules::
  260. Python2_add_library (<name> [STATIC | SHARED | MODULE]
  261. <source1> [<source2> ...])
  262. If library type is not specified, ``MODULE`` is assumed.
  263. #]=======================================================================]
  264. set (_PYTHON_PREFIX Python2)
  265. set (_Python2_REQUIRED_VERSION_MAJOR 2)
  266. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  267. if (COMMAND __Python2_add_library)
  268. macro (Python2_add_library)
  269. __Python2_add_library (Python2 ${ARGV})
  270. endmacro()
  271. endif()
  272. unset (_PYTHON_PREFIX)