1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===//
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 //===----------------------------------------------------------------------===//
11 #include "clang/Driver/ToolChain.h"
12 #include "clang/Basic/ObjCRuntime.h"
13 #include "clang/Driver/Action.h"
14 #include "clang/Driver/Arg.h"
15 #include "clang/Driver/ArgList.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/DriverDiagnostic.h"
18 #include "clang/Driver/Option.h"
19 #include "clang/Driver/Options.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FileSystem.h"
23 using namespace clang::driver;
24 using namespace clang;
26 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
28 : D(D), Triple(T), Args(A) {
31 ToolChain::~ToolChain() {
34 const Driver &ToolChain::getDriver() const {
38 bool ToolChain::useIntegratedAs() const {
39 return Args.hasFlag(options::OPT_integrated_as,
40 options::OPT_no_integrated_as,
41 IsIntegratedAssemblerDefault());
44 std::string ToolChain::getDefaultUniversalArchName() const {
45 // In universal driver terms, the arch name accepted by -arch isn't exactly
46 // the same as the ones that appear in the triple. Roughly speaking, this is
47 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
48 // only interesting special case is powerpc.
49 switch (Triple.getArch()) {
50 case llvm::Triple::ppc:
52 case llvm::Triple::ppc64:
55 return Triple.getArchName();
59 bool ToolChain::IsUnwindTablesDefault() const {
63 Tool *ToolChain::getClang() const {
65 Clang.reset(new tools::Clang(*this));
69 Tool *ToolChain::buildAssembler() const {
70 return new tools::ClangAs(*this);
73 Tool *ToolChain::buildLinker() const {
74 llvm_unreachable("Linking is not supported by this toolchain");
77 Tool *ToolChain::getAssemble() const {
79 Assemble.reset(buildAssembler());
80 return Assemble.get();
83 Tool *ToolChain::getClangAs() const {
85 Assemble.reset(new tools::ClangAs(*this));
86 return Assemble.get();
89 Tool *ToolChain::getLink() const {
91 Link.reset(buildLinker());
95 Tool *ToolChain::getTool(Action::ActionClass AC) const {
97 case Action::AssembleJobClass:
100 case Action::LinkJobClass:
103 case Action::InputClass:
104 case Action::BindArchClass:
105 case Action::LipoJobClass:
106 case Action::DsymutilJobClass:
107 case Action::VerifyJobClass:
108 llvm_unreachable("Invalid tool kind.");
110 case Action::CompileJobClass:
111 case Action::PrecompileJobClass:
112 case Action::PreprocessJobClass:
113 case Action::AnalyzeJobClass:
114 case Action::MigrateJobClass:
118 llvm_unreachable("Invalid tool kind.");
121 Tool *ToolChain::SelectTool(const JobAction &JA) const {
122 if (getDriver().ShouldUseClangCompiler(JA))
124 Action::ActionClass AC = JA.getKind();
125 if (AC == Action::AssembleJobClass && useIntegratedAs())
130 std::string ToolChain::GetFilePath(const char *Name) const {
131 return D.GetFilePath(Name, *this);
135 std::string ToolChain::GetProgramPath(const char *Name) const {
136 return D.GetProgramPath(Name, *this);
139 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
140 return types::lookupTypeForExtension(Ext);
143 bool ToolChain::HasNativeLLVMSupport() const {
147 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
148 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
152 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
154 // FIXME: tblgen this.
155 static const char *getARMTargetCPU(const ArgList &Args,
156 const llvm::Triple &Triple) {
157 // For Darwin targets, the -arch option (which is translated to a
158 // corresponding -march option) should determine the architecture
159 // (and the Mach-O slice) regardless of any -mcpu options.
160 if (!Triple.isOSDarwin()) {
161 // FIXME: Warn on inconsistent use of -mcpu and -march.
162 // If we have -mcpu=, use that.
163 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
164 return A->getValue();
168 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
169 // Otherwise, if we have -march= choose the base CPU for that arch.
170 MArch = A->getValue();
172 // Otherwise, use the Arch from the triple.
173 MArch = Triple.getArchName();
176 return llvm::StringSwitch<const char *>(MArch)
177 .Cases("armv2", "armv2a","arm2")
178 .Case("armv3", "arm6")
179 .Case("armv3m", "arm7m")
180 .Cases("armv4", "armv4t", "arm7tdmi")
181 .Cases("armv5", "armv5t", "arm10tdmi")
182 .Cases("armv5e", "armv5te", "arm1026ejs")
183 .Case("armv5tej", "arm926ej-s")
184 .Cases("armv6", "armv6k", "arm1136jf-s")
185 .Case("armv6j", "arm1136j-s")
186 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
187 .Case("armv6t2", "arm1156t2-s")
188 .Cases("armv6m", "armv6-m", "cortex-m0")
189 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
190 .Cases("armv7l", "armv7-l", "cortex-a8")
191 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
192 .Cases("armv7s", "armv7-s", "swift")
193 .Cases("armv7r", "armv7-r", "cortex-r4")
194 .Cases("armv7m", "armv7-m", "cortex-m3")
195 .Cases("armv7em", "armv7e-m", "cortex-m4")
196 .Case("ep9312", "ep9312")
197 .Case("iwmmxt", "iwmmxt")
198 .Case("xscale", "xscale")
199 // If all else failed, return the most base CPU LLVM supports.
200 .Default("arm7tdmi");
203 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
206 // FIXME: This is redundant with -mcpu, why does LLVM use this.
207 // FIXME: tblgen this, or kill it!
208 static const char *getLLVMArchSuffixForARM(StringRef CPU) {
209 return llvm::StringSwitch<const char *>(CPU)
210 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
211 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
212 .Cases("arm920", "arm920t", "arm922t", "v4t")
213 .Cases("arm940t", "ep9312","v4t")
214 .Cases("arm10tdmi", "arm1020t", "v5")
215 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
216 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
217 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
218 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
219 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
220 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
221 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
222 .Cases("cortex-a9", "cortex-a15", "v7")
223 .Case("cortex-r5", "v7r")
224 .Case("cortex-m0", "v6m")
225 .Case("cortex-m3", "v7m")
226 .Case("cortex-m4", "v7em")
227 .Case("cortex-a9-mp", "v7f")
228 .Case("swift", "v7s")
232 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
233 types::ID InputType) const {
234 switch (getTriple().getArch()) {
236 return getTripleString();
238 case llvm::Triple::arm:
239 case llvm::Triple::thumb: {
240 // FIXME: Factor into subclasses.
241 llvm::Triple Triple = getTriple();
243 // Thumb2 is the default for V7 on Darwin.
245 // FIXME: Thumb should just be another -target-feaure, not in the triple.
247 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
248 bool ThumbDefault = Suffix.startswith("v6m") ||
249 (Suffix.startswith("v7") && getTriple().isOSDarwin());
250 std::string ArchName = "arm";
252 // Assembly files should start in ARM mode.
253 if (InputType != types::TY_PP_Asm &&
254 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
256 Triple.setArchName(ArchName + Suffix.str());
258 return Triple.getTriple();
263 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
264 types::ID InputType) const {
265 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
266 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
267 options::OPT_miphoneos_version_min_EQ,
268 options::OPT_mios_simulator_version_min_EQ))
269 getDriver().Diag(diag::err_drv_clang_unsupported)
270 << A->getAsString(Args);
272 return ComputeLLVMTriple(Args, InputType);
275 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
276 ArgStringList &CC1Args) const {
277 // Each toolchain should provide the appropriate include flags.
280 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
281 ArgStringList &CC1Args) const {
284 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
285 const ArgList &Args) const
287 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
288 StringRef Value = A->getValue();
289 if (Value == "compiler-rt")
290 return ToolChain::RLT_CompilerRT;
291 if (Value == "libgcc")
292 return ToolChain::RLT_Libgcc;
293 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
294 << A->getAsString(Args);
297 return GetDefaultRuntimeLibType();
300 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
301 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
302 StringRef Value = A->getValue();
303 if (Value == "libc++")
304 return ToolChain::CST_Libcxx;
305 if (Value == "libstdc++")
306 return ToolChain::CST_Libstdcxx;
307 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
308 << A->getAsString(Args);
311 return ToolChain::CST_Libstdcxx;
314 /// \brief Utility function to add a system include directory to CC1 arguments.
315 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
316 ArgStringList &CC1Args,
318 CC1Args.push_back("-internal-isystem");
319 CC1Args.push_back(DriverArgs.MakeArgString(Path));
322 /// \brief Utility function to add a system include directory with extern "C"
323 /// semantics to CC1 arguments.
325 /// Note that this should be used rarely, and only for directories that
326 /// historically and for legacy reasons are treated as having implicit extern
327 /// "C" semantics. These semantics are *ignored* by and large today, but its
328 /// important to preserve the preprocessor changes resulting from the
330 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
331 ArgStringList &CC1Args,
333 CC1Args.push_back("-internal-externc-isystem");
334 CC1Args.push_back(DriverArgs.MakeArgString(Path));
337 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
338 ArgStringList &CC1Args,
340 if (llvm::sys::fs::exists(Path))
341 addExternCSystemInclude(DriverArgs, CC1Args, Path);
344 /// \brief Utility function to add a list of system include directories to CC1.
345 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
346 ArgStringList &CC1Args,
347 ArrayRef<StringRef> Paths) {
348 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
350 CC1Args.push_back("-internal-isystem");
351 CC1Args.push_back(DriverArgs.MakeArgString(*I));
355 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
356 ArgStringList &CC1Args) const {
357 // Header search paths should be handled by each of the subclasses.
358 // Historically, they have not been, and instead have been handled inside of
359 // the CC1-layer frontend. As the logic is hoisted out, this generic function
360 // will slowly stop being called.
362 // While it is being called, replicate a bit of a hack to propagate the
363 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
364 // header search paths with it. Once all systems are overriding this
365 // function, the CC1 flag and this line can be removed.
366 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
369 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
370 ArgStringList &CmdArgs) const {
371 CXXStdlibType Type = GetCXXStdlibType(Args);
374 case ToolChain::CST_Libcxx:
375 CmdArgs.push_back("-lc++");
378 case ToolChain::CST_Libstdcxx:
379 CmdArgs.push_back("-lstdc++");
384 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
385 ArgStringList &CmdArgs) const {
386 CmdArgs.push_back("-lcc_kext");
389 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
390 ArgStringList &CmdArgs) const {
391 // Check if -ffast-math or -funsafe-math is enabled.
392 Arg *A = Args.getLastArg(options::OPT_ffast_math,
393 options::OPT_fno_fast_math,
394 options::OPT_funsafe_math_optimizations,
395 options::OPT_fno_unsafe_math_optimizations);
397 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
398 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
401 // If crtfastmath.o exists add it to the arguments.
402 std::string Path = GetFilePath("crtfastmath.o");
403 if (Path == "crtfastmath.o") // Not found.
406 CmdArgs.push_back(Args.MakeArgString(Path));