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#============================================================================= 26# Copyright 2011 Blender Foundation. 27# 28# Distributed under the OSI-approved BSD 3-Clause License, 29# see accompanying file BSD-3-Clause-license.txt for details. 30#============================================================================= 31 32# If PYTHON_ROOT_DIR was defined in the environment, use it. 33IF(NOT PYTHON_ROOT_DIR AND NOT $ENV{PYTHON_ROOT_DIR} STREQUAL "") 34 SET(PYTHON_ROOT_DIR $ENV{PYTHON_ROOT_DIR}) 35ENDIF() 36 37SET(PYTHON_VERSION 3.8 CACHE STRING "Python Version (major and minor only)") 38MARK_AS_ADVANCED(PYTHON_VERSION) 39 40 41# See: http://docs.python.org/extending/embedding.html#linking-requirements 42# for why this is needed 43SET(PYTHON_LINKFLAGS "-Xlinker -export-dynamic" CACHE STRING "Linker flags for python") 44MARK_AS_ADVANCED(PYTHON_LINKFLAGS) 45 46 47# if the user passes these defines as args, we dont want to overwrite 48SET(_IS_INC_DEF OFF) 49SET(_IS_INC_CONF_DEF OFF) 50SET(_IS_LIB_DEF OFF) 51SET(_IS_LIB_PATH_DEF OFF) 52IF(DEFINED PYTHON_INCLUDE_DIR) 53 SET(_IS_INC_DEF ON) 54ENDIF() 55IF(DEFINED PYTHON_INCLUDE_CONFIG_DIR) 56 SET(_IS_INC_CONF_DEF ON) 57ENDIF() 58IF(DEFINED PYTHON_LIBRARY) 59 SET(_IS_LIB_DEF ON) 60ENDIF() 61IF(DEFINED PYTHON_LIBPATH) 62 SET(_IS_LIB_PATH_DEF ON) 63ENDIF() 64 65STRING(REPLACE "." "" _PYTHON_VERSION_NO_DOTS ${PYTHON_VERSION}) 66 67SET(_python_SEARCH_DIRS 68 ${PYTHON_ROOT_DIR} 69 "$ENV{HOME}/py${_PYTHON_VERSION_NO_DOTS}" 70 "/opt/lib/python-${PYTHON_VERSION}" 71) 72 73# only search for the dirs if we haven't already 74IF((NOT _IS_INC_DEF) OR (NOT _IS_INC_CONF_DEF) OR (NOT _IS_LIB_DEF) OR (NOT _IS_LIB_PATH_DEF)) 75 SET(_PYTHON_ABI_FLAGS_TEST 76 "m;mu;u; " # release 77 "dm;dmu;du;d" # debug 78 ) 79 80 FOREACH(_CURRENT_ABI_FLAGS ${_PYTHON_ABI_FLAGS_TEST}) 81 # IF(CMAKE_BUILD_TYPE STREQUAL Debug) 82 # SET(_CURRENT_ABI_FLAGS "d${_CURRENT_ABI_FLAGS}") 83 # ENDIF() 84 STRING(REPLACE " " "" _CURRENT_ABI_FLAGS ${_CURRENT_ABI_FLAGS}) 85 86 IF(NOT DEFINED PYTHON_INCLUDE_DIR) 87 FIND_PATH(PYTHON_INCLUDE_DIR 88 NAMES 89 Python.h 90 HINTS 91 ${_python_SEARCH_DIRS} 92 PATH_SUFFIXES 93 include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS} 94 include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS} 95 ) 96 ENDIF() 97 98 IF(NOT DEFINED PYTHON_INCLUDE_CONFIG_DIR) 99 FIND_PATH(PYTHON_INCLUDE_CONFIG_DIR 100 NAMES 101 pyconfig.h 102 HINTS 103 ${_python_SEARCH_DIRS} 104 PATH_SUFFIXES 105 include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS} 106 include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS} 107 ) 108 IF((NOT PYTHON_INCLUDE_CONFIG_DIR) AND PYTHON_INCLUDE_DIR) 109 # Fallback... 110 UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE) 111 SET(PYTHON_INCLUDE_CONFIG_DIR ${PYTHON_INCLUDE_DIR} CACHE PATH "") 112 ENDIF() 113 ENDIF() 114 115 IF(NOT DEFINED PYTHON_LIBRARY) 116 FIND_LIBRARY(PYTHON_LIBRARY 117 NAMES 118 "python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}" 119 HINTS 120 ${_python_SEARCH_DIRS} 121 PATH_SUFFIXES 122 lib64 lib 123 ) 124 ENDIF() 125 126 IF(NOT DEFINED PYTHON_LIBPATH) 127 FIND_PATH(PYTHON_LIBPATH 128 NAMES 129 "python${PYTHON_VERSION}/abc.py" # This is a bit hackish! :/ 130 HINTS 131 ${_python_SEARCH_DIRS} 132 PATH_SUFFIXES 133 lib64 lib 134 ) 135 IF((NOT PYTHON_LIBPATH) AND PYTHON_LIBRARY) 136 # Fallback... 137 UNSET(PYTHON_LIBPATH CACHE) 138 GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH) 139 ENDIF() 140 ENDIF() 141 142 IF(PYTHON_LIBRARY AND PYTHON_LIBPATH AND PYTHON_INCLUDE_DIR AND PYTHON_INCLUDE_CONFIG_DIR) 143 SET(_PYTHON_ABI_FLAGS "${_CURRENT_ABI_FLAGS}") 144 break() 145 ELSE() 146 # ensure we dont find values from 2 different ABI versions 147 IF(NOT _IS_INC_DEF) 148 UNSET(PYTHON_INCLUDE_DIR CACHE) 149 ENDIF() 150 IF(NOT _IS_INC_CONF_DEF) 151 UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE) 152 ENDIF() 153 IF(NOT _IS_LIB_DEF) 154 UNSET(PYTHON_LIBRARY CACHE) 155 ENDIF() 156 IF(NOT _IS_LIB_PATH_DEF) 157 UNSET(PYTHON_LIBPATH CACHE) 158 ENDIF() 159 ENDIF() 160 ENDFOREACH() 161 162 UNSET(_CURRENT_ABI_FLAGS) 163 UNSET(_CURRENT_PATH) 164 165 UNSET(_PYTHON_ABI_FLAGS_TEST) 166ENDIF() 167 168UNSET(_IS_INC_DEF) 169UNSET(_IS_INC_CONF_DEF) 170UNSET(_IS_LIB_DEF) 171UNSET(_IS_LIB_PATH_DEF) 172 173# handle the QUIETLY and REQUIRED arguments and SET PYTHONLIBSUNIX_FOUND to TRUE IF 174# all listed variables are TRUE 175INCLUDE(FindPackageHandleStandardArgs) 176FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibsUnix DEFAULT_MSG 177 PYTHON_LIBRARY PYTHON_LIBPATH PYTHON_INCLUDE_DIR PYTHON_INCLUDE_CONFIG_DIR) 178 179IF(PYTHONLIBSUNIX_FOUND) 180 # Assign cache items 181 SET(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR} ${PYTHON_INCLUDE_CONFIG_DIR}) 182 SET(PYTHON_LIBRARIES ${PYTHON_LIBRARY}) 183 184 FIND_FILE(PYTHON_SITE_PACKAGES 185 NAMES 186 # debian specific 187 dist-packages 188 site-packages 189 HINTS 190 ${PYTHON_LIBPATH}/python${PYTHON_VERSION} 191 ) 192 193 # we need this for installation 194 # XXX No more valid with debian-like py3.5 packages... 195# GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH) 196 197 # not required for build, just used when bundling Python. 198 FIND_PROGRAM( 199 PYTHON_EXECUTABLE 200 NAMES 201 "python${PYTHON_VERSION}${_PYTHON_ABI_FLAGS}" 202 "python${PYTHON_VERSION}" 203 "python" 204 HINTS 205 ${_python_SEARCH_DIRS} 206 PATH_SUFFIXES bin 207 ) 208ENDIF() 209 210UNSET(_PYTHON_VERSION_NO_DOTS) 211UNSET(_PYTHON_ABI_FLAGS) 212UNSET(_python_SEARCH_DIRS) 213 214MARK_AS_ADVANCED( 215 PYTHON_INCLUDE_DIR 216 PYTHON_INCLUDE_CONFIG_DIR 217 PYTHON_LIBRARY 218 PYTHON_LIBPATH 219 PYTHON_SITE_PACKAGES 220 PYTHON_EXECUTABLE 221) 222