Skip to content

Commit 490f199

Browse files
committed
wip(UseCython): Add support for cython depfile
1 parent cb61a04 commit 490f199

File tree

1 file changed

+7
-173
lines changed

1 file changed

+7
-173
lines changed

src/cython_cmake/cmake/UseCython.cmake

Lines changed: 7 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,13 @@ function(add_cython_target _name)
217217
${CMAKE_BINARY_DIR} ${generated_file})
218218

219219
set(comment "Generating ${_target_language} source ${generated_file_relative}")
220-
set(cython_include_directories "")
221-
set(c_header_dependencies "")
222-
223-
# Get the include directories.
224-
get_directory_property(cmake_include_directories
225-
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
226-
INCLUDE_DIRECTORIES)
227-
list(APPEND cython_include_directories ${cmake_include_directories})
228220

229221
get_source_file_property(pyx_location ${_source_file} LOCATION)
230222

231-
_extract_cython_pxd_dependencies(
232-
SOURCE_FILE ${_source_file}
233-
OUTPUT_VARIABLE pxd_dependencies
234-
)
223+
# Generated depfile is expected to have the ".dep" extension and be located along
224+
# side the generated source file.
225+
set(_depfile ${generated_file}.dep)
226+
set(_depfile_arg "-M")
235227

236228
# Set additional flags.
237229
set(annotate_arg "")
@@ -247,16 +239,6 @@ function(add_cython_target _name)
247239
set(line_directives_arg "--line-directives")
248240
endif()
249241

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-
260242
string(REGEX REPLACE " " ";" CYTHON_FLAGS_LIST "${CYTHON_FLAGS}")
261243

262244
# Add the command to run the compiler.
@@ -265,20 +247,18 @@ function(add_cython_target _name)
265247
COMMAND ${CYTHON_EXECUTABLE}
266248
ARGS
267249
${_target_language_arg}
268-
${include_directory_arg}
269250
${_language_level_arg}
270251
${annotate_arg}
271252
${cython_debug_arg}
272253
${line_directives_arg}
273254
${CYTHON_FLAGS_LIST}
255+
${_depfile_arg}
274256
${pyx_location}
275257
--output-file ${generated_file}
276258
DEPENDS
277259
${_source_file}
278-
${pxd_dependencies}
279-
IMPLICIT_DEPENDS
280-
${_target_language}
281-
${c_header_dependencies}
260+
DEPFILE
261+
${_cython_depfile}
282262
COMMENT ${comment}
283263
)
284264

