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; }
154 /// \brief Return any implicit target and/or mode flag for an invocation of
155 /// the compiler driver as `ProgName`.
157 /// For example, when called with i686-linux-android-g++, the first element
158 /// of the return value will be set to `"i686-linux-android"` and the second
159 /// will be set to "--driver-mode=g++"`.
161 /// \pre `llvm::InitializeAllTargets()` has been called.
162 /// \param ProgName The name the Clang driver was invoked with (from,
164 /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
165 static std::pair<std::string, std::string>
166 getTargetAndModeFromProgramName(StringRef ProgName);
170 /// TranslateArgs - Create a new derived argument list for any argument
171 /// translations this ToolChain may wish to perform, or 0 if no tool chain
172 /// specific translations are needed.
174 /// \param BoundArch - The bound architecture name, or 0.
175 virtual llvm::opt::DerivedArgList *
176 TranslateArgs(const llvm::opt::DerivedArgList &Args,
177 const char *BoundArch) const {
181 /// Choose a tool to use to handle the action \p JA.
183 /// This can be overridden when a particular ToolChain needs to use
184 /// a C compiler other than Clang.
185 virtual Tool *SelectTool(const JobAction &JA) const;
189 std::string GetFilePath(const char *Name) const;
190 std::string GetProgramPath(const char *Name) const;
192 /// Returns the linker path, respecting the -fuse-ld= argument to determine
193 /// the linker suffix or name.
194 std::string GetLinkerPath() const;
196 /// \brief Dispatch to the specific toolchain for verbose printing.
198 /// This is used when handling the verbose option to print detailed,
199 /// toolchain-specific information useful for understanding the behavior of
200 /// the driver on a specific platform.
201 virtual void printVerboseInfo(raw_ostream &OS) const {}
203 // Platform defaults information
205 /// \brief Returns true if the toolchain is targeting a non-native
207 virtual bool isCrossCompiling() const;
209 /// HasNativeLTOLinker - Check whether the linker and related tools have
210 /// native LLVM support.
211 virtual bool HasNativeLLVMSupport() const;
213 /// LookupTypeForExtension - Return the default language type to use for the
215 virtual types::ID LookupTypeForExtension(const char *Ext) const;
217 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
218 virtual bool IsBlocksDefault() const { return false; }
220 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
222 virtual bool IsIntegratedAssemblerDefault() const { return false; }
224 /// \brief Check if the toolchain should use the integrated assembler.
225 bool useIntegratedAs() const;
227 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
228 virtual bool IsMathErrnoDefault() const { return true; }
230 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
231 /// -fencode-extended-block-signature by default.
232 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
234 /// IsObjCNonFragileABIDefault - Does this tool chain set
235 /// -fobjc-nonfragile-abi by default.
236 virtual bool IsObjCNonFragileABIDefault() const { return false; }
238 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
239 /// mixed dispatch method be used?
240 virtual bool UseObjCMixedDispatch() const { return false; }
242 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
243 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
244 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
248 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
249 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
250 return ToolChain::RLT_Libgcc;
253 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
255 virtual bool IsUnwindTablesDefault() const;
257 /// \brief Test whether this toolchain defaults to PIC.
258 virtual bool isPICDefault() const = 0;
260 /// \brief Test whether this toolchain defaults to PIE.
261 virtual bool isPIEDefault() const = 0;
263 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
264 /// non-PIC. If this returns true, any PIC related flags should be ignored
265 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
266 /// used exclusively.
267 virtual bool isPICDefaultForced() const = 0;
269 /// SupportsProfiling - Does this tool chain support -pg.
270 virtual bool SupportsProfiling() const { return true; }
272 /// Does this tool chain support Objective-C garbage collection.
273 virtual bool SupportsObjCGC() const { return true; }
275 /// Complain if this tool chain doesn't support Objective-C ARC.
276 virtual void CheckObjCARC() const {}
278 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
279 /// compile unit information.
280 virtual bool UseDwarfDebugFlags() const { return false; }
282 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
283 virtual bool UseSjLjExceptions() const { return false; }
285 /// getThreadModel() - Which thread model does this target use?
286 virtual std::string getThreadModel() const { return "posix"; }
288 /// isThreadModelSupported() - Does this target support a thread model?
289 virtual bool isThreadModelSupported(const StringRef Model) const;
291 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
292 /// command line arguments into account.
294 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
295 types::ID InputType = types::TY_INVALID) const;
297 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
298 /// target, which may take into account the command line arguments. For
299 /// example, on Darwin the -mmacosx-version-min= command line argument (which
300 /// sets the deployment target) determines the version in the triple passed to
302 virtual std::string ComputeEffectiveClangTriple(
303 const llvm::opt::ArgList &Args,
304 types::ID InputType = types::TY_INVALID) const;
306 /// getDefaultObjCRuntime - Return the default Objective-C runtime
307 /// for this platform.
309 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
310 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
312 /// hasBlocksRuntime - Given that the user is compiling with
313 /// -fblocks, does this tool chain guarantee the existence of a
316 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
317 virtual bool hasBlocksRuntime() const { return true; }
319 /// \brief Add the clang cc1 arguments for system include paths.
321 /// This routine is responsible for adding the necessary cc1 arguments to
322 /// include headers from standard system header directories.
324 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
325 llvm::opt::ArgStringList &CC1Args) const;
327 /// \brief Add options that need to be passed to cc1 for this target.
328 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
329 llvm::opt::ArgStringList &CC1Args) const;
331 /// \brief Add warning options that need to be passed to cc1 for this target.
332 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
334 // GetRuntimeLibType - Determine the runtime library type to use with the
335 // given compilation arguments.
336 virtual RuntimeLibType
337 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
339 // GetCXXStdlibType - Determine the C++ standard library type to use with the
340 // given compilation arguments.
341 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
343 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
344 /// the include paths to use for the given C++ standard library type.
346 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
347 llvm::opt::ArgStringList &CC1Args) const;
349 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
350 /// for the given C++ standard library type.
351 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
352 llvm::opt::ArgStringList &CmdArgs) const;
354 /// AddCCKextLibArgs - Add the system specific linker arguments to use
355 /// for kernel extensions (Darwin-specific).
356 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
357 llvm::opt::ArgStringList &CmdArgs) const;
359 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
360 /// global flags for unsafe floating point math, add it and return true.
362 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
364 AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args,
365 llvm::opt::ArgStringList &CmdArgs) const;
367 /// \brief Return sanitizers which are available in this toolchain.
368 virtual SanitizerMask getSupportedSanitizers() const;
371 } // end namespace driver
372 } // end namespace clang