RunCMakeTest.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. run_BuildDepends(FortranInclude)
  67. endif()
  68. run_BuildDepends(Custom-Symbolic-and-Byproduct)
  69. run_BuildDepends(Custom-Always)
  70. set(RunCMake_TEST_OUTPUT_MERGE_save "${RunCMake_TEST_OUTPUT_MERGE}")
  71. set(RunCMake_TEST_OUTPUT_MERGE 1)
  72. run_BuildDepends(ExternalProjectCacheArgs)
  73. set(RunCMake_TEST_OUTPUT_MERGE "${RunCMake_TEST_OUTPUT_MERGE_save}")
  74. # Test header dependencies with a build tree underneath a source tree.
  75. set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/BuildUnderSource")
  76. set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/BuildUnderSource/build")
  77. file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
  78. file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}/include")
  79. foreach(f CMakeLists.txt BuildUnderSource.cmake BuildUnderSource.c)
  80. configure_file("${RunCMake_SOURCE_DIR}/${f}" "${RunCMake_TEST_SOURCE_DIR}/${f}" COPYONLY)
  81. endforeach()
  82. run_BuildDepends(BuildUnderSource)
  83. unset(RunCMake_TEST_BINARY_DIR)
  84. unset(RunCMake_TEST_SOURCE_DIR)
  85. if(RunCMake_GENERATOR MATCHES "Make")
  86. run_BuildDepends(MakeCustomIncludes)
  87. if(NOT "${RunCMake_BINARY_DIR}" STREQUAL "${RunCMake_SOURCE_DIR}")
  88. run_BuildDepends(MakeInProjectOnly)
  89. endif()
  90. endif()
  91. function(run_RepeatCMake CASE)
  92. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
  93. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  94. set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
  95. else()
  96. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  97. endif()
  98. run_cmake(${CASE})
  99. set(RunCMake_TEST_NO_CLEAN 1)
  100. run_cmake_command(${CASE}-build1 ${CMAKE_COMMAND} --build . --config Debug)
  101. run_cmake_command(${CASE}-rerun1 ${CMAKE_COMMAND} .)
  102. file(WRITE ${RunCMake_TEST_BINARY_DIR}/exists-for-build2 "")
  103. run_cmake_command(${CASE}-build2 ${CMAKE_COMMAND} --build . --config Debug)
  104. endfunction()
  105. run_RepeatCMake(RepeatCMake-Custom)
  106. function(run_ReGeneration)
  107. # test re-generation of project even if CMakeLists.txt files disappeared
  108. # Use a single build tree for a few tests without cleaning.
  109. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/regenerate-project-build)
  110. set(RunCMake_TEST_SOURCE_DIR ${RunCMake_BINARY_DIR}/regenerate-project-source)
  111. set(RunCMake_TEST_NO_CLEAN 1)
  112. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  113. file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
  114. set(ProjectHeader [=[
  115. cmake_minimum_required(VERSION 3.5)
  116. project(Regenerate-Project NONE)
  117. ]=])
  118. # create project with subdirectory
  119. file(WRITE "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" "${ProjectHeader}"
  120. "add_subdirectory(mysubdir)")
  121. file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}/mysubdir")
  122. file(WRITE "${RunCMake_TEST_SOURCE_DIR}/mysubdir/CMakeLists.txt" "# empty")
  123. run_cmake(Regenerate-Project)
  124. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay})
  125. # now we delete the subdirectory and adjust the CMakeLists.txt
  126. file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}/mysubdir")
  127. file(WRITE "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" "${ProjectHeader}")
  128. run_cmake_command(Regenerate-Project-Directory-Removed
  129. ${CMAKE_COMMAND} --build "${RunCMake_TEST_BINARY_DIR}")
  130. unset(RunCMake_TEST_BINARY_DIR)
  131. unset(RunCMake_TEST_SOURCE_DIR)
  132. unset(RunCMake_TEST_NO_CLEAN)
  133. endfunction()
  134. if(RunCMake_GENERATOR STREQUAL "Xcode")
  135. run_ReGeneration(regenerate-project)
  136. endif()
  137. if(CMake_TEST_BuildDepends_GNU_AS)
  138. set(ENV{ASM} "${CMake_TEST_BuildDepends_GNU_AS}")
  139. run_BuildDepends(GNU-AS)
  140. endif()
  141. if ((RunCMake_GENERATOR STREQUAL "Unix Makefiles"
  142. AND (CMAKE_C_COMPILER_ID STREQUAL "GNU"
  143. OR CMAKE_C_COMPILER_ID STREQUAL "LCC"
  144. OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
  145. OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang"))
  146. OR (RunCMake_GENERATOR STREQUAL "NMake Makefiles"
  147. AND MSVC_VERSION GREATER 1300
  148. AND CMAKE_C_COMPILER_ID STREQUAL "MSVC"))
  149. run_BuildDepends(CompilerDependencies)
  150. run_BuildDepends(CustomCommandDependencies)
  151. endif()
  152. if (RunCMake_GENERATOR MATCHES "Makefiles")
  153. run_cmake(CustomCommandDependencies-BadArgs)
  154. run_cmake_with_options(CustomCommandDependencies-compiler-deps-legacy -DCMAKE_DEPENDS_USE_COMPILER=FALSE)
  155. set(RunCMake_TEST_NO_CLEAN 1)
  156. run_cmake_command(CustomCommandDependencies-compiler-deps-legacy ${CMAKE_COMMAND} --build . --config Debug)
  157. unset(RunCMake_TEST_NO_CLEAN)
  158. endif()
  159. if(RunCMake_GENERATOR MATCHES "Make|Ninja|Visual Studio|Xcode" AND
  160. NOT RunCMake_GENERATOR MATCHES "Visual Studio (9|10)( |$)")
  161. unset(run_BuildDepends_skip_step_3)
  162. run_BuildDepends(CustomCommandDepfile)
  163. set(run_BuildDepends_skip_step_3 1)
  164. endif()
  165. if(RunCMake_GENERATOR MATCHES "Make")
  166. run_BuildDepends(MakeDependencies)
  167. endif()
  168. if(RunCMake_GENERATOR MATCHES "^Visual Studio 9 " OR
  169. (RunCMake_GENERATOR MATCHES "Ninja" AND ninja_version VERSION_LESS 1.7))
  170. # This build tool misses the dependency.
  171. set(run_BuildDepends_skip_step_2 1)
  172. endif()
  173. run_BuildDepends(CustomCommandUnityBuild)
  174. unset(run_BuildDepends_skip_step_2)
  175. #if (RunCMake_GENERATOR MATCHES "Make|Ninja" AND CMAKE_C_LINK_DEPENDS_USE_LINKER)
  176. if (RunCMake_GENERATOR MATCHES "Make|Ninja")
  177. set(run_BuildDepends_skip_step_2 1)
  178. run_BuildDepends(LinkDependsCheck)
  179. include("${RunCMake_BINARY_DIR}/LinkDependsCheck-build/LinkDependsUseLinker.cmake")
  180. if (CMAKE_C_LINK_DEPENDS_USE_LINKER)
  181. run_BuildDepends(LinkDependsExternalLibrary)
  182. unset(run_BuildDepends_skip_step_2)
  183. run_BuildDepends(LinkDepends)
  184. endif()
  185. endif()