d825adbc49606f1650d4e95dd92bb40d341eaef5
[blender.git] / build_files / cmake / Modules / FindPythonLibsUnix.cmake
1 # - Find python libraries
2 #
3 #  PYTHON_VERSION
4 #  PYTHON_INCLUDE_DIRS
5 #  PYTHON_LIBRARIES
6 #  PYTHON_LINKFLAGS
7 #  PYTHON_ROOT_DIR, The base directory to search for Python.
8 #                   This can also be an environment variable.
9
10 #=============================================================================
11
12 # If PYTHON_ROOT_DIR was defined in the environment, use it.
13 if(NOT PYTHON_ROOT_DIR AND NOT $ENV{PYTHON_ROOT_DIR} STREQUAL "")
14         set(PYTHON_ROOT_DIR $ENV{PYTHON_ROOT_DIR})
15 endif()
16
17
18 set(PYTHON_VERSION 3.2 CACHE STRING "")
19 mark_as_advanced(PYTHON_VERSION)
20
21 set(PYTHON_LINKFLAGS "-Xlinker -export-dynamic")
22 mark_as_advanced(PYTHON_LINKFLAGS)
23
24 set(_python_ABI_FLAGS
25         "m;mu;u; "  # release
26         "md;mud;ud" # debug
27 )
28
29 string(REPLACE "." "" _PYTHON_VERSION_NO_DOTS ${PYTHON_VERSION})
30
31 set(_python_SEARCH_DIRS
32         ${PYTHON_ROOT_DIR}
33         "$ENV{HOME}/py${_PYTHON_VERSION_NO_DOTS}"
34         "/opt/py${_PYTHON_VERSION_NO_DOTS}"
35 )
36
37 foreach(_CURRENT_ABI_FLAGS ${_python_ABI_FLAGS})
38         #if(CMAKE_BUILD_TYPE STREQUAL Debug)
39         #       set(_CURRENT_ABI_FLAGS "d${_CURRENT_ABI_FLAGS}")
40         #endif()
41         string(REPLACE " " "" _CURRENT_ABI_FLAGS ${_CURRENT_ABI_FLAGS})
42
43         find_path(PYTHON_INCLUDE_DIR
44                 NAMES Python.h
45                 HINTS ${_python_SEARCH_DIRS}
46                 PATH_SUFFIXES include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}
47         )
48
49         find_library(PYTHON_LIBRARIES
50                 NAMES "python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}"
51                 HINTS ${_python_SEARCH_DIRS}
52                 PATH_SUFFIXES lib64 lib
53         )
54
55         if(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIR)
56                 break()
57         else()
58                 # ensure we dont find values from 2 different ABI versions
59                 unset(PYTHON_INCLUDE_DIR CACHE)
60                 unset(PYTHON_LIBRARIES CACHE)
61         endif()
62 endforeach()
63
64 unset(_CURRENT_ABI_FLAGS)
65 unset(_CURRENT_PATH)
66
67 unset(_python_ABI_FLAGS)
68 unset(_python_SEARCH_DIRS)
69
70 # handle the QUIETLY and REQUIRED arguments and set PYTHONLIBSUNIX_FOUND to TRUE if 
71 # all listed variables are TRUE
72 include(FindPackageHandleStandardArgs)
73 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibsUnix  DEFAULT_MSG
74     PYTHON_LIBRARIES PYTHON_INCLUDE_DIR)
75
76
77 if(PYTHONLIBSUNIX_FOUND)
78         # Assign cache items
79         set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR} CACHE STRING "")
80         set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE STRING "")
81         # not used
82         # set(PYTHON_BINARY ${PYTHON_EXECUTABLE} CACHE STRING "")
83
84         mark_as_advanced(
85                 PYTHON_INCLUDE_DIRS
86                 PYTHON_INCLUDE_DIR
87                 PYTHON_LIBRARIES
88         )
89 endif()