RunCMakeTest.cmake 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. include(RunCMake)
  2. # For `if (IN_LIST)`
  3. cmake_policy(SET CMP0057 NEW)
  4. run_cmake(compiler_introspection)
  5. include("${RunCMake_BINARY_DIR}/compiler_introspection-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. endif ()
  14. # This test uses C++20, but another prerequisite is missing, so forced
  15. # standards don't matter.
  16. run_cmake(NoCXX20ModuleFlag)
  17. endif ()
  18. if (RunCMake_GENERATOR MATCHES "Ninja")
  19. execute_process(
  20. COMMAND "${CMAKE_MAKE_PROGRAM}" --version
  21. RESULT_VARIABLE res
  22. OUTPUT_VARIABLE ninja_version
  23. ERROR_VARIABLE err
  24. OUTPUT_STRIP_TRAILING_WHITESPACE
  25. ERROR_STRIP_TRAILING_WHITESPACE)
  26. if (res)
  27. message(WARNING
  28. "Failed to determine `ninja` version: ${err}")
  29. set(ninja_version "0")
  30. endif ()
  31. endif ()
  32. set(generator_supports_cxx_modules 0)
  33. if (RunCMake_GENERATOR MATCHES "Ninja" AND
  34. ninja_version VERSION_GREATER_EQUAL "1.11" AND
  35. "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  36. set(generator_supports_cxx_modules 1)
  37. endif ()
  38. if (RunCMake_GENERATOR MATCHES "Visual Studio" AND
  39. CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "19.34")
  40. set(generator_supports_cxx_modules 1)
  41. endif ()
  42. # Test behavior when the generator does not support C++20 modules.
  43. if (NOT generator_supports_cxx_modules)
  44. if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  45. run_cmake(NoDyndepSupport)
  46. endif ()
  47. # Bail; the remaining tests require the generator to successfully generate
  48. # with C++20 modules in the source list.
  49. return ()
  50. endif ()
  51. set(fileset_types
  52. Modules)
  53. set(scopes
  54. Interface
  55. Private
  56. Public)
  57. foreach (fileset_type IN LISTS fileset_types)
  58. foreach (scope IN LISTS scopes)
  59. run_cmake("FileSet${fileset_type}${scope}")
  60. endforeach ()
  61. run_cmake("FileSet${fileset_type}InterfaceImported")
  62. # Test the error messages when a non-C++ source file is found in the source
  63. # list.
  64. run_cmake("NotCompiledSource${fileset_type}")
  65. run_cmake("NotCXXSource${fileset_type}")
  66. endforeach ()
  67. run_cmake(InstallBMI)
  68. run_cmake(InstallBMIGenericArgs)
  69. run_cmake(InstallBMIIgnore)
  70. run_cmake(ExportBuildCxxModules)
  71. run_cmake(ExportInstallCxxModules)
  72. # Generator-specific tests.
  73. if (RunCMake_GENERATOR MATCHES "Ninja")
  74. run_cmake(NinjaDependInfoFileSet)
  75. run_cmake(NinjaDependInfoExport)
  76. run_cmake(NinjaDependInfoBMIInstall)
  77. elseif (RunCMake_GENERATOR MATCHES "Visual Studio")
  78. # Not supported yet.
  79. else ()
  80. message(FATAL_ERROR
  81. "Please add 'DependInfo' tests for the '${RunCMake_GENERATOR}' generator.")
  82. endif ()
  83. # Actual compilation tests.
  84. if (NOT CMake_TEST_MODULE_COMPILATION)
  85. return ()
  86. endif ()
  87. function (run_cxx_module_test directory)
  88. set(test_name "${directory}")
  89. if (NOT ARGN STREQUAL "")
  90. list(POP_FRONT ARGN test_name)
  91. endif ()
  92. set(RunCMake_TEST_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/examples/${directory}")
  93. set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/examples/${test_name}-build")
  94. if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
  95. set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
  96. else ()
  97. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  98. endif ()
  99. if (RunCMake_CXXModules_INSTALL)
  100. set(prefix "${RunCMake_BINARY_DIR}/examples/${test_name}-install")
  101. file(REMOVE_RECURSE "${prefix}")
  102. list(APPEND RunCMake_TEST_OPTIONS
  103. "-DCMAKE_INSTALL_PREFIX=${prefix}")
  104. endif ()
  105. list(APPEND RunCMake_TEST_OPTIONS
  106. "-DCMake_TEST_MODULE_COMPILATION_RULES=${CMake_TEST_MODULE_COMPILATION_RULES}"
  107. ${ARGN})
  108. run_cmake("examples/${test_name}")
  109. set(RunCMake_TEST_NO_CLEAN 1)
  110. run_cmake_command("examples/${test_name}-build" "${CMAKE_COMMAND}" --build . --config Debug)
  111. if (RunCMake_CXXModules_INSTALL)
  112. run_cmake_command("examples/${test_name}-install" "${CMAKE_COMMAND}" --build . --target install --config Debug)
  113. endif ()
  114. if (NOT RunCMake_CXXModules_NO_TEST)
  115. run_cmake_command("examples/${test_name}-test" "${CMAKE_CTEST_COMMAND}" -C Debug --output-on-failure)
  116. endif ()
  117. endfunction ()
  118. string(REPLACE "," ";" CMake_TEST_MODULE_COMPILATION "${CMake_TEST_MODULE_COMPILATION}")
  119. # Tests which use named modules.
  120. if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
  121. run_cxx_module_test(simple)
  122. run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
  123. run_cxx_module_test(object-library)
  124. run_cxx_module_test(generated)
  125. run_cxx_module_test(deep-chain)
  126. run_cxx_module_test(duplicate)
  127. set(RunCMake_CXXModules_NO_TEST 1)
  128. run_cxx_module_test(circular)
  129. run_cxx_module_test(try-compile)
  130. run_cxx_module_test(try-run)
  131. unset(RunCMake_CXXModules_NO_TEST)
  132. run_cxx_module_test(same-src-name)
  133. run_cxx_module_test(scan_properties)
  134. endif ()
  135. # Tests which require compile commands support.
  136. if ("compile_commands" IN_LIST CMake_TEST_MODULE_COMPILATION)
  137. run_cxx_module_test(export-compile-commands)
  138. endif ()
  139. # Tests which require collation work.
  140. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION)
  141. run_cxx_module_test(public-req-private)
  142. set(RunCMake_CXXModules_NO_TEST 1)
  143. run_cxx_module_test(req-private-other-target)
  144. unset(RunCMake_CXXModules_NO_TEST)
  145. endif ()
  146. # Tests which use named modules in shared libraries.
  147. if ("shared" IN_LIST CMake_TEST_MODULE_COMPILATION)
  148. run_cxx_module_test(library library-shared -DBUILD_SHARED_LIBS=ON)
  149. endif ()
  150. # Tests which use partitions.
  151. if ("partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  152. run_cxx_module_test(partitions)
  153. endif ()
  154. # Tests which use internal partitions.
  155. if ("internal_partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  156. run_cxx_module_test(internal-partitions)
  157. endif ()
  158. # Tests which install BMIs
  159. if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  160. run_cxx_module_test(export-interface-no-properties-build)
  161. run_cxx_module_test(export-interface-build)
  162. run_cxx_module_test(export-usage-build)
  163. run_cxx_module_test(export-bmi-and-interface-build)
  164. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND
  165. "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  166. set(test_suffix export-interface-build)
  167. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build")
  168. set(test_suffix export-interface-no-properties-build)
  169. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DNO_PROPERTIES=1)
  170. set(test_suffix export-bmi-and-interface-build)
  171. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DWITH_BMIS=1)
  172. endif ()
  173. endif ()
  174. # All of the following tests perform installation.
  175. set(RunCMake_CXXModules_INSTALL 1)
  176. # Tests which install BMIs
  177. if ("install_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  178. run_cxx_module_test(install-bmi)
  179. run_cxx_module_test(install-bmi-and-interfaces)
  180. if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
  181. run_cxx_module_test(export-interface-no-properties-install)
  182. run_cxx_module_test(export-interface-install)
  183. run_cxx_module_test(export-usage-install)
  184. run_cxx_module_test(export-bmi-and-interface-install)
  185. if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND
  186. "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION)
  187. set(RunCMake_CXXModules_INSTALL 0)
  188. set(test_suffix export-interface-install)
  189. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install")
  190. set(test_suffix export-interface-no-properties-install)
  191. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DNO_PROPERTIES=1)
  192. set(test_suffix export-bmi-and-interface-install)
  193. run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DWITH_BMIS=1)
  194. set(RunCMake_CXXModules_INSTALL 1)
  195. endif ()
  196. endif ()
  197. endif ()