diff --git a/Runtimes/Overlay/CMakeLists.txt b/Runtimes/Overlay/CMakeLists.txt index 9b8b0c600f1a2..38bb4ae54b7a6 100644 --- a/Runtimes/Overlay/CMakeLists.txt +++ b/Runtimes/Overlay/CMakeLists.txt @@ -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$<$>:_static>$<$:/${SwiftOverlay_PLATFORM_SUBDIR}/${SwiftOverlay_ARCH_SUBDIR}>") @@ -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( + $<$:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>) + add_compile_options( $<$:-explicit-module-build> $<$:-nostdlibimport> - "$<$:SHELL:-Xfrontend -enforce-exclusivity=unchecked>" - "$<$:SHELL:-Xfrontend -target-min-inlining-version -Xfrontend min>" + $<$:-strict-memory-safety> "$<$:SHELL:-Xfrontend -enable-lexical-lifetimes=false>" - "$<$:SHELL:-Xfrontend -enable-ossa-modules>" - "$<$,$>:-enable-library-evolution>" "$<$:SHELL:-Xfrontend -disable-implicit-concurrency-module-import>" - "$<$:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>") + "$<$:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>" + "$<$:SHELL:-Xfrontend -enforce-exclusivity=unchecked>" + "$<$:SHELL:-Xfrontend -enable-ossa-modules>" + "$<$:SHELL:-Xfrontend -target-min-inlining-version -Xfrontend min>" + "$<$,$>:-enable-library-evolution>" + "$<$,$>: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' @@ -87,9 +95,6 @@ add_compile_options( # a compromise, treat all linker warnings as errors. add_link_options($<$:LINKER:/WX>) -add_compile_definitions( - $<$:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>) - include(ExperimentalFeatures) add_subdirectory(clang) @@ -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") diff --git a/Runtimes/Overlay/Cxx/CMakeLists.txt b/Runtimes/Overlay/Cxx/CMakeLists.txt new file mode 100644 index 0000000000000..c9de5b997b0ca --- /dev/null +++ b/Runtimes/Overlay/Cxx/CMakeLists.txt @@ -0,0 +1,45 @@ + +if(NOT APPLE) + add_subdirectory(cxxshim) +endif() +if(LINUX) + 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 + "$<$:-cxx-interoperability-mode=default>" + "$<$:-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). + "$<$:SHELL:-Xcc -nostdinc++>" + "$<$:SHELL:-enable-experimental-feature AllowUnsafeAttribute>" + "$<$:SHELL:-enable-experimental-feature BuiltinModule>" + "$<$: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) diff --git a/Runtimes/Overlay/Cxx/cxxshim/CMakeLists.txt b/Runtimes/Overlay/Cxx/cxxshim/CMakeLists.txt new file mode 100644 index 0000000000000..de0e8b1fa5b7c --- /dev/null +++ b/Runtimes/Overlay/Cxx/cxxshim/CMakeLists.txt @@ -0,0 +1,17 @@ + +add_library(cxxshim INTERFACE) +target_compile_options(cxxshim INTERFACE + "$<$:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/libcxxshim.modulemap>") +target_include_directories(cxxshim INTERFACE + $<$:$>) + +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}") diff --git a/Runtimes/Overlay/Cxx/libstdcxx/CMakeLists.txt b/Runtimes/Overlay/Cxx/libstdcxx/CMakeLists.txt new file mode 100644 index 0000000000000..4050cd5be0efc --- /dev/null +++ b/Runtimes/Overlay/Cxx/libstdcxx/CMakeLists.txt @@ -0,0 +1,16 @@ + +add_library(libstdcxx INTERFACE) +target_compile_options(libstdcxx INTERFACE + "$<$:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/libstdcxx.modulemap>") +target_include_directories(libstdcxx INTERFACE + $<$:$>) + +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}") diff --git a/Runtimes/Overlay/Cxx/std/CMakeLists.txt b/Runtimes/Overlay/Cxx/std/CMakeLists.txt new file mode 100644 index 0000000000000..1b9797b6da2ac --- /dev/null +++ b/Runtimes/Overlay/Cxx/std/CMakeLists.txt @@ -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 + "$<$:SHELL:-Xcc --sysroot -Xcc ${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot>") +target_link_libraries(swiftCxxStdlib PRIVATE + $<$:libstdcxx> + $<$>:cxxshim> + swiftCxx + swiftCore + swift_Builtin_float + $<$:SwiftAndroid> + $<$: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) diff --git a/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake b/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake index abdde72cc85f2..259f24aa20a18 100644 --- a/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake +++ b/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake @@ -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) diff --git a/Runtimes/Resync.cmake b/Runtimes/Resync.cmake index 098d006ba14bd..7f355944d8c1e 100644 --- a/Runtimes/Resync.cmake +++ b/Runtimes/Resync.cmake @@ -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