Home | History | Annotate | Download | only in Driver
      1 //===--- ToolChain.h - Collections of tools for one platform ----*- 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 LLVM_CLANG_DRIVER_TOOLCHAIN_H
     11 #define LLVM_CLANG_DRIVER_TOOLCHAIN_H
     12 
     13 #include "clang/Basic/Sanitizers.h"
     14 #include "clang/Driver/Action.h"
     15 #include "clang/Driver/Multilib.h"
     16 #include "clang/Driver/Types.h"
     17 #include "clang/Driver/Util.h"
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/ADT/Triple.h"
     20 #include "llvm/Support/Path.h"
     21 #include "llvm/Target/TargetOptions.h"
     22 #include <memory>
     23 #include <string>
     24 
     25 namespace llvm {
     26 namespace opt {
     27   class ArgList;
     28   class DerivedArgList;
     29   class InputArgList;
     30 }
     31 }
     32 
     33 namespace clang {
     34 class ObjCRuntime;
     35 namespace vfs {
     36 class FileSystem;
     37 }
     38 
     39 namespace driver {
     40   class Compilation;
     41   class Driver;
     42   class JobAction;
     43   class SanitizerArgs;
     44   class Tool;
     45 
     46 /// ToolChain - Access to tools for a single platform.
     47 class ToolChain {
     48 public:
     49   typedef SmallVector<std::string, 16> path_list;
     50 
     51   enum CXXStdlibType {
     52     CST_Libcxx,
     53     CST_Libstdcxx
     54   };
     55 
     56   enum RuntimeLibType {
     57     RLT_CompilerRT,
     58     RLT_Libgcc
     59   };
     60 
     61   enum RTTIMode {
     62     RM_EnabledExplicitly,
     63     RM_EnabledImplicitly,
     64     RM_DisabledExplicitly,
     65     RM_DisabledImplicitly
     66   };
     67 
     68 private:
     69   const Driver &D;
     70   const llvm::Triple Triple;
     71   const llvm::opt::ArgList &Args;
     72   // We need to initialize CachedRTTIArg before CachedRTTIMode
     73   const llvm::opt::Arg *const CachedRTTIArg;
     74   const RTTIMode CachedRTTIMode;
     75 
     76   /// The list of toolchain specific path prefixes to search for
     77   /// files.
     78   path_list FilePaths;
     79 
     80   /// The list of toolchain specific path prefixes to search for
     81   /// programs.
     82   path_list ProgramPaths;
     83 
     84   mutable std::unique_ptr<Tool> Clang;
     85   mutable std::unique_ptr<Tool> Assemble;
     86   mutable std::unique_ptr<Tool> Link;
     87   Tool *getClang() const;
     88   Tool *getAssemble() const;
     89   Tool *getLink() const;
     90   Tool *getClangAs() const;
     91 
     92   mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
     93 
     94 protected:
     95   MultilibSet Multilibs;
     96   const char *DefaultLinker = "ld";
     97 
     98   ToolChain(const Driver &D, const llvm::Triple &T,
     99             const llvm::opt::ArgList &Args);
    100 
    101   virtual Tool *buildAssembler() const;
    102   virtual Tool *buildLinker() const;
    103   virtual Tool *getTool(Action::ActionClass AC) const;
    104 
    105   /// \name Utilities for implementing subclasses.
    106   ///@{
    107   static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
    108                                llvm::opt::ArgStringList &CC1Args,
    109                                const Twine &Path);
    110   static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
    111                                       llvm::opt::ArgStringList &CC1Args,
    112                                       const Twine &Path);
    113   static void
    114       addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
    115                                       llvm::opt::ArgStringList &CC1Args,
    116                                       const Twine &Path);
    117   static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
    118                                 llvm::opt::ArgStringList &CC1Args,
    119                                 ArrayRef<StringRef> Paths);
    120   ///@}
    121 
    122 public:
    123   virtual ~ToolChain();
    124 
    125   // Accessors
    126 
    127   const Driver &getDriver() const { return D; }
    128   vfs::FileSystem &getVFS() const;
    129   const llvm::Triple &getTriple() const { return Triple; }
    130 
    131   llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
    132   StringRef getArchName() const { return Triple.getArchName(); }
    133   StringRef getPlatform() const { return Triple.getVendorName(); }
    134   StringRef getOS() const { return Triple.getOSName(); }
    135 
    136   /// \brief Provide the default architecture name (as expected by -arch) for
    137   /// this toolchain. Note t
    138   StringRef getDefaultUniversalArchName() const;
    139 
    140   std::string getTripleString() const {
    141     return Triple.getTriple();
    142   }
    143 
    144   path_list &getFilePaths() { return FilePaths; }
    145   const path_list &getFilePaths() const { return FilePaths; }
    146 
    147   path_list &getProgramPaths() { return ProgramPaths; }
    148   const path_list &getProgramPaths() const { return ProgramPaths; }
    149 
    150   const MultilibSet &getMultilibs() const { return Multilibs; }
    151 
    152   const SanitizerArgs& getSanitizerArgs() const;
    153 
    154   // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
    155   const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
    156 
    157   // Returns the RTTIMode for the toolchain with the current arguments.
    158   RTTIMode getRTTIMode() const { return CachedRTTIMode; }
    159 
    160   /// \brief Return any implicit target and/or mode flag for an invocation of
    161   /// the compiler driver as `ProgName`.
    162   ///
    163   /// For example, when called with i686-linux-android-g++, the first element
    164   /// of the return value will be set to `"i686-linux-android"` and the second
    165   /// will be set to "--driver-mode=g++"`.
    166   ///
    167   /// \pre `llvm::InitializeAllTargets()` has been called.
    168   /// \param ProgName The name the Clang driver was invoked with (from,
    169   /// e.g., argv[0])
    170   /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
    171   static std::pair<std::string, std::string>
    172   getTargetAndModeFromProgramName(StringRef ProgName);
    173 
    174   // Tool access.
    175 
    176   /// TranslateArgs - Create a new derived argument list for any argument
    177   /// translations this ToolChain may wish to perform, or 0 if no tool chain
    178   /// specific translations are needed.
    179   ///
    180   /// \param BoundArch - The bound architecture name, or 0.
    181   virtual llvm::opt::DerivedArgList *
    182   TranslateArgs(const llvm::opt::DerivedArgList &Args,
    183                 const char *BoundArch) const {
    184     return nullptr;
    185   }
    186 
    187   /// Choose a tool to use to handle the action \p JA.
    188   ///
    189   /// This can be overridden when a particular ToolChain needs to use
    190   /// a compiler other than Clang.
    191   virtual Tool *SelectTool(const JobAction &JA) const;
    192 
    193   // Helper methods
    194 
    195   std::string GetFilePath(const char *Name) const;
    196   std::string GetProgramPath(const char *Name) const;
    197 
    198   /// Returns the linker path, respecting the -fuse-ld= argument to determine
    199   /// the linker suffix or name.
    200   std::string GetLinkerPath() const;
    201 
    202   /// \brief Dispatch to the specific toolchain for verbose printing.
    203   ///
    204   /// This is used when handling the verbose option to print detailed,
    205   /// toolchain-specific information useful for understanding the behavior of
    206   /// the driver on a specific platform.
    207   virtual void printVerboseInfo(raw_ostream &OS) const {}
    208 
    209   // Platform defaults information
    210 
    211   /// \brief Returns true if the toolchain is targeting a non-native
    212   /// architecture.
    213   virtual bool isCrossCompiling() const;
    214 
    215   /// HasNativeLTOLinker - Check whether the linker and related tools have
    216   /// native LLVM support.
    217   virtual bool HasNativeLLVMSupport() const;
    218 
    219   /// LookupTypeForExtension - Return the default language type to use for the
    220   /// given extension.
    221   virtual types::ID LookupTypeForExtension(const char *Ext) const;
    222 
    223   /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
    224   virtual bool IsBlocksDefault() const { return false; }
    225 
    226   /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
    227   /// by default.
    228   virtual bool IsIntegratedAssemblerDefault() const { return false; }
    229 
    230   /// \brief Check if the toolchain should use the integrated assembler.
    231   bool useIntegratedAs() const;
    232 
    233   /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
    234   virtual bool IsMathErrnoDefault() const { return true; }
    235 
    236   /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
    237   /// -fencode-extended-block-signature by default.
    238   virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
    239 
    240   /// IsObjCNonFragileABIDefault - Does this tool chain set
    241   /// -fobjc-nonfragile-abi by default.
    242   virtual bool IsObjCNonFragileABIDefault() const { return false; }
    243 
    244   /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
    245   /// mixed dispatch method be used?
    246   virtual bool UseObjCMixedDispatch() const { return false; }
    247 
    248   /// GetDefaultStackProtectorLevel - Get the default stack protector level for
    249   /// this tool chain (0=off, 1=on, 2=strong, 3=all).
    250   virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
    251     return 0;
    252   }
    253 
    254   /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
    255   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
    256     return ToolChain::RLT_Libgcc;
    257   }
    258 
    259   virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
    260                                     StringRef Component,
    261                                     bool Shared = false) const;
    262 
    263   const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
    264                                      StringRef Component,
    265                                      bool Shared = false) const;
    266   /// needsProfileRT - returns true if instrumentation profile is on.
    267   static bool needsProfileRT(const llvm::opt::ArgList &Args);
    268 
    269   /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
    270   /// by default.
    271   virtual bool IsUnwindTablesDefault() const;
    272 
    273   /// \brief Test whether this toolchain defaults to PIC.
    274   virtual bool isPICDefault() const = 0;
    275 
    276   /// \brief Test whether this toolchain defaults to PIE.
    277   virtual bool isPIEDefault() const = 0;
    278 
    279   /// \brief Tests whether this toolchain forces its default for PIC, PIE or
    280   /// non-PIC.  If this returns true, any PIC related flags should be ignored
    281   /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
    282   /// used exclusively.
    283   virtual bool isPICDefaultForced() const = 0;
    284 
    285   /// SupportsProfiling - Does this tool chain support -pg.
    286   virtual bool SupportsProfiling() const { return true; }
    287 
    288   /// Does this tool chain support Objective-C garbage collection.
    289   virtual bool SupportsObjCGC() const { return true; }
    290 
    291   /// Complain if this tool chain doesn't support Objective-C ARC.
    292   virtual void CheckObjCARC() const {}
    293 
    294   /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
    295   /// compile unit information.
    296   virtual bool UseDwarfDebugFlags() const { return false; }
    297 
    298   // Return the DWARF version to emit, in the absence of arguments
    299   // to the contrary.
    300   virtual unsigned GetDefaultDwarfVersion() const { return 4; }
    301 
    302   // True if the driver should assume "-fstandalone-debug"
    303   // in the absence of an option specifying otherwise,
    304   // provided that debugging was requested in the first place.
    305   // i.e. a value of 'true' does not imply that debugging is wanted.
    306   virtual bool GetDefaultStandaloneDebug() const { return false; }
    307 
    308   // Return the default debugger "tuning."
    309   virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
    310     return llvm::DebuggerKind::GDB;
    311   }
    312 
    313   /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
    314   virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
    315     return false;
    316   }
    317 
    318   /// getThreadModel() - Which thread model does this target use?
    319   virtual std::string getThreadModel() const { return "posix"; }
    320 
    321   /// isThreadModelSupported() - Does this target support a thread model?
    322   virtual bool isThreadModelSupported(const StringRef Model) const;
    323 
    324   /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
    325   /// command line arguments into account.
    326   virtual std::string
    327   ComputeLLVMTriple(const llvm::opt::ArgList &Args,
    328                     types::ID InputType = types::TY_INVALID) const;
    329 
    330   /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
    331   /// target, which may take into account the command line arguments. For
    332   /// example, on Darwin the -mmacosx-version-min= command line argument (which
    333   /// sets the deployment target) determines the version in the triple passed to
    334   /// Clang.
    335   virtual std::string ComputeEffectiveClangTriple(
    336       const llvm::opt::ArgList &Args,
    337       types::ID InputType = types::TY_INVALID) const;
    338 
    339   /// getDefaultObjCRuntime - Return the default Objective-C runtime
    340   /// for this platform.
    341   ///
    342   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
    343   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
    344 
    345   /// hasBlocksRuntime - Given that the user is compiling with
    346   /// -fblocks, does this tool chain guarantee the existence of a
    347   /// blocks runtime?
    348   ///
    349   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
    350   virtual bool hasBlocksRuntime() const { return true; }
    351 
    352   /// \brief Add the clang cc1 arguments for system include paths.
    353   ///
    354   /// This routine is responsible for adding the necessary cc1 arguments to
    355   /// include headers from standard system header directories.
    356   virtual void
    357   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
    358                             llvm::opt::ArgStringList &CC1Args) const;
    359 
    360   /// \brief Add options that need to be passed to cc1 for this target.
    361   virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
    362                                      llvm::opt::ArgStringList &CC1Args) const;
    363 
    364   /// \brief Add warning options that need to be passed to cc1 for this target.
    365   virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
    366 
    367   // GetRuntimeLibType - Determine the runtime library type to use with the
    368   // given compilation arguments.
    369   virtual RuntimeLibType
    370   GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
    371 
    372   // GetCXXStdlibType - Determine the C++ standard library type to use with the
    373   // given compilation arguments.
    374   virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
    375 
    376   /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
    377   /// the include paths to use for the given C++ standard library type.
    378   virtual void
    379   AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
    380                                llvm::opt::ArgStringList &CC1Args) const;
    381 
    382   /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
    383   /// for the given C++ standard library type.
    384   virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
    385                                    llvm::opt::ArgStringList &CmdArgs) const;
    386 
    387   /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
    388   void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
    389                           llvm::opt::ArgStringList &CmdArgs) const;
    390 
    391   /// AddCCKextLibArgs - Add the system specific linker arguments to use
    392   /// for kernel extensions (Darwin-specific).
    393   virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
    394                                 llvm::opt::ArgStringList &CmdArgs) const;
    395 
    396   /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
    397   /// global flags for unsafe floating point math, add it and return true.
    398   ///
    399   /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
    400   virtual bool AddFastMathRuntimeIfAvailable(
    401       const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
    402   /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
    403   /// a suitable profile runtime library to the linker.
    404   virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
    405                                 llvm::opt::ArgStringList &CmdArgs) const;
    406 
    407   /// \brief Add arguments to use system-specific CUDA includes.
    408   virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
    409                                   llvm::opt::ArgStringList &CC1Args) const;
    410 
    411   /// \brief Return sanitizers which are available in this toolchain.
    412   virtual SanitizerMask getSupportedSanitizers() const;
    413 };
    414 
    415 } // end namespace driver
    416 } // end namespace clang
    417 
    418 #endif
    419