RunCMakeTest.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. include(RunCMake)
  2. # For `if (IN_LIST)`
  3. cmake_policy(SET CMP0057 NEW)
  4. run_cmake(Inspect)
  5. include("${RunCMake_BINARY_DIR}/Inspect-build/info.cmake")
  6. # Test negative cases where C++20 modules do not work.
  7. run_cmake(NoCXX)
  8. if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  9. # This test requires that the compiler be told to compile in an older-than-20
  10. # standard. If the compiler forces a standard to be used, skip it.
  11. if (NOT forced_cxx_standard)
  12. run_cmake(NoCXX20)
  13. if(CMAKE_CXX_STANDARD_DEFAULT AND CMAKE_CXX20_STANDARD_COMPILE_OPTION)
  14. run_cmake_with_options(ImplicitCXX20 -DCMAKE_CXX20_STANDARD_COMPILE_OPTION=${CMAKE_CXX20_STANDARD_COMPILE_OPTION})
  15. endif()
  16. endif ()
  17. run_cmake(NoScanningSourceFileProperty)
  18. run_cmake(NoScanningTargetProperty)
  19. run_cmake(NoScanningVariable)
  20. run_cmake(CMP0155-OLD)
  21. run_cmake(CMP0155-NEW)
  22. run_cmake(CMP0155-NEW-with-rule)
  23. endif ()
  24. if (RunCMake_GENERATOR MATCHES "Ninja")
  25. execute_process(
  26. COMMAND "${CMAKE_MAKE_PROGRAM}" --version
  27. RESULT_VARIABLE res
  28. OUTPUT_VARIABLE ninja_version
  29. ERROR_VARIABLE err
  30. OUTPUT_STRIP_TRAILING_WHITESPACE
  31. ERROR_STRIP_TRAILING_WHITESPACE)
  32. if (res)
  33. message(WARNING
  34. "Failed to determine `ninja` version: ${err}")
  35. set(ninja_version "0")
  36. endif ()
  37. endif ()
  38. set(generator_supports_cxx_modules 0)
  39. if (RunCMake_GENERATOR MATCHES "Ninja" AND
  40. ninja_version VERSION_GREATER_EQUAL "1.11" AND
  41. "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  42. set(generator_supports_cxx_modules 1)
  43. endif ()
  44. if (RunCMake_GENERATOR MATCHES "Visual Studio" AND
  45. CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "19.34")
  46. set(generator_supports_cxx_modules 1)
  47. endif ()
  48. # Test behavior when the generator does not support C++20 modules.
  49. if (NOT generator_supports_cxx_modules)
  50. if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  51. run_cmake(NoDyndepSupport)
  52. endif ()
  53. # Bail; the remaining tests require the generator to successfully generate
  54. # with C++20 modules in the source list.
  55. return ()
  56. endif ()
  57. set(fileset_types
  58. Modules)
  59. set(scopes
  60. Interface
  61. Private
  62. Public)
  63. foreach (fileset_type IN LISTS fileset_types)
  64. foreach (scope IN LISTS scopes)
  65. run_cmake("FileSet${fileset_type}${scope}")
  66. endforeach ()
  67. run_cmake("FileSet${fileset_type}InterfaceImported")
  68. # Test the error messages when a non-C++ source file is found in the source
  69. # list.
  70. run_cmake("NotCompiledSource${fileset_type}")
  71. run_cmake("NotCXXSource${fileset_type}")
  72. endforeach ()
  73. run_cmake(InstallBMI)
  74. run_cmake(InstallBMIGenericArgs)
  75. run_cmake(InstallBMIIgnore)
  76. run_cmake(ExportBuildCxxModules)
  77. run_cmake(ExportBuildCxxModulesTargets)
  78. run_cmake(ExportInstallCxxModules)
  79. # Generator-specific tests.
  80. if (RunCMake_GENERATOR MATCHES "Ninja")
  81. run_cmake(NinjaDependInfoFileSet)
  82. run_cmake(NinjaDependInfoExport)
  83. run_cmake(NinjaDependInfoBMIInstall)
  84. run_cmake(NinjaForceResponseFile) # issue#25367
  85. elseif (RunCMake_GENERATOR MATCHES "Visual Studio")
  86. run_cmake(VisualStudioNoSyntheticTargets)
  87. else ()
  88. message(FATAL_ERROR
  89. "Please add 'DependInfo' tests for the '${RunCMake_GENERATOR}' generator.")
  90. endif ()
  91. # Actual compilation tests.
  92. if (NOT CMake_TEST_MODULE_COMPILATION)
  93. return ()
  94. endif ()
  95. function (run_cxx_module_test directory)
  96. set(test_name "${directory}")
  97. if (NOT ARGN STREQUAL "")
  98. list(POP_FRONT ARGN test_name)
  99. endif ()
  100. set(RunCMake_TEST_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/examples/${directory}")
  101. set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/examples/${test_name}-build")
  102. if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
  103. set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
  104. else ()
  105. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  106. endif ()
  107. if (RunCMake_CXXModules_INSTALL)
  108. set(prefix "${RunCMake_BINARY_DIR}/examples/${test_name}-install")
  109. file(REMOVE_RECURSE "${prefix}")
  110. list(APPEND RunCMake_TEST_OPTIONS
  111. "-DCMAKE_INSTALL_PREFIX=${prefix}")
  112. endif ()
  113. list(APPEND RunCMake_TEST_OPTIONS
  114. "-DCMake_TEST_MODULE_COMPILATION_RULES=${CMake_TEST_MODULE_COMPILATION_RULES}"
  115. ${ARGN})
  116. run_cmake("examples/${test_name}")
  117. set(RunCMake_TEST_NO_CLEAN 1)
  118. if (RunCMake_CXXModules_TARGET)
  119. run_cmake_command("examples/${test_name}-build" "${CMAKE_COMMAND}" --build . --config Debug --target "${RunCMake_CXXModules_TARGET}")
  120. else ()
  121. run_cmake_command("examples/${test_name}-build" "${CMAKE_COMMAND}" --build . --config Debug)
  122. endif ()
  123. if (RunCMake_CXXModules_INSTALL)
  124. run_cmake_command("examples/${test_name}-install" "${CMAKE_COMMAND}" --build . --target install --config Debug)
  125. endif ()
  126. if (NOT RunCMake_CXXModules_NO_TEST)
  127. run_cmake_command("examples/${test_name}-test" "${CMAKE_CTEST_COMMAND}" -C Debug --output-on-failure)
  128. endif ()
  129. if (RunCMake_CXXModules_REBUILD)
  130. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1.125) # handle 1s resolution
  131. include("${RunCMake_TEST_SOURCE_DIR}/pre-rebuild.cmake")
  132. execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1.125) # handle 1s resolution
  133. run_cmake_command("examples/${test_name}-rebuild" "${CMAKE_COMMAND}" --build . --config Debug)
  134. endif ()
  135. endfunction ()
  136. function (run_cxx_module_test_target directory target)
  137. set(RunCMake_CXXModules_TARGET "${target}")
  138. set(RunCMake_CXXModules_NO_TEST 1)
  139. run_cxx_module_test("${directory}" ${ARGN})
  140. endfunction ()
  141. function (run_cxx_module_test_rebuild directory)
  142. set(RunCMake_CXXModules_INSTALL 0)
  143. set(RunCMake_CXXModules_NO_TEST 1)
  144. set(RunCMake_CXXModules_REBUILD 1)
  145. run_cxx_module_test("${directory}" ${ARGN})
  146. endfunction ()
  147. # Module compilation features:
  148. # Compiler-based:
  149. # - `named`: basic support for named modules is available
  150. # - `shared`: shared libraries are supported
  151. # - `partitions`: module partitions are supported
  152. # - `internal_partitions`: internal module partitions are supported
  153. # - `bmionly`: the compiler supports BMI-only builds
  154. #
  155. # Generator-based:
  156. # - `compile_commands`: the generator supports `compile_commands.json`
  157. # - `collation`: the generator supports module collation features
  158. # - `export_bmi`: the generator supports exporting BMIs
  159. # - `ninja`: a `ninja` binary is available to perform `Ninja`-only testing
  160. # (assumed if the generator matches `Ninja`).
  161. string(REPLACE "," ";" CMake_TEST_MODULE_COMPILATION "${CMake_TEST_MODULE_COMPILATION}")
  162. if (RunCMake_GENERATOR MATCHES "Ninja")
  163. list(APPEND CMake_TEST_MODULE_COMPILATION
  164. "ninja")
  165. endif ()
  166. if (RunCMake_GENERATOR MATCHES "Ninja")
  167. if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
  168. set(ninja_cmp0154_target "CMakeFiles/ninja_cmp0154.dir/Debug/unrelated.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
  169. else ()
  170. set(ninja_cmp0154_target "CMakeFiles/ninja_cmp0154.dir/unrelated.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
  171. endif ()
  172. run_cxx_module_test_target(ninja-cmp0154 "${ninja_cmp0154_target}")
  173. endif ()
  174. run_cxx_module_test(scan-with-pch)
  175. # Tests which use named modules.
  176. if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
  177. run_cxx_module_test(simple)
  178. run_cxx_module_test(file-sets-with-dot)
  179. run_cxx_module_test(vs-without-flags)
  180. run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
  181. run_cxx_module_test(unity-build)
  182. run_cxx_module_test(object-library)
  183. run_cxx_module_test(generated)
  184. run_cxx_module_test(deep-chain)
  185. run_cxx_module_test(non-trivial-collation-order)
  186. run_cxx_module_test(non-trivial-collation-order-randomized)
  187. run_cxx_module_test(duplicate)
  188. set(RunCMake_CXXModules_NO_TEST 1)
  189. run_cxx_module_test(import-from-object)
  190. run_cxx_module_test(circular)
  191. run_cxx_module_test(try-compile)
  192. run_cxx_module_test(try-run)
  193. unset(RunCMake_CXXModules_NO_TEST)
  194. run_cxx_module_test(same-src-name)
  195. run_cxx_module_test(scan_properties)
  196. endif ()
  197. # Tests which require compile commands support.
  198. if ("compile_commands" IN_LIST CMake_TEST_MODULE_COMPILATION)
  199. run_cxx_module_test(export-compile-commands)
  200. endif ()
  201. # Tests which require collation work.
  202. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION)
  203. run_cxx_module_test(public-req-private)
  204. set(RunCMake_CXXModules_NO_TEST 1)
  205. run_cxx_module_test(req-private-other-target)
  206. unset(RunCMake_CXXModules_NO_TEST)
  207. run_cxx_module_test_rebuild(depchain-modmap)
  208. run_cxx_module_test_rebuild(depchain-modules-json-file)
  209. if (RunCMake_GENERATOR MATCHES "Ninja")
  210. run_cxx_module_test_rebuild(depchain-collation-restat)
  211. endif ()
  212. endif ()
  213. # Tests which use named modules in shared libraries.
  214. if ("shared" IN_LIST CMake_TEST_MODULE_COMPILATION)
  215. run_cxx_module_test(library library-shared -DBUILD_SHARED_LIBS=ON)
  216. endif ()
  217. # Tests which use partitions.
  218. if ("partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  219. run_cxx_module_test(partitions)
  220. endif ()
  221. # Tests which use internal partitions.
  222. if ("internal_partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  223. run_cxx_module_test(internal-partitions)
  224. endif ()
  225. # Tests which install BMIs
  226. if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  227. run_cxx_module_test(export-interface-no-properties-build)
  228. run_cxx_module_test(export-interface-build)
  229. run_cxx_module_test(export-include-directories-build)
  230. run_cxx_module_test(export-usage-build)
  231. run_cxx_module_test(export-bmi-and-interface-build)
  232. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND
  233. "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  234. set(test_suffix export-interface-build)
  235. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build")
  236. set(test_suffix export-interface-no-properties-build)
  237. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DNO_PROPERTIES=1)
  238. set(test_suffix export-include-directories-build)
  239. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DINCLUDE_PROPERTIES=1)
  240. set(test_suffix export-bmi-and-interface-build)
  241. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DWITH_BMIS=1)
  242. endif ()
  243. endif ()
  244. # All of the following tests perform installation.
  245. set(RunCMake_CXXModules_INSTALL 1)
  246. # Tests which install BMIs
  247. if ("install_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  248. run_cxx_module_test(install-bmi)
  249. run_cxx_module_test(install-bmi-and-interfaces)
  250. if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  251. run_cxx_module_test(export-interface-no-properties-install)
  252. run_cxx_module_test(export-interface-install)
  253. run_cxx_module_test(export-include-directories-install)
  254. run_cxx_module_test(export-usage-install)
  255. run_cxx_module_test(export-bmi-and-interface-install)
  256. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND
  257. "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  258. set(RunCMake_CXXModules_INSTALL 0)
  259. set(test_suffix export-interface-install)
  260. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install")
  261. set(test_suffix export-interface-no-properties-install)
  262. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DNO_PROPERTIES=1)
  263. set(test_suffix export-include-directories-install)
  264. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DINCLUDE_PROPERTIES=1)
  265. set(test_suffix export-bmi-and-interface-install)
  266. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DWITH_BMIS=1)
  267. set(RunCMake_CXXModules_INSTALL 1)
  268. endif ()
  269. endif ()
  270. endif ()
  271. # All remaining tests require a working `Ninja` generator to set up a test case
  272. # for the current generator.
  273. if (NOT "ninja" IN_LIST CMake_TEST_MODULE_COMPILATION)
  274. return ()
  275. endif ()
  276. # All remaining tests require `bmionly` in order to consume from the `ninja`
  277. # build.
  278. if (NOT "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  279. return ()
  280. endif ()
  281. function (run_cxx_module_test_ninja directory)
  282. set(RunCMake_GENERATOR "Ninja")
  283. set(RunCMake_CXXModules_NO_TEST 1)
  284. set(RunCMake_CXXModules_INSTALL 1)
  285. # `Ninja` is not a multi-config generator.
  286. set(RunCMake_GENERATOR_IS_MULTI_CONFIG 0)
  287. run_cxx_module_test("${directory}" "${directory}-ninja" ${ARGN})
  288. endfunction ()
  289. # Installation happens within `run_cxx_module_test_ninja`.
  290. set(RunCMake_CXXModules_INSTALL 0)
  291. set(test_set modules-from-ninja)
  292. run_cxx_module_test_ninja("export-${test_set}")
  293. run_cxx_module_test(import-modules "import-${test_set}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/export-${test_set}-ninja-install" -DFROM_NINJA=1)