]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/Driver/ToolChain.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / clang / lib / Driver / ToolChain.cpp
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/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;
28
29 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
30                      const ArgList &A)
31   : D(D), Triple(T), Args(A) {
32 }
33
34 ToolChain::~ToolChain() {
35 }
36
37 const Driver &ToolChain::getDriver() const {
38  return D;
39 }
40
41 bool ToolChain::useIntegratedAs() const {
42   return Args.hasFlag(options::OPT_integrated_as,
43                       options::OPT_no_integrated_as,
44                       IsIntegratedAssemblerDefault());
45 }
46
47 const SanitizerArgs& ToolChain::getSanitizerArgs() const {
48   if (!SanitizerArguments.get())
49     SanitizerArguments.reset(new SanitizerArgs(*this, Args));
50   return *SanitizerArguments.get();
51 }
52
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:
60     return "ppc";
61   case llvm::Triple::ppc64:
62     return "ppc64";
63   case llvm::Triple::ppc64le:
64     return "ppc64le";
65   default:
66     return Triple.getArchName();
67   }
68 }
69
70 bool ToolChain::IsUnwindTablesDefault() const {
71   return false;
72 }
73
74 Tool *ToolChain::getClang() const {
75   if (!Clang)
76     Clang.reset(new tools::Clang(*this));
77   return Clang.get();
78 }
79
80 Tool *ToolChain::buildAssembler() const {
81   return new tools::ClangAs(*this);
82 }
83
84 Tool *ToolChain::buildLinker() const {
85   llvm_unreachable("Linking is not supported by this toolchain");
86 }
87
88 Tool *ToolChain::getAssemble() const {
89   if (!Assemble)
90     Assemble.reset(buildAssembler());
91   return Assemble.get();
92 }
93
94 Tool *ToolChain::getClangAs() const {
95   if (!Assemble)
96     Assemble.reset(new tools::ClangAs(*this));
97   return Assemble.get();
98 }
99
100 Tool *ToolChain::getLink() const {
101   if (!Link)
102     Link.reset(buildLinker());
103   return Link.get();
104 }
105
106 Tool *ToolChain::getTool(Action::ActionClass AC) const {
107   switch (AC) {
108   case Action::AssembleJobClass:
109     return getAssemble();
110
111   case Action::LinkJobClass:
112     return getLink();
113
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.");
120
121   case Action::CompileJobClass:
122   case Action::PrecompileJobClass:
123   case Action::PreprocessJobClass:
124   case Action::AnalyzeJobClass:
125   case Action::MigrateJobClass:
126     return getClang();
127   }
128
129   llvm_unreachable("Invalid tool kind.");
130 }
131
132 Tool *ToolChain::SelectTool(const JobAction &JA) const {
133   if (getDriver().ShouldUseClangCompiler(JA))
134     return getClang();
135   Action::ActionClass AC = JA.getKind();
136   if (AC == Action::AssembleJobClass && useIntegratedAs())
137     return getClangAs();
138   return getTool(AC);
139 }
140
141 std::string ToolChain::GetFilePath(const char *Name) const {
142   return D.GetFilePath(Name, *this);
143
144 }
145
146 std::string ToolChain::GetProgramPath(const char *Name) const {
147   return D.GetProgramPath(Name, *this);
148 }
149
150 std::string ToolChain::GetLinkerPath() const {
151   if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
152     StringRef Suffix = A->getValue();
153
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");
158
159     llvm::SmallString<8> LinkerName("ld.");
160     LinkerName.append(Suffix);
161
162     std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
163     if (llvm::sys::fs::exists(LinkerPath))
164       return LinkerPath;
165
166     getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
167     return "";
168   }
169
170   return GetProgramPath("ld");
171 }
172
173
174 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
175   return types::lookupTypeForExtension(Ext);
176 }
177
178 bool ToolChain::HasNativeLLVMSupport() const {
179   return false;
180 }
181
182 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
183   return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
184                      VersionTuple());
185 }
186
187 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
188 //
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();
200   }
201
202   StringRef MArch;
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();
206   } else {
207     // Otherwise, use the Arch from the triple.
208     MArch = Triple.getArchName();
209   }
210
211   if (Triple.getOS() == llvm::Triple::NetBSD ||
212       Triple.getOS() == llvm::Triple::FreeBSD) {
213     if (MArch == "armv6")
214       return "arm1176jzf-s";
215   }
216
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.
244     .Default(0);
245
246   if (result)
247     return result;
248
249   return
250     Triple.getEnvironment() == llvm::Triple::GNUEABIHF
251       ? "arm1176jzf-s"
252       : "arm7tdmi";
253 }
254
255 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
256 /// CPU.
257 //
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")
283     .Default("");
284 }
285
286 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
287                                          types::ID InputType) const {
288   switch (getTriple().getArch()) {
289   default:
290     return getTripleString();
291
292   case llvm::Triple::x86_64: {
293     llvm::Triple Triple = getTriple();
294     if (!Triple.isOSDarwin())
295       return getTripleString();
296
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);
303     }
304     return Triple.getTriple();
305   }
306   case llvm::Triple::arm:
307   case llvm::Triple::thumb: {
308     // FIXME: Factor into subclasses.
309     llvm::Triple Triple = getTriple();
310
311     // Thumb2 is the default for V7 on Darwin.
312     //
313     // FIXME: Thumb should just be another -target-feaure, not in the triple.
314     StringRef Suffix =
315       getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
316     bool ThumbDefault = Suffix.startswith("v6m") ||
317       (Suffix.startswith("v7") && getTriple().isOSDarwin());
318     std::string ArchName = "arm";
319
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))
323       ArchName = "thumb";
324     Triple.setArchName(ArchName + Suffix.str());
325
326     return Triple.getTriple();
327   }
328   }
329 }
330
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);
339
340   return ComputeLLVMTriple(Args, InputType);
341 }
342
343 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
344                                           ArgStringList &CC1Args) const {
345   // Each toolchain should provide the appropriate include flags.
346 }
347
348 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
349                                       ArgStringList &CC1Args) const {
350 }
351
352 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
353   const ArgList &Args) const
354 {
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);
363   }
364
365   return GetDefaultRuntimeLibType();
366 }
367
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);
377   }
378
379   return ToolChain::CST_Libstdcxx;
380 }
381
382 /// \brief Utility function to add a system include directory to CC1 arguments.
383 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
384                                             ArgStringList &CC1Args,
385                                             const Twine &Path) {
386   CC1Args.push_back("-internal-isystem");
387   CC1Args.push_back(DriverArgs.MakeArgString(Path));
388 }
389
390 /// \brief Utility function to add a system include directory with extern "C"
391 /// semantics to CC1 arguments.
392 ///
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
397 /// classification.
398 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
399                                                    ArgStringList &CC1Args,
400                                                    const Twine &Path) {
401   CC1Args.push_back("-internal-externc-isystem");
402   CC1Args.push_back(DriverArgs.MakeArgString(Path));
403 }
404
405 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
406                                                 ArgStringList &CC1Args,
407                                                 const Twine &Path) {
408   if (llvm::sys::fs::exists(Path))
409     addExternCSystemInclude(DriverArgs, CC1Args, Path);
410 }
411
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();
417        I != E; ++I) {
418     CC1Args.push_back("-internal-isystem");
419     CC1Args.push_back(DriverArgs.MakeArgString(*I));
420   }
421 }
422
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.
429   //
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);
435 }
436
437 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
438                                     ArgStringList &CmdArgs) const {
439   CXXStdlibType Type = GetCXXStdlibType(Args);
440
441   switch (Type) {
442   case ToolChain::CST_Libcxx:
443     CmdArgs.push_back("-lc++");
444     break;
445
446   case ToolChain::CST_Libstdcxx:
447     CmdArgs.push_back("-lstdc++");
448     break;
449   }
450 }
451
452 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
453                                  ArgStringList &CmdArgs) const {
454   CmdArgs.push_back("-lcc_kext");
455 }
456
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.
463     Arg *A =
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);
467
468     if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
469         A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
470       return false;
471   }
472   // If crtfastmath.o exists add it to the arguments.
473   std::string Path = GetFilePath("crtfastmath.o");
474   if (Path == "crtfastmath.o") // Not found.
475     return false;
476
477   CmdArgs.push_back(Args.MakeArgString(Path));
478   return true;
479 }