@@ -1518,8 +1518,14 @@ function(add_swift_target_library_single target name)
15181518 "${SWIFT_NATIVE_SWIFT_TOOLS_PATH} /../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK} _LIB_SUBDIR}" )
15191519 target_link_directories (${target_static} PRIVATE
15201520 ${library_search_directories} )
1521+
1522+ _list_add_string_suffix(
1523+ "${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES} "
1524+ "-static"
1525+ target_private_libs)
1526+
15211527 target_link_libraries ("${target_static} " PRIVATE
1522- ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES } )
1528+ ${target_private_libs } )
15231529
15241530 # Force executables linker language to be CXX so that we do not link using the
15251531 # host toolchain swiftc.
@@ -2539,7 +2545,8 @@ endfunction()
25392545# The Swift installation component that this executable belongs to.
25402546# Defaults to never_install.
25412547function (_add_swift_target_executable_single name )
2542- set (options )
2548+ set (options
2549+ NOSWIFTRT)
25432550 set (single_parameter_options
25442551 ARCHITECTURE
25452552 SDK
@@ -2617,6 +2624,15 @@ function(_add_swift_target_executable_single name)
26172624 ${SWIFTEXE_SINGLE_SOURCES}
26182625 ${SWIFTEXE_SINGLE_EXTERNAL_SOURCES} )
26192626
2627+ # ELF and COFF need swiftrt
2628+ if (("${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _OBJECT_FORMAT}" STREQUAL "ELF" OR
2629+ "${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _OBJECT_FORMAT}" STREQUAL "COFF" )
2630+ AND NOT SWIFTEXE_SINGLE_NOSWIFTRT)
2631+ target_sources (${name}
2632+ PRIVATE
2633+ $<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _OBJECT_FORMAT}-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _LIB_SUBDIR}-${SWIFTEXE_SINGLE_ARCHITECTURE} >)
2634+ endif ()
2635+
26202636 add_dependencies_multiple_targets(
26212637 TARGETS "${name} "
26222638 DEPENDS
@@ -2680,6 +2696,24 @@ function(_add_swift_target_executable_single name)
26802696 set_target_properties (${name} PROPERTIES FOLDER "Swift executables" )
26812697endfunction ()
26822698
2699+ # Conditionally append -static to a name, if that variant is a valid target
2700+ function (append_static name result_var_name)
2701+ cmake_parse_arguments (APPEND_TARGET
2702+ "STATIC_SWIFT_STDLIB"
2703+ ""
2704+ ""
2705+ ${ARGN} )
2706+ if (STATIC_SWIFT_STDLIB)
2707+ if (TARGET "${name} -static" )
2708+ set ("${result_var_name} " "${name} -static" PARENT_SCOPE)
2709+ else ()
2710+ set ("${result_var_name} " "${name} " PARENT_SCOPE)
2711+ endif ()
2712+ else ()
2713+ set ("${result_var_name} " "${name} " PARENT_SCOPE)
2714+ endif ()
2715+ endfunction ()
2716+
26832717# Add an executable for each target variant. Executables are given suffixes
26842718# with the variant SDK and ARCH.
26852719#
@@ -2688,7 +2722,9 @@ function(add_swift_target_executable name)
26882722 set (SWIFTEXE_options
26892723 EXCLUDE_FROM_ALL
26902724 BUILD_WITH_STDLIB
2691- BUILD_WITH_LIBEXEC)
2725+ BUILD_WITH_LIBEXEC
2726+ PREFER_STATIC
2727+ NOSWIFTRT)
26922728 set (SWIFTEXE_single_parameter_options
26932729 INSTALL_IN_COMPONENT)
26942730 set (SWIFTEXE_multiple_parameter_options
@@ -2874,8 +2910,12 @@ function(add_swift_target_executable name)
28742910 list (APPEND swiftexe_module_dependency_targets
28752911 "swift${mod}${MODULE_VARIANT_SUFFIX} " )
28762912
2877- list (APPEND swiftexe_link_libraries_targets
2878- "swift${mod}${VARIANT_SUFFIX} " )
2913+ set (library_target "swift${mod}${VARIANT_SUFFIX} " )
2914+ if (SWIFTEXE_TARGET_PREFER_STATIC AND TARGET "${library_target} -static" )
2915+ set (library_target "${library_target} -static" )
2916+ endif ()
2917+
2918+ list (APPEND swiftexe_link_libraries_targets "${library_target} " )
28792919 endforeach ()
28802920
28812921 # Don't add the ${arch} to the suffix. We want to link against fat
@@ -2887,6 +2927,7 @@ function(add_swift_target_executable name)
28872927
28882928 _add_swift_target_executable_single(
28892929 ${VARIANT_NAME}
2930+ ${SWIFTEXE_TARGET_NOSWIFTRT_keyword}
28902931 ${SWIFTEXE_TARGET_SOURCES}
28912932 DEPENDS
28922933 ${SWIFTEXE_TARGET_DEPENDS_with_suffix}
0 commit comments