clang  5.0.0
ToolChain.cpp
Go to the documentation of this file.
1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===//
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 "clang/Driver/ToolChain.h"
11 #include "ToolChains/CommonArgs.h"
12 #include "ToolChains/Arch/ARM.h"
13 #include "ToolChains/Clang.h"
16 #include "clang/Config/config.h"
17 #include "clang/Driver/Action.h"
18 #include "clang/Driver/Driver.h"
20 #include "clang/Driver/Options.h"
22 #include "clang/Driver/XRayArgs.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/Option/Arg.h"
25 #include "llvm/Option/ArgList.h"
26 #include "llvm/Option/Option.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/TargetParser.h"
31 #include "llvm/Support/TargetRegistry.h"
32 
33 using namespace clang::driver;
34 using namespace clang::driver::tools;
35 using namespace clang;
36 using namespace llvm;
37 using namespace llvm::opt;
38 
39 static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
40  return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
41  options::OPT_fno_rtti, options::OPT_frtti);
42 }
43 
44 static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
45  const llvm::Triple &Triple,
46  const Arg *CachedRTTIArg) {
47  // Explicit rtti/no-rtti args
48  if (CachedRTTIArg) {
49  if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
51  else
53  }
54 
55  // -frtti is default, except for the PS4 CPU.
56  if (!Triple.isPS4CPU())
58 
59  // On the PS4, turning on c++ exceptions turns on rtti.
60  // We're assuming that, if we see -fexceptions, rtti gets turned on.
61  Arg *Exceptions = Args.getLastArgNoClaim(
62  options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
63  options::OPT_fexceptions, options::OPT_fno_exceptions);
64  if (Exceptions &&
65  (Exceptions->getOption().matches(options::OPT_fexceptions) ||
66  Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
68 
70 }
71 
72 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
73  const ArgList &Args)
74  : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
75  CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)),
76  EffectiveTriple() {
77  if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
78  if (!isThreadModelSupported(A->getValue()))
79  D.Diag(diag::err_drv_invalid_thread_model_for_target)
80  << A->getValue() << A->getAsString(Args);
81 
82  std::string CandidateLibPath = getArchSpecificLibPath();
83  if (getVFS().exists(CandidateLibPath))
84  getFilePaths().push_back(CandidateLibPath);
85 }
86 
88 }
89 
91 
93  return Args.hasFlag(options::OPT_fintegrated_as,
94  options::OPT_fno_integrated_as,
96 }
97 
99  if (!SanitizerArguments.get())
100  SanitizerArguments.reset(new SanitizerArgs(*this, Args));
101  return *SanitizerArguments.get();
102 }
103 
105  if (!XRayArguments.get())
106  XRayArguments.reset(new XRayArgs(*this, Args));
107  return *XRayArguments.get();
108 }
109 
110 namespace {
111 struct DriverSuffix {
112  const char *Suffix;
113  const char *ModeFlag;
114 };
115 
116 const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
117  // A list of known driver suffixes. Suffixes are compared against the
118  // program name in order. If there is a match, the frontend type is updated as
119  // necessary by applying the ModeFlag.
120  static const DriverSuffix DriverSuffixes[] = {
121  {"clang", nullptr},
122  {"clang++", "--driver-mode=g++"},
123  {"clang-c++", "--driver-mode=g++"},
124  {"clang-cc", nullptr},
125  {"clang-cpp", "--driver-mode=cpp"},
126  {"clang-g++", "--driver-mode=g++"},
127  {"clang-gcc", nullptr},
128  {"clang-cl", "--driver-mode=cl"},
129  {"cc", nullptr},
130  {"cpp", "--driver-mode=cpp"},
131  {"cl", "--driver-mode=cl"},
132  {"++", "--driver-mode=g++"},
133  };
134 
135  for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
136  if (ProgName.endswith(DriverSuffixes[i].Suffix))
137  return &DriverSuffixes[i];
138  return nullptr;
139 }
140 
141 /// Normalize the program name from argv[0] by stripping the file extension if
142 /// present and lower-casing the string on Windows.
143 std::string normalizeProgramName(llvm::StringRef Argv0) {
144  std::string ProgName = llvm::sys::path::stem(Argv0);
145 #ifdef LLVM_ON_WIN32
146  // Transform to lowercase for case insensitive file systems.
147  std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
148 #endif
149  return ProgName;
150 }
151 
152 const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
153  // Try to infer frontend type and default target from the program name by
154  // comparing it against DriverSuffixes in order.
155 
156  // If there is a match, the function tries to identify a target as prefix.
157  // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
158  // prefix "x86_64-linux". If such a target prefix is found, it may be
159  // added via -target as implicit first argument.
160  const DriverSuffix *DS = FindDriverSuffix(ProgName);
161 
162  if (!DS) {
163  // Try again after stripping any trailing version number:
164  // clang++3.5 -> clang++
165  ProgName = ProgName.rtrim("0123456789.");
166  DS = FindDriverSuffix(ProgName);
167  }
168 
169  if (!DS) {
170  // Try again after stripping trailing -component.
171  // clang++-tot -> clang++
172  ProgName = ProgName.slice(0, ProgName.rfind('-'));
173  DS = FindDriverSuffix(ProgName);
174  }
175  return DS;
176 }
177 } // anonymous namespace
178 
179 std::pair<std::string, std::string>
181  std::string ProgName = normalizeProgramName(PN);
182  const DriverSuffix *DS = parseDriverSuffix(ProgName);
183  if (!DS)
184  return std::make_pair("", "");
185  std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag;
186 
187  std::string::size_type LastComponent =
188  ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix));
189  if (LastComponent == std::string::npos)
190  return std::make_pair("", ModeFlag);
191 
192  // Infer target from the prefix.
193  StringRef Prefix(ProgName);
194  Prefix = Prefix.slice(0, LastComponent);
195  std::string IgnoredError;
196  std::string Target;
197  if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
198  Target = Prefix;
199  }
200  return std::make_pair(Target, ModeFlag);
201 }
202 
204  // In universal driver terms, the arch name accepted by -arch isn't exactly
205  // the same as the ones that appear in the triple. Roughly speaking, this is
206  // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
207  // only interesting special case is powerpc.
208  switch (Triple.getArch()) {
209  case llvm::Triple::ppc:
210  return "ppc";
211  case llvm::Triple::ppc64:
212  return "ppc64";
213  case llvm::Triple::ppc64le:
214  return "ppc64le";
215  default:
216  return Triple.getArchName();
217  }
218 }
219 
220 bool ToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
221  return false;
222 }
223 
224 Tool *ToolChain::getClang() const {
225  if (!Clang)
226  Clang.reset(new tools::Clang(*this));
227  return Clang.get();
228 }
229 
231  return new tools::ClangAs(*this);
232 }
233 
235  llvm_unreachable("Linking is not supported by this toolchain");
236 }
237 
238 Tool *ToolChain::getAssemble() const {
239  if (!Assemble)
240  Assemble.reset(buildAssembler());
241  return Assemble.get();
242 }
243 
244 Tool *ToolChain::getClangAs() const {
245  if (!Assemble)
246  Assemble.reset(new tools::ClangAs(*this));
247  return Assemble.get();
248 }
249 
250 Tool *ToolChain::getLink() const {
251  if (!Link)
252  Link.reset(buildLinker());
253  return Link.get();
254 }
255 
256 Tool *ToolChain::getOffloadBundler() const {
257  if (!OffloadBundler)
258  OffloadBundler.reset(new tools::OffloadBundler(*this));
259  return OffloadBundler.get();
260 }
261 
263  switch (AC) {
265  return getAssemble();
266 
268  return getLink();
269 
270  case Action::InputClass:
276  llvm_unreachable("Invalid tool kind.");
277 
285  return getClang();
286 
289  return getOffloadBundler();
290  }
291 
292  llvm_unreachable("Invalid tool kind.");
293 }
294 
295 static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
296  const ArgList &Args) {
297  const llvm::Triple &Triple = TC.getTriple();
298  bool IsWindows = Triple.isOSWindows();
299 
300  if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86)
301  return "i386";
302 
303  if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
304  return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows)
305  ? "armhf"
306  : "arm";
307 
308  return TC.getArchName();
309 }
310 
311 std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
312  bool Shared) const {
313  const llvm::Triple &TT = getTriple();
314  const char *Env = TT.isAndroid() ? "-android" : "";
315  bool IsITANMSVCWindows =
316  TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
317 
318  StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
319  const char *Prefix = IsITANMSVCWindows ? "" : "lib";
320  const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so")
321  : (IsITANMSVCWindows ? ".lib" : ".a");
322 
323  SmallString<128> Path(getDriver().ResourceDir);
324  StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS();
325  llvm::sys::path::append(Path, "lib", OSLibName);
326  llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
327  Arch + Env + Suffix);
328  return Path.str();
329 }
330 
331 const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
332  StringRef Component,
333  bool Shared) const {
334  return Args.MakeArgString(getCompilerRT(Args, Component, Shared));
335 }
336 
338  SmallString<128> Path(getDriver().ResourceDir);
339  StringRef OSLibName = getTriple().isOSFreeBSD() ? "freebsd" : getOS();
340  llvm::sys::path::append(Path, "lib", OSLibName,
341  llvm::Triple::getArchTypeName(getArch()));
342  return Path.str();
343 }
344 
345 bool ToolChain::needsProfileRT(const ArgList &Args) {
346  if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
347  false) ||
348  Args.hasArg(options::OPT_fprofile_generate) ||
349  Args.hasArg(options::OPT_fprofile_generate_EQ) ||
350  Args.hasArg(options::OPT_fprofile_instr_generate) ||
351  Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
352  Args.hasArg(options::OPT_fcreate_profile) ||
353  Args.hasArg(options::OPT_coverage))
354  return true;
355 
356  return false;
357 }
358 
360  if (getDriver().ShouldUseClangCompiler(JA)) return getClang();
361  Action::ActionClass AC = JA.getKind();
363  return getClangAs();
364  return getTool(AC);
365 }
366 
367 std::string ToolChain::GetFilePath(const char *Name) const {
368  return D.GetFilePath(Name, *this);
369 }
370 
371 std::string ToolChain::GetProgramPath(const char *Name) const {
372  return D.GetProgramPath(Name, *this);
373 }
374 
375 std::string ToolChain::GetLinkerPath() const {
376  const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
377  StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
378 
379  if (llvm::sys::path::is_absolute(UseLinker)) {
380  // If we're passed what looks like an absolute path, don't attempt to
381  // second-guess that.
382  if (llvm::sys::fs::exists(UseLinker))
383  return UseLinker;
384  } else if (UseLinker.empty() || UseLinker == "ld") {
385  // If we're passed -fuse-ld= with no argument, or with the argument ld,
386  // then use whatever the default system linker is.
388  } else {
389  llvm::SmallString<8> LinkerName("ld.");
390  LinkerName.append(UseLinker);
391 
392  std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
393  if (llvm::sys::fs::exists(LinkerPath))
394  return LinkerPath;
395  }
396 
397  if (A)
398  getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
399 
401 }
402 
404  return types::lookupTypeForExtension(Ext);
405 }
406 
408  return false;
409 }
410 
412  llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
413  switch (HostTriple.getArch()) {
414  // The A32/T32/T16 instruction sets are not separate architectures in this
415  // context.
416  case llvm::Triple::arm:
417  case llvm::Triple::armeb:
418  case llvm::Triple::thumb:
419  case llvm::Triple::thumbeb:
420  return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
421  getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
422  default:
423  return HostTriple.getArch() != getArch();
424  }
425 }
426 
428  return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
429  VersionTuple());
430 }
431 
432 bool ToolChain::isThreadModelSupported(const StringRef Model) const {
433  if (Model == "single") {
434  // FIXME: 'single' is only supported on ARM and WebAssembly so far.
435  return Triple.getArch() == llvm::Triple::arm ||
436  Triple.getArch() == llvm::Triple::armeb ||
437  Triple.getArch() == llvm::Triple::thumb ||
438  Triple.getArch() == llvm::Triple::thumbeb ||
439  Triple.getArch() == llvm::Triple::wasm32 ||
440  Triple.getArch() == llvm::Triple::wasm64;
441  } else if (Model == "posix")
442  return true;
443 
444  return false;
445 }
446 
447 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
448  types::ID InputType) const {
449  switch (getTriple().getArch()) {
450  default:
451  return getTripleString();
452 
453  case llvm::Triple::x86_64: {
454  llvm::Triple Triple = getTriple();
455  if (!Triple.isOSBinFormatMachO())
456  return getTripleString();
457 
458  if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
459  // x86_64h goes in the triple. Other -march options just use the
460  // vanilla triple we already have.
461  StringRef MArch = A->getValue();
462  if (MArch == "x86_64h")
463  Triple.setArchName(MArch);
464  }
465  return Triple.getTriple();
466  }
467  case llvm::Triple::aarch64: {
468  llvm::Triple Triple = getTriple();
469  if (!Triple.isOSBinFormatMachO())
470  return getTripleString();
471 
472  // FIXME: older versions of ld64 expect the "arm64" component in the actual
473  // triple string and query it to determine whether an LTO file can be
474  // handled. Remove this when we don't care any more.
475  Triple.setArchName("arm64");
476  return Triple.getTriple();
477  }
478  case llvm::Triple::arm:
479  case llvm::Triple::armeb:
480  case llvm::Triple::thumb:
481  case llvm::Triple::thumbeb: {
482  // FIXME: Factor into subclasses.
483  llvm::Triple Triple = getTriple();
484  bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
485  getTriple().getArch() == llvm::Triple::thumbeb;
486 
487  // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
488  // '-mbig-endian'/'-EB'.
489  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
490  options::OPT_mbig_endian)) {
491  IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
492  }
493 
494  // Thumb2 is the default for V7 on Darwin.
495  //
496  // FIXME: Thumb should just be another -target-feaure, not in the triple.
497  StringRef MCPU, MArch;
498  if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
499  MCPU = A->getValue();
500  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
501  MArch = A->getValue();
502  std::string CPU =
503  Triple.isOSBinFormatMachO()
504  ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
505  : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
506  StringRef Suffix =
507  tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
508  bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
509  bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
510  getTriple().isOSBinFormatMachO());
511  // FIXME: this is invalid for WindowsCE
512  if (getTriple().isOSWindows())
513  ThumbDefault = true;
514  std::string ArchName;
515  if (IsBigEndian)
516  ArchName = "armeb";
517  else
518  ArchName = "arm";
519 
520  // Assembly files should start in ARM mode, unless arch is M-profile.
521  // Windows is always thumb.
522  if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
523  options::OPT_mno_thumb, ThumbDefault)) || IsMProfile ||
524  getTriple().isOSWindows()) {
525  if (IsBigEndian)
526  ArchName = "thumbeb";
527  else
528  ArchName = "thumb";
529  }
530  Triple.setArchName(ArchName + Suffix.str());
531 
532  return Triple.getTriple();
533  }
534  }
535 }
536 
537 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
538  types::ID InputType) const {
539  return ComputeLLVMTriple(Args, InputType);
540 }
541 
542 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
543  ArgStringList &CC1Args) const {
544  // Each toolchain should provide the appropriate include flags.
545 }
546 
548  const ArgList &DriverArgs, ArgStringList &CC1Args,
549  Action::OffloadKind DeviceOffloadKind) const {}
550 
551 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
552 
553 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
554  llvm::opt::ArgStringList &CmdArgs) const {
555  if (!needsProfileRT(Args)) return;
556 
557  CmdArgs.push_back(getCompilerRTArgString(Args, "profile"));
558 }
559 
561  const ArgList &Args) const {
562  const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
563  StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB;
564 
565  // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB!
566  if (LibName == "compiler-rt")
568  else if (LibName == "libgcc")
569  return ToolChain::RLT_Libgcc;
570  else if (LibName == "platform")
571  return GetDefaultRuntimeLibType();
572 
573  if (A)
574  getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args);
575 
576  return GetDefaultRuntimeLibType();
577 }
578 
580  const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
581  StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB;
582 
583  // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB!
584  if (LibName == "libc++")
585  return ToolChain::CST_Libcxx;
586  else if (LibName == "libstdc++")
588  else if (LibName == "platform")
589  return GetDefaultCXXStdlibType();
590 
591  if (A)
592  getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
593 
594  return GetDefaultCXXStdlibType();
595 }
596 
597 /// \brief Utility function to add a system include directory to CC1 arguments.
598 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
599  ArgStringList &CC1Args,
600  const Twine &Path) {
601  CC1Args.push_back("-internal-isystem");
602  CC1Args.push_back(DriverArgs.MakeArgString(Path));
603 }
604 
605 /// \brief Utility function to add a system include directory with extern "C"
606 /// semantics to CC1 arguments.
607 ///
608 /// Note that this should be used rarely, and only for directories that
609 /// historically and for legacy reasons are treated as having implicit extern
610 /// "C" semantics. These semantics are *ignored* by and large today, but its
611 /// important to preserve the preprocessor changes resulting from the
612 /// classification.
613 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
614  ArgStringList &CC1Args,
615  const Twine &Path) {
616  CC1Args.push_back("-internal-externc-isystem");
617  CC1Args.push_back(DriverArgs.MakeArgString(Path));
618 }
619 
620 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
621  ArgStringList &CC1Args,
622  const Twine &Path) {
623  if (llvm::sys::fs::exists(Path))
624  addExternCSystemInclude(DriverArgs, CC1Args, Path);
625 }
626 
627 /// \brief Utility function to add a list of system include directories to CC1.
628 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
629  ArgStringList &CC1Args,
630  ArrayRef<StringRef> Paths) {
631  for (StringRef Path : Paths) {
632  CC1Args.push_back("-internal-isystem");
633  CC1Args.push_back(DriverArgs.MakeArgString(Path));
634  }
635 }
636 
637 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
638  ArgStringList &CC1Args) const {
639  // Header search paths should be handled by each of the subclasses.
640  // Historically, they have not been, and instead have been handled inside of
641  // the CC1-layer frontend. As the logic is hoisted out, this generic function
642  // will slowly stop being called.
643  //
644  // While it is being called, replicate a bit of a hack to propagate the
645  // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
646  // header search paths with it. Once all systems are overriding this
647  // function, the CC1 flag and this line can be removed.
648  DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
649 }
650 
651 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
652  ArgStringList &CmdArgs) const {
654 
655  switch (Type) {
657  CmdArgs.push_back("-lc++");
658  break;
659 
661  CmdArgs.push_back("-lstdc++");
662  break;
663  }
664 }
665 
666 void ToolChain::AddFilePathLibArgs(const ArgList &Args,
667  ArgStringList &CmdArgs) const {
668  for (const auto &LibPath : getFilePaths())
669  if(LibPath.length() > 0)
670  CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
671 }
672 
673 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
674  ArgStringList &CmdArgs) const {
675  CmdArgs.push_back("-lcc_kext");
676 }
677 
679  ArgStringList &CmdArgs) const {
680  // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
681  // (to keep the linker options consistent with gcc and clang itself).
682  if (!isOptimizationLevelFast(Args)) {
683  // Check if -ffast-math or -funsafe-math.
684  Arg *A =
685  Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
686  options::OPT_funsafe_math_optimizations,
687  options::OPT_fno_unsafe_math_optimizations);
688 
689  if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
690  A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
691  return false;
692  }
693  // If crtfastmath.o exists add it to the arguments.
694  std::string Path = GetFilePath("crtfastmath.o");
695  if (Path == "crtfastmath.o") // Not found.
696  return false;
697 
698  CmdArgs.push_back(Args.MakeArgString(Path));
699  return true;
700 }
701 
703  // Return sanitizers which don't require runtime support and are not
704  // platform dependent.
705  using namespace SanitizerKind;
706  SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
707  CFICastStrict | UnsignedIntegerOverflow | Nullability |
708  LocalBounds;
709  if (getTriple().getArch() == llvm::Triple::x86 ||
710  getTriple().getArch() == llvm::Triple::x86_64 ||
711  getTriple().getArch() == llvm::Triple::arm ||
712  getTriple().getArch() == llvm::Triple::aarch64 ||
713  getTriple().getArch() == llvm::Triple::wasm32 ||
714  getTriple().getArch() == llvm::Triple::wasm64)
715  Res |= CFIICall;
716  return Res;
717 }
718 
719 void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
720  ArgStringList &CC1Args) const {}
721 
722 void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
723  ArgStringList &CC1Args) const {}
724 
725 static VersionTuple separateMSVCFullVersion(unsigned Version) {
726  if (Version < 100)
727  return VersionTuple(Version);
728 
729  if (Version < 10000)
730  return VersionTuple(Version / 100, Version % 100);
731 
732  unsigned Build = 0, Factor = 1;
733  for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
734  Build = Build + (Version % 10) * Factor;
735  return VersionTuple(Version / 100, Version % 100, Build);
736 }
737 
740  const llvm::opt::ArgList &Args) const {
741  const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
742  const Arg *MSCompatibilityVersion =
743  Args.getLastArg(options::OPT_fms_compatibility_version);
744 
745  if (MSCVersion && MSCompatibilityVersion) {
746  if (D)
747  D->Diag(diag::err_drv_argument_not_allowed_with)
748  << MSCVersion->getAsString(Args)
749  << MSCompatibilityVersion->getAsString(Args);
750  return VersionTuple();
751  }
752 
753  if (MSCompatibilityVersion) {
754  VersionTuple MSVT;
755  if (MSVT.tryParse(MSCompatibilityVersion->getValue())) {
756  if (D)
757  D->Diag(diag::err_drv_invalid_value)
758  << MSCompatibilityVersion->getAsString(Args)
759  << MSCompatibilityVersion->getValue();
760  } else {
761  return MSVT;
762  }
763  }
764 
765  if (MSCVersion) {
766  unsigned Version = 0;
767  if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
768  if (D)
769  D->Diag(diag::err_drv_invalid_value)
770  << MSCVersion->getAsString(Args) << MSCVersion->getValue();
771  } else {
772  return separateMSVCFullVersion(Version);
773  }
774  }
775 
776  return VersionTuple();
777 }
const llvm::Triple & getTriple() const
Definition: ToolChain.h:144
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments...
Definition: ToolChain.cpp:613
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:553
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:262
virtual Tool * SelectTool(const JobAction &JA) const
Choose a tool to use to handle the action JA.
Definition: ToolChain.cpp:359
static void addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Definition: ToolChain.cpp:620
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
std::string GetProgramPath(const char *Name) const
Definition: ToolChain.cpp:371
std::string GetProgramPath(StringRef Name, const ToolChain &TC) const
GetProgramPath - Lookup Name in the list of program search paths.
Definition: Driver.cpp:3721
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:579
virtual 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:678
Defines types useful for describing an Objective-C runtime.
The base class of the type hierarchy.
Definition: Type.h:1303
virtual bool useIntegratedAs() const
Check if the toolchain should use the integrated assembler.
Definition: ToolChain.cpp:92
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
'gcc' is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI ...
Definition: ObjCRuntime.h:50
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:153
static VersionTuple separateMSVCFullVersion(unsigned Version)
Definition: ToolChain.cpp:725
virtual Tool * buildAssembler() const
Definition: ToolChain.cpp:230
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:702
Clang integrated assembler tool.
Definition: Clang.h:109
virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCCKextLibArgs - Add the system specific linker arguments to use for kernel extensions (Darwin-spec...
Definition: ToolChain.cpp:673
virtual bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const
IsUnwindTablesDefault - Does this tool chain use -funwind-tables by default.
Definition: ToolChain.cpp:220
virtual bool isThreadModelSupported(const StringRef Model) const
isThreadModelSupported() - Does this target support a thread model?
Definition: ToolChain.cpp:432
The virtual file system interface.
virtual bool HasNativeLLVMSupport() const
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: ToolChain.cpp:407
static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, const ArgList &Args)
Definition: ToolChain.cpp:295
ActionClass getKind() const
Definition: Action.h:131
virtual bool isCrossCompiling() const
Returns true if the toolchain is targeting a non-native architecture.
Definition: ToolChain.cpp:411
static bool needsProfileRT(const llvm::opt::ArgList &Args)
needsProfileRT - returns true if instrumentation profile is on.
Definition: ToolChain.cpp:345
StringRef getARMCPUForMArch(llvm::StringRef Arch, const llvm::Triple &Triple)
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:651
bool isOptimizationLevelFast(const llvm::opt::ArgList &Args)
std::string getTripleString() const
Definition: ToolChain.h:162
path_list & getFilePaths()
Definition: ToolChain.h:172
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
const Driver & getDriver() const
Definition: ToolChain.h:142
std::string getArchSpecificLibPath() const
Definition: ToolChain.cpp:337
virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const
Add options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:547
ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
Definition: ToolChain.cpp:72
StringRef getArchName() const
Definition: ToolChain.h:154
virtual RuntimeLibType GetDefaultRuntimeLibType() const
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: ToolChain.h:293
virtual const char * getDefaultLinker() const
GetDefaultLinker - Get the default linker to use.
Definition: ToolChain.h:288
virtual VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const
On Windows, returns the MSVC compatibility version.
Definition: ToolChain.cpp:739
virtual std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, bool Shared=false) const
Definition: ToolChain.cpp:311
StringRef getOS() const
Definition: ToolChain.h:156
virtual CXXStdlibType GetDefaultCXXStdlibType() const
Definition: ToolChain.h:297
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:560
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:628
static llvm::opt::Arg * GetRTTIArgument(const ArgList &Args)
Definition: ToolChain.cpp:39
virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add arguments to use MCU GCC toolchain includes.
Definition: ToolChain.cpp:722
virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const
Add warning options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:551
virtual std::string ComputeLLVMTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
ComputeLLVMTriple - Return the LLVM target triple to use, after taking command line arguments into ac...
Definition: ToolChain.cpp:447
static std::pair< std::string, std::string > getTargetAndModeFromProgramName(StringRef ProgName)
Return any implicit target and/or mode flag for an invocation of the compiler driver as ProgName...
Definition: ToolChain.cpp:180
vfs::FileSystem & getVFS() const
Definition: Driver.h:284
'gnustep' is the modern non-fragile GNUstep runtime.
Definition: ObjCRuntime.h:53
virtual void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: ToolChain.cpp:637
Offload bundler tool.
Definition: Clang.h:128
StringRef getDefaultUniversalArchName() const
Provide the default architecture name (as expected by -arch) for this toolchain.
Definition: ToolChain.cpp:203
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:537
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:598
virtual void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add the clang cc1 arguments for system include paths.
Definition: ToolChain.cpp:542
std::string getARMTargetCPU(StringRef CPU, llvm::StringRef Arch, const llvm::Triple &Triple)
uint64_t SanitizerMask
Definition: Sanitizers.h:24
bool tryParse(StringRef string)
Try to parse the given string as a version number.
StringRef Name
Definition: USRFinder.cpp:123
Clang compiler tool.
Definition: Clang.h:29
static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, const llvm::Triple &Triple, const Arg *CachedRTTIArg)
Definition: ToolChain.cpp:44
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:25
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, bool Shared=false) const
Definition: ToolChain.cpp:331
const XRayArgs & getXRayArgs() const
Definition: ToolChain.cpp:104
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Defines the virtual file system interface vfs::FileSystem.
std::string GetFilePath(StringRef Name, const ToolChain &TC) const
GetFilePath - Lookup Name in the list of file search paths.
Definition: Driver.cpp:3668
virtual Tool * buildLinker() const
Definition: ToolChain.cpp:234
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:177
virtual types::ID LookupTypeForExtension(StringRef Ext) const
LookupTypeForExtension - Return the default language type to use for the given extension.
Definition: ToolChain.cpp:403
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:666
vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:90
StringRef getLLVMArchSuffixForARM(llvm::StringRef CPU, llvm::StringRef Arch, const llvm::Triple &Triple)
virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const
getDefaultObjCRuntime - Return the default Objective-C runtime for this platform. ...
Definition: ToolChain.cpp:427
virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add arguments to use system-specific CUDA includes.
Definition: ToolChain.cpp:719
virtual bool IsIntegratedAssemblerDefault() const
IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as by default.
Definition: ToolChain.h:261
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:375
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:98
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:367
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:50