Explorar o código

Merge topic 'swift-mix-cxx'

f6ff19cc9d Tests: Add mixed Swift+CXX source test case
399343486f Tests: Add swift compiler version to test check
a9509cec7e Ninja: Fix mixed Swift/CXX library target generation

Acked-by: Kitware Robot <[email protected]>
Merge-request: !7679
Brad King %!s(int64=3) %!d(string=hai) anos
pai
achega
a50346c1d0

+ 14 - 7
Source/cmNinjaNormalTargetGenerator.cxx

@@ -1085,10 +1085,12 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
       this->GetGeneratorTarget()->GetObjectSources(sources, config);
       cmLocalGenerator const* LocalGen = this->GetLocalGenerator();
       for (const auto& source : sources) {
+        const std::string sourcePath = source->GetLanguage() == "Swift"
+          ? this->GetCompiledSourceNinjaPath(source)
+          : this->GetObjectFilePath(source, config);
         oss << " "
-            << LocalGen->ConvertToOutputFormat(
-                 this->GetCompiledSourceNinjaPath(source),
-                 cmOutputConverter::SHELL);
+            << LocalGen->ConvertToOutputFormat(sourcePath,
+                                               cmOutputConverter::SHELL);
       }
       return oss.str();
     }();
@@ -1106,10 +1108,15 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
     std::vector<cmSourceFile const*> sources;
     gt->GetObjectSources(sources, config);
     for (const auto& source : sources) {
-      linkBuild.Outputs.push_back(
-        this->ConvertToNinjaPath(this->GetObjectFilePath(source, config)));
-      linkBuild.ExplicitDeps.emplace_back(
-        this->GetCompiledSourceNinjaPath(source));
+      if (source->GetLanguage() == "Swift") {
+        linkBuild.Outputs.push_back(
+          this->ConvertToNinjaPath(this->GetObjectFilePath(source, config)));
+        linkBuild.ExplicitDeps.emplace_back(
+          this->GetCompiledSourceNinjaPath(source));
+      } else {
+        linkBuild.ExplicitDeps.emplace_back(
+          this->GetObjectFilePath(source, config));
+      }
     }
     linkBuild.Outputs.push_back(vars["SWIFT_MODULE"]);
   } else {

+ 3 - 0
Tests/CMakeLists.txt

@@ -394,6 +394,9 @@ if(BUILD_TESTING)
     if(CMake_TEST_XCODE_SWIFT)
       ADD_TEST_MACRO(SwiftMix SwiftMix)
     endif()
+    if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 5.1)
+      ADD_TEST_MACRO(SwiftMixLib Swifty)
+    endif()
   endif()
   if(CMAKE_Fortran_COMPILER)
     ADD_TEST_MACRO(FortranOnly FortranOnly)

+ 2 - 0
Tests/CheckSwift.cmake

@@ -14,6 +14,7 @@ if(NOT DEFINED CMAKE_Swift_COMPILER)
 project(CheckSwift Swift)
 file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
   \"set(CMAKE_Swift_COMPILER \\\"\${CMAKE_Swift_COMPILER}\\\")\\n\"
+  \"set(CMAKE_Swift_COMPILER_VERSION \\\"\${CMAKE_Swift_COMPILER_VERSION}\\\")\\n\"
   \"set(CMAKE_Swift_FLAGS \\\"\${CMAKE_Swift_FLAGS}\\\")\\n\")
 ")
 
@@ -54,6 +55,7 @@ file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
   message(STATUS "${_desc} - ${CMAKE_Swift_COMPILER}")
 
   set(CMAKE_Swift_COMPILER "${CMAKE_Swift_COMPILER}" CACHE FILEPATH "Swift compiler")
+  set(CMAKE_Swift_COMPILER_VERSION "${CMAKE_Swift_COMPILER_VERSION}" CACHE FILEPATH "Swift compiler version")
   set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS}" CACHE STRING "Swift flags")
 
   mark_as_advanced(CMAKE_Swift_COMPILER)

+ 6 - 0
Tests/SwiftMixLib/CMakeLists.txt

@@ -0,0 +1,6 @@
+cmake_minimum_required(VERSION 3.24)
+project(SwiftMixLib C CXX Swift)
+
+add_library(SwiftMixedLib lib.c lib.cpp lib.swift)
+add_executable(Swifty main.swift)
+target_link_libraries(Swifty PUBLIC SwiftMixedLib)

+ 4 - 0
Tests/SwiftMixLib/lib.c

@@ -0,0 +1,4 @@
+int add_int(int a, int b)
+{
+  return a + b;
+}

+ 4 - 0
Tests/SwiftMixLib/lib.cpp

@@ -0,0 +1,4 @@
+int add(int a, int b)
+{
+  return a + b;
+}

+ 3 - 0
Tests/SwiftMixLib/lib.swift

@@ -0,0 +1,3 @@
+public func add(a: Int, b: Int) -> Int {
+    a + b
+}

+ 3 - 0
Tests/SwiftMixLib/main.swift

@@ -0,0 +1,3 @@
+import SwiftMixedLib
+
+print(add(a: 1, b: 2))