RunCMakeTest.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. endfunction ()
  130. function (run_cxx_module_test_target directory target)
  131. set(RunCMake_CXXModules_TARGET "${target}")
  132. set(RunCMake_CXXModules_NO_TEST 1)
  133. run_cxx_module_test("${directory}" ${ARGN})
  134. endfunction ()
  135. string(REPLACE "," ";" CMake_TEST_MODULE_COMPILATION "${CMake_TEST_MODULE_COMPILATION}")
  136. if (RunCMake_GENERATOR MATCHES "Ninja")
  137. if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
  138. set(ninja_cmp0154_target "CMakeFiles/ninja_cmp0154.dir/Debug/unrelated.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
  139. else ()
  140. set(ninja_cmp0154_target "CMakeFiles/ninja_cmp0154.dir/unrelated.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
  141. endif ()
  142. run_cxx_module_test_target(ninja-cmp0154 "${ninja_cmp0154_target}")
  143. endif ()
  144. run_cxx_module_test(scan-with-pch)
  145. # Tests which use named modules.
  146. if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
  147. run_cxx_module_test(simple)
  148. run_cxx_module_test(vs-without-flags)
  149. run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
  150. run_cxx_module_test(unity-build)
  151. run_cxx_module_test(object-library)
  152. run_cxx_module_test(generated)
  153. run_cxx_module_test(deep-chain)
  154. run_cxx_module_test(non-trivial-collation-order)
  155. run_cxx_module_test(non-trivial-collation-order-randomized)
  156. run_cxx_module_test(duplicate)
  157. set(RunCMake_CXXModules_NO_TEST 1)
  158. run_cxx_module_test(import-from-object)
  159. run_cxx_module_test(circular)
  160. run_cxx_module_test(try-compile)
  161. run_cxx_module_test(try-run)
  162. unset(RunCMake_CXXModules_NO_TEST)
  163. run_cxx_module_test(same-src-name)
  164. run_cxx_module_test(scan_properties)
  165. endif ()
  166. # Tests which require compile commands support.
  167. if ("compile_commands" IN_LIST CMake_TEST_MODULE_COMPILATION)
  168. run_cxx_module_test(export-compile-commands)
  169. endif ()
  170. # Tests which require collation work.
  171. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION)
  172. run_cxx_module_test(public-req-private)
  173. set(RunCMake_CXXModules_NO_TEST 1)
  174. run_cxx_module_test(req-private-other-target)
  175. unset(RunCMake_CXXModules_NO_TEST)
  176. endif ()
  177. # Tests which use named modules in shared libraries.
  178. if ("shared" IN_LIST CMake_TEST_MODULE_COMPILATION)
  179. run_cxx_module_test(library library-shared -DBUILD_SHARED_LIBS=ON)
  180. endif ()
  181. # Tests which use partitions.
  182. if ("partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  183. run_cxx_module_test(partitions)
  184. endif ()
  185. # Tests which use internal partitions.
  186. if ("internal_partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  187. run_cxx_module_test(internal-partitions)
  188. endif ()
  189. # Tests which install BMIs
  190. if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  191. run_cxx_module_test(export-interface-no-properties-build)
  192. run_cxx_module_test(export-interface-build)
  193. run_cxx_module_test(export-include-directories-build)
  194. run_cxx_module_test(export-usage-build)
  195. run_cxx_module_test(export-bmi-and-interface-build)
  196. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND
  197. "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  198. set(test_suffix export-interface-build)
  199. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build")
  200. set(test_suffix export-interface-no-properties-build)
  201. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DNO_PROPERTIES=1)
  202. set(test_suffix export-include-directories-build)
  203. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DINCLUDE_PROPERTIES=1)
  204. set(test_suffix export-bmi-and-interface-build)
  205. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DWITH_BMIS=1)
  206. endif ()
  207. endif ()
  208. # All of the following tests perform installation.
  209. set(RunCMake_CXXModules_INSTALL 1)
  210. # Tests which install BMIs
  211. if ("install_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  212. run_cxx_module_test(install-bmi)
  213. run_cxx_module_test(install-bmi-and-interfaces)
  214. if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  215. run_cxx_module_test(export-interface-no-properties-install)
  216. run_cxx_module_test(export-interface-install)
  217. run_cxx_module_test(export-include-directories-install)
  218. run_cxx_module_test(export-usage-install)
  219. run_cxx_module_test(export-bmi-and-interface-install)
  220. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND
  221. "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  222. set(RunCMake_CXXModules_INSTALL 0)
  223. set(test_suffix export-interface-install)
  224. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install")
  225. set(test_suffix export-interface-no-properties-install)
  226. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DNO_PROPERTIES=1)
  227. set(test_suffix export-include-directories-install)
  228. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DINCLUDE_PROPERTIES=1)
  229. set(test_suffix export-bmi-and-interface-install)
  230. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DWITH_BMIS=1)
  231. set(RunCMake_CXXModules_INSTALL 1)
  232. endif ()
  233. endif ()
  234. endif ()