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