[clang] Use the Windows SDK arguments over the environment (PR #144805)
Fabrice de Gans via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 18 15:11:09 PDT 2025
https://github.com/Steelskin created https://github.com/llvm/llvm-project/pull/144805
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.
>From 472cb7e13aa720a3fd53078d45e9c30503029003 Mon Sep 17 00:00:00 2001
From: Fabrice de Gans <steelskin at gmail.com>
Date: Wed, 18 Jun 2025 15:04:35 -0700
Subject: [PATCH] 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.
---
clang/lib/Driver/ToolChains/MSVC.cpp | 39 +++++++++++++++++-----------
1 file changed, 24 insertions(+), 15 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 1e244865b2117..632cc3a0a8b9b 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -94,10 +94,14 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// If the VC environment hasn't been configured (perhaps because the user
// did not run vcvarsall), try to build a consistent link environment. If
// the environment variable is set however, assume the user knows what
- // they're doing. If the user passes /vctoolsdir or /winsdkdir, trust that
- // over env vars.
- if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diasdkdir,
- options::OPT__SLASH_winsysroot)) {
+ // they're doing. If the user passes /vctoolsdir or /winsdkdir or any of the
+ // other Windows SDK options, trust that over env vars.
+ const Arg *A = Args.getLastArg(options::OPT__SLASH_vctoolsdir,
+ options::OPT__SLASH_vctoolsversion,
+ options::OPT__SLASH_winsysroot,
+ options::OPT__SLASH_winsdkdir,
+ options::OPT__SLASH_winsdkversion);
+ if (A) {
// cl.exe doesn't find the DIA SDK automatically, so this too requires
// explicit flags and doesn't automatically look in "DIA SDK" relative
// to the path we found for VCToolChainPath.
@@ -110,9 +114,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
llvm::archToLegacyVCArch(TC.getArch()));
CmdArgs.push_back(Args.MakeArgString(Twine("-libpath:") + DIAPath));
}
- if (!llvm::sys::Process::GetEnv("LIB") ||
- Args.getLastArg(options::OPT__SLASH_vctoolsdir,
- options::OPT__SLASH_winsysroot)) {
+ if (!llvm::sys::Process::GetEnv("LIB") || A) {
CmdArgs.push_back(Args.MakeArgString(
Twine("-libpath:") +
TC.getSubDirectoryPath(llvm::SubDirectoryType::Lib)));
@@ -120,9 +122,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Twine("-libpath:") +
TC.getSubDirectoryPath(llvm::SubDirectoryType::Lib, "atlmfc")));
}
- if (!llvm::sys::Process::GetEnv("LIB") ||
- Args.getLastArg(options::OPT__SLASH_winsdkdir,
- options::OPT__SLASH_winsysroot)) {
+ if (!llvm::sys::Process::GetEnv("LIB") || A) {
if (TC.useUniversalCRT()) {
std::string UniversalCRTLibPath;
if (TC.getUniversalCRTLibraryPath(Args, UniversalCRTLibPath))
@@ -677,9 +677,18 @@ void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
AddSystemIncludesFromEnv(Var);
}
+ // Check if the user has explicitly set a vctoolsdir or any of the
+ // Windows SDK options. If so, we assume the user knows what they're doing
+ // and don't try to find the include directories automatically.
+ // If not, we try to find the include directories automatically.
+ const Arg *A = DriverArgs.getLastArg(options::OPT__SLASH_vctoolsdir,
+ options::OPT__SLASH_vctoolsversion,
+ options::OPT__SLASH_winsysroot,
+ options::OPT__SLASH_winsdkdir,
+ options::OPT__SLASH_winsdkversion);
+
// Add DIA SDK include if requested.
- if (const Arg *A = DriverArgs.getLastArg(options::OPT__SLASH_diasdkdir,
- options::OPT__SLASH_winsysroot)) {
+ if (A) {
// cl.exe doesn't find the DIA SDK automatically, so this too requires
// explicit flags and doesn't automatically look in "DIA SDK" relative
// to the path we found for VCToolChainPath.
@@ -694,9 +703,9 @@ void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
return;
// Honor %INCLUDE% and %EXTERNAL_INCLUDE%. It should have essential search
- // paths set by vcvarsall.bat. Skip if the user expressly set a vctoolsdir.
- if (!DriverArgs.getLastArg(options::OPT__SLASH_vctoolsdir,
- options::OPT__SLASH_winsysroot)) {
+ // paths set by vcvarsall.bat. Skip if the user expressly set any of the
+ // Windows SDK options.
+ if (!A) {
bool Found = AddSystemIncludesFromEnv("INCLUDE");
Found |= AddSystemIncludesFromEnv("EXTERNAL_INCLUDE");
if (Found)
More information about the cfe-commits
mailing list