Skip to content

[cxx-interop] Support CxxStdlib overlay for libc++ on Linux #78014

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 1 commit into from
Jan 20, 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
6 changes: 0 additions & 6 deletions lib/AST/ModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ void ModuleLoader::findOverlayFiles(SourceLoc diagLoc, ModuleDecl *module,
using namespace llvm::sys;
using namespace file_types;

// If an overlay for CxxStdlib was requested, only proceed if compiling with
// the platform-default C++ stdlib.
if (module->getName() == module->getASTContext().Id_CxxStdlib &&
!module->getASTContext().LangOpts.isUsingPlatformDefaultCXXStdlib())
return;

// If cross import information is passed on command-line, prefer use that.
auto &crossImports = module->getASTContext().SearchPathOpts.CrossImportInfo;
auto overlays = crossImports.find(module->getNameStr());
Expand Down
6 changes: 2 additions & 4 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4355,10 +4355,8 @@ ModuleDecl *ClangModuleUnit::getOverlayModule() const {
}
// If this Clang module is a part of the C++ stdlib, and we haven't loaded
// the overlay for it so far, it is a split libc++ module (e.g. std_vector).
// Load the CxxStdlib overlay explicitly, if building with the
// platform-default C++ stdlib.
if (!overlay && importer::isCxxStdModule(clangModule) &&
Ctx.LangOpts.isUsingPlatformDefaultCXXStdlib()) {
// Load the CxxStdlib overlay explicitly.
if (!overlay && importer::isCxxStdModule(clangModule)) {
ImportPath::Module::Builder builder(Ctx.Id_CxxStdlib);
overlay = owner.loadModule(SourceLoc(), std::move(builder).get());
}
Expand Down
9 changes: 9 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ void CompilerInvocation::computeCXXStdlibOptions() {
LangOpts.CXXStdlib = toCXXStdlibKind(cxxStdlibKind);
LangOpts.PlatformDefaultCXXStdlib = toCXXStdlibKind(cxxDefaultStdlibKind);
}

if (!LangOpts.isUsingPlatformDefaultCXXStdlib()) {
// The CxxStdlib overlay was built for the platform default C++ stdlib, and
// its .swiftmodule file refers to implementation-specific symbols (such as
// namespace __1 in libc++, or namespace __cxx11 in libstdc++). Let's
// proactively rebuild the CxxStdlib module from its .swiftinterface if a
// non-default C++ stdlib is used.
FrontendOpts.PreferInterfaceForModules.push_back("CxxStdlib");
}
}

void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
Expand Down
9 changes: 9 additions & 0 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,15 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(

GenericArgs.push_back(
ArgSaver.save("-cxx-interoperability-mode=" + compatVersion));

if (!langOpts.isUsingPlatformDefaultCXXStdlib() &&
langOpts.CXXStdlib == CXXStdlibKind::Libcxx) {
genericSubInvocation.getLangOptions().CXXStdlib = CXXStdlibKind::Libcxx;
genericSubInvocation.getClangImporterOptions().ExtraArgs.push_back(
"-stdlib=libc++");
GenericArgs.push_back("-Xcc");
GenericArgs.push_back("-stdlib=libc++");
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-chrono-libcxx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This test runs another test, use-std-chrono.swift, with libc++ explicitly specified as the C++ stdlib.

// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/use-std-chrono.swift -I %S/Inputs -o %t/exe -cxx-interoperability-mode=upcoming-swift -Xcc -stdlib=libc++
// RUN: %target-codesign %t/exe
// RUN: %target-run %t/exe

// REQUIRES: executable_test
// REQUIRES: OS=linux-gnu
// REQUIRES: system_wide_libcxx
10 changes: 10 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-string-view-libcxx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This test runs another test, use-std-string-view.swift, with libc++ explicitly specified as the C++ stdlib.

// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/use-std-string-view.swift -I %S/Inputs -o %t/exe -cxx-interoperability-mode=upcoming-swift -Xcc -stdlib=libc++
// RUN: %target-codesign %t/exe
// RUN: %target-run %t/exe

// REQUIRES: executable_test
// REQUIRES: OS=linux-gnu
// REQUIRES: system_wide_libcxx