1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Tools.h"
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/ToolChain.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/Option/Arg.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Option/Option.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FileSystem.h"
23 using namespace clang::driver;
24 using namespace clang;
25 using namespace llvm::opt;
26 
27 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
28                      const ArgList &A)
29   : D(D), Triple(T), Args(A) {
30 }
31 
32 ToolChain::~ToolChain() {
33 }
34 
35 const Driver &ToolChain::getDriver() const {
36  return D;
37 }
38 
39 bool ToolChain::useIntegratedAs() const {
40   return Args.hasFlag(options::OPT_integrated_as,
41                       options::OPT_no_integrated_as,
42                       IsIntegratedAssemblerDefault());
43 }
44 
45 std::string ToolChain::getDefaultUniversalArchName() const {
46   // In universal driver terms, the arch name accepted by -arch isn't exactly
47   // the same as the ones that appear in the triple. Roughly speaking, this is
48   // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
49   // only interesting special case is powerpc.
50   switch (Triple.getArch()) {
51   case llvm::Triple::ppc:
52     return "ppc";
53   case llvm::Triple::ppc64:
54     return "ppc64";
55   default:
56     return Triple.getArchName();
57   }
58 }
59 
60 bool ToolChain::IsUnwindTablesDefault() const {
61   return false;
62 }
63 
64 Tool *ToolChain::getClang() const {
65   if (!Clang)
66     Clang.reset(new tools::Clang(*this));
67   return Clang.get();
68 }
69 
70 Tool *ToolChain::buildAssembler() const {
71   return new tools::ClangAs(*this);
72 }
73 
74 Tool *ToolChain::buildLinker() const {
75   llvm_unreachable("Linking is not supported by this toolchain");
76 }
77 
78 Tool *ToolChain::getAssemble() const {
79   if (!Assemble)
80     Assemble.reset(buildAssembler());
81   return Assemble.get();
82 }
83 
84 Tool *ToolChain::getClangAs() const {
85   if (!Assemble)
86     Assemble.reset(new tools::ClangAs(*this));
87   return Assemble.get();
88 }
89 
90 Tool *ToolChain::getLink() const {
91   if (!Link)
92     Link.reset(buildLinker());
93   return Link.get();
94 }
95 
96 Tool *ToolChain::getTool(Action::ActionClass AC) const {
97   switch (AC) {
98   case Action::AssembleJobClass:
99     return getAssemble();
100 
101   case Action::LinkJobClass:
102     return getLink();
103 
104   case Action::InputClass:
105   case Action::BindArchClass:
106   case Action::LipoJobClass:
107   case Action::DsymutilJobClass:
108   case Action::VerifyJobClass:
109     llvm_unreachable("Invalid tool kind.");
110 
111   case Action::CompileJobClass:
112   case Action::PrecompileJobClass:
113   case Action::PreprocessJobClass:
114   case Action::AnalyzeJobClass:
115   case Action::MigrateJobClass:
116     return getClang();
117   }
118 
119   llvm_unreachable("Invalid tool kind.");
120 }
121 
122 Tool *ToolChain::SelectTool(const JobAction &JA) const {
123   if (getDriver().ShouldUseClangCompiler(JA))
124     return getClang();
125   Action::ActionClass AC = JA.getKind();
126   if (AC == Action::AssembleJobClass && useIntegratedAs())
127     return getClangAs();
128   return getTool(AC);
129 }
130 
131 std::string ToolChain::GetFilePath(const char *Name) const {
132   return D.GetFilePath(Name, *this);
133 
134 }
135 
136 std::string ToolChain::GetProgramPath(const char *Name) const {
137   return D.GetProgramPath(Name, *this);
138 }
139 
140 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
141   return types::lookupTypeForExtension(Ext);
142 }
143 
144 bool ToolChain::HasNativeLLVMSupport() const {
145   return false;
146 }
147 
148 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
149   return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
150                      VersionTuple());
151 }
152 
153 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
154 //
155 // FIXME: tblgen this.
156 static const char *getARMTargetCPU(const ArgList &Args,
157                                    const llvm::Triple &Triple) {
158   // For Darwin targets, the -arch option (which is translated to a
159   // corresponding -march option) should determine the architecture
160   // (and the Mach-O slice) regardless of any -mcpu options.
161   if (!Triple.isOSDarwin()) {
162     // FIXME: Warn on inconsistent use of -mcpu and -march.
163     // If we have -mcpu=, use that.
164     if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
165       return A->getValue();
166   }
167 
168   StringRef MArch;
169   if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
170     // Otherwise, if we have -march= choose the base CPU for that arch.
171     MArch = A->getValue();
172   } else {
173     // Otherwise, use the Arch from the triple.
174     MArch = Triple.getArchName();
175   }
176 
177   return llvm::StringSwitch<const char *>(MArch)
178     .Cases("armv2", "armv2a","arm2")
179     .Case("armv3", "arm6")
180     .Case("armv3m", "arm7m")
181     .Case("armv4", "strongarm")
182     .Case("armv4t", "arm7tdmi")
183     .Cases("armv5", "armv5t", "arm10tdmi")
184     .Cases("armv5e", "armv5te", "arm1026ejs")
185     .Case("armv5tej", "arm926ej-s")
186     .Cases("armv6", "armv6k", "arm1136jf-s")
187     .Case("armv6j", "arm1136j-s")
188     .Cases("armv6z", "armv6zk", "arm1176jzf-s")
189     .Case("armv6t2", "arm1156t2-s")
190     .Cases("armv6m", "armv6-m", "cortex-m0")
191     .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
192     .Cases("armv7l", "armv7-l", "cortex-a8")
193     .Cases("armv7f", "armv7-f", "cortex-a9-mp")
194     .Cases("armv7s", "armv7-s", "swift")
195     .Cases("armv7r", "armv7-r", "cortex-r4")
196     .Cases("armv7m", "armv7-m", "cortex-m3")
197     .Cases("armv7em", "armv7e-m", "cortex-m4")
198     .Cases("armv8", "armv8a", "armv8-a", "cortex-a53")
199     .Case("ep9312", "ep9312")
200     .Case("iwmmxt", "iwmmxt")
201     .Case("xscale", "xscale")
202     // If all else failed, return the most base CPU with thumb interworking
203     // supported by LLVM.
204     .Default("arm7tdmi");
205 }
206 
207 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
208 /// CPU.
209 //
210 // FIXME: This is redundant with -mcpu, why does LLVM use this.
211 // FIXME: tblgen this, or kill it!
212 static const char *getLLVMArchSuffixForARM(StringRef CPU) {
213   return llvm::StringSwitch<const char *>(CPU)
214     .Case("strongarm", "v4")
215     .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
216     .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
217     .Cases("arm920", "arm920t", "arm922t", "v4t")
218     .Cases("arm940t", "ep9312","v4t")
219     .Cases("arm10tdmi",  "arm1020t", "v5")
220     .Cases("arm9e",  "arm926ej-s",  "arm946e-s", "v5e")
221     .Cases("arm966e-s",  "arm968e-s",  "arm10e", "v5e")
222     .Cases("arm1020e",  "arm1022e",  "xscale", "iwmmxt", "v5e")
223     .Cases("arm1136j-s",  "arm1136jf-s",  "arm1176jz-s", "v6")
224     .Cases("arm1176jzf-s",  "mpcorenovfp",  "mpcore", "v6")
225     .Cases("arm1156t2-s",  "arm1156t2f-s", "v6t2")
226     .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
227     .Cases("cortex-a9", "cortex-a15", "v7")
228     .Case("cortex-r5", "v7r")
229     .Case("cortex-m0", "v6m")
230     .Case("cortex-m3", "v7m")
231     .Case("cortex-m4", "v7em")
232     .Case("cortex-a9-mp", "v7f")
233     .Case("swift", "v7s")
234     .Case("cortex-a53", "v8")
235     .Default("");
236 }
237 
238 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
239                                          types::ID InputType) const {
240   switch (getTriple().getArch()) {
241   default:
242     return getTripleString();
243 
244   case llvm::Triple::arm:
245   case llvm::Triple::thumb: {
246     // FIXME: Factor into subclasses.
247     llvm::Triple Triple = getTriple();
248 
249     // Thumb2 is the default for V7 on Darwin.
250     //
251     // FIXME: Thumb should just be another -target-feaure, not in the triple.
252     StringRef Suffix =
253       getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
254     bool ThumbDefault = Suffix.startswith("v6m") ||
255       (Suffix.startswith("v7") && getTriple().isOSDarwin());
256     std::string ArchName = "arm";
257 
258     // Assembly files should start in ARM mode.
259     if (InputType != types::TY_PP_Asm &&
260         Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
261       ArchName = "thumb";
262     Triple.setArchName(ArchName + Suffix.str());
263 
264     return Triple.getTriple();
265   }
266   }
267 }
268 
269 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
270                                                    types::ID InputType) const {
271   // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
272   if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
273                                options::OPT_miphoneos_version_min_EQ,
274                                options::OPT_mios_simulator_version_min_EQ))
275     getDriver().Diag(diag::err_drv_clang_unsupported)
276       << A->getAsString(Args);
277 
278   return ComputeLLVMTriple(Args, InputType);
279 }
280 
281 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
282                                           ArgStringList &CC1Args) const {
283   // Each toolchain should provide the appropriate include flags.
284 }
285 
286 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
287                                       ArgStringList &CC1Args) const {
288 }
289 
290 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
291   const ArgList &Args) const
292 {
293   if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
294     StringRef Value = A->getValue();
295     if (Value == "compiler-rt")
296       return ToolChain::RLT_CompilerRT;
297     if (Value == "libgcc")
298       return ToolChain::RLT_Libgcc;
299     getDriver().Diag(diag::err_drv_invalid_rtlib_name)
300       << A->getAsString(Args);
301   }
302 
303   return GetDefaultRuntimeLibType();
304 }
305 
306 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
307   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
308     StringRef Value = A->getValue();
309     if (Value == "libc++")
310       return ToolChain::CST_Libcxx;
311     if (Value == "libstdc++")
312       return ToolChain::CST_Libstdcxx;
313     getDriver().Diag(diag::err_drv_invalid_stdlib_name)
314       << A->getAsString(Args);
315   }
316 
317   return ToolChain::CST_Libstdcxx;
318 }
319 
320 /// \brief Utility function to add a system include directory to CC1 arguments.
321 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
322                                             ArgStringList &CC1Args,
323                                             const Twine &Path) {
324   CC1Args.push_back("-internal-isystem");
325   CC1Args.push_back(DriverArgs.MakeArgString(Path));
326 }
327 
328 /// \brief Utility function to add a system include directory with extern "C"
329 /// semantics to CC1 arguments.
330 ///
331 /// Note that this should be used rarely, and only for directories that
332 /// historically and for legacy reasons are treated as having implicit extern
333 /// "C" semantics. These semantics are *ignored* by and large today, but its
334 /// important to preserve the preprocessor changes resulting from the
335 /// classification.
336 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
337                                                    ArgStringList &CC1Args,
338                                                    const Twine &Path) {
339   CC1Args.push_back("-internal-externc-isystem");
340   CC1Args.push_back(DriverArgs.MakeArgString(Path));
341 }
342 
343 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
344                                                 ArgStringList &CC1Args,
345                                                 const Twine &Path) {
346   if (llvm::sys::fs::exists(Path))
347     addExternCSystemInclude(DriverArgs, CC1Args, Path);
348 }
349 
350 /// \brief Utility function to add a list of system include directories to CC1.
351 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
352                                              ArgStringList &CC1Args,
353                                              ArrayRef<StringRef> Paths) {
354   for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
355        I != E; ++I) {
356     CC1Args.push_back("-internal-isystem");
357     CC1Args.push_back(DriverArgs.MakeArgString(*I));
358   }
359 }
360 
361 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
362                                              ArgStringList &CC1Args) const {
363   // Header search paths should be handled by each of the subclasses.
364   // Historically, they have not been, and instead have been handled inside of
365   // the CC1-layer frontend. As the logic is hoisted out, this generic function
366   // will slowly stop being called.
367   //
368   // While it is being called, replicate a bit of a hack to propagate the
369   // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
370   // header search paths with it. Once all systems are overriding this
371   // function, the CC1 flag and this line can be removed.
372   DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
373 }
374 
375 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
376                                     ArgStringList &CmdArgs) const {
377   CXXStdlibType Type = GetCXXStdlibType(Args);
378 
379   switch (Type) {
380   case ToolChain::CST_Libcxx:
381     CmdArgs.push_back("-lc++");
382     break;
383 
384   case ToolChain::CST_Libstdcxx:
385     CmdArgs.push_back("-lstdc++");
386     break;
387   }
388 }
389 
390 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
391                                  ArgStringList &CmdArgs) const {
392   CmdArgs.push_back("-lcc_kext");
393 }
394 
395 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
396                                               ArgStringList &CmdArgs) const {
397   // Check if -ffast-math or -funsafe-math is enabled.
398   Arg *A = Args.getLastArg(options::OPT_ffast_math,
399                            options::OPT_fno_fast_math,
400                            options::OPT_funsafe_math_optimizations,
401                            options::OPT_fno_unsafe_math_optimizations);
402 
403   if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
404       A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
405     return false;
406 
407   // If crtfastmath.o exists add it to the arguments.
408   std::string Path = GetFilePath("crtfastmath.o");
409   if (Path == "crtfastmath.o") // Not found.
410     return false;
411 
412   CmdArgs.push_back(Args.MakeArgString(Path));
413   return true;
414 }
415