Debian Codegraph @

CMake/PythonSetup.cmake

raw
1 #########################################################################################
2 ### Find Python and set PYTHON_SITEDIR, the location to install python modules
3 # macro for running python and getting output
4 macro(run_python code result)
5 execute_process(
6 COMMAND ${PYTHON_EXECUTABLE} -c ${code}
7 OUTPUT_VARIABLE ${result}
8 RESULT_VARIABLE PY_ERR
9 OUTPUT_STRIP_TRAILING_WHITESPACE
10 )
11
12 if(PY_ERR)
13 message(STATUS "Error while querying python for information")
14 endif(PY_ERR)
15 endmacro(run_python)
16
17 # find the python interpreter
18 find_program(PYTHON_EXECUTABLE NAMES python3 python)
19
20 # get the python installation prefix and version
21 run_python("import sys\; print('%d' % (sys.version_info[0]))" PYTHON_VERSION_MAJOR)
22 run_python("import sys\; print('%d' % (sys.version_info[1]))" PYTHON_VERSION_MINOR)
23 set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
24 string(REPLACE "." "" _python_version_no_dots ${PYTHON_VERSION})
25
26 # determine the include directory
27 run_python("import sysconfig\; print(sysconfig.get_path('include'))" _python_include_hint)
28 run_python("import sysconfig\; print(sysconfig.get_config_var('LIBDIR'))" _python_lib_hint)
29 run_python("import sysconfig\; print(sysconfig.get_config_var('BINDIR'))" _python_prefix_hint)
30
31 find_path(
32 PYTHON_INCLUDE_DIR
33 Python.h
34 HINTS ${_python_include_hint}
35 NO_DEFAULT_PATH
36 )
37
38 # find the python library
39 # add a blank suffix to the beginning to find the Python framework
40 set(_old_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
41 set(CMAKE_FIND_LIBRARY_SUFFIXES ";${CMAKE_FIND_LIBRARY_SUFFIXES}")
42 find_library(
43 PYTHON_LIBRARY
44 NAMES
45 python${PYTHON_VERSION}
46 python${PYTHON_VERSION}m
47 python${_python_version_no_dots}
48 HINTS ${_python_lib_hint} ${_python_prefix_hint} ${_python_prefix_hint}/DLLs
49 PATH_SUFFIXES lib64 lib libs
50 NO_DEFAULT_PATH
51 )
52 set(${CMAKE_FIND_LIBRARY_SUFFIXES} _old_suffixes)
53
54 include(FindPackageHandleStandardArgs)
55 message(STATUS "Python library: ${PYTHON_LIBRARY}")
56 find_package_message(
57 python_library
58 "Found Python library: ${PYTHON_LIBRARY}"
59 "[${PYTHON_LIBRARY}]"
60 )
61 find_package_handle_standard_args(
62 Python
63 DEFAULT_MSG
64 PYTHON_EXECUTABLE
65 PYTHON_LIBRARY
66 PYTHON_INCLUDE_DIR
67 )
68
69 #### Setup numpy
70 if(PYTHON_VERSION VERSION_GREATER 3)
71 run_python("import numpy\; print(numpy.get_include())" NUMPY_INCLUDE_GUESS)
72 else()
73 run_python("import numpy\; print numpy.get_include()" NUMPY_INCLUDE_GUESS)
74 endif()
75
76 # We use the full path name (including numpy on the end), but
77 # Double-check that all is well with that choice.
78 find_path(NUMPY_INCLUDE_DIR numpy/arrayobject.h HINTS ${NUMPY_INCLUDE_GUESS})
79
80 find_package_handle_standard_args(numpy DEFAULT_MSG NUMPY_INCLUDE_DIR)
81
82 if(NUMPY_INCLUDE_DIR)
83 mark_as_advanced(NUMPY_INCLUDE_DIR)
84 endif(NUMPY_INCLUDE_DIR)
85
86 include_directories(${NUMPY_INCLUDE_DIR})
87 # add_definitions(-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
88
89 #############################################################################################
90 # Find cython
91 find_program(CYTHON_EXECUTABLE NAMES cython cython3)
92 find_package_handle_standard_args(cython DEFAULT_MSG CYTHON_EXECUTABLE)
93
94 if(NOT CYTHON_EXECUTABLE)
95 message(ERROR "cython not found")
96 endif()