• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llvm/ADT/DenseMap.h"
17 #include "llvm/Support/Compiler.h"
18 
19 #include "Tools.h"
20 
21 namespace clang {
22 namespace driver {
23 namespace toolchains {
24 
25 /// Generic_GCC - A tool chain using the 'gcc' command to perform
26 /// all subcommands; this relies on gcc translating the majority of
27 /// command line options.
28 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
29 protected:
30   mutable llvm::DenseMap<unsigned, Tool*> Tools;
31 
32 public:
33   Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple);
34   ~Generic_GCC();
35 
36   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
37                            const ActionList &Inputs) const;
38 
39   virtual bool IsUnwindTablesDefault() const;
40   virtual const char *GetDefaultRelocationModel() const;
41   virtual const char *GetForcedPicModel() const;
42 };
43 
44 /// Darwin - The base Darwin tool chain.
45 class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
46 public:
47   /// The host version.
48   unsigned DarwinVersion[3];
49 
50 private:
51   mutable llvm::DenseMap<unsigned, Tool*> Tools;
52 
53   /// Whether the information on the target has been initialized.
54   //
55   // FIXME: This should be eliminated. What we want to do is make this part of
56   // the "default target for arguments" selection process, once we get out of
57   // the argument translation business.
58   mutable bool TargetInitialized;
59 
60   // FIXME: Remove this once there is a proper way to detect an ARC runtime
61   // for the simulator.
62  public:
63   mutable enum {
64     ARCSimulator_None,
65     ARCSimulator_HasARCRuntime,
66     ARCSimulator_NoARCRuntime
67   } ARCRuntimeForSimulator;
68 
69 private:
70   /// Whether we are targeting iPhoneOS target.
71   mutable bool TargetIsIPhoneOS;
72 
73   /// Whether we are targeting the iPhoneOS simulator target.
74   mutable bool TargetIsIPhoneOSSimulator;
75 
76   /// The OS version we are targeting.
77   mutable unsigned TargetVersion[3];
78 
79   /// The default macosx-version-min of this tool chain; empty until
80   /// initialized.
81   std::string MacosxVersionMin;
82 
83   bool hasARCRuntime() const;
84 
85 private:
86   void AddDeploymentTarget(DerivedArgList &Args) const;
87 
88 public:
89   Darwin(const HostInfo &Host, const llvm::Triple& Triple);
90   ~Darwin();
91 
92   std::string ComputeEffectiveClangTriple(const ArgList &Args) const;
93 
94   /// @name Darwin Specific Toolchain API
95   /// {
96 
97   // FIXME: Eliminate these ...Target functions and derive separate tool chains
98   // for these targets and put version in constructor.
setTarget(bool IsIPhoneOS,unsigned Major,unsigned Minor,unsigned Micro,bool IsIOSSim)99   void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
100                  unsigned Micro, bool IsIOSSim) const {
101     assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
102 
103     // FIXME: For now, allow reinitialization as long as values don't
104     // change. This will go away when we move away from argument translation.
105     if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
106         TargetIsIPhoneOSSimulator == IsIOSSim &&
107         TargetVersion[0] == Major && TargetVersion[1] == Minor &&
108         TargetVersion[2] == Micro)
109       return;
110 
111     assert(!TargetInitialized && "Target already initialized!");
112     TargetInitialized = true;
113     TargetIsIPhoneOS = IsIPhoneOS;
114     TargetIsIPhoneOSSimulator = IsIOSSim;
115     TargetVersion[0] = Major;
116     TargetVersion[1] = Minor;
117     TargetVersion[2] = Micro;
118   }
119 
isTargetIPhoneOS()120   bool isTargetIPhoneOS() const {
121     assert(TargetInitialized && "Target not initialized!");
122     return TargetIsIPhoneOS;
123   }
124 
isTargetIOSSimulator()125   bool isTargetIOSSimulator() const {
126     assert(TargetInitialized && "Target not initialized!");
127     return TargetIsIPhoneOSSimulator;
128   }
129 
isTargetInitialized()130   bool isTargetInitialized() const { return TargetInitialized; }
131 
getTargetVersion(unsigned (& Res)[3])132   void getTargetVersion(unsigned (&Res)[3]) const {
133     assert(TargetInitialized && "Target not initialized!");
134     Res[0] = TargetVersion[0];
135     Res[1] = TargetVersion[1];
136     Res[2] = TargetVersion[2];
137   }
138 
139   /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
140   /// invocation. For example, Darwin treats different ARM variations as
141   /// distinct architectures.
142   llvm::StringRef getDarwinArchName(const ArgList &Args) const;
143 
isVersionLT(unsigned (& A)[3],unsigned (& B)[3])144   static bool isVersionLT(unsigned (&A)[3], unsigned (&B)[3]) {
145     for (unsigned i=0; i < 3; ++i) {
146       if (A[i] > B[i]) return false;
147       if (A[i] < B[i]) return true;
148     }
149     return false;
150   }
151 
152   bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
153     assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
154     unsigned B[3] = { V0, V1, V2 };
155     return isVersionLT(TargetVersion, B);
156   }
157 
158   bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
159     assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
160     unsigned B[3] = { V0, V1, V2 };
161     return isVersionLT(TargetVersion, B);
162   }
163 
164   /// AddLinkSearchPathArgs - Add the linker search paths to \arg CmdArgs.
165   ///
166   /// \param Args - The input argument list.
167   /// \param CmdArgs [out] - The command argument list to append the paths
168   /// (prefixed by -L) to.
169   virtual void AddLinkSearchPathArgs(const ArgList &Args,
170                                      ArgStringList &CmdArgs) const = 0;
171 
172   /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
173   virtual void AddLinkARCArgs(const ArgList &Args,
174                               ArgStringList &CmdArgs) const = 0;
175 
176   /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
177   /// runtime library.
178   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
179                                      ArgStringList &CmdArgs) const = 0;
180 
181   /// }
182   /// @name ToolChain Implementation
183   /// {
184 
185   virtual types::ID LookupTypeForExtension(const char *Ext) const;
186 
187   virtual bool HasNativeLLVMSupport() const;
188 
189   virtual void configureObjCRuntime(ObjCRuntime &runtime) const;
190 
191   virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
192                                         const char *BoundArch) const;
193 
194   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
195                            const ActionList &Inputs) const;
196 
IsBlocksDefault()197   virtual bool IsBlocksDefault() const {
198     // Always allow blocks on Darwin; users interested in versioning are
199     // expected to use /usr/include/Blocks.h.
200     return true;
201   }
IsIntegratedAssemblerDefault()202   virtual bool IsIntegratedAssemblerDefault() const {
203 #ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
204     return false;
205 #else
206     // Default integrated assembler to on for x86.
207     return (getTriple().getArch() == llvm::Triple::x86 ||
208             getTriple().getArch() == llvm::Triple::x86_64);
209 #endif
210   }
IsStrictAliasingDefault()211   virtual bool IsStrictAliasingDefault() const {
212 #ifdef DISABLE_DEFAULT_STRICT_ALIASING
213     return false;
214 #else
215     return ToolChain::IsStrictAliasingDefault();
216 #endif
217   }
218 
IsObjCDefaultSynthPropertiesDefault()219   virtual bool IsObjCDefaultSynthPropertiesDefault() const {
220     return false;
221   }
222 
IsObjCNonFragileABIDefault()223   virtual bool IsObjCNonFragileABIDefault() const {
224     // Non-fragile ABI is default for everything but i386.
225     return getTriple().getArch() != llvm::Triple::x86;
226   }
IsObjCLegacyDispatchDefault()227   virtual bool IsObjCLegacyDispatchDefault() const {
228     // This is only used with the non-fragile ABI.
229 
230     // Legacy dispatch is used everywhere except on x86_64.
231     return getTriple().getArch() != llvm::Triple::x86_64;
232   }
UseObjCMixedDispatch()233   virtual bool UseObjCMixedDispatch() const {
234     // This is only used with the non-fragile ABI and non-legacy dispatch.
235 
236     // Mixed dispatch is used everywhere except OS X before 10.6.
237     return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
238   }
239   virtual bool IsUnwindTablesDefault() const;
GetDefaultStackProtectorLevel()240   virtual unsigned GetDefaultStackProtectorLevel() const {
241     // Stack protectors default to on for 10.6 and beyond.
242     return !isTargetIPhoneOS() && !isMacosxVersionLT(10, 6);
243   }
244   virtual const char *GetDefaultRelocationModel() const;
245   virtual const char *GetForcedPicModel() const;
246 
247   virtual bool SupportsProfiling() const;
248 
249   virtual bool SupportsObjCGC() const;
250 
251   virtual bool UseDwarfDebugFlags() const;
252 
253   virtual bool UseSjLjExceptions() const;
254 
255   /// }
256 };
257 
258 /// DarwinClang - The Darwin toolchain used by Clang.
259 class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
260 public:
261   DarwinClang(const HostInfo &Host, const llvm::Triple& Triple);
262 
263   /// @name Darwin ToolChain Implementation
264   /// {
265 
266   virtual void AddLinkSearchPathArgs(const ArgList &Args,
267                                     ArgStringList &CmdArgs) const;
268 
269   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
270                                      ArgStringList &CmdArgs) const;
271   void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
272                          const char *DarwinStaticLib) const;
273 
274   virtual void AddCXXStdlibLibArgs(const ArgList &Args,
275                                    ArgStringList &CmdArgs) const;
276 
277   virtual void AddCCKextLibArgs(const ArgList &Args,
278                                 ArgStringList &CmdArgs) const;
279 
280   virtual void AddLinkARCArgs(const ArgList &Args,
281                               ArgStringList &CmdArgs) const;
282   /// }
283 };
284 
285 /// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
286 class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
287 public:
Darwin_Generic_GCC(const HostInfo & Host,const llvm::Triple & Triple)288   Darwin_Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
289     : Generic_GCC(Host, Triple) {}
290 
291   std::string ComputeEffectiveClangTriple(const ArgList &Args) const;
292 
GetDefaultRelocationModel()293   virtual const char *GetDefaultRelocationModel() const { return "pic"; }
294 };
295 
296 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
297  public:
Generic_ELF(const HostInfo & Host,const llvm::Triple & Triple)298   Generic_ELF(const HostInfo &Host, const llvm::Triple& Triple)
299     : Generic_GCC(Host, Triple) {}
300 
IsIntegratedAssemblerDefault()301   virtual bool IsIntegratedAssemblerDefault() const {
302     // Default integrated assembler to on for x86.
303     return (getTriple().getArch() == llvm::Triple::x86 ||
304             getTriple().getArch() == llvm::Triple::x86_64);
305   }
306 };
307 
308 class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
309 public:
310   AuroraUX(const HostInfo &Host, const llvm::Triple& Triple);
311 
312   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
313                            const ActionList &Inputs) const;
314 };
315 
316 class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
317 public:
318   OpenBSD(const HostInfo &Host, const llvm::Triple& Triple);
319 
320   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
321                            const ActionList &Inputs) const;
322 };
323 
324 class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
325 public:
326   FreeBSD(const HostInfo &Host, const llvm::Triple& Triple);
327 
328   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
329                            const ActionList &Inputs) const;
330 };
331 
332 class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
333   const llvm::Triple ToolTriple;
334 
335 public:
336   NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
337          const llvm::Triple& ToolTriple);
338 
339   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
340                            const ActionList &Inputs) const;
341 };
342 
343 class LLVM_LIBRARY_VISIBILITY Minix : public Generic_GCC {
344 public:
345   Minix(const HostInfo &Host, const llvm::Triple& Triple);
346 
347   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
348                            const ActionList &Inputs) const;
349 };
350 
351 class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
352 public:
353   DragonFly(const HostInfo &Host, const llvm::Triple& Triple);
354 
355   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
356                            const ActionList &Inputs) const;
357 };
358 
359 class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
360 public:
361   Linux(const HostInfo &Host, const llvm::Triple& Triple);
362 
363   virtual bool HasNativeLLVMSupport() const;
364 
365   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
366                            const ActionList &Inputs) const;
367 
368   std::string Linker;
369   std::vector<std::string> ExtraOpts;
370 };
371 
372 
373 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
374 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
375 class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
376 public:
377   TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple);
378   ~TCEToolChain();
379 
380   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
381                            const ActionList &Inputs) const;
382   bool IsMathErrnoDefault() const;
383   bool IsUnwindTablesDefault() const;
384   const char* GetDefaultRelocationModel() const;
385   const char* GetForcedPicModel() const;
386 
387 private:
388   mutable llvm::DenseMap<unsigned, Tool*> Tools;
389 
390 };
391 
392 class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
393   mutable llvm::DenseMap<unsigned, Tool*> Tools;
394 
395 public:
396   Windows(const HostInfo &Host, const llvm::Triple& Triple);
397 
398   virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
399                            const ActionList &Inputs) const;
400 
401   virtual bool IsIntegratedAssemblerDefault() const;
402   virtual bool IsUnwindTablesDefault() const;
403   virtual const char *GetDefaultRelocationModel() const;
404   virtual const char *GetForcedPicModel() const;
405 };
406 
407 } // end namespace toolchains
408 } // end namespace driver
409 } // end namespace clang
410 
411 #endif
412