CMakeLists.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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("${CMAKE_GENERATOR}" MATCHES "Xcode")
  15. set(HELP_XCODE 1)
  16. endif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  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(MACRO_OBJS)
  26. endif(HELP_XCODE)
  27. endfunction(help_xcode_depends)
  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/zot_custom.hxx.in
  49. "static const char* zot_custom = \"zot_custom\";\n")
  50. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  51. "static const char* zot_macro_dir = \"zot_macro_dir\";\n")
  52. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  53. "static const char* zot_macro_tgt = \"zot_macro_tgt\";\n")
  54. help_xcode_depends()
  55. message("Building project first time")
  56. try_compile(RESULT
  57. ${BuildDepends_BINARY_DIR}/Project
  58. ${BuildDepends_SOURCE_DIR}/Project
  59. testRebuild
  60. CMAKE_FLAGS ${_cmake_options}
  61. OUTPUT_VARIABLE OUTPUT)
  62. if(HELP_XCODE)
  63. try_compile(RESULT
  64. ${BuildDepends_BINARY_DIR}/Project
  65. ${BuildDepends_SOURCE_DIR}/Project
  66. testRebuild
  67. OUTPUT_VARIABLE OUTPUT)
  68. try_compile(RESULT
  69. ${BuildDepends_BINARY_DIR}/Project
  70. ${BuildDepends_SOURCE_DIR}/Project
  71. testRebuild
  72. OUTPUT_VARIABLE OUTPUT)
  73. endif(HELP_XCODE)
  74. message("Output from first build:\n${OUTPUT}")
  75. if(NOT RESULT)
  76. message(SEND_ERROR "Could not build test project (1)!")
  77. endif(NOT RESULT)
  78. set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
  79. if(EXISTS
  80. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  81. message("found debug")
  82. set(bar
  83. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  84. endif(EXISTS
  85. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  86. set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
  87. if(EXISTS
  88. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  89. message("found debug")
  90. set(zot
  91. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  92. endif(EXISTS
  93. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  94. message("Running ${bar} ")
  95. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  96. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  97. message("Run result: ${runResult} Output: \"${out}\"")
  98. if("${out}" STREQUAL "foo ")
  99. message("Worked!")
  100. else("${out}" STREQUAL "foo ")
  101. message(SEND_ERROR "Project did not initially build properly: ${out}")
  102. endif("${out}" STREQUAL "foo ")
  103. message("Running ${zot} ")
  104. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  105. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  106. message("Run result: ${runResult} Output: \"${out}\"")
  107. set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] ")
  108. if("${out}" STREQUAL "${VALUE_UNCHANGED}")
  109. message("Worked!")
  110. else("${out}" STREQUAL "${VALUE_UNCHANGED}")
  111. message(SEND_ERROR "Project did not initially build properly: ${out}")
  112. endif("${out}" STREQUAL "${VALUE_UNCHANGED}")
  113. message("Waiting 3 seconds...")
  114. # any additional argument will cause ${bar} to wait forever
  115. execute_process(COMMAND ${bar} -infinite TIMEOUT 3 OUTPUT_VARIABLE out)
  116. message("Modifying Project/foo.cxx")
  117. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  118. "const char* foo() { return \"foo changed\";}" )
  119. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  120. "static const char* zot = \"zot changed\";\n")
  121. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  122. "static const char* zot_custom = \"zot_custom changed\";\n")
  123. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  124. "static const char* zot_macro_dir = \"zot_macro_dir changed\";\n")
  125. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  126. "static const char* zot_macro_tgt = \"zot_macro_tgt changed\";\n")
  127. if(TEST_LINK_DEPENDS)
  128. file(WRITE ${TEST_LINK_DEPENDS} "2")
  129. endif()
  130. help_xcode_depends()
  131. message("Building project second time")
  132. try_compile(RESULT
  133. ${BuildDepends_BINARY_DIR}/Project
  134. ${BuildDepends_SOURCE_DIR}/Project
  135. testRebuild
  136. CMAKE_FLAGS ${_cmake_options}
  137. OUTPUT_VARIABLE OUTPUT)
  138. # Xcode is in serious need of help here
  139. if(HELP_XCODE OR HELP_NINJA)
  140. try_compile(RESULT
  141. ${BuildDepends_BINARY_DIR}/Project
  142. ${BuildDepends_SOURCE_DIR}/Project
  143. testRebuild
  144. OUTPUT_VARIABLE OUTPUT)
  145. try_compile(RESULT
  146. ${BuildDepends_BINARY_DIR}/Project
  147. ${BuildDepends_SOURCE_DIR}/Project
  148. testRebuild
  149. OUTPUT_VARIABLE OUTPUT)
  150. endif()
  151. message("Output from second build:\n${OUTPUT}")
  152. if(NOT RESULT)
  153. message(SEND_ERROR "Could not build test project (2)!")
  154. endif(NOT RESULT)
  155. if(EXISTS
  156. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  157. message("found debug")
  158. endif(EXISTS
  159. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  160. if(EXISTS
  161. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  162. message("found debug")
  163. endif(EXISTS
  164. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  165. message("Running ${bar} ")
  166. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  167. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  168. message("Run result: ${runResult} Output: \"${out}\"")
  169. if("${out}" STREQUAL "foo changed ")
  170. message("Worked!")
  171. else("${out}" STREQUAL "foo changed ")
  172. message(SEND_ERROR "Project did not rebuild properly!")
  173. endif("${out}" STREQUAL "foo changed ")
  174. message("Running ${zot} ")
  175. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  176. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  177. message("Run result: ${runResult} Output: \"${out}\"")
  178. set(VALUE_CHANGED
  179. "[zot changed] [zot_custom changed] [zot_macro_dir changed] [zot_macro_tgt changed] "
  180. )
  181. if("${out}" STREQUAL "${VALUE_CHANGED}")
  182. message("Worked!")
  183. else("${out}" STREQUAL "${VALUE_CHANGED}")
  184. message(SEND_ERROR "Project did not rebuild properly!")
  185. endif("${out}" STREQUAL "${VALUE_CHANGED}")
  186. if(TEST_LINK_DEPENDS)
  187. set(linkdep ${BuildDepends_BINARY_DIR}/Project/linkdep${CMAKE_EXECUTABLE_SUFFIX})
  188. if(${linkdep} IS_NEWER_THAN ${TEST_LINK_DEPENDS})
  189. message("LINK_DEPENDS worked")
  190. else()
  191. message(SEND_ERROR "LINK_DEPENDS failed. Executable
  192. ${linkdep}
  193. is not newer than dependency
  194. ${TEST_LINK_DEPENDS}
  195. ")
  196. endif()
  197. endif()