You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
10 KiB

  1. # - Find python libraries
  2. # This module finds the libraries corresponding to the Python interpreter
  3. # FindPythonInterp provides.
  4. # This code sets the following variables:
  5. #
  6. # PYTHONLIBS_FOUND - have the Python libs been found
  7. # PYTHON_PREFIX - path to the Python installation
  8. # PYTHON_LIBRARIES - path to the python library
  9. # PYTHON_INCLUDE_DIRS - path to where Python.h is found
  10. # PYTHON_MODULE_EXTENSION - lib extension, e.g. '.so' or '.pyd'
  11. # PYTHON_MODULE_PREFIX - lib name prefix: usually an empty string
  12. # PYTHON_SITE_PACKAGES - path to installation site-packages
  13. # PYTHON_IS_DEBUG - whether the Python interpreter is a debug build
  14. #
  15. # Thanks to talljimbo for the patch adding the 'LDVERSION' config
  16. # variable usage.
  17. #=============================================================================
  18. # Copyright 2001-2009 Kitware, Inc.
  19. # Copyright 2012 Continuum Analytics, Inc.
  20. #
  21. # All rights reserved.
  22. #
  23. # Redistribution and use in source and binary forms, with or without
  24. # modification, are permitted provided that the following conditions
  25. # are met:
  26. #
  27. # * Redistributions of source code must retain the above copyright
  28. # notice, this list of conditions and the following disclaimer.
  29. #
  30. # * Redistributions in binary form must reproduce the above copyright
  31. # notice, this list of conditions and the following disclaimer in the
  32. # documentation and/or other materials provided with the distribution.
  33. #
  34. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  35. # nor the names of their contributors may be used to endorse or promote
  36. # products derived from this software without specific prior written
  37. # permission.
  38. #
  39. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  40. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  41. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  42. # # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  43. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  45. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  46. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  47. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  48. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  49. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. #=============================================================================
  51. # Checking for the extension makes sure that `LibsNew` was found and not just `Libs`.
  52. if(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION)
  53. return()
  54. endif()
  55. if(PythonLibsNew_FIND_QUIETLY)
  56. set(_pythonlibs_quiet QUIET)
  57. else()
  58. set(_pythonlibs_quiet "")
  59. endif()
  60. if(PythonLibsNew_FIND_REQUIRED)
  61. set(_pythonlibs_required REQUIRED)
  62. endif()
  63. # Check to see if the `python` command is present and from a virtual
  64. # environment, conda, or GHA activation - if it is, try to use that.
  65. if(NOT DEFINED PYTHON_EXECUTABLE)
  66. if(DEFINED ENV{VIRTUAL_ENV})
  67. find_program(
  68. PYTHON_EXECUTABLE python
  69. PATHS "$ENV{VIRTUAL_ENV}" "$ENV{VIRTUAL_ENV}/bin"
  70. NO_DEFAULT_PATH)
  71. elseif(DEFINED ENV{CONDA_PREFIX})
  72. find_program(
  73. PYTHON_EXECUTABLE python
  74. PATHS "$ENV{CONDA_PREFIX}" "$ENV{CONDA_PREFIX}/bin"
  75. NO_DEFAULT_PATH)
  76. elseif(DEFINED ENV{pythonLocation})
  77. find_program(
  78. PYTHON_EXECUTABLE python
  79. PATHS "$ENV{pythonLocation}" "$ENV{pythonLocation}/bin"
  80. NO_DEFAULT_PATH)
  81. endif()
  82. if(NOT PYTHON_EXECUTABLE)
  83. unset(PYTHON_EXECUTABLE)
  84. endif()
  85. endif()
  86. # Use the Python interpreter to find the libs.
  87. if(NOT PythonLibsNew_FIND_VERSION)
  88. set(PythonLibsNew_FIND_VERSION "")
  89. endif()
  90. find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} ${_pythonlibs_required}
  91. ${_pythonlibs_quiet})
  92. if(NOT PYTHONINTERP_FOUND)
  93. set(PYTHONLIBS_FOUND FALSE)
  94. set(PythonLibsNew_FOUND FALSE)
  95. return()
  96. endif()
  97. # According to https://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
  98. # testing whether sys has the gettotalrefcount function is a reliable, cross-platform
  99. # way to detect a CPython debug interpreter.
  100. #
  101. # The library suffix is from the config var LDVERSION sometimes, otherwise
  102. # VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
  103. execute_process(
  104. COMMAND
  105. "${PYTHON_EXECUTABLE}" "-c" "from distutils import sysconfig as s;import sys;import struct;
  106. print('.'.join(str(v) for v in sys.version_info));
  107. print(sys.prefix);
  108. print(s.get_python_inc(plat_specific=True));
  109. print(s.get_python_lib(plat_specific=True));
  110. print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'));
  111. print(hasattr(sys, 'gettotalrefcount')+0);
  112. print(struct.calcsize('@P'));
  113. print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
  114. print(s.get_config_var('LIBDIR') or '');
  115. print(s.get_config_var('MULTIARCH') or '');
  116. "
  117. RESULT_VARIABLE _PYTHON_SUCCESS
  118. OUTPUT_VARIABLE _PYTHON_VALUES
  119. ERROR_VARIABLE _PYTHON_ERROR_VALUE)
  120. if(NOT _PYTHON_SUCCESS MATCHES 0)
  121. if(PythonLibsNew_FIND_REQUIRED)
  122. message(FATAL_ERROR "Python config failure:\n${_PYTHON_ERROR_VALUE}")
  123. endif()
  124. set(PYTHONLIBS_FOUND FALSE)
  125. set(PythonLibsNew_FOUND FALSE)
  126. return()
  127. endif()
  128. # Convert the process output into a list
  129. if(WIN32)
  130. string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
  131. endif()
  132. string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
  133. string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
  134. list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
  135. list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
  136. list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
  137. list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
  138. list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
  139. list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
  140. list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
  141. list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
  142. list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
  143. list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
  144. # Make sure the Python has the same pointer-size as the chosen compiler
  145. # Skip if CMAKE_SIZEOF_VOID_P is not defined
  146. if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
  147. if(PythonLibsNew_FIND_REQUIRED)
  148. math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
  149. math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
  150. message(FATAL_ERROR "Python config failure: Python is ${_PYTHON_BITS}-bit, "
  151. "chosen compiler is ${_CMAKE_BITS}-bit")
  152. endif()
  153. set(PYTHONLIBS_FOUND FALSE)
  154. set(PythonLibsNew_FOUND FALSE)
  155. return()
  156. endif()
  157. # The built-in FindPython didn't always give the version numbers
  158. string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
  159. list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
  160. list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
  161. list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
  162. set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.${PYTHON_VERSION_PATCH}")
  163. # Make sure all directory separators are '/'
  164. string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX "${PYTHON_PREFIX}")
  165. string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
  166. string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}")
  167. if(CMAKE_HOST_WIN32)
  168. set(PYTHON_LIBRARY "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
  169. # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
  170. # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
  171. if(NOT EXISTS "${PYTHON_LIBRARY}")
  172. get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
  173. set(PYTHON_LIBRARY "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
  174. elseif(DEFINED VCPKG_TOOLCHAIN AND NOT EXISTS "${PYTHON_LIBRARY}")
  175. set(PYTHON_LIBRARY "${PYTHON_PREFIX}/../../lib/python${PYTHON_LIBRARY_SUFFIX}.lib")
  176. endif()
  177. if(DEFINED VCPKG_TOOLCHAIN)
  178. unset(PYTHON_LIBRARY)
  179. find_library(
  180. PYTHON_LIBRARY
  181. NAMES "python${PYTHON_LIBRARY_SUFFIX}"
  182. NO_SYSTEM_ENVIRONMENT_PATH)
  183. find_library(PYTHON_DEBUG_LIBRARY
  184. NAMES python${PYTHON_LIBRARY_SUFFIX}_d
  185. NO_SYSTEM_ENVIRONMENT_PATH
  186. )
  187. endif()
  188. # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib
  189. if(DEFINED ENV{MSYSTEM}
  190. AND MINGW
  191. AND NOT EXISTS "${PYTHON_LIBRARY}")
  192. if(PYTHON_MULTIARCH)
  193. set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
  194. else()
  195. set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
  196. endif()
  197. unset(PYTHON_LIBRARY)
  198. find_library(
  199. PYTHON_LIBRARY
  200. NAMES "python${PYTHON_LIBRARY_SUFFIX}"
  201. PATHS ${_PYTHON_LIBS_SEARCH}
  202. NO_DEFAULT_PATH)
  203. endif()
  204. # raise an error if the python libs are still not found.
  205. if(NOT EXISTS "${PYTHON_LIBRARY}")
  206. message(FATAL_ERROR "Python libraries not found")
  207. endif()
  208. else()
  209. if(PYTHON_MULTIARCH)
  210. set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
  211. else()
  212. set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
  213. endif()
  214. #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
  215. # Probably this needs to be more involved. It would be nice if the config
  216. # information the python interpreter itself gave us were more complete.
  217. find_library(
  218. PYTHON_LIBRARY
  219. NAMES "python${PYTHON_LIBRARY_SUFFIX}"
  220. PATHS ${_PYTHON_LIBS_SEARCH}
  221. NO_DEFAULT_PATH)
  222. # If all else fails, just set the name/version and let the linker figure out the path.
  223. if(NOT PYTHON_LIBRARY)
  224. set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
  225. endif()
  226. endif()
  227. mark_as_advanced(PYTHON_DEBUG_LIBRARY PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
  228. # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
  229. # cache entries because they are meant to specify the location of a single
  230. # library. We now set the variables listed by the documentation for this
  231. # module.
  232. set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
  233. set(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
  234. if(NOT PYTHON_DEBUG_LIBRARY)
  235. set(PYTHON_DEBUG_LIBRARY "")
  236. endif()
  237. set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  238. set(PYTHON_LIBRARY_DEBUG "${PYTHON_DEBUG_LIBRARY}")
  239. set(PYTHON_LIBRARY_RELEASE "${PYTHON_LIBRARY}")
  240. select_library_configurations(PYTHON)
  241. find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARY}"
  242. "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}")
  243. set(PYTHONLIBS_FOUND TRUE)
  244. set(PythonLibsNew_FOUND TRUE)
  245. if(NOT PYTHON_MODULE_PREFIX)
  246. set(PYTHON_MODULE_PREFIX "")
  247. endif()