Index: lib/Driver/ToolChains.cpp =================================================================== --- lib/Driver/ToolChains.cpp +++ lib/Driver/ToolChains.cpp @@ -4161,6 +4161,17 @@ const std::string OSLibDir = getOSLibDir(Triple, Args); const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); + // Similar to the logic for GCC below, if we currently running Clang inside + // of the requested system root, add its parent library paths to those + // searched. + // FIXME: It's not clear whether we should use the driver's installed + // directory ('Dir' below) or the ResourceDir. + if (StringRef(D.Dir).startswith(SysRoot)) { + addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); + addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); + addPathIfExists(D, D.Dir + "/../lib", Paths); + } + // Add the multilib suffixed paths where they are available. if (GCCInstallation.isValid()) { const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); @@ -4214,16 +4225,6 @@ } } - // Similar to the logic for GCC above, if we currently running Clang inside - // of the requested system root, add its parent library paths to - // those searched. - // FIXME: It's not clear whether we should use the driver's installed - // directory ('Dir' below) or the ResourceDir. - if (StringRef(D.Dir).startswith(SysRoot)) { - addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); - addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); - } - addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); @@ -4260,14 +4261,6 @@ addPathIfExists(D, LibPath, Paths); } - // Similar to the logic for GCC above, if we are currently running Clang - // inside of the requested system root, add its parent library path to those - // searched. - // FIXME: It's not clear whether we should use the driver's installed - // directory ('Dir' below) or the ResourceDir. - if (StringRef(D.Dir).startswith(SysRoot)) - addPathIfExists(D, D.Dir + "/../lib", Paths); - addPathIfExists(D, SysRoot + "/lib", Paths); addPathIfExists(D, SysRoot + "/usr/lib", Paths); } Index: test/Driver/clang-libraries.c =================================================================== --- /dev/null +++ test/Driver/clang-libraries.c @@ -0,0 +1,24 @@ +// Check that libraries installed next to Clang are preferred over those from GCC. + +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: --target=x86_64-unknown-linux --gcc-toolchain=%S/Inputs/basic_linux_tree/usr \ +// RUN: | FileCheck --check-prefix=CHECK-DEFAULT %s + +// CHECK-DEFAULT: "[[CLANG_BIN:[^"]+]]/clang" +// CHECK-DEFAULT: "{{.*}}ld{{(.exe)?}}" +// CHECK-DEFAULT-NOT: "-L +// CHECK-DEFAULT: "-L[[CLANG_BIN]]/../lib + + +// Check that manual library paths preceed the default ones. + +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: --target=x86_64-unknown-linux --gcc-toolchain=%S/Inputs/basic_linux_tree/usr \ +// RUN: -L%S/Inputs/basic_linux_tree/usr/lib \ +// RUN: | FileCheck --check-prefix=CHECK-MANUAL %s + +// CHECK-MANUAL: "[[CLANG_BIN:[^"]+]]/clang" +// CHECK-MANUAL: "{{.*}}ld{{(.exe)?}}" +// CHECK-MANUAL: "-L{{[^"]*}}Inputs/basic_linux_tree/usr/lib +// CHECK-MANUAL-NOT: "-L +// CHECK-MANUAL: "-L[[CLANG_BIN]]/../lib