Skip to content

Commit 07aec92

Browse files
committed
[cxx-interop] Pass sysroot to the Clang driver when detecting libstdc++ path
The Swift build scripts pass `-sdk /` to the Swift compiler when building the Swift stdlib and the overlays, and this affects the default include paths of Clang. We need to make sure that libstdc++ path detected by ClangImporter is the same path that will be used by Clang during compilation. To do that, let's pass `--sysroot` to the Clang driver when detecting the libstdc++ path.
1 parent 91c64f7 commit 07aec92

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,12 +876,23 @@ getClangInvocationFileMapping(ASTContext &ctx) {
876876
if (!triple.isOSLinux())
877877
return {};
878878

879+
// Extract the libstdc++ installation path from Clang driver.
879880
auto clangDiags = clang::CompilerInstance::createDiagnostics(
880881
new clang::DiagnosticOptions());
881882
clang::driver::Driver clangDriver(ctx.ClangImporterOpts.clangPath,
882883
triple.str(), *clangDiags);
884+
llvm::opt::InputArgList clangDriverArgs;
885+
// If an SDK path was explicitly passed to Swift, make sure to pass it to
886+
// Clang driver as well. It affects the resulting include paths.
887+
auto sdkPath = ctx.SearchPathOpts.getSDKPath();
888+
if (!sdkPath.empty()) {
889+
unsigned argIndex = clangDriverArgs.MakeIndex("--sysroot", sdkPath);
890+
clangDriverArgs.append(new llvm::opt::Arg(
891+
clangDriver.getOpts().getOption(clang::driver::options::OPT__sysroot),
892+
sdkPath, argIndex));
893+
}
883894
auto cxxStdlibDirs =
884-
clangDriver.getLibStdCxxIncludePaths(llvm::opt::InputArgList(), triple);
895+
clangDriver.getLibStdCxxIncludePaths(clangDriverArgs, triple);
885896
if (cxxStdlibDirs.empty()) {
886897
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
887898
return {};

0 commit comments

Comments
 (0)