FindPythonLibsUnix.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # - Find Python libraries
  2. # Find the native Python includes and library
  3. #
  4. # Note:, This is not _yet_ intended to be a general python module for other
  5. # projects to use since its hard coded to fixed Python version
  6. # as Blender only supports a single Python version at the moment.
  7. #
  8. # Note:
  9. # this is for Blender/Unix Python only.
  10. #
  11. # This module defines
  12. # PYTHON_VERSION
  13. # PYTHON_INCLUDE_DIRS
  14. # PYTHON_INCLUDE_CONFIG_DIRS
  15. # PYTHON_LIBRARIES
  16. # PYTHON_LIBPATH, Used for installation
  17. # PYTHON_SITE_PACKAGES, Used for installation (as a Python module)
  18. # PYTHON_LINKFLAGS
  19. # PYTHON_ROOT_DIR, The base directory to search for Python.
  20. # This can also be an environment variable.
  21. #
  22. # also defined, but not for general use are
  23. # PYTHON_LIBRARY, where to find the python library.
  24. #=============================================================================
  25. # Copyright 2011 Blender Foundation.
  26. #
  27. # Distributed under the OSI-approved BSD License (the "License");
  28. # see accompanying file Copyright.txt for details.
  29. #
  30. # This software is distributed WITHOUT ANY WARRANTY; without even the
  31. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  32. # See the License for more information.
  33. #=============================================================================
  34. # If PYTHON_ROOT_DIR was defined in the environment, use it.
  35. IF(NOT PYTHON_ROOT_DIR AND NOT $ENV{PYTHON_ROOT_DIR} STREQUAL "")
  36. SET(PYTHON_ROOT_DIR $ENV{PYTHON_ROOT_DIR})
  37. ENDIF()
  38. SET(PYTHON_VERSION 3.5 CACHE STRING "Python Version (major and minor only)")
  39. MARK_AS_ADVANCED(PYTHON_VERSION)
  40. # See: http://docs.python.org/extending/embedding.html#linking-requirements
  41. # for why this is needed
  42. SET(PYTHON_LINKFLAGS "-Xlinker -export-dynamic" CACHE STRING "Linker flags for python")
  43. MARK_AS_ADVANCED(PYTHON_LINKFLAGS)
  44. # if the user passes these defines as args, we dont want to overwrite
  45. SET(_IS_INC_DEF OFF)
  46. SET(_IS_INC_CONF_DEF OFF)
  47. SET(_IS_LIB_DEF OFF)
  48. SET(_IS_LIB_PATH_DEF OFF)
  49. IF(DEFINED PYTHON_INCLUDE_DIR)
  50. SET(_IS_INC_DEF ON)
  51. ENDIF()
  52. IF(DEFINED PYTHON_INCLUDE_CONFIG_DIR)
  53. SET(_IS_INC_CONF_DEF ON)
  54. ENDIF()
  55. IF(DEFINED PYTHON_LIBRARY)
  56. SET(_IS_LIB_DEF ON)
  57. ENDIF()
  58. IF(DEFINED PYTHON_LIBPATH)
  59. SET(_IS_LIB_PATH_DEF ON)
  60. ENDIF()
  61. STRING(REPLACE "." "" _PYTHON_VERSION_NO_DOTS ${PYTHON_VERSION})
  62. SET(_python_SEARCH_DIRS
  63. ${PYTHON_ROOT_DIR}
  64. "$ENV{HOME}/py${_PYTHON_VERSION_NO_DOTS}"
  65. "/opt/py${_PYTHON_VERSION_NO_DOTS}"
  66. "/opt/lib/python-${PYTHON_VERSION}"
  67. )
  68. # only search for the dirs if we havn't already
  69. IF((NOT _IS_INC_DEF) OR (NOT _IS_INC_CONF_DEF) OR (NOT _IS_LIB_DEF) OR (NOT _IS_LIB_PATH_DEF))
  70. SET(_python_ABI_FLAGS
  71. "m;mu;u; " # release
  72. "dm;dmu;du;d" # debug
  73. )
  74. FOREACH(_CURRENT_ABI_FLAGS ${_python_ABI_FLAGS})
  75. #IF(CMAKE_BUILD_TYPE STREQUAL Debug)
  76. # SET(_CURRENT_ABI_FLAGS "d${_CURRENT_ABI_FLAGS}")
  77. #ENDIF()
  78. STRING(REPLACE " " "" _CURRENT_ABI_FLAGS ${_CURRENT_ABI_FLAGS})
  79. IF(NOT DEFINED PYTHON_INCLUDE_DIR)
  80. FIND_PATH(PYTHON_INCLUDE_DIR
  81. NAMES
  82. Python.h
  83. HINTS
  84. ${_python_SEARCH_DIRS}
  85. PATH_SUFFIXES
  86. include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
  87. include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
  88. )
  89. ENDIF()
  90. IF(NOT DEFINED PYTHON_INCLUDE_CONFIG_DIR)
  91. FIND_PATH(PYTHON_INCLUDE_CONFIG_DIR
  92. NAMES
  93. pyconfig.h
  94. HINTS
  95. ${_python_SEARCH_DIRS}
  96. PATH_SUFFIXES
  97. include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
  98. include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
  99. )
  100. IF((NOT PYTHON_INCLUDE_CONFIG_DIR) AND PYTHON_INCLUDE_DIR)
  101. # Fallback...
  102. UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE)
  103. SET(PYTHON_INCLUDE_CONFIG_DIR ${PYTHON_INCLUDE_DIR} CACHE PATH "")
  104. ENDIF()
  105. ENDIF()
  106. IF(NOT DEFINED PYTHON_LIBRARY)
  107. FIND_LIBRARY(PYTHON_LIBRARY
  108. NAMES
  109. "python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}"
  110. HINTS
  111. ${_python_SEARCH_DIRS}
  112. PATH_SUFFIXES
  113. lib64 lib
  114. )
  115. ENDIF()
  116. IF(NOT DEFINED PYTHON_LIBPATH)
  117. FIND_PATH(PYTHON_LIBPATH
  118. NAMES
  119. "python${PYTHON_VERSION}/abc.py" # This is a bit hackish! :/
  120. HINTS
  121. ${_python_SEARCH_DIRS}
  122. PATH_SUFFIXES
  123. lib64 lib
  124. )
  125. IF((NOT PYTHON_LIBPATH) AND PYTHON_LIBRARY)
  126. # Fallback...
  127. UNSET(PYTHON_LIBPATH CACHE)
  128. GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH)
  129. ENDIF()
  130. ENDIF()
  131. IF(PYTHON_LIBRARY AND PYTHON_LIBPATH AND PYTHON_INCLUDE_DIR AND PYTHON_INCLUDE_CONFIG_DIR)
  132. SET(_PYTHON_ABI_FLAGS "${_CURRENT_ABI_FLAGS}")
  133. break()
  134. ELSE()
  135. # ensure we dont find values from 2 different ABI versions
  136. IF(NOT _IS_INC_DEF)
  137. UNSET(PYTHON_INCLUDE_DIR CACHE)
  138. ENDIF()
  139. IF(NOT _IS_INC_CONF_DEF)
  140. UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE)
  141. ENDIF()
  142. IF(NOT _IS_LIB_DEF)
  143. UNSET(PYTHON_LIBRARY CACHE)
  144. ENDIF()
  145. IF(NOT _IS_LIB_PATH_DEF)
  146. UNSET(PYTHON_LIBPATH CACHE)
  147. ENDIF()
  148. ENDIF()
  149. ENDFOREACH()
  150. UNSET(_CURRENT_ABI_FLAGS)
  151. UNSET(_CURRENT_PATH)
  152. UNSET(_python_ABI_FLAGS)
  153. ENDIF()
  154. UNSET(_IS_INC_DEF)
  155. UNSET(_IS_INC_CONF_DEF)
  156. UNSET(_IS_LIB_DEF)
  157. UNSET(_IS_LIB_PATH_DEF)
  158. # handle the QUIETLY and REQUIRED arguments and SET PYTHONLIBSUNIX_FOUND to TRUE IF
  159. # all listed variables are TRUE
  160. INCLUDE(FindPackageHandleStandardArgs)
  161. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibsUnix DEFAULT_MSG
  162. PYTHON_LIBRARY PYTHON_LIBPATH PYTHON_INCLUDE_DIR PYTHON_INCLUDE_CONFIG_DIR)
  163. IF(PYTHONLIBSUNIX_FOUND)
  164. # Assign cache items
  165. SET(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR} ${PYTHON_INCLUDE_CONFIG_DIR})
  166. SET(PYTHON_LIBRARIES ${PYTHON_LIBRARY})
  167. FIND_FILE(PYTHON_SITE_PACKAGES
  168. NAMES
  169. # debian specific
  170. dist-packages
  171. site-packages
  172. HINTS
  173. ${PYTHON_LIBPATH}/python${PYTHON_VERSION}
  174. )
  175. # we need this for installation
  176. # XXX No more valid with debian-like py3.5 packages...
  177. # GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH)
  178. # not required for build, just used when bundling Python.
  179. FIND_PROGRAM(
  180. PYTHON_EXECUTABLE
  181. NAMES
  182. "python${PYTHON_VERSION}${_PYTHON_ABI_FLAGS}"
  183. "python${PYTHON_VERSION}"
  184. "python"
  185. HINTS
  186. ${_python_SEARCH_DIRS}
  187. PATH_SUFFIXES bin
  188. )
  189. ENDIF()
  190. UNSET(_PYTHON_VERSION_NO_DOTS)
  191. UNSET(_PYTHON_ABI_FLAGS)
  192. UNSET(_python_SEARCH_DIRS)
  193. MARK_AS_ADVANCED(
  194. PYTHON_INCLUDE_DIR
  195. PYTHON_INCLUDE_CONFIG_DIR
  196. PYTHON_LIBRARY
  197. PYTHON_LIBPATH
  198. PYTHON_SITE_PACKAGES
  199. PYTHON_EXECUTABLE
  200. )