clang API Documentation
00001 //===--- ToolChains.h - ToolChain Implementations ---------------*- 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 CLANG_LIB_DRIVER_TOOLCHAINS_H_ 00011 #define CLANG_LIB_DRIVER_TOOLCHAINS_H_ 00012 00013 #include "clang/Driver/Action.h" 00014 #include "clang/Driver/ToolChain.h" 00015 00016 #include "clang/Basic/VersionTuple.h" 00017 #include "llvm/ADT/DenseMap.h" 00018 #include "llvm/Support/Compiler.h" 00019 00020 #include "Tools.h" 00021 00022 namespace clang { 00023 namespace driver { 00024 namespace toolchains { 00025 00026 /// Generic_GCC - A tool chain using the 'gcc' command to perform 00027 /// all subcommands; this relies on gcc translating the majority of 00028 /// command line options. 00029 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain { 00030 protected: 00031 /// \brief Struct to store and manipulate GCC versions. 00032 /// 00033 /// We rely on assumptions about the form and structure of GCC version 00034 /// numbers: they consist of at most three '.'-separated components, and each 00035 /// component is a non-negative integer except for the last component. For 00036 /// the last component we are very flexible in order to tolerate release 00037 /// candidates or 'x' wildcards. 00038 /// 00039 /// Note that the ordering established among GCCVersions is based on the 00040 /// preferred version string to use. For example we prefer versions without 00041 /// a hard-coded patch number to those with a hard coded patch number. 00042 /// 00043 /// Currently this doesn't provide any logic for textual suffixes to patches 00044 /// in the way that (for example) Debian's version format does. If that ever 00045 /// becomes necessary, it can be added. 00046 struct GCCVersion { 00047 /// \brief The unparsed text of the version. 00048 std::string Text; 00049 00050 /// \brief The parsed major, minor, and patch numbers. 00051 int Major, Minor, Patch; 00052 00053 /// \brief Any textual suffix on the patch number. 00054 std::string PatchSuffix; 00055 00056 static GCCVersion Parse(StringRef VersionText); 00057 bool operator<(const GCCVersion &RHS) const; 00058 bool operator>(const GCCVersion &RHS) const { return RHS < *this; } 00059 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); } 00060 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); } 00061 }; 00062 00063 00064 /// \brief This is a class to find a viable GCC installation for Clang to 00065 /// use. 00066 /// 00067 /// This class tries to find a GCC installation on the system, and report 00068 /// information about it. It starts from the host information provided to the 00069 /// Driver, and has logic for fuzzing that where appropriate. 00070 class GCCInstallationDetector { 00071 00072 bool IsValid; 00073 llvm::Triple GCCTriple; 00074 00075 // FIXME: These might be better as path objects. 00076 std::string GCCInstallPath; 00077 std::string GCCMultiarchSuffix; 00078 std::string GCCParentLibPath; 00079 00080 GCCVersion Version; 00081 00082 public: 00083 GCCInstallationDetector(const Driver &D, const llvm::Triple &TargetTriple, 00084 const ArgList &Args); 00085 00086 /// \brief Check whether we detected a valid GCC install. 00087 bool isValid() const { return IsValid; } 00088 00089 /// \brief Get the GCC triple for the detected install. 00090 const llvm::Triple &getTriple() const { return GCCTriple; } 00091 00092 /// \brief Get the detected GCC installation path. 00093 StringRef getInstallPath() const { return GCCInstallPath; } 00094 00095 /// \brief Get the detected GCC installation path suffix for multiarch GCCs. 00096 StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; } 00097 00098 /// \brief Get the detected GCC parent lib path. 00099 StringRef getParentLibPath() const { return GCCParentLibPath; } 00100 00101 /// \brief Get the detected GCC version string. 00102 StringRef getVersion() const { return Version.Text; } 00103 00104 private: 00105 static void CollectLibDirsAndTriples( 00106 const llvm::Triple &TargetTriple, 00107 const llvm::Triple &MultiarchTriple, 00108 SmallVectorImpl<StringRef> &LibDirs, 00109 SmallVectorImpl<StringRef> &TripleAliases, 00110 SmallVectorImpl<StringRef> &MultiarchLibDirs, 00111 SmallVectorImpl<StringRef> &MultiarchTripleAliases); 00112 00113 void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch, 00114 const std::string &LibDir, 00115 StringRef CandidateTriple, 00116 bool NeedsMultiarchSuffix = false); 00117 }; 00118 00119 GCCInstallationDetector GCCInstallation; 00120 00121 mutable llvm::DenseMap<unsigned, Tool*> Tools; 00122 00123 public: 00124 Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00125 ~Generic_GCC(); 00126 00127 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00128 const ActionList &Inputs) const; 00129 00130 virtual bool IsUnwindTablesDefault() const; 00131 virtual const char *GetDefaultRelocationModel() const; 00132 virtual const char *GetForcedPicModel() const; 00133 00134 protected: 00135 /// \name ToolChain Implementation Helper Functions 00136 /// @{ 00137 00138 /// \brief Check whether the target triple's architecture is 64-bits. 00139 bool isTarget64Bit() const { return getTriple().isArch64Bit(); } 00140 00141 /// \brief Check whether the target triple's architecture is 32-bits. 00142 bool isTarget32Bit() const { return getTriple().isArch32Bit(); } 00143 00144 /// @} 00145 }; 00146 00147 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public ToolChain { 00148 protected: 00149 mutable llvm::DenseMap<unsigned, Tool*> Tools; 00150 00151 public: 00152 Hexagon_TC(const Driver &D, const llvm::Triple& Triple); 00153 ~Hexagon_TC(); 00154 00155 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00156 const ActionList &Inputs) const; 00157 00158 virtual bool IsUnwindTablesDefault() const; 00159 virtual const char *GetDefaultRelocationModel() const; 00160 virtual const char *GetForcedPicModel() const; 00161 }; 00162 00163 /// Darwin - The base Darwin tool chain. 00164 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain { 00165 public: 00166 /// The host version. 00167 unsigned DarwinVersion[3]; 00168 00169 private: 00170 mutable llvm::DenseMap<unsigned, Tool*> Tools; 00171 00172 /// Whether the information on the target has been initialized. 00173 // 00174 // FIXME: This should be eliminated. What we want to do is make this part of 00175 // the "default target for arguments" selection process, once we get out of 00176 // the argument translation business. 00177 mutable bool TargetInitialized; 00178 00179 // FIXME: Remove this once there is a proper way to detect an ARC runtime 00180 // for the simulator. 00181 public: 00182 mutable enum { 00183 ARCSimulator_None, 00184 ARCSimulator_HasARCRuntime, 00185 ARCSimulator_NoARCRuntime 00186 } ARCRuntimeForSimulator; 00187 00188 mutable enum { 00189 LibCXXSimulator_None, 00190 LibCXXSimulator_NotAvailable, 00191 LibCXXSimulator_Available 00192 } LibCXXForSimulator; 00193 00194 private: 00195 /// Whether we are targeting iPhoneOS target. 00196 mutable bool TargetIsIPhoneOS; 00197 00198 /// Whether we are targeting the iPhoneOS simulator target. 00199 mutable bool TargetIsIPhoneOSSimulator; 00200 00201 /// The OS version we are targeting. 00202 mutable VersionTuple TargetVersion; 00203 00204 /// The default macosx-version-min of this tool chain; empty until 00205 /// initialized. 00206 std::string MacosxVersionMin; 00207 00208 /// The default ios-version-min of this tool chain; empty until 00209 /// initialized. 00210 std::string iOSVersionMin; 00211 00212 bool hasARCRuntime() const; 00213 bool hasSubscriptingRuntime() const; 00214 00215 private: 00216 void AddDeploymentTarget(DerivedArgList &Args) const; 00217 00218 public: 00219 Darwin(const Driver &D, const llvm::Triple& Triple); 00220 ~Darwin(); 00221 00222 std::string ComputeEffectiveClangTriple(const ArgList &Args, 00223 types::ID InputType) const; 00224 00225 /// @name Darwin Specific Toolchain API 00226 /// { 00227 00228 // FIXME: Eliminate these ...Target functions and derive separate tool chains 00229 // for these targets and put version in constructor. 00230 void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor, 00231 unsigned Micro, bool IsIOSSim) const { 00232 assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!"); 00233 00234 // FIXME: For now, allow reinitialization as long as values don't 00235 // change. This will go away when we move away from argument translation. 00236 if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS && 00237 TargetIsIPhoneOSSimulator == IsIOSSim && 00238 TargetVersion == VersionTuple(Major, Minor, Micro)) 00239 return; 00240 00241 assert(!TargetInitialized && "Target already initialized!"); 00242 TargetInitialized = true; 00243 TargetIsIPhoneOS = IsIPhoneOS; 00244 TargetIsIPhoneOSSimulator = IsIOSSim; 00245 TargetVersion = VersionTuple(Major, Minor, Micro); 00246 } 00247 00248 bool isTargetIPhoneOS() const { 00249 assert(TargetInitialized && "Target not initialized!"); 00250 return TargetIsIPhoneOS; 00251 } 00252 00253 bool isTargetIOSSimulator() const { 00254 assert(TargetInitialized && "Target not initialized!"); 00255 return TargetIsIPhoneOSSimulator; 00256 } 00257 00258 bool isTargetMacOS() const { 00259 return !isTargetIOSSimulator() && 00260 !isTargetIPhoneOS() && 00261 ARCRuntimeForSimulator == ARCSimulator_None; 00262 } 00263 00264 bool isTargetInitialized() const { return TargetInitialized; } 00265 00266 VersionTuple getTargetVersion() const { 00267 assert(TargetInitialized && "Target not initialized!"); 00268 return TargetVersion; 00269 } 00270 00271 /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler 00272 /// invocation. For example, Darwin treats different ARM variations as 00273 /// distinct architectures. 00274 StringRef getDarwinArchName(const ArgList &Args) const; 00275 00276 bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const { 00277 assert(isTargetIPhoneOS() && "Unexpected call for OS X target!"); 00278 return TargetVersion < VersionTuple(V0, V1, V2); 00279 } 00280 00281 bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const { 00282 assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!"); 00283 return TargetVersion < VersionTuple(V0, V1, V2); 00284 } 00285 00286 /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library. 00287 virtual void AddLinkARCArgs(const ArgList &Args, 00288 ArgStringList &CmdArgs) const = 0; 00289 00290 /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler 00291 /// runtime library. 00292 virtual void AddLinkRuntimeLibArgs(const ArgList &Args, 00293 ArgStringList &CmdArgs) const = 0; 00294 00295 /// } 00296 /// @name ToolChain Implementation 00297 /// { 00298 00299 virtual types::ID LookupTypeForExtension(const char *Ext) const; 00300 00301 virtual bool HasNativeLLVMSupport() const; 00302 00303 virtual void configureObjCRuntime(ObjCRuntime &runtime) const; 00304 virtual bool hasBlocksRuntime() const; 00305 00306 virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args, 00307 const char *BoundArch) const; 00308 00309 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00310 const ActionList &Inputs) const; 00311 00312 virtual bool IsBlocksDefault() const { 00313 // Always allow blocks on Darwin; users interested in versioning are 00314 // expected to use /usr/include/Blocks.h. 00315 return true; 00316 } 00317 virtual bool IsIntegratedAssemblerDefault() const { 00318 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER 00319 return false; 00320 #else 00321 // Default integrated assembler to on for Darwin. 00322 return true; 00323 #endif 00324 } 00325 virtual bool IsStrictAliasingDefault() const { 00326 #ifdef DISABLE_DEFAULT_STRICT_ALIASING 00327 return false; 00328 #else 00329 return ToolChain::IsStrictAliasingDefault(); 00330 #endif 00331 } 00332 00333 virtual bool IsMathErrnoDefault() const { 00334 return false; 00335 } 00336 00337 virtual bool IsObjCDefaultSynthPropertiesDefault() const { 00338 return true; 00339 } 00340 00341 virtual bool IsObjCNonFragileABIDefault() const { 00342 // Non-fragile ABI is default for everything but i386. 00343 return getTriple().getArch() != llvm::Triple::x86; 00344 } 00345 virtual bool IsObjCLegacyDispatchDefault() const { 00346 // This is only used with the non-fragile ABI. 00347 00348 // Legacy dispatch is used everywhere except on x86_64. 00349 return getTriple().getArch() != llvm::Triple::x86_64; 00350 } 00351 virtual bool UseObjCMixedDispatch() const { 00352 // This is only used with the non-fragile ABI and non-legacy dispatch. 00353 00354 // Mixed dispatch is used everywhere except OS X before 10.6. 00355 return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6)); 00356 } 00357 virtual bool IsUnwindTablesDefault() const; 00358 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const { 00359 // Stack protectors default to on for user code on 10.5, 00360 // and for everything in 10.6 and beyond 00361 return isTargetIPhoneOS() || 00362 (!isMacosxVersionLT(10, 6) || 00363 (!isMacosxVersionLT(10, 5) && !KernelOrKext)); 00364 } 00365 virtual RuntimeLibType GetDefaultRuntimeLibType() const { 00366 return ToolChain::RLT_CompilerRT; 00367 } 00368 virtual const char *GetDefaultRelocationModel() const; 00369 virtual const char *GetForcedPicModel() const; 00370 00371 virtual bool SupportsProfiling() const; 00372 00373 virtual bool SupportsObjCGC() const; 00374 00375 virtual bool SupportsObjCARC() const; 00376 00377 virtual bool UseDwarfDebugFlags() const; 00378 00379 virtual bool UseSjLjExceptions() const; 00380 00381 /// } 00382 }; 00383 00384 /// DarwinClang - The Darwin toolchain used by Clang. 00385 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin { 00386 private: 00387 void AddGCCLibexecPath(unsigned darwinVersion); 00388 00389 public: 00390 DarwinClang(const Driver &D, const llvm::Triple& Triple); 00391 00392 /// @name Darwin ToolChain Implementation 00393 /// { 00394 00395 virtual void AddLinkRuntimeLibArgs(const ArgList &Args, 00396 ArgStringList &CmdArgs) const; 00397 void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, 00398 const char *DarwinStaticLib) const; 00399 00400 virtual void AddCXXStdlibLibArgs(const ArgList &Args, 00401 ArgStringList &CmdArgs) const; 00402 00403 virtual void AddCCKextLibArgs(const ArgList &Args, 00404 ArgStringList &CmdArgs) const; 00405 00406 virtual void AddLinkARCArgs(const ArgList &Args, 00407 ArgStringList &CmdArgs) const; 00408 /// } 00409 }; 00410 00411 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc. 00412 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC { 00413 public: 00414 Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 00415 : Generic_GCC(D, Triple, Args) {} 00416 00417 std::string ComputeEffectiveClangTriple(const ArgList &Args, 00418 types::ID InputType) const; 00419 00420 virtual const char *GetDefaultRelocationModel() const { return "pic"; } 00421 }; 00422 00423 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC { 00424 virtual void anchor(); 00425 public: 00426 Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 00427 : Generic_GCC(D, Triple, Args) {} 00428 00429 virtual bool IsIntegratedAssemblerDefault() const { 00430 // Default integrated assembler to on for x86. 00431 return (getTriple().getArch() == llvm::Triple::x86 || 00432 getTriple().getArch() == llvm::Triple::x86_64); 00433 } 00434 }; 00435 00436 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC { 00437 public: 00438 AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00439 00440 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00441 const ActionList &Inputs) const; 00442 }; 00443 00444 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC { 00445 public: 00446 Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00447 00448 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00449 const ActionList &Inputs) const; 00450 00451 virtual bool IsIntegratedAssemblerDefault() const { return true; } 00452 }; 00453 00454 00455 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF { 00456 public: 00457 OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00458 00459 virtual bool IsMathErrnoDefault() const { return false; } 00460 virtual bool IsObjCNonFragileABIDefault() const { return true; } 00461 virtual bool IsObjCLegacyDispatchDefault() const { 00462 llvm::Triple::ArchType Arch = getTriple().getArch(); 00463 if (Arch == llvm::Triple::arm || 00464 Arch == llvm::Triple::x86 || 00465 Arch == llvm::Triple::x86_64) 00466 return false; 00467 return true; 00468 } 00469 00470 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00471 const ActionList &Inputs) const; 00472 }; 00473 00474 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF { 00475 public: 00476 FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00477 00478 virtual bool IsMathErrnoDefault() const { return false; } 00479 virtual bool IsObjCNonFragileABIDefault() const { return true; } 00480 virtual bool IsObjCLegacyDispatchDefault() const { 00481 llvm::Triple::ArchType Arch = getTriple().getArch(); 00482 if (Arch == llvm::Triple::arm || 00483 Arch == llvm::Triple::x86 || 00484 Arch == llvm::Triple::x86_64) 00485 return false; 00486 return true; 00487 } 00488 00489 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00490 const ActionList &Inputs) const; 00491 }; 00492 00493 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF { 00494 public: 00495 NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00496 00497 virtual bool IsMathErrnoDefault() const { return false; } 00498 virtual bool IsObjCNonFragileABIDefault() const { return true; } 00499 virtual bool IsObjCLegacyDispatchDefault() const { 00500 llvm::Triple::ArchType Arch = getTriple().getArch(); 00501 if (Arch == llvm::Triple::arm || 00502 Arch == llvm::Triple::x86 || 00503 Arch == llvm::Triple::x86_64) 00504 return false; 00505 return true; 00506 } 00507 00508 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00509 const ActionList &Inputs) const; 00510 }; 00511 00512 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF { 00513 public: 00514 Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00515 00516 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00517 const ActionList &Inputs) const; 00518 }; 00519 00520 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF { 00521 public: 00522 DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00523 00524 virtual bool IsMathErrnoDefault() const { return false; } 00525 00526 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00527 const ActionList &Inputs) const; 00528 }; 00529 00530 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF { 00531 public: 00532 Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args); 00533 00534 virtual bool HasNativeLLVMSupport() const; 00535 00536 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00537 const ActionList &Inputs) const; 00538 00539 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs, 00540 ArgStringList &CC1Args) const; 00541 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 00542 ArgStringList &CC1Args) const; 00543 00544 std::string Linker; 00545 std::vector<std::string> ExtraOpts; 00546 00547 private: 00548 static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir, 00549 const ArgList &DriverArgs, 00550 ArgStringList &CC1Args); 00551 }; 00552 00553 00554 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform 00555 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target. 00556 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain { 00557 public: 00558 TCEToolChain(const Driver &D, const llvm::Triple& Triple); 00559 ~TCEToolChain(); 00560 00561 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00562 const ActionList &Inputs) const; 00563 bool IsMathErrnoDefault() const; 00564 bool IsUnwindTablesDefault() const; 00565 const char* GetDefaultRelocationModel() const; 00566 const char* GetForcedPicModel() const; 00567 00568 private: 00569 mutable llvm::DenseMap<unsigned, Tool*> Tools; 00570 00571 }; 00572 00573 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain { 00574 mutable llvm::DenseMap<unsigned, Tool*> Tools; 00575 00576 public: 00577 Windows(const Driver &D, const llvm::Triple& Triple); 00578 00579 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA, 00580 const ActionList &Inputs) const; 00581 00582 virtual bool IsIntegratedAssemblerDefault() const; 00583 virtual bool IsUnwindTablesDefault() const; 00584 virtual const char *GetDefaultRelocationModel() const; 00585 virtual const char *GetForcedPicModel() const; 00586 00587 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs, 00588 ArgStringList &CC1Args) const; 00589 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 00590 ArgStringList &CC1Args) const; 00591 00592 }; 00593 00594 } // end namespace toolchains 00595 } // end namespace driver 00596 } // end namespace clang 00597 00598 #endif