Bläddra i källkod

Tests/CXXModules: add a test with unity build support

C++ module-using TUs cannot participate in unity builds. Add a test case
for this situation.
Ben Boeckel 1 år sedan
förälder
incheckning
7fc2a83fe6

+ 1 - 0
Tests/RunCMake/CXXModules/RunCMakeTest.cmake

@@ -171,6 +171,7 @@ run_cxx_module_test(scan-with-pch)
 if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
   run_cxx_module_test(simple)
   run_cxx_module_test(library library-static -DBUILD_SHARED_LIBS=OFF)
+  run_cxx_module_test(unity-build)
   run_cxx_module_test(object-library)
   run_cxx_module_test(generated)
   run_cxx_module_test(deep-chain)

+ 26 - 0
Tests/RunCMake/CXXModules/examples/unity-build/CMakeLists.txt

@@ -0,0 +1,26 @@
+cmake_minimum_required(VERSION 3.28)
+project(cxx_modules_unity CXX)
+
+include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
+
+set(CMAKE_UNITY_BUILD 1)
+
+add_executable(unity)
+target_sources(unity
+  PRIVATE
+    main.cxx
+    unity1.cxx
+    unity2.cxx
+  PRIVATE
+    FILE_SET CXX_MODULES
+      BASE_DIRS
+        "${CMAKE_CURRENT_SOURCE_DIR}"
+      FILES
+        importable.cxx)
+target_compile_features(unity PUBLIC cxx_std_20)
+
+set_property(SOURCE unity1.cxx unity2.cxx
+  PROPERTY
+    CXX_SCAN_FOR_MODULES 0)
+
+add_test(NAME unity COMMAND unity)

+ 10 - 0
Tests/RunCMake/CXXModules/examples/unity-build/importable.cxx

@@ -0,0 +1,10 @@
+module;
+
+#include "unity.h"
+
+export module importable;
+
+export int from_import()
+{
+  return unity1() + unity2();
+}

+ 6 - 0
Tests/RunCMake/CXXModules/examples/unity-build/main.cxx

@@ -0,0 +1,6 @@
+import importable;
+
+int main(int argc, char* argv[])
+{
+  return from_import();
+}

+ 7 - 0
Tests/RunCMake/CXXModules/examples/unity-build/unity.h

@@ -0,0 +1,7 @@
+#ifndef unity_h
+#define unity_h
+
+int unity1();
+int unity2();
+
+#endif

+ 8 - 0
Tests/RunCMake/CXXModules/examples/unity-build/unity1.cxx

@@ -0,0 +1,8 @@
+#include "unity.h"
+
+#define DETECT_UNITY
+
+int unity1()
+{
+  return 0;
+}

+ 10 - 0
Tests/RunCMake/CXXModules/examples/unity-build/unity2.cxx

@@ -0,0 +1,10 @@
+#include "unity.h"
+
+#ifndef DETECT_UNITY
+#  error "Should have detected a unity build"
+#endif
+
+int unity2()
+{
+  return 0;
+}