CMakeLists.txt 13 KB

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