-
Notifications
You must be signed in to change notification settings - Fork 6
/
FindCython.cmake
133 lines (117 loc) · 4.47 KB
/
FindCython.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# initialization
#
# output variables Cython_FOUND, Cython_VERSION function cython_add_module
execute_process(
COMMAND ${Python3_EXECUTABLE} -m cython --version
OUTPUT_VARIABLE CYTHON_version_output
ERROR_VARIABLE CYTHON_version_error
RESULT_VARIABLE CYTHON_version_result
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)
message(STATUS "CYTHON_version_output=${CYTHON_version_output}")
message(STATUS "CYTHON_version_error=${CYTHON_version_error}")
message(STATUS "CYTHON_version_result=${CYTHON_version_result}")
if(NOT ${CYTHON_version_result} EQUAL 0)
# installation of cython, numpy
execute_process(
COMMAND ${Python3_EXECUTABLE} -m pip install cython numpy
OUTPUT_VARIABLE install_version_output
ERROR_VARIABLE install_version_error
RESULT_VARIABLE install_version_result)
message(STATUS "install_version_output=${install_version_output}")
message(STATUS "install_version_error=${install_version_error}")
message(STATUS "install_version_result=${install_version_result}")
execute_process(
COMMAND ${Python3_EXECUTABLE} -m cython --version
OUTPUT_VARIABLE CYTHON_version_output
ERROR_VARIABLE CYTHON_version_error
RESULT_VARIABLE CYTHON_version_result
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)
message(STATUS "CYTHON_version_output=${CYTHON_version_output}")
message(STATUS "CYTHON_version_error=${CYTHON_version_error}")
message(STATUS "CYTHON_version_result=${CYTHON_version_result}")
if(NOT ${CYTHON_version_result} EQUAL 0)
message(FATAL_ERROR("Unable to find cython for '${PYTHON_EXECUTABLE}'."))
endif()
set(Cython_VERSION ${CYTHON_version_error})
else()
set(Cython_VERSION ${CYTHON_version_error})
endif()
execute_process(
COMMAND "${Python3_EXECUTABLE}" -c "import numpy;print(numpy.get_include())"
OUTPUT_VARIABLE NUMPY_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE NUMPY_NOT_FOUND)
if(NUMPY_NOT_FOUND)
message(FATAL_ERROR
"Numpy headers not found with "
"Python3_EXECUTABLE='${Python3_EXECUTABLE}' and "
"Cython_VERSION=${Cython_VERSION}.")
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
Cython
VERSION_VAR Cython_VERSION
REQUIRED_VARS NUMPY_INCLUDE_DIR)
#
#! compile_cython : compile a pyx file into cpp
#
# \arg:filename extension name
# \arg:pyx_file_cpp output pyx file name
#
function(compile_cython filename pyx_file_cpp)
message(STATUS "cython cythonize '${filename}'")
set(fullfilename "${CMAKE_CURRENT_SOURCE_DIR}/${filename}")
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${pyx_file_cpp}
COMMAND
${Python3_EXECUTABLE} -m cython -3 --cplus ${fullfilename} -X
boundscheck=False -X cdivision=True -X wraparound=False -X
cdivision_warnings=False -X embedsignature=True -X initializedcheck=False
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${filename})
message(STATUS "cython cythonize '${filename}' - done")
endfunction()
#
#! cython_add_module : compile a pyx file into cpp
#
# \arg:name extension name
# \arg:pyx_file pyx file name
# \arg:omp_lib omp library to link with
# \argn: additional c++ files to compile
#
function(cython_add_module name pyx_file omp_lib)
set(options "")
set(oneValueArgs "")
set(multiValueArgs SOURCES DEPS)
message(STATUS "cython module '${name}': ${pyx_file} ++ ${ARGN}")
get_filename_component(pyx_dir ${pyx_file} DIRECTORY)
# cythonize
compile_cython(${pyx_file} ${pyx_dir}/${name}.cpp)
list(APPEND ARGN ${pyx_dir}/${name}.cpp)
# adding the library
message(STATUS "cython all files: ${ARGN}")
python3_add_library(${name} MODULE ${ARGN})
target_include_directories(
${name} PRIVATE
${Python3_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIR}
${Python3_NumPy_INCLUDE_DIRS}
${NUMPY_INCLUDE_DIR}
${OMP_INCLUDE_DIR})
message(STATUS " LINK ${name} <- ${Python3_LIBRARY_RELEASE} "
"${Python3_NumPy_LIBRARIES} ${omp_lib}")
target_link_libraries(
${name} PRIVATE
${Python3_LIBRARY_RELEASE} # use ${Python3_LIBRARIES} if python debug
${Python3_NumPy_LIBRARIES}
${omp_lib})
target_compile_definitions(${name} PUBLIC NPY_NO_DEPRECATED_API)
set_target_properties(
${name} PROPERTIES
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")
# install(TARGETS ${name} LIBRARY DESTINATION ${pyx_dir})
message(STATUS "cython added module '${name}'")
get_target_property(prop ${name} BINARY_DIR)
message(STATUS "cython added into '${prop}'.")
endfunction()