Skip to content

Commit 20daad3

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 20daad3

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
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: 10 additions & 9 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}"
@@ -1124,7 +1128,7 @@ function(add_swift_target_library_single target name)
11241128
# Set compile and link flags for the non-static target.
11251129
# Do these LAST.
11261130
set(target_static)
1127-
if(SWIFTLIB_SINGLE_IS_STDLIB AND SWIFTLIB_SINGLE_STATIC)
1131+
if(SWIFTLIB_SINGLE_IS_STDLIB AND SWIFTLIB_SINGLE_STATIC AND NOT SWIFTLIB_SINGLE_INSTALL_WITH_SHARED)
11281132
set(target_static "${target}-static")
11291133

11301134
# We have already compiled Swift sources. Link everything into a static
@@ -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}
@@ -2399,19 +2405,14 @@ function(add_swift_target_library name)
23992405
# If we built static variants of the library, create a lipo target for
24002406
# them.
24012407
set(lipo_target_static)
2402-
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
2408+
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC AND NOT SWIFTLIB_INSTALL_WITH_SHARED)
24032409
set(THIN_INPUT_TARGETS_STATIC)
24042410
foreach(TARGET ${THIN_INPUT_TARGETS})
24052411
list(APPEND THIN_INPUT_TARGETS_STATIC "${TARGET}-static")
24062412
endforeach()
24072413

2408-
if(SWIFTLIB_INSTALL_WITH_SHARED)
2409-
set(install_subdir "swift")
2410-
set(universal_subdir ${SWIFTLIB_DIR})
2411-
else()
2412-
set(install_subdir "swift_static")
2413-
set(universal_subdir ${SWIFTSTATICLIB_DIR})
2414-
endif()
2414+
set(install_subdir "swift_static")
2415+
set(universal_subdir ${SWIFTSTATICLIB_DIR})
24152416

24162417
set(lipo_target_static
24172418
"${name}-${library_subdir}-static")

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: 3 additions & 2 deletions
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}
@@ -8,7 +8,8 @@ add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
88
-Xcc -nostdinc++
99

1010
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
11-
INSTALL_IN_COMPONENT sdk-overlay)
11+
INSTALL_IN_COMPONENT sdk-overlay
12+
INSTALL_WITH_SHARED)
1213

1314
add_subdirectory(std)
1415
add_subdirectory(cxxshim)

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 2 additions & 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

@@ -148,4 +148,5 @@ add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
148148
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
149149
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
150150
INSTALL_IN_COMPONENT sdk-overlay
151+
INSTALL_WITH_SHARED
151152
DEPENDS libstdcxx-modulemap)

0 commit comments

Comments
 (0)