1 //===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MSVC.h"
11 #include "CommonArgs.h"
12 #include "Darwin.h"
13 #include "clang/Basic/CharInfo.h"
14 #include "clang/Basic/Version.h"
15 #include "clang/Driver/Compilation.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/DriverDiagnostic.h"
18 #include "clang/Driver/Options.h"
19 #include "clang/Driver/SanitizerArgs.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/Option/Arg.h"
24 #include "llvm/Option/ArgList.h"
25 #include "llvm/Support/ConvertUTF.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/Process.h"
32 #include <cstdio>
33 
34 // Include the necessary headers to interface with the Windows registry and
35 // environment.
36 #if defined(LLVM_ON_WIN32)
37 #define USE_WIN32
38 #endif
39 
40 #ifdef USE_WIN32
41   #define WIN32_LEAN_AND_MEAN
42   #define NOGDI
43   #ifndef NOMINMAX
44     #define NOMINMAX
45   #endif
46   #include <windows.h>
47 #endif
48 
49 #ifdef _MSC_VER
50 // Don't support SetupApi on MinGW.
51 #define USE_MSVC_SETUP_API
52 
53 // Make sure this comes before MSVCSetupApi.h
54 #include <comdef.h>
55 
56 #include "MSVCSetupApi.h"
57 #include "llvm/Support/COM.h"
58 _COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
59 _COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
60 _COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
61 _COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
62 _COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
63 _COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
64 #endif
65 
66 using namespace clang::driver;
67 using namespace clang::driver::toolchains;
68 using namespace clang::driver::tools;
69 using namespace clang;
70 using namespace llvm::opt;
71 
72 // Defined below.
73 // Forward declare this so there aren't too many things above the constructor.
74 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
75                                     std::string &value, std::string *phValue);
76 
77 // Check various environment variables to try and find a toolchain.
78 static bool findVCToolChainViaEnvironment(std::string &Path,
79                                           MSVCToolChain::ToolsetLayout &VSLayout) {
80   // These variables are typically set by vcvarsall.bat
81   // when launching a developer command prompt.
82   if (llvm::Optional<std::string> VCToolsInstallDir =
83           llvm::sys::Process::GetEnv("VCToolsInstallDir")) {
84     // This is only set by newer Visual Studios, and it leads straight to
85     // the toolchain directory.
86     Path = std::move(*VCToolsInstallDir);
87     VSLayout = MSVCToolChain::ToolsetLayout::VS2017OrNewer;
88     return true;
89   }
90   if (llvm::Optional<std::string> VCInstallDir =
91           llvm::sys::Process::GetEnv("VCINSTALLDIR")) {
92     // If the previous variable isn't set but this one is, then we've found
93     // an older Visual Studio. This variable is set by newer Visual Studios too,
94     // so this check has to appear second.
95     // In older Visual Studios, the VC directory is the toolchain.
96     Path = std::move(*VCInstallDir);
97     VSLayout = MSVCToolChain::ToolsetLayout::OlderVS;
98     return true;
99   }
100 
101   // We couldn't find any VC environment variables. Let's walk through PATH and
102   // see if it leads us to a VC toolchain bin directory. If it does, pick the
103   // first one that we find.
104   if (llvm::Optional<std::string> PathEnv =
105           llvm::sys::Process::GetEnv("PATH")) {
106     llvm::SmallVector<llvm::StringRef, 8> PathEntries;
107     llvm::StringRef(*PathEnv).split(PathEntries, llvm::sys::EnvPathSeparator);
108     for (llvm::StringRef PathEntry : PathEntries) {
109       if (PathEntry.empty())
110         continue;
111 
112       llvm::SmallString<256> ExeTestPath;
113 
114       // If cl.exe doesn't exist, then this definitely isn't a VC toolchain.
115       ExeTestPath = PathEntry;
116       llvm::sys::path::append(ExeTestPath, "cl.exe");
117       if (!llvm::sys::fs::exists(ExeTestPath))
118         continue;
119 
120       // cl.exe existing isn't a conclusive test for a VC toolchain; clang also
121       // has a cl.exe. So let's check for link.exe too.
122       ExeTestPath = PathEntry;
123       llvm::sys::path::append(ExeTestPath, "link.exe");
124       if (!llvm::sys::fs::exists(ExeTestPath))
125         continue;
126 
127       // whatever/VC/bin --> old toolchain, VC dir is toolchain dir.
128       llvm::StringRef TestPath = PathEntry;
129       bool IsBin = llvm::sys::path::filename(TestPath).equals_lower("bin");
130       if (!IsBin) {
131         // Strip any architecture subdir like "amd64".
132         TestPath = llvm::sys::path::parent_path(TestPath);
133         IsBin = llvm::sys::path::filename(TestPath).equals_lower("bin");
134       }
135       if (IsBin) {
136         llvm::StringRef ParentPath = llvm::sys::path::parent_path(TestPath);
137         llvm::StringRef ParentFilename = llvm::sys::path::filename(ParentPath);
138         if (ParentFilename == "VC") {
139           Path = ParentPath;
140           VSLayout = MSVCToolChain::ToolsetLayout::OlderVS;
141           return true;
142         }
143         if (ParentFilename == "x86ret" || ParentFilename == "x86chk"
144           || ParentFilename == "amd64ret" || ParentFilename == "amd64chk") {
145           Path = ParentPath;
146           VSLayout = MSVCToolChain::ToolsetLayout::DevDivInternal;
147           return true;
148         }
149 
150       } else {
151         // This could be a new (>=VS2017) toolchain. If it is, we should find
152         // path components with these prefixes when walking backwards through
153         // the path.
154         // Note: empty strings match anything.
155         llvm::StringRef ExpectedPrefixes[] = {"",     "Host",  "bin", "",
156                                               "MSVC", "Tools", "VC"};
157 
158         auto It = llvm::sys::path::rbegin(PathEntry);
159         auto End = llvm::sys::path::rend(PathEntry);
160         for (llvm::StringRef Prefix : ExpectedPrefixes) {
161           if (It == End)
162             goto NotAToolChain;
163           if (!It->startswith(Prefix))
164             goto NotAToolChain;
165           ++It;
166         }
167 
168         // We've found a new toolchain!
169         // Back up 3 times (/bin/Host/arch) to get the root path.
170         llvm::StringRef ToolChainPath(PathEntry);
171         for (int i = 0; i < 3; ++i)
172           ToolChainPath = llvm::sys::path::parent_path(ToolChainPath);
173 
174         Path = ToolChainPath;
175         VSLayout = MSVCToolChain::ToolsetLayout::VS2017OrNewer;
176         return true;
177       }
178 
179     NotAToolChain:
180       continue;
181     }
182   }
183   return false;
184 }
185 
186 // Query the Setup Config server for installs, then pick the newest version
187 // and find its default VC toolchain.
188 // This is the preferred way to discover new Visual Studios, as they're no
189 // longer listed in the registry.
190 static bool findVCToolChainViaSetupConfig(std::string &Path,
191                                           MSVCToolChain::ToolsetLayout &VSLayout) {
192 #if !defined(USE_MSVC_SETUP_API)
193   return false;
194 #else
195   // FIXME: This really should be done once in the top-level program's main
196   // function, as it may have already been initialized with a different
197   // threading model otherwise.
198   llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::SingleThreaded);
199   HRESULT HR;
200 
201   // _com_ptr_t will throw a _com_error if a COM calls fail.
202   // The LLVM coding standards forbid exception handling, so we'll have to
203   // stop them from being thrown in the first place.
204   // The destructor will put the regular error handler back when we leave
205   // this scope.
206   struct SuppressCOMErrorsRAII {
207     static void __stdcall handler(HRESULT hr, IErrorInfo *perrinfo) {}
208 
209     SuppressCOMErrorsRAII() { _set_com_error_handler(handler); }
210 
211     ~SuppressCOMErrorsRAII() { _set_com_error_handler(_com_raise_error); }
212 
213   } COMErrorSuppressor;
214 
215   ISetupConfigurationPtr Query;
216   HR = Query.CreateInstance(__uuidof(SetupConfiguration));
217   if (FAILED(HR))
218     return false;
219 
220   IEnumSetupInstancesPtr EnumInstances;
221   HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances);
222   if (FAILED(HR))
223     return false;
224 
225   ISetupInstancePtr Instance;
226   HR = EnumInstances->Next(1, &Instance, nullptr);
227   if (HR != S_OK)
228     return false;
229 
230   ISetupInstancePtr NewestInstance;
231   Optional<uint64_t> NewestVersionNum;
232   do {
233     bstr_t VersionString;
234     uint64_t VersionNum;
235     HR = Instance->GetInstallationVersion(VersionString.GetAddress());
236     if (FAILED(HR))
237       continue;
238     HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, &VersionNum);
239     if (FAILED(HR))
240       continue;
241     if (!NewestVersionNum || (VersionNum > NewestVersionNum)) {
242       NewestInstance = Instance;
243       NewestVersionNum = VersionNum;
244     }
245   } while ((HR = EnumInstances->Next(1, &Instance, nullptr)) == S_OK);
246 
247   if (!NewestInstance)
248     return false;
249 
250   bstr_t VCPathWide;
251   HR = NewestInstance->ResolvePath(L"VC", VCPathWide.GetAddress());
252   if (FAILED(HR))
253     return false;
254 
255   std::string VCRootPath;
256   llvm::convertWideToUTF8(std::wstring(VCPathWide), VCRootPath);
257 
258   llvm::SmallString<256> ToolsVersionFilePath(VCRootPath);
259   llvm::sys::path::append(ToolsVersionFilePath, "Auxiliary", "Build",
260                           "Microsoft.VCToolsVersion.default.txt");
261 
262   auto ToolsVersionFile = llvm::MemoryBuffer::getFile(ToolsVersionFilePath);
263   if (!ToolsVersionFile)
264     return false;
265 
266   llvm::SmallString<256> ToolchainPath(VCRootPath);
267   llvm::sys::path::append(ToolchainPath, "Tools", "MSVC",
268                           ToolsVersionFile->get()->getBuffer().rtrim());
269   if (!llvm::sys::fs::is_directory(ToolchainPath))
270     return false;
271 
272   Path = ToolchainPath.str();
273   VSLayout = MSVCToolChain::ToolsetLayout::VS2017OrNewer;
274   return true;
275 #endif
276 }
277 
278 // Look in the registry for Visual Studio installs, and use that to get
279 // a toolchain path. VS2017 and newer don't get added to the registry.
280 // So if we find something here, we know that it's an older version.
281 static bool findVCToolChainViaRegistry(std::string &Path,
282                                        MSVCToolChain::ToolsetLayout &VSLayout) {
283   std::string VSInstallPath;
284   if (getSystemRegistryString(R"(SOFTWARE\Microsoft\VisualStudio\$VERSION)",
285                               "InstallDir", VSInstallPath, nullptr) ||
286       getSystemRegistryString(R"(SOFTWARE\Microsoft\VCExpress\$VERSION)",
287                               "InstallDir", VSInstallPath, nullptr)) {
288     if (!VSInstallPath.empty()) {
289       llvm::SmallString<256> VCPath(llvm::StringRef(
290           VSInstallPath.c_str(), VSInstallPath.find(R"(\Common7\IDE)")));
291       llvm::sys::path::append(VCPath, "VC");
292 
293       Path = VCPath.str();
294       VSLayout = MSVCToolChain::ToolsetLayout::OlderVS;
295       return true;
296     }
297   }
298   return false;
299 }
300 
301 // Try to find Exe from a Visual Studio distribution.  This first tries to find
302 // an installed copy of Visual Studio and, failing that, looks in the PATH,
303 // making sure that whatever executable that's found is not a same-named exe
304 // from clang itself to prevent clang from falling back to itself.
305 static std::string FindVisualStudioExecutable(const ToolChain &TC,
306                                               const char *Exe) {
307   const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(TC);
308   SmallString<128> FilePath(MSVC.getSubDirectoryPath(
309       toolchains::MSVCToolChain::SubDirectoryType::Bin));
310   llvm::sys::path::append(FilePath, Exe);
311   return llvm::sys::fs::can_execute(FilePath) ? FilePath.str() : Exe;
312 }
313 
314 void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
315                                         const InputInfo &Output,
316                                         const InputInfoList &Inputs,
317                                         const ArgList &Args,
318                                         const char *LinkingOutput) const {
319   ArgStringList CmdArgs;
320 
321   auto &TC = static_cast<const toolchains::MSVCToolChain &>(getToolChain());
322 
323   assert((Output.isFilename() || Output.isNothing()) && "invalid output");
324   if (Output.isFilename())
325     CmdArgs.push_back(
326         Args.MakeArgString(std::string("-out:") + Output.getFilename()));
327 
328   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
329       !C.getDriver().IsCLMode())
330     CmdArgs.push_back("-defaultlib:libcmt");
331 
332   if (!llvm::sys::Process::GetEnv("LIB")) {
333     // If the VC environment hasn't been configured (perhaps because the user
334     // did not run vcvarsall), try to build a consistent link environment.  If
335     // the environment variable is set however, assume the user knows what
336     // they're doing.
337     CmdArgs.push_back(Args.MakeArgString(
338         Twine("-libpath:") +
339         TC.getSubDirectoryPath(
340             toolchains::MSVCToolChain::SubDirectoryType::Lib)));
341 
342     if (TC.useUniversalCRT()) {
343       std::string UniversalCRTLibPath;
344       if (TC.getUniversalCRTLibraryPath(UniversalCRTLibPath))
345         CmdArgs.push_back(
346             Args.MakeArgString(Twine("-libpath:") + UniversalCRTLibPath));
347     }
348 
349     std::string WindowsSdkLibPath;
350     if (TC.getWindowsSDKLibraryPath(WindowsSdkLibPath))
351       CmdArgs.push_back(
352           Args.MakeArgString(std::string("-libpath:") + WindowsSdkLibPath));
353   }
354 
355   if (!C.getDriver().IsCLMode() && Args.hasArg(options::OPT_L))
356     for (const auto &LibPath : Args.getAllArgValues(options::OPT_L))
357       CmdArgs.push_back(Args.MakeArgString("-libpath:" + LibPath));
358 
359   CmdArgs.push_back("-nologo");
360 
361   if (Args.hasArg(options::OPT_g_Group, options::OPT__SLASH_Z7,
362                   options::OPT__SLASH_Zd))
363     CmdArgs.push_back("-debug");
364 
365   bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd,
366                          options::OPT_shared);
367   if (DLL) {
368     CmdArgs.push_back(Args.MakeArgString("-dll"));
369 
370     SmallString<128> ImplibName(Output.getFilename());
371     llvm::sys::path::replace_extension(ImplibName, "lib");
372     CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") + ImplibName));
373   }
374 
375   if (TC.getSanitizerArgs().needsAsanRt()) {
376     CmdArgs.push_back(Args.MakeArgString("-debug"));
377     CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
378     if (TC.getSanitizerArgs().needsSharedAsanRt() ||
379         Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
380       for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
381         CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
382       // Make sure the dynamic runtime thunk is not optimized out at link time
383       // to ensure proper SEH handling.
384       CmdArgs.push_back(Args.MakeArgString(
385           TC.getArch() == llvm::Triple::x86
386               ? "-include:___asan_seh_interceptor"
387               : "-include:__asan_seh_interceptor"));
388       // Make sure the linker consider all object files from the dynamic runtime
389       // thunk.
390       CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
391           TC.getCompilerRT(Args, "asan_dynamic_runtime_thunk")));
392     } else if (DLL) {
393       CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
394     } else {
395       for (const auto &Lib : {"asan", "asan_cxx"}) {
396         CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
397         // Make sure the linker consider all object files from the static lib.
398         // This is necessary because instrumented dlls need access to all the
399         // interface exported by the static lib in the main executable.
400         CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
401             TC.getCompilerRT(Args, Lib)));
402       }
403     }
404   }
405 
406   Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
407 
408   if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
409                    options::OPT_fno_openmp, false)) {
410     CmdArgs.push_back("-nodefaultlib:vcomp.lib");
411     CmdArgs.push_back("-nodefaultlib:vcompd.lib");
412     CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
413                                          TC.getDriver().Dir + "/../lib"));
414     switch (TC.getDriver().getOpenMPRuntime(Args)) {
415     case Driver::OMPRT_OMP:
416       CmdArgs.push_back("-defaultlib:libomp.lib");
417       break;
418     case Driver::OMPRT_IOMP5:
419       CmdArgs.push_back("-defaultlib:libiomp5md.lib");
420       break;
421     case Driver::OMPRT_GOMP:
422       break;
423     case Driver::OMPRT_Unknown:
424       // Already diagnosed.
425       break;
426     }
427   }
428 
429   // Add compiler-rt lib in case if it was explicitly
430   // specified as an argument for --rtlib option.
431   if (!Args.hasArg(options::OPT_nostdlib)) {
432     AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args);
433   }
434 
435   // Add filenames, libraries, and other linker inputs.
436   for (const auto &Input : Inputs) {
437     if (Input.isFilename()) {
438       CmdArgs.push_back(Input.getFilename());
439       continue;
440     }
441 
442     const Arg &A = Input.getInputArg();
443 
444     // Render -l options differently for the MSVC linker.
445     if (A.getOption().matches(options::OPT_l)) {
446       StringRef Lib = A.getValue();
447       const char *LinkLibArg;
448       if (Lib.endswith(".lib"))
449         LinkLibArg = Args.MakeArgString(Lib);
450       else
451         LinkLibArg = Args.MakeArgString(Lib + ".lib");
452       CmdArgs.push_back(LinkLibArg);
453       continue;
454     }
455 
456     // Otherwise, this is some other kind of linker input option like -Wl, -z,
457     // or -L. Render it, even if MSVC doesn't understand it.
458     A.renderAsInput(Args, CmdArgs);
459   }
460 
461   TC.addProfileRTLibs(Args, CmdArgs);
462 
463   std::vector<const char *> Environment;
464 
465   // We need to special case some linker paths.  In the case of lld, we need to
466   // translate 'lld' into 'lld-link', and in the case of the regular msvc
467   // linker, we need to use a special search algorithm.
468   llvm::SmallString<128> linkPath;
469   StringRef Linker = Args.getLastArgValue(options::OPT_fuse_ld_EQ, "link");
470   if (Linker.equals_lower("lld"))
471     Linker = "lld-link";
472 
473   if (Linker.equals_lower("link")) {
474     // If we're using the MSVC linker, it's not sufficient to just use link
475     // from the program PATH, because other environments like GnuWin32 install
476     // their own link.exe which may come first.
477     linkPath = FindVisualStudioExecutable(TC, "link.exe");
478 
479 #ifdef USE_WIN32
480     // When cross-compiling with VS2017 or newer, link.exe expects to have
481     // its containing bin directory at the top of PATH, followed by the
482     // native target bin directory.
483     // e.g. when compiling for x86 on an x64 host, PATH should start with:
484     // /bin/HostX64/x86;/bin/HostX64/x64
485     // This doesn't attempt to handle ToolsetLayout::DevDivInternal.
486     if (TC.getIsVS2017OrNewer() &&
487         llvm::Triple(llvm::sys::getProcessTriple()).getArch() != TC.getArch()) {
488       auto HostArch = llvm::Triple(llvm::sys::getProcessTriple()).getArch();
489 
490       auto EnvBlockWide =
491           std::unique_ptr<wchar_t[], decltype(&FreeEnvironmentStringsW)>(
492               GetEnvironmentStringsW(), FreeEnvironmentStringsW);
493       if (!EnvBlockWide)
494         goto SkipSettingEnvironment;
495 
496       size_t EnvCount = 0;
497       size_t EnvBlockLen = 0;
498       while (EnvBlockWide[EnvBlockLen] != L'\0') {
499         ++EnvCount;
500         EnvBlockLen += std::wcslen(&EnvBlockWide[EnvBlockLen]) +
501                        1 /*string null-terminator*/;
502       }
503       ++EnvBlockLen; // add the block null-terminator
504 
505       std::string EnvBlock;
506       if (!llvm::convertUTF16ToUTF8String(
507               llvm::ArrayRef<char>(reinterpret_cast<char *>(EnvBlockWide.get()),
508                                    EnvBlockLen * sizeof(EnvBlockWide[0])),
509               EnvBlock))
510         goto SkipSettingEnvironment;
511 
512       Environment.reserve(EnvCount);
513 
514       // Now loop over each string in the block and copy them into the
515       // environment vector, adjusting the PATH variable as needed when we
516       // find it.
517       for (const char *Cursor = EnvBlock.data(); *Cursor != '\0';) {
518         llvm::StringRef EnvVar(Cursor);
519         if (EnvVar.startswith_lower("path=")) {
520           using SubDirectoryType = toolchains::MSVCToolChain::SubDirectoryType;
521           constexpr size_t PrefixLen = 5; // strlen("path=")
522           Environment.push_back(Args.MakeArgString(
523               EnvVar.substr(0, PrefixLen) +
524               TC.getSubDirectoryPath(SubDirectoryType::Bin) +
525               llvm::Twine(llvm::sys::EnvPathSeparator) +
526               TC.getSubDirectoryPath(SubDirectoryType::Bin, HostArch) +
527               (EnvVar.size() > PrefixLen
528                    ? llvm::Twine(llvm::sys::EnvPathSeparator) +
529                          EnvVar.substr(PrefixLen)
530                    : "")));
531         } else {
532           Environment.push_back(Args.MakeArgString(EnvVar));
533         }
534         Cursor += EnvVar.size() + 1 /*null-terminator*/;
535       }
536     }
537   SkipSettingEnvironment:;
538 #endif
539   } else {
540     linkPath = TC.GetProgramPath(Linker.str().c_str());
541   }
542 
543   auto LinkCmd = llvm::make_unique<Command>(
544       JA, *this, Args.MakeArgString(linkPath), CmdArgs, Inputs);
545   if (!Environment.empty())
546     LinkCmd->setEnvironment(Environment);
547   C.addCommand(std::move(LinkCmd));
548 }
549 
550 void visualstudio::Compiler::ConstructJob(Compilation &C, const JobAction &JA,
551                                           const InputInfo &Output,
552                                           const InputInfoList &Inputs,
553                                           const ArgList &Args,
554                                           const char *LinkingOutput) const {
555   C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput));
556 }
557 
558 std::unique_ptr<Command> visualstudio::Compiler::GetCommand(
559     Compilation &C, const JobAction &JA, const InputInfo &Output,
560     const InputInfoList &Inputs, const ArgList &Args,
561     const char *LinkingOutput) const {
562   ArgStringList CmdArgs;
563   CmdArgs.push_back("/nologo");
564   CmdArgs.push_back("/c");  // Compile only.
565   CmdArgs.push_back("/W0"); // No warnings.
566 
567   // The goal is to be able to invoke this tool correctly based on
568   // any flag accepted by clang-cl.
569 
570   // These are spelled the same way in clang and cl.exe,.
571   Args.AddAllArgs(CmdArgs, {options::OPT_D, options::OPT_U, options::OPT_I});
572 
573   // Optimization level.
574   if (Arg *A = Args.getLastArg(options::OPT_fbuiltin, options::OPT_fno_builtin))
575     CmdArgs.push_back(A->getOption().getID() == options::OPT_fbuiltin ? "/Oi"
576                                                                       : "/Oi-");
577   if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
578     if (A->getOption().getID() == options::OPT_O0) {
579       CmdArgs.push_back("/Od");
580     } else {
581       CmdArgs.push_back("/Og");
582 
583       StringRef OptLevel = A->getValue();
584       if (OptLevel == "s" || OptLevel == "z")
585         CmdArgs.push_back("/Os");
586       else
587         CmdArgs.push_back("/Ot");
588 
589       CmdArgs.push_back("/Ob2");
590     }
591   }
592   if (Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer,
593                                options::OPT_fno_omit_frame_pointer))
594     CmdArgs.push_back(A->getOption().getID() == options::OPT_fomit_frame_pointer
595                           ? "/Oy"
596                           : "/Oy-");
597   if (!Args.hasArg(options::OPT_fwritable_strings))
598     CmdArgs.push_back("/GF");
599 
600   // Flags for which clang-cl has an alias.
601   // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
602 
603   if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
604                    /*default=*/false))
605     CmdArgs.push_back("/GR-");
606 
607   if (Args.hasFlag(options::OPT__SLASH_GS_, options::OPT__SLASH_GS,
608                    /*default=*/false))
609     CmdArgs.push_back("/GS-");
610 
611   if (Arg *A = Args.getLastArg(options::OPT_ffunction_sections,
612                                options::OPT_fno_function_sections))
613     CmdArgs.push_back(A->getOption().getID() == options::OPT_ffunction_sections
614                           ? "/Gy"
615                           : "/Gy-");
616   if (Arg *A = Args.getLastArg(options::OPT_fdata_sections,
617                                options::OPT_fno_data_sections))
618     CmdArgs.push_back(
619         A->getOption().getID() == options::OPT_fdata_sections ? "/Gw" : "/Gw-");
620   if (Args.hasArg(options::OPT_fsyntax_only))
621     CmdArgs.push_back("/Zs");
622   if (Args.hasArg(options::OPT_g_Flag, options::OPT_gline_tables_only,
623                   options::OPT__SLASH_Z7))
624     CmdArgs.push_back("/Z7");
625 
626   std::vector<std::string> Includes =
627       Args.getAllArgValues(options::OPT_include);
628   for (const auto &Include : Includes)
629     CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Include));
630 
631   // Flags that can simply be passed through.
632   Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD);
633   Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd);
634   Args.AddAllArgs(CmdArgs, options::OPT__SLASH_GX);
635   Args.AddAllArgs(CmdArgs, options::OPT__SLASH_GX_);
636   Args.AddAllArgs(CmdArgs, options::OPT__SLASH_EH);
637   Args.AddAllArgs(CmdArgs, options::OPT__SLASH_Zl);
638 
639   // The order of these flags is relevant, so pick the last one.
640   if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd,
641                                options::OPT__SLASH_MT, options::OPT__SLASH_MTd))
642     A->render(Args, CmdArgs);
643 
644   // Use MSVC's default threadsafe statics behaviour unless there was a flag.
645   if (Arg *A = Args.getLastArg(options::OPT_fthreadsafe_statics,
646                                options::OPT_fno_threadsafe_statics)) {
647     CmdArgs.push_back(A->getOption().getID() == options::OPT_fthreadsafe_statics
648                           ? "/Zc:threadSafeInit"
649                           : "/Zc:threadSafeInit-");
650   }
651 
652   // Pass through all unknown arguments so that the fallback command can see
653   // them too.
654   Args.AddAllArgs(CmdArgs, options::OPT_UNKNOWN);
655 
656   // Input filename.
657   assert(Inputs.size() == 1);
658   const InputInfo &II = Inputs[0];
659   assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
660   CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp");
661   if (II.isFilename())
662     CmdArgs.push_back(II.getFilename());
663   else
664     II.getInputArg().renderAsInput(Args, CmdArgs);
665 
666   // Output filename.
667   assert(Output.getType() == types::TY_Object);
668   const char *Fo =
669       Args.MakeArgString(std::string("/Fo") + Output.getFilename());
670   CmdArgs.push_back(Fo);
671 
672   std::string Exec = FindVisualStudioExecutable(getToolChain(), "cl.exe");
673   return llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
674                                     CmdArgs, Inputs);
675 }
676 
677 MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple &Triple,
678                              const ArgList &Args)
679     : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
680   getProgramPaths().push_back(getDriver().getInstalledDir());
681   if (getDriver().getInstalledDir() != getDriver().Dir)
682     getProgramPaths().push_back(getDriver().Dir);
683 
684   // Check the environment first, since that's probably the user telling us
685   // what they want to use.
686   // Failing that, just try to find the newest Visual Studio version we can
687   // and use its default VC toolchain.
688   findVCToolChainViaEnvironment(VCToolChainPath, VSLayout) ||
689       findVCToolChainViaSetupConfig(VCToolChainPath, VSLayout) ||
690       findVCToolChainViaRegistry(VCToolChainPath, VSLayout);
691 }
692 
693 Tool *MSVCToolChain::buildLinker() const {
694   if (VCToolChainPath.empty())
695     getDriver().Diag(clang::diag::warn_drv_msvc_not_found);
696   return new tools::visualstudio::Linker(*this);
697 }
698 
699 Tool *MSVCToolChain::buildAssembler() const {
700   if (getTriple().isOSBinFormatMachO())
701     return new tools::darwin::Assembler(*this);
702   getDriver().Diag(clang::diag::err_no_external_assembler);
703   return nullptr;
704 }
705 
706 bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
707   return true;
708 }
709 
710 bool MSVCToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
711   // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms
712   // such as ARM and PPC actually require unwind tables, but LLVM doesn't know
713   // how to generate them yet.
714 
715   // Don't emit unwind tables by default for MachO targets.
716   if (getTriple().isOSBinFormatMachO())
717     return false;
718 
719   return getArch() == llvm::Triple::x86_64;
720 }
721 
722 bool MSVCToolChain::isPICDefault() const {
723   return getArch() == llvm::Triple::x86_64;
724 }
725 
726 bool MSVCToolChain::isPIEDefault() const {
727   return false;
728 }
729 
730 bool MSVCToolChain::isPICDefaultForced() const {
731   return getArch() == llvm::Triple::x86_64;
732 }
733 
734 void MSVCToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
735                                        ArgStringList &CC1Args) const {
736   CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
737 }
738 
739 void MSVCToolChain::printVerboseInfo(raw_ostream &OS) const {
740   CudaInstallation.print(OS);
741 }
742 
743 // Windows SDKs and VC Toolchains group their contents into subdirectories based
744 // on the target architecture. This function converts an llvm::Triple::ArchType
745 // to the corresponding subdirectory name.
746 static const char *llvmArchToWindowsSDKArch(llvm::Triple::ArchType Arch) {
747   using ArchType = llvm::Triple::ArchType;
748   switch (Arch) {
749   case ArchType::x86:
750     return "x86";
751   case ArchType::x86_64:
752     return "x64";
753   case ArchType::arm:
754     return "arm";
755   default:
756     return "";
757   }
758 }
759 
760 // Similar to the above function, but for Visual Studios before VS2017.
761 static const char *llvmArchToLegacyVCArch(llvm::Triple::ArchType Arch) {
762   using ArchType = llvm::Triple::ArchType;
763   switch (Arch) {
764   case ArchType::x86:
765     // x86 is default in legacy VC toolchains.
766     // e.g. x86 libs are directly in /lib as opposed to /lib/x86.
767     return "";
768   case ArchType::x86_64:
769     return "amd64";
770   case ArchType::arm:
771     return "arm";
772   default:
773     return "";
774   }
775 }
776 
777 // Similar to the above function, but for DevDiv internal builds.
778 static const char *llvmArchToDevDivInternalArch(llvm::Triple::ArchType Arch) {
779   using ArchType = llvm::Triple::ArchType;
780   switch (Arch) {
781   case ArchType::x86:
782     return "i386";
783   case ArchType::x86_64:
784     return "amd64";
785   case ArchType::arm:
786     return "arm";
787   default:
788     return "";
789   }
790 }
791 
792 // Get the path to a specific subdirectory in the current toolchain for
793 // a given target architecture.
794 // VS2017 changed the VC toolchain layout, so this should be used instead
795 // of hardcoding paths.
796 std::string
797 MSVCToolChain::getSubDirectoryPath(SubDirectoryType Type,
798                                    llvm::Triple::ArchType TargetArch) const {
799   const char *SubdirName;
800   const char *IncludeName;
801   switch (VSLayout) {
802   case ToolsetLayout::OlderVS:
803     SubdirName = llvmArchToLegacyVCArch(TargetArch);
804     IncludeName = "include";
805     break;
806   case ToolsetLayout::VS2017OrNewer:
807     SubdirName = llvmArchToWindowsSDKArch(TargetArch);
808     IncludeName = "include";
809     break;
810   case ToolsetLayout::DevDivInternal:
811     SubdirName = llvmArchToDevDivInternalArch(TargetArch);
812     IncludeName = "inc";
813     break;
814   }
815 
816   llvm::SmallString<256> Path(VCToolChainPath);
817   switch (Type) {
818   case SubDirectoryType::Bin:
819     if (VSLayout == ToolsetLayout::VS2017OrNewer) {
820       const bool HostIsX64 =
821           llvm::Triple(llvm::sys::getProcessTriple()).isArch64Bit();
822       const char *const HostName = HostIsX64 ? "HostX64" : "HostX86";
823       llvm::sys::path::append(Path, "bin", HostName, SubdirName);
824     } else { // OlderVS or DevDivInternal
825       llvm::sys::path::append(Path, "bin", SubdirName);
826     }
827     break;
828   case SubDirectoryType::Include:
829     llvm::sys::path::append(Path, IncludeName);
830     break;
831   case SubDirectoryType::Lib:
832     llvm::sys::path::append(Path, "lib", SubdirName);
833     break;
834   }
835   return Path.str();
836 }
837 
838 #ifdef USE_WIN32
839 static bool readFullStringValue(HKEY hkey, const char *valueName,
840                                 std::string &value) {
841   std::wstring WideValueName;
842   if (!llvm::ConvertUTF8toWide(valueName, WideValueName))
843     return false;
844 
845   DWORD result = 0;
846   DWORD valueSize = 0;
847   DWORD type = 0;
848   // First just query for the required size.
849   result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL,
850                             &valueSize);
851   if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize)
852     return false;
853   std::vector<BYTE> buffer(valueSize);
854   result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
855                             &valueSize);
856   if (result == ERROR_SUCCESS) {
857     std::wstring WideValue(reinterpret_cast<const wchar_t *>(buffer.data()),
858                            valueSize / sizeof(wchar_t));
859     if (valueSize && WideValue.back() == L'\0') {
860       WideValue.pop_back();
861     }
862     // The destination buffer must be empty as an invariant of the conversion
863     // function; but this function is sometimes called in a loop that passes in
864     // the same buffer, however. Simply clear it out so we can overwrite it.
865     value.clear();
866     return llvm::convertWideToUTF8(WideValue, value);
867   }
868   return false;
869 }
870 #endif
871 
872 /// \brief Read registry string.
873 /// This also supports a means to look for high-versioned keys by use
874 /// of a $VERSION placeholder in the key path.
875 /// $VERSION in the key path is a placeholder for the version number,
876 /// causing the highest value path to be searched for and used.
877 /// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
878 /// There can be additional characters in the component.  Only the numeric
879 /// characters are compared.  This function only searches HKLM.
880 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
881                                     std::string &value, std::string *phValue) {
882 #ifndef USE_WIN32
883   return false;
884 #else
885   HKEY hRootKey = HKEY_LOCAL_MACHINE;
886   HKEY hKey = NULL;
887   long lResult;
888   bool returnValue = false;
889 
890   const char *placeHolder = strstr(keyPath, "$VERSION");
891   std::string bestName;
892   // If we have a $VERSION placeholder, do the highest-version search.
893   if (placeHolder) {
894     const char *keyEnd = placeHolder - 1;
895     const char *nextKey = placeHolder;
896     // Find end of previous key.
897     while ((keyEnd > keyPath) && (*keyEnd != '\\'))
898       keyEnd--;
899     // Find end of key containing $VERSION.
900     while (*nextKey && (*nextKey != '\\'))
901       nextKey++;
902     size_t partialKeyLength = keyEnd - keyPath;
903     char partialKey[256];
904     if (partialKeyLength >= sizeof(partialKey))
905       partialKeyLength = sizeof(partialKey) - 1;
906     strncpy(partialKey, keyPath, partialKeyLength);
907     partialKey[partialKeyLength] = '\0';
908     HKEY hTopKey = NULL;
909     lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
910                             &hTopKey);
911     if (lResult == ERROR_SUCCESS) {
912       char keyName[256];
913       double bestValue = 0.0;
914       DWORD index, size = sizeof(keyName) - 1;
915       for (index = 0; RegEnumKeyExA(hTopKey, index, keyName, &size, NULL, NULL,
916                                     NULL, NULL) == ERROR_SUCCESS;
917            index++) {
918         const char *sp = keyName;
919         while (*sp && !isDigit(*sp))
920           sp++;
921         if (!*sp)
922           continue;
923         const char *ep = sp + 1;
924         while (*ep && (isDigit(*ep) || (*ep == '.')))
925           ep++;
926         char numBuf[32];
927         strncpy(numBuf, sp, sizeof(numBuf) - 1);
928         numBuf[sizeof(numBuf) - 1] = '\0';
929         double dvalue = strtod(numBuf, NULL);
930         if (dvalue > bestValue) {
931           // Test that InstallDir is indeed there before keeping this index.
932           // Open the chosen key path remainder.
933           bestName = keyName;
934           // Append rest of key.
935           bestName.append(nextKey);
936           lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0,
937                                   KEY_READ | KEY_WOW64_32KEY, &hKey);
938           if (lResult == ERROR_SUCCESS) {
939             if (readFullStringValue(hKey, valueName, value)) {
940               bestValue = dvalue;
941               if (phValue)
942                 *phValue = bestName;
943               returnValue = true;
944             }
945             RegCloseKey(hKey);
946           }
947         }
948         size = sizeof(keyName) - 1;
949       }
950       RegCloseKey(hTopKey);
951     }
952   } else {
953     lResult =
954         RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
955     if (lResult == ERROR_SUCCESS) {
956       if (readFullStringValue(hKey, valueName, value))
957         returnValue = true;
958       if (phValue)
959         phValue->clear();
960       RegCloseKey(hKey);
961     }
962   }
963   return returnValue;
964 #endif // USE_WIN32
965 }
966 
967 // Find the most recent version of Universal CRT or Windows 10 SDK.
968 // vcvarsqueryregistry.bat from Visual Studio 2015 sorts entries in the include
969 // directory by name and uses the last one of the list.
970 // So we compare entry names lexicographically to find the greatest one.
971 static bool getWindows10SDKVersionFromPath(const std::string &SDKPath,
972                                            std::string &SDKVersion) {
973   SDKVersion.clear();
974 
975   std::error_code EC;
976   llvm::SmallString<128> IncludePath(SDKPath);
977   llvm::sys::path::append(IncludePath, "Include");
978   for (llvm::sys::fs::directory_iterator DirIt(IncludePath, EC), DirEnd;
979        DirIt != DirEnd && !EC; DirIt.increment(EC)) {
980     if (!llvm::sys::fs::is_directory(DirIt->path()))
981       continue;
982     StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
983     // If WDK is installed, there could be subfolders like "wdf" in the
984     // "Include" directory.
985     // Allow only directories which names start with "10.".
986     if (!CandidateName.startswith("10."))
987       continue;
988     if (CandidateName > SDKVersion)
989       SDKVersion = CandidateName;
990   }
991 
992   return !SDKVersion.empty();
993 }
994 
995 /// \brief Get Windows SDK installation directory.
996 static bool getWindowsSDKDir(std::string &Path, int &Major,
997                              std::string &WindowsSDKIncludeVersion,
998                              std::string &WindowsSDKLibVersion) {
999   std::string RegistrySDKVersion;
1000   // Try the Windows registry.
1001   if (!getSystemRegistryString(
1002           "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
1003           "InstallationFolder", Path, &RegistrySDKVersion))
1004     return false;
1005   if (Path.empty() || RegistrySDKVersion.empty())
1006     return false;
1007 
1008   WindowsSDKIncludeVersion.clear();
1009   WindowsSDKLibVersion.clear();
1010   Major = 0;
1011   std::sscanf(RegistrySDKVersion.c_str(), "v%d.", &Major);
1012   if (Major <= 7)
1013     return true;
1014   if (Major == 8) {
1015     // Windows SDK 8.x installs libraries in a folder whose names depend on the
1016     // version of the OS you're targeting.  By default choose the newest, which
1017     // usually corresponds to the version of the OS you've installed the SDK on.
1018     const char *Tests[] = {"winv6.3", "win8", "win7"};
1019     for (const char *Test : Tests) {
1020       llvm::SmallString<128> TestPath(Path);
1021       llvm::sys::path::append(TestPath, "Lib", Test);
1022       if (llvm::sys::fs::exists(TestPath.c_str())) {
1023         WindowsSDKLibVersion = Test;
1024         break;
1025       }
1026     }
1027     return !WindowsSDKLibVersion.empty();
1028   }
1029   if (Major == 10) {
1030     if (!getWindows10SDKVersionFromPath(Path, WindowsSDKIncludeVersion))
1031       return false;
1032     WindowsSDKLibVersion = WindowsSDKIncludeVersion;
1033     return true;
1034   }
1035   // Unsupported SDK version
1036   return false;
1037 }
1038 
1039 // Gets the library path required to link against the Windows SDK.
1040 bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const {
1041   std::string sdkPath;
1042   int sdkMajor = 0;
1043   std::string windowsSDKIncludeVersion;
1044   std::string windowsSDKLibVersion;
1045 
1046   path.clear();
1047   if (!getWindowsSDKDir(sdkPath, sdkMajor, windowsSDKIncludeVersion,
1048                         windowsSDKLibVersion))
1049     return false;
1050 
1051   llvm::SmallString<128> libPath(sdkPath);
1052   llvm::sys::path::append(libPath, "Lib");
1053   if (sdkMajor >= 8) {
1054     llvm::sys::path::append(libPath, windowsSDKLibVersion, "um",
1055                             llvmArchToWindowsSDKArch(getArch()));
1056   } else {
1057     switch (getArch()) {
1058     // In Windows SDK 7.x, x86 libraries are directly in the Lib folder.
1059     case llvm::Triple::x86:
1060       break;
1061     case llvm::Triple::x86_64:
1062       llvm::sys::path::append(libPath, "x64");
1063       break;
1064     case llvm::Triple::arm:
1065       // It is not necessary to link against Windows SDK 7.x when targeting ARM.
1066       return false;
1067     default:
1068       return false;
1069     }
1070   }
1071 
1072   path = libPath.str();
1073   return true;
1074 }
1075 
1076 // Check if the Include path of a specified version of Visual Studio contains
1077 // specific header files. If not, they are probably shipped with Universal CRT.
1078 bool MSVCToolChain::useUniversalCRT() const {
1079   llvm::SmallString<128> TestPath(
1080       getSubDirectoryPath(SubDirectoryType::Include));
1081   llvm::sys::path::append(TestPath, "stdlib.h");
1082   return !llvm::sys::fs::exists(TestPath);
1083 }
1084 
1085 static bool getUniversalCRTSdkDir(std::string &Path, std::string &UCRTVersion) {
1086   // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry
1087   // for the specific key "KitsRoot10". So do we.
1088   if (!getSystemRegistryString(
1089           "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", "KitsRoot10",
1090           Path, nullptr))
1091     return false;
1092 
1093   return getWindows10SDKVersionFromPath(Path, UCRTVersion);
1094 }
1095 
1096 bool MSVCToolChain::getUniversalCRTLibraryPath(std::string &Path) const {
1097   std::string UniversalCRTSdkPath;
1098   std::string UCRTVersion;
1099 
1100   Path.clear();
1101   if (!getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion))
1102     return false;
1103 
1104   StringRef ArchName = llvmArchToWindowsSDKArch(getArch());
1105   if (ArchName.empty())
1106     return false;
1107 
1108   llvm::SmallString<128> LibPath(UniversalCRTSdkPath);
1109   llvm::sys::path::append(LibPath, "Lib", UCRTVersion, "ucrt", ArchName);
1110 
1111   Path = LibPath.str();
1112   return true;
1113 }
1114 
1115 static VersionTuple getMSVCVersionFromTriple(const llvm::Triple &Triple) {
1116   unsigned Major, Minor, Micro;
1117   Triple.getEnvironmentVersion(Major, Minor, Micro);
1118   if (Major || Minor || Micro)
1119     return VersionTuple(Major, Minor, Micro);
1120   return VersionTuple();
1121 }
1122 
1123 static VersionTuple getMSVCVersionFromExe(const std::string &BinDir) {
1124   VersionTuple Version;
1125 #ifdef USE_WIN32
1126   SmallString<128> ClExe(BinDir);
1127   llvm::sys::path::append(ClExe, "cl.exe");
1128 
1129   std::wstring ClExeWide;
1130   if (!llvm::ConvertUTF8toWide(ClExe.c_str(), ClExeWide))
1131     return Version;
1132 
1133   const DWORD VersionSize = ::GetFileVersionInfoSizeW(ClExeWide.c_str(),
1134                                                       nullptr);
1135   if (VersionSize == 0)
1136     return Version;
1137 
1138   SmallVector<uint8_t, 4 * 1024> VersionBlock(VersionSize);
1139   if (!::GetFileVersionInfoW(ClExeWide.c_str(), 0, VersionSize,
1140                              VersionBlock.data()))
1141     return Version;
1142 
1143   VS_FIXEDFILEINFO *FileInfo = nullptr;
1144   UINT FileInfoSize = 0;
1145   if (!::VerQueryValueW(VersionBlock.data(), L"\\",
1146                         reinterpret_cast<LPVOID *>(&FileInfo), &FileInfoSize) ||
1147       FileInfoSize < sizeof(*FileInfo))
1148     return Version;
1149 
1150   const unsigned Major = (FileInfo->dwFileVersionMS >> 16) & 0xFFFF;
1151   const unsigned Minor = (FileInfo->dwFileVersionMS      ) & 0xFFFF;
1152   const unsigned Micro = (FileInfo->dwFileVersionLS >> 16) & 0xFFFF;
1153 
1154   Version = VersionTuple(Major, Minor, Micro);
1155 #endif
1156   return Version;
1157 }
1158 
1159 void MSVCToolChain::AddSystemIncludeWithSubfolder(
1160     const ArgList &DriverArgs, ArgStringList &CC1Args,
1161     const std::string &folder, const Twine &subfolder1, const Twine &subfolder2,
1162     const Twine &subfolder3) const {
1163   llvm::SmallString<128> path(folder);
1164   llvm::sys::path::append(path, subfolder1, subfolder2, subfolder3);
1165   addSystemInclude(DriverArgs, CC1Args, path);
1166 }
1167 
1168 void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1169                                               ArgStringList &CC1Args) const {
1170   if (DriverArgs.hasArg(options::OPT_nostdinc))
1171     return;
1172 
1173   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
1174     AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, getDriver().ResourceDir,
1175                                   "include");
1176   }
1177 
1178   // Add %INCLUDE%-like directories from the -imsvc flag.
1179   for (const auto &Path : DriverArgs.getAllArgValues(options::OPT__SLASH_imsvc))
1180     addSystemInclude(DriverArgs, CC1Args, Path);
1181 
1182   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
1183     return;
1184 
1185   // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
1186   if (llvm::Optional<std::string> cl_include_dir =
1187           llvm::sys::Process::GetEnv("INCLUDE")) {
1188     SmallVector<StringRef, 8> Dirs;
1189     StringRef(*cl_include_dir)
1190         .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
1191     for (StringRef Dir : Dirs)
1192       addSystemInclude(DriverArgs, CC1Args, Dir);
1193     if (!Dirs.empty())
1194       return;
1195   }
1196 
1197   // When built with access to the proper Windows APIs, try to actually find
1198   // the correct include paths first.
1199   if (!VCToolChainPath.empty()) {
1200     addSystemInclude(DriverArgs, CC1Args,
1201                      getSubDirectoryPath(SubDirectoryType::Include));
1202 
1203     if (useUniversalCRT()) {
1204       std::string UniversalCRTSdkPath;
1205       std::string UCRTVersion;
1206       if (getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) {
1207         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, UniversalCRTSdkPath,
1208                                       "Include", UCRTVersion, "ucrt");
1209       }
1210     }
1211 
1212     std::string WindowsSDKDir;
1213     int major;
1214     std::string windowsSDKIncludeVersion;
1215     std::string windowsSDKLibVersion;
1216     if (getWindowsSDKDir(WindowsSDKDir, major, windowsSDKIncludeVersion,
1217                          windowsSDKLibVersion)) {
1218       if (major >= 8) {
1219         // Note: windowsSDKIncludeVersion is empty for SDKs prior to v10.
1220         // Anyway, llvm::sys::path::append is able to manage it.
1221         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
1222                                       "include", windowsSDKIncludeVersion,
1223                                       "shared");
1224         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
1225                                       "include", windowsSDKIncludeVersion,
1226                                       "um");
1227         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
1228                                       "include", windowsSDKIncludeVersion,
1229                                       "winrt");
1230       } else {
1231         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
1232                                       "include");
1233       }
1234     }
1235 
1236     return;
1237   }
1238 
1239 #if defined(LLVM_ON_WIN32)
1240   // As a fallback, select default install paths.
1241   // FIXME: Don't guess drives and paths like this on Windows.
1242   const StringRef Paths[] = {
1243     "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
1244     "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
1245     "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
1246     "C:/Program Files/Microsoft Visual Studio 8/VC/include",
1247     "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
1248   };
1249   addSystemIncludes(DriverArgs, CC1Args, Paths);
1250 #endif
1251 }
1252 
1253 void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1254                                                  ArgStringList &CC1Args) const {
1255   // FIXME: There should probably be logic here to find libc++ on Windows.
1256 }
1257 
1258 VersionTuple MSVCToolChain::computeMSVCVersion(const Driver *D,
1259                                                const ArgList &Args) const {
1260   bool IsWindowsMSVC = getTriple().isWindowsMSVCEnvironment();
1261   VersionTuple MSVT = ToolChain::computeMSVCVersion(D, Args);
1262   if (MSVT.empty())
1263     MSVT = getMSVCVersionFromTriple(getTriple());
1264   if (MSVT.empty() && IsWindowsMSVC)
1265     MSVT = getMSVCVersionFromExe(getSubDirectoryPath(SubDirectoryType::Bin));
1266   if (MSVT.empty() &&
1267       Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1268                    IsWindowsMSVC)) {
1269     // -fms-compatibility-version=18.00 is default.
1270     // FIXME: Consider bumping this to 19 (MSVC2015) soon.
1271     MSVT = VersionTuple(18);
1272   }
1273   return MSVT;
1274 }
1275 
1276 std::string
1277 MSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
1278                                            types::ID InputType) const {
1279   // The MSVC version doesn't care about the architecture, even though it
1280   // may look at the triple internally.
1281   VersionTuple MSVT = computeMSVCVersion(/*D=*/nullptr, Args);
1282   MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0),
1283                       MSVT.getSubminor().getValueOr(0));
1284 
1285   // For the rest of the triple, however, a computed architecture name may
1286   // be needed.
1287   llvm::Triple Triple(ToolChain::ComputeEffectiveClangTriple(Args, InputType));
1288   if (Triple.getEnvironment() == llvm::Triple::MSVC) {
1289     StringRef ObjFmt = Triple.getEnvironmentName().split('-').second;
1290     if (ObjFmt.empty())
1291       Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str());
1292     else
1293       Triple.setEnvironmentName(
1294           (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str());
1295   }
1296   return Triple.getTriple();
1297 }
1298 
1299 SanitizerMask MSVCToolChain::getSupportedSanitizers() const {
1300   SanitizerMask Res = ToolChain::getSupportedSanitizers();
1301   Res |= SanitizerKind::Address;
1302   return Res;
1303 }
1304 
1305 static void TranslateOptArg(Arg *A, llvm::opt::DerivedArgList &DAL,
1306                             bool SupportsForcingFramePointer,
1307                             const char *ExpandChar, const OptTable &Opts) {
1308   assert(A->getOption().matches(options::OPT__SLASH_O));
1309 
1310   StringRef OptStr = A->getValue();
1311   for (size_t I = 0, E = OptStr.size(); I != E; ++I) {
1312     const char &OptChar = *(OptStr.data() + I);
1313     switch (OptChar) {
1314     default:
1315       break;
1316     case '1':
1317     case '2':
1318     case 'x':
1319     case 'd':
1320       if (&OptChar == ExpandChar) {
1321         if (OptChar == 'd') {
1322           DAL.AddFlagArg(A, Opts.getOption(options::OPT_O0));
1323         } else {
1324           if (OptChar == '1') {
1325             DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s");
1326           } else if (OptChar == '2' || OptChar == 'x') {
1327             DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
1328             DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
1329           }
1330           if (SupportsForcingFramePointer &&
1331               !DAL.hasArgNoClaim(options::OPT_fno_omit_frame_pointer))
1332             DAL.AddFlagArg(A,
1333                            Opts.getOption(options::OPT_fomit_frame_pointer));
1334           if (OptChar == '1' || OptChar == '2')
1335             DAL.AddFlagArg(A,
1336                            Opts.getOption(options::OPT_ffunction_sections));
1337         }
1338       }
1339       break;
1340     case 'b':
1341       if (I + 1 != E && isdigit(OptStr[I + 1])) {
1342         switch (OptStr[I + 1]) {
1343         case '0':
1344           DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_inline));
1345           break;
1346         case '1':
1347           DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_hint_functions));
1348           break;
1349         case '2':
1350           DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_functions));
1351           break;
1352         }
1353         ++I;
1354       }
1355       break;
1356     case 'g':
1357       break;
1358     case 'i':
1359       if (I + 1 != E && OptStr[I + 1] == '-') {
1360         ++I;
1361         DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_builtin));
1362       } else {
1363         DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
1364       }
1365       break;
1366     case 's':
1367       DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s");
1368       break;
1369     case 't':
1370       DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
1371       break;
1372     case 'y': {
1373       bool OmitFramePointer = true;
1374       if (I + 1 != E && OptStr[I + 1] == '-') {
1375         OmitFramePointer = false;
1376         ++I;
1377       }
1378       if (SupportsForcingFramePointer) {
1379         if (OmitFramePointer)
1380           DAL.AddFlagArg(A,
1381                          Opts.getOption(options::OPT_fomit_frame_pointer));
1382         else
1383           DAL.AddFlagArg(
1384               A, Opts.getOption(options::OPT_fno_omit_frame_pointer));
1385       } else {
1386         // Don't warn about /Oy- in 64-bit builds (where
1387         // SupportsForcingFramePointer is false).  The flag having no effect
1388         // there is a compiler-internal optimization, and people shouldn't have
1389         // to special-case their build files for 64-bit clang-cl.
1390         A->claim();
1391       }
1392       break;
1393     }
1394     }
1395   }
1396 }
1397 
1398 static void TranslateDArg(Arg *A, llvm::opt::DerivedArgList &DAL,
1399                           const OptTable &Opts) {
1400   assert(A->getOption().matches(options::OPT_D));
1401 
1402   StringRef Val = A->getValue();
1403   size_t Hash = Val.find('#');
1404   if (Hash == StringRef::npos || Hash > Val.find('=')) {
1405     DAL.append(A);
1406     return;
1407   }
1408 
1409   std::string NewVal = Val;
1410   NewVal[Hash] = '=';
1411   DAL.AddJoinedArg(A, Opts.getOption(options::OPT_D), NewVal);
1412 }
1413 
1414 llvm::opt::DerivedArgList *
1415 MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
1416                              StringRef BoundArch, Action::OffloadKind) const {
1417   DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
1418   const OptTable &Opts = getDriver().getOpts();
1419 
1420   // /Oy and /Oy- only has an effect under X86-32.
1421   bool SupportsForcingFramePointer = getArch() == llvm::Triple::x86;
1422 
1423   // The -O[12xd] flag actually expands to several flags.  We must desugar the
1424   // flags so that options embedded can be negated.  For example, the '-O2' flag
1425   // enables '-Oy'.  Expanding '-O2' into its constituent flags allows us to
1426   // correctly handle '-O2 -Oy-' where the trailing '-Oy-' disables a single
1427   // aspect of '-O2'.
1428   //
1429   // Note that this expansion logic only applies to the *last* of '[12xd]'.
1430 
1431   // First step is to search for the character we'd like to expand.
1432   const char *ExpandChar = nullptr;
1433   for (Arg *A : Args) {
1434     if (!A->getOption().matches(options::OPT__SLASH_O))
1435       continue;
1436     StringRef OptStr = A->getValue();
1437     for (size_t I = 0, E = OptStr.size(); I != E; ++I) {
1438       char OptChar = OptStr[I];
1439       char PrevChar = I > 0 ? OptStr[I - 1] : '0';
1440       if (PrevChar == 'b') {
1441         // OptChar does not expand; it's an argument to the previous char.
1442         continue;
1443       }
1444       if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd')
1445         ExpandChar = OptStr.data() + I;
1446     }
1447   }
1448 
1449   for (Arg *A : Args) {
1450     if (A->getOption().matches(options::OPT__SLASH_O)) {
1451       // The -O flag actually takes an amalgam of other options.  For example,
1452       // '/Ogyb2' is equivalent to '/Og' '/Oy' '/Ob2'.
1453       TranslateOptArg(A, *DAL, SupportsForcingFramePointer, ExpandChar, Opts);
1454     } else if (A->getOption().matches(options::OPT_D)) {
1455       // Translate -Dfoo#bar into -Dfoo=bar.
1456       TranslateDArg(A, *DAL, Opts);
1457     } else {
1458       DAL->append(A);
1459     }
1460   }
1461 
1462   return DAL;
1463 }
1464