CMakeLists.txt 12 KB

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