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