1 //===--- ToolChains.h - ToolChain Implementations ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef CLANG_LIB_DRIVER_TOOLCHAINS_H_
11 #define CLANG_LIB_DRIVER_TOOLCHAINS_H_
14 #include "clang/Basic/VersionTuple.h"
15 #include "clang/Driver/Action.h"
16 #include "clang/Driver/ToolChain.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/Support/Compiler.h"
22 namespace toolchains {
24 /// Generic_GCC - A tool chain using the 'gcc' command to perform
25 /// all subcommands; this relies on gcc translating the majority of
26 /// command line options.
27 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
29 /// \brief Struct to store and manipulate GCC versions.
31 /// We rely on assumptions about the form and structure of GCC version
32 /// numbers: they consist of at most three '.'-separated components, and each
33 /// component is a non-negative integer except for the last component. For
34 /// the last component we are very flexible in order to tolerate release
35 /// candidates or 'x' wildcards.
37 /// Note that the ordering established among GCCVersions is based on the
38 /// preferred version string to use. For example we prefer versions without
39 /// a hard-coded patch number to those with a hard coded patch number.
41 /// Currently this doesn't provide any logic for textual suffixes to patches
42 /// in the way that (for example) Debian's version format does. If that ever
43 /// becomes necessary, it can be added.
45 /// \brief The unparsed text of the version.
48 /// \brief The parsed major, minor, and patch numbers.
49 int Major, Minor, Patch;
51 /// \brief Any textual suffix on the patch number.
52 std::string PatchSuffix;
54 static GCCVersion Parse(StringRef VersionText);
55 bool operator<(const GCCVersion &RHS) const;
56 bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
57 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
58 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
62 /// \brief This is a class to find a viable GCC installation for Clang to
65 /// This class tries to find a GCC installation on the system, and report
66 /// information about it. It starts from the host information provided to the
67 /// Driver, and has logic for fuzzing that where appropriate.
68 class GCCInstallationDetector {
71 llvm::Triple GCCTriple;
73 // FIXME: These might be better as path objects.
74 std::string GCCInstallPath;
75 std::string GCCMultiarchSuffix;
76 std::string GCCParentLibPath;
81 GCCInstallationDetector(const Driver &D, const llvm::Triple &TargetTriple,
84 /// \brief Check whether we detected a valid GCC install.
85 bool isValid() const { return IsValid; }
87 /// \brief Get the GCC triple for the detected install.
88 const llvm::Triple &getTriple() const { return GCCTriple; }
90 /// \brief Get the detected GCC installation path.
91 StringRef getInstallPath() const { return GCCInstallPath; }
93 /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
94 StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
96 /// \brief Get the detected GCC parent lib path.
97 StringRef getParentLibPath() const { return GCCParentLibPath; }
99 /// \brief Get the detected GCC version string.
100 const GCCVersion &getVersion() const { return Version; }
103 static void CollectLibDirsAndTriples(
104 const llvm::Triple &TargetTriple,
105 const llvm::Triple &MultiarchTriple,
106 SmallVectorImpl<StringRef> &LibDirs,
107 SmallVectorImpl<StringRef> &TripleAliases,
108 SmallVectorImpl<StringRef> &MultiarchLibDirs,
109 SmallVectorImpl<StringRef> &MultiarchTripleAliases);
111 void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
113 const std::string &LibDir,
114 StringRef CandidateTriple,
115 bool NeedsMultiarchSuffix = false);
118 GCCInstallationDetector GCCInstallation;
121 Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
124 virtual bool IsUnwindTablesDefault() const;
125 virtual bool isPICDefault() const;
126 virtual bool isPIEDefault() const;
127 virtual bool isPICDefaultForced() const;
130 virtual Tool *getTool(Action::ActionClass AC) const;
131 virtual Tool *buildAssembler() const;
132 virtual Tool *buildLinker() const;
134 /// \name ToolChain Implementation Helper Functions
137 /// \brief Check whether the target triple's architecture is 64-bits.
138 bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
140 /// \brief Check whether the target triple's architecture is 32-bits.
141 bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
146 mutable OwningPtr<tools::gcc::Preprocess> Preprocess;
147 mutable OwningPtr<tools::gcc::Precompile> Precompile;
148 mutable OwningPtr<tools::gcc::Compile> Compile;
151 /// Darwin - The base Darwin tool chain.
152 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
154 /// The host version.
155 unsigned DarwinVersion[3];
158 virtual Tool *buildAssembler() const;
159 virtual Tool *buildLinker() const;
160 virtual Tool *getTool(Action::ActionClass AC) const;
163 mutable OwningPtr<tools::darwin::Lipo> Lipo;
164 mutable OwningPtr<tools::darwin::Dsymutil> Dsymutil;
165 mutable OwningPtr<tools::darwin::VerifyDebug> VerifyDebug;
167 /// Whether the information on the target has been initialized.
169 // FIXME: This should be eliminated. What we want to do is make this part of
170 // the "default target for arguments" selection process, once we get out of
171 // the argument translation business.
172 mutable bool TargetInitialized;
174 /// Whether we are targeting iPhoneOS target.
175 mutable bool TargetIsIPhoneOS;
177 /// Whether we are targeting the iPhoneOS simulator target.
178 mutable bool TargetIsIPhoneOSSimulator;
180 /// The OS version we are targeting.
181 mutable VersionTuple TargetVersion;
184 /// The default macosx-version-min of this tool chain; empty until
186 std::string MacosxVersionMin;
188 /// The default ios-version-min of this tool chain; empty until
190 std::string iOSVersionMin;
193 void AddDeploymentTarget(DerivedArgList &Args) const;
196 Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
199 std::string ComputeEffectiveClangTriple(const ArgList &Args,
200 types::ID InputType) const;
202 /// @name Darwin Specific Toolchain API
205 // FIXME: Eliminate these ...Target functions and derive separate tool chains
206 // for these targets and put version in constructor.
207 void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
208 unsigned Micro, bool IsIOSSim) const {
209 assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
211 // FIXME: For now, allow reinitialization as long as values don't
212 // change. This will go away when we move away from argument translation.
213 if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
214 TargetIsIPhoneOSSimulator == IsIOSSim &&
215 TargetVersion == VersionTuple(Major, Minor, Micro))
218 assert(!TargetInitialized && "Target already initialized!");
219 TargetInitialized = true;
220 TargetIsIPhoneOS = IsIPhoneOS;
221 TargetIsIPhoneOSSimulator = IsIOSSim;
222 TargetVersion = VersionTuple(Major, Minor, Micro);
225 bool isTargetIPhoneOS() const {
226 assert(TargetInitialized && "Target not initialized!");
227 return TargetIsIPhoneOS;
230 bool isTargetIOSSimulator() const {
231 assert(TargetInitialized && "Target not initialized!");
232 return TargetIsIPhoneOSSimulator;
235 bool isTargetMacOS() const {
236 return !isTargetIOSSimulator() && !isTargetIPhoneOS();
239 bool isTargetInitialized() const { return TargetInitialized; }
241 VersionTuple getTargetVersion() const {
242 assert(TargetInitialized && "Target not initialized!");
243 return TargetVersion;
246 /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
247 /// invocation. For example, Darwin treats different ARM variations as
248 /// distinct architectures.
249 StringRef getDarwinArchName(const ArgList &Args) const;
251 bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
252 assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
253 return TargetVersion < VersionTuple(V0, V1, V2);
256 bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
257 assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
258 return TargetVersion < VersionTuple(V0, V1, V2);
261 /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
262 virtual void AddLinkARCArgs(const ArgList &Args,
263 ArgStringList &CmdArgs) const = 0;
265 /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
267 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
268 ArgStringList &CmdArgs) const = 0;
271 /// @name ToolChain Implementation
274 virtual types::ID LookupTypeForExtension(const char *Ext) const;
276 virtual bool HasNativeLLVMSupport() const;
278 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
279 virtual bool hasBlocksRuntime() const;
281 virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
282 const char *BoundArch) const;
284 virtual bool IsBlocksDefault() const {
285 // Always allow blocks on Darwin; users interested in versioning are
286 // expected to use /usr/include/Blocks.h.
289 virtual bool IsIntegratedAssemblerDefault() const {
290 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
293 // Default integrated assembler to on for Darwin.
297 virtual bool IsStrictAliasingDefault() const {
298 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
301 return ToolChain::IsStrictAliasingDefault();
305 virtual bool IsMathErrnoDefault() const {
309 virtual bool IsEncodeExtendedBlockSignatureDefault() const {
313 virtual bool IsObjCNonFragileABIDefault() const {
314 // Non-fragile ABI is default for everything but i386.
315 return getTriple().getArch() != llvm::Triple::x86;
318 virtual bool UseObjCMixedDispatch() const {
319 // This is only used with the non-fragile ABI and non-legacy dispatch.
321 // Mixed dispatch is used everywhere except OS X before 10.6.
322 return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
324 virtual bool IsUnwindTablesDefault() const;
325 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
326 // Stack protectors default to on for user code on 10.5,
327 // and for everything in 10.6 and beyond
328 return isTargetIPhoneOS() ||
329 (!isMacosxVersionLT(10, 6) ||
330 (!isMacosxVersionLT(10, 5) && !KernelOrKext));
332 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
333 return ToolChain::RLT_CompilerRT;
335 virtual bool isPICDefault() const;
336 virtual bool isPIEDefault() const;
337 virtual bool isPICDefaultForced() const;
339 virtual bool SupportsProfiling() const;
341 virtual bool SupportsObjCGC() const;
343 virtual void CheckObjCARC() const;
345 virtual bool UseDwarfDebugFlags() const;
347 virtual bool UseSjLjExceptions() const;
352 /// DarwinClang - The Darwin toolchain used by Clang.
353 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
355 DarwinClang(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
357 /// @name Darwin ToolChain Implementation
360 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
361 ArgStringList &CmdArgs) const;
362 void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
363 const char *DarwinStaticLib,
364 bool AlwaysLink = false) const;
366 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
367 ArgStringList &CmdArgs) const;
369 virtual void AddCCKextLibArgs(const ArgList &Args,
370 ArgStringList &CmdArgs) const;
372 virtual void AddLinkARCArgs(const ArgList &Args,
373 ArgStringList &CmdArgs) const;
377 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
378 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
380 Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
381 : Generic_GCC(D, Triple, Args) {}
383 std::string ComputeEffectiveClangTriple(const ArgList &Args,
384 types::ID InputType) const;
386 virtual bool isPICDefault() const { return false; }
389 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
390 virtual void anchor();
392 Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
393 : Generic_GCC(D, Triple, Args) {}
395 virtual bool IsIntegratedAssemblerDefault() const {
396 // Default integrated assembler to on for x86.
397 return (getTriple().getArch() == llvm::Triple::aarch64 ||
398 getTriple().getArch() == llvm::Triple::x86 ||
399 getTriple().getArch() == llvm::Triple::x86_64);
403 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
405 AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
408 virtual Tool *buildAssembler() const;
409 virtual Tool *buildLinker() const;
412 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
414 Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
416 virtual bool IsIntegratedAssemblerDefault() const { return true; }
418 virtual Tool *buildAssembler() const;
419 virtual Tool *buildLinker() const;
424 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
426 OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
428 virtual bool IsMathErrnoDefault() const { return false; }
429 virtual bool IsObjCNonFragileABIDefault() const { return true; }
432 virtual Tool *buildAssembler() const;
433 virtual Tool *buildLinker() const;
436 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
438 Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
440 virtual bool IsMathErrnoDefault() const { return false; }
441 virtual bool IsObjCNonFragileABIDefault() const { return true; }
442 virtual bool IsObjCLegacyDispatchDefault() const { return false; }
444 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
445 ArgStringList &CC1Args) const;
446 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
447 ArgStringList &CmdArgs) const;
448 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
453 virtual Tool *buildAssembler() const;
454 virtual Tool *buildLinker() const;
457 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
459 FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
461 virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
463 virtual bool IsMathErrnoDefault() const { return false; }
464 virtual bool IsObjCNonFragileABIDefault() const { return true; }
466 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
467 ArgStringList &CC1Args) const;
469 virtual bool UseSjLjExceptions() const;
471 virtual Tool *buildAssembler() const;
472 virtual Tool *buildLinker() const;
475 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
477 NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
479 virtual bool IsMathErrnoDefault() const { return false; }
480 virtual bool IsObjCNonFragileABIDefault() const { return true; }
482 virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
484 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
485 ArgStringList &CC1Args) const;
488 virtual Tool *buildAssembler() const;
489 virtual Tool *buildLinker() const;
492 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
494 Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
497 virtual Tool *buildAssembler() const;
498 virtual Tool *buildLinker() const;
501 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
503 DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
505 virtual bool IsMathErrnoDefault() const { return false; }
508 virtual Tool *buildAssembler() const;
509 virtual Tool *buildLinker() const;
512 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
514 Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
516 virtual bool HasNativeLLVMSupport() const;
518 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
519 ArgStringList &CC1Args) const;
520 virtual void addClangTargetOptions(const ArgList &DriverArgs,
521 ArgStringList &CC1Args) const;
522 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
523 ArgStringList &CC1Args) const;
524 virtual bool isPIEDefault() const;
527 std::vector<std::string> ExtraOpts;
531 virtual Tool *buildAssembler() const;
532 virtual Tool *buildLinker() const;
535 static bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
537 Twine MultiLibSuffix,
538 const ArgList &DriverArgs,
539 ArgStringList &CC1Args);
540 static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
541 const ArgList &DriverArgs,
542 ArgStringList &CC1Args);
544 std::string computeSysRoot(const ArgList &Args) const;
547 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public Linux {
549 GCCVersion GCCLibAndIncVersion;
550 virtual Tool *buildAssembler() const;
551 virtual Tool *buildLinker() const;
554 Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
555 const ArgList &Args);
558 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
559 ArgStringList &CC1Args) const;
560 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
561 ArgStringList &CC1Args) const;
562 virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
564 StringRef GetGCCLibAndIncVersion() const { return GCCLibAndIncVersion.Text; }
566 static std::string GetGnuDir(const std::string &InstalledDir);
568 static StringRef GetTargetCPU(const ArgList &Args);
571 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
572 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
573 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
575 TCEToolChain(const Driver &D, const llvm::Triple& Triple,
576 const ArgList &Args);
579 bool IsMathErrnoDefault() const;
580 bool isPICDefault() const;
581 bool isPIEDefault() const;
582 bool isPICDefaultForced() const;
585 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
587 Windows(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
589 virtual bool IsIntegratedAssemblerDefault() const;
590 virtual bool IsUnwindTablesDefault() const;
591 virtual bool isPICDefault() const;
592 virtual bool isPIEDefault() const;
593 virtual bool isPICDefaultForced() const;
595 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
596 ArgStringList &CC1Args) const;
597 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
598 ArgStringList &CC1Args) const;
600 virtual Tool *buildLinker() const;
601 virtual Tool *buildAssembler() const;
604 } // end namespace toolchains
605 } // end namespace driver
606 } // end namespace clang