Skip to content

Commit d41dd85

Browse files
committed
[Swift/C++ interop] Find Swiftrt file
The Windows toolchains keep the Swift runtime in the SDK instead of in the toolchain. The driver lists the runtime locations for the toolchain, but doesn't list them for the SDK, so we don't really have a great way to ask for the location. Instead, we translate the system processor to the name of the architecture directory in the swift SDK and grab the file directly.
1 parent 34dccdc commit d41dd85

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

3_bidirectional_cxx_interop/cmake/modules/InitializeSwift.cmake

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
#
66
# See https://swift.org/LICENSE.txt for license information
77

8+
# Compute the name of the architecture directory on Windows from the CMake
9+
# system processor name.
10+
function(_swift_windows_arch_name output_variable_name target_arch)
11+
if(NOT WIN32)
12+
return()
13+
endif()
14+
15+
if("${target_arch}" STREQUAL "AMD64")
16+
set("${output_variable_name}" "x86_64" PARENT_SCOPE)
17+
elseif("${target_arch}" STREQUAL "ARM64")
18+
set("${output_variable_name}" "aarch64" PARENT_SCOPE)
19+
else()
20+
message(FATAL_ERROR "Unknown windows architecture: ${target_arch}")
21+
endif()
22+
endfunction()
23+
824
# Compute flags and search paths
925
# NOTE: This logic will eventually move to CMake
1026
function(_setup_swift_paths)
@@ -55,7 +71,11 @@ function(_setup_swift_paths)
5571

5672
link_directories(${SWIFT_LIBRARY_SEARCH_PATHS})
5773

58-
if(NOT APPLE)
74+
if(WIN32)
75+
_swift_windows_arch_name(SWIFT_WIN_ARCH_DIR "${CMAKE_SYSTEM_PROCESSOR}")
76+
set(SWIFT_SWIFTRT_FILE "$ENV{SDKROOT}/usr/lib/swift/windows/${SWIFT_WIN_ARCH_DIR}/swiftrt.obj")
77+
add_link_options("$<$<LINK_LANGUAGE:Swift>:${SWIFT_SWIFTRT_FILE}>")
78+
elseif(NOT APPLE)
5979
find_file(SWIFT_SWIFTRT_FILE
6080
swiftrt.o
6181
PATHS ${SWIFT_LIBRARY_SEARCH_PATHS}

0 commit comments

Comments
 (0)