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 CLANG_DRIVER_TOOLCHAIN_H_ 11#define CLANG_DRIVER_TOOLCHAIN_H_ 12 13#include "clang/Driver/Action.h" 14#include "clang/Driver/Types.h" 15#include "clang/Driver/Util.h" 16#include "llvm/ADT/OwningPtr.h" 17#include "llvm/ADT/SmallVector.h" 18#include "llvm/ADT/Triple.h" 19#include "llvm/Support/Path.h" 20#include <string> 21 22namespace llvm { 23namespace opt { 24 class ArgList; 25 class DerivedArgList; 26 class InputArgList; 27} 28} 29 30namespace clang { 31 class ObjCRuntime; 32 33namespace driver { 34 class Compilation; 35 class Driver; 36 class JobAction; 37 class SanitizerArgs; 38 class Tool; 39 40/// ToolChain - Access to tools for a single platform. 41class ToolChain { 42public: 43 typedef SmallVector<std::string, 4> path_list; 44 45 enum CXXStdlibType { 46 CST_Libcxx, 47 CST_Libstdcxx 48 }; 49 50 enum RuntimeLibType { 51 RLT_CompilerRT, 52 RLT_Libgcc 53 }; 54 55private: 56 const Driver &D; 57 const llvm::Triple Triple; 58 const llvm::opt::ArgList &Args; 59 60 /// The list of toolchain specific path prefixes to search for 61 /// files. 62 path_list FilePaths; 63 64 /// The list of toolchain specific path prefixes to search for 65 /// programs. 66 path_list ProgramPaths; 67 68 mutable OwningPtr<Tool> Clang; 69 mutable OwningPtr<Tool> Assemble; 70 mutable OwningPtr<Tool> Link; 71 Tool *getClang() const; 72 Tool *getAssemble() const; 73 Tool *getLink() const; 74 Tool *getClangAs() const; 75 76 mutable OwningPtr<SanitizerArgs> SanitizerArguments; 77 78protected: 79 ToolChain(const Driver &D, const llvm::Triple &T, 80 const llvm::opt::ArgList &Args); 81 82 virtual Tool *buildAssembler() const; 83 virtual Tool *buildLinker() const; 84 virtual Tool *getTool(Action::ActionClass AC) const; 85 86 /// \name Utilities for implementing subclasses. 87 ///@{ 88 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, 89 llvm::opt::ArgStringList &CC1Args, 90 const Twine &Path); 91 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, 92 llvm::opt::ArgStringList &CC1Args, 93 const Twine &Path); 94 static void 95 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs, 96 llvm::opt::ArgStringList &CC1Args, 97 const Twine &Path); 98 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs, 99 llvm::opt::ArgStringList &CC1Args, 100 ArrayRef<StringRef> Paths); 101 ///@} 102 103public: 104 virtual ~ToolChain(); 105 106 // Accessors 107 108 const Driver &getDriver() const; 109 const llvm::Triple &getTriple() const { return Triple; } 110 111 llvm::Triple::ArchType getArch() const { return Triple.getArch(); } 112 StringRef getArchName() const { return Triple.getArchName(); } 113 StringRef getPlatform() const { return Triple.getVendorName(); } 114 StringRef getOS() const { return Triple.getOSName(); } 115 116 /// \brief Provide the default architecture name (as expected by -arch) for 117 /// this toolchain. Note t 118 std::string getDefaultUniversalArchName() const; 119 120 std::string getTripleString() const { 121 return Triple.getTriple(); 122 } 123 124 path_list &getFilePaths() { return FilePaths; } 125 const path_list &getFilePaths() const { return FilePaths; } 126 127 path_list &getProgramPaths() { return ProgramPaths; } 128 const path_list &getProgramPaths() const { return ProgramPaths; } 129 130 const SanitizerArgs& getSanitizerArgs() const; 131 132 // Tool access. 133 134 /// TranslateArgs - Create a new derived argument list for any argument 135 /// translations this ToolChain may wish to perform, or 0 if no tool chain 136 /// specific translations are needed. 137 /// 138 /// \param BoundArch - The bound architecture name, or 0. 139 virtual llvm::opt::DerivedArgList * 140 TranslateArgs(const llvm::opt::DerivedArgList &Args, 141 const char *BoundArch) const { 142 return 0; 143 } 144 145 /// Choose a tool to use to handle the action \p JA. 146 Tool *SelectTool(const JobAction &JA) const; 147 148 // Helper methods 149 150 std::string GetFilePath(const char *Name) const; 151 std::string GetProgramPath(const char *Name) const; 152 153 /// Returns the linker path, respecting the -fuse-ld= argument to determine 154 /// the linker suffix or name. 155 std::string GetLinkerPath() const; 156 157 /// \brief Dispatch to the specific toolchain for verbose printing. 158 /// 159 /// This is used when handling the verbose option to print detailed, 160 /// toolchain-specific information useful for understanding the behavior of 161 /// the driver on a specific platform. 162 virtual void printVerboseInfo(raw_ostream &OS) const {}; 163 164 // Platform defaults information 165 166 /// HasNativeLTOLinker - Check whether the linker and related tools have 167 /// native LLVM support. 168 virtual bool HasNativeLLVMSupport() const; 169 170 /// LookupTypeForExtension - Return the default language type to use for the 171 /// given extension. 172 virtual types::ID LookupTypeForExtension(const char *Ext) const; 173 174 /// IsBlocksDefault - Does this tool chain enable -fblocks by default. 175 virtual bool IsBlocksDefault() const { return false; } 176 177 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as 178 /// by default. 179 virtual bool IsIntegratedAssemblerDefault() const { return false; } 180 181 /// \brief Check if the toolchain should use the integrated assembler. 182 bool useIntegratedAs() const; 183 184 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default. 185 virtual bool IsMathErrnoDefault() const { return true; } 186 187 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable 188 /// -fencode-extended-block-signature by default. 189 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; } 190 191 /// IsObjCNonFragileABIDefault - Does this tool chain set 192 /// -fobjc-nonfragile-abi by default. 193 virtual bool IsObjCNonFragileABIDefault() const { return false; } 194 195 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the 196 /// mixed dispatch method be used? 197 virtual bool UseObjCMixedDispatch() const { return false; } 198 199 /// GetDefaultStackProtectorLevel - Get the default stack protector level for 200 /// this tool chain (0=off, 1=on, 2=all). 201 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const { 202 return 0; 203 } 204 205 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use. 206 virtual RuntimeLibType GetDefaultRuntimeLibType() const { 207 return ToolChain::RLT_Libgcc; 208 } 209 210 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables 211 /// by default. 212 virtual bool IsUnwindTablesDefault() const; 213 214 /// \brief Test whether this toolchain defaults to PIC. 215 virtual bool isPICDefault() const = 0; 216 217 /// \brief Test whether this toolchain defaults to PIE. 218 virtual bool isPIEDefault() const = 0; 219 220 /// \brief Tests whether this toolchain forces its default for PIC, PIE or 221 /// non-PIC. If this returns true, any PIC related flags should be ignored 222 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are 223 /// used exclusively. 224 virtual bool isPICDefaultForced() const = 0; 225 226 /// SupportsProfiling - Does this tool chain support -pg. 227 virtual bool SupportsProfiling() const { return true; } 228 229 /// Does this tool chain support Objective-C garbage collection. 230 virtual bool SupportsObjCGC() const { return true; } 231 232 /// Complain if this tool chain doesn't support Objective-C ARC. 233 virtual void CheckObjCARC() const {} 234 235 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf 236 /// compile unit information. 237 virtual bool UseDwarfDebugFlags() const { return false; } 238 239 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions. 240 virtual bool UseSjLjExceptions() const { return false; } 241 242 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking 243 /// command line arguments into account. 244 virtual std::string 245 ComputeLLVMTriple(const llvm::opt::ArgList &Args, 246 types::ID InputType = types::TY_INVALID) const; 247 248 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this 249 /// target, which may take into account the command line arguments. For 250 /// example, on Darwin the -mmacosx-version-min= command line argument (which 251 /// sets the deployment target) determines the version in the triple passed to 252 /// Clang. 253 virtual std::string ComputeEffectiveClangTriple( 254 const llvm::opt::ArgList &Args, 255 types::ID InputType = types::TY_INVALID) const; 256 257 /// getDefaultObjCRuntime - Return the default Objective-C runtime 258 /// for this platform. 259 /// 260 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction 261 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const; 262 263 /// hasBlocksRuntime - Given that the user is compiling with 264 /// -fblocks, does this tool chain guarantee the existence of a 265 /// blocks runtime? 266 /// 267 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction 268 virtual bool hasBlocksRuntime() const { return true; } 269 270 /// \brief Add the clang cc1 arguments for system include paths. 271 /// 272 /// This routine is responsible for adding the necessary cc1 arguments to 273 /// include headers from standard system header directories. 274 virtual void 275 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, 276 llvm::opt::ArgStringList &CC1Args) const; 277 278 /// \brief Add options that need to be passed to cc1 for this target. 279 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, 280 llvm::opt::ArgStringList &CC1Args) const; 281 282 // GetRuntimeLibType - Determine the runtime library type to use with the 283 // given compilation arguments. 284 virtual RuntimeLibType 285 GetRuntimeLibType(const llvm::opt::ArgList &Args) const; 286 287 // GetCXXStdlibType - Determine the C++ standard library type to use with the 288 // given compilation arguments. 289 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const; 290 291 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set 292 /// the include paths to use for the given C++ standard library type. 293 virtual void 294 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, 295 llvm::opt::ArgStringList &CC1Args) const; 296 297 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use 298 /// for the given C++ standard library type. 299 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 300 llvm::opt::ArgStringList &CmdArgs) const; 301 302 /// AddCCKextLibArgs - Add the system specific linker arguments to use 303 /// for kernel extensions (Darwin-specific). 304 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args, 305 llvm::opt::ArgStringList &CmdArgs) const; 306 307 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets 308 /// global flags for unsafe floating point math, add it and return true. 309 /// 310 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags. 311 virtual bool 312 AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args, 313 llvm::opt::ArgStringList &CmdArgs) const; 314}; 315 316} // end namespace driver 317} // end namespace clang 318 319#endif 320