Skip to content

Commit 951f362

Browse files
committed
[clang-cl] Add a /diasdkdir flag and make /winsysroot imply it
D109708 added "DIA SDK" to our win sysroot for hermetic builds that use LLVM_ENABLE_DIA_SDK. But the build system still has to manually pass flags pointing to it. Since we have a /winsysroot flag, make it look at DIA SDK in the sysroot. With this, the following is enough to compile the DIA2Dump example: out\gn\bin\clang-cl ^ "sysroot\DIA SDK\Samples\DIA2Dump\DIA2Dump.cpp" ^ "sysroot\DIA SDK\Samples\DIA2Dump\PrintSymbol.cpp" ^ "sysroot\DIA SDK\Samples\DIA2Dump\regs.cpp" ^ /diasdkdir "sysroot\DIA SDK" ^ ole32.lib oleaut32.lib diaguids.lib Differential Revision: https://reviews.llvm.org/D109828
1 parent 99ece01 commit 951f362

File tree

3 files changed

+102
-61
lines changed

3 files changed

+102
-61
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6228,6 +6228,8 @@ def _SLASH_TC : CLCompileFlag<"TC">, HelpText<"Treat all source files as C">;
62286228
def _SLASH_Tp : CLCompileJoinedOrSeparate<"Tp">,
62296229
HelpText<"Treat <file> as C++ source file">, MetaVarName<"<file>">;
62306230
def _SLASH_TP : CLCompileFlag<"TP">, HelpText<"Treat all source files as C++">;
6231+
def _SLASH_diasdkdir : CLJoinedOrSeparate<"diasdkdir">,
6232+
HelpText<"Path to the DIA SDK">, MetaVarName<"<dir>">;
62316233
def _SLASH_vctoolsdir : CLJoinedOrSeparate<"vctoolsdir">,
62326234
HelpText<"Path to the VCToolChain">, MetaVarName<"<dir>">;
62336235
def _SLASH_vctoolsversion : CLJoinedOrSeparate<"vctoolsversion">,
@@ -6237,7 +6239,7 @@ def _SLASH_winsdkdir : CLJoinedOrSeparate<"winsdkdir">,
62376239
def _SLASH_winsdkversion : CLJoinedOrSeparate<"winsdkversion">,
62386240
HelpText<"Full version of the Windows SDK, defaults to newest found">;
62396241
def _SLASH_winsysroot : CLJoinedOrSeparate<"winsysroot">,
6240-
HelpText<"Same as /vctoolsdir <dir>/VC/Tools/MSVC/<vctoolsversion> /winsdkdir <dir>/Windows Kits/10">,
6242+
HelpText<"Same as \"/diasdkdir <dir>/DIA SDK\" /vctoolsdir <dir>/VC/Tools/MSVC/<vctoolsversion> \"/winsdkdir <dir>/Windows Kits/10\"">,
62416243
MetaVarName<"<dir>">;
62426244
def _SLASH_volatile_iso : Option<["/", "-"], "volatile:iso", KIND_FLAG>,
62436245
Group<_SLASH_volatile_Group>, Flags<[CLOption, NoXarchOption]>,

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,61 @@ using namespace clang::driver::tools;
6363
using namespace clang;
6464
using namespace llvm::opt;
6565

