Skip to content

Commit af9bdf5

Browse files
committed
[cxx-interop] Make Cxx Swift library static
Instead of a dynamic `swiftCxx.dylib` library, let's build a static library to simplify backdeployment and reduce potential compatibility difficulties in the future. This also adds `NO_LINK_NAME` option to `add_swift_target_library` to prevent the CMake scripts from passing `-module-link-name` to swiftc when building a given module. This fixes linker errors, which would otherwise occur due to the force-load symbol name (`_swift_FORCE_LOAD_$xyz`) being emitted for the libraries that are now static (`swiftCxx`, `swiftstd`).
1 parent 7d9328e commit af9bdf5

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,17 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
471471
this->addLinkLibrary(LinkLibrary("objc", LibraryKind::Library));
472472

473473
// If C++ interop is enabled, add -lc++ on Darwin and -lstdc++ on linux.
474+
// Also link with C++ bridging utility module (Cxx) and C++ stdlib overlay
475+
// (std) if available.
474476
if (Context.LangOpts.EnableCXXInterop) {
475-
if (Context.LangOpts.Target.isOSDarwin())
477+
this->addLinkLibrary(LinkLibrary("swiftCxx", LibraryKind::Library));
478+
if (Context.LangOpts.Target.isOSDarwin()) {
476479
this->addLinkLibrary(LinkLibrary("c++", LibraryKind::Library));
477-
else if (Context.LangOpts.Target.isOSLinux())
480+
this->addLinkLibrary(LinkLibrary("swiftstd", LibraryKind::Library));
481+
} else if (Context.LangOpts.Target.isOSLinux()) {
478482
this->addLinkLibrary(LinkLibrary("stdc++", LibraryKind::Library));
483+
this->addLinkLibrary(LinkLibrary("swiftstd", LibraryKind::Library));
484+
}
479485
}
480486

481487
// FIXME: It'd be better to have the driver invocation or build system that

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ function(add_swift_target_library_single target name)
689689
OBJECT_LIBRARY
690690
SHARED
691691
STATIC
692+
NO_LINK_NAME
692693
INSTALL_WITH_SHARED)
693694
set(SWIFTLIB_SINGLE_single_parameter_options
694695
ARCHITECTURE
@@ -727,6 +728,8 @@ function(add_swift_target_library_single target name)
727728

728729
translate_flag(${SWIFTLIB_SINGLE_STATIC} "STATIC"
729730
SWIFTLIB_SINGLE_STATIC_keyword)
731+
translate_flag(${SWIFTLIB_SINGLE_NO_LINK_NAME} "NO_LINK_NAME"
732+
SWIFTLIB_SINGLE_NO_LINK_NAME_keyword)
730733
if(DEFINED SWIFTLIB_SINGLE_BOOTSTRAPPING)
731734
set(BOOTSTRAPPING_arg "BOOTSTRAPPING" ${SWIFTLIB_SINGLE_BOOTSTRAPPING})
732735
endif()
@@ -896,6 +899,7 @@ function(add_swift_target_library_single target name)
896899
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
897900
${embed_bitcode_arg}
898901
${SWIFTLIB_SINGLE_STATIC_keyword}
902+
${SWIFTLIB_SINGLE_NO_LINK_NAME_keyword}
899903
ENABLE_LTO "${SWIFTLIB_SINGLE_ENABLE_LTO}"
900904
INSTALL_IN_COMPONENT "${install_in_component}"
901905
MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}"
@@ -1643,6 +1647,7 @@ function(add_swift_target_library name)
16431647
OBJECT_LIBRARY
16441648
SHARED
16451649
STATIC
1650+
NO_LINK_NAME
16461651
INSTALL_WITH_SHARED)
16471652
set(SWIFTLIB_single_parameter_options
16481653
DEPLOYMENT_VERSION_IOS
@@ -2131,6 +2136,7 @@ function(add_swift_target_library name)
21312136
${name}
21322137
${SWIFTLIB_SHARED_keyword}
21332138
${SWIFTLIB_STATIC_keyword}
2139+
${SWIFTLIB_NO_LINK_NAME_keyword}
21342140
${SWIFTLIB_OBJECT_LIBRARY_keyword}
21352141
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
21362142
${SWIFTLIB_SOURCES}

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function(handle_swift_sources
4949
dependency_sibgen_target_out_var_name
5050
sourcesvar externalvar name)
5151
cmake_parse_arguments(SWIFTSOURCES
52-
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
52+
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC;NO_LINK_NAME"
5353
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR;BOOTSTRAPPING"
5454
"DEPENDS;COMPILE_FLAGS;MODULE_NAME;ENABLE_LTO"
5555
${ARGN})
@@ -63,6 +63,7 @@ function(handle_swift_sources
6363
EMBED_BITCODE_arg)
6464
translate_flag(${SWIFTSOURCES_STATIC} "STATIC"
6565
STATIC_arg)
66+
translate_flag(${SWIFTSOURCES_NO_LINK_NAME} "NO_LINK_NAME" NO_LINK_NAME_arg)
6667
if(DEFINED SWIFTSOURCES_BOOTSTRAPPING)
6768
set(BOOTSTRAPPING_arg "BOOTSTRAPPING" ${SWIFTSOURCES_BOOTSTRAPPING})
6869
endif()
@@ -95,7 +96,7 @@ function(handle_swift_sources
9596
endforeach()
9697

9798
set(swift_compile_flags ${SWIFTSOURCES_COMPILE_FLAGS})
98-
if (NOT SWIFTSOURCES_IS_MAIN)
99+
if (NOT SWIFTSOURCES_IS_MAIN AND NOT SWIFTSOURCES_NO_LINK_NAME)
99100
list(APPEND swift_compile_flags "-module-link-name" "${name}")
100101
endif()
101102

@@ -364,6 +365,8 @@ endfunction()
364365
# [EMBED_BITCODE] # Embed LLVM bitcode into the .o files
365366
# [STATIC] # Also write .swiftmodule etc. to static
366367
# # resource folder
368+
# [NO_LINK_NAME] # Do not pass -module-link-name flag.
369+
# # This disables emission of force load symbol.
367370
# )
368371
function(_compile_swift_files
369372
dependency_target_out_var_name dependency_module_target_out_var_name
@@ -372,7 +375,7 @@ function(_compile_swift_files
372375
cmake_parse_arguments(SWIFTFILE
373376
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
374377
"OUTPUT;MODULE_NAME;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR;BOOTSTRAPPING"
375-
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR"
378+
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR;NO_LINK_NAME"
376379
${ARGN})
377380

378381
# Check arguments.

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
1+
add_swift_target_library(swiftCxx STATIC NO_LINK_NAME IS_STDLIB
22
CxxSequence.swift
33

44
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ add_dependencies(sdk-overlay libstdcxx-modulemap)
127127
#
128128
# C++ Standard Library Overlay.
129129
#
130-
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
130+
add_swift_target_library(swiftstd STATIC NO_LINK_NAME IS_STDLIB
131131
std.swift
132132
String.swift
133133

0 commit comments

Comments
 (0)