@@ -1550,8 +1550,14 @@ function(add_swift_target_library_single target name)
15501550 "${SWIFT_NATIVE_SWIFT_TOOLS_PATH} /../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK} _LIB_SUBDIR}" )
15511551 target_link_directories (${target_static} PRIVATE
15521552 ${library_search_directories} )
1553+
1554+ _list_add_string_suffix(
1555+ "${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES} "
1556+ "-static"
1557+ target_private_libs)
1558+
15531559 target_link_libraries ("${target_static} " PRIVATE
1554- ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES } )
1560+ ${target_private_libs } )
15551561
15561562 # Force executables linker language to be CXX so that we do not link using the
15571563 # host toolchain swiftc.
@@ -2571,7 +2577,8 @@ endfunction()
25712577# The Swift installation component that this executable belongs to.
25722578# Defaults to never_install.
25732579function (_add_swift_target_executable_single name )
2574- set (options )
2580+ set (options
2581+ NOSWIFTRT)
25752582 set (single_parameter_options
25762583 ARCHITECTURE
25772584 SDK
@@ -2649,6 +2656,15 @@ function(_add_swift_target_executable_single name)
26492656 ${SWIFTEXE_SINGLE_SOURCES}
26502657 ${SWIFTEXE_SINGLE_EXTERNAL_SOURCES} )
26512658
2659+ # ELF and COFF need swiftrt
2660+ if (("${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _OBJECT_FORMAT}" STREQUAL "ELF" OR
2661+ "${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _OBJECT_FORMAT}" STREQUAL "COFF" )
2662+ AND NOT SWIFTEXE_SINGLE_NOSWIFTRT)
2663+ target_sources (${name}
2664+ PRIVATE
2665+ $<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _OBJECT_FORMAT}-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK} _LIB_SUBDIR}-${SWIFTEXE_SINGLE_ARCHITECTURE} >)
2666+ endif ()
2667+
26522668 add_dependencies_multiple_targets(
26532669 TARGETS "${name} "
26542670 DEPENDS
@@ -2712,6 +2728,24 @@ function(_add_swift_target_executable_single name)
27122728 set_target_properties (${name} PROPERTIES FOLDER "Swift executables" )
27132729endfunction ()
27142730
2731+ # Conditionally append -static to a name, if that variant is a valid target
2732+ function (append_static name result_var_name)
2733+ cmake_parse_arguments (APPEND_TARGET
2734+ "STATIC_SWIFT_STDLIB"
2735+ ""
2736+ ""
2737+ ${ARGN} )
2738+ if (STATIC_SWIFT_STDLIB)
2739+ if (TARGET "${name} -static" )
2740+ set ("${result_var_name} " "${name} -static" PARENT_SCOPE)
2741+ else ()
2742+ set ("${result_var_name} " "${name} " PARENT_SCOPE)
2743+ endif ()
2744+ else ()
2745+ set ("${result_var_name} " "${name} " PARENT_SCOPE)
2746+ endif ()
2747+ endfunction ()
2748+
27152749# Add an executable for each target variant. Executables are given suffixes
27162750# with the variant SDK and ARCH.
27172751#
@@ -2720,7 +2754,9 @@ function(add_swift_target_executable name)
27202754 set (SWIFTEXE_options
27212755 EXCLUDE_FROM_ALL
27222756 BUILD_WITH_STDLIB
2723- BUILD_WITH_LIBEXEC)
2757+ BUILD_WITH_LIBEXEC
2758+ PREFER_STATIC
2759+ NOSWIFTRT)
27242760 set (SWIFTEXE_single_parameter_options
27252761 INSTALL_IN_COMPONENT)
27262762 set (SWIFTEXE_multiple_parameter_options
@@ -2906,8 +2942,12 @@ function(add_swift_target_executable name)
29062942 list (APPEND swiftexe_module_dependency_targets
29072943 "swift${mod}${MODULE_VARIANT_SUFFIX} " )
29082944
2909- list (APPEND swiftexe_link_libraries_targets
2910- "swift${mod}${VARIANT_SUFFIX} " )
2945+ set (library_target "swift${mod}${VARIANT_SUFFIX} " )
2946+ if (SWIFTEXE_TARGET_PREFER_STATIC AND TARGET "${library_target} -static" )
2947+ set (library_target "${library_target} -static" )
2948+ endif ()
2949+
2950+ list (APPEND swiftexe_link_libraries_targets "${library_target} " )
29112951 endforeach ()
29122952
29132953 # Don't add the ${arch} to the suffix. We want to link against fat
@@ -2919,6 +2959,7 @@ function(add_swift_target_executable name)
29192959
29202960 _add_swift_target_executable_single(
29212961 ${VARIANT_NAME}
2962+ ${SWIFTEXE_TARGET_NOSWIFTRT_keyword}
29222963 ${SWIFTEXE_TARGET_SOURCES}
29232964 DEPENDS
29242965 ${SWIFTEXE_TARGET_DEPENDS_with_suffix}
0 commit comments