Skip to content

Overlay: add C++ interop libraries #83347

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
Aug 7, 2025
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
25 changes: 17 additions & 8 deletions Runtimes/Overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ include(ResourceEmbedding)
include("${${PROJECT_NAME}_VENDOR_MODULE_DIR}/Settings.cmake" OPTIONAL)

defaulted_option(SwiftOverlay_ENABLE_REFLECTION "Enable runtime support for mirrors and reflection support")
defaulted_option(SwiftOverlay_ENABLE_CXX_INTEROP "Enable C++ Interop support overlays")

option(SwiftOverlay_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" ON)
set(SwiftOverlay_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>$<$<BOOL:${SwiftOverlay_INSTALL_NESTED_SUBDIR}>:/${SwiftOverlay_PLATFORM_SUBDIR}/${SwiftOverlay_ARCH_SUBDIR}>")
Expand All @@ -67,16 +68,23 @@ option(SwiftOverlay_ENABLE_LIBRARY_EVOLUTION "Generate ABI resilient runtime lib
option(SwiftOverlay_ENABLE_BACKDEPLOYMENT_SUPPORT "Add symbols for runtime backdeployment"
${SwiftCore_ENABLE_BACKDEPLOYMENT_SUPPORT})

add_compile_definitions(
$<$<BOOL:${SwiftOverlay_ENABLE_BACKDEPLOYMENT_SUPPORT}>:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>)

add_compile_options(
$<$<COMPILE_LANGUAGE:Swift>:-explicit-module-build>
$<$<COMPILE_LANGUAGE:Swift>:-nostdlibimport>
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enforce-exclusivity=unchecked>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -target-min-inlining-version -Xfrontend min>"
$<$<COMPILE_LANGUAGE:Swift>:-strict-memory-safety>
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enable-lexical-lifetimes=false>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enable-ossa-modules>"
"$<$<AND:$<BOOL:${SwiftOverlay_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-concurrency-module-import>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>")
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enforce-exclusivity=unchecked>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enable-ossa-modules>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -target-min-inlining-version -Xfrontend min>"
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>"
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_PRESPECIALIZATION}>,$<COMPILE_LANGUAGE:Swift>>:SHELL:-Xfrontend -prespecialize-generic-metadata>")

include(ExperimentalFeatures)

# LNK4049: symbol 'symbol' defined in 'filename.obj' is imported
# LNK4286: symbol 'symbol' defined in 'filename_1.obj' is imported by 'filename_2.obj'
Expand All @@ -87,9 +95,6 @@ add_compile_options(
# a compromise, treat all linker warnings as errors.
add_link_options($<$<PLATFORM_ID:Windows>:LINKER:/WX>)

add_compile_definitions(
$<$<BOOL:${SwiftOverlay_ENABLE_BACKDEPLOYMENT_SUPPORT}>:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>)

include(ExperimentalFeatures)

add_subdirectory(clang)
Expand All @@ -100,6 +105,10 @@ if(WIN32)
add_subdirectory(Windows)
endif()

if(SwiftOverlay_ENABLE_CXX_INTEROP)
add_subdirectory(Cxx)
endif()

# Inter-project install info
export(EXPORT SwiftOverlayTargets
FILE "cmake/SwiftOverlay/SwiftOverlayTargets.cmake")
Expand Down
45 changes: 45 additions & 0 deletions Runtimes/Overlay/Cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

if(NOT APPLE)
add_subdirectory(cxxshim)
endif()
Comment on lines +2 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem correct: CxxShim headers are used on all of the platforms, including Apple ones.

if(LINUX)
Copy link
Member

Choose a reason for hiding this comment

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

I actually need to target a Linux that does not have a libstdc++, but uses libcxx instead. This assumption is incorrect.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough - I think that we can iterate on that subsequently.

add_subdirectory(libstdcxx)
endif()
add_subdirectory(std)

add_library(swiftCxx STATIC
CxxConvertibleToBool.swift
CxxConvertibleToCollection.swift
CxxDictionary.swift
CxxOptional.swift
CxxPair.swift
CxxRandomAccessCollection.swift
CxxSequence.swift
CxxSet.swift
CxxSpan.swift
CxxVector.swift
UnsafeCxxIterators.swift)
set_target_properties(swiftCxx PROPERTIES
Swift_MODULE_NAME Cxx)
target_compile_options(swiftCxx PRIVATE
"$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>"
"$<$<COMPILE_LANGUAGE:Swift>:-warn-implicit-overrides>"
# This module should not pull in the C++ standard library, so we disable it
# explicitly. For functionality that depends on the C++ stdlib, use C++
# stdlib overlay (`swiftstd` module).
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -nostdinc++>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature AllowUnsafeAttribute>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature BuiltinModule>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature Span>")
target_link_libraries(swiftCxx PRIVATE
swiftCore)

install(TARGETS swiftCxx
EXPORT SwiftOverlayTargets
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
emit_swift_interface(swiftCxx)
install_swift_interface(swiftCxx)

embed_manifest(swiftCxx)
17 changes: 17 additions & 0 deletions Runtimes/Overlay/Cxx/cxxshim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

add_library(cxxshim INTERFACE)
target_compile_options(cxxshim INTERFACE
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/libcxxshim.modulemap>")
target_include_directories(cxxshim INTERFACE
$<$<COMPILE_LANGUAGE:Swift>:$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>>)

install(TARGETS cxxshim
EXPORT SwiftOverlayTargets
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES
libcxxshim.h
libcxxshim.modulemap
libcxxstdlibshim.h
DESTINATION "${CMAKE_INSTALL_LIBDIR}/swift/${SwiftOverlay_PLATFORM_SUBDIR}")
16 changes: 16 additions & 0 deletions Runtimes/Overlay/Cxx/libstdcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

add_library(libstdcxx INTERFACE)
target_compile_options(libstdcxx INTERFACE
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/libstdcxx.modulemap>")
target_include_directories(libstdcxx INTERFACE
$<$<COMPILE_LANGUAGE:Swift>:$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>>)

install(TARGETS libstdcxx
EXPORT SwiftOverlayTargets
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES
libstdcxx.h
libstdcxx.modulemap
DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}")
43 changes: 43 additions & 0 deletions Runtimes/Overlay/Cxx/std/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

add_library(swiftCxxStdlib STATIC
std.swift
Chrono.swift
String.swift)
set_target_properties(swiftCxxStdlib PROPERTIES
Swift_MODULE_NAME CxxStdlib)
target_compile_options(swiftCxxStdlib PRIVATE
"-strict-memory-safety"
"-cxx-interoperability-mode=default"
"SHELL:-enable-experimental-feature AllowUnsafeAttribute"
# This flag is unnecessary when building with newer compilers that allow using
# C++ symbols in resilient overlays (see f4204568).
"SHELL:-enable-experimental-feature AssumeResilientCxxTypes"
# The varying modularization of the C++ standard library on different
# platforms makes it difficult to enable MemberImportVisibility for this
# module
"SHELL:-disable-upcoming-feature MemberImportVisibility"
"SHELL:-Xfrontend -module-interface-preserve-types-as-written")
# NOTE: We need to setup the sysroot here as we need to ensure that we pick up
# the module.map from the C++ runtime for the `std` (spelt `CxxStdlib`) import.
target_compile_options(swiftCxxStdlib PRIVATE
"$<$<PLATFORM_ID:Android>:SHELL:-Xcc --sysroot -Xcc ${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot>")
target_link_libraries(swiftCxxStdlib PRIVATE
$<$<PLATFORM_ID:Linux>:libstdcxx>
$<$<NOT:$<PLATFORM_ID:Darwin>>:cxxshim>
swiftCxx
swiftCore
swift_Builtin_float
$<$<PLATFORM_ID:Android>:SwiftAndroid>
$<$<PLATFORM_ID:Windows>:ClangModules>)

install(FILES std.apinotes
DESTINATION ${CMAKE_INSTALL_LIBDIR}/swift/apinotes)
install(TARGETS swiftCxxStdlib
EXPORT SwiftOverlayTargets
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
emit_swift_interface(swiftCxxStdlib)
install_swift_interface(swiftCxxStdlib)

embed_manifest(swiftCxxStdlib)
4 changes: 4 additions & 0 deletions Runtimes/Overlay/cmake/modules/DefaultSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ endmacro()

if(APPLE)
set(SwiftOverlay_ENABLE_REFLECTION_default ON)
set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF)
elseif(CMAKE_SYSTEM_NAME STREQUAL "WASM")
set(SwiftOverlay_ENABLE_REFLECTION_default OFF)
set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF)
elseif(LINUX OR ANDROID OR BSD)
set(SwiftOverlay_ENABLE_REFLECTION_default OFF)
set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF)
elseif(WIN32)
set(SwiftOverlay_ENABLE_REFLECTION_default ON)
set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF)
endif()

include("${SwiftOverlay_VENDOR_MODULE_DIR}/DefaultSettings.cmake" OPTIONAL)
2 changes: 2 additions & 0 deletions Runtimes/Resync.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ copy_library_sources("linker-support" "public/ClangOverlays" "Overlay")
message(STATUS "Clang[${StdlibSources}/public/ClangOverlays] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/clang")
copy_files(public/ClangOverlays Overlay/clang FILES float.swift.gyb)

copy_library_sources("Cxx" "public" "Overlay")

# Android Overlay
message(STATUS "Android modulemaps[${StdlibSources}/Platform] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/Android/clang")
copy_files(public/Platform Overlay/Android/clang
Expand Down