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