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