clang API Documentation

ToolChain.h
Go to the documentation of this file.
00001 //===--- ToolChain.h - Collections of tools for one platform ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_DRIVER_TOOLCHAIN_H
00011 #define LLVM_CLANG_DRIVER_TOOLCHAIN_H
00012 
00013 #include "clang/Driver/Action.h"
00014 #include "clang/Driver/Multilib.h"
00015 #include "clang/Driver/Types.h"
00016 #include "clang/Driver/Util.h"
00017 #include "llvm/ADT/SmallVector.h"
00018 #include "llvm/ADT/Triple.h"
00019 #include "llvm/Support/Path.h"
00020 #include <memory>
00021 #include <string>
00022 
00023 namespace llvm {
00024 namespace opt {
00025   class ArgList;
00026   class DerivedArgList;
00027   class InputArgList;
00028 }
00029 }
00030 
00031 namespace clang {
00032   class ObjCRuntime;
00033 
00034 namespace driver {
00035   class Compilation;
00036   class Driver;
00037   class JobAction;
00038   class SanitizerArgs;
00039   class Tool;
00040 
00041 /// ToolChain - Access to tools for a single platform.
00042 class ToolChain {
00043 public:
00044   typedef SmallVector<std::string, 16> path_list;
00045 
00046   enum CXXStdlibType {
00047     CST_Libcxx,
00048     CST_Libstdcxx
00049   };
00050 
00051   enum RuntimeLibType {
00052     RLT_CompilerRT,
00053     RLT_Libgcc
00054   };
00055 
00056 private:
00057   const Driver &D;
00058   const llvm::Triple Triple;
00059   const llvm::opt::ArgList &Args;
00060 
00061   /// The list of toolchain specific path prefixes to search for
00062   /// files.
00063   path_list FilePaths;
00064 
00065   /// The list of toolchain specific path prefixes to search for
00066   /// programs.
00067   path_list ProgramPaths;
00068 
00069   mutable std::unique_ptr<Tool> Clang;
00070   mutable std::unique_ptr<Tool> Assemble;
00071   mutable std::unique_ptr<Tool> Link;
00072   Tool *getClang() const;
00073   Tool *getAssemble() const;
00074   Tool *getLink() const;
00075   Tool *getClangAs() const;
00076 
00077   mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
00078 
00079 protected:
00080   MultilibSet Multilibs;
00081 
00082   ToolChain(const Driver &D, const llvm::Triple &T,
00083             const llvm::opt::ArgList &Args);
00084 
00085   virtual Tool *buildAssembler() const;
00086   virtual Tool *buildLinker() const;
00087   virtual Tool *getTool(Action::ActionClass AC) const;
00088 
00089   /// \name Utilities for implementing subclasses.
00090   ///@{
00091   static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
00092                                llvm::opt::ArgStringList &CC1Args,
00093                                const Twine &Path);
00094   static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
00095                                       llvm::opt::ArgStringList &CC1Args,
00096                                       const Twine &Path);
00097   static void
00098       addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
00099                                       llvm::opt::ArgStringList &CC1Args,
00100                                       const Twine &Path);
00101   static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
00102                                 llvm::opt::ArgStringList &CC1Args,
00103                                 ArrayRef<StringRef> Paths);
00104   ///@}
00105 
00106 public:
00107   virtual ~ToolChain();
00108 
00109   // Accessors
00110 
00111   const Driver &getDriver() const;
00112   const llvm::Triple &getTriple() const { return Triple; }
00113 
00114   llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
00115   StringRef getArchName() const { return Triple.getArchName(); }
00116   StringRef getPlatform() const { return Triple.getVendorName(); }
00117   StringRef getOS() const { return Triple.getOSName(); }
00118 
00119   /// \brief Provide the default architecture name (as expected by -arch) for
00120   /// this toolchain. Note t
00121   StringRef getDefaultUniversalArchName() const;
00122 
00123   std::string getTripleString() const {
00124     return Triple.getTriple();
00125   }
00126 
00127   path_list &getFilePaths() { return FilePaths; }
00128   const path_list &getFilePaths() const { return FilePaths; }
00129 
00130   path_list &getProgramPaths() { return ProgramPaths; }
00131   const path_list &getProgramPaths() const { return ProgramPaths; }
00132 
00133   const MultilibSet &getMultilibs() const { return Multilibs; }
00134 
00135   const SanitizerArgs& getSanitizerArgs() const;
00136 
00137   // Tool access.
00138 
00139   /// TranslateArgs - Create a new derived argument list for any argument
00140   /// translations this ToolChain may wish to perform, or 0 if no tool chain
00141   /// specific translations are needed.
00142   ///
00143   /// \param BoundArch - The bound architecture name, or 0.
00144   virtual llvm::opt::DerivedArgList *
00145   TranslateArgs(const llvm::opt::DerivedArgList &Args,
00146                 const char *BoundArch) const {
00147     return nullptr;
00148   }
00149 
00150   /// Choose a tool to use to handle the action \p JA.
00151   Tool *SelectTool(const JobAction &JA) const;
00152 
00153   // Helper methods
00154 
00155   std::string GetFilePath(const char *Name) const;
00156   std::string GetProgramPath(const char *Name) const;
00157 
00158   /// Returns the linker path, respecting the -fuse-ld= argument to determine
00159   /// the linker suffix or name.
00160   std::string GetLinkerPath() const;
00161 
00162   /// \brief Dispatch to the specific toolchain for verbose printing.
00163   ///
00164   /// This is used when handling the verbose option to print detailed,
00165   /// toolchain-specific information useful for understanding the behavior of
00166   /// the driver on a specific platform.
00167   virtual void printVerboseInfo(raw_ostream &OS) const {};
00168 
00169   // Platform defaults information
00170 
00171   /// \brief Returns true if the toolchain is targeting a non-native
00172   /// architecture.
00173   virtual bool isCrossCompiling() const;
00174 
00175   /// HasNativeLTOLinker - Check whether the linker and related tools have
00176   /// native LLVM support.
00177   virtual bool HasNativeLLVMSupport() const;
00178 
00179   /// LookupTypeForExtension - Return the default language type to use for the
00180   /// given extension.
00181   virtual types::ID LookupTypeForExtension(const char *Ext) const;
00182 
00183   /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
00184   virtual bool IsBlocksDefault() const { return false; }
00185 
00186   /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
00187   /// by default.
00188   virtual bool IsIntegratedAssemblerDefault() const { return false; }
00189 
00190   /// \brief Check if the toolchain should use the integrated assembler.
00191   bool useIntegratedAs() const;
00192 
00193   /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
00194   virtual bool IsMathErrnoDefault() const { return true; }
00195 
00196   /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
00197   /// -fencode-extended-block-signature by default.
00198   virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
00199 
00200   /// IsObjCNonFragileABIDefault - Does this tool chain set
00201   /// -fobjc-nonfragile-abi by default.
00202   virtual bool IsObjCNonFragileABIDefault() const { return false; }
00203 
00204   /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
00205   /// mixed dispatch method be used?
00206   virtual bool UseObjCMixedDispatch() const { return false; }
00207 
00208   /// GetDefaultStackProtectorLevel - Get the default stack protector level for
00209   /// this tool chain (0=off, 1=on, 2=strong, 3=all).
00210   virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
00211     return 0;
00212   }
00213 
00214   /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
00215   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
00216     return ToolChain::RLT_Libgcc;
00217   }
00218 
00219   /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
00220   /// by default.
00221   virtual bool IsUnwindTablesDefault() const;
00222 
00223   /// \brief Test whether this toolchain defaults to PIC.
00224   virtual bool isPICDefault() const = 0;
00225 
00226   /// \brief Test whether this toolchain defaults to PIE.
00227   virtual bool isPIEDefault() const = 0;
00228 
00229   /// \brief Tests whether this toolchain forces its default for PIC, PIE or
00230   /// non-PIC.  If this returns true, any PIC related flags should be ignored
00231   /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
00232   /// used exclusively.
00233   virtual bool isPICDefaultForced() const = 0;
00234 
00235   /// SupportsProfiling - Does this tool chain support -pg.
00236   virtual bool SupportsProfiling() const { return true; }
00237 
00238   /// Does this tool chain support Objective-C garbage collection.
00239   virtual bool SupportsObjCGC() const { return true; }
00240 
00241   /// Complain if this tool chain doesn't support Objective-C ARC.
00242   virtual void CheckObjCARC() const {}
00243 
00244   /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
00245   /// compile unit information.
00246   virtual bool UseDwarfDebugFlags() const { return false; }
00247 
00248   /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
00249   virtual bool UseSjLjExceptions() const { return false; }
00250 
00251   /// getThreadModel() - Which thread model does this target use?
00252   virtual std::string getThreadModel() const { return "posix"; }
00253 
00254   /// supportsThreadModel() - Does this target support a thread model?
00255   virtual bool isThreadModelSupported(const StringRef Model) const;
00256 
00257   /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
00258   /// command line arguments into account.
00259   virtual std::string
00260   ComputeLLVMTriple(const llvm::opt::ArgList &Args,
00261                     types::ID InputType = types::TY_INVALID) const;
00262 
00263   /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
00264   /// target, which may take into account the command line arguments. For
00265   /// example, on Darwin the -mmacosx-version-min= command line argument (which
00266   /// sets the deployment target) determines the version in the triple passed to
00267   /// Clang.
00268   virtual std::string ComputeEffectiveClangTriple(
00269       const llvm::opt::ArgList &Args,
00270       types::ID InputType = types::TY_INVALID) const;
00271 
00272   /// getDefaultObjCRuntime - Return the default Objective-C runtime
00273   /// for this platform.
00274   ///
00275   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
00276   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
00277 
00278   /// hasBlocksRuntime - Given that the user is compiling with
00279   /// -fblocks, does this tool chain guarantee the existence of a
00280   /// blocks runtime?
00281   ///
00282   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
00283   virtual bool hasBlocksRuntime() const { return true; }
00284 
00285   /// \brief Add the clang cc1 arguments for system include paths.
00286   ///
00287   /// This routine is responsible for adding the necessary cc1 arguments to
00288   /// include headers from standard system header directories.
00289   virtual void
00290   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
00291                             llvm::opt::ArgStringList &CC1Args) const;
00292 
00293   /// \brief Add options that need to be passed to cc1 for this target.
00294   virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
00295                                      llvm::opt::ArgStringList &CC1Args) const;
00296 
00297   /// \brief Add warning options that need to be passed to cc1 for this target.
00298   virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
00299 
00300   // GetRuntimeLibType - Determine the runtime library type to use with the
00301   // given compilation arguments.
00302   virtual RuntimeLibType
00303   GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
00304 
00305   // GetCXXStdlibType - Determine the C++ standard library type to use with the
00306   // given compilation arguments.
00307   virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
00308 
00309   /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
00310   /// the include paths to use for the given C++ standard library type.
00311   virtual void
00312   AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
00313                                llvm::opt::ArgStringList &CC1Args) const;
00314 
00315   /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
00316   /// for the given C++ standard library type.
00317   virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
00318                                    llvm::opt::ArgStringList &CmdArgs) const;
00319 
00320   /// AddCCKextLibArgs - Add the system specific linker arguments to use
00321   /// for kernel extensions (Darwin-specific).
00322   virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
00323                                 llvm::opt::ArgStringList &CmdArgs) const;
00324 
00325   /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
00326   /// global flags for unsafe floating point math, add it and return true.
00327   ///
00328   /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
00329   virtual bool
00330   AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args,
00331                                 llvm::opt::ArgStringList &CmdArgs) const;
00332 };
00333 
00334 } // end namespace driver
00335 } // end namespace clang
00336 
00337 #endif