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