RunCMakeTest.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. # Actual compilation tests.
  65. if (NOT CMake_TEST_MODULE_COMPILATION)
  66. return ()
  67. endif ()
  68. function (run_cxx_module_test directory)
  69. set(test_name "${directory}")
  70. if (NOT ARGN STREQUAL "")
  71. list(POP_FRONT ARGN test_name)
  72. endif ()
  73. set(RunCMake_TEST_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/examples/${directory}")
  74. set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/examples/${test_name}-build")
  75. if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
  76. set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
  77. else ()
  78. set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
  79. endif ()
  80. set(RunCMake_TEST_OPTIONS
  81. "-DCMake_TEST_MODULE_COMPILATION_RULES=${CMake_TEST_MODULE_COMPILATION_RULES}"
  82. ${ARGN})
  83. run_cmake("examples/${test_name}")
  84. set(RunCMake_TEST_NO_CLEAN 1)
  85. run_cmake_command("${test_name}-build" "${CMAKE_COMMAND}" --build . --config Debug)
  86. run_cmake_command("${test_name}-test" "${CMAKE_CTEST_COMMAND}" -C Debug)
  87. endfunction ()
  88. string(REPLACE "," ";" CMake_TEST_MODULE_COMPILATION "${CMake_TEST_MODULE_COMPILATION}")
  89. # Tests which use named modules.
  90. if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
  91. run_cxx_module_test(simple)
  92. run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
  93. run_cxx_module_test(generated)
  94. endif ()
  95. # Tests which use named modules in shared libraries.
  96. if ("shared" IN_LIST CMake_TEST_MODULE_COMPILATION)
  97. run_cxx_module_test(library library-shared -DBUILD_SHARED_LIBS=ON)
  98. endif ()
  99. # Tests which use partitions.
  100. if ("partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  101. run_cxx_module_test(partitions)
  102. endif ()
  103. # Tests which use internal partitions.
  104. if ("internal_partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
  105. run_cxx_module_test(internal-partitions)
  106. endif ()