Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,24 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);

// Pass -L <toolchain-install>/bin/../lib/ to linker to prioritize the
// toolchain's libc++.dylib over the sysroot-provided one. This matches
// what we do for determining which libc++ headers to use.
//
// If the toolchain does not provide libc++.dylib, we will fall back to the
// normal system search paths for finding libc++.dylib (on Darwin, that would
// be in the SDK so we would find Apple's system libc++ instead of using
// LLVM's).
{
llvm::SmallString<128> InstallBinPath =
llvm::StringRef(D.Dir); // <install>/bin
llvm::SmallString<128> InstallLibPath = InstallBinPath;
llvm::sys::path::append(InstallLibPath, "..",
"lib"); // <install>/bin/../lib
CmdArgs.push_back("-L");
CmdArgs.push_back(C.getArgs().MakeArgString(InstallLibPath));
}

// Give --sysroot= preference, over the Apple specific behavior to also use
// --isysroot as the syslibroot.
// We check `OPT__sysroot_EQ` directly instead of `getSysRoot` to make sure we
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Driver/darwin-header-search-libcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"

// Make sure that using -nostdinc, -nostdinc++ or -nostdlib will drop both the toolchain
// Make sure that using -nostdinc, -nostdinc++ or -nostdlibinc will drop both the toolchain
// C++ include path and the sysroot one.
//
// RUN: %clang -### %s -fsyntax-only 2>&1 \
Expand All @@ -116,7 +116,7 @@
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
// RUN: -stdlib=platform \
// RUN: -nostdinc++ \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINCXX %s
// CHECK-LIBCXX-NOSTDINCXX: "-cc1"
Expand Down
39 changes: 39 additions & 0 deletions clang/test/Driver/darwin-link-libcxx-from-toolchain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// UNSUPPORTED: system-windows

// Tests to check that we pass -L <install>/bin/../lib/ to the linker to prioritize the toolchain's
// libc++.dylib over the system's libc++.dylib on Darwin. This matches the behavior we have for
// header search paths, where we prioritize toolchain headers and then fall back to the sysroot ones.

// Check that we pass the right -L to the linker even when -stdlib=libc++ is not passed.
//
// RUN: %clang -### %s 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
// RUN: --check-prefix=CHECK-1 %s
//
// CHECK-1: "/usr/bin/ld"
// CHECK-1: "-L" "[[TOOLCHAIN]]/usr/bin/../lib"

// Check that we pass the right -L to the linker when -stdlib=libc++ is passed, both in the
// case where there is libc++.dylib in the toolchain and when there isn't.
//
// RUN: %clang -### %s 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: -stdlib=libc++ \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
// RUN: --check-prefix=CHECK-2 %s
//
// CHECK-2: "/usr/bin/ld"
// CHECK-2: "-L" "[[TOOLCHAIN]]/usr/bin/../lib"
//
// RUN: %clang -### %s 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: -stdlib=libc++ \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
// RUN: --check-prefix=CHECK-3 %s
//
// CHECK-3: "/usr/bin/ld"
// CHECK-3: "-L" "[[TOOLCHAIN]]/usr/bin/../lib"
Loading