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/Driver/Action.h"
15 #include "clang/Driver/Multilib.h"
16 #include "clang/Driver/Types.h"
17 #include "clang/Driver/Util.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Support/Path.h"
45 /// ToolChain - Access to tools for a single platform.
48 typedef SmallVector<std::string, 16> path_list;
63 RM_DisabledExplicitly,
69 const llvm::Triple Triple;
70 const llvm::opt::ArgList &Args;
71 // We need to initialize CachedRTTIArg before CachedRTTIMode
72 const llvm::opt::Arg *const CachedRTTIArg;
73 const RTTIMode CachedRTTIMode;
75 /// The list of toolchain specific path prefixes to search for
79 /// The list of toolchain specific path prefixes to search for
81 path_list ProgramPaths;
83 mutable std::unique_ptr<Tool> Clang;
84 mutable std::unique_ptr<Tool> Assemble;
85 mutable std::unique_ptr<Tool> Link;
86 Tool *getClang() const;
87 Tool *getAssemble() const;
88 Tool *getLink() const;
89 Tool *getClangAs() const;
91 mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
94 MultilibSet Multilibs;
96 ToolChain(const Driver &D, const llvm::Triple &T,
97 const llvm::opt::ArgList &Args);
99 virtual Tool *buildAssembler() const;
100 virtual Tool *buildLinker() const;
101 virtual Tool *getTool(Action::ActionClass AC) const;
103 /// \name Utilities for implementing subclasses.
105 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
106 llvm::opt::ArgStringList &CC1Args,
108 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
109 llvm::opt::ArgStringList &CC1Args,
112 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
113 llvm::opt::ArgStringList &CC1Args,
115 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
116 llvm::opt::ArgStringList &CC1Args,
117 ArrayRef<StringRef> Paths);
121 virtual ~ToolChain();
125 const Driver &getDriver() const { return D; }
126 vfs::FileSystem &getVFS() const;
127 const llvm::Triple &getTriple() const { return Triple; }
129 llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
130 StringRef getArchName() const { return Triple.getArchName(); }
131 StringRef getPlatform() const { return Triple.getVendorName(); }
132 StringRef getOS() const { return Triple.getOSName(); }
134 /// \brief Provide the default architecture name (as expected by -arch) for
135 /// this toolchain. Note t
136 StringRef getDefaultUniversalArchName() const;
138 std::string getTripleString() const {
139 return Triple.getTriple();
142 path_list &getFilePaths() { return FilePaths; }
143 const path_list &getFilePaths() const { return FilePaths; }
145 path_list &getProgramPaths() { return ProgramPaths; }
146 const path_list &getProgramPaths() const { return ProgramPaths; }
148 const MultilibSet &getMultilibs() const { return Multilibs; }
150 const SanitizerArgs& getSanitizerArgs() const;
152 // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
153 const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
155 // Returns the RTTIMode for the toolchain with the current arguments.
156 RTTIMode getRTTIMode() const { return CachedRTTIMode; }
158 /// \brief Return any implicit target and/or mode flag for an invocation of
159 /// the compiler driver as `ProgName`.
161 /// For example, when called with i686-linux-android-g++, the first element
162 /// of the return value will be set to `"i686-linux-android"` and the second
163 /// will be set to "--driver-mode=g++"`.
165 /// \pre `llvm::InitializeAllTargets()` has been called.
166 /// \param ProgName The name the Clang driver was invoked with (from,
168 /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
169 static std::pair<std::string, std::string>
170 getTargetAndModeFromProgramName(StringRef ProgName);
174 /// TranslateArgs - Create a new derived argument list for any argument
175 /// translations this ToolChain may wish to perform, or 0 if no tool chain
176 /// specific translations are needed.
178 /// \param BoundArch - The bound architecture name, or 0.
179 virtual llvm::opt::DerivedArgList *
180 TranslateArgs(const llvm::opt::DerivedArgList &Args,
181 const char *BoundArch) const {
185 /// Choose a tool to use to handle the action \p JA.
187 /// This can be overridden when a particular ToolChain needs to use
188 /// a C compiler other than Clang.
189 virtual Tool *SelectTool(const JobAction &JA) const;
193 std::string GetFilePath(const char *Name) const;
194 std::string GetProgramPath(const char *Name) const;
196 /// Returns the linker path, respecting the -fuse-ld= argument to determine
197 /// the linker suffix or name.
198 std::string GetLinkerPath() const;
200 /// \brief Dispatch to the specific toolchain for verbose printing.
202 /// This is used when handling the verbose option to print detailed,
203 /// toolchain-specific information useful for understanding the behavior of
204 /// the driver on a specific platform.
205 virtual void printVerboseInfo(raw_ostream &OS) const {}
207 // Platform defaults information
209 /// \brief Returns true if the toolchain is targeting a non-native
211 virtual bool isCrossCompiling() const;
213 /// HasNativeLTOLinker - Check whether the linker and related tools have
214 /// native LLVM support.
215 virtual bool HasNativeLLVMSupport() const;
217 /// LookupTypeForExtension - Return the default language type to use for the
219 virtual types::ID LookupTypeForExtension(const char *Ext) const;
221 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
222 virtual bool IsBlocksDefault() const { return false; }
224 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
226 virtual bool IsIntegratedAssemblerDefault() const { return false; }
228 /// \brief Check if the toolchain should use the integrated assembler.
229 bool useIntegratedAs() const;
231 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
232 virtual bool IsMathErrnoDefault() const { return true; }
234 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
235 /// -fencode-extended-block-signature by default.
236 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
238 /// IsObjCNonFragileABIDefault - Does this tool chain set
239 /// -fobjc-nonfragile-abi by default.
240 virtual bool IsObjCNonFragileABIDefault() const { return false; }
242 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
243 /// mixed dispatch method be used?
244 virtual bool UseObjCMixedDispatch() const { return false; }
246 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
247 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
248 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
252 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
253 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
254 return ToolChain::RLT_Libgcc;
257 virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
259 bool Shared = false) const;
261 const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
263 bool Shared = false) const;
264 /// needsProfileRT - returns true if instrumentation profile is on.
265 static bool needsProfileRT(const llvm::opt::ArgList &Args);
267 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
269 virtual bool IsUnwindTablesDefault() const;
271 /// \brief Test whether this toolchain defaults to PIC.
272 virtual bool isPICDefault() const = 0;
274 /// \brief Test whether this toolchain defaults to PIE.
275 virtual bool isPIEDefault() const = 0;
277 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
278 /// non-PIC. If this returns true, any PIC related flags should be ignored
279 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
280 /// used exclusively.
281 virtual bool isPICDefaultForced() const = 0;
283 /// SupportsProfiling - Does this tool chain support -pg.
284 virtual bool SupportsProfiling() const { return true; }
286 /// Does this tool chain support Objective-C garbage collection.
287 virtual bool SupportsObjCGC() const { return true; }
289 /// Complain if this tool chain doesn't support Objective-C ARC.
290 virtual void CheckObjCARC() const {}
292 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
293 /// compile unit information.
294 virtual bool UseDwarfDebugFlags() const { return false; }
296 // Return the DWARF version to emit, in the absence of arguments
298 virtual unsigned GetDefaultDwarfVersion() const { return 4; }
300 // True if the driver should assume "-fstandalone-debug"
301 // in the absence of an option specifying otherwise,
302 // provided that debugging was requested in the first place.
303 // i.e. a value of 'true' does not imply that debugging is wanted.
304 virtual bool GetDefaultStandaloneDebug() const { return false; }
306 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
307 virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
311 /// getThreadModel() - Which thread model does this target use?
312 virtual std::string getThreadModel() const { return "posix"; }
314 /// isThreadModelSupported() - Does this target support a thread model?
315 virtual bool isThreadModelSupported(const StringRef Model) const;
317 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
318 /// command line arguments into account.
320 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
321 types::ID InputType = types::TY_INVALID) const;
323 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
324 /// target, which may take into account the command line arguments. For
325 /// example, on Darwin the -mmacosx-version-min= command line argument (which
326 /// sets the deployment target) determines the version in the triple passed to
328 virtual std::string ComputeEffectiveClangTriple(
329 const llvm::opt::ArgList &Args,
330 types::ID InputType = types::TY_INVALID) const;
332 /// getDefaultObjCRuntime - Return the default Objective-C runtime
333 /// for this platform.
335 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
336 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
338 /// hasBlocksRuntime - Given that the user is compiling with
339 /// -fblocks, does this tool chain guarantee the existence of a
342 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
343 virtual bool hasBlocksRuntime() const { return true; }
345 /// \brief Add the clang cc1 arguments for system include paths.
347 /// This routine is responsible for adding the necessary cc1 arguments to
348 /// include headers from standard system header directories.
350 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
351 llvm::opt::ArgStringList &CC1Args) const;
353 /// \brief Add options that need to be passed to cc1 for this target.
354 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
355 llvm::opt::ArgStringList &CC1Args) const;
357 /// \brief Add warning options that need to be passed to cc1 for this target.
358 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
360 // GetRuntimeLibType - Determine the runtime library type to use with the
361 // given compilation arguments.
362 virtual RuntimeLibType
363 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
365 // GetCXXStdlibType - Determine the C++ standard library type to use with the
366 // given compilation arguments.
367 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
369 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
370 /// the include paths to use for the given C++ standard library type.
372 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
373 llvm::opt::ArgStringList &CC1Args) const;
375 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
376 /// for the given C++ standard library type.
377 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
378 llvm::opt::ArgStringList &CmdArgs) const;
380 /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
381 void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
382 llvm::opt::ArgStringList &CmdArgs) const;
384 /// AddCCKextLibArgs - Add the system specific linker arguments to use
385 /// for kernel extensions (Darwin-specific).
386 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
387 llvm::opt::ArgStringList &CmdArgs) const;
389 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
390 /// global flags for unsafe floating point math, add it and return true.
392 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
393 virtual bool AddFastMathRuntimeIfAvailable(
394 const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
395 /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
396 /// a suitable profile runtime library to the linker.
397 virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
398 llvm::opt::ArgStringList &CmdArgs) const;
400 /// \brief Return sanitizers which are available in this toolchain.
401 virtual SanitizerMask getSupportedSanitizers() const;
404 } // end namespace driver
405 } // end namespace clang