STYLE: Nightly Date Stamp
[cmake.git] / Tests / CustomCommand / CMakeLists.txt
blobb8410e1835026f93b685bd68216bd3f5aa17d20b
2 # Wrapping
4 cmake_minimum_required (VERSION 2.6)
5 PROJECT (CustomCommand)
7 ADD_SUBDIRECTORY(GeneratedHeader)
10 # Lib and exe path
12 SET (LIBRARY_OUTPUT_PATH 
13   ${PROJECT_BINARY_DIR}/bin/ CACHE INTERNAL 
14   "Single output directory for building all libraries.")
16 SET (EXECUTABLE_OUTPUT_PATH 
17   ${PROJECT_BINARY_DIR}/bin/ CACHE INTERNAL 
18   "Single output directory for building all executables.")
20 ################################################################
22 #  First test using a compiled generator to create a .c file
24 ################################################################
25 # add the executable that will generate the file
26 ADD_EXECUTABLE(generator generator.cxx)
28 GET_TARGET_PROPERTY(generator_PATH generator LOCATION)
29 MESSAGE("Location ${generator_PATH}")
31 ################################################################
33 #  Test using a wrapper to wrap a header file
35 ################################################################
36 # add the executable that will generate the file
37 ADD_EXECUTABLE(wrapper wrapper.cxx)
39 ADD_CUSTOM_COMMAND(
40   OUTPUT ${PROJECT_BINARY_DIR}/wrapped.c ${PROJECT_BINARY_DIR}/wrapped_help.c
41   DEPENDS wrapper
42   MAIN_DEPENDENCY ${PROJECT_SOURCE_DIR}/wrapped.h
43   COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/wrapper
44   ${PROJECT_BINARY_DIR}/wrapped.c ${PROJECT_BINARY_DIR}/wrapped_help.c
45   ${CMAKE_CFG_INTDIR} # this argument tests passing of the configuration
46   VERBATIM # passing of configuration should work in this mode
47   )
49 ################################################################
51 #  Test creating files from a custom target
53 ################################################################
54 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/doc1.dvi
55   DEPENDS ${PROJECT_SOURCE_DIR}/doc1.tex 
56   COMMAND   ${CMAKE_COMMAND}  
57   ARGS      -E copy ${PROJECT_SOURCE_DIR}/doc1.tex 
58   ${PROJECT_BINARY_DIR}/doc1.dvi
59   )
61 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/doc1.h
62   COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1.dvi to doc1temp.h."
63   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1.dvi
64                                    ${PROJECT_BINARY_DIR}/doc1temp.h
65   )
66 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/doc1.h APPEND
67   DEPENDS ${PROJECT_BINARY_DIR}/doc1.dvi
68   COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1temp.h to doc1.h."
69   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1temp.h
70                                    ${PROJECT_BINARY_DIR}/doc1.h
71   COMMAND ${CMAKE_COMMAND} -E echo " Removing doc1temp.h."
72   COMMAND ${CMAKE_COMMAND} -E remove -f ${PROJECT_BINARY_DIR}/doc1temp.h
73   )
75 # Add custom command to generate foo.h.
76 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.h
77   DEPENDS ${PROJECT_SOURCE_DIR}/foo.h.in
78   COMMAND ${CMAKE_COMMAND} -E echo " Copying foo.h.in to foo.h."
79   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/foo.h.in
80                                    ${PROJECT_BINARY_DIR}/foo.h
81   )
83 # Add the location of foo.h to the include path.
84 INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR})
86 # Test generation of a file to the build tree without full path.  As
87 # of CMake 2.6 custom command outputs specified by relative path go in
88 # the build tree.
89 ADD_CUSTOM_COMMAND(
90   OUTPUT doc1.txt
91   COMMAND ${CMAKE_COMMAND} -E echo "Example Document Target" > doc1.txt
92   DEPENDS doc1.tex
93   VERBATIM
94   )
96 # Add a custom target to drive generation of doc1.h.
97 ADD_CUSTOM_TARGET(TDocument ALL
98   COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1.h to doc2.h."
99   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1.h
100                                    ${PROJECT_BINARY_DIR}/doc2.h
101   DEPENDS ${PROJECT_BINARY_DIR}/doc1.h doc1.txt
102   COMMENT "Running top-level TDocument commands"
103   )
105 # Setup a pre- and post-build pair that will fail if not run in the
106 # proper order.
107 ADD_CUSTOM_COMMAND(
108   TARGET TDocument PRE_BUILD
109   COMMAND ${CMAKE_COMMAND} -E echo " Writing doc1pre.txt."
110   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/doc1.tex ${PROJECT_BINARY_DIR}/doc1pre.txt
111   COMMENT "Running TDocument pre-build commands"
112   )
113 ADD_CUSTOM_COMMAND(
114   TARGET TDocument POST_BUILD
115   COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1pre.txt to doc2post.txt."
116   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1pre.txt
117                                    ${PROJECT_BINARY_DIR}/doc2post.txt
118   COMMENT "Running TDocument post-build commands"
119   )
121 ################################################################
123 #  Test using a multistep generated file
125 ################################################################
126 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.pre
127   DEPENDS ${PROJECT_SOURCE_DIR}/foo.in
128   COMMAND   ${CMAKE_COMMAND}  
129   ARGS      -E copy ${PROJECT_SOURCE_DIR}/foo.in 
130   ${PROJECT_BINARY_DIR}/foo.pre
131   )
133 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.c
134   DEPENDS   ${PROJECT_BINARY_DIR}/foo.pre 
135   COMMAND   ${CMAKE_COMMAND}
136   ARGS      -E copy ${PROJECT_BINARY_DIR}/foo.pre
137   ${PROJECT_BINARY_DIR}/foo.c
138   )
140 # Add custom command to generate not_included.h, which is a header
141 # file that is not included by any source in this project.  This will
142 # test whether all custom command outputs explicitly listed as sources
143 # get generated even if they are not needed by an object file.
144 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/not_included.h
145   DEPENDS ${PROJECT_SOURCE_DIR}/foo.h.in
146   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/foo.h.in
147                                    ${PROJECT_BINARY_DIR}/not_included.h
148   )
150 # Tell the executable where to find not_included.h.
151 CONFIGURE_FILE(
152   ${PROJECT_SOURCE_DIR}/config.h.in
153   ${PROJECT_BINARY_DIR}/config.h
154   @ONLY IMMEDIATE
155   )
157 # add the executable
158 ADD_EXECUTABLE(CustomCommand 
159   ${PROJECT_BINARY_DIR}/foo.h
160   ${PROJECT_BINARY_DIR}/foo.c
161   ${PROJECT_BINARY_DIR}/wrapped.c
162   ${PROJECT_BINARY_DIR}/wrapped_help.c
163   ${PROJECT_BINARY_DIR}/generated.c
164   ${PROJECT_BINARY_DIR}/not_included.h
165   gen_redirect.c # default location for custom commands is in build tree
166   )
168 # Add the rule to create generated.c at build time.  This is placed
169 # here to test adding the generation rule after referencing the
170 # generated source in a target.
171 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/generated.c
172   DEPENDS generator
173   COMMAND ${generator_PATH}
174   ARGS ${PROJECT_BINARY_DIR}/generated.c
175   )
177 TARGET_LINK_LIBRARIES(CustomCommand GeneratedHeader)
179 # must add a dependency on TDocument otherwise it might never build and 
180 # the CustomCommand executable really needs doc1.h
181 ADD_DEPENDENCIES(CustomCommand TDocument)
183 ##############################################################################
184 # Test for using just the target name as executable in the COMMAND
185 # section. Has to be recognized and replaced by CMake with the output
186 # actual location of the executable.
187 # Additionally the generator is created in an extra subdir after the 
188 # ADD_CUSTOM_COMMAND() is used.
190 # Test the same for ADD_CUSTOM_TARGET()
192 ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx
193   COMMAND generator_extern ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx
194   )
196 ADD_EXECUTABLE(CustomCommandUsingTargetTest main.cxx ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx )
198 ADD_CUSTOM_TARGET(RunTarget 
199   COMMAND generator_extern ${CMAKE_CURRENT_BINARY_DIR}/run_target.cxx
200   )
202 ADD_CUSTOM_COMMAND(TARGET CustomCommandUsingTargetTest POST_BUILD 
203                    COMMAND dummy_generator ${CMAKE_CURRENT_BINARY_DIR}/generated_dummy.cxx)
205 ADD_SUBDIRECTORY(GeneratorInExtraDir)
207 ##############################################################################
208 # Test shell operators in custom commands.
210 ADD_EXECUTABLE(tcat tcat.cxx)
212 ADD_CUSTOM_COMMAND(OUTPUT gen_redirect.c
213   DEPENDS tcat gen_redirect_in.c
214   COMMAND tcat < ${CMAKE_CURRENT_SOURCE_DIR}/gen_redirect_in.c > gen_redirect.c
215   COMMAND ${CMAKE_COMMAND} -E echo "#endif" >> gen_redirect.c
216   VERBATIM
217   )
219 ##############################################################################
220 # Test non-trivial command line arguments in custom commands.
221 SET(EXPECTED_ARGUMENTS)
222 SET(CHECK_ARGS
223   c:/posix/path
224   c:\\windows\\path
225   'single-quotes'
226   single'quote
227   \"double-quotes\"
228   "\\;semi-colons\\;"
229   "semi\\;colon"
230   `back-ticks`
231   back`tick
232   "(parens)"
233   "(lparen"
234   "rparen)"
235   {curly}
236   {lcurly}
237   rcurly}
238   <angle>
239   <langle
240   rangle>
241   [square]
242   [lsquare # these have funny behavior due to special cases for
243   rsquare] # windows registry value names in list expansion
244   $dollar-signs$
245   dollar$sign
246   &ampersands&x # Borland make does not like trailing ampersand
247   one&ampersand
248   @two-ats@
249   one@at
250   ~two-tilda~
251   one~tilda
252   ^two-carrots^
253   one^carrot
254   %two-percents%
255   one%percent
256   !two-exclamations!
257   one!exclamation
258   ?two-questions?
259   one?question
260   *two-stars*
261   one*star
262   =two+equals=
263   one=equals
264   _two-underscores_
265   one_underscore
266   ,two-commas,
267   one,comma
268   .two-periods.
269   one.period
270   |two-pipes|
271   one|pipe
272   |nopipe
273   "#two-pounds#"
274   "one#pound"
275   "#nocomment"
276   "c:/posix/path/with space"
277   "c:\\windows\\path\\with space"
278   "'single quotes with space'"
279   "single'quote with space"
280   "\"double-quotes with space\""
281   "\\;semi-colons w s\\;"
282   "semi\\;colon w s"
283   "`back-ticks` w s"
284   "back`tick w s"
285   "(parens) w s"
286   "(lparen w s"
287   "rparen) w s"
288   "{curly} w s"
289   "{lcurly w s"
290   "rcurly} w s"
291   "<angle> w s"
292   "<langle w s"
293   "rangle> w s"
294   "[square] w s"
295   "[lsquare w s" # these have funny behavior due to special cases for
296   "rsquare] w s" # windows registry value names in list expansion
297   "$dollar-signs$ w s"
298   "dollar$sign w s"
299   "&ampersands& w s"
300   "one&ampersand w s"
301   "@two-ats@ w s"
302   "one@at w s"
303   "~two-tilda~ w s"
304   "one~tilda w s"
305   "^two-carrots^ w s"
306   "one^carrot w s"
307   "%two-percents% w s"
308   "one%percent w s"
309   "!two-exclamations! w s"
310   "one!exclamation w s"
311   "*two-stars* w s"
312   "one*star w s"
313   "=two+equals= w s"
314   "one=equals w s"
315   "_two-underscores_ w s"
316   "one_underscore w s"
317   "?two-questions? w s"
318   "one?question w s"
319   ",two-commas, w s"
320   "one,comma w s"
321   ".two-periods. w s"
322   "one.period w s"
323   "|two-pipes| w s"
324   "one|pipe w s"
325   "#two-pounds# w s"
326   "one#pound w s"
327   ~ ` ! @ \# $ % ^ & _ - + = : "\;" \" ' , . ? "(" ")" { } []
328   )
329 IF(NOT MINGW)
330   # *  # MinGW programs on windows always expands the wildcard!
331   # /  # MSys make converts a leading slash to the mingw home directory
332   LIST(APPEND CHECK_ARGS * /)
333 ENDIF(NOT MINGW)
335 # The windows command shell does not support a double quote by itself:
336 #   double\"quote
337 # without messing up quoting of arguments following it.
339 # Make tools need help with escaping a single backslash
340 #   \
341 # at the end of a command because they think it is a continuation
342 # character.
344 # We now have special cases for shell operators:
345 #   | < > << >> &> 2>&1 1>&2
346 # to allow custom commands to perform redirection.
348 FOREACH(arg ${CHECK_ARGS})
349   SET(ARG "${arg}")
350   STRING(REGEX REPLACE "\\\\" "\\\\\\\\" ARG "${ARG}")
351   STRING(REGEX REPLACE "\"" "\\\\\"" ARG "${ARG}")
352   SET(EXPECTED_ARGUMENTS
353     "${EXPECTED_ARGUMENTS}  \"${ARG}\",
355 ENDFOREACH(arg)
356 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}")
357 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/check_command_line.c.in
358                ${CMAKE_CURRENT_BINARY_DIR}/check_command_line.c
359                @ONLY IMMEDIATE)
360 ADD_EXECUTABLE(check_command_line
361   ${CMAKE_CURRENT_BINARY_DIR}/check_command_line.c)
362 # SET_TARGET_PROPERTIES(check_command_line PROPERTIES
363 #   COMPILE_FLAGS -DCHECK_COMMAND_LINE_VERBOSE)
364 ADD_CUSTOM_COMMAND(
365   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/command_line_check
366   COMMAND ${CMAKE_COMMAND} -DMARK_FILE=${CMAKE_CURRENT_BINARY_DIR}/check_mark.txt
367   -P ${CMAKE_CURRENT_SOURCE_DIR}/check_mark.cmake
368   COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/check_command_line
369   ${CHECK_ARGS}
370   VERBATIM
371   COMMENT "Checking custom command line escapes (single'quote)"
372   )
373 SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/command_line_check
374   PROPERTIES SYMBOLIC 1)
375 ADD_CUSTOM_TARGET(do_check_command_line ALL
376   DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/command_line_check
377   COMMAND ${CMAKE_COMMAND} -E echo "Checking custom target command escapes"
378   COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/check_command_line
379   ${CHECK_ARGS}
380   VERBATIM
381   COMMENT "Checking custom target command line escapes ($dollar-signs$)"
382   )
383 ADD_DEPENDENCIES(do_check_command_line check_command_line)
385 ADD_CUSTOM_TARGET(pre_check_command_line
386   COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/check_mark.txt
387   )
388 ADD_DEPENDENCIES(do_check_command_line pre_check_command_line)