|
57 | 57 | # ) |
58 | 58 | # |
59 | 59 | # |
60 | | -# .. cmake:command:: add_cython_target |
61 | | -# |
62 | | -# Create a custom rule to generate the source code for a Python extension module |
63 | | -# using cython. |
64 | | -# |
65 | | -# add_cython_target(<Name> [<CythonInput>] |
66 | | -# [C | CXX] |
67 | | -# [PY2 | PY3] |
68 | | -# [OUTPUT_VAR <OutputVar>]) |
69 | | -# |
70 | | -# ``<Name>`` is the name of the new target, and ``<CythonInput>`` |
71 | | -# is the path to a cython source file. Note that, despite the name, no new |
72 | | -# targets are created by this function. Instead, see ``OUTPUT_VAR`` for |
73 | | -# retrieving the path to the generated source for subsequent targets. |
74 | | -# |
75 | | -# If only ``<Name>`` is provided, and it ends in the ".pyx" extension, then it |
76 | | -# is assumed to be the ``<CythonInput>``. The name of the input without the |
77 | | -# extension is used as the target name. If only ``<Name>`` is provided, and it |
78 | | -# does not end in the ".pyx" extension, then the ``<CythonInput>`` is assumed to |
79 | | -# be ``<Name>.pyx``. |
80 | | -# |
81 | | -# The Cython include search path is amended with any entries found in the |
82 | | -# ``INCLUDE_DIRECTORIES`` property of the directory containing the |
83 | | -# ``<CythonInput>`` file. Use ``include_directories`` to add to the Cython |
84 | | -# include search path. |
85 | | -# |
86 | | -# Options: |
87 | | -# |
88 | | -# ``C | CXX`` |
89 | | -# Force the generation of either a C or C++ file. By default, a C file is |
90 | | -# generated, unless the C language is not enabled for the project; in this |
91 | | -# case, a C++ file is generated by default. |
92 | | -# |
93 | | -# ``PY2 | PY3`` |
94 | | -# Force compilation using either Python-2 or Python-3 syntax and code |
95 | | -# semantics. By default, Python-2 syntax and semantics are used if the major |
96 | | -# version of Python found is 2. Otherwise, Python-3 syntax and semantics are |
97 | | -# used. |
98 | | -# |
99 | | -# ``OUTPUT_VAR <OutputVar>`` |
100 | | -# Set the variable ``<OutputVar>`` in the parent scope to the path to the |
101 | | -# generated source file. By default, ``<Name>`` is used as the output |
102 | | -# variable name. |
103 | | -# |
104 | | -# Defined variables: |
105 | | -# |
106 | | -# ``<OutputVar>`` |
107 | | -# The path of the generated source file. |
108 | | -# |
109 | | -# Cache variables that affect the behavior include: |
110 | | -# |
111 | | -# ``CYTHON_ANNOTATE`` |
112 | | -# Whether to create an annotated .html file when compiling. |
113 | | -# |
114 | | -# ``CYTHON_FLAGS`` |
115 | | -# Additional flags to pass to the Cython compiler. |
116 | | -# |
117 | | -# Usage example: |
118 | | -# |
119 | | -# .. code-block:: cmake |
120 | | -# |
121 | | -# find_package(Cython) |
122 | | -# |
123 | | -# # Note: In this case, either one of these arguments may be omitted; their |
124 | | -# # value would have been inferred from that of the other. |
125 | | -# add_cython_target(cy_code cy_code.pyx) |
126 | | -# |
127 | | -# add_library(cy_code MODULE ${cy_code}) |
128 | | -# target_link_libraries(cy_code ...) |
129 | | -# |
130 | 60 | #============================================================================= |
131 | 61 | # Copyright 2011 Kitware, Inc. |
132 | 62 | # |
|
144 | 74 | #============================================================================= |
145 | 75 |
|
146 | 76 |
|
147 | | -function(add_cython_target _name) |
148 | | - set(_options C CXX PY2 PY3) |
149 | | - set(_one_value OUTPUT_VAR) |
150 | | - set(_multi_value ) |
151 | | - |
152 | | - cmake_parse_arguments(_args |
153 | | - "${_options}" |
154 | | - "${_one_value}" |
155 | | - "${_multi_value}" |
156 | | - ${ARGN} |
157 | | - ) |
158 | | - |
159 | | - # Configuration options. |
160 | | - set(CYTHON_ANNOTATE OFF |
161 | | - CACHE BOOL "Create an annotated .html file when compiling *.pyx.") |
162 | | - |
163 | | - set(CYTHON_FLAGS "" CACHE STRING |
164 | | - "Extra flags to the cython compiler.") |
165 | | - mark_as_advanced(CYTHON_ANNOTATE CYTHON_FLAGS) |
166 | | - |
167 | | - if(_args_C) |
168 | | - set(_language "C") |
169 | | - endif() |
170 | | - if(_args_CXX) |
171 | | - set(_language "CXX") |
172 | | - endif() |
173 | | - |
174 | | - if(_args_PY2) |
175 | | - set(_language_level "2") |
176 | | - endif() |
177 | | - if(_args_PY3) |
178 | | - set(_language_level "3") |
179 | | - endif() |
180 | | - |
181 | | - list(GET _args_UNPARSED_ARGUMENTS 0 _arg0) |
182 | | - |
183 | | - # if provided, use _arg0 as the input file path |
184 | | - if(_arg0) |
185 | | - set(_source_file ${_arg0}) |
186 | | - |
187 | | - # otherwise, must determine source file from name, or vice versa |
188 | | - else() |
189 | | - get_filename_component(_name_ext "${_name}" EXT) |
190 | | - |
191 | | - # if extension provided, _name is the source file |
192 | | - if(_name_ext) |
193 | | - set(_source_file ${_name}) |
194 | | - get_filename_component(_name "${_source_file}" NAME_WE) |
195 | | - |
196 | | - # otherwise, assume the source file is ${_name}.pyx |
197 | | - else() |
198 | | - set(_source_file ${_name}.pyx) |
199 | | - endif() |
200 | | - endif() |
201 | | - |
202 | | - # Set additional flags. |
203 | | - set(_cython_args) |
204 | | - if(CYTHON_ANNOTATE) |
205 | | - list(APPEND _cython_args "--annotate") |
206 | | - endif() |
207 | | - if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR |
208 | | - CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") |
209 | | - list(APPEND _cython_args |
210 | | - "--gdb" |
211 | | - "--line-directives" |
212 | | - ) |
213 | | - endif() |
214 | | - string(STRIP "${CYTHON_FLAGS}" _stripped_cython_flags) |
215 | | - if(_stripped_cython_flags) |
216 | | - string(REGEX REPLACE " " ";" CYTHON_FLAGS_LIST "${_stripped_cython_flags}") |
217 | | - list(APPEND _cython_args ${CYTHON_FLAGS_LIST}) |
218 | | - endif() |
219 | | - |
220 | | - Cython_compile_pyx( |
221 | | - LANGUAGE ${_language} |
222 | | - LANGUAGE_LEVEL ${_language_level} |
223 | | - CYTHON_ARGS ${_cython_args} |
224 | | - OUTPUT_VARIABLE ${_args_OUTPUT_VAR} |
225 | | - ${_source_file} |
226 | | - ) |
227 | | - |
228 | | - if(_args_OUTPUT_VAR) |
229 | | - set(${_args_OUTPUT_VAR} ${${_args_OUTPUT_VAR}} PARENT_SCOPE) |
230 | | - endif() |
231 | | -endfunction() |
232 | | - |
233 | 77 | function(Cython_compile_pyx) |
234 | 78 | set(_options ) |
235 | 79 | set(_one_value LANGUAGE_LEVEL LANGUAGE OUTPUT_VARIABLE) |
|
0 commit comments