summaryrefslogtreecommitdiffstats
path: root/Tests/SwiftOnly
diff options
context:
space:
mode:
authorEvan Wilde <etceterawilde@gmail.com>2022-12-31 07:41:16 (GMT)
committerEvan Wilde <etceterawilde@gmail.com>2023-01-19 19:49:24 (GMT)
commit4165eb3d0b21601977b26e8f8af5193c55169cee (patch)
treedefffcae770b9328019490df5135b1df81b20364 /Tests/SwiftOnly
parent06dfd7b7054aa8d78d736c2e5d84f6f873dd48e3 (diff)
downloadCMake-4165eb3d0b21601977b26e8f8af5193c55169cee.zip
CMake-4165eb3d0b21601977b26e8f8af5193c55169cee.tar.gz
CMake-4165eb3d0b21601977b26e8f8af5193c55169cee.tar.bz2
Ninja: Emit swiftmodule from executable with exports
This patch adds support for tracking the swiftmodules for executables exporting symbols. This fixes a bug in the earlier implementation around emitting the swiftmodule. Previously, the code would use `CMAKE_EXE_EXPORTS_Swift_FLAG` to inject the `-emit-module`, and module path information into the `CMAKE_Swift_LINK_EXECUTABLE` rule. Because Swift skips the build step and only runs during the link phase, these flags were injected in `cmNinjaNormalTargetGenerator::ComputeLinkCmd` instead of `cmLocalGenerator::GetTargetFlags` where it is done normally. Unfortunately, injecting in `ComputeLinkCmd` didn't do anything because we have a `linkCmd` so `ComputeLinkCmd` exits early, before the EXE_EXPORT flags get added to the link command. Instead of playing with that flag, CMake checks `CMAKE_Swift_LINK_EXECUTABLE_WITH_EXPORTS` and uses that as the link rule if it exists and falls back on `CMAKE_Swift_LINK_EXECUTABLE`. I've defined that variable in terms of `CMAKE_Swift_LINK_EXECUTABLE` with the necessary additional flags for emitting the swift module instead. This has the same end effect as the desired behavior, but simplifies things a bit. Since we're generating the swiftmodule for executables with exports, I've also updated the dependency graph to include the swiftmodule as an output in the build dependency graph if the executable has exports. Tests updated: - RunCMake/NoWorkToDo: Ensure that the build graph does not result in unnecessary rebuilds with exporting executables. - SwiftOnly: Ensure that we can consume functions defined in the executable by a library getting linked into said executable.
Diffstat (limited to 'Tests/SwiftOnly')
-rw-r--r--Tests/SwiftOnly/CMakeLists.txt11
-rw-r--r--Tests/SwiftOnly/SwiftPlugin/CMakeLists.txt5
-rw-r--r--Tests/SwiftOnly/SwiftPlugin/main.swift4
-rw-r--r--Tests/SwiftOnly/SwiftPlugin/plugin.swift3
4 files changed, 23 insertions, 0 deletions
diff --git a/Tests/SwiftOnly/CMakeLists.txt b/Tests/SwiftOnly/CMakeLists.txt
index fa8687d..13cf2b1 100644
--- a/Tests/SwiftOnly/CMakeLists.txt
+++ b/Tests/SwiftOnly/CMakeLists.txt
@@ -43,3 +43,14 @@ target_link_libraries(N PUBLIC
# Dummy to make sure generation works with such targets.
add_library(SwiftIface INTERFACE)
target_link_libraries(SwiftOnly PRIVATE SwiftIface)
+
+# @_alwaysEmitIntoClient ensures that the function body is inserted into the
+# swiftmodule instead of as a symbol in the binary itself. I'm doing this to
+# avoid having to link the executable. There are some flags required in order to
+# link an executable into a library that I didn't see CMake emitting for Swift
+# on macOS. AEIC is the easiest workaround that still tests this functionality.
+# Unfortunately, AEIC was only added recently (~Swift 5.2), so we need to check
+# that it is available before using it.
+if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 5.2)
+ add_subdirectory("SwiftPlugin")
+endif()
diff --git a/Tests/SwiftOnly/SwiftPlugin/CMakeLists.txt b/Tests/SwiftOnly/SwiftPlugin/CMakeLists.txt
new file mode 100644
index 0000000..4069f16
--- /dev/null
+++ b/Tests/SwiftOnly/SwiftPlugin/CMakeLists.txt
@@ -0,0 +1,5 @@
+add_executable(main main.swift)
+set_target_properties(main PROPERTIES ENABLE_EXPORTS TRUE)
+
+add_library(plugin plugin.swift)
+target_link_libraries(plugin PRIVATE main)
diff --git a/Tests/SwiftOnly/SwiftPlugin/main.swift b/Tests/SwiftOnly/SwiftPlugin/main.swift
new file mode 100644
index 0000000..f5aac51
--- /dev/null
+++ b/Tests/SwiftOnly/SwiftPlugin/main.swift
@@ -0,0 +1,4 @@
+@_alwaysEmitIntoClient
+public func exported() -> Int { 32 }
+
+print(exported())
diff --git a/Tests/SwiftOnly/SwiftPlugin/plugin.swift b/Tests/SwiftOnly/SwiftPlugin/plugin.swift
new file mode 100644
index 0000000..e84f248
--- /dev/null
+++ b/Tests/SwiftOnly/SwiftPlugin/plugin.swift
@@ -0,0 +1,3 @@
+import main
+
+public func importing() -> Int { main.exported() + 1 }