From a9509cec7ec5d672194f51071b9e9e54ebd9ccda Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Fri, 2 Sep 2022 15:41:01 -0700 Subject: Ninja: Fix mixed Swift/CXX library target generation With how things were before, mixed Swift/C++ libraries would result in a broken ninja file. `cpp.cpp.o` was emitted by the compiler, but was also being included in the `linkBuild.Outputs` list, so it was being emitted by multiple targets. The fix checks that the source language is Swift before adding it to the list of additional outputs. If it is Swift, this isn't a problem. If it isn't Swift, we don't include it in the list of outputs. On the other side, the C++ file was also being passed as a source file, which the Swift compiler can't compile. So we add the C++ object file as an explicit dependency and the object file is then added to the list of Swift sources. --- Source/cmNinjaNormalTargetGenerator.cxx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index 42f0329..bda8a5f 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/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 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 { -- cgit v0.12 From 399343486f129f28b61683dbd95bec4ff16e60a0 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Mon, 19 Sep 2022 11:34:21 -0700 Subject: Tests: Add swift compiler version to test check Include the swift compiler version in the set of exported variables from CheckSwift.cmake for Swift related tests. --- Tests/CheckSwift.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/CheckSwift.cmake b/Tests/CheckSwift.cmake index fcbae7e..099c298 100644 --- a/Tests/CheckSwift.cmake +++ b/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) -- cgit v0.12 From f6ff19cc9d2ce51791f32b7e4fd0ad6d917a00cc Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Mon, 12 Sep 2022 15:15:24 -0700 Subject: Tests: Add mixed Swift+CXX source test case This test ensures we can configure and build mixed source binaries. The test configures, but fails to verify due to multiple targets emitting the `lib.c.o` and `lib.cpp.o` outputs. Both the clang build step and the swift link step report that they emit the `lib.c.o` and `lib.cpp.o` outputs. The `.o`'s are emitted by clang, not by swift. --- Tests/CMakeLists.txt | 3 +++ Tests/SwiftMixLib/CMakeLists.txt | 6 ++++++ Tests/SwiftMixLib/lib.c | 4 ++++ Tests/SwiftMixLib/lib.cpp | 4 ++++ Tests/SwiftMixLib/lib.swift | 3 +++ Tests/SwiftMixLib/main.swift | 3 +++ 6 files changed, 23 insertions(+) create mode 100644 Tests/SwiftMixLib/CMakeLists.txt create mode 100644 Tests/SwiftMixLib/lib.c create mode 100644 Tests/SwiftMixLib/lib.cpp create mode 100644 Tests/SwiftMixLib/lib.swift create mode 100644 Tests/SwiftMixLib/main.swift diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index abe742e..3540aaa 100644 --- a/Tests/CMakeLists.txt +++ b/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) diff --git a/Tests/SwiftMixLib/CMakeLists.txt b/Tests/SwiftMixLib/CMakeLists.txt new file mode 100644 index 0000000..40d3498 --- /dev/null +++ b/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) diff --git a/Tests/SwiftMixLib/lib.c b/Tests/SwiftMixLib/lib.c new file mode 100644 index 0000000..8eca512 --- /dev/null +++ b/Tests/SwiftMixLib/lib.c @@ -0,0 +1,4 @@ +int add_int(int a, int b) +{ + return a + b; +} diff --git a/Tests/SwiftMixLib/lib.cpp b/Tests/SwiftMixLib/lib.cpp new file mode 100644 index 0000000..3e156b8 --- /dev/null +++ b/Tests/SwiftMixLib/lib.cpp @@ -0,0 +1,4 @@ +int add(int a, int b) +{ + return a + b; +} diff --git a/Tests/SwiftMixLib/lib.swift b/Tests/SwiftMixLib/lib.swift new file mode 100644 index 0000000..61009d8 --- /dev/null +++ b/Tests/SwiftMixLib/lib.swift @@ -0,0 +1,3 @@ +public func add(a: Int, b: Int) -> Int { + a + b +} diff --git a/Tests/SwiftMixLib/main.swift b/Tests/SwiftMixLib/main.swift new file mode 100644 index 0000000..d2e9364 --- /dev/null +++ b/Tests/SwiftMixLib/main.swift @@ -0,0 +1,3 @@ +import SwiftMixedLib + +print(add(a: 1, b: 2)) -- cgit v0.12