Skip to content

Commit 412ac0e

Browse files
committed
clang-cl: Prefer /vctoolsdir, /winsdkdir over LIB for link invocations
/vctoolsdir and /winsdkdir take precedence over the INCLUDE env var, so they should also take precedence over LIB. It's not quite as neat since LIB is still read by the linker and the linker just prefers the -libpath: paths the driver now passes, but as long as all libraries are present at /vctoolsdir and /winsdkdir, there's no harm in the linker also looking at LIB later. This fixes cl-options.c after a5d85cb on Windows when LIB is set. Another way to fix the test would be to prefix the clang-cl line with `env --unset=LIB`, but I think it's better to fix the flag to work as expected instead of making the test work around the surprising behavior that LIB being set causes clang-cl to not pass -libpath: flags to the linker when /vctoolsdir and /winsdkdir are used.
1 parent d0e8a9e commit 412ac0e

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,28 +339,30 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
339339
CmdArgs.push_back("-defaultlib:oldnames");
340340
}
341341

342-
if (!llvm::sys::Process::GetEnv("LIB")) {
343-
// If the VC environment hasn't been configured (perhaps because the user
344-
// did not run vcvarsall), try to build a consistent link environment. If
345-
// the environment variable is set however, assume the user knows what
346-
// they're doing.
342+
// If the VC environment hasn't been configured (perhaps because the user
343+
// did not run vcvarsall), try to build a consistent link environment. If
344+
// the environment variable is set however, assume the user knows what
345+
// they're doing. If the user passes /vctoolsdir or /winsdkdir, trust that
346+
// over env vars.
347+
if (!llvm::sys::Process::GetEnv("LIB") ||
348+
Args.getLastArg(options::OPT__SLASH_vctoolsdir)) {
347349
CmdArgs.push_back(Args.MakeArgString(
348350
Twine("-libpath:") +
349351
TC.getSubDirectoryPath(
350352
toolchains::MSVCToolChain::SubDirectoryType::Lib)));
351-
352353
CmdArgs.push_back(Args.MakeArgString(
353354
Twine("-libpath:") +
354355
TC.getSubDirectoryPath(toolchains::MSVCToolChain::SubDirectoryType::Lib,
355356
"atlmfc")));
356-
357+
}
358+
if (!llvm::sys::Process::GetEnv("LIB") ||
359+
Args.getLastArg(options::OPT__SLASH_winsdkdir)) {
357360
if (TC.useUniversalCRT()) {
358361
std::string UniversalCRTLibPath;
359362
if (TC.getUniversalCRTLibraryPath(Args, UniversalCRTLibPath))
360363
CmdArgs.push_back(
361364
Args.MakeArgString(Twine("-libpath:") + UniversalCRTLibPath));
362365
}
363-
364366
std::string WindowsSdkLibPath;
365367
if (TC.getWindowsSDKLibraryPath(Args, WindowsSdkLibPath))
366368
CmdArgs.push_back(
@@ -1115,7 +1117,7 @@ static bool getWindowsSDKDirViaCommandLine(const ArgList &Args,
11151117
static bool getWindowsSDKDir(const ArgList &Args, std::string &Path, int &Major,
11161118
std::string &WindowsSDKIncludeVersion,
11171119
std::string &WindowsSDKLibVersion) {
1118-
// Trust /winsdkdir: and /winsdkversion: if present.
1120+
// Trust /winsdkdir and /winsdkversion if present.
11191121
if (getWindowsSDKDirViaCommandLine(
11201122
Args, Path, Major, WindowsSDKIncludeVersion)) {
11211123
WindowsSDKLibVersion = WindowsSDKIncludeVersion;
@@ -1213,8 +1215,8 @@ bool MSVCToolChain::useUniversalCRT() const {
12131215

12141216
static bool getUniversalCRTSdkDir(const ArgList &Args, std::string &Path,
12151217
std::string &UCRTVersion) {
1216-
// If /winsdkdir: is passed, use it as location for the UCRT too.
1217-
// FIXME: Should there be a dedicated /ucrtdir: to override /winsdkdir:?
1218+
// If /winsdkdir is passed, use it as location for the UCRT too.
1219+
// FIXME: Should there be a dedicated /ucrtdir to override /winsdkdir?
12181220
int Major;
12191221
if (getWindowsSDKDirViaCommandLine(Args, Path, Major, UCRTVersion))
12201222
return true;

0 commit comments

Comments
 (0)