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/Basic/ObjCRuntime.h"
12 #include "clang/Driver/Action.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/DriverDiagnostic.h"
15 #include "clang/Driver/Options.h"
16 #include "clang/Driver/SanitizerArgs.h"
17 #include "clang/Driver/ToolChain.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Option/Option.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FileSystem.h"
25 using namespace clang::driver;
26 using namespace clang;
27 using namespace llvm::opt;
29 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
31 : D(D), Triple(T), Args(A) {
34 ToolChain::~ToolChain() {
37 const Driver &ToolChain::getDriver() const {
41 bool ToolChain::useIntegratedAs() const {
42 return Args.hasFlag(options::OPT_integrated_as,
43 options::OPT_no_integrated_as,
44 IsIntegratedAssemblerDefault());
47 const SanitizerArgs& ToolChain::getSanitizerArgs() const {
48 if (!SanitizerArguments.get())
49 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
50 return *SanitizerArguments.get();
53 std::string ToolChain::getDefaultUniversalArchName() const {
54 // In universal driver terms, the arch name accepted by -arch isn't exactly
55 // the same as the ones that appear in the triple. Roughly speaking, this is
56 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
57 // only interesting special case is powerpc.
58 switch (Triple.getArch()) {
59 case llvm::Triple::ppc:
61 case llvm::Triple::ppc64:
63 case llvm::Triple::ppc64le:
66 return Triple.getArchName();
70 bool ToolChain::IsUnwindTablesDefault() const {
74 Tool *ToolChain::getClang() const {
76 Clang.reset(new tools::Clang(*this));
80 Tool *ToolChain::buildAssembler() const {
81 return new tools::ClangAs(*this);
84 Tool *ToolChain::buildLinker() const {
85 llvm_unreachable("Linking is not supported by this toolchain");
88 Tool *ToolChain::getAssemble() const {
90 Assemble.reset(buildAssembler());
91 return Assemble.get();
94 Tool *ToolChain::getClangAs() const {
96 Assemble.reset(new tools::ClangAs(*this));
97 return Assemble.get();
100 Tool *ToolChain::getLink() const {
102 Link.reset(buildLinker());
106 Tool *ToolChain::getTool(Action::ActionClass AC) const {
108 case Action::AssembleJobClass:
109 return getAssemble();
111 case Action::LinkJobClass:
114 case Action::InputClass:
115 case Action::BindArchClass:
116 case Action::LipoJobClass:
117 case Action::DsymutilJobClass:
118 case Action::VerifyJobClass:
119 llvm_unreachable("Invalid tool kind.");
121 case Action::CompileJobClass:
122 case Action::PrecompileJobClass:
123 case Action::PreprocessJobClass:
124 case Action::AnalyzeJobClass:
125 case Action::MigrateJobClass:
129 llvm_unreachable("Invalid tool kind.");
132 Tool *ToolChain::SelectTool(const JobAction &JA) const {
133 if (getDriver().ShouldUseClangCompiler(JA))
135 Action::ActionClass AC = JA.getKind();
136 if (AC == Action::AssembleJobClass && useIntegratedAs())
141 std::string ToolChain::GetFilePath(const char *Name) const {
142 return D.GetFilePath(Name, *this);
146 std::string ToolChain::GetProgramPath(const char *Name) const {
147 return D.GetProgramPath(Name, *this);
150 std::string ToolChain::GetLinkerPath() const {
151 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
152 StringRef Suffix = A->getValue();
154 // If we're passed -fuse-ld= with no argument, or with the argument ld,
155 // then use whatever the default system linker is.
156 if (Suffix.empty() || Suffix == "ld")
157 return GetProgramPath("ld");
159 llvm::SmallString<8> LinkerName("ld.");
160 LinkerName.append(Suffix);
162 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
163 if (llvm::sys::fs::exists(LinkerPath))
166 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
170 return GetProgramPath("ld");
174 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
175 return types::lookupTypeForExtension(Ext);
178 bool ToolChain::HasNativeLLVMSupport() const {
182 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
183 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
187 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
189 // FIXME: tblgen this.
190 static const char *getARMTargetCPU(const ArgList &Args,
191 const llvm::Triple &Triple) {
192 // For Darwin targets, the -arch option (which is translated to a
193 // corresponding -march option) should determine the architecture
194 // (and the Mach-O slice) regardless of any -mcpu options.
195 if (!Triple.isOSDarwin()) {
196 // FIXME: Warn on inconsistent use of -mcpu and -march.
197 // If we have -mcpu=, use that.
198 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
199 return A->getValue();
203 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
204 // Otherwise, if we have -march= choose the base CPU for that arch.
205 MArch = A->getValue();
207 // Otherwise, use the Arch from the triple.
208 MArch = Triple.getArchName();
211 if (Triple.getOS() == llvm::Triple::NetBSD ||
212 Triple.getOS() == llvm::Triple::FreeBSD) {
213 if (MArch == "armv6")
214 return "arm1176jzf-s";
217 const char *result = llvm::StringSwitch<const char *>(MArch)
218 .Cases("armv2", "armv2a","arm2")
219 .Case("armv3", "arm6")
220 .Case("armv3m", "arm7m")
221 .Case("armv4", "strongarm")
222 .Case("armv4t", "arm7tdmi")
223 .Cases("armv5", "armv5t", "arm10tdmi")
224 .Cases("armv5e", "armv5te", "arm1026ejs")
225 .Case("armv5tej", "arm926ej-s")
226 .Cases("armv6", "armv6k", "arm1136jf-s")
227 .Case("armv6j", "arm1136j-s")
228 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
229 .Case("armv6t2", "arm1156t2-s")
230 .Cases("armv6m", "armv6-m", "cortex-m0")
231 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
232 .Cases("armv7l", "armv7-l", "cortex-a8")
233 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
234 .Cases("armv7s", "armv7-s", "swift")
235 .Cases("armv7r", "armv7-r", "cortex-r4")
236 .Cases("armv7m", "armv7-m", "cortex-m3")
237 .Cases("armv7em", "armv7e-m", "cortex-m4")
238 .Cases("armv8", "armv8a", "armv8-a", "cortex-a53")
239 .Case("ep9312", "ep9312")
240 .Case("iwmmxt", "iwmmxt")
241 .Case("xscale", "xscale")
242 // If all else failed, return the most base CPU with thumb interworking
243 // supported by LLVM.
250 Triple.getEnvironment() == llvm::Triple::GNUEABIHF
255 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
258 // FIXME: This is redundant with -mcpu, why does LLVM use this.
259 // FIXME: tblgen this, or kill it!
260 static const char *getLLVMArchSuffixForARM(StringRef CPU) {
261 return llvm::StringSwitch<const char *>(CPU)
262 .Case("strongarm", "v4")
263 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
264 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
265 .Cases("arm920", "arm920t", "arm922t", "v4t")
266 .Cases("arm940t", "ep9312","v4t")
267 .Cases("arm10tdmi", "arm1020t", "v5")
268 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
269 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
270 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
271 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
272 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
273 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
274 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
275 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "v7")
276 .Cases("cortex-r4", "cortex-r5", "v7r")
277 .Case("cortex-m0", "v6m")
278 .Case("cortex-m3", "v7m")
279 .Case("cortex-m4", "v7em")
280 .Case("cortex-a9-mp", "v7f")
281 .Case("swift", "v7s")
282 .Cases("cortex-a53", "cortex-a57", "v8")
286 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
287 types::ID InputType) const {
288 switch (getTriple().getArch()) {
290 return getTripleString();
292 case llvm::Triple::x86_64: {
293 llvm::Triple Triple = getTriple();
294 if (!Triple.isOSDarwin())
295 return getTripleString();
297 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
298 // x86_64h goes in the triple. Other -march options just use the
299 // vanilla triple we already have.
300 StringRef MArch = A->getValue();
301 if (MArch == "x86_64h")
302 Triple.setArchName(MArch);
304 return Triple.getTriple();
306 case llvm::Triple::arm:
307 case llvm::Triple::thumb: {
308 // FIXME: Factor into subclasses.
309 llvm::Triple Triple = getTriple();
311 // Thumb2 is the default for V7 on Darwin.
313 // FIXME: Thumb should just be another -target-feaure, not in the triple.
315 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
316 bool ThumbDefault = Suffix.startswith("v6m") ||
317 (Suffix.startswith("v7") && getTriple().isOSDarwin());
318 std::string ArchName = "arm";
320 // Assembly files should start in ARM mode.
321 if (InputType != types::TY_PP_Asm &&
322 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
324 Triple.setArchName(ArchName + Suffix.str());
326 return Triple.getTriple();
331 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
332 types::ID InputType) const {
333 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
334 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
335 options::OPT_miphoneos_version_min_EQ,
336 options::OPT_mios_simulator_version_min_EQ))
337 getDriver().Diag(diag::err_drv_clang_unsupported)
338 << A->getAsString(Args);
340 return ComputeLLVMTriple(Args, InputType);
343 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
344 ArgStringList &CC1Args) const {
345 // Each toolchain should provide the appropriate include flags.
348 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
349 ArgStringList &CC1Args) const {
352 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
353 const ArgList &Args) const
355 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
356 StringRef Value = A->getValue();
357 if (Value == "compiler-rt")
358 return ToolChain::RLT_CompilerRT;
359 if (Value == "libgcc")
360 return ToolChain::RLT_Libgcc;
361 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
362 << A->getAsString(Args);
365 return GetDefaultRuntimeLibType();
368 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
369 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
370 StringRef Value = A->getValue();
371 if (Value == "libc++")
372 return ToolChain::CST_Libcxx;
373 if (Value == "libstdc++")
374 return ToolChain::CST_Libstdcxx;
375 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
376 << A->getAsString(Args);
379 return ToolChain::CST_Libstdcxx;
382 /// \brief Utility function to add a system include directory to CC1 arguments.
383 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
384 ArgStringList &CC1Args,
386 CC1Args.push_back("-internal-isystem");
387 CC1Args.push_back(DriverArgs.MakeArgString(Path));
390 /// \brief Utility function to add a system include directory with extern "C"
391 /// semantics to CC1 arguments.
393 /// Note that this should be used rarely, and only for directories that
394 /// historically and for legacy reasons are treated as having implicit extern
395 /// "C" semantics. These semantics are *ignored* by and large today, but its
396 /// important to preserve the preprocessor changes resulting from the
398 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
399 ArgStringList &CC1Args,
401 CC1Args.push_back("-internal-externc-isystem");
402 CC1Args.push_back(DriverArgs.MakeArgString(Path));
405 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
406 ArgStringList &CC1Args,
408 if (llvm::sys::fs::exists(Path))
409 addExternCSystemInclude(DriverArgs, CC1Args, Path);
412 /// \brief Utility function to add a list of system include directories to CC1.
413 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
414 ArgStringList &CC1Args,
415 ArrayRef<StringRef> Paths) {
416 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
418 CC1Args.push_back("-internal-isystem");
419 CC1Args.push_back(DriverArgs.MakeArgString(*I));
423 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
424 ArgStringList &CC1Args) const {
425 // Header search paths should be handled by each of the subclasses.
426 // Historically, they have not been, and instead have been handled inside of
427 // the CC1-layer frontend. As the logic is hoisted out, this generic function
428 // will slowly stop being called.
430 // While it is being called, replicate a bit of a hack to propagate the
431 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
432 // header search paths with it. Once all systems are overriding this
433 // function, the CC1 flag and this line can be removed.
434 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
437 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
438 ArgStringList &CmdArgs) const {
439 CXXStdlibType Type = GetCXXStdlibType(Args);
442 case ToolChain::CST_Libcxx:
443 CmdArgs.push_back("-lc++");
446 case ToolChain::CST_Libstdcxx:
447 CmdArgs.push_back("-lstdc++");
452 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
453 ArgStringList &CmdArgs) const {
454 CmdArgs.push_back("-lcc_kext");
457 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
458 ArgStringList &CmdArgs) const {
459 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
460 // (to keep the linker options consistent with gcc and clang itself).
461 if (!isOptimizationLevelFast(Args)) {
462 // Check if -ffast-math or -funsafe-math.
464 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
465 options::OPT_funsafe_math_optimizations,
466 options::OPT_fno_unsafe_math_optimizations);
468 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
469 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
472 // If crtfastmath.o exists add it to the arguments.
473 std::string Path = GetFilePath("crtfastmath.o");
474 if (Path == "crtfastmath.o") // Not found.
477 CmdArgs.push_back(Args.MakeArgString(Path));