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