Skip to content

Commit 737d78a

Browse files
carlocabBo98
andauthored
[Darwin][Driver][clang] Prioritise command line args over DEFAULT_SYSROOT (#115993)
If a toolchain is configured with `DEFAULT_SYSROOT`, then this could result in an unintended value for `-syslibroot` being passed to the linker if the user manually sets `-isysroot` or `SDKROOT`. Let's fix this by prioritising command line flags when determining `-syslibroot` before checking `getSysRoot`. Downstream bug report: Homebrew/homebrew-core#197277 Co-authored-by: Bo Anderson <mail@boanderson.me> Co-authored-by: Bo Anderson <mail@boanderson.me>
1 parent cfad8f1 commit 737d78a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,17 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
430430

431431
// Give --sysroot= preference, over the Apple specific behavior to also use
432432
// --isysroot as the syslibroot.
433-
StringRef sysroot = C.getSysRoot();
434-
if (sysroot != "") {
433+
// We check `OPT__sysroot_EQ` directly instead of `getSysRoot` to make sure we
434+
// prioritise command line arguments over configuration of `DEFAULT_SYSROOT`.
435+
if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
435436
CmdArgs.push_back("-syslibroot");
436-
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
437+
CmdArgs.push_back(A->getValue());
437438
} else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
438439
CmdArgs.push_back("-syslibroot");
439440
CmdArgs.push_back(A->getValue());
441+
} else if (StringRef sysroot = C.getSysRoot(); sysroot != "") {
442+
CmdArgs.push_back("-syslibroot");
443+
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
440444
}
441445

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

clang/test/Driver/sysroot.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
// CHECK-SYSROOTEQ: "-cc1"{{.*}} "-isysroot" "{{[^"]*}}/FOO"
55

66
// Apple Darwin uses -isysroot as the syslib root, too.
7-
// We pass --sysroot="" to defeat any -DDEFAULT_SYSROOT parameter.
87
// RUN: touch %t2.o
98
// RUN: %clang -target i386-apple-darwin10 \
10-
// RUN: -isysroot /FOO --sysroot="" -### %t2.o 2> %t2
9+
// RUN: -isysroot /FOO -### %t2.o 2> %t2
1110
// RUN: FileCheck --check-prefix=CHECK-APPLE-ISYSROOT < %t2 %s
1211
// CHECK-APPLE-ISYSROOT: "-arch" "i386"{{.*}} "-syslibroot" "{{[^"]*}}/FOO"
1312

0 commit comments

Comments
 (0)