Skip to content

[cxx-interop] Make Cxx Swift library static #61160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,25 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
this->addLinkLibrary(LinkLibrary("objc", LibraryKind::Library));

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

// Do not try to link Cxx with itself.
if (!getSwiftModule()->getName().is("Cxx"))
this->addLinkLibrary(LinkLibrary("swiftCxx", LibraryKind::Library));

// Only link with std on platforms where the overlay is available.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, the overlay is available on windows as well. This seems wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swiftstd is not built on Windows, only swiftCxx is available currently.
I've tried enabling swiftstd on Windows (#62232) but ran into build issues that I haven't figured out so far.

// Do not try to link std with itself.
if ((target.isOSDarwin() || target.isOSLinux()) &&
!getSwiftModule()->getName().is("Cxx") &&
!getSwiftModule()->getName().is("std"))
this->addLinkLibrary(LinkLibrary("swiftstd", LibraryKind::Library));
}

// FIXME: It'd be better to have the driver invocation or build system that
Expand Down
19 changes: 10 additions & 9 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ function(add_swift_target_library_single target name)
OBJECT_LIBRARY
SHARED
STATIC
NO_LINK_NAME
INSTALL_WITH_SHARED)
set(SWIFTLIB_SINGLE_single_parameter_options
ARCHITECTURE
Expand Down Expand Up @@ -727,6 +728,8 @@ function(add_swift_target_library_single target name)

translate_flag(${SWIFTLIB_SINGLE_STATIC} "STATIC"
SWIFTLIB_SINGLE_STATIC_keyword)
translate_flag(${SWIFTLIB_SINGLE_NO_LINK_NAME} "NO_LINK_NAME"
SWIFTLIB_SINGLE_NO_LINK_NAME_keyword)
if(DEFINED SWIFTLIB_SINGLE_BOOTSTRAPPING)
set(BOOTSTRAPPING_arg "BOOTSTRAPPING" ${SWIFTLIB_SINGLE_BOOTSTRAPPING})
endif()
Expand Down Expand Up @@ -896,6 +899,7 @@ function(add_swift_target_library_single target name)
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
${embed_bitcode_arg}
${SWIFTLIB_SINGLE_STATIC_keyword}
${SWIFTLIB_SINGLE_NO_LINK_NAME_keyword}
ENABLE_LTO "${SWIFTLIB_SINGLE_ENABLE_LTO}"
INSTALL_IN_COMPONENT "${install_in_component}"
MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}"
Expand Down Expand Up @@ -1124,7 +1128,7 @@ function(add_swift_target_library_single target name)
# Set compile and link flags for the non-static target.
# Do these LAST.
set(target_static)
if(SWIFTLIB_SINGLE_IS_STDLIB AND SWIFTLIB_SINGLE_STATIC)
if(SWIFTLIB_SINGLE_IS_STDLIB AND SWIFTLIB_SINGLE_STATIC AND NOT SWIFTLIB_SINGLE_INSTALL_WITH_SHARED)
set(target_static "${target}-static")

# We have already compiled Swift sources. Link everything into a static
Expand Down Expand Up @@ -1643,6 +1647,7 @@ function(add_swift_target_library name)
OBJECT_LIBRARY
SHARED
STATIC
NO_LINK_NAME
INSTALL_WITH_SHARED)
set(SWIFTLIB_single_parameter_options
DEPLOYMENT_VERSION_IOS
Expand Down Expand Up @@ -2137,6 +2142,7 @@ function(add_swift_target_library name)
${name}
${SWIFTLIB_SHARED_keyword}
${SWIFTLIB_STATIC_keyword}
${SWIFTLIB_NO_LINK_NAME_keyword}
${SWIFTLIB_OBJECT_LIBRARY_keyword}
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
${SWIFTLIB_SOURCES}
Expand Down Expand Up @@ -2405,19 +2411,14 @@ function(add_swift_target_library name)
# If we built static variants of the library, create a lipo target for
# them.
set(lipo_target_static)
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC AND NOT SWIFTLIB_INSTALL_WITH_SHARED)
set(THIN_INPUT_TARGETS_STATIC)
foreach(TARGET ${THIN_INPUT_TARGETS})
list(APPEND THIN_INPUT_TARGETS_STATIC "${TARGET}-static")
endforeach()

