Skip to content

Commit 1f458f5

Browse files
committed
Overlay: add C++ interop libraries
This introduces a build of the C++ interop runtime support to the new runtime library build.
1 parent e9f19fc commit 1f458f5

File tree

7 files changed

+144
-8
lines changed

7 files changed

+144
-8
lines changed

Runtimes/Overlay/CMakeLists.txt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ include(ResourceEmbedding)
5757
include("${${PROJECT_NAME}_VENDOR_MODULE_DIR}/Settings.cmake" OPTIONAL)
5858

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

6162
option(SwiftOverlay_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" ON)
6263
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}>")
@@ -67,16 +68,23 @@ option(SwiftOverlay_ENABLE_LIBRARY_EVOLUTION "Generate ABI resilient runtime lib
6768
option(SwiftOverlay_ENABLE_BACKDEPLOYMENT_SUPPORT "Add symbols for runtime backdeployment"
6869
${SwiftCore_ENABLE_BACKDEPLOYMENT_SUPPORT})
6970

71+
add_compile_definitions(
72+
$<$<BOOL:${SwiftOverlay_ENABLE_BACKDEPLOYMENT_SUPPORT}>:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>)
73+
7074
add_compile_options(
7175
$<$<COMPILE_LANGUAGE:Swift>:-explicit-module-build>
7276
$<$<COMPILE_LANGUAGE:Swift>:-nostdlibimport>
73-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enforce-exclusivity=unchecked>"
74-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -target-min-inlining-version -Xfrontend min>"
77+
$<$<COMPILE_LANGUAGE:Swift>:-strict-memory-safety>
7578
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enable-lexical-lifetimes=false>"
76-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enable-ossa-modules>"
77-
"$<$<AND:$<BOOL:${SwiftOverlay_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>"
7879
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-concurrency-module-import>"
79-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>")
80+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>"
81+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enforce-exclusivity=unchecked>"
82+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -enable-ossa-modules>"
83+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -target-min-inlining-version -Xfrontend min>"
84+
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>"
85+
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_PRESPECIALIZATION}>,$<COMPILE_LANGUAGE:Swift>>:SHELL:-Xfrontend -prespecialize-generic-metadata>")
86+
87+
include(ExperimentalFeatures)
8088

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

90-
add_compile_definitions(
91-
$<$<BOOL:${SwiftOverlay_ENABLE_BACKDEPLOYMENT_SUPPORT}>:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>)
92-
9398
include(ExperimentalFeatures)
9499

95100
add_subdirectory(clang)
@@ -100,6 +105,10 @@ if(WIN32)
100105
add_subdirectory(Windows)
101106
endif()
102107

108+
if(SwiftOverlay_ENABLE_CXX_INTEROP)
109+
add_subdirectory(Cxx)
110+
endif()
111+
103112
# Inter-project install info
104113
export(EXPORT SwiftOverlayTargets
105114
FILE "cmake/SwiftOverlay/SwiftOverlayTargets.cmake")

Runtimes/Overlay/Cxx/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
if(NOT APPLE)
3+
add_subdirectory(cxxshim)
4+
endif()
5+
if(LINUX)
6+
add_subdirectory(libstdcxx)
7+
endif()
8+
add_subdirectory(std)
9+
10+
add_library(swiftCxx STATIC
11+
CxxConvertibleToBool.swift
12+
CxxConvertibleToCollection.swift
13+
CxxDictionary.swift
14+
CxxOptional.swift
15+
CxxPair.swift
16+
CxxRandomAccessCollection.swift
17+
CxxSequence.swift
18+
CxxSet.swift
19+
CxxSpan.swift
20+
CxxVector.swift
21+
UnsafeCxxIterators.swift)
22+
set_target_properties(swiftCxx PROPERTIES
23+
Swift_MODULE_NAME Cxx)
24+
target_compile_options(swiftCxx PRIVATE
25+
"$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>"
26+
"$<$<COMPILE_LANGUAGE:Swift>:-warn-implicit-overrides>"
27+
# This module should not pull in the C++ standard library, so we disable it
28+
# explicitly. For functionality that depends on the C++ stdlib, use C++
29+
# stdlib overlay (`swiftstd` module).
30+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -nostdinc++>"
31+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature AllowUnsafeAttribute>"
32+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature BuiltinModule>"
33+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature Span>")
34+
target_link_libraries(swiftCxx PRIVATE
35+
swiftCore)
36+
37+
install(TARGETS swiftCxx
38+
EXPORT SwiftOverlayTargets
39+
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
40+
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
41+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
42+
emit_swift_interface(swiftCxx)
43+
install_swift_interface(swiftCxx)
44+
45+
embed_manifest(swiftCxx)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
add_library(cxxshim INTERFACE)
3+
target_compile_options(cxxshim INTERFACE
4+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/libcxxshim.modulemap>")
5+
target_include_directories(cxxshim INTERFACE
6+
$<$<COMPILE_LANGUAGE:Swift>:$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>>)
7+
8+
install(TARGETS cxxshim
9+
EXPORT SwiftOverlayTargets
10+
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
11+
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
12+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
13+
install(FILES
14+
libcxxshim.h
15+
libcxxshim.modulemap
16+
libcxxstdlibshim.h
17+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/swift/${SwiftOverlay_PLATFORM_SUBDIR}")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
add_library(libstdcxx INTERFACE)
3+
target_compile_options(libstdcxx INTERFACE
4+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/libstdcxx.modulemap>")
5+
target_include_directories(libstdcxx INTERFACE
6+
$<$<COMPILE_LANGUAGE:Swift>:$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>>)
7+
8+
install(TARGETS libstdcxx
9+
EXPORT SwiftOverlayTargets
10+
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
11+
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
12+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
13+
install(FILES
14+
libstdcxx.h
15+
libstdcxx.modulemap
16+
DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
add_library(swiftCxxStdlib STATIC
3+
std.swift
4+
Chrono.swift
5+
String.swift)
6+
set_target_properties(swiftCxxStdlib PROPERTIES
7+
Swift_MODULE_NAME CxxStdlib)
8+
target_compile_options(swiftCxxStdlib PRIVATE
9+
"-strict-memory-safety"
10+
"-cxx-interoperability-mode=default"
11+
"SHELL:-enable-experimental-feature AllowUnsafeAttribute"
12+
# This flag is unnecessary when building with newer compilers that allow using
13+
# C++ symbols in resilient overlays (see f4204568).
14+
"SHELL:-enable-experimental-feature AssumeResilientCxxTypes"
15+
# The varying modularization of the C++ standard library on different
16+
# platforms makes it difficult to enable MemberImportVisibility for this
17+
# module
18+
"SHELL:-disable-upcoming-feature MemberImportVisibility"
19+
"SHELL:-Xfrontend -module-interface-preserve-types-as-written")
20+
# NOTE: We need to setup the sysroot here as we need to ensure that we pick up
21+
# the module.map from the C++ runtime for the `std` (spelt `CxxStdlib`) import.
22+
target_compile_options(swiftCxxStdlib PRIVATE
23+
"$<$<PLATFORM_ID:Android>:SHELL:-Xcc --sysroot -Xcc ${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot>")
24+
target_link_libraries(swiftCxxStdlib PRIVATE
25+
$<$<PLATFORM_ID:Linux>:libstdcxx>
26+
$<$<NOT:$<PLATFORM_ID:Darwin>>:cxxshim>
27+
swiftCxx
28+
swiftCore
29+
swift_Builtin_float
30+
$<$<PLATFORM_ID:Android>:SwiftAndroid>
31+
$<$<PLATFORM_ID:Windows>:ClangModules>)
32+
33+
install(FILES std.apinotes
34+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/swift/apinotes)
35+
install(TARGETS swiftCxxStdlib
36+
EXPORT SwiftOverlayTargets
37+
ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
38+
LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}"
39+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
40+
emit_swift_interface(swiftCxxStdlib)
41+
install_swift_interface(swiftCxxStdlib)
42+
43+
embed_manifest(swiftCxxStdlib)

Runtimes/Overlay/cmake/modules/DefaultSettings.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ endmacro()
2626

2727
if(APPLE)
2828
set(SwiftOverlay_ENABLE_REFLECTION_default ON)
29+
set(SwiftOverlay_ENABLE_CXX_INTEROP OFF)
2930
elseif(CMAKE_SYSTEM_NAME STREQUAL "WASM")
3031
set(SwiftOverlay_ENABLE_REFLECTION_default OFF)
32+
set(SwiftOverlay_ENABLE_CXX_INTEROP OFF)
3133
elseif(LINUX OR ANDROID OR BSD)
3234
set(SwiftOverlay_ENABLE_REFLECTION_default OFF)
35+
set(SwiftOverlay_ENABLE_CXX_INTEROP OFF)
3336
elseif(WIN32)
3437
set(SwiftOverlay_ENABLE_REFLECTION_default ON)
38+
set(SwiftOverlay_ENABLE_CXX_INTEROP ON)
3539
endif()
3640

3741
include("${SwiftOverlay_VENDOR_MODULE_DIR}/DefaultSettings.cmake" OPTIONAL)

Runtimes/Resync.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ copy_library_sources("linker-support" "public/ClangOverlays" "Overlay")
122122
message(STATUS "Clang[${StdlibSources}/public/ClangOverlays] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/clang")
123123
copy_files(public/ClangOverlays Overlay/clang FILES float.swift.gyb)
124124

125+
copy_library_sources("Cxx" "public" "Overlay")
126+
125127
# Android Overlay
126128
message(STATUS "Android modulemaps[${StdlibSources}/Platform] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/Android/clang")
127129
copy_files(public/Platform Overlay/Android/clang

0 commit comments

Comments
 (0)