1 # WRF Macro for copying files with generated dependency
2 # https://stackoverflow.com/a/34800230
3 macro( wrf_copy_source_files )
6 set( oneValueArgs TARGET_NAME SUFFIX PREFIX EXTENSION OUTPUT_DIR )
7 set( multiValueArgs DEPENDENCIES SOURCES )
11 "${options}" "${oneValueArgs}" "${multiValueArgs}"
15 # Generate compile command and file outputs
16 set( WRF_COPY_OUTPUT )
17 set( WRF_COPY_COMMANDS )
18 foreach( WRF_COPY_SOURCE_FILE ${WRF_COPY_SOURCES} )
19 get_filename_component( WRF_COPY_INPUT_SOURCE ${WRF_COPY_SOURCE_FILE} REALPATH )
20 get_filename_component( WRF_COPY_INPUT_SOURCE_FILE_ONLY ${WRF_COPY_SOURCE_FILE} NAME )
22 if ( ${WRF_COPY_EXTENSION} MATCHES "^[.][a-z0-9]+$" )
23 string( REGEX REPLACE "[.].*$" "${WRF_COPY_EXTENSION}" WRF_COPY_OUTPUT_FILE ${WRF_COPY_INPUT_SOURCE_FILE_ONLY} )
25 # Default to original filename
26 set( WRF_COPY_OUTPUT_FILE ${WRF_COPY_INPUT_SOURCE_FILE_ONLY} )
29 set( WRF_COPY_OUTPUT_FILE ${WRF_COPY_OUTPUT_DIR}/${WRF_COPY_PREFIX}${WRF_COPY_OUTPUT_FILE}${WRF_COPY_SUFFIX} )
33 APPEND WRF_COPY_COMMANDS
34 COMMAND ${CMAKE_COMMAND} -E copy ${WRF_COPY_INPUT_SOURCE} ${WRF_COPY_OUTPUT_FILE}
35 # Force check that they were made
36 COMMAND ${CMAKE_COMMAND} -E compare_files ${WRF_COPY_OUTPUT_FILE} ${WRF_COPY_OUTPUT_FILE}
39 APPEND WRF_COPY_OUTPUT
40 ${WRF_COPY_OUTPUT_FILE}
43 # # Tell all targets that eventually use this file that it is generated - this is useful if this macro is used in a
44 # # different directory than where the target dependency is set
45 # # Thanks to https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-add-a-dependency-to-a-source-file-which-is-generated-in-a-subdirectory
46 # # and https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/
47 # # It keeps getting better lol
48 # # https://gitlab.kitware.com/cmake/cmake/-/issues/18399
49 # # We could use cmake 3.20+ and CMP0118, but this allows usage from 3.18.6+
50 # TL;DR - This doesn't work despite all documentation stating otherwise, need to use CMP0118
51 # set_source_files_properties(
52 # ${WRF_COPY_OUTPUT_FILE}
53 # ${WRF_COPY_TARGET_DIRECTORY}
58 message( STATUS "File ${WRF_COPY_SOURCE_FILE} will be copied to ${WRF_COPY_OUTPUT_FILE}" )
62 # Preprocess sources into a custom target
64 OUTPUT ${WRF_COPY_OUTPUT}
65 COMMAND ${CMAKE_COMMAND} -E make_directory ${WRF_COPY_OUTPUT_DIR}
67 COMMENT "Preprocessing ${WRF_COPY_TARGET_NAME}"
68 DEPENDS ${WRF_COPY_DEPENDENCIES}
72 ${WRF_COPY_TARGET_NAME}
73 DEPENDS ${WRF_COPY_OUTPUT}