]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Driver/ToolChains.h
MFC r244628:
[FreeBSD/stable/9.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 "clang/Driver/Action.h"
14 #include "clang/Driver/ToolChain.h"
15
16 #include "clang/Basic/VersionTuple.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/Support/Compiler.h"
19
20 #include "Tools.h"
21
22 namespace clang {
23 namespace driver {
24 namespace toolchains {
25
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 {
30 protected:
31   /// \brief Struct to store and manipulate GCC versions.
32   ///
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.
38   ///
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.
42   ///
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.
46   struct GCCVersion {
47     /// \brief The unparsed text of the version.
48     std::string Text;
49
50     /// \brief The parsed major, minor, and patch numbers.
51     int Major, Minor, Patch;
52
53     /// \brief Any textual suffix on the patch number.
54     std::string PatchSuffix;
55
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); }
61   };
62
63
64   /// \brief This is a class to find a viable GCC installation for Clang to
65   /// use.
66   ///
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 {
71
72     bool IsValid;
73     llvm::Triple GCCTriple;
74
75     // FIXME: These might be better as path objects.
76     std::string GCCInstallPath;
77     std::string GCCMultiarchSuffix;
78     std::string GCCParentLibPath;
79
80     GCCVersion Version;
81
82   public:
83     GCCInstallationDetector(const Driver &D, const llvm::Triple &TargetTriple,
84                             const ArgList &Args);
85
86     /// \brief Check whether we detected a valid GCC install.
87     bool isValid() const { return IsValid; }
88
89     /// \brief Get the GCC triple for the detected install.
90     const llvm::Triple &getTriple() const { return GCCTriple; }
91
92     /// \brief Get the detected GCC installation path.
93     StringRef getInstallPath() const { return GCCInstallPath; }
94
95     /// \brief Get the detected GCC installation path suffix for multiarch GCCs.
96     StringRef getMultiarchSuffix() const { return GCCMultiarchSuffix; }
97
98     /// \brief Get the detected GCC parent lib path.
99     StringRef getParentLibPath() const { return GCCParentLibPath; }
100
101     /// \brief Get the detected GCC version string.
102     const GCCVersion &getVersion() const { return Version; }
103
104   private:
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);
112
113     void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch,
114                                 const ArgList &Args,
115                                 const std::string &LibDir,
116                                 StringRef CandidateTriple,
117                                 bool NeedsMultiarchSuffix = false);
118   };
119
120   GCCInstallationDetector GCCInstallation;
121
122   mutable llvm::DenseMap<unsigned, Tool*> Tools;
123
124 public:
125   Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
126   ~Generic_GCC();
127
128   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
129                            const ActionList &Inputs) const;
130
131   virtual bool IsUnwindTablesDefault() const;
132   virtual bool isPICDefault() const;
133   virtual bool isPICDefaultForced() const;
134
135 protected:
136   /// \name ToolChain Implementation Helper Functions
137   /// @{
138
139   /// \brief Check whether the target triple's architecture is 64-bits.
140   bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
141
142   /// \brief Check whether the target triple's architecture is 32-bits.
143   bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
144
145   /// @}
146 };
147
148 class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public ToolChain {
149 protected:
150   mutable llvm::DenseMap<unsigned, Tool*> Tools;
151
152 public:
153   Hexagon_TC(const Driver &D, const llvm::Triple& Triple);
154   ~Hexagon_TC();
155
156   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
157                            const ActionList &Inputs) const;
158
159   virtual bool isPICDefault() const;
160   virtual bool isPICDefaultForced() const;
161 };
162
163   /// Darwin - The base Darwin tool chain.
164 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
165 public:
166   /// The host version.
167   unsigned DarwinVersion[3];
168
169 private:
170   mutable llvm::DenseMap<unsigned, Tool*> Tools;
171
172   /// Whether the information on the target has been initialized.
173   //
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;
178
179   /// Whether we are targeting iPhoneOS target.
180   mutable bool TargetIsIPhoneOS;
181
182   /// Whether we are targeting the iPhoneOS simulator target.
183   mutable bool TargetIsIPhoneOSSimulator;
184
185   /// The OS version we are targeting.
186   mutable VersionTuple TargetVersion;
187
188 private:
189   /// The default macosx-version-min of this tool chain; empty until
190   /// initialized.
191   std::string MacosxVersionMin;
192
193   /// The default ios-version-min of this tool chain; empty until
194   /// initialized.
195   std::string iOSVersionMin;
196
197 private:
198   void AddDeploymentTarget(DerivedArgList &Args) const;
199
200 public:
201   Darwin(const Driver &D, const llvm::Triple& Triple);
202   ~Darwin();
203
204   std::string ComputeEffectiveClangTriple(const ArgList &Args,
205                                           types::ID InputType) const;
206
207   /// @name Darwin Specific Toolchain API
208   /// {
209
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!");
215
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))
221       return;
222
223     assert(!TargetInitialized && "Target already initialized!");
224     TargetInitialized = true;
225     TargetIsIPhoneOS = IsIPhoneOS;
226     TargetIsIPhoneOSSimulator = IsIOSSim;
227     TargetVersion = VersionTuple(Major, Minor, Micro);
228   }
229
230   bool isTargetIPhoneOS() const {
231     assert(TargetInitialized && "Target not initialized!");
232     return TargetIsIPhoneOS;
233   }
234
235   bool isTargetIOSSimulator() const {
236     assert(TargetInitialized && "Target not initialized!");
237     return TargetIsIPhoneOSSimulator;
238   }
239
240   bool isTargetMacOS() const {
241     return !isTargetIOSSimulator() && !isTargetIPhoneOS();
242   }
243
244   bool isTargetInitialized() const { return TargetInitialized; }
245
246   VersionTuple getTargetVersion() const {
247     assert(TargetInitialized && "Target not initialized!");
248     return TargetVersion;
249   }
250
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;
255
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);
259   }
260
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);
264   }
265
266   /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
267   virtual void AddLinkARCArgs(const ArgList &Args,
268                               ArgStringList &CmdArgs) const = 0;
269   
270   /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
271   /// runtime library.
272   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
273                                      ArgStringList &CmdArgs) const = 0;
274   
275   /// }
276   /// @name ToolChain Implementation
277   /// {
278
279   virtual types::ID LookupTypeForExtension(const char *Ext) const;
280
281   virtual bool HasNativeLLVMSupport() const;
282
283   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
284   virtual bool hasBlocksRuntime() const;
285
286   virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
287                                         const char *BoundArch) const;
288
289   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
290                            const ActionList &Inputs) const;
291
292   virtual bool IsBlocksDefault() const {
293     // Always allow blocks on Darwin; users interested in versioning are
294     // expected to use /usr/include/Blocks.h.
295     return true;
296   }
297   virtual bool IsIntegratedAssemblerDefault() const {
298 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
299     return false;
300 #else
301     // Default integrated assembler to on for Darwin.
302     return true;
303 #endif
304   }
305   virtual bool IsStrictAliasingDefault() const {
306 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
307     return false;
308 #else
309     return ToolChain::IsStrictAliasingDefault();
310 #endif
311   }
312
313   virtual bool IsMathErrnoDefault() const {
314     return false;
315   }
316
317   virtual bool IsObjCDefaultSynthPropertiesDefault() const {
318     return true;
319   }
320
321   virtual bool IsEncodeExtendedBlockSignatureDefault() const {
322     return true;
323   }
324   
325   virtual bool IsObjCNonFragileABIDefault() const {
326     // Non-fragile ABI is default for everything but i386.
327     return getTriple().getArch() != llvm::Triple::x86;
328   }
329
330   virtual bool UseObjCMixedDispatch() const {
331     // This is only used with the non-fragile ABI and non-legacy dispatch.
332
333     // Mixed dispatch is used everywhere except OS X before 10.6.
334     return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
335   }
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));
343   }
344   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
345     return ToolChain::RLT_CompilerRT;
346   }
347   virtual bool isPICDefault() const;
348   virtual bool isPICDefaultForced() const;
349
350   virtual bool SupportsProfiling() const;
351
352   virtual bool SupportsObjCGC() const;
353
354   virtual void CheckObjCARC() const;
355
356   virtual bool UseDwarfDebugFlags() const;
357
358   virtual bool UseSjLjExceptions() const;
359
360   /// }
361 };
362
363 /// DarwinClang - The Darwin toolchain used by Clang.
364 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
365 public:
366   DarwinClang(const Driver &D, const llvm::Triple& Triple);
367
368   /// @name Darwin ToolChain Implementation
369   /// {
370
371   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
372                                      ArgStringList &CmdArgs) const;
373   void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, 
374                          const char *DarwinStaticLib) const;
375   
376   virtual void AddCXXStdlibLibArgs(const ArgList &Args,
377                                    ArgStringList &CmdArgs) const;
378
379   virtual void AddCCKextLibArgs(const ArgList &Args,
380                                 ArgStringList &CmdArgs) const;
381
382   virtual void AddLinkARCArgs(const ArgList &Args,
383                               ArgStringList &CmdArgs) const;
384   /// }
385 };
386
387 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
388 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
389 public:
390   Darwin_Generic_GCC(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
391     : Generic_GCC(D, Triple, Args) {}
392
393   std::string ComputeEffectiveClangTriple(const ArgList &Args,
394                                           types::ID InputType) const;
395
396   virtual bool isPICDefault() const { return false; };
397 };
398
399 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
400   virtual void anchor();
401 public:
402   Generic_ELF(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
403     : Generic_GCC(D, Triple, Args) {}
404
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);
409   }
410 };
411
412 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
413 public:
414   AuroraUX(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
415
416   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
417                            const ActionList &Inputs) const;
418 };
419
420 class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_GCC {
421 public:
422   Solaris(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
423
424   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
425                            const ActionList &Inputs) const;
426
427   virtual bool IsIntegratedAssemblerDefault() const { return true; }
428 };
429
430
431 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
432 public:
433   OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
434
435   virtual bool IsMathErrnoDefault() const { return false; }
436   virtual bool IsObjCNonFragileABIDefault() const { return true; }
437
438   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
439                            const ActionList &Inputs) const;
440 };
441
442 class LLVM_LIBRARY_VISIBILITY Bitrig : public Generic_ELF {
443 public:
444   Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
445
446   virtual bool IsMathErrnoDefault() const { return false; }
447   virtual bool IsObjCNonFragileABIDefault() const { return true; }
448   virtual bool IsObjCLegacyDispatchDefault() const { return false; }
449
450   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
451                            const ActionList &Inputs) const;
452
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 {
458      return 1;
459   }
460 };
461
462 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
463 public:
464   FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
465
466   virtual bool IsMathErrnoDefault() const { return false; }
467   virtual bool IsObjCNonFragileABIDefault() const { return true; }
468
469   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
470                            const ActionList &Inputs) const;
471   virtual bool UseSjLjExceptions() const;
472 };
473
474 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
475 public:
476   NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
477
478   virtual bool IsMathErrnoDefault() const { return false; }
479   virtual bool IsObjCNonFragileABIDefault() const { return true; }
480
481   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
482                            const ActionList &Inputs) const;
483 };
484
485 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
486 public:
487   Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
488
489   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
490                            const ActionList &Inputs) const;
491 };
492
493 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
494 public:
495   DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
496
497   virtual bool IsMathErrnoDefault() const { return false; }
498
499   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
500                            const ActionList &Inputs) const;
501 };
502
503 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
504 public:
505   Linux(const Driver &D, const llvm::Triple& Triple, const ArgList &Args);
506
507   virtual bool HasNativeLLVMSupport() const;
508
509   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
510                            const ActionList &Inputs) const;
511
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;
517
518   std::string Linker;
519   std::vector<std::string> ExtraOpts;
520
521 private:
522   static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
523                                        const ArgList &DriverArgs,
524                                        ArgStringList &CC1Args);
525 };
526
527
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 {
531 public:
532   TCEToolChain(const Driver &D, const llvm::Triple& Triple);
533   ~TCEToolChain();
534
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;
540
541 private:
542   mutable llvm::DenseMap<unsigned, Tool*> Tools;
543
544 };
545
546 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
547   mutable llvm::DenseMap<unsigned, Tool*> Tools;
548
549 public:
550   Windows(const Driver &D, const llvm::Triple& Triple);
551
552   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
553                            const ActionList &Inputs) const;
554
555   virtual bool IsObjCDefaultSynthPropertiesDefault() const {
556     return true;
557   }
558
559   virtual bool IsIntegratedAssemblerDefault() const;
560   virtual bool IsUnwindTablesDefault() const;
561   virtual bool isPICDefault() const;
562   virtual bool isPICDefaultForced() const;
563
564   virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
565                                          ArgStringList &CC1Args) const;
566   virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
567                                             ArgStringList &CC1Args) const;
568
569 };
570
571 } // end namespace toolchains
572 } // end namespace driver
573 } // end namespace clang
574
575 #endif