blob: 76263c2d3d416a3476f0da3914700467b74ff17a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#
# Wrapping
#
PROJECT (CustomCommand)
ADD_SUBDIRECTORY(GeneratedHeader)
#
# Lib and exe path
#
SET (LIBRARY_OUTPUT_PATH
${PROJECT_BINARY_DIR}/bin/ CACHE INTERNAL
"Single output directory for building all libraries.")
SET (EXECUTABLE_OUTPUT_PATH
${PROJECT_BINARY_DIR}/bin/ CACHE INTERNAL
"Single output directory for building all executables.")
################################################################
#
# First test using a compiled generator to create a .c file
#
################################################################
# add the executable that will generate the file
ADD_EXECUTABLE(generator generator.cxx)
GET_TARGET_PROPERTY(generator_PATH generator LOCATION)
MESSAGE("Location ${generator_PATH}")
# the folowing assumes that a cmSourceFile
# is instantiated for the output, with GENERATED 1
# at the end of the day this becomes a what in VS ?
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/generated.c
DEPENDS generator
COMMAND ${generator_PATH}
ARGS ${PROJECT_BINARY_DIR}/generated.c
)
################################################################
#
# Test using a wrapper to wrap a header file
#
################################################################
# add the executable that will generate the file
ADD_EXECUTABLE(wrapper wrapper.cxx)
# the following assumes that a cmSourceFile
# is instantiated for the output, with GENERATED 1
# at the end of the day this becomes a what in VS ?
ADD_CUSTOM_COMMAND(
OUTPUT ${PROJECT_BINARY_DIR}/wrapped.c ${PROJECT_BINARY_DIR}/wrapped_help.c
DEPENDS wrapper
MAIN_DEPENDENCY ${PROJECT_SOURCE_DIR}/wrapped.h
COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/wrapper
${PROJECT_BINARY_DIR}/wrapped.c ${PROJECT_BINARY_DIR}/wrapped_help.c
)
################################################################
#
# Test creating files from a custom target
#
################################################################
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/doc1.dvi
DEPENDS ${PROJECT_SOURCE_DIR}/doc1.tex
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${PROJECT_SOURCE_DIR}/doc1.tex
${PROJECT_BINARY_DIR}/doc1.dvi
)
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/doc1.h
COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1.dvi to doc1temp.h."
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1.dvi
${PROJECT_BINARY_DIR}/doc1temp.h
)
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/doc1.h APPEND
DEPENDS ${PROJECT_BINARY_DIR}/doc1.dvi
COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1temp.h to doc1.h."
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1temp.h
${PROJECT_BINARY_DIR}/doc1.h
COMMAND ${CMAKE_COMMAND} -E echo " Removing doc1temp.h."
COMMAND ${CMAKE_COMMAND} -E remove -f ${PROJECT_BINARY_DIR}/doc1temp.h
)
# Add custom command to generate foo.h.
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.h
DEPENDS ${PROJECT_SOURCE_DIR}/foo.h.in
COMMAND ${CMAKE_COMMAND} -E echo " Copying foo.h.in to foo.h."
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/foo.h.in
${PROJECT_BINARY_DIR}/foo.h
)
# Add the location of foo.h to the include path.
INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR})
# Add a custom target to drive generation of doc1.h.
ADD_CUSTOM_TARGET(TDocument ALL
COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1.h to doc2.h."
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1.h
${PROJECT_BINARY_DIR}/doc2.h
DEPENDS ${PROJECT_BINARY_DIR}/doc1.h
COMMENT "Running top-level TDocument commands"
)
# Setup a pre- and post-build pair that will fail if not run in the
# proper order.
ADD_CUSTOM_COMMAND(
TARGET TDocument PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo " Writing doc1pre.txt."
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/doc1.tex ${PROJECT_BINARY_DIR}/doc1pre.txt
)
ADD_CUSTOM_COMMAND(
TARGET TDocument POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1pre.txt to doc2post.txt."
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1pre.txt
${PROJECT_BINARY_DIR}/doc2post.txt
)
################################################################
#
# Test using a multistep generated file
#
################################################################
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.pre
DEPENDS ${PROJECT_SOURCE_DIR}/foo.in
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${PROJECT_SOURCE_DIR}/foo.in
${PROJECT_BINARY_DIR}/foo.pre
)
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.c
DEPENDS ${PROJECT_BINARY_DIR}/foo.pre
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${PROJECT_BINARY_DIR}/foo.pre
${PROJECT_BINARY_DIR}/foo.c
)
# Add custom command to generate not_included.h, which is a header
# file that is not included by any source in this project. This will
# test whether all custom command outputs explicitly listed as sources
# get generated even if they are not needed by an object file.
ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/not_included.h
DEPENDS ${PROJECT_SOURCE_DIR}/foo.h.in
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/foo.h.in
${PROJECT_BINARY_DIR}/not_included.h
)
# Tell the executable where to find not_included.h.
CONFIGURE_FILE(
${PROJECT_SOURCE_DIR}/config.h.in
${PROJECT_BINARY_DIR}/config.h
@ONLY IMMEDIATE
)
# add the executable
ADD_EXECUTABLE(CustomCommand
${PROJECT_BINARY_DIR}/foo.h
${PROJECT_BINARY_DIR}/foo.c
${PROJECT_BINARY_DIR}/wrapped.c
${PROJECT_BINARY_DIR}/wrapped_help.c
${PROJECT_BINARY_DIR}/generated.c
${PROJECT_BINARY_DIR}/not_included.h
)
TARGET_LINK_LIBRARIES(CustomCommand GeneratedHeader)
# must add a dependency on TDocument otherwise it might never build and
# the CustomCommand executable really needs doc1.h
ADD_DEPENDENCIES(CustomCommand TDocument)
##############################################################################
# Test for using just the target name as executable in the COMMAND
# section. Has to be recognized and replaced by CMake with the output
# actual location of the executable.
# Additionally the generator is created in an extra subdir after the
# ADD_CUSTOM_COMMAND() is used.
#
# Test the same for ADD_CUSTOM_TARGET()
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx
COMMAND generator_extern ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx
)
ADD_EXECUTABLE(CustomCommandUsingTargetTest main.cxx ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx )
ADD_CUSTOM_TARGET(RunTarget
COMMAND generator_extern ${CMAKE_CURRENT_BINARY_DIR}/run_target.cxx
)
ADD_CUSTOM_COMMAND(TARGET CustomCommandUsingTargetTest POST_BUILD
COMMAND dummy_generator ${CMAKE_CURRENT_BINARY_DIR}/generated_dummy.cxx)
ADD_SUBDIRECTORY(GeneratorInExtraDir)
##############################################################################
# Test non-trivial command line arguments in custom commands.
SET(EXPECTED_ARGUMENTS)
# TODO: Check shell operators < > << >> | 2>&1 1>&2 &> ! &
SET(CHECK_ARGS
c:/posix/path
c:\\windows\\path
'single-quotes'
single'quote
\"double-quotes\"
double\"quote
"\\;semi-colons\\;"
"semi\\;colon"
`back-ticks`
back`tick
"(parens)"
"(lparen"
"rparen)"
{curly}
{lcurly}
rcurly}
#<angle> # angle-brackets are inconsistent on windows right now
#<langle
#rangle>
[square]
[lsquare # these have funny behavior due to special cases for
rsquare] # windows registry value names in list expansion
$dollar-signs$
dollar$sign
&ersands&
one&ersand
@two-ats@
one@at
~two-tilda~
one~tilda
^two-carrots^
one^carrot
%two-percents%
one%percent
!two-exclamations!
one!exclamation
?two-questions?
one?question
*two-stars*
one*star
=two+equals=
one=equals
_two-underscores_
one_underscore
,two-commas,
one,comma
.two-periods.
one.period
|two-pipes|
one|pipe
"#two-pounds#"
"one#pound"
"c:/posix/path/with space"
"c:\\windows\\path\\with space"
"'single quotes with space'"
"single'quote with space"
"\"double-quotes with space\""
"double\"quote with space"
"\\;semi-colons with space\\;"
"semi\\;colon with space"
"`back-ticks` with space"
"back`tick with space"
"(parens) with space"
"(lparen with space"
"rparen) with space"
"{curly} with space"
"{lcurly with space"
"rcurly} with space"
#"<angle> with space" # angle-brackets are inconsistent on windows right now
#"<langle with space"
#"rangle> with space"
"[square] with space"
"[lsquare with space" # these have funny behavior due to special cases for
"rsquare] with space" # windows registry value names in list expansion
"$dollar-signs$ with space"
"dollar$sign with space"
"&ersands& with space"
"one&ersand with space"
"@two-ats@ with space"
"one@at with space"
"~two-tilda~ with space"
"one~tilda with space"
"^two-carrots^ with space"
"one^carrot with space"
"%two-percents% with space"
"one%percent with space"
"!two-exclamations! with space"
"one!exclamation with space"
"*two-stars* with space"
"one*star with space"
"=two+equals= with space"
"one=equals with space"
"_two-underscores_ with space"
"one_underscore with space"
"?two-questions? with space"
"one?question with space"
",two-commas, with space"
"one,comma with space"
".two-periods. with space"
"one.period with space"
"|two-pipes| with space"
"one|pipe with space"
"#two-pounds# with space"
"one#pound with space"
# ~ ` ! @ \# $ % ^ & * _ - + = | : \" ' < > , . ? /
# "(" ")" { } []
# >> << &> 2>&1 1>&2
# \\ \\;
)
FOREACH(arg ${CHECK_ARGS})
SET(ARG "${arg}")
STRING(REGEX REPLACE "\\\\" "\\\\\\\\" ARG "${ARG}")
STRING(REGEX REPLACE "\"" "\\\\\"" ARG "${ARG}")
SET(EXPECTED_ARGUMENTS
"${EXPECTED_ARGUMENTS} \"${ARG}\",
")
ENDFOREACH(arg)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}")
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/check_command_line.c.in
${CMAKE_CURRENT_BINARY_DIR}/check_command_line.c
@ONLY IMMEDIATE)
ADD_EXECUTABLE(check_command_line
${CMAKE_CURRENT_BINARY_DIR}/check_command_line.c)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/command_line_check
COMMAND ${CMAKE_COMMAND} -DMARK_FILE=${CMAKE_CURRENT_BINARY_DIR}/check_mark.txt
-P ${CMAKE_CURRENT_SOURCE_DIR}/check_mark.cmake
COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/check_command_line
${CHECK_ARGS}
VERBATIM
COMMENT "Checking custom command line escapes (single'quote)"
)
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/command_line_check
PROPERTIES SYMBOLIC 1)
ADD_CUSTOM_TARGET(do_check_command_line ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/command_line_check
COMMAND ${CMAKE_COMMAND} -E echo "Checking custom target command escapes"
COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/check_command_line
${CHECK_ARGS}
VERBATIM
COMMENT "Checking custom target command line escapes ($dollar-signs$)"
)
ADD_DEPENDENCIES(do_check_command_line check_command_line)
ADD_CUSTOM_TARGET(pre_check_command_line
COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/check_mark.txt
)
ADD_DEPENDENCIES(do_check_command_line pre_check_command_line)
|