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