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