@@ -299,149 +279,3 @@ function(add_cython_target _name)
299279
# add_custom_target(${_name} DEPENDS ${generated_file})
300280
endfunction()
301281

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-
317-
# Determine dependencies.
318-
# Add the pxd file with the same basename as the given pyx file.
319-
get_source_file_property(pyx_location ${_source_file} LOCATION)
320-
get_filename_component(pyx_path ${pyx_location} PATH)
321-
get_filename_component(pyx_file_basename ${_source_file} NAME_WE)
322-
unset(corresponding_pxd_file CACHE)
323-
find_file(corresponding_pxd_file ${pyx_file_basename}.pxd
324-
PATHS "${pyx_path}" ${cmake_include_directories}
325-
NO_DEFAULT_PATH)
326-
if(corresponding_pxd_file)
327-
message(STATUS "Found ${corresponding_pxd_file}")
328-
list(APPEND pxd_dependencies "${corresponding_pxd_file}")
329-
endif()
330-
331-
# pxd files to check for additional dependencies
332-
set(pxds_to_check "${_source_file}" "${pxd_dependencies}")
333-
set(pxds_checked "")
334-
set(number_pxds_to_check 1)
335-
while(number_pxds_to_check GREATER 0)
336-
foreach(pxd ${pxds_to_check})
337-
list(APPEND pxds_checked "${pxd}")
338-
list(REMOVE_ITEM pxds_to_check "${pxd}")
339-
340-
# look for C headers
341-
#
342-
# cdef extern from "spam.h"
343-
#
344-
file(STRINGS "${pxd}" extern_from_statements
345-
REGEX "cdef[ ]+extern[ ]+from.*$")
346-
foreach(statement ${extern_from_statements})
347-
# Had trouble getting the quote in the regex
348-
string(REGEX REPLACE
349-
"cdef[ ]+extern[ ]+from[ ]+[\"]([^\"]+)[\"].*" "\\1"
350-
header "${statement}")
351-
unset(header_location CACHE)
352-
find_file(header_location ${header} PATHS ${cmake_include_directories})
353-
if(header_location)
354-
list(FIND c_header_dependencies "${header_location}" header_idx)
355-
if(${header_idx} LESS 0)
356-
list(APPEND c_header_dependencies "${header_location}")
357-
endif()
358-
endif()
359-
endforeach()
360-
361-
# check for pxd dependencies
362-
# Look for cimport statements.
363-
#
364-
# cimport dishes
365-
# from dishes cimport spamdish
366-
#
367-
set(module_dependencies "")
368-
file(STRINGS "${pxd}" cimport_statements REGEX cimport)
369-
foreach(statement ${cimport_statements})
370-
if(${statement} MATCHES from)
371-
string(REGEX REPLACE
372-
"from[ ]+([^ ]+).*" "\\1"
373-
module "${statement}")
374-
else()
375-
string(REGEX REPLACE
376-
"cimport[ ]+([^ ]+).*" "\\1"
377-
module "${statement}")
378-
endif()
379-
list(APPEND module_dependencies ${module})
380-
endforeach()
381-
382-
# check for pxi dependencies
383-
# Look for include statements.
384-
#
385-
# include "spamstuff.pxi"
386-
#
387-
set(include_dependencies "")
388-
file(STRINGS "${pxd}" include_statements REGEX include)
389-
foreach(statement ${include_statements})
390-
string(REGEX REPLACE
391-
"include[ ]+[\"]([^\"]+)[\"].*" "\\1"
392-
module "${statement}")
393-
list(APPEND include_dependencies ${module})
394-
endforeach()
395-
396-
list(REMOVE_DUPLICATES module_dependencies)
397-
list(REMOVE_DUPLICATES include_dependencies)
398-
399-
# Add modules to the files to check, if appropriate.
400-
foreach(module ${module_dependencies})
401-
unset(pxd_location CACHE)
402-
find_file(pxd_location ${module}.pxd
403-
PATHS "${pyx_path}" ${cmake_include_directories}
404-
NO_DEFAULT_PATH)
405-
if(pxd_location)
406-
list(FIND pxds_checked ${pxd_location} pxd_idx)
407-
if(${pxd_idx} LESS 0)
408-
list(FIND pxds_to_check ${pxd_location} pxd_idx)
409-
if(${pxd_idx} LESS 0)
410-
list(APPEND pxds_to_check ${pxd_location})
411-
list(APPEND pxd_dependencies ${pxd_location})
412-
endif() # if it is not already going to be checked
413-
endif() # if it has not already been checked
414-
endif() # if pxd file can be found
415-
endforeach() # for each module dependency discovered
416-
417-
# Add includes to the files to check, if appropriate.
418-
foreach(_include ${include_dependencies})
419-
unset(pxi_location CACHE)
420-
find_file(pxi_location ${_include}
421-
PATHS "${pyx_path}" ${cmake_include_directories}
422-
NO_DEFAULT_PATH)
423-
if(pxi_location)
424-
list(FIND pxds_checked ${pxi_location} pxd_idx)
425-
if(${pxd_idx} LESS 0)
426-
list(FIND pxds_to_check ${pxi_location} pxd_idx)
427-
if(${pxd_idx} LESS 0)
428-
list(APPEND pxds_to_check ${pxi_location})
429-
list(APPEND pxd_dependencies ${pxi_location})
430-
endif() # if it is not already going to be checked
431-
endif() # if it has not already been checked
432-
endif() # if include file can be found
433-
endforeach() # for each include dependency discovered
434-
endforeach() # for each include file to check
435-
436-
list(LENGTH pxds_to_check number_pxds_to_check)
437-
endwhile()
438-
439-
list(REMOVE_DUPLICATES pxd_dependencies)
440-
441-
set(${OUTPUT_VARIABLE} ${pxd_dependencies} PARENT_SCOPE)
442-
443-
# Remove their visibility to the user.
444-
set(corresponding_pxd_file "" CACHE INTERNAL "")
445-
set(header_location "" CACHE INTERNAL "")
446-
set(pxd_location "" CACHE INTERNAL "")
447-
endfunction()

0 commit comments

Comments
 (0)