RunCMakeTest.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. include(RunCMake)
  2. if(RunCMake_GENERATOR MATCHES "Ninja")
  3. # Detect ninja version so we know what tests can be supported.
  4. execute_process(
  5. COMMAND "${RunCMake_MAKE_PROGRAM}" --version
  6. OUTPUT_VARIABLE ninja_out
  7. ERROR_VARIABLE ninja_out
  8. RESULT_VARIABLE ninja_res
  9. OUTPUT_STRIP_TRAILING_WHITESPACE
  10. )
  11. if(ninja_res EQUAL 0 AND "x${ninja_out}" MATCHES "^x[0-9]+\\.[0-9]+")
  12. set(ninja_version "${ninja_out}")
  13. message(STATUS "ninja version: ${ninja_version}")
  14. else()
  15. message(FATAL_ERROR "'ninja --version' reported:\n${ninja_out}")
  16. endif()
  17. endif()
  18. if(RunCMake_GENERATOR STREQUAL "Borland Makefiles" OR
  19. RunCMake_GENERATOR STREQUAL "Watcom WMake")
  20. set(fs_delay 3)
  21. else()
  22. set(fs_delay 1.125)
  23. endif()
  24. function(run_BuildDepends CASE)
  25. # Use a single build tree for a few tests without cleaning.
  26. if(NOT RunCMake_TEST_BINARY_DIR)
  27. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
  28. endif()
  29. set(RunCMake_TEST_NO_CLEAN 1)
  30. if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
  31. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  32. endif()
  33. list(APPEND RunCMake_TEST_OPTIONS ${ARGN})
  34. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  35. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  36. include(${RunCMake_SOURCE_DIR}/${CASE}.step1.cmake OPTIONAL)
  37. run_cmake(${CASE})
  38. set(RunCMake-check-file check.cmake)
  39. set(check_step 1)
  40. run_cmake_command(${CASE}-build1 ${CMAKE_COMMAND} --build . --config Debug)
  41. if(run_BuildDepends_skip_step_2)
  42. return()
  43. endif()
  44. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay}) # handle 1s resolution
  45. include(${RunCMake_SOURCE_DIR}/${CASE}.step2.cmake OPTIONAL)
  46. set(check_step 2)
  47. run_cmake_command(${CASE}-build2 ${CMAKE_COMMAND} --build . --config Debug)
  48. if(run_BuildDepends_skip_step_3)
  49. return()
  50. endif()
  51. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay}) # handle 1s resolution
  52. include(${RunCMake_SOURCE_DIR}/${CASE}.step3.cmake OPTIONAL)
  53. set(check_step 3)
  54. run_cmake_command(${CASE}-build3 ${CMAKE_COMMAND} --build . --config Debug)
  55. endfunction()
  56. set(run_BuildDepends_skip_step_3 1)
  57. run_BuildDepends(C-Exe)
  58. if(NOT RunCMake_GENERATOR STREQUAL "Xcode")
  59. if(RunCMake_GENERATOR_TOOLSET MATCHES "^(v80|v90|v100)$")
  60. # VS 10 forgets to re-link when a manifest changes
  61. set(run_BuildDepends_skip_step_2 1)
  62. endif()
  63. run_BuildDepends(C-Exe-Manifest)
  64. unset(run_BuildDepends_skip_step_2)
  65. endif()
  66. if(CMake_TEST_Fortran
  67. # FIXME(lfortran): The compiler fails on the test's includes.
  68. AND NOT CMAKE_Fortran_COMPILER_ID STREQUAL "LFortran"
  69. )
  70. run_BuildDepends(FortranInclude)
  71. endif()
  72. run_BuildDepends(Custom-Symbolic-and-Byproduct)
  73. run_BuildDepends(Custom-Always)
  74. set(RunCMake_TEST_OUTPUT_MERGE_save "${RunCMake_TEST_OUTPUT_MERGE}")
  75. set(RunCMake_TEST_OUTPUT_MERGE 1)
  76. run_BuildDepends(ExternalProjectCacheArgs)
  77. set(RunCMake_TEST_OUTPUT_MERGE "${RunCMake_TEST_OUTPUT_MERGE_save}")
  78. # Test header dependencies with a build tree underneath a source tree.
  79. set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/BuildUnderSource")
  80. set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/BuildUnderSource/build")
  81. file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
  82. file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}/include")
  83. foreach(f CMakeLists.txt BuildUnderSource.cmake BuildUnderSource.c)
  84. configure_file("${RunCMake_SOURCE_DIR}/${f}" "${RunCMake_TEST_SOURCE_DIR}/${f}" COPYONLY)
  85. endforeach()
  86. run_BuildDepends(BuildUnderSource)
  87. unset(RunCMake_TEST_BINARY_DIR)
  88. unset(RunCMake_TEST_SOURCE_DIR)
  89. if(RunCMake_GENERATOR MATCHES "Make")
  90. run_BuildDepends(MakeCustomIncludes)
  91. if(NOT "${RunCMake_BINARY_DIR}" STREQUAL "${RunCMake_SOURCE_DIR}")
  92. run_BuildDepends(MakeInProjectOnly)
  93. endif()
  94. endif()
  95. function(run_RepeatCMake CASE)
  96. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
  97. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  98. set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
  99. else()
  100. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  101. endif()
  102. run_cmake(${CASE})
  103. set(RunCMake_TEST_NO_CLEAN 1)
  104. run_cmake_command(${CASE}-build1 ${CMAKE_COMMAND} --build . --config Debug)
  105. run_cmake_command(${CASE}-rerun1 ${CMAKE_COMMAND} .)
  106. file(WRITE ${RunCMake_TEST_BINARY_DIR}/exists-for-build2 "")
  107. run_cmake_command(${CASE}-build2 ${CMAKE_COMMAND} --build . --config Debug)
  108. endfunction()
  109. run_RepeatCMake(RepeatCMake-Custom)
  110. function(run_ReGeneration)
  111. # test re-generation of project even if CMakeLists.txt files disappeared
  112. # Use a single build tree for a few tests without cleaning.
  113. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/regenerate-project-build)
  114. set(RunCMake_TEST_SOURCE_DIR ${RunCMake_BINARY_DIR}/regenerate-project-source)
  115. set(RunCMake_TEST_NO_CLEAN 1)
  116. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  117. file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
  118. set(ProjectHeader [=[
  119. cmake_minimum_required(VERSION 3.10)
  120. project(Regenerate-Project NONE)
  121. ]=])
  122. # create project with subdirectory
  123. file(WRITE "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" "${ProjectHeader}"
  124. "add_subdirectory(mysubdir)")
  125. file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}/mysubdir")
  126. file(WRITE "${RunCMake_TEST_SOURCE_DIR}/mysubdir/CMakeLists.txt" "# empty")
  127. run_cmake(Regenerate-Project)
  128. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay})
  129. # now we delete the subdirectory and adjust the CMakeLists.txt
  130. file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}/mysubdir")
  131. file(WRITE "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" "${ProjectHeader}")
  132. run_cmake_command(Regenerate-Project-Directory-Removed
  133. ${CMAKE_COMMAND} --build "${RunCMake_TEST_BINARY_DIR}")
  134. unset(RunCMake_TEST_BINARY_DIR)
  135. unset(RunCMake_TEST_SOURCE_DIR)
  136. unset(RunCMake_TEST_NO_CLEAN)
  137. endfunction()
  138. if(RunCMake_GENERATOR STREQUAL "Xcode")
  139. run_ReGeneration(regenerate-project)
  140. endif()
  141. if(CMake_TEST_BuildDepends_GNU_AS)
  142. set(ENV{ASM} "${CMake_TEST_BuildDepends_GNU_AS}")
  143. run_BuildDepends(GNU-AS)
  144. endif()
  145. if ((RunCMake_GENERATOR STREQUAL "Unix Makefiles"
  146. AND (CMAKE_C_COMPILER_ID STREQUAL "GNU"
  147. OR CMAKE_C_COMPILER_ID STREQUAL "LCC"
  148. OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
  149. OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang"))
  150. OR (RunCMake_GENERATOR STREQUAL "NMake Makefiles"
  151. AND MSVC_VERSION GREATER 1300
  152. AND CMAKE_C_COMPILER_ID STREQUAL "MSVC")
  153. OR RunCMake_GENERATOR MATCHES "Ninja"
  154. )
  155. run_BuildDepends(CompileDepends -DMAKE_SUPPORTS_SPACES=${MAKE_SUPPORTS_SPACES})
  156. run_BuildDepends(CustomCommandDepends -DMAKE_SUPPORTS_SPACES=${MAKE_SUPPORTS_SPACES})
  157. endif()
  158. if (RunCMake_GENERATOR MATCHES "Makefiles")
  159. run_cmake(CustomCommandDepends-BadArgs)
  160. run_cmake_with_options(CustomCommandDepends-compiler-deps-legacy -DCMAKE_DEPENDS_USE_COMPILER=FALSE)
  161. set(RunCMake_TEST_NO_CLEAN 1)
  162. run_cmake_command(CustomCommandDepends-compiler-deps-legacy ${CMAKE_COMMAND} --build . --config Debug)
  163. unset(RunCMake_TEST_NO_CLEAN)
  164. endif()
  165. if(RunCMake_GENERATOR MATCHES "Make|Ninja|Visual Studio|Xcode")
  166. unset(run_BuildDepends_skip_step_3)
  167. run_BuildDepends(CustomCommandDepfile)
  168. set(run_BuildDepends_skip_step_3 1)
  169. endif()
  170. if(RunCMake_GENERATOR MATCHES "Make")
  171. run_BuildDepends(MakeDependencies)
  172. endif()
  173. if(RunCMake_GENERATOR MATCHES "Ninja" AND ninja_version VERSION_LESS 1.7)
  174. # This build tool misses the dependency.
  175. set(run_BuildDepends_skip_step_2 1)
  176. endif()
  177. run_BuildDepends(CustomCommandUnityBuild)
  178. unset(run_BuildDepends_skip_step_2)
  179. if (RunCMake_GENERATOR MATCHES "Make|Ninja")
  180. set(run_BuildDepends_skip_step_2 1)
  181. run_BuildDepends(LinkDependsCheck)
  182. include("${RunCMake_BINARY_DIR}/LinkDependsCheck-build/LinkDependsUseLinker.cmake")
  183. if ((NOT DEFINED CMAKE_LINK_DEPENDS_USE_LINKER OR CMAKE_LINK_DEPENDS_USE_LINKER)
  184. AND CMAKE_C_LINK_DEPENDS_USE_LINKER
  185. # FIXME(#26401): GNU binutils 2.43 broke dependency-file generation.
  186. AND NOT (CMAKE_C_COMPILER_LINKER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_LINKER_VERSION VERSION_GREATER_EQUAL "2.43")
  187. )
  188. run_BuildDepends(LinkDependsExternalLibrary)
  189. unset(run_BuildDepends_skip_step_2)
  190. run_BuildDepends(LinkDepends -DMAKE_SUPPORTS_SPACES=${MAKE_SUPPORTS_SPACES})
  191. endif()
  192. endif()