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() : IsValid(false) {}
82 void init(const Driver &D, const llvm::Triple &TargetTriple,
85 /// \brief Check whether we detected a valid GCC install.
86 bool isValid() const { return IsValid; }
88 /// \brief Get the GCC triple for the detected install.
89 const llvm::Triple &getTriple() const { return GCCTriple; }
91 /// \brief Get the detected GCC installation path.
92 StringRef getInstallPath() const { return GCCInstallPath; }
94 /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
95 StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
97 /// \brief Get the detected GCC parent lib path.
98 StringRef getParentLibPath() const { return GCCParentLibPath; }
100 /// \brief Get the detected GCC version string.
101 const GCCVersion &getVersion() const { return Version; }
104 static void CollectLibDirsAndTriples(
105 const llvm::Triple &TargetTriple,
106 const llvm::Triple &MultiarchTriple,
107 SmallVectorImpl<StringRef> &LibDirs,
108 SmallVectorImpl<StringRef> &TripleAliases,
109 SmallVectorImpl<StringRef> &MultiarchLibDirs,
110 SmallVectorImpl<StringRef> &MultiarchTripleAliases);
112 void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
114 const std::string &LibDir,
115 StringRef CandidateTriple,
116 bool NeedsMultiarchSuffix = false);
119 GCCInstallationDetector GCCInstallation;
122 Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
125 virtual bool IsUnwindTablesDefault() const;
126 virtual bool isPICDefault() const;
127 virtual bool isPIEDefault() const;
128 virtual bool isPICDefaultForced() const;
131 virtual Tool *getTool(Action::ActionClass AC) const;
132 virtual Tool *buildAssembler() const;
133 virtual Tool *buildLinker() const;
135 /// \name ToolChain Implementation Helper Functions
138 /// \brief Check whether the target triple's architecture is 64-bits.
139 bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
141 /// \brief Check whether the target triple's architecture is 32-bits.
142 bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
147 mutable OwningPtr<tools::gcc::Preprocess> Preprocess;
148 mutable OwningPtr<tools::gcc::Precompile> Precompile;
149 mutable OwningPtr<tools::gcc::Compile> Compile;
152 /// Darwin - The base Darwin tool chain.
153 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
155 /// The host version.
156 unsigned DarwinVersion[3];
159 virtual Tool *buildAssembler() const;
160 virtual Tool *buildLinker() const;
161 virtual Tool *getTool(Action::ActionClass AC) const;
164 mutable OwningPtr<tools::darwin::Lipo> Lipo;
165 mutable OwningPtr<tools::darwin::Dsymutil> Dsymutil;
166 mutable OwningPtr<tools::darwin::VerifyDebug> VerifyDebug;
168 /// Whether the information on the target has been initialized.
170 // FIXME: This should be eliminated. What we want to do is make this part of
171 // the "default target for arguments" selection process, once we get out of
172 // the argument translation business.
173 mutable bool TargetInitialized;
175 /// Whether we are targeting iPhoneOS target.
176 mutable bool TargetIsIPhoneOS;
178 /// Whether we are targeting the iPhoneOS simulator target.
179 mutable bool TargetIsIPhoneOSSimulator;
181 /// The OS version we are targeting.
182 mutable VersionTuple TargetVersion;
185 /// The default macosx-version-min of this tool chain; empty until
187 std::string MacosxVersionMin;
189 /// The default ios-version-min of this tool chain; empty until
191 std::string iOSVersionMin;
194 void AddDeploymentTarget(DerivedArgList &Args) const;
197 Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
200 std::string ComputeEffectiveClangTriple(const ArgList &Args,
201 types::ID InputType) const;
203 /// @name Darwin Specific Toolchain API
206 // FIXME: Eliminate these ...Target functions and derive separate tool chains
207 // for these targets and put version in constructor.
208 void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
209 unsigned Micro, bool IsIOSSim) const {
210 assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
212 // FIXME: For now, allow reinitialization as long as values don't
213 // change. This will go away when we move away from argument translation.
214 if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
215 TargetIsIPhoneOSSimulator == IsIOSSim &&
216 TargetVersion == VersionTuple(Major, Minor, Micro))
219 assert(!TargetInitialized && "Target already initialized!");
220 TargetInitialized = true;
221 TargetIsIPhoneOS = IsIPhoneOS;
222 TargetIsIPhoneOSSimulator = IsIOSSim;
223 TargetVersion = VersionTuple(Major, Minor, Micro);
226 bool isTargetIPhoneOS() const {
227 assert(TargetInitialized && "Target not initialized!");
228 return TargetIsIPhoneOS;
231 bool isTargetIOSSimulator() const {
232 assert(TargetInitialized && "Target not initialized!");
233 return TargetIsIPhoneOSSimulator;
236 bool isTargetMacOS() const {
237 return !isTargetIOSSimulator() && !isTargetIPhoneOS();
240 bool isTargetInitialized() const { return TargetInitialized; }
242 VersionTuple getTargetVersion() const {
243 assert(TargetInitialized && "Target not initialized!");
244 return TargetVersion;
247 /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
248 /// invocation. For example, Darwin treats different ARM variations as
249 /// distinct architectures.
250 StringRef getDarwinArchName(const ArgList &Args) const;
252 bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
253 assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
254 return TargetVersion < VersionTuple(V0, V1, V2);
257 bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
258 assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
259 return TargetVersion < VersionTuple(V0, V1, V2);
262 /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
263 virtual void AddLinkARCArgs(const ArgList &Args,
264 ArgStringList &CmdArgs) const = 0;
266 /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
268 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
269 ArgStringList &CmdArgs) const = 0;
272 /// @name ToolChain Implementation
275 virtual types::ID LookupTypeForExtension(const char *Ext) const;
277 virtual bool HasNativeLLVMSupport() const;
279 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
280 virtual bool hasBlocksRuntime() const;
282 virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
283 const char *BoundArch) const;
285 virtual bool IsBlocksDefault() const {
286 // Always allow blocks on Darwin; users interested in versioning are
287 // expected to use /usr/include/Blocks.h.
290 virtual bool IsIntegratedAssemblerDefault() const {
291 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
294 // Default integrated assembler to on for Darwin.
298 virtual bool IsStrictAliasingDefault() const {
299 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
302 return ToolChain::IsStrictAliasingDefault();
306 virtual bool IsMathErrnoDefault() const {
310 virtual bool IsEncodeExtendedBlockSignatureDefault() const {
314 virtual bool IsObjCNonFragileABIDefault() const {
315 // Non-fragile ABI is default for everything but i386.
316 return getTriple().getArch() != llvm::Triple::x86;
319 virtual bool UseObjCMixedDispatch() const {
320 // This is only used with the non-fragile ABI and non-legacy dispatch.
322 // Mixed dispatch is used everywhere except OS X before 10.6.
323 return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
325 virtual bool IsUnwindTablesDefault() const;
326 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
327 // Stack protectors default to on for user code on 10.5,
328 // and for everything in 10.6 and beyond
329 return isTargetIPhoneOS() ||
330 (!isMacosxVersionLT(10, 6) ||
331 (!isMacosxVersionLT(10, 5) && !KernelOrKext));
333 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
334 return ToolChain::RLT_CompilerRT;
336 virtual bool isPICDefault() const;
337 virtual bool isPIEDefault() const;
338 virtual bool isPICDefaultForced() const;
340 virtual bool SupportsProfiling() const;
342 virtual bool SupportsObjCGC() const;
344 virtual void CheckObjCARC() const;
346 virtual bool UseDwarfDebugFlags() const;
348 virtual bool UseSjLjExceptions() const;
353 /// DarwinClang - The Darwin toolchain used by Clang.
354 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
356 DarwinClang(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
358 /// @name Darwin ToolChain Implementation
361 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
362 ArgStringList &CmdArgs) const;
363 void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
364 const char *DarwinStaticLib,
365 bool AlwaysLink = false) const;
367 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
368 ArgStringList &CmdArgs) const;
370 virtual void AddCCKextLibArgs(const ArgList &Args,
371 ArgStringList &CmdArgs) const;
373 virtual void AddLinkARCArgs(const ArgList &Args,
374 ArgStringList &CmdArgs) const;
378 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
379 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
381 Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
382 : Generic_GCC(D, Triple, Args) {}
384 std::string ComputeEffectiveClangTriple(const ArgList &Args,
385 types::ID InputType) const;
387 virtual bool isPICDefault() const { return false; }
390 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
391 virtual void anchor();
393 Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
394 : Generic_GCC(D, Triple, Args) {}
396 virtual bool IsIntegratedAssemblerDefault() const {
397 // Default integrated assembler to on for x86.
398 return (getTriple().getArch() == llvm::Triple::aarch64 ||
399 getTriple().getArch() == llvm::Triple::x86 ||
400 getTriple().getArch() == llvm::Triple::x86_64);
404 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
406 AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
409 virtual Tool *buildAssembler() const;
410 virtual Tool *buildLinker() const;
413 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
415 Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
417 virtual bool IsIntegratedAssemblerDefault() const { return true; }
419 virtual Tool *buildAssembler() const;
420 virtual Tool *buildLinker() const;
425 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
427 OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
429 virtual bool IsMathErrnoDefault() const { return false; }
430 virtual bool IsObjCNonFragileABIDefault() const { return true; }
433 virtual Tool *buildAssembler() const;
434 virtual Tool *buildLinker() const;
437 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
439 Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
441 virtual bool IsMathErrnoDefault() const { return false; }
442 virtual bool IsObjCNonFragileABIDefault() const { return true; }
443 virtual bool IsObjCLegacyDispatchDefault() const { return false; }
445 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
446 ArgStringList &CC1Args) const;
447 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
448 ArgStringList &CmdArgs) const;
449 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
454 virtual Tool *buildAssembler() const;
455 virtual Tool *buildLinker() const;
458 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
460 FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
462 virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
464 virtual bool IsMathErrnoDefault() const { return false; }
465 virtual bool IsObjCNonFragileABIDefault() const { return true; }
467 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
468 ArgStringList &CC1Args) const;
470 virtual bool UseSjLjExceptions() const;
472 virtual Tool *buildAssembler() const;
473 virtual Tool *buildLinker() const;
476 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
478 NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
480 virtual bool IsMathErrnoDefault() const { return false; }
481 virtual bool IsObjCNonFragileABIDefault() const { return true; }
483 virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
485 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
486 ArgStringList &CC1Args) const;
489 virtual Tool *buildAssembler() const;
490 virtual Tool *buildLinker() const;
493 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
495 Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
498 virtual Tool *buildAssembler() const;
499 virtual Tool *buildLinker() const;
502 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
504 DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
506 virtual bool IsMathErrnoDefault() const { return false; }
509 virtual Tool *buildAssembler() const;
510 virtual Tool *buildLinker() const;
513 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
515 Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
517 virtual bool HasNativeLLVMSupport() const;
519 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
520 ArgStringList &CC1Args) const;
521 virtual void addClangTargetOptions(const ArgList &DriverArgs,
522 ArgStringList &CC1Args) const;
523 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
524 ArgStringList &CC1Args) const;
525 virtual bool isPIEDefault() const;
528 std::vector<std::string> ExtraOpts;
532 virtual Tool *buildAssembler() const;
533 virtual Tool *buildLinker() const;
536 static bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
538 Twine MultiLibSuffix,
539 const ArgList &DriverArgs,
540 ArgStringList &CC1Args);
541 static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
542 const ArgList &DriverArgs,
543 ArgStringList &CC1Args);
545 std::string computeSysRoot(const ArgList &Args) const;
548 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public Linux {
550 GCCVersion GCCLibAndIncVersion;
551 virtual Tool *buildAssembler() const;
552 virtual Tool *buildLinker() const;
555 Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
556 const ArgList &Args);
559 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
560 ArgStringList &CC1Args) const;
561 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
562 ArgStringList &CC1Args) const;
563 virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
565 StringRef GetGCCLibAndIncVersion() const { return GCCLibAndIncVersion.Text; }
567 static std::string GetGnuDir(const std::string &InstalledDir);
569 static StringRef GetTargetCPU(const ArgList &Args);
572 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
573 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
574 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
576 TCEToolChain(const Driver &D, const llvm::Triple& Triple,
577 const ArgList &Args);
580 bool IsMathErrnoDefault() const;
581 bool isPICDefault() const;
582 bool isPIEDefault() const;
583 bool isPICDefaultForced() const;
586 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
588 Windows(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
590 virtual bool IsIntegratedAssemblerDefault() const;
591 virtual bool IsUnwindTablesDefault() const;
592 virtual bool isPICDefault() const;
593 virtual bool isPIEDefault() const;
594 virtual bool isPICDefaultForced() const;
596 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
597 ArgStringList &CC1Args) const;
598 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
599 ArgStringList &CC1Args) const;
601 virtual Tool *buildLinker() const;
602 virtual Tool *buildAssembler() const;
605 } // end namespace toolchains
606 } // end namespace driver
607 } // end namespace clang