diff --git a/stdlib/public/Cxx/CMakeLists.txt b/stdlib/public/Cxx/CMakeLists.txt index ab465bbd00f31..fd0edf4be6971 100644 --- a/stdlib/public/Cxx/CMakeLists.txt +++ b/stdlib/public/Cxx/CMakeLists.txt @@ -1,156 +1,13 @@ -set(libstdcxx_modulemap_target_list) -foreach(sdk ${SWIFT_SDKS}) - if(NOT ${sdk} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS) - continue() - endif() - - foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) - set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}") - - set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}") - set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}") - - set(libstdcxx_header "libstdcxx.h") - set(libstdcxx_header_out "${module_dir}/libstdcxx.h") - set(libstdcxx_header_out_static "${module_dir_static}/libstdcxx.h") - set(libstdcxx_modulemap "libstdcxx.modulemap") - set(libstdcxx_modulemap_out "${module_dir}/libstdcxx.modulemap") - set(libstdcxx_modulemap_out_static "${module_dir_static}/libstdcxx.modulemap") - - add_custom_command_target( - copy_libstdcxx_modulemap - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} - COMMAND - "${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out}" - OUTPUT ${libstdcxx_modulemap_out} - DEPENDS ${libstdcxx_modulemap} - COMMENT "Copying libstdcxx modulemap to resources") - list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap}) - add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap}) - - add_custom_command_target( - copy_libstdcxx_header - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} - COMMAND - "${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out}" - OUTPUT ${libstdcxx_header_out} - DEPENDS ${libstdcxx_header} - COMMENT "Copying libstdcxx header to resources") - list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header}) - add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header}) - - if(SWIFT_BUILD_STATIC_STDLIB) - add_custom_command_target( - copy_libstdcxx_modulemap_static - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} - COMMAND - "${CMAKE_COMMAND}" "-E" "copy_if_different" - "${libstdcxx_modulemap_out}" "${libstdcxx_modulemap_out_static}" - OUTPUT ${libstdcxx_modulemap_out_static} - DEPENDS ${copy_libstdcxx_modulemap} - COMMENT "Copying libstdcxx modulemap to static resources") - list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap_static}) - add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap_static}) - - add_custom_command_target( - copy_libstdcxx_header_static - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} - COMMAND - "${CMAKE_COMMAND}" "-E" "copy_if_different" - "${libstdcxx_header_out}" "${libstdcxx_header_out_static}" - OUTPUT ${libstdcxx_header_out_static} - DEPENDS ${copy_libstdcxx_header} - COMMENT "Copying libstdcxx header to static resources") - list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header_static}) - add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header_static}) - endif() - - swift_install_in_component(FILES "${libstdcxx_modulemap_out}" - DESTINATION "lib/swift/${arch_subdir}" - COMPONENT sdk-overlay) - swift_install_in_component(FILES "${libstdcxx_header_out}" - DESTINATION "lib/swift/${arch_subdir}" - COMPONENT sdk-overlay) - - if(SWIFT_BUILD_STATIC_STDLIB) - swift_install_in_component(FILES "${libstdcxx_modulemap_out_static}" - DESTINATION "lib/swift_static/${arch_subdir}" - COMPONENT sdk-overlay) - swift_install_in_component(FILES "${libstdcxx_header_out_static}" - DESTINATION "lib/swift_static/${arch_subdir}" - COMPONENT sdk-overlay) - endif() - - if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*") - foreach(bootstrapping "0" "1") - get_bootstrapping_path(bootstrapping_dir ${module_dir} ${bootstrapping}) - set(libstdcxx_modulemap_out_bootstrapping "${bootstrapping_dir}/libstdcxx.modulemap") - set(libstdcxx_header_out_bootstrapping "${bootstrapping_dir}/libstdcxx.h") - - add_custom_command_target(unused_var - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}" - COMMAND - "${CMAKE_COMMAND}" "-E" "copy_if_different" - "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out_bootstrapping}" - - CUSTOM_TARGET_NAME "copy-libstdcxx-modulemap-bootstrapping${bootstrapping}" - OUTPUT "${libstdcxx_modulemap_out_bootstrapping}" - DEPENDS ${libstdcxx_modulemap} - COMMENT "Copying libstdcxx modulemap to resources for bootstrapping${bootstrapping}") - - add_custom_command_target(unused_var - COMMAND - "${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}" - COMMAND - "${CMAKE_COMMAND}" "-E" "copy_if_different" - "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out_bootstrapping}" - - CUSTOM_TARGET_NAME "copy-libstdcxx-header-bootstrapping${bootstrapping}" - OUTPUT "${libstdcxx_header_out_bootstrapping}" - DEPENDS ${libstdcxx_header} - COMMENT "Copying libstdcxx header to resources for bootstrapping${bootstrapping}") - endforeach() - endif() - endforeach() -endforeach() -add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list}) -set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous") -add_dependencies(sdk-overlay libstdcxx-modulemap) - - -# -# C++ Standard Library Overlay. -# -add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY - std.swift +add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY CxxSequence.swift - String.swift - - SWIFT_MODULE_DEPENDS_OSX Darwin - SWIFT_MODULE_DEPENDS_IOS Darwin - SWIFT_MODULE_DEPENDS_TVOS Darwin - SWIFT_MODULE_DEPENDS_WATCHOS Darwin - SWIFT_MODULE_DEPENDS_LINUX Glibc SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} -Xfrontend -enable-experimental-cxx-interop - -Xfrontend -module-interface-preserve-types-as-written - - SWIFT_COMPILE_FLAGS_LINUX - # GCC on Linux is usually located under `/usr`. - # However, Ubuntu 20.04 ships with another GCC installation under `/`, which does not include libstdc++. - # Swift build scripts pass `--sysroot=/` to Clang. By default, Clang tries to find GCC installation under sysroot, - # and if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it attempts to use the GCC without the - # C++ stdlib, which causes a build failure. To fix that, we tell Clang explicitly to use GCC from `/usr`. - -Xcc --gcc-toolchain=/usr + # 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). + -Xcc -nostdinc++ LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" - TARGET_SDKS ALL_APPLE_PLATFORMS LINUX - INSTALL_IN_COMPONENT sdk-overlay - DEPENDS libstdcxx-modulemap) + INSTALL_IN_COMPONENT sdk-overlay) + +add_subdirectory(std) diff --git a/stdlib/public/Cxx/std/CMakeLists.txt b/stdlib/public/Cxx/std/CMakeLists.txt new file mode 100644 index 0000000000000..23e074fd7c78a --- /dev/null +++ b/stdlib/public/Cxx/std/CMakeLists.txt @@ -0,0 +1,156 @@ +set(libstdcxx_modulemap_target_list) +foreach(sdk ${SWIFT_SDKS}) + if(NOT ${sdk} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS) + continue() + endif() + + foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) + set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") + set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}") + + set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}") + set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}") + + set(libstdcxx_header "libstdcxx.h") + set(libstdcxx_header_out "${module_dir}/libstdcxx.h") + set(libstdcxx_header_out_static "${module_dir_static}/libstdcxx.h") + set(libstdcxx_modulemap "libstdcxx.modulemap") + set(libstdcxx_modulemap_out "${module_dir}/libstdcxx.modulemap") + set(libstdcxx_modulemap_out_static "${module_dir_static}/libstdcxx.modulemap") + + add_custom_command_target( + copy_libstdcxx_modulemap + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out}" + OUTPUT ${libstdcxx_modulemap_out} + DEPENDS ${libstdcxx_modulemap} + COMMENT "Copying libstdcxx modulemap to resources") + list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap}) + add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap}) + + add_custom_command_target( + copy_libstdcxx_header + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out}" + OUTPUT ${libstdcxx_header_out} + DEPENDS ${libstdcxx_header} + COMMENT "Copying libstdcxx header to resources") + list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header}) + add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header}) + + if(SWIFT_BUILD_STATIC_STDLIB) + add_custom_command_target( + copy_libstdcxx_modulemap_static + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" + "${libstdcxx_modulemap_out}" "${libstdcxx_modulemap_out_static}" + OUTPUT ${libstdcxx_modulemap_out_static} + DEPENDS ${copy_libstdcxx_modulemap} + COMMENT "Copying libstdcxx modulemap to static resources") + list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap_static}) + add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap_static}) + + add_custom_command_target( + copy_libstdcxx_header_static + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static} + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" + "${libstdcxx_header_out}" "${libstdcxx_header_out_static}" + OUTPUT ${libstdcxx_header_out_static} + DEPENDS ${copy_libstdcxx_header} + COMMENT "Copying libstdcxx header to static resources") + list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header_static}) + add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header_static}) + endif() + + swift_install_in_component(FILES "${libstdcxx_modulemap_out}" + DESTINATION "lib/swift/${arch_subdir}" + COMPONENT sdk-overlay) + swift_install_in_component(FILES "${libstdcxx_header_out}" + DESTINATION "lib/swift/${arch_subdir}" + COMPONENT sdk-overlay) + + if(SWIFT_BUILD_STATIC_STDLIB) + swift_install_in_component(FILES "${libstdcxx_modulemap_out_static}" + DESTINATION "lib/swift_static/${arch_subdir}" + COMPONENT sdk-overlay) + swift_install_in_component(FILES "${libstdcxx_header_out_static}" + DESTINATION "lib/swift_static/${arch_subdir}" + COMPONENT sdk-overlay) + endif() + + if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*") + foreach(bootstrapping "0" "1") + get_bootstrapping_path(bootstrapping_dir ${module_dir} ${bootstrapping}) + set(libstdcxx_modulemap_out_bootstrapping "${bootstrapping_dir}/libstdcxx.modulemap") + set(libstdcxx_header_out_bootstrapping "${bootstrapping_dir}/libstdcxx.h") + + add_custom_command_target(unused_var + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}" + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" + "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out_bootstrapping}" + + CUSTOM_TARGET_NAME "copy-libstdcxx-modulemap-bootstrapping${bootstrapping}" + OUTPUT "${libstdcxx_modulemap_out_bootstrapping}" + DEPENDS ${libstdcxx_modulemap} + COMMENT "Copying libstdcxx modulemap to resources for bootstrapping${bootstrapping}") + + add_custom_command_target(unused_var + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}" + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" + "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out_bootstrapping}" + + CUSTOM_TARGET_NAME "copy-libstdcxx-header-bootstrapping${bootstrapping}" + OUTPUT "${libstdcxx_header_out_bootstrapping}" + DEPENDS ${libstdcxx_header} + COMMENT "Copying libstdcxx header to resources for bootstrapping${bootstrapping}") + endforeach() + endif() + endforeach() +endforeach() +add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list}) +set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous") +add_dependencies(sdk-overlay libstdcxx-modulemap) + + +# +# C++ Standard Library Overlay. +# +add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY + std.swift + String.swift + + SWIFT_MODULE_DEPENDS Cxx + SWIFT_MODULE_DEPENDS_OSX Darwin + SWIFT_MODULE_DEPENDS_IOS Darwin + SWIFT_MODULE_DEPENDS_TVOS Darwin + SWIFT_MODULE_DEPENDS_WATCHOS Darwin + SWIFT_MODULE_DEPENDS_LINUX Glibc + + SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} + -Xfrontend -enable-experimental-cxx-interop + -Xfrontend -module-interface-preserve-types-as-written + + SWIFT_COMPILE_FLAGS_LINUX + # GCC on Linux is usually located under `/usr`. + # However, Ubuntu 20.04 ships with another GCC installation under `/`, which does not include libstdc++. + # Swift build scripts pass `--sysroot=/` to Clang. By default, Clang tries to find GCC installation under sysroot, + # and if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it attempts to use the GCC without the + # C++ stdlib, which causes a build failure. To fix that, we tell Clang explicitly to use GCC from `/usr`. + -Xcc --gcc-toolchain=/usr + + LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" + TARGET_SDKS ALL_APPLE_PLATFORMS LINUX + INSTALL_IN_COMPONENT sdk-overlay + DEPENDS libstdcxx-modulemap) diff --git a/stdlib/public/Cxx/String.swift b/stdlib/public/Cxx/std/String.swift similarity index 100% rename from stdlib/public/Cxx/String.swift rename to stdlib/public/Cxx/std/String.swift diff --git a/stdlib/public/Cxx/libstdcxx.h b/stdlib/public/Cxx/std/libstdcxx.h similarity index 100% rename from stdlib/public/Cxx/libstdcxx.h rename to stdlib/public/Cxx/std/libstdcxx.h diff --git a/stdlib/public/Cxx/libstdcxx.modulemap b/stdlib/public/Cxx/std/libstdcxx.modulemap similarity index 100% rename from stdlib/public/Cxx/libstdcxx.modulemap rename to stdlib/public/Cxx/std/libstdcxx.modulemap diff --git a/stdlib/public/Cxx/std.swift b/stdlib/public/Cxx/std/std.swift similarity index 93% rename from stdlib/public/Cxx/std.swift rename to stdlib/public/Cxx/std/std.swift index f813c220e6fc5..76031ff9451d5 100644 --- a/stdlib/public/Cxx/std.swift +++ b/stdlib/public/Cxx/std/std.swift @@ -11,3 +11,4 @@ //===----------------------------------------------------------------------===// @_exported import std // Clang module +@_exported import Cxx // Swift module diff --git a/test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift b/test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift index 7417d73069d5f..184be7924e998 100644 --- a/test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift +++ b/test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift @@ -3,7 +3,7 @@ // REQUIRES: OS=macosx || OS=linux-gnu import CustomSequence -import std +import Cxx // === SimpleSequence === diff --git a/test/Interop/Cxx/stdlib/overlay/custom-sequence.swift b/test/Interop/Cxx/stdlib/overlay/custom-sequence.swift index 8a801b79aa8fe..e5a5116296c1e 100644 --- a/test/Interop/Cxx/stdlib/overlay/custom-sequence.swift +++ b/test/Interop/Cxx/stdlib/overlay/custom-sequence.swift @@ -5,7 +5,7 @@ import StdlibUnittest import CustomSequence -import std +import Cxx var CxxSequenceTestSuite = TestSuite("CxxSequence")