7dcdc078c91fbe9c613a309148b56b39cd7ec5e2
[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.5 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 havn'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
81   SET(_python_ABI_FLAGS
82     "m;mu;u; "    # release
83     "dm;dmu;du;d" # debug
84   )
85
86
87
88   FOREACH(_CURRENT_ABI_FLAGS ${_python_ABI_FLAGS})
89     #IF(CMAKE_BUILD_TYPE STREQUAL Debug)
90     #  SET(_CURRENT_ABI_FLAGS "d${_CURRENT_ABI_FLAGS}")
91     #ENDIF()
92     STRING(REPLACE " " "" _CURRENT_ABI_FLAGS ${_CURRENT_ABI_FLAGS})
93
94     IF(NOT DEFINED PYTHON_INCLUDE_DIR)
95       FIND_PATH(PYTHON_INCLUDE_DIR
96         NAMES
97           Python.h
98         HINTS
99           ${_python_SEARCH_DIRS}
100         PATH_SUFFIXES
101           include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
102           include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
103       )
104     ENDIF()
105
106     IF(NOT DEFINED PYTHON_INCLUDE_CONFIG_DIR)
107       FIND_PATH(PYTHON_INCLUDE_CONFIG_DIR
108         NAMES
109           pyconfig.h
110         HINTS
111           ${_python_SEARCH_DIRS}
112         PATH_SUFFIXES
113           include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
114           include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
115       )
116       IF((NOT PYTHON_INCLUDE_CONFIG_DIR) AND PYTHON_INCLUDE_DIR)
117         # Fallback...
118         UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE)
119         SET(PYTHON_INCLUDE_CONFIG_DIR ${PYTHON_INCLUDE_DIR} CACHE PATH "")
120       ENDIF()
121     ENDIF()
122
123     IF(NOT DEFINED PYTHON_LIBRARY)
124       FIND_LIBRARY(PYTHON_LIBRARY
125         NAMES
126           "python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}"
127         HINTS
128           ${_python_SEARCH_DIRS}
129         PATH_SUFFIXES
130           lib64 lib
131       )
132     ENDIF()
133
134     IF(NOT DEFINED PYTHON_LIBPATH)
135       FIND_PATH(PYTHON_LIBPATH
136         NAMES
137           "python${PYTHON_VERSION}/abc.py" # This is a bit hackish! :/
138         HINTS
139           ${_python_SEARCH_DIRS}
140         PATH_SUFFIXES
141           lib64 lib
142       )
143       IF((NOT PYTHON_LIBPATH) AND PYTHON_LIBRARY)
144         # Fallback...
145         UNSET(PYTHON_LIBPATH CACHE)
146         GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH)
147       ENDIF()
148     ENDIF()
149
150     IF(PYTHON_LIBRARY AND PYTHON_LIBPATH AND PYTHON_INCLUDE_DIR AND PYTHON_INCLUDE_CONFIG_DIR)
151       SET(_PYTHON_ABI_FLAGS "${_CURRENT_ABI_FLAGS}")
152       break()
153     ELSE()
154       # ensure we dont find values from 2 different ABI versions
155       IF(NOT _IS_INC_DEF)
156         UNSET(PYTHON_INCLUDE_DIR CACHE)
157       ENDIF()
158       IF(NOT _IS_INC_CONF_DEF)
159         UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE)
160       ENDIF()
161       IF(NOT _IS_LIB_DEF)
162         UNSET(PYTHON_LIBRARY CACHE)
163       ENDIF()
164       IF(NOT _IS_LIB_PATH_DEF)
165         UNSET(PYTHON_LIBPATH CACHE)
166       ENDIF()
167     ENDIF()
168   ENDFOREACH()
169
170   UNSET(_CURRENT_ABI_FLAGS)
171   UNSET(_CURRENT_PATH)
172
173   UNSET(_python_ABI_FLAGS)
174 ENDIF()
175
176 UNSET(_IS_INC_DEF)
177 UNSET(_IS_INC_CONF_DEF)
178 UNSET(_IS_LIB_DEF)
179 UNSET(_IS_LIB_PATH_DEF)
180
181 # handle the QUIETLY and REQUIRED arguments and SET PYTHONLIBSUNIX_FOUND to TRUE IF 
182 # all listed variables are TRUE
183 INCLUDE(FindPackageHandleStandardArgs)
184 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibsUnix  DEFAULT_MSG
185     PYTHON_LIBRARY PYTHON_LIBPATH PYTHON_INCLUDE_DIR PYTHON_INCLUDE_CONFIG_DIR)
186
187 IF(PYTHONLIBSUNIX_FOUND)
188   # Assign cache items
189   SET(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR} ${PYTHON_INCLUDE_CONFIG_DIR})
190   SET(PYTHON_LIBRARIES ${PYTHON_LIBRARY})
191
192   FIND_FILE(PYTHON_SITE_PACKAGES
193     NAMES
194       # debian specific
195       dist-packages
196       site-packages
197     HINTS
198       ${PYTHON_LIBPATH}/python${PYTHON_VERSION}
199   )
200
201   # we need this for installation
202   # XXX No more valid with debian-like py3.4 packages...
203 #  GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH)
204
205   # not required for build, just used when bundling Python.
206   FIND_PROGRAM(
207     PYTHON_EXECUTABLE
208     NAMES
209       "python${PYTHON_VERSION}${_PYTHON_ABI_FLAGS}"
210       "python${PYTHON_VERSION}"
211       "python"
212     HINTS
213       ${_python_SEARCH_DIRS}
214     PATH_SUFFIXES bin
215   )
216 ENDIF()
217
218 UNSET(_PYTHON_VERSION_NO_DOTS)
219 UNSET(_PYTHON_ABI_FLAGS)
220 UNSET(_python_SEARCH_DIRS)
221
222 MARK_AS_ADVANCED(
223   PYTHON_INCLUDE_DIR
224   PYTHON_INCLUDE_CONFIG_DIR
225   PYTHON_LIBRARY
226   PYTHON_LIBPATH
227   PYTHON_SITE_PACKAGES
228   PYTHON_EXECUTABLE
229 )