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/Support/Path.h"
22 #include "llvm/Target/TargetOptions.h"
47 /// ToolChain - Access to tools for a single platform.
50 typedef SmallVector<std::string, 16> path_list;
65 RM_DisabledExplicitly,
71 const llvm::Triple Triple;
72 const llvm::opt::ArgList &Args;
73 // We need to initialize CachedRTTIArg before CachedRTTIMode
74 const llvm::opt::Arg *const CachedRTTIArg;
75 const RTTIMode CachedRTTIMode;
77 /// The list of toolchain specific path prefixes to search for
81 /// The list of toolchain specific path prefixes to search for
83 path_list ProgramPaths;
85 mutable std::unique_ptr<Tool> Clang;
86 mutable std::unique_ptr<Tool> Assemble;
87 mutable std::unique_ptr<Tool> Link;
88 Tool *getClang() const;
89 Tool *getAssemble() const;
90 Tool *getLink() const;
91 Tool *getClangAs() const;
93 mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
96 MultilibSet Multilibs;
97 const char *DefaultLinker = "ld";
99 ToolChain(const Driver &D, const llvm::Triple &T,
100 const llvm::opt::ArgList &Args);
102 virtual Tool *buildAssembler() const;
103 virtual Tool *buildLinker() const;
104 virtual Tool *getTool(Action::ActionClass AC) const;
106 /// \name Utilities for implementing subclasses.
108 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
109 llvm::opt::ArgStringList &CC1Args,
111 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
112 llvm::opt::ArgStringList &CC1Args,
115 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
116 llvm::opt::ArgStringList &CC1Args,
118 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
119 llvm::opt::ArgStringList &CC1Args,
120 ArrayRef<StringRef> Paths);
124 virtual ~ToolChain();
128 const Driver &getDriver() const { return D; }
129 vfs::FileSystem &getVFS() const;
130 const llvm::Triple &getTriple() const { return Triple; }
132 llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
133 StringRef getArchName() const { return Triple.getArchName(); }
134 StringRef getPlatform() const { return Triple.getVendorName(); }
135 StringRef getOS() const { return Triple.getOSName(); }
137 /// \brief Provide the default architecture name (as expected by -arch) for
139 StringRef getDefaultUniversalArchName() const;
141 std::string getTripleString() const {
142 return Triple.getTriple();
145 path_list &getFilePaths() { return FilePaths; }
146 const path_list &getFilePaths() const { return FilePaths; }
148 path_list &getProgramPaths() { return ProgramPaths; }
149 const path_list &getProgramPaths() const { return ProgramPaths; }
151 const MultilibSet &getMultilibs() const { return Multilibs; }
153 const SanitizerArgs& getSanitizerArgs() const;
155 // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
156 const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
158 // Returns the RTTIMode for the toolchain with the current arguments.
159 RTTIMode getRTTIMode() const { return CachedRTTIMode; }
161 /// \brief Return any implicit target and/or mode flag for an invocation of
162 /// the compiler driver as `ProgName`.
164 /// For example, when called with i686-linux-android-g++, the first element
165 /// of the return value will be set to `"i686-linux-android"` and the second
166 /// will be set to "--driver-mode=g++"`.
168 /// \pre `llvm::InitializeAllTargets()` has been called.
169 /// \param ProgName The name the Clang driver was invoked with (from,
171 /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
172 static std::pair<std::string, std::string>
173 getTargetAndModeFromProgramName(StringRef ProgName);
177 /// TranslateArgs - Create a new derived argument list for any argument
178 /// translations this ToolChain may wish to perform, or 0 if no tool chain
179 /// specific translations are needed.
181 /// \param BoundArch - The bound architecture name, or 0.
182 virtual llvm::opt::DerivedArgList *
183 TranslateArgs(const llvm::opt::DerivedArgList &Args,
184 const char *BoundArch) const {
188 /// Choose a tool to use to handle the action \p JA.
190 /// This can be overridden when a particular ToolChain needs to use
191 /// a compiler other than Clang.
192 virtual Tool *SelectTool(const JobAction &JA) const;
196 std::string GetFilePath(const char *Name) const;
197 std::string GetProgramPath(const char *Name) const;
199 /// Returns the linker path, respecting the -fuse-ld= argument to determine
200 /// the linker suffix or name.
201 std::string GetLinkerPath() const;
203 /// \brief Dispatch to the specific toolchain for verbose printing.
205 /// This is used when handling the verbose option to print detailed,
206 /// toolchain-specific information useful for understanding the behavior of
207 /// the driver on a specific platform.
208 virtual void printVerboseInfo(raw_ostream &OS) const {}
210 // Platform defaults information
212 /// \brief Returns true if the toolchain is targeting a non-native
214 virtual bool isCrossCompiling() const;
216 /// HasNativeLTOLinker - Check whether the linker and related tools have
217 /// native LLVM support.
218 virtual bool HasNativeLLVMSupport() const;
220 /// LookupTypeForExtension - Return the default language type to use for the
222 virtual types::ID LookupTypeForExtension(const char *Ext) const;
224 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
225 virtual bool IsBlocksDefault() const { return false; }
227 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
229 virtual bool IsIntegratedAssemblerDefault() const { return false; }
231 /// \brief Check if the toolchain should use the integrated assembler.
232 virtual bool useIntegratedAs() const;
234 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
235 virtual bool IsMathErrnoDefault() const { return true; }
237 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
238 /// -fencode-extended-block-signature by default.
239 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
241 /// IsObjCNonFragileABIDefault - Does this tool chain set
242 /// -fobjc-nonfragile-abi by default.
243 virtual bool IsObjCNonFragileABIDefault() const { return false; }
245 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
246 /// mixed dispatch method be used?
247 virtual bool UseObjCMixedDispatch() const { return false; }
249 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
250 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
251 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
255 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
256 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
257 return ToolChain::RLT_Libgcc;
260 virtual CXXStdlibType GetDefaultCXXStdlibType() const {
261 return ToolChain::CST_Libstdcxx;
264 virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
266 bool Shared = false) const;
268 const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
270 bool Shared = false) const;
271 /// needsProfileRT - returns true if instrumentation profile is on.
272 static bool needsProfileRT(const llvm::opt::ArgList &Args);
274 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
276 virtual bool IsUnwindTablesDefault() const;
278 /// \brief Test whether this toolchain defaults to PIC.
279 virtual bool isPICDefault() const = 0;
281 /// \brief Test whether this toolchain defaults to PIE.
282 virtual bool isPIEDefault() const = 0;
284 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
285 /// non-PIC. If this returns true, any PIC related flags should be ignored
286 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
287 /// used exclusively.
288 virtual bool isPICDefaultForced() const = 0;
290 /// SupportsProfiling - Does this tool chain support -pg.
291 virtual bool SupportsProfiling() const { return true; }
293 /// Does this tool chain support Objective-C garbage collection.
294 virtual bool SupportsObjCGC() const { return true; }
296 /// Complain if this tool chain doesn't support Objective-C ARC.
297 virtual void CheckObjCARC() const {}
299 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
300 /// compile unit information.
301 virtual bool UseDwarfDebugFlags() const { return false; }
303 // Return the DWARF version to emit, in the absence of arguments
305 virtual unsigned GetDefaultDwarfVersion() const { return 4; }
307 // True if the driver should assume "-fstandalone-debug"
308 // in the absence of an option specifying otherwise,
309 // provided that debugging was requested in the first place.
310 // i.e. a value of 'true' does not imply that debugging is wanted.
311 virtual bool GetDefaultStandaloneDebug() const { return false; }
313 // Return the default debugger "tuning."
314 virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
315 return llvm::DebuggerKind::GDB;
318 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
319 virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
323 /// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
324 virtual bool SupportsEmbeddedBitcode() const {
328 /// getThreadModel() - Which thread model does this target use?
329 virtual std::string getThreadModel() const { return "posix"; }
331 /// isThreadModelSupported() - Does this target support a thread model?
332 virtual bool isThreadModelSupported(const StringRef Model) const;
334 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
335 /// command line arguments into account.
337 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
338 types::ID InputType = types::TY_INVALID) const;
340 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
341 /// target, which may take into account the command line arguments. For
342 /// example, on Darwin the -mmacosx-version-min= command line argument (which
343 /// sets the deployment target) determines the version in the triple passed to
345 virtual std::string ComputeEffectiveClangTriple(
346 const llvm::opt::ArgList &Args,
347 types::ID InputType = types::TY_INVALID) const;
349 /// getDefaultObjCRuntime - Return the default Objective-C runtime
350 /// for this platform.
352 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
353 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
355 /// hasBlocksRuntime - Given that the user is compiling with
356 /// -fblocks, does this tool chain guarantee the existence of a
359 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
360 virtual bool hasBlocksRuntime() const { return true; }
362 /// \brief Add the clang cc1 arguments for system include paths.
364 /// This routine is responsible for adding the necessary cc1 arguments to
365 /// include headers from standard system header directories.
367 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
368 llvm::opt::ArgStringList &CC1Args) const;
370 /// \brief Add options that need to be passed to cc1 for this target.
371 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
372 llvm::opt::ArgStringList &CC1Args) const;
374 /// \brief Add warning options that need to be passed to cc1 for this target.
375 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
377 // GetRuntimeLibType - Determine the runtime library type to use with the
378 // given compilation arguments.
379 virtual RuntimeLibType
380 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
382 // GetCXXStdlibType - Determine the C++ standard library type to use with the
383 // given compilation arguments.
384 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
386 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
387 /// the include paths to use for the given C++ standard library type.
389 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
390 llvm::opt::ArgStringList &CC1Args) const;
392 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
393 /// for the given C++ standard library type.
394 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
395 llvm::opt::ArgStringList &CmdArgs) const;
397 /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
398 void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
399 llvm::opt::ArgStringList &CmdArgs) const;
401 /// AddCCKextLibArgs - Add the system specific linker arguments to use
402 /// for kernel extensions (Darwin-specific).
403 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
404 llvm::opt::ArgStringList &CmdArgs) const;
406 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
407 /// global flags for unsafe floating point math, add it and return true.
409 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
410 virtual bool AddFastMathRuntimeIfAvailable(
411 const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
412 /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
413 /// a suitable profile runtime library to the linker.
414 virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
415 llvm::opt::ArgStringList &CmdArgs) const;
417 /// \brief Add arguments to use system-specific CUDA includes.
418 virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
419 llvm::opt::ArgStringList &CC1Args) const;
421 /// \brief Add arguments to use MCU GCC toolchain includes.
422 virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
423 llvm::opt::ArgStringList &CC1Args) const;
425 /// \brief Return sanitizers which are available in this toolchain.
426 virtual SanitizerMask getSupportedSanitizers() const;
428 /// \brief Return sanitizers which are enabled by default.
429 virtual SanitizerMask getDefaultSanitizers() const { return 0; }
431 /// \brief On Windows, returns the version of cl.exe. On other platforms,
432 /// returns an empty VersionTuple.
433 virtual VersionTuple getMSVCVersionFromExe() const { return VersionTuple(); }
436 } // end namespace driver
437 } // end namespace clang