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