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