Skip to content

Commit 8b3464d

Browse files
committed
build] Pass target argument to the linker if needed
Following swiftlang#31125 and swiftlang#31612, `-target` is not added automatically to linker flags when that's needed (e.g. when building for Apple SDKs) -- mimic the logic used to add it for compiler flags. Addresses rdar://63138761
1 parent 44bc06b commit 8b3464d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ function(is_darwin_based_sdk sdk_name out_var)
8989
endif()
9090
endfunction()
9191

92+
function(does_compiler_support_target_triple out_var)
93+
# MSVC, clang-cl, gcc don't understand -target.
94+
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT SWIFT_COMPILER_IS_MSVC_LIKE)
95+
set(${out_var} TRUE PARENT_SCOPE)
96+
else()
97+
set(${out_var} FALSE PARENT_SCOPE)
98+
endif()
99+
endfunction()
100+
92101
# Usage:
93102
# _add_host_variant_c_compile_link_flags(name)
94103
function(_add_host_variant_c_compile_link_flags name)
@@ -97,8 +106,8 @@ function(_add_host_variant_c_compile_link_flags name)
97106
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
98107
endif()
99108

100-
# MSVC, clang-cl, gcc don't understand -target.
101-
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT SWIFT_COMPILER_IS_MSVC_LIKE)
109+
does_compiler_support_target_triple(COMPILER_SUPPORTS_TARGET_TRIPLE)
110+
if(COMPILER_SUPPORTS_TARGET_TRIPLE)
102111
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
103112
MACCATALYST_BUILD_FLAVOR ""
104113
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
@@ -555,6 +564,15 @@ function(add_swift_host_library name)
555564
target_link_options(${name} PRIVATE
556565
"LINKER:-current_version,${SWIFT_COMPILER_VERSION}")
557566
endif()
567+
568+
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
569+
does_compiler_support_target_triple(COMPILER_SUPPORTS_TARGET_TRIPLE)
570+
if(COMPILER_SUPPORTS_TARGET_TRIPLE)
571+
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
572+
MACCATALYST_BUILD_FLAVOR ""
573+
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
574+
target_link_options(${name} PRIVATE -target;${target})
575+
endif()
558576
endif()
559577

560578
add_dependencies(dev ${name})

0 commit comments

Comments
 (0)