Skip to content

Commit 4410959

Browse files
authored
Merge pull request #61160 from apple/egorzhdan/cxx-static
[cxx-interop] Make `Cxx` Swift library static
2 parents 67909c0 + 5d86f3e commit 4410959

File tree

6 files changed

+43
-18
lines changed

6 files changed

+43
-18
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,25 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
481481
this->addLinkLibrary(LinkLibrary("objc", LibraryKind::Library));
482482

483483
// If C++ interop is enabled, add -lc++ on Darwin and -lstdc++ on linux.
484+
// Also link with C++ bridging utility module (Cxx) and C++ stdlib overlay
485+
// (std) if available.
484486
if (Context.LangOpts.EnableCXXInterop) {
485-
if (Context.LangOpts.Target.isOSDarwin())
487+
const llvm::Triple &target = Context.LangOpts.Target;
488+
if (target.isOSDarwin())
486489
this->addLinkLibrary(LinkLibrary("c++", LibraryKind::Library));
487-
else if (Context.LangOpts.Target.isOSLinux())
490+
else if (target.isOSLinux())
488491
this->addLinkLibrary(LinkLibrary("stdc++", LibraryKind::Library));
489492

490493
// Do not try to link Cxx with itself.
491494
if (!getSwiftModule()->getName().is("Cxx"))
492495
this->addLinkLibrary(LinkLibrary("swiftCxx", LibraryKind::Library));
496+
497+
// Only link with std on platforms where the overlay is available.
498+
// Do not try to link std with itself.
499+
if ((target.isOSDarwin() || target.isOSLinux()) &&
500+
!getSwiftModule()->getName().is("Cxx") &&
501+
!getSwiftModule()->getName().is("std"))
502+
this->addLinkLibrary(LinkLibrary("swiftstd", LibraryKind::Library));
493503
}
494504

495505
// 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
@@ -2137,6 +2142,7 @@ function(add_swift_target_library name)
21372142
${name}
21382143
${SWIFTLIB_SHARED_keyword}
21392144
${SWIFTLIB_STATIC_keyword}
2145+
${SWIFTLIB_NO_LINK_NAME_keyword}
21402146
${SWIFTLIB_OBJECT_LIBRARY_keyword}
21412147
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
21422148
${SWIFTLIB_SOURCES}
@@ -2405,19 +2411,14 @@ function(add_swift_target_library name)
24052411
# If we built static variants of the library, create a lipo target for
24062412
# them.
24072413
set(lipo_target_static)
2408-
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
2414+
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC AND NOT SWIFTLIB_INSTALL_WITH_SHARED)
24092415
set(THIN_INPUT_TARGETS_STATIC)
24102416
foreach(TARGET ${THIN_INPUT_TARGETS})
24112417
list(APPEND THIN_INPUT_TARGETS_STATIC "${TARGET}-static")
24122418
endforeach()
24132419

2414-
if(SWIFTLIB_INSTALL_WITH_SHARED)
2415-
set(install_subdir "swift")
2416-
set(universal_subdir ${SWIFTLIB_DIR})
2417-
else()
2418-
set(install_subdir "swift_static")
2419-
set(universal_subdir ${SWIFTSTATICLIB_DIR})
2420-
endif()
2420+
set(install_subdir "swift_static")
2421+
set(universal_subdir ${SWIFTSTATICLIB_DIR})
24212422

24222423
set(lipo_target_static
24232424
"${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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
1+
set(SWIFT_CXX_LIBRARY_KIND STATIC)
2+
if("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WINDOWS")
3+
set(SWIFT_CXX_LIBRARY_KIND SHARED)
4+
endif()
5+
6+
add_swift_target_library(swiftCxx ${SWIFT_CXX_LIBRARY_KIND} NO_LINK_NAME IS_STDLIB
27
CxxConvertibleToCollection.swift
38
CxxRandomAccessCollection.swift
49
CxxSequence.swift
@@ -10,7 +15,8 @@ add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
1015
-Xcc -nostdinc++
1116

1217
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
13-
INSTALL_IN_COMPONENT sdk-overlay)
18+
INSTALL_IN_COMPONENT sdk-overlay
19+
INSTALL_WITH_SHARED)
1420

1521
add_subdirectory(std)
1622
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)

test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
1+
// FIXME: Cannot use target-run-simple-swift as it causes a runtime crash (https://github.com/apple/swift/issues/52881)
2+
// RUN: %empty-directory(%t)
3+
// RUN: %target-build-swift %s -I %S/Inputs -o %t/std-string-overlay -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none
4+
// RUN: %target-codesign %t/std-string-overlay
5+
// RUN: %target-run %t/std-string-overlay
26
//
37
// REQUIRES: executable_test
48
// REQUIRES: OS=macosx || OS=linux-gnu

0 commit comments

Comments
 (0)