Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Tests / CustomCommand / CMakeLists.txt
blobffe05237ebcbf334e602c7d4580062cd0d44a4e8
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   SOURCES doc1.tex
104   )
106 # Setup a pre- and post-build pair that will fail if not run in the
107 # proper order.
108 ADD_CUSTOM_COMMAND(
109   TARGET TDocument PRE_BUILD
110   COMMAND ${CMAKE_COMMAND} -E echo " Writing doc1pre.txt."
111   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/doc1.tex ${PROJECT_BINARY_DIR}/doc1pre.txt
112   COMMENT "Running TDocument pre-build commands"
113   )
114 ADD_CUSTOM_COMMAND(
115   TARGET TDocument POST_BUILD
116   COMMAND ${CMAKE_COMMAND} -E echo " Copying doc1pre.txt to doc2post.txt."
117   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/doc1pre.txt
118                                    ${PROJECT_BINARY_DIR}/doc2post.txt
119   COMMENT "Running TDocument post-build commands"
120   )
122 ################################################################
124 #  Test using a multistep generated file
126 ################################################################
127 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.pre
128   DEPENDS ${PROJECT_SOURCE_DIR}/foo.in
129   COMMAND   ${CMAKE_COMMAND}  
130   ARGS      -E copy ${PROJECT_SOURCE_DIR}/foo.in 
131   ${PROJECT_BINARY_DIR}/foo.pre
132   )
134 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/foo.c
135   DEPENDS   ${PROJECT_BINARY_DIR}/foo.pre 
136   COMMAND   ${CMAKE_COMMAND}
137   ARGS      -E copy ${PROJECT_BINARY_DIR}/foo.pre
138   ${PROJECT_BINARY_DIR}/foo.c
139   )
141 # Add custom command to generate not_included.h, which is a header
142 # file that is not included by any source in this project.  This will
143 # test whether all custom command outputs explicitly listed as sources
144 # get generated even if they are not needed by an object file.
145 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/not_included.h
146   DEPENDS ${PROJECT_SOURCE_DIR}/foo.h.in
147   COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/foo.h.in
148                                    ${PROJECT_BINARY_DIR}/not_included.h
149   )
151 # Tell the executable where to find not_included.h.
152 CONFIGURE_FILE(
153   ${PROJECT_SOURCE_DIR}/config.h.in
154   ${PROJECT_BINARY_DIR}/config.h
155   @ONLY IMMEDIATE
156   )
158 # add the executable
159 ADD_EXECUTABLE(CustomCommand 
160   ${PROJECT_BINARY_DIR}/foo.h
161   ${PROJECT_BINARY_DIR}/foo.c
162   ${PROJECT_BINARY_DIR}/wrapped.c
163   ${PROJECT_BINARY_DIR}/wrapped_help.c
164   ${PROJECT_BINARY_DIR}/generated.c
165   ${PROJECT_BINARY_DIR}/not_included.h
166   gen_redirect.c # default location for custom commands is in build tree
167   )
169 # Add the rule to create generated.c at build time.  This is placed
170 # here to test adding the generation rule after referencing the
171 # generated source in a target.
172 ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/generated.c
173   DEPENDS generator
174   COMMAND ${generator_PATH}
175   ARGS ${PROJECT_BINARY_DIR}/generated.c
176   )
178 TARGET_LINK_LIBRARIES(CustomCommand GeneratedHeader)
180 # must add a dependency on TDocument otherwise it might never build and 
181 # the CustomCommand executable really needs doc1.h
182 ADD_DEPENDENCIES(CustomCommand TDocument)
184 ##############################################################################
185 # Test for using just the target name as executable in the COMMAND
186 # section. Has to be recognized and replaced by CMake with the output
187 # actual location of the executable.
188 # Additionally the generator is created in an extra subdir after the 
189 # ADD_CUSTOM_COMMAND() is used.
191 # Test the same for ADD_CUSTOM_TARGET()
193 ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx
194   COMMAND generator_extern ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx
195   )
197 ADD_EXECUTABLE(CustomCommandUsingTargetTest main.cxx ${CMAKE_CURRENT_BINARY_DIR}/generated_extern.cxx )
199 ADD_CUSTOM_TARGET(RunTarget 
200   COMMAND generator_extern ${CMAKE_CURRENT_BINARY_DIR}/run_target.cxx
201   )
203 ADD_CUSTOM_COMMAND(TARGET CustomCommandUsingTargetTest POST_BUILD 
204                    COMMAND dummy_generator ${CMAKE_CURRENT_BINARY_DIR}/generated_dummy.cxx)
206 ADD_SUBDIRECTORY(GeneratorInExtraDir)
208 ##############################################################################
209 # Test shell operators in custom commands.
211 ADD_EXECUTABLE(tcat tcat.cxx)
213 ADD_CUSTOM_COMMAND(OUTPUT gen_redirect.c
214   DEPENDS tcat gen_redirect_in.c
215   COMMAND tcat < ${CMAKE_CURRENT_SOURCE_DIR}/gen_redirect_in.c > gen_redirect.c
216   COMMAND ${CMAKE_COMMAND} -E echo "#endif" >> gen_redirect.c
217   VERBATIM
218   )
220 ##############################################################################
221 # Test non-trivial command line arguments in custom commands.
222 SET(EXPECTED_ARGUMENTS)
223 SET(CHECK_ARGS
224   c:/posix/path
225   c:\\windows\\path
226   'single-quotes'
227   single'quote
228   \"double-quotes\"
229   "\\;semi-colons\\;"
230   "semi\\;colon"
231   `back-ticks`
232   back`tick
233   "(parens)"
234   "(lparen"
235   "rparen)"
236   {curly}
237   {lcurly}
238   rcurly}
239   <angle>
240   <langle
241   rangle>
242   [square]
243   [lsquare # these have funny behavior due to special cases for
244   rsquare] # windows registry value names in list expansion
245   $dollar-signs$
246   dollar$sign
247   &ampersands&x # Borland make does not like trailing ampersand
248   one&ampersand
249   @two-ats@
250   one@at
251   ~two-tilda~
252   one~tilda
253   ^two-carrots^
254   one^carrot
255   %two-percents%
256   one%percent
257   !two-exclamations!
258   one!exclamation
259   ?two-questions?
260   one?question
261   *two-stars*
262   one*star
263   =two+equals=
264   one=equals
265   _two-underscores_
266   one_underscore
267   ,two-commas,
268   one,comma
269   .two-periods.
270   one.period
271   |two-pipes|
272   one|pipe
273   |nopipe
274   "#two-pounds#"
275   "one#pound"
276   "#nocomment"
277   "c:/posix/path/with space"
278   "c:\\windows\\path\\with space"
279   "'single quotes with space'"
280   "single'quote with space"
281   "\"double-quotes with space\""
282   "\\;semi-colons w s\\;"
283   "semi\\;colon w s"
284   "`back-ticks` w s"
285   "back`tick w s"
286   "(parens) w s"
287   "(lparen w s"
288   "rparen) w s"
289   "{curly} w s"
290   "{lcurly w s"
291   "rcurly} w s"
292   "<angle> w s"
293   "<langle w s"
294   "rangle> w s"
295   "[square] w s"
296   "[lsquare w s" # these have funny behavior due to special cases for
297   "rsquare] w s" # windows registry value names in list expansion
298   "$dollar-signs$ w s"
299   "dollar$sign w s"
300   "&ampersands& w s"
301   "one&ampersand w s"
302   "@two-ats@ w s"
303   "one@at w s"
304   "~two-tilda~ w s"
305   "one~tilda w s"
306   "^two-carrots^ w s"
307   "one^carrot w s"
308   "%two-percents% w s"
309   "one%percent w s"
310   "!two-exclamations! w s"
311   "one!exclamation w s"
312   "*two-stars* w s"
313   "one*star w s"
314   "=two+equals= w s"
315   "one=equals w s"
316   "_two-underscores_ w s"
317   "one_underscore w s"
318   "?two-questions? w s"
319   "one?question w s"
320   ",two-commas, w s"
321   "one,comma w s"
322   ".two-periods. w s"
323   "one.period w s"
324   "|two-pipes| w s"
325   "one|pipe w s"
326   "#two-pounds# w s"
327   "one#pound w s"
328   ~ ` ! @ \# $ % ^ & _ - + = : "\;" \" ' , . ? "(" ")" { } []
329   )
330 IF(NOT MINGW)
331   # *  # MinGW programs on windows always expands the wildcard!
332   # /  # MSys make converts a leading slash to the mingw home directory
333   LIST(APPEND CHECK_ARGS * /)
334 ENDIF(NOT MINGW)
336 # The windows command shell does not support a double quote by itself:
337 #   double\"quote
338 # without messing up quoting of arguments following it.
340 # Make tools need help with escaping a single backslash
341 #   \
342 # at the end of a command because they think it is a continuation
343 # character.
345 # We now have special cases for shell operators:
346 #   | < > << >> &> 2>&1 1>&2
347 # to allow custom commands to perform redirection.
349 FOREACH(arg ${CHECK_ARGS} "")
350   SET(ARG "${arg}")
351   STRING(REGEX REPLACE "\\\\" "\\\\\\\\" ARG "${ARG}")
352   STRING(REGEX REPLACE "\"" "\\\\\"" ARG "${ARG}")
353   SET(EXPECTED_ARGUMENTS
354     "${EXPECTED_ARGUMENTS}  \"${ARG}\",
356 ENDFOREACH(arg)
357 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}")
358 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/check_command_line.c.in
359                ${CMAKE_CURRENT_BINARY_DIR}/check_command_line.c
360                @ONLY IMMEDIATE)
361 ADD_EXECUTABLE(check_command_line
362   ${CMAKE_CURRENT_BINARY_DIR}/check_command_line.c)
363 # SET_TARGET_PROPERTIES(check_command_line PROPERTIES
364 #   COMPILE_FLAGS -DCHECK_COMMAND_LINE_VERBOSE)
365 ADD_CUSTOM_COMMAND(
366   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/command_line_check
367   COMMAND ${CMAKE_COMMAND} -DMARK_FILE=${CMAKE_CURRENT_BINARY_DIR}/check_mark.txt
368   -P ${CMAKE_CURRENT_SOURCE_DIR}/check_mark.cmake
369   COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/check_command_line
370   ${CHECK_ARGS} ""
371   VERBATIM
372   COMMENT "Checking custom command line escapes (single'quote)"
373   )
374 SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/command_line_check
375   PROPERTIES SYMBOLIC 1)
376 ADD_CUSTOM_TARGET(do_check_command_line ALL
377   DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/command_line_check
378   COMMAND ${CMAKE_COMMAND} -E echo "Checking custom target command escapes"
379   COMMAND ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/check_command_line
380   ${CHECK_ARGS} ""
381   VERBATIM
382   COMMENT "Checking custom target command line escapes ($dollar-signs$)"
383   )
384 ADD_DEPENDENCIES(do_check_command_line check_command_line)
386 ADD_CUSTOM_TARGET(pre_check_command_line
387   COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/check_mark.txt
388   )
389 ADD_DEPENDENCIES(do_check_command_line pre_check_command_line)