4 cmake_minimum_required (VERSION 2.6)
5 PROJECT (CustomCommand)
7 ADD_SUBDIRECTORY(GeneratedHeader)
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)
40 OUTPUT ${PROJECT_BINARY_DIR}/wrapped.c ${PROJECT_BINARY_DIR}/wrapped_help.c
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
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
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
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
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
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
91 COMMAND ${CMAKE_COMMAND} -E echo "Example Document Target" > doc1.txt
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"
105 # Setup a pre- and post-build pair that will fail if not run in the
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"
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"
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
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
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
150 # Tell the executable where to find not_included.h.
152 ${PROJECT_SOURCE_DIR}/config.h.in
153 ${PROJECT_BINARY_DIR}/config.h
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
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
173 COMMAND ${generator_PATH}
174 ARGS ${PROJECT_BINARY_DIR}/generated.c
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
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
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
219 ##############################################################################
220 # Test non-trivial command line arguments in custom commands.
221 SET(EXPECTED_ARGUMENTS)
242 [lsquare # these have funny behavior due to special cases for
243 rsquare] # windows registry value names in list expansion
246 &ersands&x # Borland make does not like trailing ampersand
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\\;"
295 "[lsquare w s" # these have funny behavior due to special cases for
296 "rsquare] w s" # windows registry value names in list expansion
309 "!two-exclamations! w s"
310 "one!exclamation w s"
315 "_two-underscores_ w s"
317 "?two-questions? w s"
327 ~ ` ! @ \# $ % ^ & _ - + = : "\;" \" ' , . ? "(" ")" { } []
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 * /)
335 # The windows command shell does not support a double quote by itself:
337 # without messing up quoting of arguments following it.
339 # Make tools need help with escaping a single backslash
341 # at the end of a command because they think it is a continuation
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})
350 STRING(REGEX REPLACE "\\\\" "\\\\\\\\" ARG "${ARG}")
351 STRING(REGEX REPLACE "\"" "\\\\\"" ARG "${ARG}")
352 SET(EXPECTED_ARGUMENTS
353 "${EXPECTED_ARGUMENTS} \"${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
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)
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
371 COMMENT "Checking custom command line escapes (single'quote)"
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
381 COMMENT "Checking custom target command line escapes ($dollar-signs$)"
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
388 ADD_DEPENDENCIES(do_check_command_line pre_check_command_line)