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,
115 const std::string &LibDir,
116 StringRef CandidateTriple,
117 bool NeedsMultiarchSuffix = false);
120 GCCInstallationDetector GCCInstallation;
122 mutable llvm::DenseMap<unsigned, Tool*> Tools;
125 Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
128 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
129 const ActionList &Inputs) const;
131 virtual bool IsUnwindTablesDefault() const;
132 virtual bool isPICDefault() const;
133 virtual bool isPICDefaultForced() const;
136 /// \name ToolChain Implementation Helper Functions
139 /// \brief Check whether the target triple's architecture is 64-bits.
140 bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
142 /// \brief Check whether the target triple's architecture is 32-bits.
143 bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
148 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public ToolChain {
150 mutable llvm::DenseMap<unsigned, Tool*> Tools;
153 Hexagon_TC(const Driver &D, const llvm::Triple& Triple);
156 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
157 const ActionList &Inputs) const;
159 virtual bool isPICDefault() const;
160 virtual bool isPICDefaultForced() 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 /// The default macosx-version-min of this tool chain; empty until
191 std::string MacosxVersionMin;
193 /// The default ios-version-min of this tool chain; empty until
195 std::string iOSVersionMin;
198 void AddDeploymentTarget(DerivedArgList &Args) const;
201 Darwin(const Driver &D, const llvm::Triple& Triple);
204 std::string ComputeEffectiveClangTriple(const ArgList &Args,
205 types::ID InputType) const;
207 /// @name Darwin Specific Toolchain API
210 // FIXME: Eliminate these ...Target functions and derive separate tool chains
211 // for these targets and put version in constructor.
212 void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
213 unsigned Micro, bool IsIOSSim) const {
214 assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
216 // FIXME: For now, allow reinitialization as long as values don't
217 // change. This will go away when we move away from argument translation.
218 if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
219 TargetIsIPhoneOSSimulator == IsIOSSim &&
220 TargetVersion == VersionTuple(Major, Minor, Micro))
223 assert(!TargetInitialized && "Target already initialized!");
224 TargetInitialized = true;
225 TargetIsIPhoneOS = IsIPhoneOS;
226 TargetIsIPhoneOSSimulator = IsIOSSim;
227 TargetVersion = VersionTuple(Major, Minor, Micro);
230 bool isTargetIPhoneOS() const {
231 assert(TargetInitialized && "Target not initialized!");
232 return TargetIsIPhoneOS;
235 bool isTargetIOSSimulator() const {
236 assert(TargetInitialized && "Target not initialized!");
237 return TargetIsIPhoneOSSimulator;
240 bool isTargetMacOS() const {
241 return !isTargetIOSSimulator() && !isTargetIPhoneOS();
244 bool isTargetInitialized() const { return TargetInitialized; }
246 VersionTuple getTargetVersion() const {
247 assert(TargetInitialized && "Target not initialized!");
248 return TargetVersion;
251 /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
252 /// invocation. For example, Darwin treats different ARM variations as
253 /// distinct architectures.
254 StringRef getDarwinArchName(const ArgList &Args) const;
256 bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
257 assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
258 return TargetVersion < VersionTuple(V0, V1, V2);
261 bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
262 assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
263 return TargetVersion < VersionTuple(V0, V1, V2);
266 /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
267 virtual void AddLinkARCArgs(const ArgList &Args,
268 ArgStringList &CmdArgs) const = 0;
270 /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
272 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
273 ArgStringList &CmdArgs) const = 0;
276 /// @name ToolChain Implementation
279 virtual types::ID LookupTypeForExtension(const char *Ext) const;
281 virtual bool HasNativeLLVMSupport() const;
283 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
284 virtual bool hasBlocksRuntime() const;
286 virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
287 const char *BoundArch) const;
289 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
290 const ActionList &Inputs) const;
292 virtual bool IsBlocksDefault() const {
293 // Always allow blocks on Darwin; users interested in versioning are
294 // expected to use /usr/include/Blocks.h.
297 virtual bool IsIntegratedAssemblerDefault() const {
298 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
301 // Default integrated assembler to on for Darwin.
305 virtual bool IsStrictAliasingDefault() const {
306 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
309 return ToolChain::IsStrictAliasingDefault();
313 virtual bool IsMathErrnoDefault() const {
317 virtual bool IsObjCDefaultSynthPropertiesDefault() const {
321 virtual bool IsEncodeExtendedBlockSignatureDefault() const {
325 virtual bool IsObjCNonFragileABIDefault() const {
326 // Non-fragile ABI is default for everything but i386.
327 return getTriple().getArch() != llvm::Triple::x86;
330 virtual bool UseObjCMixedDispatch() const {
331 // This is only used with the non-fragile ABI and non-legacy dispatch.
333 // Mixed dispatch is used everywhere except OS X before 10.6.
334 return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
336 virtual bool IsUnwindTablesDefault() const;
337 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
338 // Stack protectors default to on for user code on 10.5,
339 // and for everything in 10.6 and beyond
340 return isTargetIPhoneOS() ||
341 (!isMacosxVersionLT(10, 6) ||
342 (!isMacosxVersionLT(10, 5) && !KernelOrKext));
344 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
345 return ToolChain::RLT_CompilerRT;
347 virtual bool isPICDefault() const;
348 virtual bool isPICDefaultForced() const;
350 virtual bool SupportsProfiling() const;
352 virtual bool SupportsObjCGC() const;
354 virtual void CheckObjCARC() const;
356 virtual bool UseDwarfDebugFlags() const;
358 virtual bool UseSjLjExceptions() const;
363 /// DarwinClang - The Darwin toolchain used by Clang.
364 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
366 DarwinClang(const Driver &D, const llvm::Triple& Triple);
368 /// @name Darwin ToolChain Implementation
371 virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
372 ArgStringList &CmdArgs) const;
373 void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
374 const char *DarwinStaticLib) const;
376 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
377 ArgStringList &CmdArgs) const;
379 virtual void AddCCKextLibArgs(const ArgList &Args,
380 ArgStringList &CmdArgs) const;
382 virtual void AddLinkARCArgs(const ArgList &Args,
383 ArgStringList &CmdArgs) const;
387 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
388 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
390 Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
391 : Generic_GCC(D, Triple, Args) {}
393 std::string ComputeEffectiveClangTriple(const ArgList &Args,
394 types::ID InputType) const;
396 virtual bool isPICDefault() const { return false; };
399 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
400 virtual void anchor();
402 Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
403 : Generic_GCC(D, Triple, Args) {}
405 virtual bool IsIntegratedAssemblerDefault() const {
406 // Default integrated assembler to on for x86.
407 return (getTriple().getArch() == llvm::Triple::x86 ||
408 getTriple().getArch() == llvm::Triple::x86_64);
412 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
414 AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
416 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
417 const ActionList &Inputs) const;
420 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
422 Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
424 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
425 const ActionList &Inputs) const;
427 virtual bool IsIntegratedAssemblerDefault() const { return true; }
431 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
433 OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
435 virtual bool IsMathErrnoDefault() const { return false; }
436 virtual bool IsObjCNonFragileABIDefault() const { return true; }
438 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
439 const ActionList &Inputs) const;
442 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
444 Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
446 virtual bool IsMathErrnoDefault() const { return false; }
447 virtual bool IsObjCNonFragileABIDefault() const { return true; }
448 virtual bool IsObjCLegacyDispatchDefault() const { return false; }
450 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
451 const ActionList &Inputs) const;
453 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
454 ArgStringList &CC1Args) const;
455 virtual void AddCXXStdlibLibArgs(const ArgList &Args,
456 ArgStringList &CmdArgs) const;
457 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
462 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
464 FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
466 virtual bool IsMathErrnoDefault() const { return false; }
467 virtual bool IsObjCNonFragileABIDefault() const { return true; }
469 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
470 const ActionList &Inputs) const;
471 virtual bool UseSjLjExceptions() const;
474 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
476 NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
478 virtual bool IsMathErrnoDefault() const { return false; }
479 virtual bool IsObjCNonFragileABIDefault() const { return true; }
481 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
482 const ActionList &Inputs) const;
485 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
487 Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
489 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
490 const ActionList &Inputs) const;
493 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
495 DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
497 virtual bool IsMathErrnoDefault() const { return false; }
499 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
500 const ActionList &Inputs) const;
503 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
505 Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
507 virtual bool HasNativeLLVMSupport() const;
509 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
510 const ActionList &Inputs) const;
512 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
513 ArgStringList &CC1Args) const;
514 virtual void addClangTargetOptions(ArgStringList &CC1Args) const;
515 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
516 ArgStringList &CC1Args) const;
519 std::vector<std::string> ExtraOpts;
522 static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
523 const ArgList &DriverArgs,
524 ArgStringList &CC1Args);
528 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
529 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
530 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
532 TCEToolChain(const Driver &D, const llvm::Triple& Triple);
535 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
536 const ActionList &Inputs) const;
537 bool IsMathErrnoDefault() const;
538 bool isPICDefault() const;
539 bool isPICDefaultForced() const;
542 mutable llvm::DenseMap<unsigned, Tool*> Tools;
546 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
547 mutable llvm::DenseMap<unsigned, Tool*> Tools;
550 Windows(const Driver &D, const llvm::Triple& Triple);
552 virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
553 const ActionList &Inputs) const;
555 virtual bool IsObjCDefaultSynthPropertiesDefault() const {
559 virtual bool IsIntegratedAssemblerDefault() const;
560 virtual bool IsUnwindTablesDefault() const;
561 virtual bool isPICDefault() const;
562 virtual bool isPICDefaultForced() const;
564 virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
565 ArgStringList &CC1Args) const;
566 virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
567 ArgStringList &CC1Args) const;
571 } // end namespace toolchains
572 } // end namespace driver
573 } // end namespace clang