Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / RunCMake / CXXModules / RunCMakeTest.cmake
1 include(RunCMake)
2
3 # For `if (IN_LIST)`
4 cmake_policy(SET CMP0057 NEW)
5
6 run_cmake(compiler_introspection)
7 include("${RunCMake_BINARY_DIR}/compiler_introspection-build/info.cmake")
8
9 # Test negative cases where C++20 modules do not work.
10 run_cmake(NoCXX)
11 if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
12   # This test requires that the compiler be told to compile in an older-than-20
13   # standard. If the compiler forces a standard to be used, skip it.
14   if (NOT forced_cxx_standard)
15     run_cmake(NoCXX20)
16   endif ()
17
18   # This test uses C++20, but another prerequisite is missing, so forced
19   # standards don't matter.
20   run_cmake(NoCXX20ModuleFlag)
21 endif ()
22
23 if (RunCMake_GENERATOR MATCHES "Ninja")
24   execute_process(
25     COMMAND "${CMAKE_MAKE_PROGRAM}" --version
26     RESULT_VARIABLE res
27     OUTPUT_VARIABLE ninja_version
28     ERROR_VARIABLE err
29     OUTPUT_STRIP_TRAILING_WHITESPACE
30     ERROR_STRIP_TRAILING_WHITESPACE)
31
32   if (res)
33     message(WARNING
34       "Failed to determine `ninja` version: ${err}")
35     set(ninja_version "0")
36   endif ()
37 endif ()
38
39 # Test behavior when the generator does not support C++20 modules.
40 if (NOT RunCMake_GENERATOR MATCHES "Ninja" OR
41     ninja_version VERSION_LESS "1.10" OR
42     NOT "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
43   if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
44     run_cmake(NoDyndepSupport)
45   endif ()
46
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
52 set(fileset_types
53   Modules
54   ModuleHeaderUnits)
55 set(scopes
56   Interface
57   Private
58   Public)
59 foreach (fileset_type IN LISTS fileset_types)
60   foreach (scope IN LISTS scopes)
61     run_cmake("FileSet${fileset_type}${scope}")
62   endforeach ()
63   run_cmake("FileSet${fileset_type}InterfaceImported")
64
65   # Test the error message when a non-C++ source file is found in the source
66   # list.
67   run_cmake("NotCXXSource${fileset_type}")
68 endforeach ()
69
70 run_cmake(InstallBMI)
71 run_cmake(InstallBMIGenericArgs)
72 run_cmake(InstallBMIIgnore)
73
74 run_cmake(ExportBuildCxxModules)
75 run_cmake(ExportInstallCxxModules)
76
77 # Generator-specific tests.
78 if (RunCMake_GENERATOR MATCHES "Ninja")
79   run_cmake(NinjaDependInfoFileSet)
80   run_cmake(NinjaDependInfoExport)
81   run_cmake(NinjaDependInfoBMIInstall)
82 else ()
83   message(FATAL_ERROR
84     "Please add 'DependInfo' tests for the '${RunCMake_GENERATOR}' generator.")
85 endif ()
86
87 # Actual compilation tests.
88 if (NOT CMake_TEST_MODULE_COMPILATION)
89   return ()
90 endif ()
91
92 function (run_cxx_module_test directory)
93   set(test_name "${directory}")
94   if (NOT ARGN STREQUAL "")
95     list(POP_FRONT ARGN test_name)
96   endif ()
97
98   set(RunCMake_TEST_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/examples/${directory}")
99   set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/examples/${test_name}-build")
100
101   if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
102     set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
103   else ()
104     set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
105   endif ()
106
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
114   list(APPEND RunCMake_TEST_OPTIONS
115     "-DCMake_TEST_MODULE_COMPILATION_RULES=${CMake_TEST_MODULE_COMPILATION_RULES}"
116     ${ARGN})
117   run_cmake("examples/${test_name}")
118   set(RunCMake_TEST_NO_CLEAN 1)
119   run_cmake_command("examples/${test_name}-build" "${CMAKE_COMMAND}" --build . --config Debug)
120   if (RunCMake_CXXModules_INSTALL)
121     run_cmake_command("examples/${test_name}-install" "${CMAKE_COMMAND}" --build . --target install --config Debug)
122   endif ()
123   run_cmake_command("examples/${test_name}-test" "${CMAKE_CTEST_COMMAND}" -C Debug --output-on-failure)
124 endfunction ()
125
126 string(REPLACE "," ";" CMake_TEST_MODULE_COMPILATION "${CMake_TEST_MODULE_COMPILATION}")
127
128 # Tests which use named modules.
129 if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
130   run_cxx_module_test(simple)
131   run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
132   run_cxx_module_test(generated)
133   run_cxx_module_test(public-req-private)
134   run_cxx_module_test(deep-chain)
135 endif ()
136
137 # Tests which use named modules in shared libraries.
138 if ("shared" IN_LIST CMake_TEST_MODULE_COMPILATION)
139   run_cxx_module_test(library library-shared -DBUILD_SHARED_LIBS=ON)
140 endif ()
141
142 # Tests which use partitions.
143 if ("partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
144   run_cxx_module_test(partitions)
145 endif ()
146
147 # Tests which use internal partitions.
148 if ("internal_partitions" IN_LIST CMake_TEST_MODULE_COMPILATION)
149   run_cxx_module_test(internal-partitions)
150 endif ()
151
152 # Tests which install BMIs
153 if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
154   run_cxx_module_test(export-interface-build)
155   run_cxx_module_test(export-bmi-and-interface-build)
156 endif ()
157
158 # All of the following tests perform installation.
159 set(RunCMake_CXXModules_INSTALL 1)
160
161 # Tests which install BMIs
162 if ("install_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
163   run_cxx_module_test(install-bmi)
164   run_cxx_module_test(install-bmi-and-interfaces)
165
166   if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION)
167     run_cxx_module_test(export-interface-install)
168     run_cxx_module_test(export-bmi-and-interface-install)
169   endif ()
170 endif ()