CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. if("${CMAKE_GENERATOR}" MATCHES "Ninja")
  29. set(HELP_NINJA 1) # TODO Why is this needed?
  30. endif()
  31. # The Intel compiler causes the MSVC linker to crash during
  32. # incremental linking, so avoid the /INCREMENTAL:YES flag.
  33. if(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
  34. set(_cmake_options "-DCMAKE_EXE_LINKER_FLAGS=")
  35. endif()
  36. if("${CMAKE_GENERATOR}" MATCHES "Make")
  37. set(TEST_LINK_DEPENDS ${BuildDepends_BINARY_DIR}/Project/linkdep.txt)
  38. file(WRITE ${TEST_LINK_DEPENDS} "1")
  39. endif()
  40. list(APPEND _cmake_options "-DTEST_LINK_DEPENDS=${TEST_LINK_DEPENDS}")
  41. list(APPEND _cmake_options "-DCMAKE_FORCE_DEPFILES=1")
  42. file(MAKE_DIRECTORY ${BuildDepends_BINARY_DIR}/Project)
  43. message("Creating Project/foo.cxx")
  44. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  45. "const char* foo() { return \"foo\";}" )
  46. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  47. "static const char* zot = \"zot\";\n")
  48. file(WRITE ${BuildDepends_BINARY_DIR}/Project/dir/header.txt
  49. "#define HEADER_STRING \"ninja\"\n" )
  50. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  51. "static const char* zot_custom = \"zot_custom\";\n")
  52. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  53. "static const char* zot_macro_dir = \"zot_macro_dir\";\n")
  54. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  55. "static const char* zot_macro_tgt = \"zot_macro_tgt\";\n")
  56. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  57. "#define link_depends_no_shared_lib_value 1\n")
  58. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_exe.h
  59. "#define link_depends_no_shared_exe_value 0\n")
  60. set(link_depends_no_shared_check_txt ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_check.txt)
  61. help_xcode_depends()
  62. message("Building project first time")
  63. try_compile(RESULT
  64. ${BuildDepends_BINARY_DIR}/Project
  65. ${BuildDepends_SOURCE_DIR}/Project
  66. testRebuild
  67. CMAKE_FLAGS ${_cmake_options}
  68. OUTPUT_VARIABLE OUTPUT)
  69. if(HELP_XCODE)
  70. try_compile(RESULT
  71. ${BuildDepends_BINARY_DIR}/Project
  72. ${BuildDepends_SOURCE_DIR}/Project
  73. testRebuild
  74. OUTPUT_VARIABLE OUTPUT)
  75. try_compile(RESULT
  76. ${BuildDepends_BINARY_DIR}/Project
  77. ${BuildDepends_SOURCE_DIR}/Project
  78. testRebuild
  79. OUTPUT_VARIABLE OUTPUT)
  80. endif()
  81. message("Output from first build:\n${OUTPUT}")
  82. if(NOT RESULT)
  83. message(SEND_ERROR "Could not build test project (1)!")
  84. endif()
  85. # find and save the ninjadep executable
  86. set(ninjadep ${BuildDepends_BINARY_DIR}/Project/ninjadep${CMAKE_EXECUTABLE_SUFFIX})
  87. if(EXISTS
  88. "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}" )
  89. message("found debug")
  90. set(ninjadep
  91. "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}")
  92. endif()
  93. message("Running ${ninjadep} ")
  94. execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  95. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  96. message("Run result: ${runResult} Output: \"${out}\"")
  97. if("${out}" STREQUAL "HEADER_STRING: ninja ")
  98. message("Worked!")
  99. else()
  100. message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
  101. " expected [HEADER_STRING: ninja]")
  102. endif()
  103. set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
  104. if(EXISTS
  105. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  106. message("found debug")
  107. set(bar
  108. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  109. endif()
  110. set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
  111. if(EXISTS
  112. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  113. message("found debug")
  114. set(zot
  115. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  116. endif()
  117. message("Running ${bar} ")
  118. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  119. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  120. message("Run result: ${runResult} Output: \"${out}\"")
  121. if("${out}" STREQUAL "foo ")
  122. message("Worked!")
  123. else()
  124. message(SEND_ERROR "Project did not initially build properly: ${out}")
  125. endif()
  126. message("Running ${zot} ")
  127. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  128. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  129. message("Run result: ${runResult} Output: \"${out}\"")
  130. set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] ")
  131. if("${out}" STREQUAL "${VALUE_UNCHANGED}")
  132. message("Worked!")
  133. else()
  134. message(SEND_ERROR "Project did not initially build properly: ${out}")
  135. endif()
  136. if(EXISTS "${link_depends_no_shared_check_txt}")
  137. file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  138. if("${link_depends_no_shared_check}" STREQUAL "1")
  139. message(STATUS "link_depends_no_shared_exe is newer than link_depends_no_shared_lib as expected.")
  140. else()
  141. message(SEND_ERROR "Project did not initially build properly: "
  142. "link_depends_no_shared_exe is older than link_depends_no_shared_lib.")
  143. endif()
  144. else()
  145. message(SEND_ERROR "Project did not initially build properly: "
  146. "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
  147. endif()
  148. message("Waiting 3 seconds...")
  149. # any additional argument will cause ${bar} to wait forever
  150. execute_process(COMMAND ${bar} -infinite TIMEOUT 3 OUTPUT_VARIABLE out)
  151. message("Modifying Project/foo.cxx")
  152. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  153. "const char* foo() { return \"foo changed\";}" )
  154. file(WRITE "${BuildDepends_BINARY_DIR}/Project/dir/header.txt"
  155. "#define HEADER_STRING \"ninja changed\"\n" )
  156. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  157. "static const char* zot = \"zot changed\";\n")
  158. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  159. "static const char* zot_custom = \"zot_custom changed\";\n")
  160. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  161. "static const char* zot_macro_dir = \"zot_macro_dir changed\";\n")
  162. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  163. "static const char* zot_macro_tgt = \"zot_macro_tgt changed\";\n")
  164. file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  165. "#define link_depends_no_shared_lib_value 0\n")
  166. if(TEST_LINK_DEPENDS)
  167. file(WRITE ${TEST_LINK_DEPENDS} "2")
  168. endif()
  169. help_xcode_depends()
  170. message("Building project second time")
  171. try_compile(RESULT
  172. ${BuildDepends_BINARY_DIR}/Project
  173. ${BuildDepends_SOURCE_DIR}/Project
  174. testRebuild
  175. CMAKE_FLAGS ${_cmake_options}
  176. OUTPUT_VARIABLE OUTPUT)
  177. # Xcode is in serious need of help here
  178. if(HELP_XCODE OR HELP_NINJA)
  179. try_compile(RESULT
  180. ${BuildDepends_BINARY_DIR}/Project
  181. ${BuildDepends_SOURCE_DIR}/Project
  182. testRebuild
  183. OUTPUT_VARIABLE OUTPUT)
  184. try_compile(RESULT
  185. ${BuildDepends_BINARY_DIR}/Project
  186. ${BuildDepends_SOURCE_DIR}/Project
  187. testRebuild
  188. OUTPUT_VARIABLE OUTPUT)
  189. endif()
  190. message("Output from second build:\n${OUTPUT}")
  191. if(NOT RESULT)
  192. message(SEND_ERROR "Could not build test project (2)!")
  193. endif()
  194. if(EXISTS
  195. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  196. message("found debug")
  197. endif()
  198. if(EXISTS
  199. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  200. message("found debug")
  201. endif()
  202. message("Running ${ninjadep} ")
  203. execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  204. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  205. message("Run result: ${runResult} Output: \"${out}\"")
  206. if("${out}" STREQUAL "HEADER_STRING: ninja changed ")
  207. message("Worked!")
  208. elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 6")
  209. # Tolerate failure because VS 6 does not seem to recompile ninjadep.cpp
  210. # when the "dir/header.h" it includes changes.
  211. else()
  212. message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
  213. " expected [HEADER_STRING: ninja changed]")
  214. endif()
  215. message("Running ${bar} ")
  216. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  217. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  218. message("Run result: ${runResult} Output: \"${out}\"")
  219. if("${out}" STREQUAL "foo changed ")
  220. message("Worked!")
  221. else()
  222. message(SEND_ERROR "Project did not rebuild properly!")
  223. endif()
  224. message("Running ${zot} ")
  225. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  226. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  227. message("Run result: ${runResult} Output: \"${out}\"")
  228. set(VALUE_CHANGED
  229. "[zot changed] [zot_custom changed] [zot_macro_dir changed] [zot_macro_tgt changed] "
  230. )
  231. if("${out}" STREQUAL "${VALUE_CHANGED}")
  232. message("Worked!")
  233. else()
  234. message(SEND_ERROR "Project did not rebuild properly!")
  235. endif()
  236. if(TEST_LINK_DEPENDS)
  237. set(linkdep ${BuildDepends_BINARY_DIR}/Project/linkdep${CMAKE_EXECUTABLE_SUFFIX})
  238. if(${linkdep} IS_NEWER_THAN ${TEST_LINK_DEPENDS})
  239. message("LINK_DEPENDS worked")
  240. else()
  241. message(SEND_ERROR "LINK_DEPENDS failed. Executable
  242. ${linkdep}
  243. is not newer than dependency
  244. ${TEST_LINK_DEPENDS}
  245. ")
  246. endif()
  247. endif()
  248. if(EXISTS "${link_depends_no_shared_check_txt}")
  249. file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  250. if("${link_depends_no_shared_check}" STREQUAL "0")
  251. message(STATUS "link_depends_no_shared_exe is older than link_depends_no_shared_lib as expected.")
  252. elseif(XCODE AND NOT XCODE_VERSION VERSION_LESS 5)
  253. 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.")
  254. else()
  255. message(SEND_ERROR "Project did not rebuild properly: link_depends_no_shared_exe is newer than link_depends_no_shared_lib.")
  256. endif()
  257. else()
  258. message(SEND_ERROR "Project did not rebuild properly. "
  259. "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
  260. endif()