Skip to content

Commit e257a79

Browse files
carlocabBo98
andcommitted
[Darwin][Driver][clang] Prioritise command line flags over DEFAULT_SYSROOT
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>
1 parent de0fd64 commit e257a79

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
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);

0 commit comments

Comments
 (0)