Skip to content

Commit

Permalink
Dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
csbnw committed Aug 20, 2024
1 parent 56ec809 commit 0d3b328
Showing 1 changed file with 11 additions and 32 deletions.
43 changes: 11 additions & 32 deletions cmake/cudawrappers-helper.cmake
Original file line number Diff line number Diff line change
@@ -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 <helper.h>'
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 <kernel.cu> into target <example_program>, use
# target_embed_source(example_program, kernel.cu). This will expose symbols
Expand All @@ -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}
Expand Down

0 comments on commit 0d3b328

Please sign in to comment.