CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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")
  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. file(MAKE_DIRECTORY ${BuildDepends_BINARY_DIR}/Project)
  40. message("Creating Project/foo.cxx")
  41. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  42. "const char* foo() { return \"foo\";}" )
  43. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  44. "static const char* zot = \"zot\";\n")
  45. file(WRITE ${BuildDepends_BINARY_DIR}/Project/dir/header.txt
  46. "#define HEADER_STRING \"ninja\"\n" )
  47. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  48. "static const char* zot_custom = \"zot_custom\";\n")
  49. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  50. "static const char* zot_macro_dir = \"zot_macro_dir\";\n")
  51. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  52. "static const char* zot_macro_tgt = \"zot_macro_tgt\";\n")
  53. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  54. "#define link_depends_no_shared_lib_value 1\n")
  55. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_exe.h
  56. "#define link_depends_no_shared_exe_value 0\n")
  57. set(link_depends_no_shared_check_txt ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_check.txt)
  58. file(WRITE ${BuildDepends_BINARY_DIR}/Project/external.in "external original\n")
  59. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi1-in.txt "multi1-in original\n")
  60. help_xcode_depends()
  61. message("Building project first time")
  62. try_compile(RESULT
  63. ${BuildDepends_BINARY_DIR}/Project
  64. ${BuildDepends_SOURCE_DIR}/Project
  65. testRebuild
  66. CMAKE_FLAGS ${_cmake_options}
  67. OUTPUT_VARIABLE OUTPUT)
  68. if(HELP_XCODE)
  69. try_compile(RESULT
  70. ${BuildDepends_BINARY_DIR}/Project
  71. ${BuildDepends_SOURCE_DIR}/Project
  72. testRebuild
  73. OUTPUT_VARIABLE OUTPUT)
  74. try_compile(RESULT
  75. ${BuildDepends_BINARY_DIR}/Project
  76. ${BuildDepends_SOURCE_DIR}/Project
  77. testRebuild
  78. OUTPUT_VARIABLE OUTPUT)
  79. endif()
  80. message("Output from first build:\n${OUTPUT}")
  81. if(NOT RESULT)
  82. message(SEND_ERROR "Could not build test project (1)!")
  83. endif()
  84. # find and save the ninjadep executable
  85. set(ninjadep ${BuildDepends_BINARY_DIR}/Project/ninjadep${CMAKE_EXECUTABLE_SUFFIX})
  86. if(EXISTS
  87. "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}" )
  88. message("found debug")
  89. set(ninjadep
  90. "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}")
  91. endif()
  92. message("Running ${ninjadep} ")
  93. execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  94. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  95. message("Run result: ${runResult} Output: \"${out}\"")
  96. if("${out}" STREQUAL "HEADER_STRING: ninja ")
  97. message("Worked!")
  98. else()
  99. message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
  100. " expected [HEADER_STRING: ninja]")
  101. endif()
  102. set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
  103. if(EXISTS
  104. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  105. message("found debug")
  106. set(bar
  107. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  108. endif()
  109. set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
  110. if(EXISTS
  111. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  112. message("found debug")
  113. set(zot
  114. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  115. endif()
  116. message("Running ${bar} ")
  117. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  118. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  119. message("Run result: ${runResult} Output: \"${out}\"")
  120. if("${out}" STREQUAL "foo ")
  121. message("Worked!")
  122. else()
  123. message(SEND_ERROR "Project did not initially build properly: ${out}")
  124. endif()
  125. message("Running ${zot} ")
  126. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  127. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  128. message("Run result: ${runResult} Output: \"${out}\"")
  129. set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] ")
  130. if("${out}" STREQUAL "${VALUE_UNCHANGED}")
  131. message("Worked!")
  132. else()
  133. message(SEND_ERROR "Project did not initially build properly: ${out}")
  134. endif()
  135. if(EXISTS "${link_depends_no_shared_check_txt}")
  136. file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  137. if("${link_depends_no_shared_check}" STREQUAL "1")
  138. message(STATUS "link_depends_no_shared_exe is newer than link_depends_no_shared_lib as expected.")
  139. else()
  140. message(SEND_ERROR "Project did not initially build properly: "
  141. "link_depends_no_shared_exe is older than link_depends_no_shared_lib.")
  142. endif()
  143. else()
  144. message(SEND_ERROR "Project did not initially build properly: "
  145. "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
  146. endif()
  147. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/external.out)
  148. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/external.out external_out)
  149. if("${external_out}" STREQUAL "external original")
  150. message(STATUS "external.out contains '${external_out}'")
  151. else()
  152. message(SEND_ERROR "Project did not initially build properly: "
  153. "external.out contains '${external_out}'")
  154. endif()
  155. else()
  156. message(SEND_ERROR "Project did not initially build properly: "
  157. "external.out is missing")
  158. endif()
  159. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt)
  160. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt multi1_out)
  161. if("${multi1_out}" STREQUAL "multi1-in original")
  162. message(STATUS "multi1-out2-copy.txt contains '${multi1_out}'")
  163. else()
  164. message(SEND_ERROR "Project did not initially build properly: "
  165. "multi1-out2-copy.txt contains '${multi1_out}'")
  166. endif()
  167. else()
  168. message(SEND_ERROR "Project did not initially build properly: "
  169. "multi1-out2-copy.txt is missing")
  170. endif()
  171. message("Waiting 3 seconds...")
  172. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 3)
  173. message("Modifying Project/foo.cxx")
  174. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  175. "const char* foo() { return \"foo changed\";}" )
  176. file(WRITE "${BuildDepends_BINARY_DIR}/Project/dir/header.txt"
  177. "#define HEADER_STRING \"ninja changed\"\n" )
  178. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  179. "static const char* zot = \"zot changed\";\n")
  180. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  181. "static const char* zot_custom = \"zot_custom changed\";\n")
  182. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  183. "static const char* zot_macro_dir = \"zot_macro_dir changed\";\n")
  184. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  185. "static const char* zot_macro_tgt = \"zot_macro_tgt changed\";\n")
  186. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  187. "#define link_depends_no_shared_lib_value 0\n")
  188. if(TEST_LINK_DEPENDS)
  189. file(WRITE ${TEST_LINK_DEPENDS} "2")
  190. endif()
  191. file(WRITE ${BuildDepends_BINARY_DIR}/Project/external.in "external changed\n")
  192. file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi1-in.txt "multi1-in changed\n")
  193. help_xcode_depends()
  194. message("Building project second time")
  195. try_compile(RESULT
  196. ${BuildDepends_BINARY_DIR}/Project
  197. ${BuildDepends_SOURCE_DIR}/Project
  198. testRebuild
  199. CMAKE_FLAGS ${_cmake_options}
  200. OUTPUT_VARIABLE OUTPUT)
  201. # Xcode is in serious need of help here
  202. if(HELP_XCODE)
  203. try_compile(RESULT
  204. ${BuildDepends_BINARY_DIR}/Project
  205. ${BuildDepends_SOURCE_DIR}/Project
  206. testRebuild
  207. OUTPUT_VARIABLE OUTPUT)
  208. try_compile(RESULT
  209. ${BuildDepends_BINARY_DIR}/Project
  210. ${BuildDepends_SOURCE_DIR}/Project
  211. testRebuild
  212. OUTPUT_VARIABLE OUTPUT)
  213. endif()
  214. message("Output from second build:\n${OUTPUT}")
  215. if(NOT RESULT)
  216. message(SEND_ERROR "Could not build test project (2)!")
  217. endif()
  218. if(EXISTS
  219. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  220. message("found debug")
  221. endif()
  222. if(EXISTS
  223. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  224. message("found debug")
  225. endif()
  226. message("Running ${ninjadep} ")
  227. execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  228. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  229. message("Run result: ${runResult} Output: \"${out}\"")
  230. if("${out}" STREQUAL "HEADER_STRING: ninja changed ")
  231. message("Worked!")
  232. elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 6")
  233. # Tolerate failure because VS 6 does not seem to recompile ninjadep.cpp
  234. # when the "dir/header.h" it includes changes.
  235. else()
  236. message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
  237. " expected [HEADER_STRING: ninja changed]")
  238. endif()
  239. message("Running ${bar} ")
  240. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  241. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  242. message("Run result: ${runResult} Output: \"${out}\"")
  243. if("${out}" STREQUAL "foo changed ")
  244. message("Worked!")
  245. else()
  246. message(SEND_ERROR "Project did not rebuild properly!")
  247. endif()
  248. message("Running ${zot} ")
  249. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  250. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  251. message("Run result: ${runResult} Output: \"${out}\"")
  252. set(VALUE_CHANGED
  253. "[zot changed] [zot_custom changed] [zot_macro_dir changed] [zot_macro_tgt changed] "
  254. )
  255. if("${out}" STREQUAL "${VALUE_CHANGED}")
  256. message("Worked!")
  257. else()
  258. message(SEND_ERROR "Project did not rebuild properly!")
  259. endif()
  260. if(TEST_LINK_DEPENDS)
  261. set(linkdep ${BuildDepends_BINARY_DIR}/Project/linkdep${CMAKE_EXECUTABLE_SUFFIX})
  262. if(${linkdep} IS_NEWER_THAN ${TEST_LINK_DEPENDS})
  263. message("LINK_DEPENDS worked")
  264. else()
  265. message(SEND_ERROR "LINK_DEPENDS failed. Executable
  266. ${linkdep}
  267. is not newer than dependency
  268. ${TEST_LINK_DEPENDS}
  269. ")
  270. endif()
  271. endif()
  272. if(EXISTS "${link_depends_no_shared_check_txt}")
  273. file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  274. if("${link_depends_no_shared_check}" STREQUAL "0")
  275. message(STATUS "link_depends_no_shared_exe is older than link_depends_no_shared_lib as expected.")
  276. elseif(XCODE AND NOT XCODE_VERSION VERSION_LESS 5)
  277. 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.")
  278. else()
  279. message(SEND_ERROR "Project did not rebuild properly: link_depends_no_shared_exe is newer than link_depends_no_shared_lib.")
  280. endif()
  281. else()
  282. message(SEND_ERROR "Project did not rebuild properly. "
  283. "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
  284. endif()
  285. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/external.out)
  286. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/external.out external_out)
  287. if("${external_out}" STREQUAL "external changed")
  288. message(STATUS "external.out contains '${external_out}'")
  289. else()
  290. message(SEND_ERROR "Project did not rebuild properly: "
  291. "external.out contains '${external_out}'")
  292. endif()
  293. else()
  294. message(SEND_ERROR "Project did not rebuild properly: "
  295. "external.out is missing")
  296. endif()
  297. if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt)
  298. file(STRINGS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt multi1_out)
  299. if("${multi1_out}" STREQUAL "multi1-in changed")
  300. message(STATUS "multi1-out2-copy.txt contains '${multi1_out}'")
  301. else()
  302. message(SEND_ERROR "Project did not rebuild properly: "
  303. "multi1-out2-copy.txt contains '${multi1_out}'")
  304. endif()
  305. else()
  306. message(SEND_ERROR "Project did not rebuild properly: "
  307. "multi1-out2-copy.txt is missing")
  308. endif()