66+
// Windows SDKs and VC Toolchains group their contents into subdirectories based
67+
// on the target architecture. This function converts an llvm::Triple::ArchType
68+
// to the corresponding subdirectory name.
69+
static const char *llvmArchToWindowsSDKArch(llvm::Triple::ArchType Arch) {
70+
using ArchType = llvm::Triple::ArchType;
71+
switch (Arch) {
72+
case ArchType::x86:
73+
return "x86";
74+
case ArchType::x86_64:
75+
return "x64";
76+
case ArchType::arm:
77+
return "arm";
78+
case ArchType::aarch64:
79+
return "arm64";
80+
default:
81+
return "";
82+
}
83+
}
84+
85+
// Similar to the above function, but for Visual Studios before VS2017.
86+
static const char *llvmArchToLegacyVCArch(llvm::Triple::ArchType Arch) {
87+
using ArchType = llvm::Triple::ArchType;
88+
switch (Arch) {
89+
case ArchType::x86:
90+
// x86 is default in legacy VC toolchains.
91+
// e.g. x86 libs are directly in /lib as opposed to /lib/x86.
92+
return "";
93+
case ArchType::x86_64:
94+
return "amd64";
95+
case ArchType::arm:
96+
return "arm";
97+
case ArchType::aarch64:
98+
return "arm64";
99+
default:
100+
return "";
101+
}
102+
}
103+
104+
// Similar to the above function, but for DevDiv internal builds.
105+
static const char *llvmArchToDevDivInternalArch(llvm::Triple::ArchType Arch) {
106+
using ArchType = llvm::Triple::ArchType;
107+
switch (Arch) {
108+
case ArchType::x86:
109+
return "i386";
110+
case ArchType::x86_64:
111+
return "amd64";
112+
case ArchType::arm:
113+
return "arm";
114+
case ArchType::aarch64:
115+
return "arm64";
116+
default:
117+
return "";
118+
}
119+
}
120+
66121
static bool canExecute(llvm::vfs::FileSystem &VFS, StringRef Path) {
67122
auto Status = VFS.status(Path);
68123
if (!Status)
@@ -396,6 +451,20 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
396451
// the environment variable is set however, assume the user knows what
397452
// they're doing. If the user passes /vctoolsdir or /winsdkdir, trust that
398453
// over env vars.
454+
if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diasdkdir,
455+
options::OPT__SLASH_winsysroot)) {
456+
// cl.exe doesn't find the DIA SDK automatically, so this too requires
457+
// explicit flags and doesn't automatically look in "DIA SDK" relative
458+
// to the path we found for VCToolChainPath.
459+
llvm::SmallString<128> DIAPath(A->getValue());
460+
if (A->getOption().getID() == options::OPT__SLASH_winsysroot)
461+
llvm::sys::path::append(DIAPath, "DIA SDK");
462+
463+
// The DIA SDK always uses the legacy vc arch, even in new MSVC versions.
464+
llvm::sys::path::append(DIAPath, "lib",
465+
llvmArchToLegacyVCArch(TC.getArch()));
466+
CmdArgs.push_back(Args.MakeArgString(Twine("-libpath:") + DIAPath));
467+
}
399468
if (!llvm::sys::Process::GetEnv("LIB") ||
400469
Args.getLastArg(options::OPT__SLASH_vctoolsdir,
401470
options::OPT__SLASH_winsysroot)) {
@@ -752,61 +821,6 @@ void MSVCToolChain::printVerboseInfo(raw_ostream &OS) const {
752821
RocmInstallation.print(OS);
753822
}
754823

755-
// Windows SDKs and VC Toolchains group their contents into subdirectories based
756-
// on the target architecture. This function converts an llvm::Triple::ArchType
757-
// to the corresponding subdirectory name.
758-
static const char *llvmArchToWindowsSDKArch(llvm::Triple::ArchType Arch) {
759-
using ArchType = llvm::Triple::ArchType;
760-
switch (Arch) {
761-
case ArchType::x86:
762-
return "x86";
763-
case ArchType::x86_64:
764-
return "x64";
765-
case ArchType::arm:
766-
return "arm";
767-
case ArchType::aarch64:
768-
return "arm64";
769-
default:
770-
return "";
771-
}
772-
}
773-
774-
// Similar to the above function, but for Visual Studios before VS2017.
775-
static const char *llvmArchToLegacyVCArch(llvm::Triple::ArchType Arch) {
776-
using ArchType = llvm::Triple::ArchType;
777-
switch (Arch) {
778-
case ArchType::x86:
779-
// x86 is default in legacy VC toolchains.
780-
// e.g. x86 libs are directly in /lib as opposed to /lib/x86.
781-
return "";
782-
case ArchType::x86_64:
783-
return "amd64";
784-
case ArchType::arm:
785-
return "arm";
786-
case ArchType::aarch64:
787-
return "arm64";
788-
default:
789-
return "";
790-
}
791-
}
792-
793-
// Similar to the above function, but for DevDiv internal builds.
794-
static const char *llvmArchToDevDivInternalArch(llvm::Triple::ArchType Arch) {
795-
using ArchType = llvm::Triple::ArchType;
796-
switch (Arch) {
797-
case ArchType::x86:
798-
return "i386";
799-
case ArchType::x86_64:
800-
return "amd64";
801-
case ArchType::arm:
802-
return "arm";
803-
case ArchType::aarch64:
804-
return "arm64";
805-
default:
806-
return "";
807-
}
808-
}
809-
810824
// Get the path to a specific subdirectory in the current toolchain for
811825
// a given target architecture.
812826
// VS2017 changed the VC toolchain layout, so this should be used instead
@@ -1263,6 +1277,19 @@ void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
12631277
AddSystemIncludesFromEnv(Var);
12641278
}
12651279

1280+
// Add DIA SDK include if requested.
1281+
if (const Arg *A = DriverArgs.getLastArg(options::OPT__SLASH_diasdkdir,
1282+
options::OPT__SLASH_winsysroot)) {
1283+
// cl.exe doesn't find the DIA SDK automatically, so this too requires
1284+
// explicit flags and doesn't automatically look in "DIA SDK" relative
1285+
// to the path we found for VCToolChainPath.
1286+
llvm::SmallString<128> DIASDKPath(A->getValue());
1287+
if (A->getOption().getID() == options::OPT__SLASH_winsysroot)
1288+
llvm::sys::path::append(DIASDKPath, "DIA SDK");
1289+
AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, std::string(DIASDKPath),
1290+
"include");
1291+
}
1292+
12661293
if (DriverArgs.hasArg(options::OPT_nostdlibinc))
12671294
return;
12681295

