Home | History | Annotate | Download | only in Driver
      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 
     20 namespace clang {
     21 namespace driver {
     22 namespace 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.
     27 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
     28 protected:
     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(const Driver &D, const llvm::Triple &TargetTriple,
     82                             const ArgList &Args);
     83 
     84     /// \brief Check whether we detected a valid GCC install.
     85     bool isValid() const { return IsValid; }
     86 
     87     /// \brief Get the GCC triple for the detected install.
     88     const llvm::Triple &getTriple() const { return GCCTriple; }
     89 
     90     /// \brief Get the detected GCC installation path.
     91     StringRef getInstallPath() const { return GCCInstallPath; }
     92 
     93     /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
     94     StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
     95 
     96     /// \brief Get the detected GCC parent lib path.
     97     StringRef getParentLibPath() const { return GCCParentLibPath; }
     98 
     99     /// \brief Get the detected GCC version string.
    100     const GCCVersion &getVersion() const { return Version; }
    101 
    102   private:
    103     static void CollectLibDirsAndTriples(
    104       const llvm::Triple &TargetTriple,
    105       const llvm::Triple &MultiarchTriple,
    106       SmallVectorImpl<StringRef> &LibDirs,
    107       SmallVectorImpl<StringRef> &TripleAliases,
    108       SmallVectorImpl<StringRef> &MultiarchLibDirs,
    109       SmallVectorImpl<StringRef> &MultiarchTripleAliases);
    110 
    111     void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
    112                                 const ArgList &Args,
    113                                 const std::string &LibDir,
    114                                 StringRef CandidateTriple,
    115                                 bool NeedsMultiarchSuffix = false);
    116   };
    117 
    118   GCCInstallationDetector GCCInstallation;
    119 
    120 public:
    121   Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    122   ~Generic_GCC();
    123 
    124   virtual Tool *constructTool(Action::ActionClass AC) const;
    125 
    126   virtual bool IsUnwindTablesDefault() const;
    127   virtual bool isPICDefault() const;
    128   virtual bool isPICDefaultForced() const;
    129 
    130 protected:
    131   /// \name ToolChain Implementation Helper Functions
    132   /// @{
    133 
    134   /// \brief Check whether the target triple's architecture is 64-bits.
    135   bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
    136 
    137   /// \brief Check whether the target triple's architecture is 32-bits.
    138   bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
    139 
    140   /// @}
    141 };
    142 
    143   /// Darwin - The base Darwin tool chain.
    144 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
    145 public:
    146   /// The host version.
    147   unsigned DarwinVersion[3];
    148 
    149 private:
    150   /// Whether the information on the target has been initialized.
    151   //
    152   // FIXME: This should be eliminated. What we want to do is make this part of
    153   // the "default target for arguments" selection process, once we get out of
    154   // the argument translation business.
    155   mutable bool TargetInitialized;
    156 
    157   /// Whether we are targeting iPhoneOS target.
    158   mutable bool TargetIsIPhoneOS;
    159 
    160   /// Whether we are targeting the iPhoneOS simulator target.
    161   mutable bool TargetIsIPhoneOSSimulator;
    162 
    163   /// The OS version we are targeting.
    164   mutable VersionTuple TargetVersion;
    165 
    166 private:
    167   /// The default macosx-version-min of this tool chain; empty until
    168   /// initialized.
    169   std::string MacosxVersionMin;
    170 
    171   /// The default ios-version-min of this tool chain; empty until
    172   /// initialized.
    173   std::string iOSVersionMin;
    174 
    175 private:
    176   void AddDeploymentTarget(DerivedArgList &Args) const;
    177 
    178 public:
    179   Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    180   ~Darwin();
    181 
    182   std::string ComputeEffectiveClangTriple(const ArgList &Args,
    183                                           types::ID InputType) const;
    184 
    185   /// @name Darwin Specific Toolchain API
    186   /// {
    187 
    188   // FIXME: Eliminate these ...Target functions and derive separate tool chains
    189   // for these targets and put version in constructor.
    190   void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
    191                  unsigned Micro, bool IsIOSSim) const {
    192     assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
    193 
    194     // FIXME: For now, allow reinitialization as long as values don't
    195     // change. This will go away when we move away from argument translation.
    196     if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
    197         TargetIsIPhoneOSSimulator == IsIOSSim &&
    198         TargetVersion == VersionTuple(Major, Minor, Micro))
    199       return;
    200 
    201     assert(!TargetInitialized && "Target already initialized!");
    202     TargetInitialized = true;
    203     TargetIsIPhoneOS = IsIPhoneOS;
    204     TargetIsIPhoneOSSimulator = IsIOSSim;
    205     TargetVersion = VersionTuple(Major, Minor, Micro);
    206   }
    207 
    208   bool isTargetIPhoneOS() const {
    209     assert(TargetInitialized && "Target not initialized!");
    210     return TargetIsIPhoneOS;
    211   }
    212 
    213   bool isTargetIOSSimulator() const {
    214     assert(TargetInitialized && "Target not initialized!");
    215     return TargetIsIPhoneOSSimulator;
    216   }
    217 
    218   bool isTargetMacOS() const {
    219     return !isTargetIOSSimulator() && !isTargetIPhoneOS();
    220   }
    221 
    222   bool isTargetInitialized() const { return TargetInitialized; }
    223 
    224   VersionTuple getTargetVersion() const {
    225     assert(TargetInitialized && "Target not initialized!");
    226     return TargetVersion;
    227   }
    228 
    229   /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
    230   /// invocation. For example, Darwin treats different ARM variations as
    231   /// distinct architectures.
    232   StringRef getDarwinArchName(const ArgList &Args) const;
    233 
    234   bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
    235     assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
    236     return TargetVersion < VersionTuple(V0, V1, V2);
    237   }
    238 
    239   bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
    240     assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
    241     return TargetVersion < VersionTuple(V0, V1, V2);
    242   }
    243 
    244   /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
    245   virtual void AddLinkARCArgs(const ArgList &Args,
    246                               ArgStringList &CmdArgs) const = 0;
    247 
    248   /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
    249   /// runtime library.
    250   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
    251                                      ArgStringList &CmdArgs) const = 0;
    252 
    253   /// }
    254   /// @name ToolChain Implementation
    255   /// {
    256 
    257   virtual types::ID LookupTypeForExtension(const char *Ext) const;
    258 
    259   virtual bool HasNativeLLVMSupport() const;
    260 
    261   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
    262   virtual bool hasBlocksRuntime() const;
    263 
    264   virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
    265                                         const char *BoundArch) const;
    266 
    267   virtual Tool *constructTool(Action::ActionClass AC) const;
    268 
    269   virtual bool IsBlocksDefault() const {
    270     // Always allow blocks on Darwin; users interested in versioning are
    271     // expected to use /usr/include/Blocks.h.
    272     return true;
    273   }
    274   virtual bool IsIntegratedAssemblerDefault() const {
    275 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
    276     return false;
    277 #else
    278     // Default integrated assembler to on for Darwin.
    279     return true;
    280 #endif
    281   }
    282   virtual bool IsStrictAliasingDefault() const {
    283 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
    284     return false;
    285 #else
    286     return ToolChain::IsStrictAliasingDefault();
    287 #endif
    288   }
    289 
    290   virtual bool IsMathErrnoDefault() const {
    291     return false;
    292   }
    293 
    294   virtual bool IsEncodeExtendedBlockSignatureDefault() const {
    295     return true;
    296   }
    297 
    298   virtual bool IsObjCNonFragileABIDefault() const {
    299     // Non-fragile ABI is default for everything but i386.
    300     return getTriple().getArch() != llvm::Triple::x86;
    301   }
    302 
    303   virtual bool UseObjCMixedDispatch() const {
    304     // This is only used with the non-fragile ABI and non-legacy dispatch.
    305 
    306     // Mixed dispatch is used everywhere except OS X before 10.6.
    307     return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
    308   }
    309   virtual bool IsUnwindTablesDefault() const;
    310   virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
    311     // Stack protectors default to on for user code on 10.5,
    312     // and for everything in 10.6 and beyond
    313     return isTargetIPhoneOS() ||
    314       (!isMacosxVersionLT(10, 6) ||
    315          (!isMacosxVersionLT(10, 5) && !KernelOrKext));
    316   }
    317   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
    318     return ToolChain::RLT_CompilerRT;
    319   }
    320   virtual bool isPICDefault() const;
    321   virtual bool isPICDefaultForced() const;
    322 
    323   virtual bool SupportsProfiling() const;
    324 
    325   virtual bool SupportsObjCGC() const;
    326 
    327   virtual void CheckObjCARC() const;
    328 
    329   virtual bool UseDwarfDebugFlags() const;
    330 
    331   virtual bool UseSjLjExceptions() const;
    332 
    333   /// }
    334 };
    335 
    336 /// DarwinClang - The Darwin toolchain used by Clang.
    337 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
    338 public:
    339   DarwinClang(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    340 
    341   /// @name Darwin ToolChain Implementation
    342   /// {
    343 
    344   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
    345                                      ArgStringList &CmdArgs) const;
    346   void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
    347                          const char *DarwinStaticLib,
    348                          bool AlwaysLink = false) const;
    349 
    350   virtual void AddCXXStdlibLibArgs(const ArgList &Args,
    351                                    ArgStringList &CmdArgs) const;
    352 
    353   virtual void AddCCKextLibArgs(const ArgList &Args,
    354                                 ArgStringList &CmdArgs) const;
    355 
    356   virtual void AddLinkARCArgs(const ArgList &Args,
    357                               ArgStringList &CmdArgs) const;
    358   /// }
    359 };
    360 
    361 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
    362 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
    363 public:
    364   Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
    365     : Generic_GCC(D, Triple, Args) {}
    366 
    367   std::string ComputeEffectiveClangTriple(const ArgList &Args,
    368                                           types::ID InputType) const;
    369 
    370   virtual bool isPICDefault() const { return false; }
    371 };
    372 
    373 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
    374   virtual void anchor();
    375 public:
    376   Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
    377     : Generic_GCC(D, Triple, Args) {}
    378 
    379   virtual bool IsIntegratedAssemblerDefault() const {
    380     // Default integrated assembler to on for x86.
    381     return (getTriple().getArch() == llvm::Triple::aarch64 ||
    382             getTriple().getArch() == llvm::Triple::x86 ||
    383             getTriple().getArch() == llvm::Triple::x86_64);
    384   }
    385 };
    386 
    387 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
    388 public:
    389   AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    390 
    391   virtual Tool *constructTool(Action::ActionClass AC) const;
    392 };
    393 
    394 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
    395 public:
    396   Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    397 
    398   virtual Tool *constructTool(Action::ActionClass AC) const;
    399 
    400   virtual bool IsIntegratedAssemblerDefault() const { return true; }
    401 };
    402 
    403 
    404 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
    405 public:
    406   OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    407 
    408   virtual bool IsMathErrnoDefault() const { return false; }
    409   virtual bool IsObjCNonFragileABIDefault() const { return true; }
    410 
    411   virtual Tool *constructTool(Action::ActionClass AC) const;
    412 };
    413 
    414 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
    415 public:
    416   Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    417 
    418   virtual bool IsMathErrnoDefault() const { return false; }
    419   virtual bool IsObjCNonFragileABIDefault() const { return true; }
    420   virtual bool IsObjCLegacyDispatchDefault() const { return false; }
    421 
    422   virtual Tool *constructTool(Action::ActionClass AC) const;
    423 
    424   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
    425                                             ArgStringList &CC1Args) const;
    426   virtual void AddCXXStdlibLibArgs(const ArgList &Args,
    427                                    ArgStringList &CmdArgs) const;
    428   virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
    429      return 1;
    430   }
    431 };
    432 
    433 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
    434 public:
    435   FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    436 
    437   virtual bool IsMathErrnoDefault() const { return false; }
    438   virtual bool IsObjCNonFragileABIDefault() const { return true; }
    439 
    440   virtual Tool *constructTool(Action::ActionClass AC) const;
    441   virtual bool UseSjLjExceptions() const;
    442 };
    443 
    444 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
    445 public:
    446   NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    447 
    448   virtual bool IsMathErrnoDefault() const { return false; }
    449   virtual bool IsObjCNonFragileABIDefault() const { return true; }
    450 
    451   virtual Tool *constructTool(Action::ActionClass AC) const;
    452 };
    453 
    454 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
    455 public:
    456   Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    457 
    458   virtual Tool *constructTool(Action::ActionClass AC) const;
    459 };
    460 
    461 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
    462 public:
    463   DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    464 
    465   virtual bool IsMathErrnoDefault() const { return false; }
    466 
    467   virtual Tool *constructTool(Action::ActionClass AC) const;
    468 };
    469 
    470 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
    471 public:
    472   Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    473 
    474   virtual bool HasNativeLLVMSupport() const;
    475 
    476   virtual Tool *constructTool(Action::ActionClass AC) const;
    477 
    478   virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
    479                                          ArgStringList &CC1Args) const;
    480   virtual void addClangTargetOptions(const ArgList &DriverArgs,
    481                                      ArgStringList &CC1Args) const;
    482   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
    483                                             ArgStringList &CC1Args) const;
    484 
    485   std::string Linker;
    486   std::vector<std::string> ExtraOpts;
    487 
    488 private:
    489   static bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
    490                                        Twine TargetArchDir,
    491                                        Twine MultiLibSuffix,
    492                                        const ArgList &DriverArgs,
    493                                        ArgStringList &CC1Args);
    494   static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
    495                                        const ArgList &DriverArgs,
    496                                        ArgStringList &CC1Args);
    497 };
    498 
    499 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public Linux {
    500 protected:
    501   GCCVersion GCCLibAndIncVersion;
    502 
    503 public:
    504   Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
    505              const ArgList &Args);
    506   ~Hexagon_TC();
    507 
    508   virtual Tool *constructTool(Action::ActionClass AC) const;
    509 
    510   virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
    511                                          ArgStringList &CC1Args) const;
    512   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
    513                                             ArgStringList &CC1Args) const;
    514   virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
    515 
    516   StringRef GetGCCLibAndIncVersion() const { return GCCLibAndIncVersion.Text; }
    517 
    518   static std::string GetGnuDir(const std::string &InstalledDir);
    519 
    520   static StringRef GetTargetCPU(const ArgList &Args);
    521 };
    522 
    523 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
    524 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
    525 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
    526 public:
    527   TCEToolChain(const Driver &D, const llvm::Triple& Triple,
    528                const ArgList &Args);
    529   ~TCEToolChain();
    530 
    531   virtual Tool *constructTool(Action::ActionClass AC) const;
    532   bool IsMathErrnoDefault() const;
    533   bool isPICDefault() const;
    534   bool isPICDefaultForced() const;
    535 };
    536 
    537 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
    538 public:
    539   Windows(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
    540 
    541   virtual Tool *constructTool(Action::ActionClass AC) const;
    542 
    543   virtual bool IsIntegratedAssemblerDefault() const;
    544   virtual bool IsUnwindTablesDefault() const;
    545   virtual bool isPICDefault() const;
    546   virtual bool isPICDefaultForced() const;
    547 
    548   virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
    549                                          ArgStringList &CC1Args) const;
    550   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
    551                                             ArgStringList &CC1Args) const;
    552 
    553 };
    554 
    555 } // end namespace toolchains
    556 } // end namespace driver
    557 } // end namespace clang
    558 
    559 #endif
    560