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