Skip to content

Commit 649889f

Browse files
authored
Merge pull request #59442 from apple/egorzhdan/cxx-overlay-linux-reenable
[cxx-interop] Re-enable C++ stdlib overlay on Linux
2 parents 12d0d2b + 81b18d2 commit 649889f

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,14 @@ getClangInvocationFileMapping(ASTContext &ctx) {
902902
new clang::DiagnosticOptions());
903903
clang::driver::Driver clangDriver(ctx.ClangImporterOpts.clangPath,
904904
triple.str(), *clangDiags);
905-
llvm::opt::InputArgList clangDriverArgs;
905+
// Flags passed to Swift with `-Xcc` might affect include paths.
906+
unsigned unused1, unused2;
907+
std::vector<const char *> clangArgs;
908+
for (const auto &each : ctx.ClangImporterOpts.ExtraArgs) {
909+
clangArgs.push_back(each.c_str());
910+
}
911+
llvm::opt::InputArgList clangDriverArgs =
912+
clangDriver.getOpts().ParseArgs(clangArgs, unused1, unused2);
906913
// If an SDK path was explicitly passed to Swift, make sure to pass it to
907914
// Clang driver as well. It affects the resulting include paths.
908915
auto sdkPath = ctx.SearchPathOpts.getSDKPath();

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,21 @@ add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
135135
SWIFT_MODULE_DEPENDS_IOS Darwin
136136
SWIFT_MODULE_DEPENDS_TVOS Darwin
137137
SWIFT_MODULE_DEPENDS_WATCHOS Darwin
138+
SWIFT_MODULE_DEPENDS_LINUX Glibc
138139

139140
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
140141
-Xfrontend -enable-experimental-cxx-interop
141142
-Xfrontend -module-interface-preserve-types-as-written
142143

143-
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" -lc++
144-
TARGET_SDKS ALL_APPLE_PLATFORMS # TODO: support other platforms as well
145-
INSTALL_IN_COMPONENT sdk-overlay)
144+
SWIFT_COMPILE_FLAGS_LINUX
145+
# GCC on Linux is usually located under `/usr`.
146+
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which does not include libstdc++.
147+
# Swift build scripts pass `--sysroot=/` to Clang. By default, Clang tries to find GCC installation under sysroot,
148+
# and if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it attempts to use the GCC without the
149+
# C++ stdlib, which causes a build failure. To fix that, we tell Clang explicitly to use GCC from `/usr`.
150+
-Xcc --gcc-toolchain=/usr
151+
152+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
153+
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
154+
INSTALL_IN_COMPONENT sdk-overlay
155+
DEPENDS libstdcxx-modulemap)

test/Interop/Cxx/stdlib/overlay/string.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-cxx-interop)
22
//
33
// REQUIRES: executable_test
4-
// REQUIRES: OS=macosx
4+
// REQUIRES: OS=macosx || OS=linux-gnu
55

66
import StdlibUnittest
77
import std

0 commit comments

Comments
 (0)