Skip to content

Commit

Permalink
[Darwin][Driver][clang] Prioritise command line args over `DEFAULT_SY…
Browse files Browse the repository at this point in the history
…SROOT` (#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 <[email protected]>

Co-authored-by: Bo Anderson <[email protected]>
  • Loading branch information
carlocab and Bo98 authored Dec 12, 2024
1 parent cfad8f1 commit 737d78a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 7 additions & 3 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,17 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,

// Give --sysroot= preference, over the Apple specific behavior to also use
// --isysroot as the syslibroot.
StringRef sysroot = C.getSysRoot();
if (sysroot != "") {
// We check `OPT__sysroot_EQ` directly instead of `getSysRoot` to make sure we
// prioritise command line arguments over configuration of `DEFAULT_SYSROOT`.
if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
CmdArgs.push_back("-syslibroot");
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
CmdArgs.push_back(A->getValue());
} else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
CmdArgs.push_back("-syslibroot");
CmdArgs.push_back(A->getValue());
} else if (StringRef sysroot = C.getSysRoot(); sysroot != "") {
CmdArgs.push_back("-syslibroot");
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
}

Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
Expand Down
3 changes: 1 addition & 2 deletions clang/test/Driver/sysroot.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// CHECK-SYSROOTEQ: "-cc1"{{.*}} "-isysroot" "{{[^"]*}}/FOO"

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

Expand Down

0 comments on commit 737d78a

Please sign in to comment.