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 /// \brief Dispatch to the specific toolchain for verbose printing.
163 /// This is used when handling the verbose option to print detailed,
164 /// toolchain-specific information useful for understanding the behavior of
165 /// the driver on a specific platform.
166 virtual void printVerboseInfo(raw_ostream &OS) const {};
168 // Platform defaults information
170 /// HasNativeLTOLinker - Check whether the linker and related tools have
171 /// native LLVM support.
172 virtual bool HasNativeLLVMSupport() const;
174 /// LookupTypeForExtension - Return the default language type to use for the
176 virtual types::ID LookupTypeForExtension(const char *Ext) const;
178 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
179 virtual bool IsBlocksDefault() const { return false; }
181 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
183 virtual bool IsIntegratedAssemblerDefault() const { return false; }
185 /// \brief Check if the toolchain should use the integrated assembler.
186 bool useIntegratedAs() const;
188 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
189 virtual bool IsMathErrnoDefault() const { return true; }
191 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
192 /// -fencode-extended-block-signature by default.
193 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
195 /// IsObjCNonFragileABIDefault - Does this tool chain set
196 /// -fobjc-nonfragile-abi by default.
197 virtual bool IsObjCNonFragileABIDefault() const { return false; }
199 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
200 /// mixed dispatch method be used?
201 virtual bool UseObjCMixedDispatch() const { return false; }
203 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
204 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
205 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
209 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
210 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
211 return ToolChain::RLT_Libgcc;
214 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
216 virtual bool IsUnwindTablesDefault() const;
218 /// \brief Test whether this toolchain defaults to PIC.
219 virtual bool isPICDefault() const = 0;
221 /// \brief Test whether this toolchain defaults to PIE.
222 virtual bool isPIEDefault() const = 0;
224 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
225 /// non-PIC. If this returns true, any PIC related flags should be ignored
226 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
227 /// used exclusively.
228 virtual bool isPICDefaultForced() const = 0;
230 /// SupportsProfiling - Does this tool chain support -pg.
231 virtual bool SupportsProfiling() const { return true; }
233 /// Does this tool chain support Objective-C garbage collection.
234 virtual bool SupportsObjCGC() const { return true; }
236 /// Complain if this tool chain doesn't support Objective-C ARC.
237 virtual void CheckObjCARC() const {}
239 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
240 /// compile unit information.
241 virtual bool UseDwarfDebugFlags() const { return false; }
243 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
244 virtual bool UseSjLjExceptions() const { return false; }
246 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
247 /// command line arguments into account.
249 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
250 types::ID InputType = types::TY_INVALID) const;
252 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
253 /// target, which may take into account the command line arguments. For
254 /// example, on Darwin the -mmacosx-version-min= command line argument (which
255 /// sets the deployment target) determines the version in the triple passed to
257 virtual std::string ComputeEffectiveClangTriple(
258 const llvm::opt::ArgList &Args,
259 types::ID InputType = types::TY_INVALID) const;
261 /// getDefaultObjCRuntime - Return the default Objective-C runtime
262 /// for this platform.
264 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
265 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
267 /// hasBlocksRuntime - Given that the user is compiling with
268 /// -fblocks, does this tool chain guarantee the existence of a
271 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
272 virtual bool hasBlocksRuntime() const { return true; }
274 /// \brief Add the clang cc1 arguments for system include paths.
276 /// This routine is responsible for adding the necessary cc1 arguments to
277 /// include headers from standard system header directories.
279 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
280 llvm::opt::ArgStringList &CC1Args) const;
282 /// \brief Add options that need to be passed to cc1 for this target.
283 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
284 llvm::opt::ArgStringList &CC1Args) const;
286 /// \brief Add warning options that need to be passed to cc1 for this target.
287 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
289 // GetRuntimeLibType - Determine the runtime library type to use with the
290 // given compilation arguments.
291 virtual RuntimeLibType
292 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
294 // GetCXXStdlibType - Determine the C++ standard library type to use with the
295 // given compilation arguments.
296 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
298 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
299 /// the include paths to use for the given C++ standard library type.
301 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
302 llvm::opt::ArgStringList &CC1Args) const;
304 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
305 /// for the given C++ standard library type.
306 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
307 llvm::opt::ArgStringList &CmdArgs) const;
309 /// AddCCKextLibArgs - Add the system specific linker arguments to use
310 /// for kernel extensions (Darwin-specific).
311 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
312 llvm::opt::ArgStringList &CmdArgs) const;
314 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
315 /// global flags for unsafe floating point math, add it and return true.
317 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
319 AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args,
320 llvm::opt::ArgStringList &CmdArgs) const;
323 } // end namespace driver
324 } // end namespace clang