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