ToolChains.h revision 259157
1//===--- ToolChains.h - ToolChain Implementations ---------------*- C++ -*-===//
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#ifndef CLANG_LIB_DRIVER_TOOLCHAINS_H_
11#define CLANG_LIB_DRIVER_TOOLCHAINS_H_
12
13#include "Tools.h"
14#include "clang/Basic/VersionTuple.h"
15#include "clang/Driver/Action.h"
16#include "clang/Driver/ToolChain.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/Support/Compiler.h"
19
20namespace clang {
21namespace driver {
22namespace toolchains {
23
24/// Generic_GCC - A tool chain using the 'gcc' command to perform
25/// all subcommands; this relies on gcc translating the majority of
26/// command line options.
27class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
28protected:
29  /// \brief Struct to store and manipulate GCC versions.
30  ///
31  /// We rely on assumptions about the form and structure of GCC version
32  /// numbers: they consist of at most three '.'-separated components, and each
33  /// component is a non-negative integer except for the last component. For
34  /// the last component we are very flexible in order to tolerate release
35  /// candidates or 'x' wildcards.
36  ///
37  /// Note that the ordering established among GCCVersions is based on the
38  /// preferred version string to use. For example we prefer versions without
39  /// a hard-coded patch number to those with a hard coded patch number.
40  ///
41  /// Currently this doesn't provide any logic for textual suffixes to patches
42  /// in the way that (for example) Debian's version format does. If that ever
43  /// becomes necessary, it can be added.
44  struct GCCVersion {
45    /// \brief The unparsed text of the version.
46    std::string Text;
47
48    /// \brief The parsed major, minor, and patch numbers.
49    int Major, Minor, Patch;
50
51    /// \brief Any textual suffix on the patch number.
52    std::string PatchSuffix;
53
54    static GCCVersion Parse(StringRef VersionText);
55    bool operator<(const GCCVersion &RHS) const;
56    bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
57    bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
58    bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
59  };
60
61
62  /// \brief This is a class to find a viable GCC installation for Clang to
63  /// use.
64  ///
65  /// This class tries to find a GCC installation on the system, and report
66  /// information about it. It starts from the host information provided to the
67  /// Driver, and has logic for fuzzing that where appropriate.
68  class GCCInstallationDetector {
69
70    bool IsValid;
71    llvm::Triple GCCTriple;
72
73    // FIXME: These might be better as path objects.
74    std::string GCCInstallPath;
75    std::string GCCMultiarchSuffix;
76    std::string GCCParentLibPath;
77
78    GCCVersion Version;
79
80  public:
81    GCCInstallationDetector() : IsValid(false) {}
82    void init(const Driver &D, const llvm::Triple &TargetTriple,
83                            const ArgList &Args);
84
85    /// \brief Check whether we detected a valid GCC install.
86    bool isValid() const { return IsValid; }
87
88    /// \brief Get the GCC triple for the detected install.
89    const llvm::Triple &getTriple() const { return GCCTriple; }
90
91    /// \brief Get the detected GCC installation path.
92    StringRef getInstallPath() const { return GCCInstallPath; }
93
94    /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
95    StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
96
97    /// \brief Get the detected GCC parent lib path.
98    StringRef getParentLibPath() const { return GCCParentLibPath; }
99
100    /// \brief Get the detected GCC version string.
101    const GCCVersion &getVersion() const { return Version; }
102
103  private:
104    static void CollectLibDirsAndTriples(
105      const llvm::Triple &TargetTriple,
106      const llvm::Triple &MultiarchTriple,
107      SmallVectorImpl<StringRef> &LibDirs,
108      SmallVectorImpl<StringRef> &TripleAliases,
109      SmallVectorImpl<StringRef> &MultiarchLibDirs,
110      SmallVectorImpl<StringRef> &MultiarchTripleAliases);
111
112    void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
113                                const ArgList &Args,
114                                const std::string &LibDir,
115                                StringRef CandidateTriple,
116                                bool NeedsMultiarchSuffix = false);
117  };
118
119  GCCInstallationDetector GCCInstallation;
120
121public:
122  Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
123  ~Generic_GCC();
124
125  virtual bool IsUnwindTablesDefault() const;
126  virtual bool isPICDefault() const;
127  virtual bool isPIEDefault() const;
128  virtual bool isPICDefaultForced() const;
129
130protected:
131  virtual Tool *getTool(Action::ActionClass AC) const;
132  virtual Tool *buildAssembler() const;
133  virtual Tool *buildLinker() const;
134
135  /// \name ToolChain Implementation Helper Functions
136  /// @{
137
138  /// \brief Check whether the target triple's architecture is 64-bits.
139  bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
140
141  /// \brief Check whether the target triple's architecture is 32-bits.
142  bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
143
144  /// @}
145
146private:
147  mutable OwningPtr<tools::gcc::Preprocess> Preprocess;
148  mutable OwningPtr<tools::gcc::Precompile> Precompile;
149  mutable OwningPtr<tools::gcc::Compile> Compile;
150};
151
152  /// Darwin - The base Darwin tool chain.
153class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
154public:
155  /// The host version.
156  unsigned DarwinVersion[3];
157
158protected:
159  virtual Tool *buildAssembler() const;
160  virtual Tool *buildLinker() const;
161  virtual Tool *getTool(Action::ActionClass AC) const;
162
163private:
164  mutable OwningPtr<tools::darwin::Lipo> Lipo;
165  mutable OwningPtr<tools::darwin::Dsymutil> Dsymutil;
166  mutable OwningPtr<tools::darwin::VerifyDebug> VerifyDebug;
167
168  /// Whether the information on the target has been initialized.
169  //
170  // FIXME: This should be eliminated. What we want to do is make this part of
171  // the "default target for arguments" selection process, once we get out of
172  // the argument translation business.
173  mutable bool TargetInitialized;
174
175  /// Whether we are targeting iPhoneOS target.
176  mutable bool TargetIsIPhoneOS;
177
178  /// Whether we are targeting the iPhoneOS simulator target.
179  mutable bool TargetIsIPhoneOSSimulator;
180
181  /// The OS version we are targeting.
182  mutable VersionTuple TargetVersion;
183
184private:
185  /// The default macosx-version-min of this tool chain; empty until
186  /// initialized.
187  std::string MacosxVersionMin;
188
189  /// The default ios-version-min of this tool chain; empty until
190  /// initialized.
191  std::string iOSVersionMin;
192
193private:
194  void AddDeploymentTarget(DerivedArgList &Args) const;
195
196public:
197  Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
198  ~Darwin();
199
200  std::string ComputeEffectiveClangTriple(const ArgList &Args,
201                                          types::ID InputType) const;
202
203  /// @name Darwin Specific Toolchain API
204  /// {
205
206  // FIXME: Eliminate these ...Target functions and derive separate tool chains
207  // for these targets and put version in constructor.
208  void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
209                 unsigned Micro, bool IsIOSSim) const {
210    assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
211
212    // FIXME: For now, allow reinitialization as long as values don't
213    // change. This will go away when we move away from argument translation.
214    if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
215        TargetIsIPhoneOSSimulator == IsIOSSim &&
216        TargetVersion == VersionTuple(Major, Minor, Micro))
217      return;
218
219    assert(!TargetInitialized && "Target already initialized!");
220    TargetInitialized = true;
221    TargetIsIPhoneOS = IsIPhoneOS;
222    TargetIsIPhoneOSSimulator = IsIOSSim;
223    TargetVersion = VersionTuple(Major, Minor, Micro);
224  }
225
226  bool isTargetIPhoneOS() const {
227    assert(TargetInitialized && "Target not initialized!");
228    return TargetIsIPhoneOS;
229  }
230
231  bool isTargetIOSSimulator() const {
232    assert(TargetInitialized && "Target not initialized!");
233    return TargetIsIPhoneOSSimulator;
234  }
235
236  bool isTargetMacOS() const {
237    return !isTargetIOSSimulator() && !isTargetIPhoneOS();
238  }
239
240  bool isTargetInitialized() const { return TargetInitialized; }
241
242  VersionTuple getTargetVersion() const {
243    assert(TargetInitialized && "Target not initialized!");
244    return TargetVersion;
245  }
246
247  /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
248  /// invocation. For example, Darwin treats different ARM variations as
249  /// distinct architectures.
250  StringRef getDarwinArchName(const ArgList &Args) const;
251
252  bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
253    assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
254    return TargetVersion < VersionTuple(V0, V1, V2);
255  }
256
257  bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
258    assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
259    return TargetVersion < VersionTuple(V0, V1, V2);
260  }
261
262  /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
263  virtual void AddLinkARCArgs(const ArgList &Args,
264                              ArgStringList &CmdArgs) const = 0;
265
266  /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
267  /// runtime library.
268  virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
269                                     ArgStringList &CmdArgs) const = 0;
270
271  /// }
272  /// @name ToolChain Implementation
273  /// {
274
275  virtual types::ID LookupTypeForExtension(const char *Ext) const;
276
277  virtual bool HasNativeLLVMSupport() const;
278
279  virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
280  virtual bool hasBlocksRuntime() const;
281
282  virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
283                                        const char *BoundArch) const;
284
285  virtual bool IsBlocksDefault() const {
286    // Always allow blocks on Darwin; users interested in versioning are
287    // expected to use /usr/include/Blocks.h.
288    return true;
289  }
290  virtual bool IsIntegratedAssemblerDefault() const {
291#ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
292    return false;
293#else
294    // Default integrated assembler to on for Darwin.
295    return true;
296#endif
297  }
298  virtual bool IsStrictAliasingDefault() const {
299#ifdef DISABLE_DEFAULT_STRICT_ALIASING
300    return false;
301#else
302    return ToolChain::IsStrictAliasingDefault();
303#endif
304  }
305
306  virtual bool IsMathErrnoDefault() const {
307    return false;
308  }
309
310  virtual bool IsEncodeExtendedBlockSignatureDefault() const {
311    return true;
312  }
313
314  virtual bool IsObjCNonFragileABIDefault() const {
315    // Non-fragile ABI is default for everything but i386.
316    return getTriple().getArch() != llvm::Triple::x86;
317  }
318
319  virtual bool UseObjCMixedDispatch() const {
320    // This is only used with the non-fragile ABI and non-legacy dispatch.
321
322    // Mixed dispatch is used everywhere except OS X before 10.6.
323    return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
324  }
325  virtual bool IsUnwindTablesDefault() const;
326  virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
327    // Stack protectors default to on for user code on 10.5,
328    // and for everything in 10.6 and beyond
329    return isTargetIPhoneOS() ||
330      (!isMacosxVersionLT(10, 6) ||
331         (!isMacosxVersionLT(10, 5) && !KernelOrKext));
332  }
333  virtual RuntimeLibType GetDefaultRuntimeLibType() const {
334    return ToolChain::RLT_CompilerRT;
335  }
336  virtual bool isPICDefault() const;
337  virtual bool isPIEDefault() const;
338  virtual bool isPICDefaultForced() const;
339
340  virtual bool SupportsProfiling() const;
341
342  virtual bool SupportsObjCGC() const;
343
344  virtual void CheckObjCARC() const;
345
346  virtual bool UseDwarfDebugFlags() const;
347
348  virtual bool UseSjLjExceptions() const;
349
350  /// }
351};
352
353/// DarwinClang - The Darwin toolchain used by Clang.
354class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
355public:
356  DarwinClang(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
357
358  /// @name Darwin ToolChain Implementation
359  /// {
360
361  virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
362                                     ArgStringList &CmdArgs) const;
363  void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
364                         const char *DarwinStaticLib,
365                         bool AlwaysLink = false) const;
366
367  virtual void AddCXXStdlibLibArgs(const ArgList &Args,
368                                   ArgStringList &CmdArgs) const;
369
370  virtual void AddCCKextLibArgs(const ArgList &Args,
371                                ArgStringList &CmdArgs) const;
372
373  virtual void AddLinkARCArgs(const ArgList &Args,
374                              ArgStringList &CmdArgs) const;
375  /// }
376};
377
378/// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
379class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
380public:
381  Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
382    : Generic_GCC(D, Triple, Args) {}
383
384  std::string ComputeEffectiveClangTriple(const ArgList &Args,
385                                          types::ID InputType) const;
386
387  virtual bool isPICDefault() const { return false; }
388};
389
390class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
391  virtual void anchor();
392public:
393  Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
394    : Generic_GCC(D, Triple, Args) {}
395
396  virtual bool IsIntegratedAssemblerDefault() const {
397    // Default integrated assembler to on for x86.
398    return (getTriple().getArch() == llvm::Triple::aarch64 ||
399            getTriple().getArch() == llvm::Triple::x86 ||
400            getTriple().getArch() == llvm::Triple::x86_64);
401  }
402};
403
404class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
405public:
406  AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
407
408protected:
409  virtual Tool *buildAssembler() const;
410  virtual Tool *buildLinker() const;
411};
412
413class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
414public:
415  Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
416
417  virtual bool IsIntegratedAssemblerDefault() const { return true; }
418protected:
419  virtual Tool *buildAssembler() const;
420  virtual Tool *buildLinker() const;
421
422};
423
424
425class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
426public:
427  OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
428
429  virtual bool IsMathErrnoDefault() const { return false; }
430  virtual bool IsObjCNonFragileABIDefault() const { return true; }
431
432protected:
433  virtual Tool *buildAssembler() const;
434  virtual Tool *buildLinker() const;
435};
436
437class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
438public:
439  Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
440
441  virtual bool IsMathErrnoDefault() const { return false; }
442  virtual bool IsObjCNonFragileABIDefault() const { return true; }
443  virtual bool IsObjCLegacyDispatchDefault() const { return false; }
444
445  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
446                                            ArgStringList &CC1Args) const;
447  virtual void AddCXXStdlibLibArgs(const ArgList &Args,
448                                   ArgStringList &CmdArgs) const;
449  virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
450     return 1;
451  }
452
453protected:
454  virtual Tool *buildAssembler() const;
455  virtual Tool *buildLinker() const;
456};
457
458class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
459public:
460  FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
461
462  virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
463
464  virtual bool IsMathErrnoDefault() const { return false; }
465  virtual bool IsObjCNonFragileABIDefault() const { return true; }
466
467  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
468                                            ArgStringList &CC1Args) const;
469
470  virtual bool UseSjLjExceptions() const;
471protected:
472  virtual Tool *buildAssembler() const;
473  virtual Tool *buildLinker() const;
474};
475
476class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
477public:
478  NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
479
480  virtual bool IsMathErrnoDefault() const { return false; }
481  virtual bool IsObjCNonFragileABIDefault() const { return true; }
482
483  virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
484
485  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
486                                            ArgStringList &CC1Args) const;
487
488protected:
489  virtual Tool *buildAssembler() const;
490  virtual Tool *buildLinker() const;
491};
492
493class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
494public:
495  Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
496
497protected:
498  virtual Tool *buildAssembler() const;
499  virtual Tool *buildLinker() const;
500};
501
502class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
503public:
504  DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
505
506  virtual bool IsMathErrnoDefault() const { return false; }
507
508protected:
509  virtual Tool *buildAssembler() const;
510  virtual Tool *buildLinker() const;
511};
512
513class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
514public:
515  Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
516
517  virtual bool HasNativeLLVMSupport() const;
518
519  virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
520                                         ArgStringList &CC1Args) const;
521  virtual void addClangTargetOptions(const ArgList &DriverArgs,
522                                     ArgStringList &CC1Args) const;
523  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
524                                            ArgStringList &CC1Args) const;
525  virtual bool isPIEDefault() const;
526
527  std::string Linker;
528  std::vector<std::string> ExtraOpts;
529  bool IsPIEDefault;
530
531protected:
532  virtual Tool *buildAssembler() const;
533  virtual Tool *buildLinker() const;
534
535private:
536  static bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
537                                       Twine TargetArchDir,
538                                       Twine MultiLibSuffix,
539                                       const ArgList &DriverArgs,
540                                       ArgStringList &CC1Args);
541  static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
542                                       const ArgList &DriverArgs,
543                                       ArgStringList &CC1Args);
544
545  std::string computeSysRoot(const ArgList &Args) const;
546};
547
548class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public Linux {
549protected:
550  GCCVersion GCCLibAndIncVersion;
551  virtual Tool *buildAssembler() const;
552  virtual Tool *buildLinker() const;
553
554public:
555  Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
556             const ArgList &Args);
557  ~Hexagon_TC();
558
559  virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
560                                         ArgStringList &CC1Args) const;
561  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
562                                            ArgStringList &CC1Args) const;
563  virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
564
565  StringRef GetGCCLibAndIncVersion() const { return GCCLibAndIncVersion.Text; }
566
567  static std::string GetGnuDir(const std::string &InstalledDir);
568
569  static StringRef GetTargetCPU(const ArgList &Args);
570};
571
572/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
573/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
574class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
575public:
576  TCEToolChain(const Driver &D, const llvm::Triple& Triple,
577               const ArgList &Args);
578  ~TCEToolChain();
579
580  bool IsMathErrnoDefault() const;
581  bool isPICDefault() const;
582  bool isPIEDefault() const;
583  bool isPICDefaultForced() const;
584};
585
586class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
587public:
588  Windows(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
589
590  virtual bool IsIntegratedAssemblerDefault() const;
591  virtual bool IsUnwindTablesDefault() const;
592  virtual bool isPICDefault() const;
593  virtual bool isPIEDefault() const;
594  virtual bool isPICDefaultForced() const;
595
596  virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
597                                         ArgStringList &CC1Args) const;
598  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
599                                            ArgStringList &CC1Args) const;
600protected:
601  virtual Tool *buildLinker() const;
602  virtual Tool *buildAssembler() const;
603};
604
605} // end namespace toolchains
606} // end namespace driver
607} // end namespace clang
608
609#endif
610