CMakeLists.txt 7.1 KB

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