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