FindPythonLibsNew.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. # Use the Python interpreter to find the libs.
  56. if(PythonLibsNew_FIND_REQUIRED)
  57. find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)
  58. else()
  59. find_package(PythonInterp ${PythonLibsNew_FIND_VERSION})
  60. endif()
  61. if(NOT PYTHONINTERP_FOUND)
  62. set(PYTHONLIBS_FOUND FALSE)
  63. return()
  64. endif()
  65. # According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
  66. # testing whether sys has the gettotalrefcount function is a reliable, cross-platform
  67. # way to detect a CPython debug interpreter.
  68. #
  69. # The library suffix is from the config var LDVERSION sometimes, otherwise
  70. # VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
  71. execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
  72. "from distutils import sysconfig as s;import sys;import struct;
  73. print('.'.join(str(v) for v in sys.version_info));
  74. print(sys.prefix);
  75. print(s.get_python_inc(plat_specific=True));
  76. print(s.get_python_lib(plat_specific=True));
  77. print(s.get_config_var('SO'));
  78. print(hasattr(sys, 'gettotalrefcount')+0);
  79. print(struct.calcsize('@P'));
  80. print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
  81. print(s.get_config_var('LIBDIR') or '');
  82. print(s.get_config_var('MULTIARCH') or '');
  83. "
  84. RESULT_VARIABLE _PYTHON_SUCCESS
  85. OUTPUT_VARIABLE _PYTHON_VALUES
  86. ERROR_VARIABLE _PYTHON_ERROR_VALUE)
  87. if(NOT _PYTHON_SUCCESS MATCHES 0)
  88. if(PythonLibsNew_FIND_REQUIRED)
  89. message(FATAL_ERROR
  90. "Python config failure:\n${_PYTHON_ERROR_VALUE}")
  91. endif()
  92. set(PYTHONLIBS_FOUND FALSE)
  93. return()
  94. endif()
  95. # Convert the process output into a list
  96. string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
  97. string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
  98. list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
  99. list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
  100. list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
  101. list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
  102. list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
  103. list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
  104. list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
  105. list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
  106. list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
  107. list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
  108. # Make sure the Python has the same pointer-size as the chosen compiler
  109. # Skip if CMAKE_SIZEOF_VOID_P is not defined
  110. if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
  111. if(PythonLibsNew_FIND_REQUIRED)
  112. math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
  113. math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
  114. message(FATAL_ERROR
  115. "Python config failure: Python is ${_PYTHON_BITS}-bit, "
  116. "chosen compiler is ${_CMAKE_BITS}-bit")
  117. endif()
  118. set(PYTHONLIBS_FOUND FALSE)
  119. return()
  120. endif()
  121. # The built-in FindPython didn't always give the version numbers
  122. string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
  123. list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
  124. list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
  125. list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
  126. # Make sure all directory separators are '/'
  127. string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX})
  128. string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
  129. string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES})
  130. if(CMAKE_HOST_WIN32)
  131. set(PYTHON_LIBRARY
  132. "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
  133. # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
  134. # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
  135. if(NOT EXISTS "${PYTHON_LIBRARY}")
  136. get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
  137. set(PYTHON_LIBRARY
  138. "${_PYTHON_ROOT}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
  139. endif()
  140. # raise an error if the python libs are still not found.
  141. if(NOT EXISTS "${PYTHON_LIBRARY}")
  142. #message(FATAL_ERROR "Python libraries not found")
  143. message(STATUS "Python libraries not found")
  144. endif()
  145. else()
  146. if(PYTHON_MULTIARCH)
  147. set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
  148. else()
  149. set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
  150. endif()
  151. #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
  152. # Probably this needs to be more involved. It would be nice if the config
  153. # information the python interpreter itself gave us were more complete.
  154. find_library(PYTHON_LIBRARY
  155. NAMES "python${PYTHON_LIBRARY_SUFFIX}"
  156. PATHS ${_PYTHON_LIBS_SEARCH}
  157. NO_DEFAULT_PATH)
  158. # If all else fails, just set the name/version and let the linker figure out the path.
  159. if(NOT PYTHON_LIBRARY)
  160. set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
  161. endif()
  162. endif()
  163. MARK_AS_ADVANCED(
  164. PYTHON_LIBRARY
  165. PYTHON_INCLUDE_DIR
  166. )
  167. # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
  168. # cache entries because they are meant to specify the location of a single
  169. # library. We now set the variables listed by the documentation for this
  170. # module.
  171. SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
  172. SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
  173. SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  174. find_package_message(PYTHON
  175. "Found PythonLibs: ${PYTHON_LIBRARY}"
  176. "${PYTHON_EXECUTABLE}${PYTHON_VERSION}")
  177. set(PYTHONLIBS_FOUND TRUE)