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"
21 #include "llvm/Target/TargetOptions.h"
46 /// ToolChain - Access to tools for a single platform.
49 typedef SmallVector<std::string, 16> path_list;
64 RM_DisabledExplicitly,
70 const llvm::Triple Triple;
71 const llvm::opt::ArgList &Args;
72 // We need to initialize CachedRTTIArg before CachedRTTIMode
73 const llvm::opt::Arg *const CachedRTTIArg;
74 const RTTIMode CachedRTTIMode;
76 /// The list of toolchain specific path prefixes to search for
80 /// The list of toolchain specific path prefixes to search for
82 path_list ProgramPaths;
84 mutable std::unique_ptr<Tool> Clang;
85 mutable std::unique_ptr<Tool> Assemble;
86 mutable std::unique_ptr<Tool> Link;
87 Tool *getClang() const;
88 Tool *getAssemble() const;
89 Tool *getLink() const;
90 Tool *getClangAs() const;
92 mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
95 MultilibSet Multilibs;
96 const char *DefaultLinker = "ld";
98 ToolChain(const Driver &D, const llvm::Triple &T,
99 const llvm::opt::ArgList &Args);
101 virtual Tool *buildAssembler() const;
102 virtual Tool *buildLinker() const;
103 virtual Tool *getTool(Action::ActionClass AC) const;
105 /// \name Utilities for implementing subclasses.
107 static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
108 llvm::opt::ArgStringList &CC1Args,
110 static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
111 llvm::opt::ArgStringList &CC1Args,
114 addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
115 llvm::opt::ArgStringList &CC1Args,
117 static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
118 llvm::opt::ArgStringList &CC1Args,
119 ArrayRef<StringRef> Paths);
123 virtual ~ToolChain();
127 const Driver &getDriver() const { return D; }
128 vfs::FileSystem &getVFS() const;
129 const llvm::Triple &getTriple() const { return Triple; }
131 llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
132 StringRef getArchName() const { return Triple.getArchName(); }
133 StringRef getPlatform() const { return Triple.getVendorName(); }
134 StringRef getOS() const { return Triple.getOSName(); }
136 /// \brief Provide the default architecture name (as expected by -arch) for
138 StringRef getDefaultUniversalArchName() const;
140 std::string getTripleString() const {
141 return Triple.getTriple();
144 path_list &getFilePaths() { return FilePaths; }
145 const path_list &getFilePaths() const { return FilePaths; }
147 path_list &getProgramPaths() { return ProgramPaths; }
148 const path_list &getProgramPaths() const { return ProgramPaths; }
150 const MultilibSet &getMultilibs() const { return Multilibs; }
152 const SanitizerArgs& getSanitizerArgs() const;
154 // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
155 const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
157 // Returns the RTTIMode for the toolchain with the current arguments.
158 RTTIMode getRTTIMode() const { return CachedRTTIMode; }
160 /// \brief Return any implicit target and/or mode flag for an invocation of
161 /// the compiler driver as `ProgName`.
163 /// For example, when called with i686-linux-android-g++, the first element
164 /// of the return value will be set to `"i686-linux-android"` and the second
165 /// will be set to "--driver-mode=g++"`.
167 /// \pre `llvm::InitializeAllTargets()` has been called.
168 /// \param ProgName The name the Clang driver was invoked with (from,
170 /// \return A pair of (`target`, `mode-flag`), where one or both may be empty.
171 static std::pair<std::string, std::string>
172 getTargetAndModeFromProgramName(StringRef ProgName);
176 /// TranslateArgs - Create a new derived argument list for any argument
177 /// translations this ToolChain may wish to perform, or 0 if no tool chain
178 /// specific translations are needed.
180 /// \param BoundArch - The bound architecture name, or 0.
181 virtual llvm::opt::DerivedArgList *
182 TranslateArgs(const llvm::opt::DerivedArgList &Args,
183 const char *BoundArch) const {
187 /// Choose a tool to use to handle the action \p JA.
189 /// This can be overridden when a particular ToolChain needs to use
190 /// a compiler other than Clang.
191 virtual Tool *SelectTool(const JobAction &JA) const;
195 std::string GetFilePath(const char *Name) const;
196 std::string GetProgramPath(const char *Name) const;
198 /// Returns the linker path, respecting the -fuse-ld= argument to determine
199 /// the linker suffix or name.
200 std::string GetLinkerPath() const;
202 /// \brief Dispatch to the specific toolchain for verbose printing.
204 /// This is used when handling the verbose option to print detailed,
205 /// toolchain-specific information useful for understanding the behavior of
206 /// the driver on a specific platform.
207 virtual void printVerboseInfo(raw_ostream &OS) const {}
209 // Platform defaults information
211 /// \brief Returns true if the toolchain is targeting a non-native
213 virtual bool isCrossCompiling() const;
215 /// HasNativeLTOLinker - Check whether the linker and related tools have
216 /// native LLVM support.
217 virtual bool HasNativeLLVMSupport() const;
219 /// LookupTypeForExtension - Return the default language type to use for the
221 virtual types::ID LookupTypeForExtension(const char *Ext) const;
223 /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
224 virtual bool IsBlocksDefault() const { return false; }
226 /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
228 virtual bool IsIntegratedAssemblerDefault() const { return false; }
230 /// \brief Check if the toolchain should use the integrated assembler.
231 bool useIntegratedAs() const;
233 /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
234 virtual bool IsMathErrnoDefault() const { return true; }
236 /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
237 /// -fencode-extended-block-signature by default.
238 virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
240 /// IsObjCNonFragileABIDefault - Does this tool chain set
241 /// -fobjc-nonfragile-abi by default.
242 virtual bool IsObjCNonFragileABIDefault() const { return false; }
244 /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
245 /// mixed dispatch method be used?
246 virtual bool UseObjCMixedDispatch() const { return false; }
248 /// GetDefaultStackProtectorLevel - Get the default stack protector level for
249 /// this tool chain (0=off, 1=on, 2=strong, 3=all).
250 virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
254 /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
255 virtual RuntimeLibType GetDefaultRuntimeLibType() const {
256 return ToolChain::RLT_Libgcc;
259 virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
261 bool Shared = false) const;
263 const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
265 bool Shared = false) const;
266 /// needsProfileRT - returns true if instrumentation profile is on.
267 static bool needsProfileRT(const llvm::opt::ArgList &Args);
269 /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
271 virtual bool IsUnwindTablesDefault() const;
273 /// \brief Test whether this toolchain defaults to PIC.
274 virtual bool isPICDefault() const = 0;
276 /// \brief Test whether this toolchain defaults to PIE.
277 virtual bool isPIEDefault() const = 0;
279 /// \brief Tests whether this toolchain forces its default for PIC, PIE or
280 /// non-PIC. If this returns true, any PIC related flags should be ignored
281 /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
282 /// used exclusively.
283 virtual bool isPICDefaultForced() const = 0;
285 /// SupportsProfiling - Does this tool chain support -pg.
286 virtual bool SupportsProfiling() const { return true; }
288 /// Does this tool chain support Objective-C garbage collection.
289 virtual bool SupportsObjCGC() const { return true; }
291 /// Complain if this tool chain doesn't support Objective-C ARC.
292 virtual void CheckObjCARC() const {}
294 /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
295 /// compile unit information.
296 virtual bool UseDwarfDebugFlags() const { return false; }
298 // Return the DWARF version to emit, in the absence of arguments
300 virtual unsigned GetDefaultDwarfVersion() const { return 4; }
302 // True if the driver should assume "-fstandalone-debug"
303 // in the absence of an option specifying otherwise,
304 // provided that debugging was requested in the first place.
305 // i.e. a value of 'true' does not imply that debugging is wanted.
306 virtual bool GetDefaultStandaloneDebug() const { return false; }
308 // Return the default debugger "tuning."
309 virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
310 return llvm::DebuggerKind::GDB;
313 /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
314 virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
318 /// getThreadModel() - Which thread model does this target use?
319 virtual std::string getThreadModel() const { return "posix"; }
321 /// isThreadModelSupported() - Does this target support a thread model?
322 virtual bool isThreadModelSupported(const StringRef Model) const;
324 /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
325 /// command line arguments into account.
327 ComputeLLVMTriple(const llvm::opt::ArgList &Args,
328 types::ID InputType = types::TY_INVALID) const;
330 /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
331 /// target, which may take into account the command line arguments. For
332 /// example, on Darwin the -mmacosx-version-min= command line argument (which
333 /// sets the deployment target) determines the version in the triple passed to
335 virtual std::string ComputeEffectiveClangTriple(
336 const llvm::opt::ArgList &Args,
337 types::ID InputType = types::TY_INVALID) const;
339 /// getDefaultObjCRuntime - Return the default Objective-C runtime
340 /// for this platform.
342 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
343 virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
345 /// hasBlocksRuntime - Given that the user is compiling with
346 /// -fblocks, does this tool chain guarantee the existence of a
349 /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
350 virtual bool hasBlocksRuntime() const { return true; }
352 /// \brief Add the clang cc1 arguments for system include paths.
354 /// This routine is responsible for adding the necessary cc1 arguments to
355 /// include headers from standard system header directories.
357 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
358 llvm::opt::ArgStringList &CC1Args) const;
360 /// \brief Add options that need to be passed to cc1 for this target.
361 virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
362 llvm::opt::ArgStringList &CC1Args) const;
364 /// \brief Add warning options that need to be passed to cc1 for this target.
365 virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
367 // GetRuntimeLibType - Determine the runtime library type to use with the
368 // given compilation arguments.
369 virtual RuntimeLibType
370 GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
372 // GetCXXStdlibType - Determine the C++ standard library type to use with the
373 // given compilation arguments.
374 virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
376 /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
377 /// the include paths to use for the given C++ standard library type.
379 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
380 llvm::opt::ArgStringList &CC1Args) const;
382 /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
383 /// for the given C++ standard library type.
384 virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
385 llvm::opt::ArgStringList &CmdArgs) const;
387 /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
388 void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
389 llvm::opt::ArgStringList &CmdArgs) const;
391 /// AddCCKextLibArgs - Add the system specific linker arguments to use
392 /// for kernel extensions (Darwin-specific).
393 virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
394 llvm::opt::ArgStringList &CmdArgs) const;
396 /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
397 /// global flags for unsafe floating point math, add it and return true.
399 /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
400 virtual bool AddFastMathRuntimeIfAvailable(
401 const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
402 /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
403 /// a suitable profile runtime library to the linker.
404 virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
405 llvm::opt::ArgStringList &CmdArgs) const;
407 /// \brief Add arguments to use system-specific CUDA includes.
408 virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
409 llvm::opt::ArgStringList &CC1Args) const;
411 /// \brief Return sanitizers which are available in this toolchain.
412 virtual SanitizerMask getSupportedSanitizers() const;
415 } // end namespace driver
416 } // end namespace clang