RunCMakeTest.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. cmake_minimum_required(VERSION 3.12)
  2. include(RunCMake)
  3. # We do not contact any remote URLs, but may use a local one.
  4. # Remove any proxy configuration that may change behavior.
  5. unset(ENV{http_proxy})
  6. unset(ENV{https_proxy})
  7. if(RunCMake_GENERATOR STREQUAL "Borland Makefiles" OR
  8. RunCMake_GENERATOR STREQUAL "Watcom WMake")
  9. set(fs_delay 3)
  10. else()
  11. set(fs_delay 1.125)
  12. endif()
  13. run_cmake(BadIndependentStep1)
  14. run_cmake(BadIndependentStep2)
  15. run_cmake(TLSVersionBadArg)
  16. run_cmake(TLSVersionBadVar)
  17. run_cmake(TLSVersionBadEnv)
  18. run_cmake(NoOptions)
  19. run_cmake(SourceEmpty)
  20. run_cmake(SourceMissing)
  21. run_cmake(CMAKE_CACHE_ARGS)
  22. run_cmake(CMAKE_CACHE_DEFAULT_ARGS)
  23. run_cmake(CMAKE_CACHE_mix)
  24. if(NOT XCODE_VERSION OR XCODE_VERSION VERSION_LESS 12)
  25. run_cmake(NO_DEPENDS-CMP0114-WARN)
  26. run_cmake(NO_DEPENDS-CMP0114-OLD)
  27. endif()
  28. run_cmake(NO_DEPENDS-CMP0114-NEW)
  29. run_cmake(NO_DEPENDS-CMP0114-NEW-Direct)
  30. run_cmake(Add_StepDependencies)
  31. run_cmake(Add_StepDependencies_iface)
  32. run_cmake(Add_StepDependencies_iface_step)
  33. run_cmake(Add_StepDependencies_no_target)
  34. run_cmake(UsesTerminal)
  35. if(XCODE_VERSION AND XCODE_VERSION VERSION_GREATER_EQUAL 12)
  36. run_cmake(Xcode-CMP0114)
  37. endif()
  38. macro(check_steps_missing proj)
  39. set(steps "${ARGN}")
  40. foreach(step ${steps})
  41. if(EXISTS ${RunCMake_TEST_BINARY_DIR}/${proj}-${step}-mark)
  42. string(APPEND RunCMake_TEST_FAILED "${proj} '${step}' step ran but should not have\n")
  43. endif()
  44. endforeach()
  45. endmacro()
  46. macro(check_steps_present proj)
  47. set(steps "${ARGN}")
  48. foreach(step ${steps})
  49. if(NOT EXISTS ${RunCMake_TEST_BINARY_DIR}/${proj}-${step}-mark)
  50. string(APPEND RunCMake_TEST_FAILED "${proj} '${step}' step did not run but should have\n")
  51. endif()
  52. endforeach()
  53. endmacro()
  54. function(run_steps_CMP0114 val)
  55. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Steps-CMP0114-${val}-build)
  56. run_cmake(Steps-CMP0114-${val})
  57. set(RunCMake_TEST_NO_CLEAN 1)
  58. run_cmake_command(Steps-CMP0114-${val}-build-download ${CMAKE_COMMAND} --build . --target proj1-download)
  59. run_cmake_command(Steps-CMP0114-${val}-build-update ${CMAKE_COMMAND} --build . --target proj1-update)
  60. run_cmake_command(Steps-CMP0114-${val}-build-install ${CMAKE_COMMAND} --build . --target proj1-install)
  61. run_cmake_command(Steps-CMP0114-${val}-build-test ${CMAKE_COMMAND} --build . --target proj1-test)
  62. endfunction()
  63. if(NOT XCODE_VERSION OR XCODE_VERSION VERSION_LESS 12)
  64. run_steps_CMP0114(OLD)
  65. endif()
  66. run_steps_CMP0114(NEW)
  67. function(__ep_test_source_dir_change)
  68. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SourceDirChange-build)
  69. set(RunCMake_TEST_NO_CLEAN 1)
  70. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  71. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  72. run_cmake(SourceDirChange)
  73. run_cmake_command(SourceDirChange-build1 ${CMAKE_COMMAND} --build .)
  74. # Because some file systems have timestamps with only one second resolution,
  75. # we have to ensure we don't re-run the configure stage too quickly after the
  76. # first build. Otherwise, the modified RepositoryInfo.txt files the next
  77. # configure writes might still have the same timestamp as the previous one.
  78. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay})
  79. run_cmake_command(SourceDirChange-change ${CMAKE_COMMAND} -DSOURCE_DIR_CHANGE=YES .)
  80. run_cmake_command(SourceDirChange-build2 ${CMAKE_COMMAND} --build .)
  81. endfunction()
  82. __ep_test_source_dir_change()
  83. # Run both cmake and build steps. We always do a clean before the
  84. # build to ensure that the download step re-runs each time.
  85. function(__ep_test_with_build testName)
  86. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${testName}-build)
  87. set(RunCMake_TEST_NO_CLEAN 1)
  88. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  89. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  90. run_cmake(${testName})
  91. run_cmake_command(${testName}-clean ${CMAKE_COMMAND} --build . --target clean)
  92. run_cmake_command(${testName}-build ${CMAKE_COMMAND} --build .)
  93. endfunction()
  94. find_package(Python3)
  95. function(__ep_test_with_build_with_server testName)
  96. if(NOT Python3_EXECUTABLE)
  97. return()
  98. endif()
  99. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${testName}-build)
  100. set(RunCMake_TEST_NO_CLEAN 1)
  101. set(RunCMake_TEST_TIMEOUT 20)
  102. set(RunCMake_TEST_OUTPUT_MERGE TRUE)
  103. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  104. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  105. set(URL_FILE ${RunCMake_BINARY_DIR}/${testName}.url)
  106. if(EXISTS "${URL_FILE}")
  107. file(REMOVE "${URL_FILE}")
  108. endif()
  109. if(NOT DOWNLOAD_SERVER_TIMEOUT)
  110. set(DOWNLOAD_SERVER_TIMEOUT 30)
  111. endif()
  112. execute_process(
  113. COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/DownloadServer.py --file "${URL_FILE}" ${ARGN}
  114. OUTPUT_FILE ${RunCMake_BINARY_DIR}/${testName}-python.txt
  115. ERROR_FILE ${RunCMake_BINARY_DIR}/${testName}-python.txt
  116. RESULT_VARIABLE result
  117. TIMEOUT "${DOWNLOAD_SERVER_TIMEOUT}"
  118. )
  119. if(NOT result EQUAL 0)
  120. message(FATAL_ERROR "Failed to start download server:\n ${result}")
  121. endif()
  122. foreach(i RANGE 1 8)
  123. if(EXISTS ${URL_FILE})
  124. break()
  125. endif()
  126. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${i})
  127. endforeach()
  128. if(NOT EXISTS ${URL_FILE})
  129. message(FATAL_ERROR "Failed to load download server URL from:\n ${URL_FILE}")
  130. endif()
  131. file(READ ${URL_FILE} SERVER_URL)
  132. message(STATUS "URL : ${URL_FILE} - ${SERVER_URL}")
  133. run_cmake_with_options(${testName} -DSERVER_URL=${SERVER_URL})
  134. run_cmake_command(${testName}-clean ${CMAKE_COMMAND} --build . --target clean)
  135. run_cmake_command(${testName}-build ${CMAKE_COMMAND} --build .)
  136. endfunction()
  137. if(RunCMake_GENERATOR MATCHES "(MSYS|MinGW|Unix) Makefiles")
  138. __ep_test_with_build(GNUMakeJobServerAware)
  139. endif()
  140. function(__ep_test_jobserver)
  141. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/DetectJobServer-build)
  142. set(RunCMake_TEST_NO_CLEAN 1)
  143. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  144. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  145. run_cmake_with_options(DetectJobServer -DDETECT_JOBSERVER=${DETECT_JOBSERVER})
  146. run_cmake_command(DetectJobServer-clean ${CMAKE_COMMAND} --build . --target clean)
  147. run_cmake_command(DetectJobServer-build ${CMAKE_COMMAND} --build . -j4)
  148. endfunction()
  149. if(RunCMake_GENERATOR MATCHES "(MinGW|Unix) Makefiles")
  150. __ep_test_jobserver()
  151. endif()
  152. __ep_test_with_build(MultiCommand)
  153. set(RunCMake_TEST_OUTPUT_MERGE 1)
  154. __ep_test_with_build(InstallBuilds)
  155. __ep_test_with_build(PreserveEmptyArgs)
  156. set(RunCMake_TEST_OUTPUT_MERGE 0)
  157. # Output is not predictable enough to be able to verify it reliably
  158. # when using the various different Visual Studio generators
  159. if(NOT RunCMake_GENERATOR MATCHES "Visual Studio")
  160. __ep_test_with_build(LogOutputOnFailure)
  161. __ep_test_with_build(LogOutputOnFailureMerged)
  162. __ep_test_with_build(DownloadTimeout)
  163. __ep_test_with_build_with_server(DownloadInactivityTimeout --speed_limit --limit_duration 40)
  164. __ep_test_with_build_with_server(DownloadInactivityResume --speed_limit --limit_duration 1)
  165. endif()
  166. # We can't test the substitution when using the old MSYS due to
  167. # make/sh mangling the paths (substitution is performed correctly,
  168. # but the mangling means we can't reliably test the output).
  169. # There is no such issue when using the newer MSYS though. Therefore,
  170. # we need to bypass the substitution test if using old MSYS.
  171. # See merge request 1537 for discussion.
  172. set(doSubstitutionTest YES)
  173. if(RunCMake_GENERATOR STREQUAL "MSYS Makefiles")
  174. execute_process(COMMAND uname OUTPUT_VARIABLE uname)
  175. if(uname MATCHES "^MINGW32_NT")
  176. set(doSubstitutionTest NO)
  177. endif()
  178. endif()
  179. if(doSubstitutionTest)
  180. __ep_test_with_build(Substitutions)
  181. endif()
  182. function(__ep_test_BUILD_ALWAYS)
  183. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/BUILD_ALWAYS-build)
  184. run_cmake(BUILD_ALWAYS)
  185. set(RunCMake_TEST_NO_CLEAN 1)
  186. file(WRITE "${RunCMake_TEST_BINARY_DIR}/once-configure.cmake" [[message(STATUS "once: configure")]])
  187. file(WRITE "${RunCMake_TEST_BINARY_DIR}/once-build.cmake" [[message(STATUS "once: build")]])
  188. file(WRITE "${RunCMake_TEST_BINARY_DIR}/once-install.cmake" [[message(STATUS "once: install")]])
  189. file(WRITE "${RunCMake_TEST_BINARY_DIR}/always-configure.cmake" [[message(STATUS "always: configure")]])
  190. file(WRITE "${RunCMake_TEST_BINARY_DIR}/always-build.cmake" [[message(STATUS "always: build")]])
  191. file(WRITE "${RunCMake_TEST_BINARY_DIR}/always-install.cmake" [[message(STATUS "always: install")]])
  192. run_cmake_command(BUILD_ALWAYS-build1 ${CMAKE_COMMAND} --build . --target always)
  193. file(WRITE "${RunCMake_TEST_BINARY_DIR}/once-configure.cmake" [[message(FATAL_ERROR "once: configure should not run again")]])
  194. file(WRITE "${RunCMake_TEST_BINARY_DIR}/once-build.cmake" [[message(FATAL_ERROR "once: build should not run again")]])
  195. file(WRITE "${RunCMake_TEST_BINARY_DIR}/once-install.cmake" [[message(FATAL_ERROR "once: install should not run again")]])
  196. if(NOT RunCMake_GENERATOR STREQUAL "Xcode")
  197. # The Xcode and VS 9 build systems decide to run this every time.
  198. file(WRITE "${RunCMake_TEST_BINARY_DIR}/always-configure.cmake" [[message(FATAL_ERROR "always: configure should not run again")]])
  199. endif()
  200. run_cmake_command(BUILD_ALWAYS-build2 ${CMAKE_COMMAND} --build . --target always)
  201. endfunction()
  202. __ep_test_BUILD_ALWAYS()
  203. function(__ep_test_CONFIGURE_HANDLED_BY_BUILD)
  204. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CONFIGURE_HANDLED_BY_BUILD-build)
  205. run_cmake(CONFIGURE_HANDLED_BY_BUILD)
  206. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  207. set(BUILD_CONFIG --config Debug)
  208. set(STAMP_DIR "${RunCMake_TEST_BINARY_DIR}/stamp/Debug")
  209. else()
  210. set(BUILD_CONFIG "")
  211. set(STAMP_DIR "${RunCMake_TEST_BINARY_DIR}/stamp")
  212. endif()
  213. set(RunCMake_TEST_NO_CLEAN 1)
  214. run_cmake_command(CONFIGURE_HANDLED_BY_BUILD-build ${CMAKE_COMMAND} --build . ${BUILD_CONFIG})
  215. # Calculate timestamps before rebuilding so we can compare before and after in
  216. # CONFIGURE_HANDLED_BY_BUILD-rebuild-check.cmake
  217. file(TIMESTAMP "${STAMP_DIR}/proj1-configure" PROJ1_CONFIGURE_TIMESTAMP_BEFORE "%s")
  218. # When BUILD_ALWAYS is set, the build stamp is never created.
  219. file(TIMESTAMP "${STAMP_DIR}/proj2-configure" PROJ2_CONFIGURE_TIMESTAMP_BEFORE "%s")
  220. file(TIMESTAMP "${STAMP_DIR}/proj2-build" PROJ2_BUILD_TIMESTAMP_BEFORE "%s")
  221. run_cmake_command(CONFIGURE_HANDLED_BY_BUILD-rebuild ${CMAKE_COMMAND} --build . ${BUILD_CONFIG})
  222. endfunction()
  223. __ep_test_CONFIGURE_HANDLED_BY_BUILD()
  224. find_package(Git QUIET)
  225. if(GIT_EXECUTABLE)
  226. # Note that there appear to be differences in where git writes its output to
  227. # on some platforms. It may go to stdout or stderr, so force it to be merged.
  228. set(RunCMake_TEST_OUTPUT_MERGE TRUE)
  229. run_cmake(FetchGitRefs)
  230. set(RunCMake_TEST_OUTPUT_MERGE FALSE)
  231. endif()