clang/test/Driver/cl-sysroot.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
// RUN: rm -rf %t
22
// RUN: split-file %s %t
33

4-
// RUN: %clang_cl /winsysroot %t -### -- %t/foo.cpp 2>&1 | FileCheck %s
5-
// RUN: %clang_cl /vctoolsdir %t/VC/Tools/MSVC/27.1828.18284 \
6-
// RUN: /winsdkdir "%t/Windows Kits/10" \
7-
// RUN: -### -- %t/foo.cpp 2>&1 | FileCheck %s
4+
// RUN: %clang_cl -m64 /winsysroot %t -### -- %t/foo.cpp 2>&1 | FileCheck %s
5+
// RUN: %clang_cl -m64 \
6+
// RUN: /diasdkdir "%t/DIA SDK" \
7+
// RUN: /vctoolsdir %t/VC/Tools/MSVC/27.1828.18284 \
8+
// RUN: /winsdkdir "%t/Windows Kits/10" \
9+
// RUN: -### -- %t/foo.cpp 2>&1 | FileCheck %s
810

9-
// CHECK: "-internal-isystem" "[[ROOT:[^"]*]]{{/|\\\\}}VC{{/|\\\\}}Tools{{/|\\\\}}MSVC{{/|\\\\}}27.1828.18284{{/|\\\\}}include"
11+
// CHECK: "-internal-isystem" "[[ROOT:[^"]*]]{{/|\\\\}}DIA SDK{{/|\\\\}}include"
12+
// CHECK: "-internal-isystem" "[[ROOT]]{{/|\\\\}}VC{{/|\\\\}}Tools{{/|\\\\}}MSVC{{/|\\\\}}27.1828.18284{{/|\\\\}}include"
1013
// CHECK: "-internal-isystem" "[[ROOT]]{{/|\\\\}}VC{{/|\\\\}}Tools{{/|\\\\}}MSVC{{/|\\\\}}27.1828.18284{{/|\\\\}}atlmfc{{/|\\\\}}include"
1114
// CHECK: "-internal-isystem" "[[ROOT]]{{/|\\\\}}Windows Kits{{/|\\\\}}10{{/|\\\\}}Include{{/|\\\\}}10.0.19041.0{{/|\\\\}}ucrt"
1215
// CHECK: "-internal-isystem" "[[ROOT]]{{/|\\\\}}Windows Kits{{/|\\\\}}10{{/|\\\\}}Include{{/|\\\\}}10.0.19041.0{{/|\\\\}}shared"
1316
// CHECK: "-internal-isystem" "[[ROOT]]{{/|\\\\}}Windows Kits{{/|\\\\}}10{{/|\\\\}}Include{{/|\\\\}}10.0.19041.0{{/|\\\\}}um"
1417
// CHECK: "-internal-isystem" "[[ROOT]]{{/|\\\\}}Windows Kits{{/|\\\\}}10{{/|\\\\}}Include{{/|\\\\}}10.0.19041.0{{/|\\\\}}winrt"
1518

19+
// CHECK: "-libpath:[[ROOT]]{{/|\\\\}}DIA SDK{{/|\\\\}}lib{{/|\\\\}}amd64"
20+
// CHECK: "-libpath:[[ROOT]]{{/|\\\\}}VC{{/|\\\\}}Tools{{/|\\\\}}MSVC{{/|\\\\}}27.1828.18284{{/|\\\\}}lib{{/|\\\\}}x64"
21+
// CHECK: "-libpath:[[ROOT]]{{/|\\\\}}VC{{/|\\\\}}Tools{{/|\\\\}}MSVC{{/|\\\\}}27.1828.18284{{/|\\\\}}atlmfc{{/|\\\\}}lib{{/|\\\\}}x64"
22+
// CHECK: "-libpath:[[ROOT]]{{/|\\\\}}Windows Kits{{/|\\\\}}10{{/|\\\\}}Lib{{/|\\\\}}10.0.19041.0{{/|\\\\}}ucrt{{/|\\\\}}x64"
23+
// CHECK: "-libpath:[[ROOT]]{{/|\\\\}}Windows Kits{{/|\\\\}}10{{/|\\\\}}Lib{{/|\\\\}}10.0.19041.0{{/|\\\\}}um{{/|\\\\}}x64"
24+
1625
#--- VC/Tools/MSVC/27.1828.18284/include/string
1726
namespace std {
1827
class mystring {
@@ -24,6 +33,9 @@ class mystring {
2433
#--- Windows Kits/10/Include/10.0.19041.0/ucrt/assert.h
2534
#define myassert(X)
2635

36+
#--- DIA SDK/include/cvconst.h
37+
#define myotherassert(X)
38+
2739
#--- foo.cpp
2840
#include <assert.h>
2941
#include <string>

0 commit comments

Comments
 (0)