clang  3.7.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 "Tools.h"
12 #include "clang/Driver/Action.h"
13 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Options.h"
17 #include "clang/Driver/ToolChain.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Option/Option.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FileSystem.h"
25 using namespace clang::driver;
26 using namespace clang;
27 using namespace llvm::opt;
28 
29 static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
30  return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
31  options::OPT_fno_rtti, options::OPT_frtti);
32 }
33 
34 static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
35  const llvm::Triple &Triple,
36  const Arg *CachedRTTIArg) {
37  // Explicit rtti/no-rtti args
38  if (CachedRTTIArg) {
39  if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
41  else
43  }
44 
45  // -frtti is default, except for the PS4 CPU.
46  if (!Triple.isPS4CPU())
48 
49  // On the PS4, turning on c++ exceptions turns on rtti.
50  // We're assuming that, if we see -fexceptions, rtti gets turned on.
51  Arg *Exceptions = Args.getLastArgNoClaim(
52  options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
53  options::OPT_fexceptions, options::OPT_fno_exceptions);
54  if (Exceptions &&
55  (Exceptions->getOption().matches(options::OPT_fexceptions) ||
56  Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
58 
60 }
61 
62 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
63  const ArgList &Args)
64  : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
65  CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
66  if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
67  if (!isThreadModelSupported(A->getValue()))
68  D.Diag(diag::err_drv_invalid_thread_model_for_target)
69  << A->getValue() << A->getAsString(Args);
70 }
71 
73 }
74 
75 const Driver &ToolChain::getDriver() const {
76  return D;
77 }
78 
80  return Args.hasFlag(options::OPT_fintegrated_as,
81  options::OPT_fno_integrated_as,
83 }
84 
86  if (!SanitizerArguments.get())
87  SanitizerArguments.reset(new SanitizerArgs(*this, Args));
88  return *SanitizerArguments.get();
89 }
90 
92  // In universal driver terms, the arch name accepted by -arch isn't exactly
93  // the same as the ones that appear in the triple. Roughly speaking, this is
94  // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
95  // only interesting special case is powerpc.
96  switch (Triple.getArch()) {
97  case llvm::Triple::ppc:
98  return "ppc";
99  case llvm::Triple::ppc64:
100  return "ppc64";
101  case llvm::Triple::ppc64le:
102  return "ppc64le";
103  default:
104  return Triple.getArchName();
105  }
106 }
107 
109  return false;
110 }
111 
112 Tool *ToolChain::getClang() const {
113  if (!Clang)
114  Clang.reset(new tools::Clang(*this));
115  return Clang.get();
116 }
117 
119  return new tools::ClangAs(*this);
120 }
121 
123  llvm_unreachable("Linking is not supported by this toolchain");
124 }
125 
126 Tool *ToolChain::getAssemble() const {
127  if (!Assemble)
128  Assemble.reset(buildAssembler());
129  return Assemble.get();
130 }
131 
132 Tool *ToolChain::getClangAs() const {
133  if (!Assemble)
134  Assemble.reset(new tools::ClangAs(*this));
135  return Assemble.get();
136 }
137 
138 Tool *ToolChain::getLink() const {
139  if (!Link)
140  Link.reset(buildLinker());
141  return Link.get();
142 }
143 
145  switch (AC) {
147  return getAssemble();
148 
150  return getLink();
151 
152  case Action::InputClass:
159  llvm_unreachable("Invalid tool kind.");
160 
168  return getClang();
169  }
170 
171  llvm_unreachable("Invalid tool kind.");
172 }
173 
175  if (getDriver().ShouldUseClangCompiler(JA))
176  return getClang();
177  Action::ActionClass AC = JA.getKind();
179  return getClangAs();
180  return getTool(AC);
181 }
182 
183 std::string ToolChain::GetFilePath(const char *Name) const {
184  return D.GetFilePath(Name, *this);
185 
186 }
187 
188 std::string ToolChain::GetProgramPath(const char *Name) const {
189  return D.GetProgramPath(Name, *this);
190 }
191 
192 std::string ToolChain::GetLinkerPath() const {
193  if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
194  StringRef Suffix = A->getValue();
195 
196  // If we're passed -fuse-ld= with no argument, or with the argument ld,
197  // then use whatever the default system linker is.
198  if (Suffix.empty() || Suffix == "ld")
199  return GetProgramPath("ld");
200 
201  llvm::SmallString<8> LinkerName("ld.");
202  LinkerName.append(Suffix);
203 
204  std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
205  if (llvm::sys::fs::exists(LinkerPath))
206  return LinkerPath;
207 
208  getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
209  return "";
210  }
211 
212  return GetProgramPath("ld");
213 }
214 
215 
217  return types::lookupTypeForExtension(Ext);
218 }
219 
221  return false;
222 }
223 
225  llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
226  switch (HostTriple.getArch()) {
227  // The A32/T32/T16 instruction sets are not separate architectures in this
228  // context.
229  case llvm::Triple::arm:
230  case llvm::Triple::armeb:
231  case llvm::Triple::thumb:
232  case llvm::Triple::thumbeb:
233  return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
234  getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
235  default:
236  return HostTriple.getArch() != getArch();
237  }
238 }
239 
241  return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
242  VersionTuple());
243 }
244 
245 bool ToolChain::isThreadModelSupported(const StringRef Model) const {
246  if (Model == "single") {
247  // FIXME: 'single' is only supported on ARM so far.
248  return Triple.getArch() == llvm::Triple::arm ||
249  Triple.getArch() == llvm::Triple::armeb ||
250  Triple.getArch() == llvm::Triple::thumb ||
251  Triple.getArch() == llvm::Triple::thumbeb;
252  } else if (Model == "posix")
253  return true;
254 
255  return false;
256 }
257 
258 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
259  types::ID InputType) const {
260  switch (getTriple().getArch()) {
261  default:
262  return getTripleString();
263 
264  case llvm::Triple::x86_64: {
265  llvm::Triple Triple = getTriple();
266  if (!Triple.isOSBinFormatMachO())
267  return getTripleString();
268 
269  if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
270  // x86_64h goes in the triple. Other -march options just use the
271  // vanilla triple we already have.
272  StringRef MArch = A->getValue();
273  if (MArch == "x86_64h")
274  Triple.setArchName(MArch);
275  }
276  return Triple.getTriple();
277  }
278  case llvm::Triple::aarch64: {
279  llvm::Triple Triple = getTriple();
280  if (!Triple.isOSBinFormatMachO())
281  return getTripleString();
282 
283  // FIXME: older versions of ld64 expect the "arm64" component in the actual
284  // triple string and query it to determine whether an LTO file can be
285  // handled. Remove this when we don't care any more.
286  Triple.setArchName("arm64");
287  return Triple.getTriple();
288  }
289  case llvm::Triple::arm:
290  case llvm::Triple::armeb:
291  case llvm::Triple::thumb:
292  case llvm::Triple::thumbeb: {
293  // FIXME: Factor into subclasses.
294  llvm::Triple Triple = getTriple();
295  bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
296  getTriple().getArch() == llvm::Triple::thumbeb;
297 
298  // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
299  // '-mbig-endian'/'-EB'.
300  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
301  options::OPT_mbig_endian)) {
302  IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
303  }
304 
305  // Thumb2 is the default for V7 on Darwin.
306  //
307  // FIXME: Thumb should just be another -target-feaure, not in the triple.
308  StringRef MCPU, MArch;
309  if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
310  MCPU = A->getValue();
311  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
312  MArch = A->getValue();
313  std::string CPU = Triple.isOSBinFormatMachO()
314  ? tools::arm::getARMCPUForMArch(MArch, Triple)
315  : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
316  StringRef Suffix =
318  tools::arm::getARMArch(MArch, Triple));
319  bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
320  Suffix.startswith("v7em") ||
321  (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
322  // FIXME: this is invalid for WindowsCE
323  if (getTriple().isOSWindows())
324  ThumbDefault = true;
325  std::string ArchName;
326  if (IsBigEndian)
327  ArchName = "armeb";
328  else
329  ArchName = "arm";
330 
331  // Assembly files should start in ARM mode.
332  if (InputType != types::TY_PP_Asm &&
333  Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
334  {
335  if (IsBigEndian)
336  ArchName = "thumbeb";
337  else
338  ArchName = "thumb";
339  }
340  Triple.setArchName(ArchName + Suffix.str());
341 
342  return Triple.getTriple();
343  }
344  }
345 }
346 
347 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
348  types::ID InputType) const {
349  return ComputeLLVMTriple(Args, InputType);
350 }
351 
352 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
353  ArgStringList &CC1Args) const {
354  // Each toolchain should provide the appropriate include flags.
355 }
356 
357 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
358  ArgStringList &CC1Args) const {
359 }
360 
361 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
362 
364  const ArgList &Args) const
365 {
366  if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
367  StringRef Value = A->getValue();
368  if (Value == "compiler-rt")
370  if (Value == "libgcc")
371  return ToolChain::RLT_Libgcc;
372  getDriver().Diag(diag::err_drv_invalid_rtlib_name)
373  << A->getAsString(Args);
374  }
375 
376  return GetDefaultRuntimeLibType();
377 }
378 
380  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
381  StringRef Value = A->getValue();
382  if (Value == "libc++")
383  return ToolChain::CST_Libcxx;
384  if (Value == "libstdc++")
386  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
387  << A->getAsString(Args);
388  }
389 
391 }
392 
393 /// \brief Utility function to add a system include directory to CC1 arguments.
394 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
395  ArgStringList &CC1Args,
396  const Twine &Path) {
397  CC1Args.push_back("-internal-isystem");
398  CC1Args.push_back(DriverArgs.MakeArgString(Path));
399 }
400 
401 /// \brief Utility function to add a system include directory with extern "C"
402 /// semantics to CC1 arguments.
403 ///
404 /// Note that this should be used rarely, and only for directories that
405 /// historically and for legacy reasons are treated as having implicit extern
406 /// "C" semantics. These semantics are *ignored* by and large today, but its
407 /// important to preserve the preprocessor changes resulting from the
408 /// classification.
409 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
410  ArgStringList &CC1Args,
411  const Twine &Path) {
412  CC1Args.push_back("-internal-externc-isystem");
413  CC1Args.push_back(DriverArgs.MakeArgString(Path));
414 }
415 
416 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
417  ArgStringList &CC1Args,
418  const Twine &Path) {
419  if (llvm::sys::fs::exists(Path))
420  addExternCSystemInclude(DriverArgs, CC1Args, Path);
421 }
422 
423 /// \brief Utility function to add a list of system include directories to CC1.
424 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
425  ArgStringList &CC1Args,
426  ArrayRef<StringRef> Paths) {
427  for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
428  I != E; ++I) {
429  CC1Args.push_back("-internal-isystem");
430  CC1Args.push_back(DriverArgs.MakeArgString(*I));
431  }
432 }
433 
434 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
435  ArgStringList &CC1Args) const {
436  // Header search paths should be handled by each of the subclasses.
437  // Historically, they have not been, and instead have been handled inside of
438  // the CC1-layer frontend. As the logic is hoisted out, this generic function
439  // will slowly stop being called.
440  //
441  // While it is being called, replicate a bit of a hack to propagate the
442  // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
443  // header search paths with it. Once all systems are overriding this
444  // function, the CC1 flag and this line can be removed.
445  DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
446 }
447 
448 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
449  ArgStringList &CmdArgs) const {
451 
452  switch (Type) {
454  CmdArgs.push_back("-lc++");
455  break;
456 
458  CmdArgs.push_back("-lstdc++");
459  break;
460  }
461 }
462 
463 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
464  ArgStringList &CmdArgs) const {
465  CmdArgs.push_back("-lcc_kext");
466 }
467 
469  ArgStringList &CmdArgs) const {
470  // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
471  // (to keep the linker options consistent with gcc and clang itself).
472  if (!isOptimizationLevelFast(Args)) {
473  // Check if -ffast-math or -funsafe-math.
474  Arg *A =
475  Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
476  options::OPT_funsafe_math_optimizations,
477  options::OPT_fno_unsafe_math_optimizations);
478 
479  if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
480  A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
481  return false;
482  }
483  // If crtfastmath.o exists add it to the arguments.
484  std::string Path = GetFilePath("crtfastmath.o");
485  if (Path == "crtfastmath.o") // Not found.
486  return false;
487 
488  CmdArgs.push_back(Args.MakeArgString(Path));
489  return true;
490 }
491 
493  // Return sanitizers which don't require runtime support and are not
494  // platform or architecture-dependent.
495  using namespace SanitizerKind;
496  return (Undefined & ~Vptr & ~Function) | CFI | CFICastStrict |
497  UnsignedIntegerOverflow | LocalBounds;
498 }
499 
const char * getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple)
Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
Definition: Tools.cpp:5908
const llvm::Triple & getTriple() const
Definition: ToolChain.h:123
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:409
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:144
virtual Tool * SelectTool(const JobAction &JA) const
Definition: ToolChain.cpp:174
ID lookupTypeForExtension(const char *Ext)
Definition: Types.cpp:143
static void addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Definition: ToolChain.cpp:416
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:188
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:379
virtual bool AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: ToolChain.cpp:468
Defines types useful for describing an Objective-C runtime.
const Driver & getDriver() const
Definition: ToolChain.cpp:75
bool useIntegratedAs() const
Check if the toolchain should use the integrated assembler.
Definition: ToolChain.cpp:79
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:72
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:125
virtual Tool * buildAssembler() const
Definition: ToolChain.cpp:118
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:492
Clang integrated assembler tool.
Definition: Tools.h:114
virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: ToolChain.cpp:463
virtual bool isThreadModelSupported(const StringRef Model) const
isThreadModelSupported() - Does this target support a thread model?
Definition: ToolChain.cpp:245
virtual bool HasNativeLLVMSupport() const
Definition: ToolChain.cpp:220
ActionClass getKind() const
Definition: Action.h:94
std::string getARMTargetCPU(StringRef CPU, StringRef Arch, const llvm::Triple &Triple)
getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Definition: Tools.cpp:5925
virtual bool isCrossCompiling() const
Returns true if the toolchain is targeting a non-native architecture.
Definition: ToolChain.cpp:224
std::string GetFilePath(const char *Name, const ToolChain &TC) const
Definition: Driver.cpp:2059
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: ToolChain.cpp:448
bool isOptimizationLevelFast(const llvm::opt::ArgList &Args)
std::string getTripleString() const
Definition: ToolChain.h:134
ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
Definition: ToolChain.cpp:62
virtual RuntimeLibType GetDefaultRuntimeLibType() const
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: ToolChain.h:235
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:363
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:424
static llvm::opt::Arg * GetRTTIArgument(const ArgList &Args)
Definition: ToolChain.cpp:29
virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const
Add warning options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:361
const char * getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch)
Definition: Tools.cpp:5944
virtual std::string ComputeLLVMTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
Definition: ToolChain.cpp:258
'gnustep' is the modern non-fragile GNUstep runtime.
Definition: ObjCRuntime.h:49
const std::string getARMArch(StringRef Arch, const llvm::Triple &Triple)
Definition: Tools.cpp:5882
std::string GetProgramPath(const char *Name, const ToolChain &TC) const
Definition: Driver.cpp:2107
virtual void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: ToolChain.cpp:434
StringRef getDefaultUniversalArchName() const
Provide the default architecture name (as expected by -arch) for this toolchain. Note t...
Definition: ToolChain.cpp:91
virtual std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
Definition: ToolChain.cpp:347
virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:357
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:394
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:352
uint64_t SanitizerMask
Definition: Sanitizers.h:24
Clang compiler tool.
Definition: Tools.h:44
static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, const llvm::Triple &Triple, const Arg *CachedRTTIArg)
Definition: ToolChain.cpp:34
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:25
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
virtual Tool * buildLinker() const
Definition: ToolChain.cpp:122
virtual bool IsUnwindTablesDefault() const
Definition: ToolChain.cpp:108
virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const
Definition: ToolChain.cpp:240
virtual bool IsIntegratedAssemblerDefault() const
Definition: ToolChain.h:208
std::string GetLinkerPath() const
Definition: ToolChain.cpp:192
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:85
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:183
virtual types::ID LookupTypeForExtension(const char *Ext) const
Definition: ToolChain.cpp:216