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