RunCMakeTest.cmake 6.7 KB

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