Skip to content

Commit 472cb7e

Browse files
committed
Use the Windows SDK arguments over the environment
If any of the Windows SDK (and MSVC)-related argument is passed in the command line, they should take priority over the environment variables like `INCLUDE` or `LIB` set by vcvarsall from the Visual Studio Developer Environment on Windows. These changes ensure that all of the arguments related to VC Tools and the Windows SDK cause the driver to ignore the environment.
1 parent 8c3fbaf commit 472cb7e

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,14 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
9494
// If the VC environment hasn't been configured (perhaps because the user
9595
// did not run vcvarsall), try to build a consistent link environment. If
9696
// the environment variable is set however, assume the user knows what
97-
// they're doing. If the user passes /vctoolsdir or /winsdkdir, trust that
98-
// over env vars.
99-
if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diasdkdir,
100-
options::OPT__SLASH_winsysroot)) {
97+
// they're doing. If the user passes /vctoolsdir or /winsdkdir or any of the
98+
// other Windows SDK options, trust that over env vars.
99+
const Arg *A = Args.getLastArg(options::OPT__SLASH_vctoolsdir,
100+
options::OPT__SLASH_vctoolsversion,
101+
options::OPT__SLASH_winsysroot,
102+
options::OPT__SLASH_winsdkdir,
103+
options::OPT__SLASH_winsdkversion);
104+
if (A) {
101105
// cl.exe doesn't find the DIA SDK automatically, so this too requires
102106
// explicit flags and doesn't automatically look in "DIA SDK" relative
103107
// to the path we found for VCToolChainPath.
@@ -110,19 +114,15 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
110114
llvm::archToLegacyVCArch(TC.getArch()));
111115
CmdArgs.push_back(Args.MakeArgString(Twine("-libpath:") + DIAPath));
112116
}
113-
if (!llvm::sys::Process::GetEnv("LIB") ||
114-
Args.getLastArg(options::OPT__SLASH_vctoolsdir,
115-
options::OPT__SLASH_winsysroot)) {
117+
if (!llvm::sys::Process::GetEnv("LIB") || A) {
116118
CmdArgs.push_back(Args.MakeArgString(
117119
Twine("-libpath:") +
118120
TC.getSubDirectoryPath(llvm::SubDirectoryType::Lib)));
119121
CmdArgs.push_back(Args.MakeArgString(
120122
Twine("-libpath:") +
121123
TC.getSubDirectoryPath(llvm::SubDirectoryType::Lib, "atlmfc")));
122124
}
123-
if (!llvm::sys::Process::GetEnv("LIB") ||
124-
Args.getLastArg(options::OPT__SLASH_winsdkdir,
125-
options::OPT__SLASH_winsysroot)) {
125+
if (!llvm::sys::Process::GetEnv("LIB") || A) {
126126
if (TC.useUniversalCRT()) {
127127
std::string UniversalCRTLibPath;
128128
if (TC.getUniversalCRTLibraryPath(Args, UniversalCRTLibPath))
@@ -677,9 +677,18 @@ void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
677677
AddSystemIncludesFromEnv(Var);
678678
}
679679

680+
// Check if the user has explicitly set a vctoolsdir or any of the
681+
// Windows SDK options. If so, we assume the user knows what they're doing
682+
// and don't try to find the include directories automatically.
683+
// If not, we try to find the include directories automatically.
684+
const Arg *A = DriverArgs.getLastArg(options::OPT__SLASH_vctoolsdir,
685+
options::OPT__SLASH_vctoolsversion,
686+
options::OPT__SLASH_winsysroot,
687+
options::OPT__SLASH_winsdkdir,
688+
options::OPT__SLASH_winsdkversion);
689+
680690
// Add DIA SDK include if requested.
681-
if (const Arg *A = DriverArgs.getLastArg(options::OPT__SLASH_diasdkdir,
682-
options::OPT__SLASH_winsysroot)) {
691+
if (A) {
683692
// cl.exe doesn't find the DIA SDK automatically, so this too requires
684693
// explicit flags and doesn't automatically look in "DIA SDK" relative
685694
// to the path we found for VCToolChainPath.
@@ -694,9 +703,9 @@ void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
694703
return;
695704

696705
// Honor %INCLUDE% and %EXTERNAL_INCLUDE%. It should have essential search
697-
// paths set by vcvarsall.bat. Skip if the user expressly set a vctoolsdir.
698-
if (!DriverArgs.getLastArg(options::OPT__SLASH_vctoolsdir,
699-
options::OPT__SLASH_winsysroot)) {
706+
// paths set by vcvarsall.bat. Skip if the user expressly set any of the
707+
// Windows SDK options.
708+
if (!A) {
700709
bool Found = AddSystemIncludesFromEnv("INCLUDE");
701710
Found |= AddSystemIncludesFromEnv("EXTERNAL_INCLUDE");
702711
if (Found)

0 commit comments

Comments
 (0)