1 //===--- ToolChain.h - Collections of tools for one platform ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_DRIVER_TOOLCHAIN_H
11 #define LLVM_CLANG_DRIVER_TOOLCHAIN_H
13 #include "clang/Basic/Sanitizers.h"
14 #include "clang/Basic/VersionTuple.h"
15 #include "clang/Driver/Action.h"
16 #include "clang/Driver/Multilib.h"
17 #include "clang/Driver/Types.h"
18 #include "clang/Driver/Util.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Target/TargetOptions.h"
41 class CudaInstallationDetector;
44 class RegisterEffectiveTriple;
48 /// ToolChain - Access to tools for a single platform.
51 typedef SmallVector<std::string, 16> path_list;
66 RM_DisabledExplicitly,
72 const llvm::Triple Triple;
73 const llvm::opt::ArgList &Args;
74 // We need to initialize CachedRTTIArg before CachedRTTIMode
75 const llvm::opt::Arg *const CachedRTTIArg;
76 const RTTIMode CachedRTTIMode;
78 /// The list of toolchain specific path prefixes to search for
82 /// The list of toolchain specific path prefixes to search for
84 path_list ProgramPaths;
86 mutable std::unique_ptr<Tool> Clang;
87 mutable std::unique_ptr<Tool> Assemble;
88 mutable std::unique_ptr<Tool> Link;
89 mutable std::unique_ptr<Tool> OffloadBundler;
90 Tool *getClang() const;
91 Tool *getAssemble() const;
92 Tool *getLink() const;
93 Tool *getClangAs() const;
94 Tool *getOffloadBundler() const;
96 mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
98 /// The effective clang triple for the current Job.
99 mutable llvm::Triple EffectiveTriple;
101 /// Set the toolchain's effective clang triple.
102 void setEffectiveTriple(llvm::Triple ET) const { EffectiveTriple = ET; }
104 friend class RegisterEffectiveTriple;
107 MultilibSet Multilibs;
109 ToolChain(const Driver &D, const llvm::Triple &T,
110 const llvm::opt::ArgList &Args);
112 virtual Tool *buildAssembler() const;
113 virtual Tool *buildLinker() const;
114 virtual Tool *getTool(Action::ActionClass AC) const;
116 /// \name Utilities for implementing subclasses.
118 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
119 llvm::opt::ArgStringList &CC1Args,
121 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
122 llvm::opt::ArgStringList &CC1Args,
125 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
126 llvm::opt::ArgStringList &CC1Args,
128 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
129 llvm::opt::ArgStringList &CC1Args,
130 ArrayRef<StringRef> Paths);
134 virtual ~ToolChain();
138 const Driver &getDriver() const { return D; }
139 vfs::FileSystem &getVFS() const;
140 const llvm::Triple &getTriple() const { return Triple; }
142 llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
143 StringRef getArchName() const { return Triple.getArchName(); }
144 StringRef getPlatform() const { return Triple.getVendorName(); }
145 StringRef getOS() const { return Triple.getOSName(); }
147 /// \brief Provide the default architecture name (as expected by -arch) for
149 StringRef getDefaultUniversalArchName() const;
151 std::string getTripleString() const {
152 return Triple.getTriple();
155 /// Get the toolchain's effective clang triple.
156 const llvm::Triple &getEffectiveTriple() const {
157 assert(!EffectiveTriple.getTriple().empty() && "No effective triple");
158 return EffectiveTriple;
161 path_list &getFilePaths() { return FilePaths; }
162 const path_list &getFilePaths() const { return FilePaths; }
164 path_list &getProgramPaths() { return ProgramPaths; }
165 const path_list &getProgramPaths() const { return ProgramPaths; }
167 const MultilibSet &getMultilibs() const { return Multilibs; }
169 const SanitizerArgs& getSanitizerArgs() const;
171 // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
172 const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
174 // Returns the RTTIMode for the toolchain with the current arguments.
175 RTTIMode getRTTIMode() const { return CachedRTTIMode; }
177 /// \brief Return any implicit target and/or mode flag for an invocation of
178 /// the compiler driver as `ProgName`.
180 /// For example, when called with i686-linux-android-g++, the first element
181 /// of the return value will be set to `"i686-linux-android"` and the second
182 /// will be set to "--driver-mode=g++"`.
184 /// \pre `llvm::InitializeAllTargets()` has been called.
185 /// \param ProgName The name the Clang driver was invoked with (from,
187 /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
188 static std::pair<std::string, std::string>
189 getTargetAndModeFromProgramName(StringRef ProgName);
193 /// TranslateArgs - Create a new derived argument list for any argument
194 /// translations this ToolChain may wish to perform, or 0 if no tool chain
195 /// specific translations are needed. If \p DeviceOffloadKind is specified
196 /// the translation specific for that offload kind is performed.
198 /// \param BoundArch - The bound architecture name, or 0.
199 /// \param DeviceOffloadKind - The device offload kind used for the
201 virtual llvm::opt::DerivedArgList *
202 TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
203 Action::OffloadKind DeviceOffloadKind) const {
207 /// Choose a tool to use to handle the action \p JA.
209 /// This can be overridden when a particular ToolChain needs to use
210 /// a compiler other than Clang.
211 virtual Tool *SelectTool(const JobAction &JA) const;
215 std::string GetFilePath(const char *Name) const;
216 std::string GetProgramPath(const char *Name) const;
218 /// Returns the linker path, respecting the -fuse-ld= argument to determine
219 /// the linker suffix or name.
220 std::string GetLinkerPath() const;
222 /// \brief Dispatch to the specific toolchain for verbose printing.
224 /// This is used when handling the verbose option to print detailed,
225 /// toolchain-specific information useful for understanding the behavior of
226 /// the driver on a specific platform.
227 virtual void printVerboseInfo(raw_ostream &OS) const {}
229 // Platform defaults information
231 /// \brief Returns true if the toolchain is targeting a non-native
233 virtual bool isCrossCompiling() const;
235 /// HasNativeLTOLinker - Check whether the linker and related tools have
236 /// native LLVM support.
237 virtual bool HasNativeLLVMSupport() const;
239 /// LookupTypeForExtension - Return the default language type to use for the
241 virtual types::ID LookupTypeForExtension(StringRef Ext) const;
243 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
244 virtual bool IsBlocksDefault() const { return false; }
246 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
248 virtual bool IsIntegratedAssemblerDefault() const { return false; }
250 /// \brief Check if the toolchain should use the integrated assembler.
251 virtual bool useIntegratedAs() const;
253 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
254 virtual bool IsMathErrnoDefault() const { return true; }
256 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
257 /// -fencode-extended-block-signature by default.
258 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
260 /// IsObjCNonFragileABIDefault - Does this tool chain set
261 /// -fobjc-nonfragile-abi by default.
262 virtual bool IsObjCNonFragileABIDefault() const { return false; }
264 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
265 /// mixed dispatch method be used?
266 virtual bool UseObjCMixedDispatch() const { return false; }
268 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
269 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
270 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
274 /// GetDefaultLinker - Get the default linker to use.
275 virtual const char *getDefaultLinker() const {
279 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
280 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
281 return ToolChain::RLT_Libgcc;
284 virtual CXXStdlibType GetDefaultCXXStdlibType() const {
285 return ToolChain::CST_Libstdcxx;
288 virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
290 bool Shared = false) const;
292 const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
294 bool Shared = false) const;
295 /// needsProfileRT - returns true if instrumentation profile is on.
296 static bool needsProfileRT(const llvm::opt::ArgList &Args);
298 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
300 virtual bool IsUnwindTablesDefault() const;
302 /// \brief Test whether this toolchain defaults to PIC.
303 virtual bool isPICDefault() const = 0;
305 /// \brief Test whether this toolchain defaults to PIE.
306 virtual bool isPIEDefault() const = 0;
308 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
309 /// non-PIC. If this returns true, any PIC related flags should be ignored
310 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
311 /// used exclusively.
312 virtual bool isPICDefaultForced() const = 0;
314 /// SupportsProfiling - Does this tool chain support -pg.
315 virtual bool SupportsProfiling() const { return true; }
317 /// Does this tool chain support Objective-C garbage collection.
318 virtual bool SupportsObjCGC() const { return true; }
320 /// Complain if this tool chain doesn't support Objective-C ARC.
321 virtual void CheckObjCARC() const {}
323 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
324 /// compile unit information.
325 virtual bool UseDwarfDebugFlags() const { return false; }
327 // Return the DWARF version to emit, in the absence of arguments
329 virtual unsigned GetDefaultDwarfVersion() const { return 4; }
331 // True if the driver should assume "-fstandalone-debug"
332 // in the absence of an option specifying otherwise,
333 // provided that debugging was requested in the first place.
334 // i.e. a value of 'true' does not imply that debugging is wanted.
335 virtual bool GetDefaultStandaloneDebug() const { return false; }
337 // Return the default debugger "tuning."
338 virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
339 return llvm::DebuggerKind::GDB;
342 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
343 virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
347 /// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
348 virtual bool SupportsEmbeddedBitcode() const {
352 /// getThreadModel() - Which thread model does this target use?
353 virtual std::string getThreadModel() const { return "posix"; }
355 /// isThreadModelSupported() - Does this target support a thread model?
356 virtual bool isThreadModelSupported(const StringRef Model) const;
358 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
359 /// command line arguments into account.
361 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
362 types::ID InputType = types::TY_INVALID) const;
364 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
365 /// target, which may take into account the command line arguments. For
366 /// example, on Darwin the -mmacosx-version-min= command line argument (which
367 /// sets the deployment target) determines the version in the triple passed to
369 virtual std::string ComputeEffectiveClangTriple(
370 const llvm::opt::ArgList &Args,
371 types::ID InputType = types::TY_INVALID) const;
373 /// getDefaultObjCRuntime - Return the default Objective-C runtime
374 /// for this platform.
376 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
377 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
379 /// hasBlocksRuntime - Given that the user is compiling with
380 /// -fblocks, does this tool chain guarantee the existence of a
383 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
384 virtual bool hasBlocksRuntime() const { return true; }
386 /// \brief Add the clang cc1 arguments for system include paths.
388 /// This routine is responsible for adding the necessary cc1 arguments to
389 /// include headers from standard system header directories.
391 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
392 llvm::opt::ArgStringList &CC1Args) const;
394 /// \brief Add options that need to be passed to cc1 for this target.
395 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
396 llvm::opt::ArgStringList &CC1Args) const;
398 /// \brief Add warning options that need to be passed to cc1 for this target.
399 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
401 // GetRuntimeLibType - Determine the runtime library type to use with the
402 // given compilation arguments.
403 virtual RuntimeLibType
404 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
406 // GetCXXStdlibType - Determine the C++ standard library type to use with the
407 // given compilation arguments.
408 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
410 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
411 /// the include paths to use for the given C++ standard library type.
413 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
414 llvm::opt::ArgStringList &CC1Args) const;
416 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
417 /// for the given C++ standard library type.
418 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
419 llvm::opt::ArgStringList &CmdArgs) const;
421 /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
422 void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
423 llvm::opt::ArgStringList &CmdArgs) const;
425 /// AddCCKextLibArgs - Add the system specific linker arguments to use
426 /// for kernel extensions (Darwin-specific).
427 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
428 llvm::opt::ArgStringList &CmdArgs) const;
430 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
431 /// global flags for unsafe floating point math, add it and return true.
433 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
434 virtual bool AddFastMathRuntimeIfAvailable(
435 const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
436 /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
437 /// a suitable profile runtime library to the linker.
438 virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
439 llvm::opt::ArgStringList &CmdArgs) const;
441 /// \brief Add arguments to use system-specific CUDA includes.
442 virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
443 llvm::opt::ArgStringList &CC1Args) const;
445 /// \brief Add arguments to use MCU GCC toolchain includes.
446 virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
447 llvm::opt::ArgStringList &CC1Args) const;
449 /// \brief On Windows, returns the MSVC compatibility version.
450 virtual VersionTuple computeMSVCVersion(const Driver *D,
451 const llvm::opt::ArgList &Args) const;
453 /// \brief Return sanitizers which are available in this toolchain.
454 virtual SanitizerMask getSupportedSanitizers() const;
456 /// \brief Return sanitizers which are enabled by default.
457 virtual SanitizerMask getDefaultSanitizers() const { return 0; }
460 /// Set a ToolChain's effective triple. Reset it when the registration object
462 class RegisterEffectiveTriple {
466 RegisterEffectiveTriple(const ToolChain &TC, llvm::Triple T) : TC(TC) {
467 TC.setEffectiveTriple(T);
470 ~RegisterEffectiveTriple() { TC.setEffectiveTriple(llvm::Triple()); }
473 } // end namespace driver
474 } // end namespace clang