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