]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Driver/ToolChain.h
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / 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/Basic/VersionTuple.h"
15 #include "clang/Driver/Action.h"
16 #include "clang/Driver/Multilib.h"
17 #include "clang/Driver/Types.h"
18 #include "clang/Driver/Util.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include <memory>
23 #include <string>
24
25 namespace llvm {
26 namespace opt {
27   class ArgList;
28   class DerivedArgList;
29   class InputArgList;
30 }
31 }
32
33 namespace clang {
34 class ObjCRuntime;
35 namespace vfs {
36 class FileSystem;
37 }
38
39 namespace driver {
40   class Compilation;
41   class CudaInstallationDetector;
42   class Driver;
43   class JobAction;
44   class RegisterEffectiveTriple;
45   class SanitizerArgs;
46   class Tool;
47
48 /// ToolChain - Access to tools for a single platform.
49 class ToolChain {
50 public:
51   typedef SmallVector<std::string, 16> path_list;
52
53   enum CXXStdlibType {
54     CST_Libcxx,
55     CST_Libstdcxx
56   };
57
58   enum RuntimeLibType {
59     RLT_CompilerRT,
60     RLT_Libgcc
61   };
62
63   enum RTTIMode {
64     RM_EnabledExplicitly,
65     RM_EnabledImplicitly,
66     RM_DisabledExplicitly,
67     RM_DisabledImplicitly
68   };
69
70 private:
71   const Driver &D;
72   const llvm::Triple Triple;
73   const llvm::opt::ArgList &Args;
74   // We need to initialize CachedRTTIArg before CachedRTTIMode
75   const llvm::opt::Arg *const CachedRTTIArg;
76   const RTTIMode CachedRTTIMode;
77
78   /// The list of toolchain specific path prefixes to search for
79   /// files.
80   path_list FilePaths;
81
82   /// The list of toolchain specific path prefixes to search for
83   /// programs.
84   path_list ProgramPaths;
85
86   mutable std::unique_ptr<Tool> Clang;
87   mutable std::unique_ptr<Tool> Assemble;
88   mutable std::unique_ptr<Tool> Link;
89   mutable std::unique_ptr<Tool> OffloadBundler;
90   Tool *getClang() const;
91   Tool *getAssemble() const;
92   Tool *getLink() const;
93   Tool *getClangAs() const;
94   Tool *getOffloadBundler() const;
95
96   mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
97
98   /// The effective clang triple for the current Job.
99   mutable llvm::Triple EffectiveTriple;
100
101   /// Set the toolchain's effective clang triple.
102   void setEffectiveTriple(llvm::Triple ET) const { EffectiveTriple = ET; }
103
104   friend class RegisterEffectiveTriple;
105
106 protected:
107   MultilibSet Multilibs;
108
109   ToolChain(const Driver &D, const llvm::Triple &T,
110             const llvm::opt::ArgList &Args);
111
112   virtual Tool *buildAssembler() const;
113   virtual Tool *buildLinker() const;
114   virtual Tool *getTool(Action::ActionClass AC) const;
115
116   /// \name Utilities for implementing subclasses.
117   ///@{
118   static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
119                                llvm::opt::ArgStringList &CC1Args,
120                                const Twine &Path);
121   static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
122                                       llvm::opt::ArgStringList &CC1Args,
123                                       const Twine &Path);
124   static void
125       addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
126                                       llvm::opt::ArgStringList &CC1Args,
127                                       const Twine &Path);
128   static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
129                                 llvm::opt::ArgStringList &CC1Args,
130                                 ArrayRef<StringRef> Paths);
131   ///@}
132
133 public:
134   virtual ~ToolChain();
135
136   // Accessors
137
138   const Driver &getDriver() const { return D; }
139   vfs::FileSystem &getVFS() const;
140   const llvm::Triple &getTriple() const { return Triple; }
141
142   /// Get the toolchain's aux triple, if it has one.
143   ///
144   /// Exactly what the aux triple represents depends on the toolchain, but for
145   /// example when compiling CUDA code for the GPU, the triple might be NVPTX,
146   /// while the aux triple is the host (CPU) toolchain, e.g. x86-linux-gnu.
147   virtual const llvm::Triple *getAuxTriple() const { return nullptr; }
148
149   llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
150   StringRef getArchName() const { return Triple.getArchName(); }
151   StringRef getPlatform() const { return Triple.getVendorName(); }
152   StringRef getOS() const { return Triple.getOSName(); }
153
154   /// \brief Provide the default architecture name (as expected by -arch) for
155   /// this toolchain.
156   StringRef getDefaultUniversalArchName() const;
157
158   std::string getTripleString() const {
159     return Triple.getTriple();
160   }
161
162   /// Get the toolchain's effective clang triple.
163   const llvm::Triple &getEffectiveTriple() const {
164     assert(!EffectiveTriple.getTriple().empty() && "No effective triple");
165     return EffectiveTriple;
166   }
167
168   path_list &getFilePaths() { return FilePaths; }
169   const path_list &getFilePaths() const { return FilePaths; }
170
171   path_list &getProgramPaths() { return ProgramPaths; }
172   const path_list &getProgramPaths() const { return ProgramPaths; }
173
174   const MultilibSet &getMultilibs() const { return Multilibs; }
175
176   const SanitizerArgs& getSanitizerArgs() const;
177
178   // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
179   const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
180
181   // Returns the RTTIMode for the toolchain with the current arguments.
182   RTTIMode getRTTIMode() const { return CachedRTTIMode; }
183
184   /// \brief Return any implicit target and/or mode flag for an invocation of
185   /// the compiler driver as `ProgName`.
186   ///
187   /// For example, when called with i686-linux-android-g++, the first element
188   /// of the return value will be set to `"i686-linux-android"` and the second
189   /// will be set to "--driver-mode=g++"`.
190   ///
191   /// \pre `llvm::InitializeAllTargets()` has been called.
192   /// \param ProgName The name the Clang driver was invoked with (from,
193   /// e.g., argv[0])
194   /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
195   static std::pair<std::string, std::string>
196   getTargetAndModeFromProgramName(StringRef ProgName);
197
198   // Tool access.
199
200   /// TranslateArgs - Create a new derived argument list for any argument
201   /// translations this ToolChain may wish to perform, or 0 if no tool chain
202   /// specific translations are needed. If \p DeviceOffloadKind is specified
203   /// the translation specific for that offload kind is performed.
204   ///
205   /// \param BoundArch - The bound architecture name, or 0.
206   /// \param DeviceOffloadKind - The device offload kind used for the
207   /// translation.
208   virtual llvm::opt::DerivedArgList *
209   TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
210                 Action::OffloadKind DeviceOffloadKind) const {
211     return nullptr;
212   }
213
214   /// Choose a tool to use to handle the action \p JA.
215   ///
216   /// This can be overridden when a particular ToolChain needs to use
217   /// a compiler other than Clang.
218   virtual Tool *SelectTool(const JobAction &JA) const;
219
220   // Helper methods
221
222   std::string GetFilePath(const char *Name) const;
223   std::string GetProgramPath(const char *Name) const;
224
225   /// Returns the linker path, respecting the -fuse-ld= argument to determine
226   /// the linker suffix or name.
227   std::string GetLinkerPath() const;
228
229   /// \brief Dispatch to the specific toolchain for verbose printing.
230   ///
231   /// This is used when handling the verbose option to print detailed,
232   /// toolchain-specific information useful for understanding the behavior of
233   /// the driver on a specific platform.
234   virtual void printVerboseInfo(raw_ostream &OS) const {}
235
236   // Platform defaults information
237
238   /// \brief Returns true if the toolchain is targeting a non-native
239   /// architecture.
240   virtual bool isCrossCompiling() const;
241
242   /// HasNativeLTOLinker - Check whether the linker and related tools have
243   /// native LLVM support.
244   virtual bool HasNativeLLVMSupport() const;
245
246   /// LookupTypeForExtension - Return the default language type to use for the
247   /// given extension.
248   virtual types::ID LookupTypeForExtension(StringRef Ext) const;
249
250   /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
251   virtual bool IsBlocksDefault() const { return false; }
252
253   /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
254   /// by default.
255   virtual bool IsIntegratedAssemblerDefault() const { return false; }
256
257   /// \brief Check if the toolchain should use the integrated assembler.
258   virtual bool useIntegratedAs() const;
259
260   /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
261   virtual bool IsMathErrnoDefault() const { return true; }
262
263   /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
264   /// -fencode-extended-block-signature by default.
265   virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
266
267   /// IsObjCNonFragileABIDefault - Does this tool chain set
268   /// -fobjc-nonfragile-abi by default.
269   virtual bool IsObjCNonFragileABIDefault() const { return false; }
270
271   /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
272   /// mixed dispatch method be used?
273   virtual bool UseObjCMixedDispatch() const { return false; }
274
275   /// GetDefaultStackProtectorLevel - Get the default stack protector level for
276   /// this tool chain (0=off, 1=on, 2=strong, 3=all).
277   virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
278     return 0;
279   }
280
281   /// GetDefaultLinker - Get the default linker to use.
282   virtual const char *getDefaultLinker() const {
283     return "ld";
284   }
285
286   /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
287   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
288     return ToolChain::RLT_Libgcc;
289   }
290
291   virtual CXXStdlibType GetDefaultCXXStdlibType() const {
292     return ToolChain::CST_Libstdcxx;
293   }
294
295   virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
296                                     StringRef Component,
297                                     bool Shared = false) const;
298
299   const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
300                                      StringRef Component,
301                                      bool Shared = false) const;
302   /// needsProfileRT - returns true if instrumentation profile is on.
303   static bool needsProfileRT(const llvm::opt::ArgList &Args);
304
305   /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
306   /// by default.
307   virtual bool IsUnwindTablesDefault() const;
308
309   /// \brief Test whether this toolchain defaults to PIC.
310   virtual bool isPICDefault() const = 0;
311
312   /// \brief Test whether this toolchain defaults to PIE.
313   virtual bool isPIEDefault() const = 0;
314
315   /// \brief Tests whether this toolchain forces its default for PIC, PIE or
316   /// non-PIC.  If this returns true, any PIC related flags should be ignored
317   /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
318   /// used exclusively.
319   virtual bool isPICDefaultForced() const = 0;
320
321   /// SupportsProfiling - Does this tool chain support -pg.
322   virtual bool SupportsProfiling() const { return true; }
323
324   /// Does this tool chain support Objective-C garbage collection.
325   virtual bool SupportsObjCGC() const { return true; }
326
327   /// Complain if this tool chain doesn't support Objective-C ARC.
328   virtual void CheckObjCARC() const {}
329
330   /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
331   /// compile unit information.
332   virtual bool UseDwarfDebugFlags() const { return false; }
333
334   // Return the DWARF version to emit, in the absence of arguments
335   // to the contrary.
336   virtual unsigned GetDefaultDwarfVersion() const { return 4; }
337
338   // True if the driver should assume "-fstandalone-debug"
339   // in the absence of an option specifying otherwise,
340   // provided that debugging was requested in the first place.
341   // i.e. a value of 'true' does not imply that debugging is wanted.
342   virtual bool GetDefaultStandaloneDebug() const { return false; }
343
344   // Return the default debugger "tuning."
345   virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
346     return llvm::DebuggerKind::GDB;
347   }
348
349   /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
350   virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
351     return false;
352   }
353
354   /// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
355   virtual bool SupportsEmbeddedBitcode() const {
356     return false;
357   }
358
359   /// getThreadModel() - Which thread model does this target use?
360   virtual std::string getThreadModel() const { return "posix"; }
361
362   /// isThreadModelSupported() - Does this target support a thread model?
363   virtual bool isThreadModelSupported(const StringRef Model) const;
364
365   /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
366   /// command line arguments into account.
367   virtual std::string
368   ComputeLLVMTriple(const llvm::opt::ArgList &Args,
369                     types::ID InputType = types::TY_INVALID) const;
370
371   /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
372   /// target, which may take into account the command line arguments. For
373   /// example, on Darwin the -mmacosx-version-min= command line argument (which
374   /// sets the deployment target) determines the version in the triple passed to
375   /// Clang.
376   virtual std::string ComputeEffectiveClangTriple(
377       const llvm::opt::ArgList &Args,
378       types::ID InputType = types::TY_INVALID) const;
379
380   /// getDefaultObjCRuntime - Return the default Objective-C runtime
381   /// for this platform.
382   ///
383   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
384   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
385
386   /// hasBlocksRuntime - Given that the user is compiling with
387   /// -fblocks, does this tool chain guarantee the existence of a
388   /// blocks runtime?
389   ///
390   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
391   virtual bool hasBlocksRuntime() const { return true; }
392
393   /// \brief Add the clang cc1 arguments for system include paths.
394   ///
395   /// This routine is responsible for adding the necessary cc1 arguments to
396   /// include headers from standard system header directories.
397   virtual void
398   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
399                             llvm::opt::ArgStringList &CC1Args) const;
400
401   /// \brief Add options that need to be passed to cc1 for this target.
402   virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
403                                      llvm::opt::ArgStringList &CC1Args) const;
404
405   /// \brief Add warning options that need to be passed to cc1 for this target.
406   virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
407
408   // GetRuntimeLibType - Determine the runtime library type to use with the
409   // given compilation arguments.
410   virtual RuntimeLibType
411   GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
412
413   // GetCXXStdlibType - Determine the C++ standard library type to use with the
414   // given compilation arguments.
415   virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
416
417   /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
418   /// the include paths to use for the given C++ standard library type.
419   virtual void
420   AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
421                                llvm::opt::ArgStringList &CC1Args) const;
422
423   /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
424   /// for the given C++ standard library type.
425   virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
426                                    llvm::opt::ArgStringList &CmdArgs) const;
427
428   /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
429   void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
430                           llvm::opt::ArgStringList &CmdArgs) const;
431
432   /// AddCCKextLibArgs - Add the system specific linker arguments to use
433   /// for kernel extensions (Darwin-specific).
434   virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
435                                 llvm::opt::ArgStringList &CmdArgs) const;
436
437   /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
438   /// global flags for unsafe floating point math, add it and return true.
439   ///
440   /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
441   virtual bool AddFastMathRuntimeIfAvailable(
442       const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
443   /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
444   /// a suitable profile runtime library to the linker.
445   virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
446                                 llvm::opt::ArgStringList &CmdArgs) const;
447
448   /// \brief Add arguments to use system-specific CUDA includes.
449   virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
450                                   llvm::opt::ArgStringList &CC1Args) const;
451
452   /// \brief Add arguments to use MCU GCC toolchain includes.
453   virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
454                                    llvm::opt::ArgStringList &CC1Args) const;
455
456   /// \brief On Windows, returns the MSVC compatibility version.
457   virtual VersionTuple computeMSVCVersion(const Driver *D,
458                                           const llvm::opt::ArgList &Args) const;
459
460   /// \brief Return sanitizers which are available in this toolchain.
461   virtual SanitizerMask getSupportedSanitizers() const;
462
463   /// \brief Return sanitizers which are enabled by default.
464   virtual SanitizerMask getDefaultSanitizers() const { return 0; }
465 };
466
467 /// Set a ToolChain's effective triple. Reset it when the registration object
468 /// is destroyed.
469 class RegisterEffectiveTriple {
470   const ToolChain &TC;
471
472 public:
473   RegisterEffectiveTriple(const ToolChain &TC, llvm::Triple T) : TC(TC) {
474     TC.setEffectiveTriple(T);
475   }
476
477   ~RegisterEffectiveTriple() { TC.setEffectiveTriple(llvm::Triple()); }
478 };
479
480 } // end namespace driver
481 } // end namespace clang
482
483 #endif