clang  16.0.0git
MinGW.cpp
Go to the documentation of this file.
1 //===--- MinGW.cpp - MinGWToolChain Implementation ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MinGW.h"
10 #include "CommonArgs.h"
11 #include "clang/Config/config.h"
13 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/InputInfo.h"
16 #include "clang/Driver/Options.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/VirtualFileSystem.h"
22 #include <system_error>
23 
24 using namespace clang::diag;
25 using namespace clang::driver;
26 using namespace clang;
27 using namespace llvm::opt;
28 
29 /// MinGW Tools
30 void tools::MinGW::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
31  const InputInfo &Output,
32  const InputInfoList &Inputs,
33  const ArgList &Args,
34  const char *LinkingOutput) const {
35  claimNoWarnArgs(Args);
36  ArgStringList CmdArgs;
37 
38  if (getToolChain().getArch() == llvm::Triple::x86) {
39  CmdArgs.push_back("--32");
40  } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
41  CmdArgs.push_back("--64");
42  }
43 
44  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
45 
46  CmdArgs.push_back("-o");
47  CmdArgs.push_back(Output.getFilename());
48 
49  for (const auto &II : Inputs)
50  CmdArgs.push_back(II.getFilename());
51 
52  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
53  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
54  Exec, CmdArgs, Inputs, Output));
55 
56  if (Args.hasArg(options::OPT_gsplit_dwarf))
57  SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
58  SplitDebugName(JA, Args, Inputs[0], Output));
59 }
60 
61 void tools::MinGW::Linker::AddLibGCC(const ArgList &Args,
62  ArgStringList &CmdArgs) const {
63  if (Args.hasArg(options::OPT_mthreads))
64  CmdArgs.push_back("-lmingwthrd");
65  CmdArgs.push_back("-lmingw32");
66 
67  // Make use of compiler-rt if --rtlib option is used
68  ToolChain::RuntimeLibType RLT = getToolChain().GetRuntimeLibType(Args);
69  if (RLT == ToolChain::RLT_Libgcc) {
70  bool Static = Args.hasArg(options::OPT_static_libgcc) ||
71  Args.hasArg(options::OPT_static);
72  bool Shared = Args.hasArg(options::OPT_shared);
73  bool CXX = getToolChain().getDriver().CCCIsCXX();
74 
75  if (Static || (!CXX && !Shared)) {
76  CmdArgs.push_back("-lgcc");
77  CmdArgs.push_back("-lgcc_eh");
78  } else {
79  CmdArgs.push_back("-lgcc_s");
80  CmdArgs.push_back("-lgcc");
81  }
82  } else {
83  AddRunTimeLibs(getToolChain(), getToolChain().getDriver(), CmdArgs, Args);
84  }
85 
86  CmdArgs.push_back("-lmoldname");
87  CmdArgs.push_back("-lmingwex");
88  for (auto Lib : Args.getAllArgValues(options::OPT_l))
89  if (StringRef(Lib).startswith("msvcr") ||
90  StringRef(Lib).startswith("ucrt") ||
91  StringRef(Lib).startswith("crtdll"))
92  return;
93  CmdArgs.push_back("-lmsvcrt");
94 }
95 
96 void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
97  const InputInfo &Output,
98  const InputInfoList &Inputs,
99  const ArgList &Args,
100  const char *LinkingOutput) const {
101  const ToolChain &TC = getToolChain();
102  const Driver &D = TC.getDriver();
103  const SanitizerArgs &Sanitize = TC.getSanitizerArgs(Args);
104 
105  ArgStringList CmdArgs;
106 
107  // Silence warning for "clang -g foo.o -o foo"
108  Args.ClaimAllArgs(options::OPT_g_Group);
109  // and "clang -emit-llvm foo.o -o foo"
110  Args.ClaimAllArgs(options::OPT_emit_llvm);
111  // and for "clang -w foo.o -o foo". Other warning options are already
112  // handled somewhere else.
113  Args.ClaimAllArgs(options::OPT_w);
114 
115  if (!D.SysRoot.empty())
116  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
117 
118  if (Args.hasArg(options::OPT_s))
119  CmdArgs.push_back("-s");
120 
121  CmdArgs.push_back("-m");
122  switch (TC.getArch()) {
123  case llvm::Triple::x86:
124  CmdArgs.push_back("i386pe");
125  break;
126  case llvm::Triple::x86_64:
127  CmdArgs.push_back("i386pep");
128  break;
129  case llvm::Triple::arm:
130  case llvm::Triple::thumb:
131  // FIXME: this is incorrect for WinCE
132  CmdArgs.push_back("thumb2pe");
133  break;
134  case llvm::Triple::aarch64:
135  CmdArgs.push_back("arm64pe");
136  break;
137  default:
138  llvm_unreachable("Unsupported target architecture.");
139  }
140 
141  Arg *SubsysArg =
142  Args.getLastArg(options::OPT_mwindows, options::OPT_mconsole);
143  if (SubsysArg && SubsysArg->getOption().matches(options::OPT_mwindows)) {
144  CmdArgs.push_back("--subsystem");
145  CmdArgs.push_back("windows");
146  } else if (SubsysArg &&
147  SubsysArg->getOption().matches(options::OPT_mconsole)) {
148  CmdArgs.push_back("--subsystem");
149  CmdArgs.push_back("console");
150  }
151 
152  if (Args.hasArg(options::OPT_mdll))
153  CmdArgs.push_back("--dll");
154  else if (Args.hasArg(options::OPT_shared))
155  CmdArgs.push_back("--shared");
156  if (Args.hasArg(options::OPT_static))
157  CmdArgs.push_back("-Bstatic");
158  else
159  CmdArgs.push_back("-Bdynamic");
160  if (Args.hasArg(options::OPT_mdll) || Args.hasArg(options::OPT_shared)) {
161  CmdArgs.push_back("-e");
162  if (TC.getArch() == llvm::Triple::x86)
163  CmdArgs.push_back("_DllMainCRTStartup@12");
164  else
165  CmdArgs.push_back("DllMainCRTStartup");
166  CmdArgs.push_back("--enable-auto-image-base");
167  }
168 
169  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
170  CmdArgs.push_back("--no-demangle");
171 
172  if (Arg *A = Args.getLastArg(options::OPT_mguard_EQ)) {
173  StringRef GuardArgs = A->getValue();
174  if (GuardArgs == "none")
175  CmdArgs.push_back("--no-guard-cf");
176  else if (GuardArgs == "cf" || GuardArgs == "cf-nochecks")
177  CmdArgs.push_back("--guard-cf");
178  else
179  D.Diag(diag::err_drv_unsupported_option_argument)
180  << A->getSpelling() << GuardArgs;
181  }
182 
183  CmdArgs.push_back("-o");
184  const char *OutputFile = Output.getFilename();
185  // GCC implicitly adds an .exe extension if it is given an output file name
186  // that lacks an extension.
187  // GCC used to do this only when the compiler itself runs on windows, but
188  // since GCC 8 it does the same when cross compiling as well.
189  if (!llvm::sys::path::has_extension(OutputFile)) {
190  CmdArgs.push_back(Args.MakeArgString(Twine(OutputFile) + ".exe"));
191  OutputFile = CmdArgs.back();
192  } else
193  CmdArgs.push_back(OutputFile);
194 
195  Args.AddAllArgs(CmdArgs, options::OPT_e);
196  // FIXME: add -N, -n flags
197  Args.AddLastArg(CmdArgs, options::OPT_r);
198  Args.AddLastArg(CmdArgs, options::OPT_s);
199  Args.AddLastArg(CmdArgs, options::OPT_t);
200  Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
201  Args.AddLastArg(CmdArgs, options::OPT_Z_Flag);
202 
203  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
204  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)) {
205  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("dllcrt2.o")));
206  } else {
207  if (Args.hasArg(options::OPT_municode))
208  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2u.o")));
209  else
210  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2.o")));
211  }
212  if (Args.hasArg(options::OPT_pg))
213  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("gcrt2.o")));
214  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o")));
215  }
216 
217  Args.AddAllArgs(CmdArgs, options::OPT_L);
218  TC.AddFilePathLibArgs(Args, CmdArgs);
219 
220  // Add the compiler-rt library directories if they exist to help
221  // the linker find the various sanitizer, builtin, and profiling runtimes.
222  for (const auto &LibPath : TC.getLibraryPaths()) {
223  if (TC.getVFS().exists(LibPath))
224  CmdArgs.push_back(Args.MakeArgString("-L" + LibPath));
225  }
226  auto CRTPath = TC.getCompilerRTPath();
227  if (TC.getVFS().exists(CRTPath))
228  CmdArgs.push_back(Args.MakeArgString("-L" + CRTPath));
229 
230  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
231 
232  if (C.getDriver().IsFlangMode()) {
233  addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
234  addFortranRuntimeLibs(TC, CmdArgs);
235  }
236 
237  // TODO: Add profile stuff here
238 
239  if (TC.ShouldLinkCXXStdlib(Args)) {
240  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
241  !Args.hasArg(options::OPT_static);
242  if (OnlyLibstdcxxStatic)
243  CmdArgs.push_back("-Bstatic");
244  TC.AddCXXStdlibLibArgs(Args, CmdArgs);
245  if (OnlyLibstdcxxStatic)
246  CmdArgs.push_back("-Bdynamic");
247  }
248 
249  bool HasWindowsApp = false;
250  for (auto Lib : Args.getAllArgValues(options::OPT_l)) {
251  if (Lib == "windowsapp") {
252  HasWindowsApp = true;
253  break;
254  }
255  }
256 
257  if (!Args.hasArg(options::OPT_nostdlib)) {
258  if (!Args.hasArg(options::OPT_nodefaultlibs)) {
259  if (Args.hasArg(options::OPT_static))
260  CmdArgs.push_back("--start-group");
261 
262  if (Args.hasArg(options::OPT_fstack_protector) ||
263  Args.hasArg(options::OPT_fstack_protector_strong) ||
264  Args.hasArg(options::OPT_fstack_protector_all)) {
265  CmdArgs.push_back("-lssp_nonshared");
266  CmdArgs.push_back("-lssp");
267  }
268 
269  if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
270  options::OPT_fno_openmp, false)) {
271  switch (TC.getDriver().getOpenMPRuntime(Args)) {
272  case Driver::OMPRT_OMP:
273  CmdArgs.push_back("-lomp");
274  break;
275  case Driver::OMPRT_IOMP5:
276  CmdArgs.push_back("-liomp5md");
277  break;
278  case Driver::OMPRT_GOMP:
279  CmdArgs.push_back("-lgomp");
280  break;
281  case Driver::OMPRT_Unknown:
282  // Already diagnosed.
283  break;
284  }
285  }
286 
287  AddLibGCC(Args, CmdArgs);
288 
289  if (Args.hasArg(options::OPT_pg))
290  CmdArgs.push_back("-lgmon");
291 
292  if (Args.hasArg(options::OPT_pthread))
293  CmdArgs.push_back("-lpthread");
294 
295  if (Sanitize.needsAsanRt()) {
296  // MinGW always links against a shared MSVCRT.
297  CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dynamic",
298  ToolChain::FT_Shared));
299  CmdArgs.push_back(
300  TC.getCompilerRTArgString(Args, "asan_dynamic_runtime_thunk"));
301  CmdArgs.push_back("--require-defined");
302  CmdArgs.push_back(TC.getArch() == llvm::Triple::x86
303  ? "___asan_seh_interceptor"
304  : "__asan_seh_interceptor");
305  // Make sure the linker consider all object files from the dynamic
306  // runtime thunk.
307  CmdArgs.push_back("--whole-archive");
308  CmdArgs.push_back(
309  TC.getCompilerRTArgString(Args, "asan_dynamic_runtime_thunk"));
310  CmdArgs.push_back("--no-whole-archive");
311  }
312 
313  TC.addProfileRTLibs(Args, CmdArgs);
314 
315  if (!HasWindowsApp) {
316  // Add system libraries. If linking to libwindowsapp.a, that import
317  // library replaces all these and we shouldn't accidentally try to
318  // link to the normal desktop mode dlls.
319  if (Args.hasArg(options::OPT_mwindows)) {
320  CmdArgs.push_back("-lgdi32");
321  CmdArgs.push_back("-lcomdlg32");
322  }
323  CmdArgs.push_back("-ladvapi32");
324  CmdArgs.push_back("-lshell32");
325  CmdArgs.push_back("-luser32");
326  CmdArgs.push_back("-lkernel32");
327  }
328 
329  if (Args.hasArg(options::OPT_static)) {
330  CmdArgs.push_back("--end-group");
331  } else {
332  AddLibGCC(Args, CmdArgs);
333  if (!HasWindowsApp)
334  CmdArgs.push_back("-lkernel32");
335  }
336  }
337 
338  if (!Args.hasArg(options::OPT_nostartfiles)) {
339  // Add crtfastmath.o if available and fast math is enabled.
340  TC.addFastMathRuntimeIfAvailable(Args, CmdArgs);
341 
342  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o")));
343  }
344  }
345  const char *Exec = Args.MakeArgString(TC.GetLinkerPath());
346  C.addCommand(std::make_unique<Command>(JA, *this,
347  ResponseFileSupport::AtFileUTF8(),
348  Exec, CmdArgs, Inputs, Output));
349 }
350 
351 // Simplified from Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple.
352 static bool findGccVersion(StringRef LibDir, std::string &GccLibDir,
353  std::string &Ver,
355  Version = toolchains::Generic_GCC::GCCVersion::Parse("0.0.0");
356  std::error_code EC;
357  for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
358  LI = LI.increment(EC)) {
359  StringRef VersionText = llvm::sys::path::filename(LI->path());
360  auto CandidateVersion =
361  toolchains::Generic_GCC::GCCVersion::Parse(VersionText);
362  if (CandidateVersion.Major == -1)
363  continue;
364  if (CandidateVersion <= Version)
365  continue;
366  Version = CandidateVersion;
367  Ver = std::string(VersionText);
368  GccLibDir = LI->path();
369  }
370  return Ver.size();
371 }
372 
373 void toolchains::MinGW::findGccLibDir() {
375  SubdirNames.emplace_back(getTriple().getArchName());
376  SubdirNames[0] += "-w64-mingw32";
377  SubdirNames.emplace_back("mingw32");
378  if (SubdirName.empty())
379  SubdirName = std::string(SubdirNames[0].str());
380  // lib: Arch Linux, Ubuntu, Windows
381  // lib64: openSUSE Linux
382  for (StringRef CandidateLib : {"lib", "lib64"}) {
383  for (StringRef CandidateSysroot : SubdirNames) {
385  llvm::sys::path::append(LibDir, CandidateLib, "gcc", CandidateSysroot);
386  if (findGccVersion(LibDir, GccLibDir, Ver, GccVer)) {
387  SubdirName = std::string(CandidateSysroot);
388  return;
389  }
390  }
391  }
392 }
393 
394 static llvm::ErrorOr<std::string> findGcc(const llvm::Triple &T) {
396  Gccs.emplace_back(T.getArchName());
397  Gccs[0] += "-w64-mingw32-gcc";
398  Gccs.emplace_back("mingw32-gcc");
399  // Please do not add "gcc" here
400  for (StringRef CandidateGcc : Gccs)
401  if (llvm::ErrorOr<std::string> GPPName = llvm::sys::findProgramByName(CandidateGcc))
402  return GPPName;
403  return make_error_code(std::errc::no_such_file_or_directory);
404 }
405 
406 static llvm::ErrorOr<std::string>
407 findClangRelativeSysroot(const Driver &D, const llvm::Triple &T,
408  std::string &SubdirName) {
410  Subdirs.emplace_back(T.str());
411  Subdirs.emplace_back(T.getArchName());
412  Subdirs[1] += "-w64-mingw32";
413  StringRef ClangRoot = llvm::sys::path::parent_path(D.getInstalledDir());
414  StringRef Sep = llvm::sys::path::get_separator();
415  for (StringRef CandidateSubdir : Subdirs) {
416  if (llvm::sys::fs::is_directory(ClangRoot + Sep + CandidateSubdir)) {
417  SubdirName = std::string(CandidateSubdir);
418  return (ClangRoot + Sep + CandidateSubdir).str();
419  }
420  }
421  return make_error_code(std::errc::no_such_file_or_directory);
422 }
423 
424 toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple,
425  const ArgList &Args)
426  : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args),
427  RocmInstallation(D, Triple, Args) {
428  getProgramPaths().push_back(getDriver().getInstalledDir());
429 
430  // The sequence for detecting a sysroot here should be kept in sync with
431  // the testTriple function below.
432  if (getDriver().SysRoot.size())
433  Base = getDriver().SysRoot;
434  // Look for <clang-bin>/../<triplet>; if found, use <clang-bin>/.. as the
435  // base as it could still be a base for a gcc setup with libgcc.
436  else if (llvm::ErrorOr<std::string> TargetSubdir =
437  findClangRelativeSysroot(getDriver(), getTriple(), SubdirName))
438  Base = std::string(llvm::sys::path::parent_path(TargetSubdir.get()));
439  else if (llvm::ErrorOr<std::string> GPPName = findGcc(getTriple()))
440  Base = std::string(llvm::sys::path::parent_path(
441  llvm::sys::path::parent_path(GPPName.get())));
442  else
443  Base = std::string(
444  llvm::sys::path::parent_path(getDriver().getInstalledDir()));
445 
446  Base += llvm::sys::path::get_separator();
447  findGccLibDir();
448  // GccLibDir must precede Base/lib so that the
449  // correct crtbegin.o ,cetend.o would be found.
450  getFilePaths().push_back(GccLibDir);
451  getFilePaths().push_back(
452  (Base + SubdirName + llvm::sys::path::get_separator() + "lib").str());
453 
454  // Gentoo
455  getFilePaths().push_back(
456  (Base + SubdirName + llvm::sys::path::get_separator() + "mingw/lib").str());
457 
458  getFilePaths().push_back(Base + "lib");
459  // openSUSE
460  getFilePaths().push_back(Base + SubdirName + "/sys-root/mingw/lib");
461 
462  NativeLLVMSupport =
463  Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER)
464  .equals_insensitive("lld");
465 }
466 
468 
470  switch (AC) {
472  if (!Preprocessor)
473  Preprocessor.reset(new tools::gcc::Preprocessor(*this));
474  return Preprocessor.get();
476  if (!Compiler)
477  Compiler.reset(new tools::gcc::Compiler(*this));
478  return Compiler.get();
479  default:
480  return ToolChain::getTool(AC);
481  }
482 }
483 
485  return new tools::MinGW::Assembler(*this);
486 }
487 
489  return new tools::MinGW::Linker(*this);
490 }
491 
493  return NativeLLVMSupport;
494 }
495 
498  Arg *ExceptionArg = Args.getLastArg(options::OPT_fsjlj_exceptions,
499  options::OPT_fseh_exceptions,
500  options::OPT_fdwarf_exceptions);
501  if (ExceptionArg &&
502  ExceptionArg->getOption().matches(options::OPT_fseh_exceptions))
503  return UnwindTableLevel::Asynchronous;
504 
505  if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
506  getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64)
507  return UnwindTableLevel::Asynchronous;
508  return UnwindTableLevel::None;
509 }
510 
512  return getArch() == llvm::Triple::x86_64 ||
513  getArch() == llvm::Triple::aarch64;
514 }
515 
516 bool toolchains::MinGW::isPIEDefault(const llvm::opt::ArgList &Args) const {
517  return false;
518 }
519 
520 bool toolchains::MinGW::isPICDefaultForced() const { return true; }
521 
522 llvm::ExceptionHandling
523 toolchains::MinGW::GetExceptionModel(const ArgList &Args) const {
524  if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch64 ||
525  getArch() == llvm::Triple::arm || getArch() == llvm::Triple::thumb)
526  return llvm::ExceptionHandling::WinEH;
527  return llvm::ExceptionHandling::DwarfCFI;
528 }
529 
532  Res |= SanitizerKind::Address;
533  Res |= SanitizerKind::PointerCompare;
534  Res |= SanitizerKind::PointerSubtract;
535  Res |= SanitizerKind::Vptr;
536  return Res;
537 }
538 
539 void toolchains::MinGW::AddCudaIncludeArgs(const ArgList &DriverArgs,
540  ArgStringList &CC1Args) const {
541  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
542 }
543 
544 void toolchains::MinGW::AddHIPIncludeArgs(const ArgList &DriverArgs,
545  ArgStringList &CC1Args) const {
546  RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
547 }
548 
549 void toolchains::MinGW::printVerboseInfo(raw_ostream &OS) const {
550  CudaInstallation.print(OS);
551  RocmInstallation.print(OS);
552 }
553 
554 // Include directories for various hosts:
555 
556 // Windows, mingw.org
557 // c:\mingw\lib\gcc\mingw32\4.8.1\include\c++
558 // c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\mingw32
559 // c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\backward
560 // c:\mingw\include
561 // c:\mingw\mingw32\include
562 
563 // Windows, mingw-w64 mingw-builds
564 // c:\mingw32\i686-w64-mingw32\include
565 // c:\mingw32\i686-w64-mingw32\include\c++
566 // c:\mingw32\i686-w64-mingw32\include\c++\i686-w64-mingw32
567 // c:\mingw32\i686-w64-mingw32\include\c++\backward
568 
569 // Windows, mingw-w64 msys2
570 // c:\msys64\mingw32\include
571 // c:\msys64\mingw32\i686-w64-mingw32\include
572 // c:\msys64\mingw32\include\c++\4.9.2
573 // c:\msys64\mingw32\include\c++\4.9.2\i686-w64-mingw32
574 // c:\msys64\mingw32\include\c++\4.9.2\backward
575 
576 // openSUSE
577 // /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++
578 // /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32
579 // /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward
580 // /usr/x86_64-w64-mingw32/sys-root/mingw/include
581 
582 // Arch Linux
583 // /usr/i686-w64-mingw32/include/c++/5.1.0
584 // /usr/i686-w64-mingw32/include/c++/5.1.0/i686-w64-mingw32
585 // /usr/i686-w64-mingw32/include/c++/5.1.0/backward
586 // /usr/i686-w64-mingw32/include
587 
588 // Ubuntu
589 // /usr/include/c++/4.8
590 // /usr/include/c++/4.8/x86_64-w64-mingw32
591 // /usr/include/c++/4.8/backward
592 // /usr/x86_64-w64-mingw32/include
593 
594 void toolchains::MinGW::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
595  ArgStringList &CC1Args) const {
596  if (DriverArgs.hasArg(options::OPT_nostdinc))
597  return;
598 
599  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
600  SmallString<1024> P(getDriver().ResourceDir);
601  llvm::sys::path::append(P, "include");
602  addSystemInclude(DriverArgs, CC1Args, P.str());
603  }
604 
605  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
606  return;
607 
608  if (GetRuntimeLibType(DriverArgs) == ToolChain::RLT_Libgcc) {
609  // openSUSE
610  addSystemInclude(DriverArgs, CC1Args,
611  Base + SubdirName + "/sys-root/mingw/include");
612  }
613 
614  addSystemInclude(DriverArgs, CC1Args,
615  Base + SubdirName + llvm::sys::path::get_separator() +
616  "include");
617 
618  // Gentoo
619  addSystemInclude(DriverArgs, CC1Args,
620  Base + SubdirName + llvm::sys::path::get_separator() + "usr/include");
621 
622  addSystemInclude(DriverArgs, CC1Args, Base + "include");
623 }
624 
626  const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
627  Action::OffloadKind DeviceOffloadKind) const {
628  if (Arg *A = DriverArgs.getLastArg(options::OPT_mguard_EQ)) {
629  StringRef GuardArgs = A->getValue();
630  if (GuardArgs == "none") {
631  // Do nothing.
632  } else if (GuardArgs == "cf") {
633  // Emit CFG instrumentation and the table of address-taken functions.
634  CC1Args.push_back("-cfguard");
635  } else if (GuardArgs == "cf-nochecks") {
636  // Emit only the table of address-taken functions.
637  CC1Args.push_back("-cfguard-no-checks");
638  } else {
639  getDriver().Diag(diag::err_drv_unsupported_option_argument)
640  << A->getSpelling() << GuardArgs;
641  }
642  }
643 }
644 
646  const ArgList &DriverArgs, ArgStringList &CC1Args) const {
647  if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
648  options::OPT_nostdincxx))
649  return;
650 
651  StringRef Slash = llvm::sys::path::get_separator();
652 
653  switch (GetCXXStdlibType(DriverArgs)) {
654  case ToolChain::CST_Libcxx: {
655  std::string TargetDir = (Base + "include" + Slash + getTripleString() +
656  Slash + "c++" + Slash + "v1")
657  .str();
658  if (getDriver().getVFS().exists(TargetDir))
659  addSystemInclude(DriverArgs, CC1Args, TargetDir);
660  addSystemInclude(DriverArgs, CC1Args,
661  Base + SubdirName + Slash + "include" + Slash + "c++" +
662  Slash + "v1");
663  addSystemInclude(DriverArgs, CC1Args,
664  Base + "include" + Slash + "c++" + Slash + "v1");
665  break;
666  }
667 
669  llvm::SmallVector<llvm::SmallString<1024>, 7> CppIncludeBases;
670  CppIncludeBases.emplace_back(Base);
671  llvm::sys::path::append(CppIncludeBases[0], SubdirName, "include", "c++");
672  CppIncludeBases.emplace_back(Base);
673  llvm::sys::path::append(CppIncludeBases[1], SubdirName, "include", "c++",
674  Ver);
675  CppIncludeBases.emplace_back(Base);
676  llvm::sys::path::append(CppIncludeBases[2], "include", "c++", Ver);
677  CppIncludeBases.emplace_back(GccLibDir);
678  llvm::sys::path::append(CppIncludeBases[3], "include", "c++");
679  CppIncludeBases.emplace_back(GccLibDir);
680  llvm::sys::path::append(CppIncludeBases[4], "include",
681  "g++-v" + GccVer.Text);
682  CppIncludeBases.emplace_back(GccLibDir);
683  llvm::sys::path::append(CppIncludeBases[5], "include",
684  "g++-v" + GccVer.MajorStr + "." + GccVer.MinorStr);
685  CppIncludeBases.emplace_back(GccLibDir);
686  llvm::sys::path::append(CppIncludeBases[6], "include",
687  "g++-v" + GccVer.MajorStr);
688  for (auto &CppIncludeBase : CppIncludeBases) {
689  addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
690  CppIncludeBase += Slash;
691  addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + SubdirName);
692  addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + "backward");
693  }
694  break;
695  }
696 }
697 
698 static bool testTriple(const Driver &D, const llvm::Triple &Triple,
699  const ArgList &Args) {
700  // If an explicit sysroot is set, that will be used and we shouldn't try to
701  // detect anything else.
702  std::string SubdirName;
703  if (D.SysRoot.size())
704  return true;
705  if (llvm::ErrorOr<std::string> TargetSubdir =
706  findClangRelativeSysroot(D, Triple, SubdirName))
707  return true;
708  if (llvm::ErrorOr<std::string> GPPName = findGcc(Triple))
709  return true;
710  // If we neither found a colocated sysroot or a matching gcc executable,
711  // conclude that we can't know if this is the correct spelling of the triple.
712  return false;
713 }
714 
715 static llvm::Triple adjustTriple(const Driver &D, const llvm::Triple &Triple,
716  const ArgList &Args) {
717  // First test if the original triple can find a sysroot with the triple
718  // name.
719  if (testTriple(D, Triple, Args))
720  return Triple;
722  // If not, test a couple other possible arch names that might be what was
723  // intended.
724  if (Triple.getArch() == llvm::Triple::x86) {
725  Archs.emplace_back("i386");
726  Archs.emplace_back("i586");
727  Archs.emplace_back("i686");
728  } else if (Triple.getArch() == llvm::Triple::arm ||
729  Triple.getArch() == llvm::Triple::thumb) {
730  Archs.emplace_back("armv7");
731  }
732  for (auto A : Archs) {
733  llvm::Triple TestTriple(Triple);
734  TestTriple.setArchName(A);
735  if (testTriple(D, TestTriple, Args))
736  return TestTriple;
737  }
738  // If none was found, just proceed with the original value.
739  return Triple;
740 }
741 
742 void toolchains::MinGW::fixTripleArch(const Driver &D, llvm::Triple &Triple,
743  const ArgList &Args) {
744  if (Triple.getArch() == llvm::Triple::x86 ||
745  Triple.getArch() == llvm::Triple::arm ||
746  Triple.getArch() == llvm::Triple::thumb)
747  Triple = adjustTriple(D, Triple, Args);
748 }
clang::driver::ToolChain::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:99
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:277
clang::diag
Definition: CLWarnings.h:16
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::toolchains::MinGW::GetExceptionModel
llvm::ExceptionHandling GetExceptionModel(const llvm::opt::ArgList &Args) const override
GetExceptionModel - Return the tool chain exception model.
Definition: MinGW.cpp:523
clang::driver::toolchains::MinGW::buildAssembler
Tool * buildAssembler() const override
Definition: MinGW.cpp:484
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::ToolChain::UnwindTableLevel
UnwindTableLevel
Definition: ToolChain.h:111
clang::driver::Action::ActionClass
ActionClass
Definition: Action.h:55
findGccVersion
static bool findGccVersion(StringRef LibDir, std::string &GccLibDir, std::string &Ver, toolchains::Generic_GCC::GCCVersion &Version)
Definition: MinGW.cpp:352
clang::driver::toolchains::MinGW::IsIntegratedAssemblerDefault
bool IsIntegratedAssemblerDefault() const override
IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as by default.
Definition: MinGW.cpp:467
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:232
clang::driver::toolchains::MinGW::printVerboseInfo
void printVerboseInfo(raw_ostream &OS) const override
Dispatch to the specific toolchain for verbose printing.
Definition: MinGW.cpp:549
clang::driver::ToolChain::addFastMathRuntimeIfAvailable
bool addFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFastMathRuntimeIfAvailable - If a runtime library exists that sets global flags for unsafe floatin...
Definition: ToolChain.cpp:1071
clang::driver::ToolChain::getCompilerRTPath
virtual std::string getCompilerRTPath() const
Definition: ToolChain.cpp:462
clang::driver::Driver::getOpenMPRuntime
OpenMPRuntimeKind getOpenMPRuntime(const llvm::opt::ArgList &Args) const
Compute the desired OpenMP runtime from the flags provided.
Definition: Driver.cpp:726
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:610
clang::driver::toolchains::MinGW::getSupportedSanitizers
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: MinGW.cpp:530
clang::driver::tools::SplitDebugInfo
void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T, const JobAction &JA, const llvm::opt::ArgList &Args, const InputInfo &Output, const char *OutFile)
clang::make_error_code
std::error_code make_error_code(BuildPreambleError Error)
Definition: PrecompiledPreamble.cpp:822
clang::driver::ToolChain::getSanitizerArgs
SanitizerArgs getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const
Definition: ToolChain.cpp:146
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::ToolChain::RuntimeLibType
RuntimeLibType
Definition: ToolChain.h:100
InputInfo.h
clang::driver::toolchains::MinGW::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: MinGW.cpp:594
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
clang::driver::toolchains::Generic_GCC::GCCVersion
Struct to store and manipulate GCC versions.
Definition: Gnu.h:161
findGcc
static llvm::ErrorOr< std::string > findGcc(const llvm::Triple &T)
Definition: MinGW.cpp:394
clang::driver::Action::PreprocessJobClass
@ PreprocessJobClass
Definition: Action.h:59
clang::driver::toolchains::MinGW::isPICDefaultForced
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
Definition: MinGW.cpp:520
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:274
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:142
clang::driver::tools::addFortranRuntimeLibs
void addFortranRuntimeLibs(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
Adds Fortran runtime libraries to CmdArgs.
Definition: CommonArgs.cpp:806
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::tools::claimNoWarnArgs
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
Options.h
clang::XRayInstrKind::None
constexpr XRayInstrMask None
Definition: XRayInstr.h:38
clang::interp::LE
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:504
clang::driver::toolchains::MinGW::buildLinker
Tool * buildLinker() const override
Definition: MinGW.cpp:488
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
clang::driver::ToolChain::RLT_Libgcc
@ RLT_Libgcc
Definition: ToolChain.h:102
findClangRelativeSysroot
static llvm::ErrorOr< std::string > findClangRelativeSysroot(const Driver &D, const llvm::Triple &T, std::string &SubdirName)
Definition: MinGW.cpp:407
clang::driver::toolchains::MinGW::fixTripleArch
static void fixTripleArch(const Driver &D, llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: MinGW.cpp:742
clang::driver::toolchains::MinGW::getTool
Tool * getTool(Action::ActionClass AC) const override
Definition: MinGW.cpp:469
llvm::SmallString
Definition: LLVM.h:37
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:234
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:181
clang::driver::SanitizerArgs::needsAsanRt
bool needsAsanRt() const
Definition: SanitizerArgs.h:78
clang::driver::ToolChain::CST_Libcxx
@ CST_Libcxx
Definition: ToolChain.h:96
Base
clang::driver::SanitizerArgs
Definition: SanitizerArgs.h:24
SanitizerArgs.h
clang::driver::ToolChain::CST_Libstdcxx
@ CST_Libstdcxx
Definition: ToolChain.h:97
clang::driver::toolchains::MinGW::isPIEDefault
bool isPIEDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain defaults to PIE.
Definition: MinGW.cpp:516
clang::driver::ToolChain::getArch
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:248
clang::driver::ToolChain::AddFilePathLibArgs
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:1039
clang::driver::ToolChain::ShouldLinkCXXStdlib
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1014
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
MinGW.h
clang::driver::ToolChain::getSupportedSanitizers
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1082
clang::driver::ToolChain::getTool
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:374
clang::driver::ToolChain::AddCXXStdlibLibArgs
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:1020
clang::driver::toolchains::MinGW::AddHIPIncludeArgs
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific HIP includes.
Definition: MinGW.cpp:544
Compilation.h
clang::driver::ToolChain::addProfileRTLibs
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:823
clang::driver::ToolChain::getCompilerRTArgString
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:537
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::ToolChain::GetLinkerPath
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:618
clang::Language::CXX
@ CXX
clang::driver::tools::AddRunTimeLibs
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
clang::driver::ToolChain::getLibraryPaths
path_list & getLibraryPaths()
Definition: ToolChain.h:271
clang::driver::toolchains::MinGW::isPICDefault
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
Definition: MinGW.cpp:511
clang::driver::tools::gcc::Preprocessor
Definition: Gnu.h:103
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::Action::CompileJobClass
@ CompileJobClass
Definition: Action.h:64
adjustTriple
static llvm::Triple adjustTriple(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
Definition: MinGW.cpp:715
testTriple
static bool testTriple(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
Definition: MinGW.cpp:698
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::toolchains::MinGW::AddCudaIncludeArgs
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific CUDA includes.
Definition: MinGW.cpp:539
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:75
clang::driver::toolchains::MinGW::HasNativeLLVMSupport
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: MinGW.cpp:492
clang::driver::tools::gcc::Compiler
Definition: Gnu.h:115
clang::driver::tools::MinGW::Assembler
Definition: MinGW.h:25
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:86
clang::driver
Definition: Action.h:31
clang::driver::toolchains::MinGW::AddClangCXXStdlibIncludeArgs
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: MinGW.cpp:645
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:129
clang::driver::JobAction
Definition: Action.h:398
clang::driver::tools::MinGW::Linker
Definition: MinGW.h:37
clang::driver::tools::addFortranRuntimeLibraryPath
void addFortranRuntimeLibraryPath(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Adds the path for the Fortran runtime libraries to CmdArgs.
clang::driver::toolchains::MinGW::addClangTargetOptions
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: MinGW.cpp:625
clang::driver::tools::SplitDebugName
const char * SplitDebugName(const JobAction &JA, const llvm::opt::ArgList &Args, const InputInfo &Input, const InputInfo &Output)
clang::driver::toolchains::MinGW::getDefaultUnwindTableLevel
UnwindTableLevel getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override
How detailed should the unwind tables be by default.
Definition: MinGW.cpp:497
clang::SanitizerMask
Definition: Sanitizers.h:30
clang::driver::Driver::getInstalledDir
const char * getInstalledDir() const
Get the path to where the clang executable was installed.
Definition: Driver.h:391