Skip to content

Commit

Permalink
[Darwin][Driver][clang] Prioritise command line flags over `DEFAULT_S…
Browse files Browse the repository at this point in the history
…YSROOT`

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]>
  • Loading branch information
carlocab and Bo98 committed Dec 5, 2024
1 parent de0fd64 commit e257a79
Showing 1 changed file with 7 additions and 3 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

0 comments on commit e257a79

Please sign in to comment.