diff --git a/cmake/cudawrappers-helper.cmake b/cmake/cudawrappers-helper.cmake index 4d4eaab..6ef8c34 100644 --- a/cmake/cudawrappers-helper.cmake +++ b/cmake/cudawrappers-helper.cmake @@ -1,34 +1,3 @@ -# Copy the contents of the input file to the output file with all the local -# includes inlined. Local includes are assumed to have ""'s, e.g. having a line -# '#include "helper.h"` will lead to `helper.h` being inlined. -function(inline_local_includes input_file output_file) - file(READ ${input_file} input_file_contents) - set(include_regex "(^|\r?\n)(#include[ \t]*\"([^\"]+)\")") - string(REGEX MATCHALL ${include_regex} includes ${input_file_contents}) - set(include_files "") - foreach(include ${includes}) - # Get the name of the file to include, e.g. 'helper.h' - string(REGEX REPLACE ${include_regex} "\\3" include_name ${include}) - # Get the complete line of the include, e.g. '#include ' - string(REGEX REPLACE ${include_regex} "\\2" include_line ${include}) - file(GLOB_RECURSE INCLUDE_PATHS "${PROJECT_SOURCE_DIR}/*/${include_name}") - if(NOT INCLUDE_PATHS STREQUAL "") - list(SORT INCLUDE_PATHS ORDER DESCENDING) - list(GET INCLUDE_PATHS 0 include_PATH) - list(APPEND include_files ${include_PATH}) - file(READ ${include_PATH} include_contents) - string(REPLACE "${include_line}" "${include_contents}" - input_file_contents "${input_file_contents}" - ) - endif() - endforeach() - file(WRITE ${output_file} "${input_file_contents}") - set(include_files - ${include_files} - PARENT_SCOPE - ) -endfunction() - # Make it possible to embed a source file in a library, and link it to a target. # E.g. to link into target , use # target_embed_source(example_program, kernel.cu). This will expose symbols @@ -43,7 +12,17 @@ function(target_embed_source target input_file) string(REPLACE "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" input_file_inlined ${input_file_absolute} ) - inline_local_includes(${input_file_absolute} ${input_file_inlined}) + + add_custom_command( + OUTPUT ${input_file_inlined} + COMMAND + ${CMAKE_COMMAND} -Dinput_file=${input_file_absolute} + -Doutput_file=${input_file_inlined} -P + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cudawrappers-inline-local-includes.cmake + DEPENDS ${input_file_absolute} + COMMENT "Inlining all includes of ${NAME} to generate ${input_file_inlined}" + ) + # Link the input_file into an object file string(REPLACE "${PROJECT_SOURCE_DIR}/" "" input_file_inlined_relative ${input_file_absolute}