Selaa lähdekoodia

Xcode: Fix PCH support with Swift & C++

Previously, when a mixed language target ends up with `Swift` as the
`LINKER_LANGUAGE`, the PCH file was not set for the target at all.

Fixes: #21224
Mikko Lehtonen 1 vuosi sitten
vanhempi
sitoutus
77c4d2f9a2

+ 2 - 1
Source/cmGlobalXCodeGenerator.cxx

@@ -3067,7 +3067,8 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
   }
 
   // Precompile Headers
-  std::string pchHeader = gtgt->GetPchHeader(configName, llang);
+  std::string pchHeader =
+    gtgt->GetPchHeader(configName, langForPreprocessorDefinitions);
   if (!pchHeader.empty()) {
     buildSettings->AddAttribute("GCC_PREFIX_HEADER",
                                 this->CreateString(pchHeader));

+ 1 - 0
Tests/CMakeLists.txt

@@ -412,6 +412,7 @@ if(BUILD_TESTING)
     PASS_REGULAR_EXPRESSION "CMake Error at CMakeLists.txt:3 \\(add_executable\\):[ \r\n]*Cannot find source file:[ \r\n]*DoesNotExist/MissingSourceFile.c")
   if(CMake_TEST_Swift)
     ADD_TEST_MACRO(SwiftOnly SwiftOnly)
+    ADD_TEST_MACRO(SwiftMixPCH SwiftMixPCH)
     if(CMake_TEST_XCODE_SWIFT)
       ADD_TEST_MACRO(SwiftMix SwiftMix)
     endif()

+ 9 - 0
Tests/SwiftMixPCH/CMain.c

@@ -0,0 +1,9 @@
+#ifndef PCH_VALUE
+#  error "PCH_VALUE not defined"
+#endif
+
+int main(void)
+{
+  const int value = PCH_VALUE;
+  return value == 42 ? 0 : 1;
+}

+ 15 - 0
Tests/SwiftMixPCH/CMakeLists.txt

@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.15)
+if(POLICY CMP0157)
+  cmake_policy(SET CMP0157 NEW)
+endif()
+
+project(SwiftMixPCH C Swift)
+
+# Swift on Windows only provides a release runtime.
+set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+
+add_executable(SwiftMixPCH CMain.c SwiftFunc.swift)
+target_precompile_headers(SwiftMixPCH PRIVATE pch.h)
+
+# We don't want swift to emit main()
+target_compile_options(SwiftMixPCH PRIVATE "$<$<COMPILE_LANGUAGE:Swift>:-parse-as-library>")

+ 4 - 0
Tests/SwiftMixPCH/SwiftFunc.swift

@@ -0,0 +1,4 @@
+
+func SwiftFunc() -> Int32 {
+    return 0;
+}

+ 1 - 0
Tests/SwiftMixPCH/pch.h

@@ -0,0 +1 @@
+#define PCH_VALUE 42