RunCMakeTest.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. # Test behavior when the generator does not support C++20 modules.
  33. if (NOT RunCMake_GENERATOR MATCHES "Ninja" OR
  34. ninja_version VERSION_LESS "1.10" OR
  35. NOT "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  36. if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
  37. run_cmake(NoDyndepSupport)
  38. endif ()
  39. # Bail; the remaining tests require the generator to successfully generate
  40. # with C++20 modules in the source list.
  41. return ()
  42. endif ()
  43. set(fileset_types
  44. Modules
  45. ModuleHeaderUnits)
  46. set(scopes
  47. Interface
  48. Private
  49. Public)
  50. foreach (fileset_type IN LISTS fileset_types)
  51. foreach (scope IN LISTS scopes)
  52. run_cmake("FileSet${fileset_type}${scope}")
  53. endforeach ()
  54. run_cmake("FileSet${fileset_type}InterfaceImported")
  55. # Test the error message when a non-C++ source file is found in the source
  56. # list.
  57. run_cmake("NotCXXSource${fileset_type}")
  58. endforeach ()
  59. run_cmake(InstallBMI)
  60. run_cmake(InstallBMIGenericArgs)
  61. run_cmake(InstallBMIIgnore)
  62. run_cmake(ExportBuildCxxModules)
  63. run_cmake(ExportInstallCxxModules)
  64. # Generator-specific tests.
  65. if (RunCMake_GENERATOR MATCHES "Ninja")
  66. run_cmake(NinjaDependInfoFileSet)
  67. run_cmake(NinjaDependInfoExport)
  68. run_cmake(NinjaDependInfoBMIInstall)
  69. else ()
  70. message(FATAL_ERROR
  71. "Please add 'DependInfo' tests for the '${RunCMake_GENERATOR}' generator.")
  72. endif ()
  73. # Actual compilation tests.
  74. if (NOT CMake_TEST_MODULE_COMPILATION)
  75. return ()
  76. endif ()
  77. function (run_cxx_module_test directory)
  78. set(test_name "${directory}")
  79. if (NOT ARGN STREQUAL "")
  80. list(POP_FRONT ARGN test_name)
  81. endif ()
  82. set(RunCMake_TEST_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/examples/${directory}")
  83. set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/examples/${test_name}-build")
  84. if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
  85. set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
  86. else ()
  87. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  88. endif ()
  89. list(APPEND RunCMake_TEST_OPTIONS
  90. "-DCMake_TEST_MODULE_COMPILATION_RULES=${CMake_TEST_MODULE_COMPILATION_RULES}"
  91. ${ARGN})
  92. run_cmake("examples/${test_name}")
  93. set(RunCMake_TEST_NO_CLEAN 1)
  94. run_cmake_command("examples/${test_name}-build" "${CMAKE_COMMAND}" --build . --config Debug)
  95. run_cmake_command("examples/${test_name}-test" "${CMAKE_CTEST_COMMAND}" -C Debug --output-on-failure)
  96. endfunction ()
  97. string(REPLACE "," ";" CMake_TEST_MODULE_COMPILATION "${CMake_TEST_MODULE_COMPILATION}")
  98. # Tests which use named modules.
  99. if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
  100. run_cxx_module_test(simple)
  101. run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
  102. run_cxx_module_test(generated)
  103. endif ()
  104. # Tests which use named modules in shared libraries.
  105. if ("shared" IN_LIST CMake_TEST_MODULE_COMPILATION)
  106. run_cxx_module_test(library library-shared -DBUILD_SHARED_LIBS=ON)
  107. endif ()
  108. # Tests which use partitions.
  109. if ("partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  110. run_cxx_module_test(partitions)
  111. endif ()
  112. # Tests which use internal partitions.
  113. if ("internal_partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  114. run_cxx_module_test(internal-partitions)
  115. endif ()