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