ToolChain.h 30 KB

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