1//===--- ToolChain.h - Collections of tools for one platform ----*- 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_DRIVER_TOOLCHAIN_H_
11#define CLANG_DRIVER_TOOLCHAIN_H_
12
13#include "clang/Driver/Action.h"
14#include "clang/Driver/Types.h"
15#include "clang/Driver/Util.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Support/Path.h"
19#include <string>
20
21namespace clang {
22  class ObjCRuntime;
23
24namespace driver {
25  class ArgList;
26  class Compilation;
27  class DerivedArgList;
28  class Driver;
29  class InputArgList;
30  class JobAction;
31  class Tool;
32
33/// ToolChain - Access to tools for a single platform.
34class ToolChain {
35public:
36  typedef SmallVector<std::string, 4> path_list;
37
38  enum CXXStdlibType {
39    CST_Libcxx,
40    CST_Libstdcxx
41  };
42
43  enum RuntimeLibType {
44    RLT_CompilerRT,
45    RLT_Libgcc
46  };
47
48private:
49  const Driver &D;
50  const llvm::Triple Triple;
51  const ArgList &Args;
52
53  /// The list of toolchain specific path prefixes to search for
54  /// files.
55  path_list FilePaths;
56
57  /// The list of toolchain specific path prefixes to search for
58  /// programs.
59  path_list ProgramPaths;
60
61  mutable llvm::DenseMap<unsigned, Tool*> Tools;
62
63protected:
64  ToolChain(const Driver &D, const llvm::Triple &T, const ArgList &Args);
65
66  virtual Tool *constructTool(Action::ActionClass AC) const = 0;
67
68  /// \name Utilities for implementing subclasses.
69  ///@{
70  static void addSystemInclude(const ArgList &DriverArgs,
71                               ArgStringList &CC1Args,
72                               const Twine &Path);
73  static void addExternCSystemInclude(const ArgList &DriverArgs,
74                                      ArgStringList &CC1Args,
75                                      const Twine &Path);
76  static void addSystemIncludes(const ArgList &DriverArgs,
77                                ArgStringList &CC1Args,
78                                ArrayRef<StringRef> Paths);
79  ///@}
80
81public:
82  virtual ~ToolChain();
83
84  // Accessors
85
86  const Driver &getDriver() const;
87  const llvm::Triple &getTriple() const { return Triple; }
88
89  llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
90  StringRef getArchName() const { return Triple.getArchName(); }
91  StringRef getPlatform() const { return Triple.getVendorName(); }
92  StringRef getOS() const { return Triple.getOSName(); }
93
94  /// \brief Provide the default architecture name (as expected by -arch) for
95  /// this toolchain. Note t
96  std::string getDefaultUniversalArchName() const;
97
98  std::string getTripleString() const {
99    return Triple.getTriple();
100  }
101
102  path_list &getFilePaths() { return FilePaths; }
103  const path_list &getFilePaths() const { return FilePaths; }
104
105  path_list &getProgramPaths() { return ProgramPaths; }
106  const path_list &getProgramPaths() const { return ProgramPaths; }
107
108  // Tool access.
109
110  /// TranslateArgs - Create a new derived argument list for any argument
111  /// translations this ToolChain may wish to perform, or 0 if no tool chain
112  /// specific translations are needed.
113  ///
114  /// \param BoundArch - The bound architecture name, or 0.
115  virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
116                                        const char *BoundArch) const {
117    return 0;
118  }
119
120  /// Choose a tool to use to handle the action \p JA.
121  Tool &SelectTool(const JobAction &JA) const;
122
123  // Helper methods
124
125  std::string GetFilePath(const char *Name) const;
126  std::string GetProgramPath(const char *Name) const;
127
128  // Platform defaults information
129
130  /// HasNativeLTOLinker - Check whether the linker and related tools have
131  /// native LLVM support.
132  virtual bool HasNativeLLVMSupport() const;
133
134  /// LookupTypeForExtension - Return the default language type to use for the
135  /// given extension.
136  virtual types::ID LookupTypeForExtension(const char *Ext) const;
137
138  /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
139  virtual bool IsBlocksDefault() const { return false; }
140
141  /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
142  /// by default.
143  virtual bool IsIntegratedAssemblerDefault() const { return false; }
144
145  /// \brief Check if the toolchain should use the integrated assembler.
146  bool useIntegratedAs() const;
147
148  /// IsStrictAliasingDefault - Does this tool chain use -fstrict-aliasing by
149  /// default.
150  virtual bool IsStrictAliasingDefault() const { return true; }
151
152  /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
153  virtual bool IsMathErrnoDefault() const { return true; }
154
155  /// IsObjCDefaultSynthPropertiesDefault - Does this tool chain enable
156  /// -fobjc-default-synthesize-properties by default.
157  virtual bool IsObjCDefaultSynthPropertiesDefault() const { return true; }
158
159  /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
160  /// -fencode-extended-block-signature by default.
161  virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
162
163  /// IsObjCNonFragileABIDefault - Does this tool chain set
164  /// -fobjc-nonfragile-abi by default.
165  virtual bool IsObjCNonFragileABIDefault() const { return false; }
166
167  /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
168  /// mixed dispatch method be used?
169  virtual bool UseObjCMixedDispatch() const { return false; }
170
171  /// GetDefaultStackProtectorLevel - Get the default stack protector level for
172  /// this tool chain (0=off, 1=on, 2=all).
173  virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
174    return 0;
175  }
176
177  /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
178  virtual RuntimeLibType GetDefaultRuntimeLibType() const {
179    return ToolChain::RLT_Libgcc;
180  }
181
182  /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
183  /// by default.
184  virtual bool IsUnwindTablesDefault() const;
185
186  /// \brief Test whether this toolchain defaults to PIC.
187  virtual bool isPICDefault() const = 0;
188
189  /// \brief Tests whether this toolchain forces its default for PIC or non-PIC.
190  /// If this returns true, any PIC related flags should be ignored and instead
191  /// the result of \c isPICDefault() is used exclusively.
192  virtual bool isPICDefaultForced() const = 0;
193
194  /// SupportsProfiling - Does this tool chain support -pg.
195  virtual bool SupportsProfiling() const { return true; }
196
197  /// Does this tool chain support Objective-C garbage collection.
198  virtual bool SupportsObjCGC() const { return true; }
199
200  /// Complain if this tool chain doesn't support Objective-C ARC.
201  virtual void CheckObjCARC() const {}
202
203  /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
204  /// compile unit information.
205  virtual bool UseDwarfDebugFlags() const { return false; }
206
207  /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
208  virtual bool UseSjLjExceptions() const { return false; }
209
210  /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
211  /// command line arguments into account.
212  virtual std::string ComputeLLVMTriple(const ArgList &Args,
213                                 types::ID InputType = types::TY_INVALID) const;
214
215  /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
216  /// target, which may take into account the command line arguments. For
217  /// example, on Darwin the -mmacosx-version-min= command line argument (which
218  /// sets the deployment target) determines the version in the triple passed to
219  /// Clang.
220  virtual std::string ComputeEffectiveClangTriple(const ArgList &Args,
221                                 types::ID InputType = types::TY_INVALID) const;
222
223  /// getDefaultObjCRuntime - Return the default Objective-C runtime
224  /// for this platform.
225  ///
226  /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
227  virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
228
229  /// hasBlocksRuntime - Given that the user is compiling with
230  /// -fblocks, does this tool chain guarantee the existence of a
231  /// blocks runtime?
232  ///
233  /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
234  virtual bool hasBlocksRuntime() const { return true; }
235
236  /// \brief Add the clang cc1 arguments for system include paths.
237  ///
238  /// This routine is responsible for adding the necessary cc1 arguments to
239  /// include headers from standard system header directories.
240  virtual void AddClangSystemIncludeArgs(const ArgList &DriverArgs,
241                                         ArgStringList &CC1Args) const;
242
243  /// \brief Add options that need to be passed to cc1 for this target.
244  virtual void addClangTargetOptions(const ArgList &DriverArgs,
245                                     ArgStringList &CC1Args) const;
246
247  // GetRuntimeLibType - Determine the runtime library type to use with the
248  // given compilation arguments.
249  virtual RuntimeLibType GetRuntimeLibType(const ArgList &Args) const;
250
251  // GetCXXStdlibType - Determine the C++ standard library type to use with the
252  // given compilation arguments.
253  virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;
254
255  /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
256  /// the include paths to use for the given C++ standard library type.
257  virtual void AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
258                                            ArgStringList &CC1Args) const;
259
260  /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
261  /// for the given C++ standard library type.
262  virtual void AddCXXStdlibLibArgs(const ArgList &Args,
263                                   ArgStringList &CmdArgs) const;
264
265  /// AddCCKextLibArgs - Add the system specific linker arguments to use
266  /// for kernel extensions (Darwin-specific).
267  virtual void AddCCKextLibArgs(const ArgList &Args,
268                                ArgStringList &CmdArgs) const;
269
270  /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
271  /// global flags for unsafe floating point math, add it and return true.
272  ///
273  /// This checks for presence of the -ffast-math or -funsafe-math flags.
274  virtual bool AddFastMathRuntimeIfAvailable(const ArgList &Args,
275                                             ArgStringList &CmdArgs) const;
276};
277
278} // end namespace driver
279} // end namespace clang
280
281#endif
282