]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/lib/Driver/ToolChains.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / lib / Driver / ToolChains.h
1 //===--- ToolChains.h - ToolChain Implementations ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef CLANG_LIB_DRIVER_TOOLCHAINS_H_
11 #define CLANG_LIB_DRIVER_TOOLCHAINS_H_
12
13 #include "Tools.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"
19
20 namespace clang {
21 namespace driver {
22 namespace toolchains {
23
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 {
28 protected:
29   /// \brief Struct to store and manipulate GCC versions.
30   ///
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.
36   ///
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.
40   ///
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.
44   struct GCCVersion {
45     /// \brief The unparsed text of the version.
46     std::string Text;
47
48     /// \brief The parsed major, minor, and patch numbers.
49     int Major, Minor, Patch;
50
51     /// \brief Any textual suffix on the patch number.
52     std::string PatchSuffix;
53
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); }
59   };
60
61
62   /// \brief This is a class to find a viable GCC installation for Clang to
63   /// use.
64   ///
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 {
69
70     bool IsValid;
71     llvm::Triple GCCTriple;
72
73     // FIXME: These might be better as path objects.
74     std::string GCCInstallPath;
75     std::string GCCMultiarchSuffix;
76     std::string GCCParentLibPath;
77
78     GCCVersion Version;
79
80   public:
81     GCCInstallationDetector(const Driver &D, const llvm::Triple &TargetTriple,
82                             const ArgList &Args);
83
84     /// \brief Check whether we detected a valid GCC install.
85     bool isValid() const { return IsValid; }
86
87     /// \brief Get the GCC triple for the detected install.
88     const llvm::Triple &getTriple() const { return GCCTriple; }
89
90     /// \brief Get the detected GCC installation path.
91     StringRef getInstallPath() const { return GCCInstallPath; }
92
93     /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
94     StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
95
96     /// \brief Get the detected GCC parent lib path.
97     StringRef getParentLibPath() const { return GCCParentLibPath; }
98
99     /// \brief Get the detected GCC version string.
100     const GCCVersion &getVersion() const { return Version; }
101
102   private:
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);
110
111     void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
112                                 const ArgList &Args,
113                                 const std::string &LibDir,
114                                 StringRef CandidateTriple,
115                                 bool NeedsMultiarchSuffix = false);
116   };
117
118   GCCInstallationDetector GCCInstallation;
119
120 public:
121   Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
122   ~Generic_GCC();
123
124   virtual bool IsUnwindTablesDefault() const;
125   virtual bool isPICDefault() const;
126   virtual bool isPIEDefault() const;
127   virtual bool isPICDefaultForced() const;
128
129 protected:
130   virtual Tool *getTool(Action::ActionClass AC) const;
131   virtual Tool *buildAssembler() const;
132   virtual Tool *buildLinker() const;
133
134   /// \name ToolChain Implementation Helper Functions
135   /// @{
136
137   /// \brief Check whether the target triple's architecture is 64-bits.
138   bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
139
140   /// \brief Check whether the target triple's architecture is 32-bits.
141   bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
142
143   /// @}
144
145 private:
146   mutable OwningPtr<tools::gcc::Preprocess> Preprocess;
147   mutable OwningPtr<tools::gcc::Precompile> Precompile;
148   mutable OwningPtr<tools::gcc::Compile> Compile;
149 };
150
151   /// Darwin - The base Darwin tool chain.
152 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
153 public:
154   /// The host version.
155   unsigned DarwinVersion[3];
156
157 protected:
158   virtual Tool *buildAssembler() const;
159   virtual Tool *buildLinker() const;
160   virtual Tool *getTool(Action::ActionClass AC) const;
161
162 private:
163   mutable OwningPtr<tools::darwin::Lipo> Lipo;
164   mutable OwningPtr<tools::darwin::Dsymutil> Dsymutil;
165   mutable OwningPtr<tools::darwin::VerifyDebug> VerifyDebug;
166
167   /// Whether the information on the target has been initialized.
168   //
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;
173
174   /// Whether we are targeting iPhoneOS target.
175   mutable bool TargetIsIPhoneOS;
176
177   /// Whether we are targeting the iPhoneOS simulator target.
178   mutable bool TargetIsIPhoneOSSimulator;
179
180   /// The OS version we are targeting.
181   mutable VersionTuple TargetVersion;
182
183 private:
184   /// The default macosx-version-min of this tool chain; empty until
185   /// initialized.
186   std::string MacosxVersionMin;
187
188   /// The default ios-version-min of this tool chain; empty until
189   /// initialized.
190   std::string iOSVersionMin;
191
192 private:
193   void AddDeploymentTarget(DerivedArgList &Args) const;
194
195 public:
196   Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
197   ~Darwin();
198
199   std::string ComputeEffectiveClangTriple(const ArgList &Args,
200                                           types::ID InputType) const;
201
202   /// @name Darwin Specific Toolchain API
203   /// {
204
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!");
210
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))
216       return;
217
218     assert(!TargetInitialized && "Target already initialized!");
219     TargetInitialized = true;
220     TargetIsIPhoneOS = IsIPhoneOS;
221     TargetIsIPhoneOSSimulator = IsIOSSim;
222     TargetVersion = VersionTuple(Major, Minor, Micro);
223   }
224
225   bool isTargetIPhoneOS() const {
226     assert(TargetInitialized && "Target not initialized!");
227     return TargetIsIPhoneOS;
228   }
229
230   bool isTargetIOSSimulator() const {
231     assert(TargetInitialized && "Target not initialized!");
232     return TargetIsIPhoneOSSimulator;
233   }
234
235   bool isTargetMacOS() const {
236     return !isTargetIOSSimulator() && !isTargetIPhoneOS();
237   }
238
239   bool isTargetInitialized() const { return TargetInitialized; }
240
241   VersionTuple getTargetVersion() const {
242     assert(TargetInitialized && "Target not initialized!");
243     return TargetVersion;
244   }
245
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;
250
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);
254   }
255
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);
259   }
260
261   /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
262   virtual void AddLinkARCArgs(const ArgList &Args,
263                               ArgStringList &CmdArgs) const = 0;
264   
265   /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
266   /// runtime library.
267   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
268                                      ArgStringList &CmdArgs) const = 0;
269   
270   /// }
271   /// @name ToolChain Implementation
272   /// {
273
274   virtual types::ID LookupTypeForExtension(const char *Ext) const;
275
276   virtual bool HasNativeLLVMSupport() const;
277
278   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
279   virtual bool hasBlocksRuntime() const;
280
281   virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
282                                         const char *BoundArch) const;
283
284   virtual bool IsBlocksDefault() const {
285     // Always allow blocks on Darwin; users interested in versioning are
286     // expected to use /usr/include/Blocks.h.
287     return true;
288   }
289   virtual bool IsIntegratedAssemblerDefault() const {
290 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
291     return false;
292 #else
293     // Default integrated assembler to on for Darwin.
294     return true;
295 #endif
296   }
297   virtual bool IsStrictAliasingDefault() const {
298 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
299     return false;
300 #else
301     return ToolChain::IsStrictAliasingDefault();
302 #endif
303   }
304
305   virtual bool IsMathErrnoDefault() const {
306     return false;
307   }
308
309   virtual bool IsEncodeExtendedBlockSignatureDefault() const {
310     return true;
311   }
312   
313   virtual bool IsObjCNonFragileABIDefault() const {
314     // Non-fragile ABI is default for everything but i386.
315     return getTriple().getArch() != llvm::Triple::x86;
316   }
317
318   virtual bool UseObjCMixedDispatch() const {
319     // This is only used with the non-fragile ABI and non-legacy dispatch.
320
321     // Mixed dispatch is used everywhere except OS X before 10.6.
322     return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
323   }
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));
331   }
332   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
333     return ToolChain::RLT_CompilerRT;
334   }
335   virtual bool isPICDefault() const;
336   virtual bool isPIEDefault() const;
337   virtual bool isPICDefaultForced() const;
338
339   virtual bool SupportsProfiling() const;
340
341   virtual bool SupportsObjCGC() const;
342
343   virtual void CheckObjCARC() const;
344
345   virtual bool UseDwarfDebugFlags() const;
346
347   virtual bool UseSjLjExceptions() const;
348
349   /// }
350 };
351
352 /// DarwinClang - The Darwin toolchain used by Clang.
353 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
354 public:
355   DarwinClang(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
356
357   /// @name Darwin ToolChain Implementation
358   /// {
359
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;
365
366   virtual void AddCXXStdlibLibArgs(const ArgList &Args,
367                                    ArgStringList &CmdArgs) const;
368
369   virtual void AddCCKextLibArgs(const ArgList &Args,
370                                 ArgStringList &CmdArgs) const;
371
372   virtual void AddLinkARCArgs(const ArgList &Args,
373                               ArgStringList &CmdArgs) const;
374   /// }
375 };
376
377 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
378 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
379 public:
380   Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
381     : Generic_GCC(D, Triple, Args) {}
382
383   std::string ComputeEffectiveClangTriple(const ArgList &Args,
384                                           types::ID InputType) const;
385
386   virtual bool isPICDefault() const { return false; }
387 };
388
389 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
390   virtual void anchor();
391 public:
392   Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
393     : Generic_GCC(D, Triple, Args) {}
394
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);
400   }
401 };
402
403 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
404 public:
405   AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
406
407 protected:
408   virtual Tool *buildAssembler() const;
409   virtual Tool *buildLinker() const;
410 };
411
412 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
413 public:
414   Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
415
416   virtual bool IsIntegratedAssemblerDefault() const { return true; }
417 protected:
418   virtual Tool *buildAssembler() const;
419   virtual Tool *buildLinker() const;
420
421 };
422
423
424 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
425 public:
426   OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
427
428   virtual bool IsMathErrnoDefault() const { return false; }
429   virtual bool IsObjCNonFragileABIDefault() const { return true; }
430
431 protected:
432   virtual Tool *buildAssembler() const;
433   virtual Tool *buildLinker() const;
434 };
435
436 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
437 public:
438   Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
439
440   virtual bool IsMathErrnoDefault() const { return false; }
441   virtual bool IsObjCNonFragileABIDefault() const { return true; }
442   virtual bool IsObjCLegacyDispatchDefault() const { return false; }
443
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 {
449      return 1;
450   }
451
452 protected:
453   virtual Tool *buildAssembler() const;
454   virtual Tool *buildLinker() const;
455 };
456
457 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
458 public:
459   FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
460
461   virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
462
463   virtual bool IsMathErrnoDefault() const { return false; }
464   virtual bool IsObjCNonFragileABIDefault() const { return true; }
465
466   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
467                                             ArgStringList &CC1Args) const;
468
469   virtual bool UseSjLjExceptions() const;
470 protected:
471   virtual Tool *buildAssembler() const;
472   virtual Tool *buildLinker() const;
473 };
474
475 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
476 public:
477   NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
478
479   virtual bool IsMathErrnoDefault() const { return false; }
480   virtual bool IsObjCNonFragileABIDefault() const { return true; }
481
482   virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
483
484   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
485                                             ArgStringList &CC1Args) const;
486
487 protected:
488   virtual Tool *buildAssembler() const;
489   virtual Tool *buildLinker() const;
490 };
491
492 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
493 public:
494   Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
495
496 protected:
497   virtual Tool *buildAssembler() const;
498   virtual Tool *buildLinker() const;
499 };
500
501 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
502 public:
503   DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
504
505   virtual bool IsMathErrnoDefault() const { return false; }
506
507 protected:
508   virtual Tool *buildAssembler() const;
509   virtual Tool *buildLinker() const;
510 };
511
512 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
513 public:
514   Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
515
516   virtual bool HasNativeLLVMSupport() const;
517
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;
525
526   std::string Linker;
527   std::vector<std::string> ExtraOpts;
528   bool IsPIEDefault;
529
530 protected:
531   virtual Tool *buildAssembler() const;
532   virtual Tool *buildLinker() const;
533
534 private:
535   static bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
536                                        Twine TargetArchDir,
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);
543
544   std::string computeSysRoot(const ArgList &Args) const;
545 };
546
547 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public Linux {
548 protected:
549   GCCVersion GCCLibAndIncVersion;
550   virtual Tool *buildAssembler() const;
551   virtual Tool *buildLinker() const;
552
553 public:
554   Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
555              const ArgList &Args);
556   ~Hexagon_TC();
557
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;
563
564   StringRef GetGCCLibAndIncVersion() const { return GCCLibAndIncVersion.Text; }
565
566   static std::string GetGnuDir(const std::string &InstalledDir);
567
568   static StringRef GetTargetCPU(const ArgList &Args);
569 };
570
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 {
574 public:
575   TCEToolChain(const Driver &D, const llvm::Triple& Triple,
576                const ArgList &Args);
577   ~TCEToolChain();
578
579   bool IsMathErrnoDefault() const;
580   bool isPICDefault() const;
581   bool isPIEDefault() const;
582   bool isPICDefaultForced() const;
583 };
584
585 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
586 public:
587   Windows(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
588
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;
594
595   virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
596                                          ArgStringList &CC1Args) const;
597   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
598                                             ArgStringList &CC1Args) const;
599 protected:
600   virtual Tool *buildLinker() const;
601   virtual Tool *buildAssembler() const;
602 };
603
604 } // end namespace toolchains
605 } // end namespace driver
606 } // end namespace clang
607
608 #endif