RunCMakeTest.cmake 7.7 KB

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