CMakeLists.txt 7.0 KB

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