Skip to content

Commit d35918e

Browse files
carlocabBo98
andcommitted
[Darwin][Driver][clang] Prioritise -isysroot over --sysroot consistently
On Darwin, `clang` currently prioritises `-isysroot` over `--sysroot` when selecting headers, but does the reverse when setting `-syslibroot` for the linker, which determines library selection. This is wrong, because it leads to using headers that are of different versions from the libraries being linked. We can see this from: ❯ bin/clang -v -xc - -o /dev/null \ -isysroot /Applications/Xcode-14.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.sdk \ --sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.sdk <<<'int main(){}' clang version 20.0.0git (https://github.com/llvm/llvm-project.git 3de5dbb) Target: arm64-apple-darwin24.1.0 [snip] #include "..." search starts here: #include <...> search starts here: /Users/carlocab/github/llvm-project/build-clang-config/lib/clang/20/include /Applications/Xcode-14.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.sdk/usr/include /Applications/Xcode-14.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.sdk/System/Library/Frameworks (framework directory) End of search list. "/usr/bin/ld" -demangle -lto_library /Users/carlocab/github/llvm-project/build-clang-config/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 13.3.0 13.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.sdk -mllvm -enable-linkonceodr-outlining -o /dev/null /var/folders/nj/9vfk4f0n377605jhxxmngd8h0000gn/T/--b914c6.o -lSystem We can fix this by reversing the order in which the `-syslibroot` flag is determined. Downstream bug report: Homebrew/homebrew-core#197277 Co-authored-by: Bo Anderson <mail@boanderson.me>
1 parent de0fd64 commit d35918e

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,14 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
428428
Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
429429
Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
430430

431-
// Give --sysroot= preference, over the Apple specific behavior to also use
432-
// --isysroot as the syslibroot.
433-
StringRef sysroot = C.getSysRoot();
434-
if (sysroot != "") {
435-
CmdArgs.push_back("-syslibroot");
436-
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
437-
} else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
431+
// Prioritise -isysroot to ensure that the libraries we link to are taken
432+
// from the same SDK that our headers come from.
433+
if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
438434
CmdArgs.push_back("-syslibroot");
439435
CmdArgs.push_back(A->getValue());
436+
} else if (StringRef sysroot = C.getSysRoot(); sysroot != "") {
437+
CmdArgs.push_back("-syslibroot");
438+
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
440439
}
441440

442441
Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);

clang/test/Driver/sysroot.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// RUN: FileCheck --check-prefix=CHECK-APPLE-ISYSROOT < %t2 %s
1212
// CHECK-APPLE-ISYSROOT: "-arch" "i386"{{.*}} "-syslibroot" "{{[^"]*}}/FOO"
1313

14-
// Check that honor --sysroot= over -isysroot, for Apple Darwin.
14+
// Check that honor -isysroot over --sysroot=, for Apple Darwin.
1515
// RUN: touch %t3.o
1616
// RUN: %clang -target i386-apple-darwin10 \
1717
// RUN: -isysroot /FOO --sysroot=/BAR -### %t3.o 2> %t3
1818
// RUN: FileCheck --check-prefix=CHECK-APPLE-SYSROOT < %t3 %s
19-
// CHECK-APPLE-SYSROOT: "-arch" "i386"{{.*}} "-syslibroot" "{{[^"]*}}/BAR"
19+
// CHECK-APPLE-SYSROOT: "-arch" "i386"{{.*}} "-syslibroot" "{{[^"]*}}/FOO"

0 commit comments

Comments
 (0)