CMakeLists.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. # this test creates a static library and an executable
  2. # the source to the library is then changed
  3. # and the build is done on the executable and if things
  4. # are working the executable should relink with the new
  5. # value. The subdir Project contains the CMakelists.txt
  6. # and source files for the test project.
  7. cmake_minimum_required (VERSION 2.6)
  8. project(BuildDepends)
  9. # This entire test takes place during the initial
  10. # configure step. It should not run again when the
  11. # project is built.
  12. set(CMAKE_SUPPRESS_REGENERATION 1)
  13. # Xcode needs some help with the fancy dependencies in this test.
  14. if(XCODE AND XCODE_VERSION VERSION_LESS 5)
  15. set(HELP_XCODE 1)
  16. endif()
  17. function(help_xcode_depends)
  18. if(HELP_XCODE)
  19. file(GLOB_RECURSE MACRO_OBJS
  20. ${BuildDepends_BINARY_DIR}/Project/zot_macro_*.o*
  21. )
  22. if(MACRO_OBJS)
  23. message("Helping Xcode by removing objects [${MACRO_OBJS}]")
  24. file(REMOVE ${MACRO_OBJS})
  25. endif()
  26. endif()
  27. endfunction()
  28. # The Intel compiler causes the MSVC linker to crash during
  29. # incremental linking, so avoid the /INCREMENTAL:YES flag.
  30. if(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
  31. set(_cmake_options "-DCMAKE_EXE_LINKER_FLAGS=")
  32. endif()
  33. if("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
  34. set(TEST_LINK_DEPENDS ${BuildDepends_BINARY_DIR}/Project/linkdep.txt)
  35. file(WRITE ${TEST_LINK_DEPENDS} "1")
  36. endif()
  37. list(APPEND _cmake_options "-DTEST_LINK_DEPENDS=${TEST_LINK_DEPENDS}")
  38. list(APPEND _cmake_options "-DCMAKE_FORCE_DEPFILES=1")
  39. if(NOT CMAKE_GENERATOR MATCHES "Visual Studio ([^89]|[89][0-9])")
  40. set(TEST_MULTI3 1)
  41. list(APPEND _cmake_options "-DTEST_MULTI3=1")
  42. endif()
  43. file(MAKE_DIRECTORY ${BuildDepends_BINARY_DIR}/Project)
  44. message("Creating Project/foo.cxx")
  45. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  46. "const char* foo() { return \"foo\";}" )
  47. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  48. "static const char* zot = \"zot\";\n")
  49. file(WRITE ${BuildDepends_BINARY_DIR}/Project/dir/header.txt
  50. "#define HEADER_STRING \"ninja\"\n" )
  51. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  52. "static const char* zot_custom = \"zot_custom\";\n")
  53. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  54. "static const char* zot_macro_dir = \"zot_macro_dir\";\n")
  55. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  56. "static const char* zot_macro_tgt = \"zot_macro_tgt\";\n")
  57. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  58. "#define link_depends_no_shared_lib_value 1\n")
  59. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_exe.h
  60. "#define link_depends_no_shared_exe_value 0\n")
  61. set(link_depends_no_shared_check_txt ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_check.txt)
  62. file(WRITE ${BuildDepends_BINARY_DIR}/Project/object_depends.txt "0\n")
  63. set(object_depends_check_txt ${BuildDepends_BINARY_DIR}/Project/object_depends_check.txt)
  64. file(WRITE ${BuildDepends_BINARY_DIR}/Project/external.in "external original\n")
  65. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi1-in.txt "multi1-in original\n")
  66. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt "multi2-stamp original\n")
  67. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt "multi3-stamp original\n")
  68. help_xcode_depends()
  69. message("Building project first time")
  70. try_compile(RESULT
  71. ${BuildDepends_BINARY_DIR}/Project
  72. ${BuildDepends_SOURCE_DIR}/Project
  73. testRebuild
  74. CMAKE_FLAGS ${_cmake_options}
  75. OUTPUT_VARIABLE OUTPUT)
  76. if(HELP_XCODE)
  77. try_compile(RESULT
  78. ${BuildDepends_BINARY_DIR}/Project
  79. ${BuildDepends_SOURCE_DIR}/Project
  80. testRebuild
  81. OUTPUT_VARIABLE OUTPUT)
  82. try_compile(RESULT
  83. ${BuildDepends_BINARY_DIR}/Project
  84. ${BuildDepends_SOURCE_DIR}/Project
  85. testRebuild
  86. OUTPUT_VARIABLE OUTPUT)
  87. endif()
  88. message("Output from first build:\n${OUTPUT}")
  89. if(NOT RESULT)
  90. message(SEND_ERROR "Could not build test project (1)!")
  91. endif()
  92. # find and save the ninjadep executable
  93. set(ninjadep ${BuildDepends_BINARY_DIR}/Project/ninjadep${CMAKE_EXECUTABLE_SUFFIX})
  94. if(EXISTS
  95. "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}" )
  96. message("found debug")
  97. set(ninjadep
  98. "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}")
  99. endif()
  100. message("Running ${ninjadep} ")
  101. execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  102. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  103. message("Run result: ${runResult} Output: \"${out}\"")
  104. if("${out}" STREQUAL "HEADER_STRING: ninja ")
  105. message("Worked!")
  106. else()
  107. message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
  108. " expected [HEADER_STRING: ninja]")
  109. endif()
  110. set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
  111. if(EXISTS
  112. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  113. message("found debug")
  114. set(bar
  115. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  116. endif()
  117. set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
  118. if(EXISTS
  119. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  120. message("found debug")
  121. set(zot
  122. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  123. endif()
  124. message("Running ${bar} ")
  125. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  126. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  127. message("Run result: ${runResult} Output: \"${out}\"")
  128. if("${out}" STREQUAL "foo ")
  129. message("Worked!")
  130. else()
  131. message(SEND_ERROR "Project did not initially build properly: ${out}")
  132. endif()
  133. message("Running ${zot} ")
  134. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  135. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  136. message("Run result: ${runResult} Output: \"${out}\"")
  137. set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] ")
  138. if("${out}" STREQUAL "${VALUE_UNCHANGED}")
  139. message("Worked!")
  140. else()
  141. message(SEND_ERROR "Project did not initially build properly: ${out}")
  142. endif()
  143. if(EXISTS "${link_depends_no_shared_check_txt}")
  144. file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  145. if("${link_depends_no_shared_check}" STREQUAL "1")
  146. message(STATUS "link_depends_no_shared_exe is newer than link_depends_no_shared_lib as expected.")
  147. else()
  148. message(SEND_ERROR "Project did not initially build properly: "
  149. "link_depends_no_shared_exe is older than link_depends_no_shared_lib.")
  150. endif()
  151. else()
  152. message(SEND_ERROR "Project did not initially build properly: "
  153. "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
  154. endif()
  155. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/external.out)
  156. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/external.out external_out)
  157. if("${external_out}" STREQUAL "external original")
  158. message(STATUS "external.out contains '${external_out}'")
  159. else()
  160. message(SEND_ERROR "Project did not initially build properly: "
  161. "external.out contains '${external_out}'")
  162. endif()
  163. else()
  164. message(SEND_ERROR "Project did not initially build properly: "
  165. "external.out is missing")
  166. endif()
  167. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt)
  168. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt multi1_out)
  169. if("${multi1_out}" STREQUAL "multi1-in original")
  170. message(STATUS "multi1-out2-copy.txt contains '${multi1_out}'")
  171. else()
  172. message(SEND_ERROR "Project did not initially build properly: "
  173. "multi1-out2-copy.txt contains '${multi1_out}'")
  174. endif()
  175. else()
  176. message(SEND_ERROR "Project did not initially build properly: "
  177. "multi1-out2-copy.txt is missing")
  178. endif()
  179. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi2-real.txt)
  180. if(${BuildDepends_BINARY_DIR}/Project/multi2-real.txt
  181. IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt)
  182. message(STATUS "multi2-real.txt is newer than multi2-stamp.txt")
  183. else()
  184. message(SEND_ERROR "Project did not initially build properly: "
  185. "multi2-real.txt is not newer than multi2-stamp.txt")
  186. endif()
  187. else()
  188. message(SEND_ERROR "Project did not initially build properly: "
  189. "multi2-real.txt is missing")
  190. endif()
  191. if(TEST_MULTI3)
  192. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi3-real.txt)
  193. if(${BuildDepends_BINARY_DIR}/Project/multi3-real.txt
  194. IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt)
  195. message(STATUS "multi3-real.txt is newer than multi3-stamp.txt")
  196. else()
  197. message(SEND_ERROR "Project did not initially build properly: "
  198. "multi3-real.txt is not newer than multi3-stamp.txt")
  199. endif()
  200. else()
  201. message(SEND_ERROR "Project did not initially build properly: "
  202. "multi3-real.txt is missing")
  203. endif()
  204. endif()
  205. message("Waiting 3 seconds...")
  206. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 3)
  207. message("Modifying Project/foo.cxx")
  208. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  209. "const char* foo() { return \"foo changed\";}" )
  210. file(WRITE "${BuildDepends_BINARY_DIR}/Project/dir/header.txt"
  211. "#define HEADER_STRING \"ninja changed\"\n" )
  212. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  213. "static const char* zot = \"zot changed\";\n")
  214. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  215. "static const char* zot_custom = \"zot_custom changed\";\n")
  216. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  217. "static const char* zot_macro_dir = \"zot_macro_dir changed\";\n")
  218. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  219. "static const char* zot_macro_tgt = \"zot_macro_tgt changed\";\n")
  220. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  221. "#define link_depends_no_shared_lib_value 0\n")
  222. file(WRITE ${BuildDepends_BINARY_DIR}/Project/object_depends.txt "1\n")
  223. if(TEST_LINK_DEPENDS)
  224. file(WRITE ${TEST_LINK_DEPENDS} "2")
  225. endif()
  226. file(WRITE ${BuildDepends_BINARY_DIR}/Project/external.in "external changed\n")
  227. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi1-in.txt "multi1-in changed\n")
  228. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt "multi2-stamp changed\n")
  229. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt "multi3-stamp changed\n")
  230. help_xcode_depends()
  231. message("Building project second time")
  232. try_compile(RESULT
  233. ${BuildDepends_BINARY_DIR}/Project
  234. ${BuildDepends_SOURCE_DIR}/Project
  235. testRebuild
  236. CMAKE_FLAGS ${_cmake_options}
  237. OUTPUT_VARIABLE OUTPUT)
  238. # Xcode is in serious need of help here
  239. if(HELP_XCODE)
  240. try_compile(RESULT
  241. ${BuildDepends_BINARY_DIR}/Project
  242. ${BuildDepends_SOURCE_DIR}/Project
  243. testRebuild
  244. OUTPUT_VARIABLE OUTPUT)
  245. try_compile(RESULT
  246. ${BuildDepends_BINARY_DIR}/Project
  247. ${BuildDepends_SOURCE_DIR}/Project
  248. testRebuild
  249. OUTPUT_VARIABLE OUTPUT)
  250. endif()
  251. message("Output from second build:\n${OUTPUT}")
  252. if(NOT RESULT)
  253. message(SEND_ERROR "Could not build test project (2)!")
  254. endif()
  255. if(EXISTS
  256. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  257. message("found debug")
  258. endif()
  259. if(EXISTS
  260. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  261. message("found debug")
  262. endif()
  263. message("Running ${ninjadep} ")
  264. execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  265. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  266. message("Run result: ${runResult} Output: \"${out}\"")
  267. if("${out}" STREQUAL "HEADER_STRING: ninja changed ")
  268. message("Worked!")
  269. else()
  270. message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
  271. " expected [HEADER_STRING: ninja changed]")
  272. endif()
  273. message("Running ${bar} ")
  274. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  275. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  276. message("Run result: ${runResult} Output: \"${out}\"")
  277. if("${out}" STREQUAL "foo changed ")
  278. message("Worked!")
  279. else()
  280. message(SEND_ERROR "Project did not rebuild properly!")
  281. endif()
  282. message("Running ${zot} ")
  283. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  284. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  285. message("Run result: ${runResult} Output: \"${out}\"")
  286. set(VALUE_CHANGED
  287. "[zot changed] [zot_custom changed] [zot_macro_dir changed] [zot_macro_tgt changed] "
  288. )
  289. if("${out}" STREQUAL "${VALUE_CHANGED}")
  290. message("Worked!")
  291. else()
  292. message(SEND_ERROR "Project did not rebuild properly!")
  293. endif()
  294. if(TEST_LINK_DEPENDS)
  295. set(linkdep ${BuildDepends_BINARY_DIR}/Project/linkdep${CMAKE_EXECUTABLE_SUFFIX})
  296. if(${linkdep} IS_NEWER_THAN ${TEST_LINK_DEPENDS})
  297. message("LINK_DEPENDS worked")
  298. else()
  299. message(SEND_ERROR "LINK_DEPENDS failed. Executable
  300. ${linkdep}
  301. is not newer than dependency
  302. ${TEST_LINK_DEPENDS}
  303. ")
  304. endif()
  305. endif()
  306. if(EXISTS "${link_depends_no_shared_check_txt}")
  307. file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  308. if("${link_depends_no_shared_check}" STREQUAL "0")
  309. message(STATUS "link_depends_no_shared_exe is older than link_depends_no_shared_lib as expected.")
  310. elseif(XCODE AND NOT XCODE_VERSION VERSION_LESS 5)
  311. message(STATUS "Known limitation: link_depends_no_shared_exe is newer than link_depends_no_shared_lib but we cannot stop Xcode ${XCODE_VERSION} from enforcing this dependency.")
  312. else()
  313. message(SEND_ERROR "Project did not rebuild properly: link_depends_no_shared_exe is newer than link_depends_no_shared_lib.")
  314. endif()
  315. else()
  316. message(SEND_ERROR "Project did not rebuild properly. "
  317. "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
  318. endif()
  319. if(EXISTS "${object_depends_check_txt}")
  320. file(STRINGS "${object_depends_check_txt}" object_depends_check LIMIT_COUNT 1)
  321. if("${object_depends_check}" STREQUAL "1")
  322. message(STATUS "object_depends exe is newer than object_depends.txt as expected.")
  323. elseif(CMAKE_GENERATOR MATCHES "Visual Studio|Xcode")
  324. message(STATUS "Known limitation: OBJECT_DEPENDS does not work on ${CMAKE_GENERATOR}")
  325. else()
  326. message(SEND_ERROR "Project did not rebuild properly: object_depends exe is not newer than object_depends.txt.")
  327. endif()
  328. else()
  329. message(SEND_ERROR "Project did not rebuild properly. "
  330. "object_depends exe and object_depends.txt are not both present.")
  331. endif()
  332. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/external.out)
  333. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/external.out external_out)
  334. if("${external_out}" STREQUAL "external changed")
  335. message(STATUS "external.out contains '${external_out}'")
  336. else()
  337. message(SEND_ERROR "Project did not rebuild properly: "
  338. "external.out contains '${external_out}'")
  339. endif()
  340. else()
  341. message(SEND_ERROR "Project did not rebuild properly: "
  342. "external.out is missing")
  343. endif()
  344. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt)
  345. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt multi1_out)
  346. if("${multi1_out}" STREQUAL "multi1-in changed")
  347. message(STATUS "multi1-out2-copy.txt contains '${multi1_out}'")
  348. else()
  349. message(SEND_ERROR "Project did not rebuild properly: "
  350. "multi1-out2-copy.txt contains '${multi1_out}'")
  351. endif()
  352. else()
  353. message(SEND_ERROR "Project did not rebuild properly: "
  354. "multi1-out2-copy.txt is missing")
  355. endif()
  356. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi2-real.txt)
  357. if(${BuildDepends_BINARY_DIR}/Project/multi2-real.txt
  358. IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt)
  359. message(STATUS "multi2-real.txt is newer than multi2-stamp.txt")
  360. else()
  361. message(SEND_ERROR "Project did not rebuild properly: "
  362. "multi2-real.txt is not newer than multi2-stamp.txt")
  363. endif()
  364. else()
  365. message(SEND_ERROR "Project did not rebuild properly: "
  366. "multi2-real.txt is missing")
  367. endif()
  368. if(TEST_MULTI3)
  369. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi3-real.txt)
  370. if(${BuildDepends_BINARY_DIR}/Project/multi3-real.txt
  371. IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt)
  372. message(STATUS "multi3-real.txt is newer than multi3-stamp.txt")
  373. else()
  374. message(SEND_ERROR "Project did not rebuild properly: "
  375. "multi3-real.txt is not newer than multi3-stamp.txt")
  376. endif()
  377. else()
  378. message(SEND_ERROR "Project did not rebuild properly: "
  379. "multi3-real.txt is missing")
  380. endif()
  381. endif()