Debian Codegraph @

cmake/tribits/core/package_arch/TribitsCopyFilesToBinaryDir.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10
11 include(CMakeParseArguments)
12
13
14 # @FUNCTION: tribits_copy_files_to_binary_dir()
15 #
16 # Function that copies a list of files from a source directory to a
17 # destination directory at configure time, typically so that it can be used in
18 # one or more tests.
19 #
20 # Usage::
21 #
22 # tribits_copy_files_to_binary_dir(
23 # <targetName>
24 # [SOURCE_FILES <file1> <file2> ...]
25 # [SOURCE_DIR <sourceDir>]
26 # [DEST_FILES <dfile1> <dfile2> ...]
27 # [DEST_DIR <destDir>]
28 # [TARGETDEPS <targDep1> <targDep2> ...]
29 # [EXEDEPS <exeDep1> <exeDep2> ...]
30 # [NOEXEPREFIX]
31 # [CATEGORIES <category1> <category2> ...]
32 # )
33 #
34 # This sets up all of the custom CMake commands and targets to ensure that the
35 # files in the destination directory are always up to date just by building
36 # the ``ALL`` target.
37 #
38 # **NOTE:** The target name ``<targetName>`` must be unique from all other
39 # targets in the same TriBITS Package. Otherwise, one will get a configure
40 # failure complaining that a target name has already been defined. Therefore,
41 # be sure to pick long and unique target names!
42 #
43 # This function has a few valid calling modes:
44 #
45 # **1) Source files and destination files have the same name**::
46 #
47 # tribits_copy_files_to_binary_dir(
48 # <targetName>
49 # SOURCE_FILES <file1> <file2> ...
50 # [SOURCE_DIR <sourceDir>]
51 # [DEST_DIR <destDir>]
52 # [TARGETDEPS <targDep1> <targDep2> ...]
53 # [EXEDEPS <exeDep1> <exeDep2> ...]
54 # [NOEXEPREFIX]
55 # [CATEGORIES <category1> <category2> ...]
56 # )
57 #
58 # In this case, the names of the source files and the destination files
59 # are the same but just live in different directories.
60 #
61 # **2) Source files have a prefix different from the destination files**::
62 #
63 # tribits_copy_files_to_binary_dir(
64 # <targetName>
65 # DEST_FILES <file1> <file2> ...
66 # SOURCE_PREFIX <srcPrefix>
67 # [SOURCE_DIR <sourceDir>]
68 # [DEST_DIR <destDir>]
69 # [EXEDEPS <exeDep1> <exeDep2> ...]
70 # [NOEXEPREFIX]
71 # [CATEGORIES <category1> <category2> ...]
72 # )
73 #
74 # In this case, the source files have the same basic name as the destination
75 # files except they have the prefix ``<srcPrefix>`` prepended to the name.
76 #
77 # **3) Source files and destination files have completely different names**::
78 #
79 # tribits_copy_files_to_binary_dir(
80 # <targetName>
81 # SOURCE_FILES <sfile1> <sfile2> ...
82 # [SOURCE_DIR <sourceDir>]
83 # DEST_FILES <dfile1> <dfile2> ...
84 # [DEST_DIR <destDir>]
85 # [EXEDEPS <exeDep1> <exeDep2> ...]
86 # [NOEXEPREFIX]
87 # [CATEGORIES <category1> <category2> ...]
88 # )
89 #
90 # In this case, the source files and destination files have completely
91 # different prefixes.
92 #
93 # The individual arguments are:
94 #
95 # ``SOURCE_FILES <file1> <file2> ...``
96 #
97 # Listing of the source files relative to the source directory given by
98 # the argument ``SOURCE_DIR <sourceDir>``. If omitted, this list will be
99 # the same as ``DEST_FILES`` with the argument ``SOURCE_PREFIX
100 # <srcPrefix>`` appended.
101 #
102 # ``SOURCE_DIR <sourceDir>``
103 #
104 # Optional argument that gives the (absolute) base directory for all of
105 # the source files. If omitted, this takes the default value of
106 # ``${CMAKE_CURRENT_SOURCE_DIR}``.
107 #
108 # ``DEST_FILES <file1> <file2> ...``
109 #
110 # Listing of the destination files relative to the destination directory
111 # given by the argument ``DEST_DIR <destDir>``. If omitted, this list will
112 # be the same as given by the ``SOURCE_FILES`` list.
113 #
114 # ``DEST_DIR <destDir>``
115 #
116 # Optional argument that gives the (absolute) base directory for all of
117 # the destination files. If omitted, this takes the default value of
118 # ``${CMAKE_CURRENT_BINARY_DIR}``
119 #
120 # ``TARGETDEPS <targDep1> <targDep2> ...``
121 #
122 # Listing of general CMake targets that these files will be added as
123 # dependencies to. This results in the copies to be performed when any of
124 # the targets ``<targDepi>`` are built.
125 #
126 # ``EXEDEPS <exeDep1> <exeDep2> ...``
127 #
128 # Listing of executable targets that these files will be added as
129 # dependencies to. By default, the prefix ``${PACKAGE_NAME}_`` will is
130 # appended to the names of the targets. This ensures that if the
131 # executable target is built that these files will also be copied as well.
132 #
133 # ``NOEXEPREFIX``
134 #
135 # Option that determines if the prefix ``${PACKAGE_NAME}_`` will be
136 # appended to the arguments in the ``EXEDEPS`` list.
137 #
138 function(tribits_copy_files_to_binary_dir TARGET_NAME)
139
140 #
141 # A) Parse input arguments
142 #
143 cmake_parse_arguments(
144 #prefix
145 PARSE
146 #options
147 "NOEXEPREFIX"
148 #one_value_keywords
149 ""
150 #multi_value_keywords
151 "SOURCE_DIR;SOURCE_FILES;SOURCE_PREFIX;DEST_DIR;DEST_FILES;EXEDEPS;TARGETDEPS;CATEGORIES"
152 ${ARGN}
153 )
154
155 tribits_check_for_unparsed_arguments()
156
157 set(ADD_THE_TEST FALSE)
158 tribits_add_test_process_categories(ADD_THE_TEST)
159 if (NOT ADD_THE_TEST)
160 return()
161 endif()
162
163 if (NOT PARSE_SOURCE_DIR)
164 set(PARSE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
165 endif()
166
167 if (NOT PARSE_DEST_DIR)
168 set(PARSE_DEST_DIR ${CMAKE_CURRENT_BINARY_DIR})
169 endif()
170
171 set(RENAME 1)
172
173 if (PARSE_SOURCE_FILES AND NOT PARSE_DEST_FILES)
174 set(PARSE_DEST_FILES ${PARSE_SOURCE_FILES})
175 set(RENAME 0)
176 endif()
177
178 if (PARSE_DEST_FILES AND NOT PARSE_SOURCE_FILES)
179 set(PARSE_SOURCE_FILES ${PARSE_DEST_FILES})
180 set(RENAME 0)
181 endif()
182
183 #
184 # B) Validate arguments
185 #
186
187 list(LENGTH PARSE_SOURCE_DIR PARSE_SOURCE_DIR_LEN)
188 list(LENGTH PARSE_SOURCE_FILES PARSE_SOURCE_FILES_LEN)
189 list(LENGTH PARSE_SOURCE_PREFIX PARSE_SOURCE_PREFIX_LEN)
190 list(LENGTH PARSE_DEST_DIR PARSE_DEST_DIR_LEN)
191 list(LENGTH PARSE_DEST_FILES PARSE_DEST_FILES_LEN)
192
193 if (PARSE_SOURCE_DIR_LEN GREATER 1)
194 message(SEND_ERROR "Error, there can only be 0 or one SOURCE_DIR arguments!")
195 endif()
196
197 if (PARSE_DEST_DIR_LEN GREATER 1)
198 message(SEND_ERROR "Error, there can only be 0 or one DEST_DIR arguments!")
199 endif()
200
201 if (PARSE_SOURCE_PREFIX_LEN GREATER 1)
202 message(SEND_ERROR "Error, If SOURCE_PREFIX can only take one argument!")
203 endif()
204
205 if (PARSE_SOURCE_FILES_LEN EQUAL 0)
206 message(SEND_ERROR "Error, there are no source files listed!")
207 endif()
208
209 if (PARSE_DEST_FILES_LEN EQUAL 0)
210 message(SEND_ERROR "Error, there are no destination files listed!")
211 endif()
212
213 if (NOT PARSE_SOURCE_FILES_LEN EQUAL ${PARSE_DEST_FILES_LEN})
214 message(SEND_ERROR "Error, there are not the same number of source files ${PARSE_SOURCE_FILES_LEN} and dest files ${PARSE_DEST_FILES_LEN}!")
215 endif()
216
217 #
218 # C) Build the list of command and dependencies
219 #
220
221 set(DEST_FILES_LIST)
222 if(RENAME)
223 math(EXPR FILES_IDX_END "${PARSE_DEST_FILES_LEN}-1")
224
225 foreach(FILE_IDX RANGE ${FILES_IDX_END})
226
227 list(GET PARSE_SOURCE_FILES ${FILE_IDX} SOURCE_FILE)
228 set(SOURCE_FILE_FULL "${PARSE_SOURCE_DIR}/${PARSE_SOURCE_PREFIX}${SOURCE_FILE}")
229
230 list(GET PARSE_DEST_FILES ${FILE_IDX} DEST_FILE)
231 set(DEST_FILE_FULL "${PARSE_DEST_DIR}/${DEST_FILE}")
232
233 #print_var(SOURCE_FILE_FULL)
234 #print_var(DEST_FILE_FULL)
235
236 add_custom_command(
237 OUTPUT ${DEST_FILE_FULL}
238 DEPENDS ${SOURCE_FILE_FULL}
239 COMMAND ${CMAKE_COMMAND} ARGS -E copy ${SOURCE_FILE_FULL} ${DEST_FILE_FULL}
240 )
241
242 list(APPEND DEST_FILES_LIST "${DEST_FILE_FULL}")
243
244 endforeach()
245 else()
246 foreach(SOURCE_FILE ${PARSE_SOURCE_FILES})
247 set(DEST_FILE "${SOURCE_FILE}")
248
249 set(SOURCE_FILE_FULL "${PARSE_SOURCE_DIR}/${PARSE_SOURCE_PREFIX}${SOURCE_FILE}")
250 set(DEST_FILE_FULL "${PARSE_DEST_DIR}/${DEST_FILE}")
251
252 #print_var(SOURCE_FILE_FULL)
253 #print_var(DEST_FILE_FULL)
254
255 add_custom_command(
256 OUTPUT ${DEST_FILE_FULL}
257 DEPENDS ${SOURCE_FILE_FULL}
258 COMMAND ${CMAKE_COMMAND} ARGS -E copy ${SOURCE_FILE_FULL} ${DEST_FILE_FULL}
259 )
260
261 list(APPEND DEST_FILES_LIST "${DEST_FILE_FULL}")
262
263 endforeach()
264 endif()
265
266 #print_var(DEST_FILES_LIST)
267
268 if (PACKAGE_NAME AND NOT PARSE_NOEXEPREFIX)
269 set(PACKAGE_PREFIX "${PACKAGE_NAME}_")
270 set(TARGET_NAME "${PACKAGE_PREFIX}${TARGET_NAME}")
271 endif()
272
273 add_custom_target( ${TARGET_NAME} ALL
274 DEPENDS ${DEST_FILES_LIST}
275 )
276
277 if (PARSE_EXEDEPS)
278 foreach(EXEDEP ${PARSE_EXEDEPS})
279 add_dependencies(${PACKAGE_PREFIX}${EXEDEP} ${TARGET_NAME})
280 endforeach()
281 endif()
282
283 if (PARSE_TARGETDEPS)
284 foreach(TARGETDEP ${PARSE_TARGETDEPS})
285 add_dependencies(${TARGETDEP} ${TARGET_NAME})
286 endforeach()
287 endif()
288
289 endfunction()