Initial implementation of a script for automatic dependencies installer/builder
[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     "/opt/lib/python-${PYTHON_VERSION}"
82   )
83
84   FOREACH(_CURRENT_ABI_FLAGS ${_python_ABI_FLAGS})
85     #IF(CMAKE_BUILD_TYPE STREQUAL Debug)
86     #  SET(_CURRENT_ABI_FLAGS "d${_CURRENT_ABI_FLAGS}")
87     #ENDIF()
88     STRING(REPLACE " " "" _CURRENT_ABI_FLAGS ${_CURRENT_ABI_FLAGS})
89
90     IF(NOT DEFINED PYTHON_INCLUDE_DIR)
91       FIND_PATH(PYTHON_INCLUDE_DIR
92         NAMES
93           Python.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     ENDIF()
101
102     IF(NOT DEFINED PYTHON_INCLUDE_CONFIG_DIR)
103       FIND_PATH(PYTHON_INCLUDE_CONFIG_DIR
104         NAMES
105           pyconfig.h
106         HINTS
107           ${_python_SEARCH_DIRS}
108         PATH_SUFFIXES
109           include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
110           include/${CMAKE_LIBRARY_ARCHITECTURE}/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
111       )
112       IF((NOT PYTHON_INCLUDE_CONFIG_DIR) AND PYTHON_INCLUDE_DIR)
113         # Fallback...
114         UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE)
115         SET(PYTHON_INCLUDE_CONFIG_DIR ${PYTHON_INCLUDE_DIR} CACHE PATH "")
116       ENDIF()
117     ENDIF()
118
119     IF(NOT DEFINED PYTHON_LIBRARY)
120       FIND_LIBRARY(PYTHON_LIBRARY
121         NAMES
122           "python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}"
123         HINTS
124           ${_python_SEARCH_DIRS}
125         PATH_SUFFIXES
126           lib64 lib
127       )
128     ENDIF()
129
130     IF(NOT DEFINED PYTHON_LIBPATH)
131       FIND_PATH(PYTHON_LIBPATH
132         NAMES
133           "python${PYTHON_VERSION}/abc.py" # This is a bit hackish! :/
134         HINTS
135           ${_python_SEARCH_DIRS}
136         PATH_SUFFIXES
137           lib64 lib
138       )
139       IF((NOT PYTHON_LIBPATH) AND PYTHON_LIBRARY)
140         # Fallback...
141         UNSET(PYTHON_LIBPATH CACHE)
142         GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH)
143       ENDIF()
144     ENDIF()
145
146     IF(PYTHON_LIBRARY AND PYTHON_LIBPATH AND PYTHON_INCLUDE_DIR AND PYTHON_INCLUDE_CONFIG_DIR)
147       break()
148     ELSE()
149       # ensure we dont find values from 2 different ABI versions
150       IF(NOT _IS_INC_DEF)
151         UNSET(PYTHON_INCLUDE_DIR CACHE)
152       ENDIF()
153       IF(NOT _IS_INC_CONF_DEF)
154         UNSET(PYTHON_INCLUDE_CONFIG_DIR CACHE)
155       ENDIF()
156       IF(NOT _IS_LIB_DEF)
157         UNSET(PYTHON_LIBRARY CACHE)
158       ENDIF()
159       IF(NOT _IS_LIB_PATH_DEF)
160         UNSET(PYTHON_LIBPATH CACHE)
161       ENDIF()
162     ENDIF()
163   ENDFOREACH()
164
165   UNSET(_CURRENT_ABI_FLAGS)
166   UNSET(_CURRENT_PATH)
167
168   UNSET(_python_ABI_FLAGS)
169   UNSET(_python_SEARCH_DIRS)
170 ENDIF()
171
172 UNSET(_IS_INC_DEF)
173 UNSET(_IS_INC_CONF_DEF)
174 UNSET(_IS_LIB_DEF)
175 UNSET(_IS_LIB_PATH_DEF)
176
177 # handle the QUIETLY and REQUIRED arguments and SET PYTHONLIBSUNIX_FOUND to TRUE IF 
178 # all listed variables are TRUE
179 INCLUDE(FindPackageHandleStandardArgs)
180 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibsUnix  DEFAULT_MSG
181     PYTHON_LIBRARY PYTHON_LIBPATH PYTHON_INCLUDE_DIR PYTHON_INCLUDE_CONFIG_DIR)
182
183 IF(PYTHONLIBSUNIX_FOUND)
184   # Assign cache items
185   SET(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR} ${PYTHON_INCLUDE_CONFIG_DIR})
186   SET(PYTHON_LIBRARIES ${PYTHON_LIBRARY})
187
188   # we need this for installation
189   # XXX No more valid with debian-like py3.3 packages...
190 #  GET_FILENAME_COMPONENT(PYTHON_LIBPATH ${PYTHON_LIBRARY} PATH)
191
192   # not used
193   # SET(PYTHON_BINARY ${PYTHON_EXECUTABLE} CACHE STRING "")
194 ENDIF()
195
196 MARK_AS_ADVANCED(
197   PYTHON_INCLUDE_DIR
198   PYTHON_INCLUDE_CONFIG_DIR
199   PYTHON_LIBRARY
200 )