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 CLANG_DRIVER_TOOLCHAIN_H_
11 #define CLANG_DRIVER_TOOLCHAIN_H_
13 #include "clang/Driver/Action.h"
14 #include "clang/Driver/Multilib.h"
15 #include "clang/Driver/Types.h"
16 #include "clang/Driver/Util.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Path.h"
41 /// ToolChain - Access to tools for a single platform.
44 typedef SmallVector<std::string, 4> path_list;
58 const llvm::Triple Triple;
59 const llvm::opt::ArgList &Args;
61 /// The list of toolchain specific path prefixes to search for
65 /// The list of toolchain specific path prefixes to search for
67 path_list ProgramPaths;
69 mutable std::unique_ptr<Tool> Clang;
70 mutable std::unique_ptr<Tool> Assemble;
71 mutable std::unique_ptr<Tool> Link;
72 Tool *getClang() const;
73 Tool *getAssemble() const;
74 Tool *getLink() const;
75 Tool *getClangAs() const;
77 mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
80 MultilibSet Multilibs;
82 ToolChain(const Driver &D, const llvm::Triple &T,
83 const llvm::opt::ArgList &Args);
85 virtual Tool *buildAssembler() const;
86 virtual Tool *buildLinker() const;
87 virtual Tool *getTool(Action::ActionClass AC) const;
89 /// \name Utilities for implementing subclasses.
91 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
92 llvm::opt::ArgStringList &CC1Args,
94 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
95 llvm::opt::ArgStringList &CC1Args,
98 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
99 llvm::opt::ArgStringList &CC1Args,
101 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
102 llvm::opt::ArgStringList &CC1Args,
103 ArrayRef<StringRef> Paths);
107 virtual ~ToolChain();
111 const Driver &getDriver() const;
112 const llvm::Triple &getTriple() const { return Triple; }
114 llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
115 StringRef getArchName() const { return Triple.getArchName(); }
116 StringRef getPlatform() const { return Triple.getVendorName(); }
117 StringRef getOS() const { return Triple.getOSName(); }
119 /// \brief Returns true if the toolchain is targeting a non-native architecture.
120 bool isCrossCompiling() const;
122 /// \brief Provide the default architecture name (as expected by -arch) for
123 /// this toolchain. Note t
124 std::string getDefaultUniversalArchName() const;
126 std::string getTripleString() const {
127 return Triple.getTriple();
130 path_list &getFilePaths() { return FilePaths; }
131 const path_list &getFilePaths() const { return FilePaths; }
133 path_list &getProgramPaths() { return ProgramPaths; }
134 const path_list &getProgramPaths() const { return ProgramPaths; }
136 const MultilibSet &getMultilibs() const { return Multilibs; }
138 const SanitizerArgs& getSanitizerArgs() const;
142 /// TranslateArgs - Create a new derived argument list for any argument
143 /// translations this ToolChain may wish to perform, or 0 if no tool chain
144 /// specific translations are needed.
146 /// \param BoundArch - The bound architecture name, or 0.
147 virtual llvm::opt::DerivedArgList *
148 TranslateArgs(const llvm::opt::DerivedArgList &Args,
149 const char *BoundArch) const {
153 /// Choose a tool to use to handle the action \p JA.
154 Tool *SelectTool(const JobAction &JA) const;
158 std::string GetFilePath(const char *Name) const;
159 std::string GetProgramPath(const char *Name) const;
161 /// Returns the linker path, respecting the -fuse-ld= argument to determine
162 /// the linker suffix or name.
163 std::string GetLinkerPath() const;
165 /// \brief Dispatch to the specific toolchain for verbose printing.
167 /// This is used when handling the verbose option to print detailed,
168 /// toolchain-specific information useful for understanding the behavior of
169 /// the driver on a specific platform.
170 virtual void printVerboseInfo(raw_ostream &OS) const {};
172 // Platform defaults information
174 /// HasNativeLTOLinker - Check whether the linker and related tools have
175 /// native LLVM support.
176 virtual bool HasNativeLLVMSupport() const;
178 /// LookupTypeForExtension - Return the default language type to use for the
180 virtual types::ID LookupTypeForExtension(const char *Ext) const;
182 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
183 virtual bool IsBlocksDefault() const { return false; }
185 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
187 virtual bool IsIntegratedAssemblerDefault() const { return false; }
189 /// \brief Check if the toolchain should use the integrated assembler.
190 bool useIntegratedAs() const;
192 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
193 virtual bool IsMathErrnoDefault() const { return true; }
195 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
196 /// -fencode-extended-block-signature by default.
197 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
199 /// IsObjCNonFragileABIDefault - Does this tool chain set
200 /// -fobjc-nonfragile-abi by default.
201 virtual bool IsObjCNonFragileABIDefault() const { return false; }
203 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
204 /// mixed dispatch method be used?
205 virtual bool UseObjCMixedDispatch() const { return false; }
207 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
208 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
209 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
213 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
214 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
215 return ToolChain::RLT_Libgcc;
218 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
220 virtual bool IsUnwindTablesDefault() const;
222 /// \brief Test whether this toolchain defaults to PIC.
223 virtual bool isPICDefault() const = 0;
225 /// \brief Test whether this toolchain defaults to PIE.
226 virtual bool isPIEDefault() const = 0;
228 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
229 /// non-PIC. If this returns true, any PIC related flags should be ignored
230 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
231 /// used exclusively.
232 virtual bool isPICDefaultForced() const = 0;
234 /// SupportsProfiling - Does this tool chain support -pg.
235 virtual bool SupportsProfiling() const { return true; }
237 /// Does this tool chain support Objective-C garbage collection.
238 virtual bool SupportsObjCGC() const { return true; }
240 /// Complain if this tool chain doesn't support Objective-C ARC.
241 virtual void CheckObjCARC() const {}
243 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
244 /// compile unit information.
245 virtual bool UseDwarfDebugFlags() const { return false; }
247 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
248 virtual bool UseSjLjExceptions() const { return false; }
250 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
251 /// command line arguments into account.
253 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
254 types::ID InputType = types::TY_INVALID) const;
256 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
257 /// target, which may take into account the command line arguments. For
258 /// example, on Darwin the -mmacosx-version-min= command line argument (which
259 /// sets the deployment target) determines the version in the triple passed to
261 virtual std::string ComputeEffectiveClangTriple(
262 const llvm::opt::ArgList &Args,
263 types::ID InputType = types::TY_INVALID) const;
265 /// getDefaultObjCRuntime - Return the default Objective-C runtime
266 /// for this platform.
268 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
269 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
271 /// hasBlocksRuntime - Given that the user is compiling with
272 /// -fblocks, does this tool chain guarantee the existence of a
275 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
276 virtual bool hasBlocksRuntime() const { return true; }
278 /// \brief Add the clang cc1 arguments for system include paths.
280 /// This routine is responsible for adding the necessary cc1 arguments to
281 /// include headers from standard system header directories.
283 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
284 llvm::opt::ArgStringList &CC1Args) const;
286 /// \brief Add options that need to be passed to cc1 for this target.
287 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
288 llvm::opt::ArgStringList &CC1Args) const;
290 /// \brief Add warning options that need to be passed to cc1 for this target.
291 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
293 // GetRuntimeLibType - Determine the runtime library type to use with the
294 // given compilation arguments.
295 virtual RuntimeLibType
296 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
298 // GetCXXStdlibType - Determine the C++ standard library type to use with the
299 // given compilation arguments.
300 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
302 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
303 /// the include paths to use for the given C++ standard library type.
305 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
306 llvm::opt::ArgStringList &CC1Args) const;
308 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
309 /// for the given C++ standard library type.
310 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
311 llvm::opt::ArgStringList &CmdArgs) const;
313 /// AddCCKextLibArgs - Add the system specific linker arguments to use
314 /// for kernel extensions (Darwin-specific).
315 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
316 llvm::opt::ArgStringList &CmdArgs) const;
318 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
319 /// global flags for unsafe floating point math, add it and return true.
321 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
323 AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args,
324 llvm::opt::ArgStringList &CmdArgs) const;
327 } // end namespace driver
328 } // end namespace clang