1 //===- ToolChain.h - Collections of tools for one platform ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_DRIVER_TOOLCHAIN_H
10 #define LLVM_CLANG_DRIVER_TOOLCHAIN_H
11 
12 #include "clang/Basic/DebugInfoOptions.h"
13 #include "clang/Basic/LLVM.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/Sanitizers.h"
16 #include "clang/Driver/Action.h"
17 #include "clang/Driver/Multilib.h"
18 #include "clang/Driver/Types.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/FloatingPointMode.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/MC/MCTargetOptions.h"
26 #include "llvm/Option/Option.h"
27 #include "llvm/Support/VersionTuple.h"
28 #include "llvm/Target/TargetOptions.h"
29 #include <cassert>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 
34 namespace llvm {
35 namespace opt {
36 
37 class Arg;
38 class ArgList;
39 class DerivedArgList;
40 
41 } // namespace opt
42 namespace vfs {
43 
44 class FileSystem;
45 
46 } // namespace vfs
47 } // namespace llvm
48 
49 namespace clang {
50 
51 class ObjCRuntime;
52 
53 namespace driver {
54 
55 class Driver;
56 class InputInfo;
57 class SanitizerArgs;
58 class Tool;
59 class XRayArgs;
60 
61 /// Helper structure used to pass information extracted from clang executable
62 /// name such as `i686-linux-android-g++`.
63 struct ParsedClangName {
64   /// Target part of the executable name, as `i686-linux-android`.
65   std::string TargetPrefix;
66 
67   /// Driver mode part of the executable name, as `g++`.
68   std::string ModeSuffix;
69 
70   /// Corresponding driver mode argument, as '--driver-mode=g++'
71   const char *DriverMode = nullptr;
72 
73   /// True if TargetPrefix is recognized as a registered target name.
74   bool TargetIsValid = false;
75 
76   ParsedClangName() = default;
ParsedClangNameParsedClangName77   ParsedClangName(std::string Suffix, const char *Mode)
78       : ModeSuffix(Suffix), DriverMode(Mode) {}
ParsedClangNameParsedClangName79   ParsedClangName(std::string Target, std::string Suffix, const char *Mode,
80                   bool IsRegistered)
81       : TargetPrefix(Target), ModeSuffix(Suffix), DriverMode(Mode),
82         TargetIsValid(IsRegistered) {}
83 
isEmptyParsedClangName84   bool isEmpty() const {
85     return TargetPrefix.empty() && ModeSuffix.empty() && DriverMode == nullptr;
86   }
87 };
88 
89 /// ToolChain - Access to tools for a single platform.
90 class ToolChain {
91 public:
92   using path_list = SmallVector<std::string, 16>;
93 
94   enum CXXStdlibType {
95     CST_Libcxx,
96     CST_Libstdcxx
97   };
98 
99   enum RuntimeLibType {
100     RLT_CompilerRT,
101     RLT_Libgcc
102   };
103 
104   enum UnwindLibType {
105     UNW_None,
106     UNW_CompilerRT,
107     UNW_Libgcc
108   };
109 
110   enum RTTIMode {
111     RM_Enabled,
112     RM_Disabled,
113   };
114 
115   enum FileType { FT_Object, FT_Static, FT_Shared };
116 
117 private:
118   friend class RegisterEffectiveTriple;
119 
120   const Driver &D;
121   llvm::Triple Triple;
122   const llvm::opt::ArgList &Args;
123 
124   // We need to initialize CachedRTTIArg before CachedRTTIMode
125   const llvm::opt::Arg *const CachedRTTIArg;
126 
127   const RTTIMode CachedRTTIMode;
128 
129   /// The list of toolchain specific path prefixes to search for libraries.
130   path_list LibraryPaths;
131 
132   /// The list of toolchain specific path prefixes to search for files.
133   path_list FilePaths;
134 
135   /// The list of toolchain specific path prefixes to search for programs.
136   path_list ProgramPaths;
137 
138   mutable std::unique_ptr<Tool> Clang;
139   mutable std::unique_ptr<Tool> Flang;
140   mutable std::unique_ptr<Tool> Assemble;
141   mutable std::unique_ptr<Tool> Link;
142   mutable std::unique_ptr<Tool> StaticLibTool;
143   mutable std::unique_ptr<Tool> IfsMerge;
144   mutable std::unique_ptr<Tool> OffloadBundler;
145   mutable std::unique_ptr<Tool> OffloadWrapper;
146 
147   Tool *getClang() const;
148   Tool *getFlang() const;
149   Tool *getAssemble() const;
150   Tool *getLink() const;
151   Tool *getStaticLibTool() const;
152   Tool *getIfsMerge() const;
153   Tool *getClangAs() const;
154   Tool *getOffloadBundler() const;
155   Tool *getOffloadWrapper() const;
156 
157   mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
158   mutable std::unique_ptr<XRayArgs> XRayArguments;
159 
160   /// The effective clang triple for the current Job.
161   mutable llvm::Triple EffectiveTriple;
162 
163   /// Set the toolchain's effective clang triple.
setEffectiveTriple(llvm::Triple ET)164   void setEffectiveTriple(llvm::Triple ET) const {
165     EffectiveTriple = std::move(ET);
166   }
167 
168 protected:
169   MultilibSet Multilibs;
170   Multilib SelectedMultilib;
171 
172   ToolChain(const Driver &D, const llvm::Triple &T,
173             const llvm::opt::ArgList &Args);
174 
175   void setTripleEnvironment(llvm::Triple::EnvironmentType Env);
176 
177   virtual Tool *buildAssembler() const;
178   virtual Tool *buildLinker() const;
179   virtual Tool *buildStaticLibTool() const;
180   virtual Tool *getTool(Action::ActionClass AC) const;
181 
182   /// \name Utilities for implementing subclasses.
183   ///@{
184   static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
185                                llvm::opt::ArgStringList &CC1Args,
186                                const Twine &Path);
187   static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
188                                       llvm::opt::ArgStringList &CC1Args,
189                                       const Twine &Path);
190   static void
191       addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
192                                       llvm::opt::ArgStringList &CC1Args,
193                                       const Twine &Path);
194   static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
195                                 llvm::opt::ArgStringList &CC1Args,
196                                 ArrayRef<StringRef> Paths);
197   ///@}
198 
199 public:
200   virtual ~ToolChain();
201 
202   // Accessors
203 
getDriver()204   const Driver &getDriver() const { return D; }
205   llvm::vfs::FileSystem &getVFS() const;
getTriple()206   const llvm::Triple &getTriple() const { return Triple; }
207 
208   /// Get the toolchain's aux triple, if it has one.
209   ///
210   /// Exactly what the aux triple represents depends on the toolchain, but for
211   /// example when compiling CUDA code for the GPU, the triple might be NVPTX,
212   /// while the aux triple is the host (CPU) toolchain, e.g. x86-linux-gnu.
getAuxTriple()213   virtual const llvm::Triple *getAuxTriple() const { return nullptr; }
214 
215   /// Some toolchains need to modify the file name, for example to replace the
216   /// extension for object files with .cubin for OpenMP offloading to Nvidia
217   /// GPUs.
218   virtual std::string getInputFilename(const InputInfo &Input) const;
219 
getArch()220   llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
getArchName()221   StringRef getArchName() const { return Triple.getArchName(); }
getPlatform()222   StringRef getPlatform() const { return Triple.getVendorName(); }
getOS()223   StringRef getOS() const { return Triple.getOSName(); }
224 
225   /// Provide the default architecture name (as expected by -arch) for
226   /// this toolchain.
227   StringRef getDefaultUniversalArchName() const;
228 
getTripleString()229   std::string getTripleString() const {
230     return Triple.getTriple();
231   }
232 
233   /// Get the toolchain's effective clang triple.
getEffectiveTriple()234   const llvm::Triple &getEffectiveTriple() const {
235     assert(!EffectiveTriple.getTriple().empty() && "No effective triple");
236     return EffectiveTriple;
237   }
238 
getLibraryPaths()239   path_list &getLibraryPaths() { return LibraryPaths; }
getLibraryPaths()240   const path_list &getLibraryPaths() const { return LibraryPaths; }
241 
getFilePaths()242   path_list &getFilePaths() { return FilePaths; }
getFilePaths()243   const path_list &getFilePaths() const { return FilePaths; }
244 
getProgramPaths()245   path_list &getProgramPaths() { return ProgramPaths; }
getProgramPaths()246   const path_list &getProgramPaths() const { return ProgramPaths; }
247 
getMultilibs()248   const MultilibSet &getMultilibs() const { return Multilibs; }
249 
getMultilib()250   const Multilib &getMultilib() const { return SelectedMultilib; }
251 
252   const SanitizerArgs& getSanitizerArgs() const;
253 
254   const XRayArgs& getXRayArgs() const;
255 
256   // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
getRTTIArg()257   const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
258 
259   // Returns the RTTIMode for the toolchain with the current arguments.
getRTTIMode()260   RTTIMode getRTTIMode() const { return CachedRTTIMode; }
261 
262   /// Return any implicit target and/or mode flag for an invocation of
263   /// the compiler driver as `ProgName`.
264   ///
265   /// For example, when called with i686-linux-android-g++, the first element
266   /// of the return value will be set to `"i686-linux-android"` and the second
267   /// will be set to "--driver-mode=g++"`.
268   /// It is OK if the target name is not registered. In this case the return
269   /// value contains false in the field TargetIsValid.
270   ///
271   /// \pre `llvm::InitializeAllTargets()` has been called.
272   /// \param ProgName The name the Clang driver was invoked with (from,
273   /// e.g., argv[0]).
274   /// \return A structure of type ParsedClangName that contains the executable
275   /// name parts.
276   static ParsedClangName getTargetAndModeFromProgramName(StringRef ProgName);
277 
278   // Tool access.
279 
280   /// TranslateArgs - Create a new derived argument list for any argument
281   /// translations this ToolChain may wish to perform, or 0 if no tool chain
282   /// specific translations are needed. If \p DeviceOffloadKind is specified
283   /// the translation specific for that offload kind is performed.
284   ///
285   /// \param BoundArch - The bound architecture name, or 0.
286   /// \param DeviceOffloadKind - The device offload kind used for the
287   /// translation.
288   virtual llvm::opt::DerivedArgList *
TranslateArgs(const llvm::opt::DerivedArgList & Args,StringRef BoundArch,Action::OffloadKind DeviceOffloadKind)289   TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
290                 Action::OffloadKind DeviceOffloadKind) const {
291     return nullptr;
292   }
293 
294   /// TranslateOpenMPTargetArgs - Create a new derived argument list for
295   /// that contains the OpenMP target specific flags passed via
296   /// -Xopenmp-target -opt=val OR -Xopenmp-target=<triple> -opt=val
297   virtual llvm::opt::DerivedArgList *TranslateOpenMPTargetArgs(
298       const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost,
299       SmallVectorImpl<llvm::opt::Arg *> &AllocatedArgs) const;
300 
301   /// Append the argument following \p A to \p DAL assuming \p A is an Xarch
302   /// argument. If \p AllocatedArgs is null pointer, synthesized arguments are
303   /// added to \p DAL, otherwise they are appended to \p AllocatedArgs.
304   virtual void TranslateXarchArgs(
305       const llvm::opt::DerivedArgList &Args, llvm::opt::Arg *&A,
306       llvm::opt::DerivedArgList *DAL,
307       SmallVectorImpl<llvm::opt::Arg *> *AllocatedArgs = nullptr) const;
308 
309   /// Translate -Xarch_ arguments. If there are no such arguments, return
310   /// a null pointer, otherwise return a DerivedArgList containing the
311   /// translated arguments.
312   virtual llvm::opt::DerivedArgList *
313   TranslateXarchArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
314                      Action::OffloadKind DeviceOffloadKind,
315                      SmallVectorImpl<llvm::opt::Arg *> *AllocatedArgs) const;
316 
317   /// Choose a tool to use to handle the action \p JA.
318   ///
319   /// This can be overridden when a particular ToolChain needs to use
320   /// a compiler other than Clang.
321   virtual Tool *SelectTool(const JobAction &JA) const;
322 
323   // Helper methods
324 
325   std::string GetFilePath(const char *Name) const;
326   std::string GetProgramPath(const char *Name) const;
327 
328   /// Returns the linker path, respecting the -fuse-ld= argument to determine
329   /// the linker suffix or name.
330   /// If LinkerIsLLD is non-nullptr, it is set to true if the returned linker
331   /// is LLD. If it's set, it can be assumed that the linker is LLD built
332   /// at the same revision as clang, and clang can make assumptions about
333   /// LLD's supported flags, error output, etc.
334   /// If LinkerIsLLDDarwinNew is non-nullptr, it's set if the linker is
335   /// the new version in lld/MachO.
336   std::string GetLinkerPath(bool *LinkerIsLLD = nullptr,
337                             bool *LinkerIsLLDDarwinNew = nullptr) const;
338 
339   /// Returns the linker path for emitting a static library.
340   std::string GetStaticLibToolPath() const;
341 
342   /// Dispatch to the specific toolchain for verbose printing.
343   ///
344   /// This is used when handling the verbose option to print detailed,
345   /// toolchain-specific information useful for understanding the behavior of
346   /// the driver on a specific platform.
printVerboseInfo(raw_ostream & OS)347   virtual void printVerboseInfo(raw_ostream &OS) const {}
348 
349   // Platform defaults information
350 
351   /// Returns true if the toolchain is targeting a non-native
352   /// architecture.
353   virtual bool isCrossCompiling() const;
354 
355   /// HasNativeLTOLinker - Check whether the linker and related tools have
356   /// native LLVM support.
357   virtual bool HasNativeLLVMSupport() const;
358 
359   /// LookupTypeForExtension - Return the default language type to use for the
360   /// given extension.
361   virtual types::ID LookupTypeForExtension(StringRef Ext) const;
362 
363   /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
IsBlocksDefault()364   virtual bool IsBlocksDefault() const { return false; }
365 
366   /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
367   /// by default.
IsIntegratedAssemblerDefault()368   virtual bool IsIntegratedAssemblerDefault() const { return false; }
369 
370   /// Check if the toolchain should use the integrated assembler.
371   virtual bool useIntegratedAs() const;
372 
373   /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
IsMathErrnoDefault()374   virtual bool IsMathErrnoDefault() const { return true; }
375 
376   /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
377   /// -fencode-extended-block-signature by default.
IsEncodeExtendedBlockSignatureDefault()378   virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
379 
380   /// IsObjCNonFragileABIDefault - Does this tool chain set
381   /// -fobjc-nonfragile-abi by default.
IsObjCNonFragileABIDefault()382   virtual bool IsObjCNonFragileABIDefault() const { return false; }
383 
384   /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
385   /// mixed dispatch method be used?
UseObjCMixedDispatch()386   virtual bool UseObjCMixedDispatch() const { return false; }
387 
388   /// Check whether to enable x86 relax relocations by default.
389   virtual bool useRelaxRelocations() const;
390 
391   /// GetDefaultStackProtectorLevel - Get the default stack protector level for
392   /// this tool chain.
393   virtual LangOptions::StackProtectorMode
GetDefaultStackProtectorLevel(bool KernelOrKext)394   GetDefaultStackProtectorLevel(bool KernelOrKext) const {
395     return LangOptions::SSPOff;
396   }
397 
398   /// Get the default trivial automatic variable initialization.
399   virtual LangOptions::TrivialAutoVarInitKind
GetDefaultTrivialAutoVarInit()400   GetDefaultTrivialAutoVarInit() const {
401     return LangOptions::TrivialAutoVarInitKind::Uninitialized;
402   }
403 
404   /// GetDefaultLinker - Get the default linker to use.
getDefaultLinker()405   virtual const char *getDefaultLinker() const { return "ld"; }
406 
407   /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
GetDefaultRuntimeLibType()408   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
409     return ToolChain::RLT_Libgcc;
410   }
411 
GetDefaultCXXStdlibType()412   virtual CXXStdlibType GetDefaultCXXStdlibType() const {
413     return ToolChain::CST_Libstdcxx;
414   }
415 
GetDefaultUnwindLibType()416   virtual UnwindLibType GetDefaultUnwindLibType() const {
417     return ToolChain::UNW_None;
418   }
419 
420   virtual std::string getCompilerRTPath() const;
421 
422   virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
423                                     StringRef Component,
424                                     FileType Type = ToolChain::FT_Static) const;
425 
426   const char *
427   getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component,
428                          FileType Type = ToolChain::FT_Static) const;
429 
430   virtual std::string
431   getCompilerRTBasename(const llvm::opt::ArgList &Args, StringRef Component,
432                         FileType Type = ToolChain::FT_Static,
433                         bool AddArch = true) const;
434 
435   // Returns target specific runtime path if it exists.
436   virtual Optional<std::string> getRuntimePath() const;
437 
438   // Returns target specific C++ library path if it exists.
439   virtual Optional<std::string> getCXXStdlibPath() const;
440 
441   // Returns <ResourceDir>/lib/<OSName>/<arch>.  This is used by runtimes (such
442   // as OpenMP) to find arch-specific libraries.
443   std::string getArchSpecificLibPath() const;
444 
445   // Returns <OSname> part of above.
446   virtual StringRef getOSLibName() const;
447 
448   /// needsProfileRT - returns true if instrumentation profile is on.
449   static bool needsProfileRT(const llvm::opt::ArgList &Args);
450 
451   /// Returns true if gcov instrumentation (-fprofile-arcs or --coverage) is on.
452   static bool needsGCovInstrumentation(const llvm::opt::ArgList &Args);
453 
454   /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
455   /// by default.
456   virtual bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const;
457 
458   /// Test whether this toolchain defaults to PIC.
459   virtual bool isPICDefault() const = 0;
460 
461   /// Test whether this toolchain defaults to PIE.
462   virtual bool isPIEDefault() const = 0;
463 
464   /// Test whether this toolchaind defaults to non-executable stacks.
465   virtual bool isNoExecStackDefault() const;
466 
467   /// Tests whether this toolchain forces its default for PIC, PIE or
468   /// non-PIC.  If this returns true, any PIC related flags should be ignored
469   /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
470   /// used exclusively.
471   virtual bool isPICDefaultForced() const = 0;
472 
473   /// SupportsProfiling - Does this tool chain support -pg.
SupportsProfiling()474   virtual bool SupportsProfiling() const { return true; }
475 
476   /// Complain if this tool chain doesn't support Objective-C ARC.
CheckObjCARC()477   virtual void CheckObjCARC() const {}
478 
479   /// Get the default debug info format. Typically, this is DWARF.
getDefaultDebugFormat()480   virtual codegenoptions::DebugInfoFormat getDefaultDebugFormat() const {
481     return codegenoptions::DIF_DWARF;
482   }
483 
484   /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
485   /// compile unit information.
UseDwarfDebugFlags()486   virtual bool UseDwarfDebugFlags() const { return false; }
487 
488   // Return the DWARF version to emit, in the absence of arguments
489   // to the contrary.
GetDefaultDwarfVersion()490   virtual unsigned GetDefaultDwarfVersion() const { return 4; }
491 
492   // True if the driver should assume "-fstandalone-debug"
493   // in the absence of an option specifying otherwise,
494   // provided that debugging was requested in the first place.
495   // i.e. a value of 'true' does not imply that debugging is wanted.
GetDefaultStandaloneDebug()496   virtual bool GetDefaultStandaloneDebug() const { return false; }
497 
498   // Return the default debugger "tuning."
getDefaultDebuggerTuning()499   virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
500     return llvm::DebuggerKind::GDB;
501   }
502 
503   /// Does this toolchain supports given debug info option or not.
supportsDebugInfoOption(const llvm::opt::Arg *)504   virtual bool supportsDebugInfoOption(const llvm::opt::Arg *) const {
505     return true;
506   }
507 
508   /// Adjust debug information kind considering all passed options.
adjustDebugInfoKind(codegenoptions::DebugInfoKind & DebugInfoKind,const llvm::opt::ArgList & Args)509   virtual void adjustDebugInfoKind(codegenoptions::DebugInfoKind &DebugInfoKind,
510                                    const llvm::opt::ArgList &Args) const {}
511 
512   /// GetExceptionModel - Return the tool chain exception model.
513   virtual llvm::ExceptionHandling
514   GetExceptionModel(const llvm::opt::ArgList &Args) const;
515 
516   /// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
SupportsEmbeddedBitcode()517   virtual bool SupportsEmbeddedBitcode() const { return false; }
518 
519   /// getThreadModel() - Which thread model does this target use?
getThreadModel()520   virtual std::string getThreadModel() const { return "posix"; }
521 
522   /// isThreadModelSupported() - Does this target support a thread model?
523   virtual bool isThreadModelSupported(const StringRef Model) const;
524 
525   /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
526   /// command line arguments into account.
527   virtual std::string
528   ComputeLLVMTriple(const llvm::opt::ArgList &Args,
529                     types::ID InputType = types::TY_INVALID) const;
530 
531   /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
532   /// target, which may take into account the command line arguments. For
533   /// example, on Darwin the -mmacosx-version-min= command line argument (which
534   /// sets the deployment target) determines the version in the triple passed to
535   /// Clang.
536   virtual std::string ComputeEffectiveClangTriple(
537       const llvm::opt::ArgList &Args,
538       types::ID InputType = types::TY_INVALID) const;
539 
540   /// getDefaultObjCRuntime - Return the default Objective-C runtime
541   /// for this platform.
542   ///
543   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
544   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
545 
546   /// hasBlocksRuntime - Given that the user is compiling with
547   /// -fblocks, does this tool chain guarantee the existence of a
548   /// blocks runtime?
549   ///
550   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
hasBlocksRuntime()551   virtual bool hasBlocksRuntime() const { return true; }
552 
553   /// Return the sysroot, possibly searching for a default sysroot using
554   /// target-specific logic.
555   virtual std::string computeSysRoot() const;
556 
557   /// Add the clang cc1 arguments for system include paths.
558   ///
559   /// This routine is responsible for adding the necessary cc1 arguments to
560   /// include headers from standard system header directories.
561   virtual void
562   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
563                             llvm::opt::ArgStringList &CC1Args) const;
564 
565   /// Add options that need to be passed to cc1 for this target.
566   virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
567                                      llvm::opt::ArgStringList &CC1Args,
568                                      Action::OffloadKind DeviceOffloadKind) const;
569 
570   /// Add warning options that need to be passed to cc1 for this target.
571   virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
572 
573   // GetRuntimeLibType - Determine the runtime library type to use with the
574   // given compilation arguments.
575   virtual RuntimeLibType
576   GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
577 
578   // GetCXXStdlibType - Determine the C++ standard library type to use with the
579   // given compilation arguments.
580   virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
581 
582   // GetUnwindLibType - Determine the unwind library type to use with the
583   // given compilation arguments.
584   virtual UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const;
585 
586   /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
587   /// the include paths to use for the given C++ standard library type.
588   virtual void
589   AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
590                                llvm::opt::ArgStringList &CC1Args) const;
591 
592   /// AddClangCXXStdlibIsystemArgs - Add the clang -cc1 level arguments to set
593   /// the specified include paths for the C++ standard library.
594   void AddClangCXXStdlibIsystemArgs(const llvm::opt::ArgList &DriverArgs,
595                                     llvm::opt::ArgStringList &CC1Args) const;
596 
597   /// Returns if the C++ standard library should be linked in.
598   /// Note that e.g. -lm should still be linked even if this returns false.
599   bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const;
600 
601   /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
602   /// for the given C++ standard library type.
603   virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
604                                    llvm::opt::ArgStringList &CmdArgs) const;
605 
606   /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
607   void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
608                           llvm::opt::ArgStringList &CmdArgs) const;
609 
610   /// AddCCKextLibArgs - Add the system specific linker arguments to use
611   /// for kernel extensions (Darwin-specific).
612   virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
613                                 llvm::opt::ArgStringList &CmdArgs) const;
614 
615   /// If a runtime library exists that sets global flags for unsafe floating
616   /// point math, return true.
617   ///
618   /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
619   virtual bool isFastMathRuntimeAvailable(
620     const llvm::opt::ArgList &Args, std::string &Path) const;
621 
622   /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
623   /// global flags for unsafe floating point math, add it and return true.
624   ///
625   /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
626   bool addFastMathRuntimeIfAvailable(
627     const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
628 
629   /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
630   /// a suitable profile runtime library to the linker.
631   virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
632                                 llvm::opt::ArgStringList &CmdArgs) const;
633 
634   /// Add arguments to use system-specific CUDA includes.
635   virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
636                                   llvm::opt::ArgStringList &CC1Args) const;
637 
638   /// Add arguments to use system-specific HIP includes.
639   virtual void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
640                                  llvm::opt::ArgStringList &CC1Args) const;
641 
642   /// Add arguments to use MCU GCC toolchain includes.
643   virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
644                                    llvm::opt::ArgStringList &CC1Args) const;
645 
646   /// On Windows, returns the MSVC compatibility version.
647   virtual VersionTuple computeMSVCVersion(const Driver *D,
648                                           const llvm::opt::ArgList &Args) const;
649 
650   /// Return sanitizers which are available in this toolchain.
651   virtual SanitizerMask getSupportedSanitizers() const;
652 
653   /// Return sanitizers which are enabled by default.
getDefaultSanitizers()654   virtual SanitizerMask getDefaultSanitizers() const {
655     return SanitizerMask();
656   }
657 
658   /// Returns true when it's possible to split LTO unit to use whole
659   /// program devirtualization and CFI santiizers.
canSplitThinLTOUnit()660   virtual bool canSplitThinLTOUnit() const { return true; }
661 
662   /// Returns the output denormal handling type in the default floating point
663   /// environment for the given \p FPType if given. Otherwise, the default
664   /// assumed mode for any floating point type.
665   virtual llvm::DenormalMode getDefaultDenormalModeForType(
666       const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
667       const llvm::fltSemantics *FPType = nullptr) const {
668     return llvm::DenormalMode::getIEEE();
669   }
670 };
671 
672 /// Set a ToolChain's effective triple. Reset it when the registration object
673 /// is destroyed.
674 class RegisterEffectiveTriple {
675   const ToolChain &TC;
676 
677 public:
RegisterEffectiveTriple(const ToolChain & TC,llvm::Triple T)678   RegisterEffectiveTriple(const ToolChain &TC, llvm::Triple T) : TC(TC) {
679     TC.setEffectiveTriple(std::move(T));
680   }
681 
~RegisterEffectiveTriple()682   ~RegisterEffectiveTriple() { TC.setEffectiveTriple(llvm::Triple()); }
683 };
684 
685 } // namespace driver
686 
687 } // namespace clang
688 
689 #endif // LLVM_CLANG_DRIVER_TOOLCHAIN_H
690