CMakeLists.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. # 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. file(MAKE_DIRECTORY ${BuildDepends_BINARY_DIR}/Project)
  39. message("Creating Project/foo.cxx")
  40. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  41. "const char* foo() { return \"foo\";}" )
  42. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  43. "static const char* zot = \"zot\";\n")
  44. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  45. "static const char* zot_custom = \"zot_custom\";\n")
  46. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  47. "static const char* zot_macro_dir = \"zot_macro_dir\";\n")
  48. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  49. "static const char* zot_macro_tgt = \"zot_macro_tgt\";\n")
  50. help_xcode_depends()
  51. message("Building project first time")
  52. try_compile(RESULT
  53. ${BuildDepends_BINARY_DIR}/Project
  54. ${BuildDepends_SOURCE_DIR}/Project
  55. testRebuild
  56. CMAKE_FLAGS ${_cmake_options}
  57. OUTPUT_VARIABLE OUTPUT)
  58. if(HELP_XCODE)
  59. try_compile(RESULT
  60. ${BuildDepends_BINARY_DIR}/Project
  61. ${BuildDepends_SOURCE_DIR}/Project
  62. testRebuild
  63. OUTPUT_VARIABLE OUTPUT)
  64. try_compile(RESULT
  65. ${BuildDepends_BINARY_DIR}/Project
  66. ${BuildDepends_SOURCE_DIR}/Project
  67. testRebuild
  68. OUTPUT_VARIABLE OUTPUT)
  69. endif(HELP_XCODE)
  70. message("Output from first build:\n${OUTPUT}")
  71. if(NOT RESULT)
  72. message(SEND_ERROR "Could not build test project (1)!")
  73. endif(NOT RESULT)
  74. set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
  75. if(EXISTS
  76. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  77. message("found debug")
  78. set(bar
  79. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  80. endif(EXISTS
  81. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  82. set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
  83. if(EXISTS
  84. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  85. message("found debug")
  86. set(zot
  87. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  88. endif(EXISTS
  89. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  90. message("Running ${bar} ")
  91. execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  92. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  93. message("Run result: ${runResult} Output: \"${out}\"")
  94. if("${out}" STREQUAL "foo ")
  95. message("Worked!")
  96. else("${out}" STREQUAL "foo ")
  97. message(SEND_ERROR "Project did not initially build properly: ${out}")
  98. endif("${out}" STREQUAL "foo ")
  99. message("Running ${zot} ")
  100. execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
  101. string(REGEX REPLACE "[\r\n]" " " out "${out}")
  102. message("Run result: ${runResult} Output: \"${out}\"")
  103. set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] ")
  104. if("${out}" STREQUAL "${VALUE_UNCHANGED}")
  105. message("Worked!")
  106. else("${out}" STREQUAL "${VALUE_UNCHANGED}")
  107. message(SEND_ERROR "Project did not initially build properly: ${out}")
  108. endif("${out}" STREQUAL "${VALUE_UNCHANGED}")
  109. message("Waiting 3 seconds...")
  110. # any additional argument will cause ${bar} to wait forever
  111. execute_process(COMMAND ${bar} -infinite TIMEOUT 3 OUTPUT_VARIABLE out)
  112. message("Modifying Project/foo.cxx")
  113. write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
  114. "const char* foo() { return \"foo changed\";}" )
  115. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  116. "static const char* zot = \"zot changed\";\n")
  117. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  118. "static const char* zot_custom = \"zot_custom changed\";\n")
  119. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  120. "static const char* zot_macro_dir = \"zot_macro_dir changed\";\n")
  121. file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  122. "static const char* zot_macro_tgt = \"zot_macro_tgt changed\";\n")
  123. if(TEST_LINK_DEPENDS)
  124. file(WRITE ${TEST_LINK_DEPENDS} "2")
  125. endif()
  126. help_xcode_depends()
  127. message("Building project second time")
  128. try_compile(RESULT
  129. ${BuildDepends_BINARY_DIR}/Project
  130. ${BuildDepends_SOURCE_DIR}/Project
  131. testRebuild
  132. CMAKE_FLAGS ${_cmake_options}
  133. OUTPUT_VARIABLE OUTPUT)
  134. # Xcode is in serious need of help here
  135. if(HELP_XCODE)
  136. try_compile(RESULT
  137. ${BuildDepends_BINARY_DIR}/Project
  138. ${BuildDepends_SOURCE_DIR}/Project
  139. testRebuild
  140. OUTPUT_VARIABLE OUTPUT)
  141. try_compile(RESULT
  142. ${BuildDepends_BINARY_DIR}/Project
  143. ${BuildDepends_SOURCE_DIR}/Project
  144. testRebuild
  145. OUTPUT_VARIABLE OUTPUT)
  146. endif(HELP_XCODE)
  147. message("Output from second build:\n${OUTPUT}")
  148. if(NOT RESULT)
  149. message(SEND_ERROR "Could not build test project (2)!")
  150. endif(NOT RESULT)
  151. if(EXISTS
  152. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  153. message("found debug")
  154. endif(EXISTS
  155. "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
  156. if(EXISTS
  157. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  158. message("found debug")
  159. endif(EXISTS
  160. "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
  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("${out}" STREQUAL "foo changed ")
  168. message(SEND_ERROR "Project did not rebuild properly!")
  169. endif("${out}" STREQUAL "foo changed ")
  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("${out}" STREQUAL "${VALUE_CHANGED}")
  180. message(SEND_ERROR "Project did not rebuild properly!")
  181. endif("${out}" STREQUAL "${VALUE_CHANGED}")
  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()