CMake netCDF Compatibility with WPS (#2121)
[WRF.git] / cmake / target_copy.cmake
blob429eddc976c55500fbe0f48720f88f8be8c7b719
1 # WRF Macro for copying files with generated dependency
2 # https://stackoverflow.com/a/34800230
3 macro( wrf_copy_source_files )
5   set( options        )
6   set( oneValueArgs   TARGET_NAME SUFFIX PREFIX EXTENSION OUTPUT_DIR )
7   set( multiValueArgs DEPENDENCIES SOURCES )
9   cmake_parse_arguments(
10                         WRF_COPY
11                         "${options}"  "${oneValueArgs}"  "${multiValueArgs}"
12                         ${ARGN}
13                         )
14   
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     )
21     
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} )
24     else()
25       # Default to original filename
26       set( WRF_COPY_OUTPUT_FILE ${WRF_COPY_INPUT_SOURCE_FILE_ONLY} )
27     endif()
29     set( WRF_COPY_OUTPUT_FILE ${WRF_COPY_OUTPUT_DIR}/${WRF_COPY_PREFIX}${WRF_COPY_OUTPUT_FILE}${WRF_COPY_SUFFIX} )
30     
32     list( 
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}
37           )
38     list( 
39           APPEND WRF_COPY_OUTPUT
40           ${WRF_COPY_OUTPUT_FILE}
41           )
42     
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}
54     #                             PROPERTIES
55     #                               GENERATED TRUE
56     #                             )
58     message( STATUS "File ${WRF_COPY_SOURCE_FILE} will be copied to ${WRF_COPY_OUTPUT_FILE}" )
60   endforeach()
62   # Preprocess sources into a custom target
63   add_custom_command(
64                       OUTPUT ${WRF_COPY_OUTPUT}
65                       COMMAND ${CMAKE_COMMAND} -E  make_directory ${WRF_COPY_OUTPUT_DIR}
66                       ${WRF_COPY_COMMANDS}
67                       COMMENT "Preprocessing ${WRF_COPY_TARGET_NAME}"
68                       DEPENDS ${WRF_COPY_DEPENDENCIES}
69                       )
71   add_custom_target(
72                     ${WRF_COPY_TARGET_NAME}
73                     DEPENDS ${WRF_COPY_OUTPUT}
74                     )
75 endmacro()