if(SWIFTLIB_INSTALL_WITH_SHARED)
set(install_subdir "swift")
set(universal_subdir ${SWIFTLIB_DIR})
else()
set(install_subdir "swift_static")
set(universal_subdir ${SWIFTSTATICLIB_DIR})
endif()
set(install_subdir "swift_static")
set(universal_subdir ${SWIFTSTATICLIB_DIR})

set(lipo_target_static
"${name}-${library_subdir}-static")
Expand Down
9 changes: 6 additions & 3 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function(handle_swift_sources
dependency_sibgen_target_out_var_name
sourcesvar externalvar name)
cmake_parse_arguments(SWIFTSOURCES
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC;NO_LINK_NAME"
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR;BOOTSTRAPPING"
"DEPENDS;COMPILE_FLAGS;MODULE_NAME;ENABLE_LTO"
${ARGN})
Expand All @@ -63,6 +63,7 @@ function(handle_swift_sources
EMBED_BITCODE_arg)
translate_flag(${SWIFTSOURCES_STATIC} "STATIC"
STATIC_arg)
translate_flag(${SWIFTSOURCES_NO_LINK_NAME} "NO_LINK_NAME" NO_LINK_NAME_arg)
if(DEFINED SWIFTSOURCES_BOOTSTRAPPING)
set(BOOTSTRAPPING_arg "BOOTSTRAPPING" ${SWIFTSOURCES_BOOTSTRAPPING})
endif()
Expand Down Expand Up @@ -95,7 +96,7 @@ function(handle_swift_sources
endforeach()

set(swift_compile_flags ${SWIFTSOURCES_COMPILE_FLAGS})
if (NOT SWIFTSOURCES_IS_MAIN)
if (NOT SWIFTSOURCES_IS_MAIN AND NOT SWIFTSOURCES_NO_LINK_NAME)
list(APPEND swift_compile_flags "-module-link-name" "${name}")
endif()

Expand Down Expand Up @@ -364,6 +365,8 @@ endfunction()
# [EMBED_BITCODE] # Embed LLVM bitcode into the .o files
# [STATIC] # Also write .swiftmodule etc. to static
# # resource folder
# [NO_LINK_NAME] # Do not pass -module-link-name flag.
# # This disables emission of force load symbol.
# )
function(_compile_swift_files
dependency_target_out_var_name dependency_module_target_out_var_name
Expand All @@ -372,7 +375,7 @@ function(_compile_swift_files
cmake_parse_arguments(SWIFTFILE
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
"OUTPUT;MODULE_NAME;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR;BOOTSTRAPPING"
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR"
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR;NO_LINK_NAME"
Copy link
Member

@etcwilde etcwilde Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is NO_LINK_NAME a multi-value option? It doesn't look like it gets used in this function.

${ARGN})

# Check arguments.
Expand Down
10 changes: 8 additions & 2 deletions stdlib/public/Cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
set(SWIFT_CXX_LIBRARY_KIND STATIC)
if("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WINDOWS")
set(SWIFT_CXX_LIBRARY_KIND SHARED)
endif()

add_swift_target_library(swiftCxx ${SWIFT_CXX_LIBRARY_KIND} NO_LINK_NAME IS_STDLIB
CxxConvertibleToCollection.swift
CxxRandomAccessCollection.swift
CxxSequence.swift
Expand All @@ -10,7 +15,8 @@ add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
-Xcc -nostdinc++

LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
INSTALL_IN_COMPONENT sdk-overlay)
INSTALL_IN_COMPONENT sdk-overlay
INSTALL_WITH_SHARED)

add_subdirectory(std)
add_subdirectory(cxxshim)
3 changes: 2 additions & 1 deletion stdlib/public/Cxx/std/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ add_dependencies(sdk-overlay libstdcxx-modulemap)
#
# C++ Standard Library Overlay.
#
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
add_swift_target_library(swiftstd STATIC NO_LINK_NAME IS_STDLIB
std.swift
String.swift

Expand All @@ -148,4 +148,5 @@ add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
INSTALL_IN_COMPONENT sdk-overlay
INSTALL_WITH_SHARED
DEPENDS libstdcxx-modulemap)
6 changes: 5 additions & 1 deletion test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
// FIXME: Cannot use target-run-simple-swift as it causes a runtime crash (https://github.com/apple/swift/issues/52881)
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -I %S/Inputs -o %t/std-string-overlay -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none
// RUN: %target-codesign %t/std-string-overlay
// RUN: %target-run %t/std-string-overlay
//
// REQUIRES: executable_test
// REQUIRES: OS=macosx || OS=linux-gnu
Expand Down