RunCMakeTest.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. cmake_minimum_required(VERSION 3.16)
  2. include(RunCMake)
  3. set(RunCMake_GENERATOR "Ninja Multi-Config")
  4. set(RunCMake_GENERATOR_IS_MULTI_CONFIG 1)
  5. function(check_files dir)
  6. cmake_parse_arguments(_check_files "" "" "INCLUDE;EXCLUDE" ${ARGN})
  7. set(expected ${_check_files_INCLUDE})
  8. list(FILTER expected EXCLUDE REGEX "^$")
  9. list(REMOVE_DUPLICATES expected)
  10. list(SORT expected)
  11. file(GLOB_RECURSE actual "${dir}/*")
  12. list(FILTER actual EXCLUDE REGEX "/CMakeFiles/|\\.ninja$|/CMakeCache\\.txt$|/target_files[^/]*\\.cmake$|/\\.ninja_[^/]*$|/cmake_install\\.cmake$|\\.ilk$|\\.manifest$|\\.pdb$|\\.exp$|/install_manifest\\.txt$")
  13. foreach(f IN LISTS _check_files_INCLUDE _check_files_EXCLUDE)
  14. if(EXISTS ${f})
  15. list(APPEND actual ${f})
  16. endif()
  17. endforeach()
  18. list(REMOVE_DUPLICATES actual)
  19. list(SORT actual)
  20. if(NOT "${expected}" STREQUAL "${actual}")
  21. string(REPLACE ";" "\n " expected_formatted "${expected}")
  22. string(REPLACE ";" "\n " actual_formatted "${actual}")
  23. string(APPEND RunCMake_TEST_FAILED "Actual files did not match expected\nExpected:\n ${expected_formatted}\nActual:\n ${actual_formatted}\n")
  24. endif()
  25. set(RunCMake_TEST_FAILED "${RunCMake_TEST_FAILED}" PARENT_SCOPE)
  26. endfunction()
  27. function(check_file_contents filename expected)
  28. if(NOT EXISTS "${filename}")
  29. string(APPEND RunCMake_TEST_FAILED "File ${filename} does not exist\n")
  30. else()
  31. file(READ "${filename}" actual)
  32. if(NOT actual MATCHES "${expected}")
  33. string(REPLACE "\n" "\n " expected_formatted "${expected}")
  34. string(REPLACE "\n" "\n " actual_formatted "${actual}")
  35. string(APPEND RunCMake_TEST_FAILED "Contents of ${filename} do not match expected\nExpected:\n ${expected_formatted}\nActual:\n ${actual_formatted}\n")
  36. endif()
  37. endif()
  38. set(RunCMake_TEST_FAILED "${RunCMake_TEST_FAILED}" PARENT_SCOPE)
  39. endfunction()
  40. function(run_cmake_configure case)
  41. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
  42. set(RunCMake_TEST_NO_CLEAN 1)
  43. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  44. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  45. run_cmake(${case})
  46. endfunction()
  47. function(run_cmake_build case suffix config)
  48. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
  49. set(RunCMake_TEST_NO_CLEAN 1)
  50. set(tgts)
  51. foreach(tgt IN LISTS ARGN)
  52. list(APPEND tgts --target ${tgt})
  53. endforeach()
  54. if(config)
  55. set(config_arg --config ${config})
  56. else()
  57. set(config_arg)
  58. endif()
  59. run_cmake_command(${case}-${suffix}-build "${CMAKE_COMMAND}" --build . ${config_arg} ${tgts})
  60. endfunction()
  61. function(run_ninja case suffix file)
  62. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
  63. set(RunCMake_TEST_NO_CLEAN 1)
  64. run_cmake_command(${case}-${suffix}-ninja "${RunCMake_MAKE_PROGRAM}" -f "${file}" ${ARGN})
  65. endfunction()
  66. ###############################################################################
  67. set(RunCMake_TEST_NO_CLEAN 1)
  68. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Simple-build)
  69. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=RelWithDebInfo;-DCMAKE_NMC_CROSS_CONFIGS=all")
  70. run_cmake_configure(Simple)
  71. unset(RunCMake_TEST_OPTIONS)
  72. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  73. run_cmake_build(Simple debug-target Debug simpleexe)
  74. run_ninja(Simple debug-target build-Debug.ninja simplestatic)
  75. get_filename_component(simpleshared_Release "${TARGET_FILE_simpleshared_Release}" NAME)
  76. run_cmake_build(Simple release-filename Release ${simpleshared_Release})
  77. file(RELATIVE_PATH simpleexe_Release "${RunCMake_TEST_BINARY_DIR}" "${TARGET_FILE_simpleexe_Release}")
  78. run_ninja(Simple release-file build-Release.ninja ${simpleexe_Release})
  79. run_cmake_build(Simple all-configs Release simplestatic:all)
  80. run_ninja(Simple default-build-file build.ninja simpleexe)
  81. run_cmake_build(Simple all-clean Release clean:all)
  82. run_cmake_build(Simple debug-subdir Debug SimpleSubdir/all)
  83. run_ninja(Simple release-in-minsizerel-graph-subdir build-MinSizeRel.ninja SimpleSubdir/all:Release)
  84. run_cmake_build(Simple all-subdir Release SimpleSubdir/all:all)
  85. run_ninja(Simple minsizerel-top build-MinSizeRel.ninja all)
  86. run_cmake_build(Simple debug-in-release-graph-top Release all:Debug)
  87. run_ninja(Simple all-clean-again build-Debug.ninja clean:all)
  88. run_ninja(Simple all-top build-RelWithDebInfo.ninja all:all)
  89. # Leave enough time for the timestamp to change on second-resolution systems
  90. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1)
  91. file(TOUCH "${RunCMake_TEST_BINARY_DIR}/empty.cmake")
  92. run_ninja(Simple reconfigure-config build-Release.ninja simpleexe)
  93. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1)
  94. file(TOUCH "${RunCMake_TEST_BINARY_DIR}/empty.cmake")
  95. run_ninja(Simple reconfigure-noconfig build.ninja simpleexe)
  96. run_ninja(Simple default-build-file-clean build.ninja clean)
  97. run_ninja(Simple default-build-file-clean-minsizerel build.ninja clean:MinSizeRel)
  98. run_ninja(Simple default-build-file-all build.ninja all)
  99. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SimpleDefaultBuildAlias-build)
  100. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=Release;-DCMAKE_NMC_DEFAULT_CONFIGS=all;-DCMAKE_NMC_CROSS_CONFIGS=all")
  101. run_cmake_configure(SimpleDefaultBuildAlias)
  102. unset(RunCMake_TEST_OPTIONS)
  103. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  104. run_ninja(SimpleDefaultBuildAlias target build.ninja simpleexe)
  105. run_ninja(SimpleDefaultBuildAlias all build.ninja all)
  106. run_ninja(SimpleDefaultBuildAlias clean build.ninja clean)
  107. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SimpleDefaultBuildAliasList-build)
  108. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=Release;-DCMAKE_NMC_DEFAULT_CONFIGS=Debug\\;Release;-DCMAKE_NMC_CROSS_CONFIGS=all")
  109. run_cmake_configure(SimpleDefaultBuildAliasList)
  110. unset(RunCMake_TEST_OPTIONS)
  111. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  112. run_ninja(SimpleDefaultBuildAliasList target-configs build.ninja simpleexe)
  113. # IMPORTANT: This tests cmake --build . with no config using build.ninja
  114. run_cmake_build(SimpleDefaultBuildAliasList all-configs "" all)
  115. run_ninja(SimpleDefaultBuildAliasList all-relwithdebinfo build.ninja all:RelWithDebInfo)
  116. run_ninja(SimpleDefaultBuildAliasList clean-configs build.ninja clean)
  117. unset(RunCMake_TEST_BINARY_DIR)
  118. set(RunCMake_TEST_OPTIONS "-DCMAKE_CONFIGURATION_TYPES=Debug\\;Release;-DCMAKE_NMC_CROSS_CONFIGS=Debug\\;Release\\;RelWithDebInfo")
  119. run_cmake(InvalidCrossConfigs)
  120. unset(RunCMake_TEST_OPTIONS)
  121. set(RunCMake_TEST_OPTIONS "-DCMAKE_CONFIGURATION_TYPES=Debug\\;Release;-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=RelWithDebInfo")
  122. run_cmake(InvalidDefaultBuildFileConfig)
  123. unset(RunCMake_TEST_OPTIONS)
  124. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=Debug\\;Release;-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=Release;-DCMAKE_NMC_DEFAULT_CONFIGS=Debug\\;Release\\;RelWithDebInfo")
  125. run_cmake(InvalidDefaultConfigsCross)
  126. unset(RunCMake_TEST_OPTIONS)
  127. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=Debug\\;Release;-DCMAKE_NMC_DEFAULT_CONFIGS=all")
  128. run_cmake(InvalidDefaultConfigsNoDefaultFile)
  129. unset(RunCMake_TEST_OPTIONS)
  130. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=Release;-DCMAKE_NMC_DEFAULT_CONFIGS=all")
  131. run_cmake(InvalidDefaultConfigsNoCross)
  132. unset(RunCMake_TEST_OPTIONS)
  133. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG=Release")
  134. run_cmake(DefaultBuildFileConfig)
  135. unset(RunCMake_TEST_OPTIONS)
  136. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SimpleNoCross-build)
  137. run_cmake_configure(SimpleNoCross)
  138. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  139. run_cmake_build(SimpleNoCross debug-target Debug simpleexe)
  140. run_ninja(SimpleNoCross debug-target build-Debug.ninja simplestatic:Debug)
  141. run_ninja(SimpleNoCross relwithdebinfo-in-release-graph-target build-Release.ninja simplestatic:RelWithDebInfo)
  142. run_cmake_build(SimpleNoCross relwithdebinfo-in-release-graph-all Release all:RelWithDebInfo)
  143. run_cmake_build(SimpleNoCross relwithdebinfo-in-release-graph-clean Release clean:RelWithDebInfo)
  144. run_ninja(SimpleNoCross all-target build-Debug.ninja simplestatic:all)
  145. run_ninja(SimpleNoCross all-all build-Debug.ninja all:all)
  146. run_cmake_build(SimpleNoCross all-clean Debug clean:all)
  147. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SimpleCrossConfigs-build)
  148. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=Debug\\;Release")
  149. run_cmake_configure(SimpleCrossConfigs)
  150. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  151. run_ninja(SimpleCrossConfigs release-in-release-graph build-Release.ninja simpleexe)
  152. run_cmake_build(SimpleCrossConfigs debug-in-release-graph Release simpleexe:Debug)
  153. run_cmake_build(SimpleCrossConfigs relwithdebinfo-in-release-graph Release simpleexe:RelWithDebInfo)
  154. run_ninja(SimpleCrossConfigs relwithdebinfo-in-relwithdebinfo-graph build-RelWithDebInfo.ninja simpleexe:RelWithDebInfo)
  155. run_ninja(SimpleCrossConfigs release-in-relwithdebinfo-graph build-RelWithDebInfo.ninja simplestatic:Release)
  156. run_cmake_build(SimpleCrossConfigs all-in-relwithdebinfo-graph RelWithDebInfo simplestatic:all)
  157. run_ninja(SimpleCrossConfigs clean-all-in-release-graph build-Release.ninja clean:all)
  158. run_cmake_build(SimpleCrossConfigs all-all-in-release-graph Release all:all)
  159. run_cmake_build(SimpleCrossConfigs all-relwithdebinfo-in-release-graph Release all:RelWithDebInfo)
  160. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Framework-build)
  161. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=all")
  162. run_cmake_configure(Framework)
  163. unset(RunCMake_TEST_OPTIONS)
  164. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  165. run_cmake_build(Framework framework Debug all)
  166. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CustomCommandGenerator-build)
  167. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=all")
  168. run_cmake_configure(CustomCommandGenerator)
  169. unset(RunCMake_TEST_OPTIONS)
  170. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  171. run_cmake_build(CustomCommandGenerator debug Debug generated)
  172. run_cmake_command(CustomCommandGenerator-debug-generated "${TARGET_FILE_generated_Debug}")
  173. run_ninja(CustomCommandGenerator release build-Release.ninja generated)
  174. run_cmake_command(CustomCommandGenerator-release-generated "${TARGET_FILE_generated_Release}")
  175. run_ninja(CustomCommandGenerator debug-clean build-Debug.ninja clean)
  176. run_cmake_build(CustomCommandGenerator release-clean Release clean)
  177. run_cmake_build(CustomCommandGenerator debug-in-release-graph Release generated:Debug)
  178. run_cmake_command(CustomCommandGenerator-debug-in-release-graph-generated "${TARGET_FILE_generated_Debug}")
  179. run_ninja(CustomCommandGenerator debug-in-release-graph-clean build-Debug.ninja clean:Debug)
  180. run_ninja(CustomCommandGenerator release-in-debug-graph build-Debug.ninja generated:Release)
  181. run_cmake_command(CustomCommandGenerator-release-in-debug-graph-generated "${TARGET_FILE_generated_Release}")
  182. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CustomCommandsAndTargets-build)
  183. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=all")
  184. run_cmake_configure(CustomCommandsAndTargets)
  185. unset(RunCMake_TEST_OPTIONS)
  186. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  187. run_cmake_build(CustomCommandsAndTargets release-command Release SubdirCommand)
  188. #FIXME Get this working
  189. #run_ninja(CustomCommandsAndTargets minsizerel-command build-MinSizeRel.ninja CustomCommandsAndTargetsSubdir/SubdirCommand)
  190. run_ninja(CustomCommandsAndTargets debug-command build-Debug.ninja TopCommand)
  191. run_ninja(CustomCommandsAndTargets release-target build-Release.ninja SubdirTarget)
  192. run_cmake_build(CustomCommandsAndTargets debug-target Debug TopTarget)
  193. run_cmake_build(CustomCommandsAndTargets debug-in-release-graph-postbuild Release SubdirPostBuild:Debug)
  194. run_ninja(CustomCommandsAndTargets release-postbuild build-Release.ninja SubdirPostBuild)
  195. run_cmake_build(CustomCommandsAndTargets debug-targetpostbuild Debug TopTargetPostBuild)
  196. run_ninja(CustomCommandsAndTargets release-targetpostbuild build-Release.ninja SubdirTargetPostBuild)
  197. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/PostfixAndLocation-build)
  198. set(RunCMake_TEST_OPTIONS "-DCMAKE_CONFIGURATION_TYPES=Debug\\;Release;-DCMAKE_NMC_CROSS_CONFIGS=all")
  199. run_cmake_configure(PostfixAndLocation)
  200. unset(RunCMake_TEST_OPTIONS)
  201. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  202. run_cmake_build(PostfixAndLocation release-in-release-graph Release mylib:Release)
  203. run_cmake_build(PostfixAndLocation debug-in-release-graph Release mylib:Debug)
  204. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Clean-build)
  205. run_cmake_configure(Clean)
  206. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  207. run_cmake_build(Clean release Release)
  208. run_ninja(Clean release-notall build-Release.ninja exenotall)
  209. run_cmake_build(Clean release-clean Release clean)
  210. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/AdditionalCleanFiles-build)
  211. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=all")
  212. run_cmake_configure(AdditionalCleanFiles)
  213. unset(RunCMake_TEST_OPTIONS)
  214. run_cmake_build(AdditionalCleanFiles release-clean Release clean)
  215. run_ninja(AdditionalCleanFiles all-clean build-Debug.ninja clean:all)
  216. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Install-build)
  217. set(RunCMake_TEST_OPTIONS "-DCMAKE_INSTALL_PREFIX=${RunCMake_TEST_BINARY_DIR}/install;-DCMAKE_NMC_CROSS_CONFIGS=all")
  218. run_cmake_configure(Install)
  219. unset(RunCMake_TEST_OPTIONS)
  220. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  221. run_cmake_build(Install release-install Release install)
  222. run_ninja(Install debug-in-release-graph-install build-Release.ninja install:Debug)
  223. # FIXME Get this working
  224. #set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/AutoMocExecutable-build)
  225. #run_cmake_configure(AutoMocExecutable)
  226. #run_cmake_build(AutoMocExecutable debug-in-release-graph Release exe)
  227. if(CMake_TEST_CUDA)
  228. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CudaSimple-build)
  229. run_cmake_configure(CudaSimple)
  230. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  231. run_cmake_build(CudaSimple debug-target Debug simplecudaexe)
  232. run_ninja(CudaSimple all-clean build-Debug.ninja clean:Debug)
  233. endif()
  234. if(CMake_TEST_Qt5)
  235. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Qt5-build)
  236. set(RunCMake_TEST_OPTIONS "-DCMAKE_NMC_CROSS_CONFIGS=all")
  237. run_cmake_configure(Qt5)
  238. unset(RunCMake_TEST_OPTIONS)
  239. include(${RunCMake_TEST_BINARY_DIR}/target_files.cmake)
  240. run_cmake_build(Qt5 debug-in-release-graph Release exe:Debug)
  241. endif()