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"
42 /// ToolChain - Access to tools for a single platform.
45 typedef SmallVector<std::string, 16> path_list;
60 RM_DisabledExplicitly,
66 const llvm::Triple Triple;
67 const llvm::opt::ArgList &Args;
68 // We need to initialize CachedRTTIArg before CachedRTTIMode
69 const llvm::opt::Arg *const CachedRTTIArg;
70 const RTTIMode CachedRTTIMode;
72 /// The list of toolchain specific path prefixes to search for
76 /// The list of toolchain specific path prefixes to search for
78 path_list ProgramPaths;
80 mutable std::unique_ptr<Tool> Clang;
81 mutable std::unique_ptr<Tool> Assemble;
82 mutable std::unique_ptr<Tool> Link;
83 Tool *getClang() const;
84 Tool *getAssemble() const;
85 Tool *getLink() const;
86 Tool *getClangAs() const;
88 mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
91 MultilibSet Multilibs;
93 ToolChain(const Driver &D, const llvm::Triple &T,
94 const llvm::opt::ArgList &Args);
96 virtual Tool *buildAssembler() const;
97 virtual Tool *buildLinker() const;
98 virtual Tool *getTool(Action::ActionClass AC) const;
100 /// \name Utilities for implementing subclasses.
102 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
103 llvm::opt::ArgStringList &CC1Args,
105 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
106 llvm::opt::ArgStringList &CC1Args,
109 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
110 llvm::opt::ArgStringList &CC1Args,
112 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
113 llvm::opt::ArgStringList &CC1Args,
114 ArrayRef<StringRef> Paths);
118 virtual ~ToolChain();
122 const Driver &getDriver() const;
123 const llvm::Triple &getTriple() const { return Triple; }
125 llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
126 StringRef getArchName() const { return Triple.getArchName(); }
127 StringRef getPlatform() const { return Triple.getVendorName(); }
128 StringRef getOS() const { return Triple.getOSName(); }
130 /// \brief Provide the default architecture name (as expected by -arch) for
131 /// this toolchain. Note t
132 StringRef getDefaultUniversalArchName() const;
134 std::string getTripleString() const {
135 return Triple.getTriple();
138 path_list &getFilePaths() { return FilePaths; }
139 const path_list &getFilePaths() const { return FilePaths; }
141 path_list &getProgramPaths() { return ProgramPaths; }
142 const path_list &getProgramPaths() const { return ProgramPaths; }
144 const MultilibSet &getMultilibs() const { return Multilibs; }
146 const SanitizerArgs& getSanitizerArgs() const;
148 // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
149 const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
151 // Returns the RTTIMode for the toolchain with the current arguments.
152 RTTIMode getRTTIMode() const { return CachedRTTIMode; }
156 /// TranslateArgs - Create a new derived argument list for any argument
157 /// translations this ToolChain may wish to perform, or 0 if no tool chain
158 /// specific translations are needed.
160 /// \param BoundArch - The bound architecture name, or 0.
161 virtual llvm::opt::DerivedArgList *
162 TranslateArgs(const llvm::opt::DerivedArgList &Args,
163 const char *BoundArch) const {
167 /// Choose a tool to use to handle the action \p JA.
169 /// This can be overridden when a particular ToolChain needs to use
170 /// a C compiler other than Clang.
171 virtual Tool *SelectTool(const JobAction &JA) const;
175 std::string GetFilePath(const char *Name) const;
176 std::string GetProgramPath(const char *Name) const;
178 /// Returns the linker path, respecting the -fuse-ld= argument to determine
179 /// the linker suffix or name.
180 std::string GetLinkerPath() const;
182 /// \brief Dispatch to the specific toolchain for verbose printing.
184 /// This is used when handling the verbose option to print detailed,
185 /// toolchain-specific information useful for understanding the behavior of
186 /// the driver on a specific platform.
187 virtual void printVerboseInfo(raw_ostream &OS) const {};
189 // Platform defaults information
191 /// \brief Returns true if the toolchain is targeting a non-native
193 virtual bool isCrossCompiling() const;
195 /// HasNativeLTOLinker - Check whether the linker and related tools have
196 /// native LLVM support.
197 virtual bool HasNativeLLVMSupport() const;
199 /// LookupTypeForExtension - Return the default language type to use for the
201 virtual types::ID LookupTypeForExtension(const char *Ext) const;
203 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
204 virtual bool IsBlocksDefault() const { return false; }
206 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
208 virtual bool IsIntegratedAssemblerDefault() const { return false; }
210 /// \brief Check if the toolchain should use the integrated assembler.
211 bool useIntegratedAs() const;
213 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
214 virtual bool IsMathErrnoDefault() const { return true; }
216 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
217 /// -fencode-extended-block-signature by default.
218 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
220 /// IsObjCNonFragileABIDefault - Does this tool chain set
221 /// -fobjc-nonfragile-abi by default.
222 virtual bool IsObjCNonFragileABIDefault() const { return false; }
224 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
225 /// mixed dispatch method be used?
226 virtual bool UseObjCMixedDispatch() const { return false; }
228 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
229 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
230 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
234 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
235 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
236 return ToolChain::RLT_Libgcc;
239 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
241 virtual bool IsUnwindTablesDefault() const;
243 /// \brief Test whether this toolchain defaults to PIC.
244 virtual bool isPICDefault() const = 0;
246 /// \brief Test whether this toolchain defaults to PIE.
247 virtual bool isPIEDefault() const = 0;
249 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
250 /// non-PIC. If this returns true, any PIC related flags should be ignored
251 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
252 /// used exclusively.
253 virtual bool isPICDefaultForced() const = 0;
255 /// SupportsProfiling - Does this tool chain support -pg.
256 virtual bool SupportsProfiling() const { return true; }
258 /// Does this tool chain support Objective-C garbage collection.
259 virtual bool SupportsObjCGC() const { return true; }
261 /// Complain if this tool chain doesn't support Objective-C ARC.
262 virtual void CheckObjCARC() const {}
264 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
265 /// compile unit information.
266 virtual bool UseDwarfDebugFlags() const { return false; }
268 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
269 virtual bool UseSjLjExceptions() const { return false; }
271 /// getThreadModel() - Which thread model does this target use?
272 virtual std::string getThreadModel() const { return "posix"; }
274 /// isThreadModelSupported() - Does this target support a thread model?
275 virtual bool isThreadModelSupported(const StringRef Model) const;
277 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
278 /// command line arguments into account.
280 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
281 types::ID InputType = types::TY_INVALID) const;
283 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
284 /// target, which may take into account the command line arguments. For
285 /// example, on Darwin the -mmacosx-version-min= command line argument (which
286 /// sets the deployment target) determines the version in the triple passed to
288 virtual std::string ComputeEffectiveClangTriple(
289 const llvm::opt::ArgList &Args,
290 types::ID InputType = types::TY_INVALID) const;
292 /// getDefaultObjCRuntime - Return the default Objective-C runtime
293 /// for this platform.
295 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
296 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
298 /// hasBlocksRuntime - Given that the user is compiling with
299 /// -fblocks, does this tool chain guarantee the existence of a
302 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
303 virtual bool hasBlocksRuntime() const { return true; }
305 /// \brief Add the clang cc1 arguments for system include paths.
307 /// This routine is responsible for adding the necessary cc1 arguments to
308 /// include headers from standard system header directories.
310 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
311 llvm::opt::ArgStringList &CC1Args) const;
313 /// \brief Add options that need to be passed to cc1 for this target.
314 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
315 llvm::opt::ArgStringList &CC1Args) const;
317 /// \brief Add warning options that need to be passed to cc1 for this target.
318 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
320 // GetRuntimeLibType - Determine the runtime library type to use with the
321 // given compilation arguments.
322 virtual RuntimeLibType
323 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
325 // GetCXXStdlibType - Determine the C++ standard library type to use with the
326 // given compilation arguments.
327 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
329 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
330 /// the include paths to use for the given C++ standard library type.
332 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
333 llvm::opt::ArgStringList &CC1Args) const;
335 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
336 /// for the given C++ standard library type.
337 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
338 llvm::opt::ArgStringList &CmdArgs) const;
340 /// AddCCKextLibArgs - Add the system specific linker arguments to use
341 /// for kernel extensions (Darwin-specific).
342 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
343 llvm::opt::ArgStringList &CmdArgs) const;
345 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
346 /// global flags for unsafe floating point math, add it and return true.
348 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
350 AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args,
351 llvm::opt::ArgStringList &CmdArgs) const;
353 /// \brief Return sanitizers which are available in this toolchain.
354 virtual SanitizerMask getSupportedSanitizers() const;
357 } // end namespace driver
358 } // end namespace clang