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_
13 #include "clang/Driver/Action.h"
14 #include "clang/Driver/ToolChain.h"
16 #include "clang/Basic/VersionTuple.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/Support/Compiler.h"
24 namespace toolchains {
26 /// Generic_GCC - A tool chain using the 'gcc' command to perform
27 /// all subcommands; this relies on gcc translating the majority of
28 /// command line options.
29 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
31 /// \brief Struct to store and manipulate GCC versions.
33 /// We rely on assumptions about the form and structure of GCC version
34 /// numbers: they consist of at most three '.'-separated components, and each
35 /// component is a non-negative integer except for the last component. For
36 /// the last component we are very flexible in order to tolerate release
37 /// candidates or 'x' wildcards.
39 /// Note that the ordering established among GCCVersions is based on the
40 /// preferred version string to use. For example we prefer versions without
41 /// a hard-coded patch number to those with a hard coded patch number.
43 /// Currently this doesn't provide any logic for textual suffixes to patches
44 /// in the way that (for example) Debian's version format does. If that ever
45 /// becomes necessary, it can be added.
47 /// \brief The unparsed text of the version.
50 /// \brief The parsed major, minor, and patch numbers.
51 int Major, Minor, Patch;
53 /// \brief Any textual suffix on the patch number.
54 std::string PatchSuffix;
56 static GCCVersion Parse(StringRef VersionText);
57 bool operator<(const GCCVersion &RHS) const;
58 bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
59 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
60 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
64 /// \brief This is a class to find a viable GCC installation for Clang to
67 /// This class tries to find a GCC installation on the system, and report
68 /// information about it. It starts from the host information provided to the
69 /// Driver, and has logic for fuzzing that where appropriate.
70 class GCCInstallationDetector {
73 llvm::Triple GCCTriple;
75 // FIXME: These might be better as path objects.
76 std::string GCCInstallPath;
77 std::string GCCMultiarchSuffix;
78 std::string GCCParentLibPath;
83 GCCInstallationDetector(const Driver &D, const llvm::Triple &TargetTriple,
86 /// \brief Check whether we detected a valid GCC install.
87 bool isValid() const { return IsValid; }
89 /// \brief Get the GCC triple for the detected install.
90 const llvm::Triple &getTriple() const { return GCCTriple; }
92 /// \brief Get the detected GCC installation path.
93 StringRef getInstallPath() const { return GCCInstallPath; }
95 /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
96 StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
98 /// \brief Get the detected GCC parent lib path.
99 StringRef getParentLibPath() const { return GCCParentLibPath; }
101 /// \brief Get the detected GCC version string.
102 const GCCVersion &getVersion() const { return Version; }
105 static void CollectLibDirsAndTriples(
106 const llvm::Triple &TargetTriple,
107 const llvm::Triple &MultiarchTriple,
108 SmallVectorImpl<StringRef> &LibDirs,
109 SmallVectorImpl<StringRef> &TripleAliases,
110 SmallVectorImpl<StringRef> &MultiarchLibDirs,
111 SmallVectorImpl<StringRef> &MultiarchTripleAliases);
113 void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
114 const std::string &LibDir,
115 StringRef CandidateTriple,
116 bool NeedsMultiarchSuffix = false);
119 GCCInstallationDetector GCCInstallation;
121 mutable llvm::DenseMap<unsigned, Tool*> Tools;
124 Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
127 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
128 const ActionList &Inputs) const;
130 virtual bool IsUnwindTablesDefault() const;
131 virtual const char *GetDefaultRelocationModel() const;
132 virtual const char *GetForcedPicModel() 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 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public ToolChain {
149 mutable llvm::DenseMap<unsigned, Tool*> Tools;
152 Hexagon_TC(const Driver &D, const llvm::Triple& Triple);
155 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
156 const ActionList &Inputs) const;
158 virtual bool IsUnwindTablesDefault() const;
159 virtual const char *GetDefaultRelocationModel() const;
160 virtual const char *GetForcedPicModel() const;
163 /// Darwin - The base Darwin tool chain.
164 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
166 /// The host version.
167 unsigned DarwinVersion[3];
170 mutable llvm::DenseMap<unsigned, Tool*> Tools;
172 /// Whether the information on the target has been initialized.
174 // FIXME: This should be eliminated. What we want to do is make this part of
175 // the "default target for arguments" selection process, once we get out of
176 // the argument translation business.
177 mutable bool TargetInitialized;
179 /// Whether we are targeting iPhoneOS target.
180 mutable bool TargetIsIPhoneOS;
182 /// Whether we are targeting the iPhoneOS simulator target.
183 mutable bool TargetIsIPhoneOSSimulator;
185 /// The OS version we are targeting.
186 mutable VersionTuple TargetVersion;
189 // FIXME: Remove this once there is a proper way to detect an ARC runtime
190 // for the simulator.
191 mutable VersionTuple TargetSimulatorVersionFromDefines;
194 /// The default macosx-version-min of this tool chain; empty until
196 std::string MacosxVersionMin;
198 /// The default ios-version-min of this tool chain; empty until
200 std::string iOSVersionMin;
203 void AddDeploymentTarget(DerivedArgList &Args) const;
206 Darwin(const Driver &D, const llvm::Triple& Triple);
209 std::string ComputeEffectiveClangTriple(const ArgList &Args,
210 types::ID InputType) const;
212 /// @name Darwin Specific Toolchain API
215 // FIXME: Eliminate these ...Target functions and derive separate tool chains
216 // for these targets and put version in constructor.
217 void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
218 unsigned Micro, bool IsIOSSim) const {
219 assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
221 // FIXME: For now, allow reinitialization as long as values don't
222 // change. This will go away when we move away from argument translation.
223 if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
224 TargetIsIPhoneOSSimulator == IsIOSSim &&
225 TargetVersion == VersionTuple(Major, Minor, Micro))
228 assert(!TargetInitialized && "Target already initialized!");
229 TargetInitialized = true;
230 TargetIsIPhoneOS = IsIPhoneOS;
231 TargetIsIPhoneOSSimulator = IsIOSSim;
232 TargetVersion = VersionTuple(Major, Minor, Micro);
235 bool isTargetIPhoneOS() const {
236 assert(TargetInitialized && "Target not initialized!");
237 return TargetIsIPhoneOS;
240 bool isTargetIOSSimulator() const {
241 assert(TargetInitialized && "Target not initialized!");
242 return TargetIsIPhoneOSSimulator;
245 bool isTargetMacOS() const {
246 return !isTargetIOSSimulator() &&
247 !isTargetIPhoneOS() &&
248 TargetSimulatorVersionFromDefines == VersionTuple();
251 bool isTargetInitialized() const { return TargetInitialized; }
253 VersionTuple getTargetVersion() const {
254 assert(TargetInitialized && "Target not initialized!");
255 return TargetVersion;
258 /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
259 /// invocation. For example, Darwin treats different ARM variations as
260 /// distinct architectures.
261 StringRef getDarwinArchName(const ArgList &Args) const;
263 bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
264 assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
265 return TargetVersion < VersionTuple(V0, V1, V2);
268 bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
269 assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
270 return TargetVersion < VersionTuple(V0, V1, V2);
273 /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
274 virtual void AddLinkARCArgs(const ArgList &Args,
275 ArgStringList &CmdArgs) const = 0;
277 /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
279 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
280 ArgStringList &CmdArgs) const = 0;
283 /// @name ToolChain Implementation
286 virtual types::ID LookupTypeForExtension(const char *Ext) const;
288 virtual bool HasNativeLLVMSupport() const;
290 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
291 virtual bool hasBlocksRuntime() const;
293 virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
294 const char *BoundArch) const;
296 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
297 const ActionList &Inputs) const;
299 virtual bool IsBlocksDefault() const {
300 // Always allow blocks on Darwin; users interested in versioning are
301 // expected to use /usr/include/Blocks.h.
304 virtual bool IsIntegratedAssemblerDefault() const {
305 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
308 // Default integrated assembler to on for Darwin.
312 virtual bool IsStrictAliasingDefault() const {
313 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
316 return ToolChain::IsStrictAliasingDefault();
320 virtual bool IsMathErrnoDefault() const {
324 virtual bool IsObjCDefaultSynthPropertiesDefault() const {
328 virtual bool IsObjCNonFragileABIDefault() const {
329 // Non-fragile ABI is default for everything but i386.
330 return getTriple().getArch() != llvm::Triple::x86;
333 virtual bool UseObjCMixedDispatch() const {
334 // This is only used with the non-fragile ABI and non-legacy dispatch.
336 // Mixed dispatch is used everywhere except OS X before 10.6.
337 return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
339 virtual bool IsUnwindTablesDefault() const;
340 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
341 // Stack protectors default to on for user code on 10.5,
342 // and for everything in 10.6 and beyond
343 return isTargetIPhoneOS() ||
344 (!isMacosxVersionLT(10, 6) ||
345 (!isMacosxVersionLT(10, 5) && !KernelOrKext));
347 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
348 return ToolChain::RLT_CompilerRT;
350 virtual const char *GetDefaultRelocationModel() const;
351 virtual const char *GetForcedPicModel() const;
353 virtual bool SupportsProfiling() const;
355 virtual bool SupportsObjCGC() const;
357 virtual bool SupportsObjCARC() const;
359 virtual bool UseDwarfDebugFlags() const;
361 virtual bool UseSjLjExceptions() const;
366 /// DarwinClang - The Darwin toolchain used by Clang.
367 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
369 void AddGCCLibexecPath(unsigned darwinVersion);
372 DarwinClang(const Driver &D, const llvm::Triple& Triple);
374 /// @name Darwin ToolChain Implementation
377 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
378 ArgStringList &CmdArgs) const;
379 void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
380 const char *DarwinStaticLib) const;
382 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
383 ArgStringList &CmdArgs) const;
385 virtual void AddCCKextLibArgs(const ArgList &Args,
386 ArgStringList &CmdArgs) const;
388 virtual void AddLinkARCArgs(const ArgList &Args,
389 ArgStringList &CmdArgs) const;
393 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
394 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
396 Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
397 : Generic_GCC(D, Triple, Args) {}
399 std::string ComputeEffectiveClangTriple(const ArgList &Args,
400 types::ID InputType) const;
402 virtual const char *GetDefaultRelocationModel() const { return "pic"; }
405 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
406 virtual void anchor();
408 Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
409 : Generic_GCC(D, Triple, Args) {}
411 virtual bool IsIntegratedAssemblerDefault() const {
412 // Default integrated assembler to on for x86.
413 return (getTriple().getArch() == llvm::Triple::x86 ||
414 getTriple().getArch() == llvm::Triple::x86_64);
418 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
420 AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
422 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
423 const ActionList &Inputs) const;
426 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
428 Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
430 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
431 const ActionList &Inputs) const;
433 virtual bool IsIntegratedAssemblerDefault() const { return true; }
437 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
439 OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
441 virtual bool IsMathErrnoDefault() const { return false; }
442 virtual bool IsObjCNonFragileABIDefault() const { return true; }
444 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
445 const ActionList &Inputs) const;
448 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
450 Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
452 virtual bool IsMathErrnoDefault() const { return false; }
453 virtual bool IsObjCNonFragileABIDefault() const { return true; }
454 virtual bool IsObjCLegacyDispatchDefault() const { return false; }
456 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
457 const ActionList &Inputs) const;
459 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
460 ArgStringList &CC1Args) const;
461 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
462 ArgStringList &CmdArgs) const;
463 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
468 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
470 FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
472 virtual bool IsMathErrnoDefault() const { return false; }
473 virtual bool IsObjCNonFragileABIDefault() const { return true; }
475 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
476 const ActionList &Inputs) const;
479 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
481 NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
483 virtual bool IsMathErrnoDefault() const { return false; }
484 virtual bool IsObjCNonFragileABIDefault() const { return true; }
486 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
487 const ActionList &Inputs) const;
490 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
492 Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
494 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
495 const ActionList &Inputs) const;
498 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
500 DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
502 virtual bool IsMathErrnoDefault() const { return false; }
504 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
505 const ActionList &Inputs) const;
508 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
510 Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
512 virtual bool HasNativeLLVMSupport() const;
514 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
515 const ActionList &Inputs) const;
517 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
518 ArgStringList &CC1Args) const;
519 virtual void addClangTargetOptions(ArgStringList &CC1Args) const;
520 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
521 ArgStringList &CC1Args) const;
524 std::vector<std::string> ExtraOpts;
527 static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
528 const ArgList &DriverArgs,
529 ArgStringList &CC1Args);
533 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
534 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
535 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
537 TCEToolChain(const Driver &D, const llvm::Triple& Triple);
540 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
541 const ActionList &Inputs) const;
542 bool IsMathErrnoDefault() const;
543 bool IsUnwindTablesDefault() const;
544 const char* GetDefaultRelocationModel() const;
545 const char* GetForcedPicModel() const;
548 mutable llvm::DenseMap<unsigned, Tool*> Tools;
552 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
553 mutable llvm::DenseMap<unsigned, Tool*> Tools;
556 Windows(const Driver &D, const llvm::Triple& Triple);
558 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
559 const ActionList &Inputs) const;
561 virtual bool IsObjCDefaultSynthPropertiesDefault() const {
565 virtual bool IsIntegratedAssemblerDefault() const;
566 virtual bool IsUnwindTablesDefault() const;
567 virtual const char *GetDefaultRelocationModel() const;
568 virtual const char *GetForcedPicModel() const;
570 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
571 ArgStringList &CC1Args) const;
572 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
573 ArgStringList &CC1Args) const;
577 } // end namespace toolchains
578 } // end namespace driver
579 } // end namespace clang