Skip to content

Commit cb61a04

Browse files
committed
wip(UseCython): Factor out pxd dependencies extraction
1 parent ddb362c commit cb61a04

File tree

1 file changed

+90
-64
lines changed

1 file changed

+90
-64
lines changed

src/cython_cmake/cmake/UseCython.cmake

Lines changed: 90 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ function(add_cython_target _name)
218218

219219
set(comment "Generating ${_target_language} source ${generated_file_relative}")
220220
set(cython_include_directories "")
221-
set(pxd_dependencies "")
222221
set(c_header_dependencies "")
223222

224223
# Get the include directories.
@@ -227,6 +226,94 @@ function(add_cython_target _name)
227226
INCLUDE_DIRECTORIES)
228227
list(APPEND cython_include_directories ${cmake_include_directories})
229228

229+
get_source_file_property(pyx_location ${_source_file} LOCATION)
230+
231+
_extract_cython_pxd_dependencies(
232+
SOURCE_FILE ${_source_file}
233+
OUTPUT_VARIABLE pxd_dependencies
234+
)
235+
236+
# Set additional flags.
237+
set(annotate_arg "")
238+
if(CYTHON_ANNOTATE)
239+
set(annotate_arg "--annotate")
240+
endif()
241+
242+
set(cython_debug_arg "")
243+
set(line_directives_arg "")
244+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR
245+
CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
246+
set(cython_debug_arg "--gdb")
247+
set(line_directives_arg "--line-directives")
248+
endif()
249+
250+
# Include directory arguments.
251+
list(REMOVE_DUPLICATES cython_include_directories)
252+
set(include_directory_arg "")
253+
foreach(_include_dir ${cython_include_directories})
254+
set(include_directory_arg
255+
${include_directory_arg} "--include-dir" "${_include_dir}")
256+
endforeach()
257+
258+
list(REMOVE_DUPLICATES c_header_dependencies)
259+
260+
string(REGEX REPLACE " " ";" CYTHON_FLAGS_LIST "${CYTHON_FLAGS}")
261+
262+
# Add the command to run the compiler.
263+
add_custom_command(
264+
OUTPUT ${generated_file}
265+
COMMAND ${CYTHON_EXECUTABLE}
266+
ARGS
267+
${_target_language_arg}
268+
${include_directory_arg}
269+
${_language_level_arg}
270+
${annotate_arg}
271+
${cython_debug_arg}
272+
${line_directives_arg}
273+
${CYTHON_FLAGS_LIST}
274+
${pyx_location}
275+
--output-file ${generated_file}
276+
DEPENDS
277+
${_source_file}
278+
${pxd_dependencies}
279+
IMPLICIT_DEPENDS
280+
${_target_language}
281+
${c_header_dependencies}
282+
COMMENT ${comment}
283+
)
284+
285+
# NOTE(opadron): I thought about making a proper target, but after trying it
286+
# out, I decided that it would be far too convenient to use the same name as
287+
# the target for the extension module (e.g.: for single-file modules):
288+
#
289+
# ...
290+
# add_cython_target(_module.pyx)
291+
# add_library(_module ${_module})
292+
# ...
293+
#
294+
# The above example would not be possible since the "_module" target name
295+
# would already be taken by the cython target. Since I can't think of a
296+
# reason why someone would need the custom target instead of just using the
297+
# generated file directly, I decided to leave this commented out.
298+
#
299+
# add_custom_target(${_name} DEPENDS ${generated_file})
300+
endfunction()
301+
302+
function(_extract_cython_pxd_dependencies)
303+
set(_options )
304+
set(_one_value SOURCE_FILE OUTPUT_VARIABLE)
305+
set(_multi_value )
306+
307+
cmake_parse_arguments(_args
308+
"${_options}"
309+
"${_one_value}"
310+
"${_multi_value}"
311+
${ARGN}
312+
)
313+
314+
set(_source_file ${_args_SOURCE_FILE})
315+
set(pxd_dependencies "")
316+
230317
# Determine dependencies.
231318
# Add the pxd file with the same basename as the given pyx file.
232319
get_source_file_property(pyx_location ${_source_file} LOCATION)
@@ -237,6 +324,7 @@ function(add_cython_target _name)
237324
PATHS "${pyx_path}" ${cmake_include_directories}
238325
NO_DEFAULT_PATH)
239326
if(corresponding_pxd_file)
327+
message(STATUS "Found ${corresponding_pxd_file}")
240328
list(APPEND pxd_dependencies "${corresponding_pxd_file}")
241329
endif()
242330

@@ -348,71 +436,9 @@ function(add_cython_target _name)
348436
list(LENGTH pxds_to_check number_pxds_to_check)
349437
endwhile()
350438

351-
# Set additional flags.
352-
set(annotate_arg "")
353-
if(CYTHON_ANNOTATE)
354-
set(annotate_arg "--annotate")
355-
endif()
356-
357-
set(cython_debug_arg "")
358-
set(line_directives_arg "")
359-
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR
360-
CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
361-
set(cython_debug_arg "--gdb")
362-
set(line_directives_arg "--line-directives")
363-
endif()
364-
365-
# Include directory arguments.
366-
list(REMOVE_DUPLICATES cython_include_directories)
367-
set(include_directory_arg "")
368-
foreach(_include_dir ${cython_include_directories})
369-
set(include_directory_arg
370-
${include_directory_arg} "--include-dir" "${_include_dir}")
371-
endforeach()
372-
373439
list(REMOVE_DUPLICATES pxd_dependencies)
374-
list(REMOVE_DUPLICATES c_header_dependencies)
375-
376-
string(REGEX REPLACE " " ";" CYTHON_FLAGS_LIST "${CYTHON_FLAGS}")
377-
378-
# Add the command to run the compiler.
379-
add_custom_command(
380-
OUTPUT ${generated_file}
381-
COMMAND ${CYTHON_EXECUTABLE}
382-
ARGS
383-
${_target_language_arg}
384-
${include_directory_arg}
385-
${_language_level_arg}
386-
${annotate_arg}
387-
${cython_debug_arg}
388-
${line_directives_arg}
389-
${CYTHON_FLAGS_LIST}
390-
${pyx_location}
391-
--output-file ${generated_file}
392-
DEPENDS
393-
${_source_file}
394-
${pxd_dependencies}
395-
IMPLICIT_DEPENDS
396-
${_target_language}
397-
${c_header_dependencies}
398-
COMMENT ${comment}
399-
)
400440

401-
# NOTE(opadron): I thought about making a proper target, but after trying it
402-
# out, I decided that it would be far too convenient to use the same name as
403-
# the target for the extension module (e.g.: for single-file modules):
404-
#
405-
# ...
406-
# add_cython_target(_module.pyx)
407-
# add_library(_module ${_module})
408-
# ...
409-
#
410-
# The above example would not be possible since the "_module" target name
411-
# would already be taken by the cython target. Since I can't think of a
412-
# reason why someone would need the custom target instead of just using the
413-
# generated file directly, I decided to leave this commented out.
414-
#
415-
# add_custom_target(${_name} DEPENDS ${generated_file})
441+
set(${OUTPUT_VARIABLE} ${pxd_dependencies} PARENT_SCOPE)
416442

417443
# Remove their visibility to the user.
418444
set(corresponding_pxd_file "" CACHE INTERNAL "")

0 commit comments

Comments
 (0)