ToolChains.h revision 99ba9e3bd70671f3441fb974895f226a83ce0e66
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  /// \brief Struct to store and manipulate GCC versions.
31  ///
32  /// We rely on assumptions about the form and structure of GCC version
33  /// numbers: they consist of at most three '.'-separated components, and each
34  /// component is a non-negative integer except for the last component. For
35  /// the last component we are very flexible in order to tolerate release
36  /// candidates or 'x' wildcards.
37  ///
38  /// Note that the ordering established among GCCVersions is based on the
39  /// preferred version string to use. For example we prefer versions without
40  /// a hard-coded patch number to those with a hard coded patch number.
41  ///
42  /// Currently this doesn't provide any logic for textual suffixes to patches
43  /// in the way that (for example) Debian's version format does. If that ever
44  /// becomes necessary, it can be added.
45  struct GCCVersion {
46    /// \brief The unparsed text of the version.
47    std::string Text;
48
49    /// \brief The parsed major, minor, and patch numbers.
50    int Major, Minor, Patch;
51
52    /// \brief Any textual suffix on the patch number.
53    std::string PatchSuffix;
54
55    static GCCVersion Parse(StringRef VersionText);
56    bool operator<(const GCCVersion &RHS) const;
57    bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
58    bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
59    bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
60  };
61
62
63  /// \brief This is a class to find a viable GCC installation for Clang to
64  /// use.
65  ///
66  /// This class tries to find a GCC installation on the system, and report
67  /// information about it. It starts from the host information provided to the
68  /// Driver, and has logic for fuzzing that where appropriate.
69  class GCCInstallationDetector {
70
71    bool IsValid;
72    std::string GccTriple;
73
74    // FIXME: These might be better as path objects.
75    std::string GccInstallPath;
76    std::string GccParentLibPath;
77
78    GCCVersion Version;
79
80  public:
81    GCCInstallationDetector(const Driver &D);
82
83    /// \brief Check whether we detected a valid GCC install.
84    bool isValid() const { return IsValid; }
85
86    /// \brief Get the GCC triple for the detected install.
87    StringRef getTriple() const { return GccTriple; }
88
89    /// \brief Get the detected GCC installation path.
90    StringRef getInstallPath() const { return GccInstallPath; }
91
92    /// \brief Get the detected GCC parent lib path.
93    StringRef getParentLibPath() const { return GccParentLibPath; }
94
95    /// \brief Get the detected GCC version string.
96    StringRef getVersion() const { return Version.Text; }
97
98  private:
99    static void CollectLibDirsAndTriples(llvm::Triple::ArchType HostArch,
100                                         SmallVectorImpl<StringRef> &LibDirs,
101                                         SmallVectorImpl<StringRef> &Triples);
102
103    void ScanLibDirForGCCTriple(llvm::Triple::ArchType HostArch,
104                                const std::string &LibDir,
105                                StringRef CandidateTriple);
106  };
107
108  GCCInstallationDetector GCCInstallation;
109
110  mutable llvm::DenseMap<unsigned, Tool*> Tools;
111
112public:
113  Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple);
114  ~Generic_GCC();
115
116  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
117                           const ActionList &Inputs) const;
118
119  virtual bool IsUnwindTablesDefault() const;
120  virtual const char *GetDefaultRelocationModel() const;
121  virtual const char *GetForcedPicModel() const;
122
123protected:
124  /// \name ToolChain Implementation Helper Functions
125  /// @{
126
127  /// \brief Check whether the target triple's architecture is 64-bits.
128  bool isTarget64Bit() const {
129    return (getTriple().getArch() == llvm::Triple::x86_64 ||
130            getTriple().getArch() == llvm::Triple::ppc64);
131  }
132  /// \brief Check whether the target triple's architecture is 32-bits.
133  /// FIXME: This should likely do more than just negate the 64-bit query.
134  bool isTarget32Bit() const { return !isTarget64Bit(); }
135
136  /// @}
137};
138
139class LLVM_LIBRARY_VISIBILITY Hexagon_TC : public ToolChain {
140protected:
141  mutable llvm::DenseMap<unsigned, Tool*> Tools;
142
143public:
144  Hexagon_TC(const HostInfo &Host, const llvm::Triple& Triple);
145  ~Hexagon_TC();
146
147  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
148                           const ActionList &Inputs) const;
149
150  virtual bool IsUnwindTablesDefault() const;
151  virtual const char *GetDefaultRelocationModel() const;
152  virtual const char *GetForcedPicModel() const;
153};
154
155  /// Darwin - The base Darwin tool chain.
156class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
157public:
158  /// The host version.
159  unsigned DarwinVersion[3];
160
161private:
162  mutable llvm::DenseMap<unsigned, Tool*> Tools;
163
164  /// Whether the information on the target has been initialized.
165  //
166  // FIXME: This should be eliminated. What we want to do is make this part of
167  // the "default target for arguments" selection process, once we get out of
168  // the argument translation business.
169  mutable bool TargetInitialized;
170
171  // FIXME: Remove this once there is a proper way to detect an ARC runtime
172  // for the simulator.
173 public:
174  mutable enum {
175    ARCSimulator_None,
176    ARCSimulator_HasARCRuntime,
177    ARCSimulator_NoARCRuntime
178  } ARCRuntimeForSimulator;
179
180  mutable enum {
181    LibCXXSimulator_None,
182    LibCXXSimulator_NotAvailable,
183    LibCXXSimulator_Available
184  } LibCXXForSimulator;
185
186private:
187  /// Whether we are targeting iPhoneOS target.
188  mutable bool TargetIsIPhoneOS;
189
190  /// Whether we are targeting the iPhoneOS simulator target.
191  mutable bool TargetIsIPhoneOSSimulator;
192
193  /// The OS version we are targeting.
194  mutable unsigned TargetVersion[3];
195
196  /// The default macosx-version-min of this tool chain; empty until
197  /// initialized.
198  std::string MacosxVersionMin;
199
200  bool hasARCRuntime() const;
201
202private:
203  void AddDeploymentTarget(DerivedArgList &Args) const;
204
205public:
206  Darwin(const HostInfo &Host, const llvm::Triple& Triple);
207  ~Darwin();
208
209  std::string ComputeEffectiveClangTriple(const ArgList &Args,
210                                          types::ID InputType) const;
211
212  /// @name Darwin Specific Toolchain API
213  /// {
214
215  // FIXME: Eliminate these ...Target functions and derive separate tool chains
216  // for these targets and put version in constructor.
217  void setTarget(bool IsIPhoneOS, unsigned Major, unsigned Minor,
218                 unsigned Micro, bool IsIOSSim) const {
219    assert((!IsIOSSim || IsIPhoneOS) && "Unexpected deployment target!");
220
221    // FIXME: For now, allow reinitialization as long as values don't
222    // change. This will go away when we move away from argument translation.
223    if (TargetInitialized && TargetIsIPhoneOS == IsIPhoneOS &&
224        TargetIsIPhoneOSSimulator == IsIOSSim &&
225        TargetVersion[0] == Major && TargetVersion[1] == Minor &&
226        TargetVersion[2] == Micro)
227      return;
228
229    assert(!TargetInitialized && "Target already initialized!");
230    TargetInitialized = true;
231    TargetIsIPhoneOS = IsIPhoneOS;
232    TargetIsIPhoneOSSimulator = IsIOSSim;
233    TargetVersion[0] = Major;
234    TargetVersion[1] = Minor;
235    TargetVersion[2] = Micro;
236  }
237
238  bool isTargetIPhoneOS() const {
239    assert(TargetInitialized && "Target not initialized!");
240    return TargetIsIPhoneOS;
241  }
242
243  bool isTargetIOSSimulator() const {
244    assert(TargetInitialized && "Target not initialized!");
245    return TargetIsIPhoneOSSimulator;
246  }
247
248  bool isTargetInitialized() const { return TargetInitialized; }
249
250  void getTargetVersion(unsigned (&Res)[3]) const {
251    assert(TargetInitialized && "Target not initialized!");
252    Res[0] = TargetVersion[0];
253    Res[1] = TargetVersion[1];
254    Res[2] = TargetVersion[2];
255  }
256
257  /// getDarwinArchName - Get the "Darwin" arch name for a particular compiler
258  /// invocation. For example, Darwin treats different ARM variations as
259  /// distinct architectures.
260  StringRef getDarwinArchName(const ArgList &Args) const;
261
262  static bool isVersionLT(unsigned (&A)[3], unsigned (&B)[3]) {
263    for (unsigned i=0; i < 3; ++i) {
264      if (A[i] > B[i]) return false;
265      if (A[i] < B[i]) return true;
266    }
267    return false;
268  }
269
270  bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
271    assert(isTargetIPhoneOS() && "Unexpected call for OS X target!");
272    unsigned B[3] = { V0, V1, V2 };
273    return isVersionLT(TargetVersion, B);
274  }
275
276  bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const {
277    assert(!isTargetIPhoneOS() && "Unexpected call for iPhoneOS target!");
278    unsigned B[3] = { V0, V1, V2 };
279    return isVersionLT(TargetVersion, B);
280  }
281
282  /// AddLinkSearchPathArgs - Add the linker search paths to \arg CmdArgs.
283  ///
284  /// \param Args - The input argument list.
285  /// \param CmdArgs [out] - The command argument list to append the paths
286  /// (prefixed by -L) to.
287  virtual void AddLinkSearchPathArgs(const ArgList &Args,
288                                     ArgStringList &CmdArgs) const = 0;
289
290  /// AddLinkARCArgs - Add the linker arguments to link the ARC runtime library.
291  virtual void AddLinkARCArgs(const ArgList &Args,
292                              ArgStringList &CmdArgs) const = 0;
293
294  /// AddLinkRuntimeLibArgs - Add the linker arguments to link the compiler
295  /// runtime library.
296  virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
297                                     ArgStringList &CmdArgs) const = 0;
298
299  /// }
300  /// @name ToolChain Implementation
301  /// {
302
303  virtual types::ID LookupTypeForExtension(const char *Ext) const;
304
305  virtual bool HasNativeLLVMSupport() const;
306
307  virtual void configureObjCRuntime(ObjCRuntime &runtime) const;
308  virtual bool hasBlocksRuntime() const;
309
310  virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
311                                        const char *BoundArch) const;
312
313  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
314                           const ActionList &Inputs) const;
315
316  virtual bool IsBlocksDefault() const {
317    // Always allow blocks on Darwin; users interested in versioning are
318    // expected to use /usr/include/Blocks.h.
319    return true;
320  }
321  virtual bool IsIntegratedAssemblerDefault() const {
322#ifdef DISABLE_DEFAULT_INTEGRATED_ASSEMBLER
323    return false;
324#else
325    // Default integrated assembler to on for x86.
326    return (getTriple().getArch() == llvm::Triple::x86 ||
327            getTriple().getArch() == llvm::Triple::x86_64);
328#endif
329  }
330  virtual bool IsStrictAliasingDefault() const {
331#ifdef DISABLE_DEFAULT_STRICT_ALIASING
332    return false;
333#else
334    return ToolChain::IsStrictAliasingDefault();
335#endif
336  }
337
338  virtual bool IsObjCDefaultSynthPropertiesDefault() const {
339    return false;
340  }
341
342  virtual bool IsObjCNonFragileABIDefault() const {
343    // Non-fragile ABI is default for everything but i386.
344    return getTriple().getArch() != llvm::Triple::x86;
345  }
346  virtual bool IsObjCLegacyDispatchDefault() const {
347    // This is only used with the non-fragile ABI.
348
349    // Legacy dispatch is used everywhere except on x86_64.
350    return getTriple().getArch() != llvm::Triple::x86_64;
351  }
352  virtual bool UseObjCMixedDispatch() const {
353    // This is only used with the non-fragile ABI and non-legacy dispatch.
354
355    // Mixed dispatch is used everywhere except OS X before 10.6.
356    return !(!isTargetIPhoneOS() && isMacosxVersionLT(10, 6));
357  }
358  virtual bool IsUnwindTablesDefault() const;
359  virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
360    // Stack protectors default to on for user code on 10.5,
361    // and for everything in 10.6 and beyond
362    return isTargetIPhoneOS() ||
363      (!isMacosxVersionLT(10, 6) ||
364         (!isMacosxVersionLT(10, 5) && !KernelOrKext));
365  }
366  virtual RuntimeLibType GetDefaultRuntimeLibType() const {
367    return ToolChain::RLT_CompilerRT;
368  }
369  virtual const char *GetDefaultRelocationModel() const;
370  virtual const char *GetForcedPicModel() const;
371
372  virtual bool SupportsProfiling() const;
373
374  virtual bool SupportsObjCGC() const;
375
376  virtual bool UseDwarfDebugFlags() const;
377
378  virtual bool UseSjLjExceptions() const;
379
380  /// }
381};
382
383/// DarwinClang - The Darwin toolchain used by Clang.
384class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
385private:
386  void AddGCCLibexecPath(unsigned darwinVersion);
387
388public:
389  DarwinClang(const HostInfo &Host, const llvm::Triple& Triple);
390
391  /// @name Darwin ToolChain Implementation
392  /// {
393
394  virtual void AddLinkSearchPathArgs(const ArgList &Args,
395                                    ArgStringList &CmdArgs) const;
396
397  virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
398                                     ArgStringList &CmdArgs) const;
399  void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
400                         const char *DarwinStaticLib) const;
401
402  virtual void AddCXXStdlibLibArgs(const ArgList &Args,
403                                   ArgStringList &CmdArgs) const;
404
405  virtual void AddCCKextLibArgs(const ArgList &Args,
406                                ArgStringList &CmdArgs) const;
407
408  virtual void AddLinkARCArgs(const ArgList &Args,
409                              ArgStringList &CmdArgs) const;
410  /// }
411};
412
413/// Darwin_Generic_GCC - Generic Darwin tool chain using gcc.
414class LLVM_LIBRARY_VISIBILITY Darwin_Generic_GCC : public Generic_GCC {
415public:
416  Darwin_Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
417    : Generic_GCC(Host, Triple) {}
418
419  std::string ComputeEffectiveClangTriple(const ArgList &Args,
420                                          types::ID InputType) const;
421
422  virtual const char *GetDefaultRelocationModel() const { return "pic"; }
423};
424
425class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
426  virtual void anchor();
427public:
428  Generic_ELF(const HostInfo &Host, const llvm::Triple& Triple)
429    : Generic_GCC(Host, Triple) {}
430
431  virtual bool IsIntegratedAssemblerDefault() const {
432    // Default integrated assembler to on for x86.
433    return (getTriple().getArch() == llvm::Triple::x86 ||
434            getTriple().getArch() == llvm::Triple::x86_64);
435  }
436};
437
438class LLVM_LIBRARY_VISIBILITY AuroraUX : public Generic_GCC {
439public:
440  AuroraUX(const HostInfo &Host, const llvm::Triple& Triple);
441
442  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
443                           const ActionList &Inputs) const;
444};
445
446class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
447public:
448  OpenBSD(const HostInfo &Host, const llvm::Triple& Triple);
449
450  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
451                           const ActionList &Inputs) const;
452};
453
454class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
455public:
456  FreeBSD(const HostInfo &Host, const llvm::Triple& Triple);
457
458  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
459                           const ActionList &Inputs) const;
460};
461
462class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
463  const llvm::Triple ToolTriple;
464
465public:
466  NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
467         const llvm::Triple& ToolTriple);
468
469  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
470                           const ActionList &Inputs) const;
471};
472
473class LLVM_LIBRARY_VISIBILITY Minix : public Generic_ELF {
474public:
475  Minix(const HostInfo &Host, const llvm::Triple& Triple);
476
477  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
478                           const ActionList &Inputs) const;
479};
480
481class LLVM_LIBRARY_VISIBILITY DragonFly : public Generic_ELF {
482public:
483  DragonFly(const HostInfo &Host, const llvm::Triple& Triple);
484
485  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
486                           const ActionList &Inputs) const;
487};
488
489class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
490public:
491  Linux(const HostInfo &Host, const llvm::Triple& Triple);
492
493  virtual bool HasNativeLLVMSupport() const;
494
495  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
496                           const ActionList &Inputs) const;
497
498  virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
499                                         ArgStringList &CC1Args) const;
500  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
501                                            ArgStringList &CC1Args) const;
502
503  std::string Linker;
504  std::vector<std::string> ExtraOpts;
505
506private:
507  static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
508                                       const ArgList &DriverArgs,
509                                       ArgStringList &CC1Args);
510};
511
512
513/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
514/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
515class LLVM_LIBRARY_VISIBILITY TCEToolChain : public ToolChain {
516public:
517  TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple);
518  ~TCEToolChain();
519
520  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
521                           const ActionList &Inputs) const;
522  bool IsMathErrnoDefault() const;
523  bool IsUnwindTablesDefault() const;
524  const char* GetDefaultRelocationModel() const;
525  const char* GetForcedPicModel() const;
526
527private:
528  mutable llvm::DenseMap<unsigned, Tool*> Tools;
529
530};
531
532class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
533  mutable llvm::DenseMap<unsigned, Tool*> Tools;
534
535public:
536  Windows(const HostInfo &Host, const llvm::Triple& Triple);
537
538  virtual Tool &SelectTool(const Compilation &C, const JobAction &JA,
539                           const ActionList &Inputs) const;
540
541  virtual bool IsIntegratedAssemblerDefault() const;
542  virtual bool IsUnwindTablesDefault() const;
543  virtual const char *GetDefaultRelocationModel() const;
544  virtual const char *GetForcedPicModel() const;
545
546  virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
547                                         ArgStringList &CC1Args) const;
548  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
549                                            ArgStringList &CC1Args) const;
550
551};
552
553} // end namespace toolchains
554} // end namespace driver
555} // end namespace clang
556
557#endif
558