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(Args) {
32 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
33 if (!isThreadModelSupported(A->getValue()))
34 D.Diag(diag::err_drv_invalid_thread_model_for_target)
36 << A->getAsString(Args);
39 ToolChain::~ToolChain() {
42 const Driver &ToolChain::getDriver() const {
46 bool ToolChain::useIntegratedAs() const {
47 return Args.hasFlag(options::OPT_fintegrated_as,
48 options::OPT_fno_integrated_as,
49 IsIntegratedAssemblerDefault());
52 const SanitizerArgs& ToolChain::getSanitizerArgs() const {
53 if (!SanitizerArguments.get())
54 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
55 return *SanitizerArguments.get();
58 StringRef ToolChain::getDefaultUniversalArchName() const {
59 // In universal driver terms, the arch name accepted by -arch isn't exactly
60 // the same as the ones that appear in the triple. Roughly speaking, this is
61 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
62 // only interesting special case is powerpc.
63 switch (Triple.getArch()) {
64 case llvm::Triple::ppc:
66 case llvm::Triple::ppc64:
68 case llvm::Triple::ppc64le:
71 return Triple.getArchName();
75 bool ToolChain::IsUnwindTablesDefault() const {
79 Tool *ToolChain::getClang() const {
81 Clang.reset(new tools::Clang(*this));
85 Tool *ToolChain::buildAssembler() const {
86 return new tools::ClangAs(*this);
89 Tool *ToolChain::buildLinker() const {
90 llvm_unreachable("Linking is not supported by this toolchain");
93 Tool *ToolChain::getAssemble() const {
95 Assemble.reset(buildAssembler());
96 return Assemble.get();
99 Tool *ToolChain::getClangAs() const {
101 Assemble.reset(new tools::ClangAs(*this));
102 return Assemble.get();
105 Tool *ToolChain::getLink() const {
107 Link.reset(buildLinker());
111 Tool *ToolChain::getTool(Action::ActionClass AC) const {
113 case Action::AssembleJobClass:
114 return getAssemble();
116 case Action::LinkJobClass:
119 case Action::InputClass:
120 case Action::BindArchClass:
121 case Action::LipoJobClass:
122 case Action::DsymutilJobClass:
123 case Action::VerifyDebugInfoJobClass:
124 llvm_unreachable("Invalid tool kind.");
126 case Action::CompileJobClass:
127 case Action::PrecompileJobClass:
128 case Action::PreprocessJobClass:
129 case Action::AnalyzeJobClass:
130 case Action::MigrateJobClass:
131 case Action::VerifyPCHJobClass:
132 case Action::BackendJobClass:
136 llvm_unreachable("Invalid tool kind.");
139 Tool *ToolChain::SelectTool(const JobAction &JA) const {
140 if (getDriver().ShouldUseClangCompiler(JA))
142 Action::ActionClass AC = JA.getKind();
143 if (AC == Action::AssembleJobClass && useIntegratedAs())
148 std::string ToolChain::GetFilePath(const char *Name) const {
149 return D.GetFilePath(Name, *this);
153 std::string ToolChain::GetProgramPath(const char *Name) const {
154 return D.GetProgramPath(Name, *this);
157 std::string ToolChain::GetLinkerPath() const {
158 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
159 StringRef Suffix = A->getValue();
161 // If we're passed -fuse-ld= with no argument, or with the argument ld,
162 // then use whatever the default system linker is.
163 if (Suffix.empty() || Suffix == "ld")
164 return GetProgramPath("ld");
166 llvm::SmallString<8> LinkerName("ld.");
167 LinkerName.append(Suffix);
169 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
170 if (llvm::sys::fs::exists(LinkerPath))
173 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
177 return GetProgramPath("ld");
181 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
182 return types::lookupTypeForExtension(Ext);
185 bool ToolChain::HasNativeLLVMSupport() const {
189 bool ToolChain::isCrossCompiling() const {
190 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
191 switch (HostTriple.getArch()) {
192 // The A32/T32/T16 instruction sets are not separate architectures in this
194 case llvm::Triple::arm:
195 case llvm::Triple::armeb:
196 case llvm::Triple::thumb:
197 case llvm::Triple::thumbeb:
198 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
199 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
201 return HostTriple.getArch() != getArch();
205 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
206 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
210 bool ToolChain::isThreadModelSupported(const StringRef Model) const {
211 if (Model == "single") {
212 // FIXME: 'single' is only supported on ARM so far.
213 return Triple.getArch() == llvm::Triple::arm ||
214 Triple.getArch() == llvm::Triple::armeb ||
215 Triple.getArch() == llvm::Triple::thumb ||
216 Triple.getArch() == llvm::Triple::thumbeb;
217 } else if (Model == "posix")
223 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
224 types::ID InputType) const {
225 switch (getTriple().getArch()) {
227 return getTripleString();
229 case llvm::Triple::x86_64: {
230 llvm::Triple Triple = getTriple();
231 if (!Triple.isOSBinFormatMachO())
232 return getTripleString();
234 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
235 // x86_64h goes in the triple. Other -march options just use the
236 // vanilla triple we already have.
237 StringRef MArch = A->getValue();
238 if (MArch == "x86_64h")
239 Triple.setArchName(MArch);
241 return Triple.getTriple();
243 case llvm::Triple::aarch64: {
244 llvm::Triple Triple = getTriple();
245 if (!Triple.isOSBinFormatMachO())
246 return getTripleString();
248 // FIXME: older versions of ld64 expect the "arm64" component in the actual
249 // triple string and query it to determine whether an LTO file can be
250 // handled. Remove this when we don't care any more.
251 Triple.setArchName("arm64");
252 return Triple.getTriple();
254 case llvm::Triple::arm:
255 case llvm::Triple::armeb:
256 case llvm::Triple::thumb:
257 case llvm::Triple::thumbeb: {
258 // FIXME: Factor into subclasses.
259 llvm::Triple Triple = getTriple();
260 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
261 getTriple().getArch() == llvm::Triple::thumbeb;
263 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
264 // '-mbig-endian'/'-EB'.
265 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
266 options::OPT_mbig_endian)) {
267 if (A->getOption().matches(options::OPT_mlittle_endian))
273 // Thumb2 is the default for V7 on Darwin.
275 // FIXME: Thumb should just be another -target-feaure, not in the triple.
276 StringRef Suffix = Triple.isOSBinFormatMachO()
277 ? tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMCPUForMArch(Args, Triple))
278 : tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMTargetCPU(Args, Triple));
279 bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
280 Suffix.startswith("v7em") ||
281 (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
282 // FIXME: this is invalid for WindowsCE
283 if (getTriple().isOSWindows())
285 std::string ArchName;
291 // Assembly files should start in ARM mode.
292 if (InputType != types::TY_PP_Asm &&
293 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
296 ArchName = "thumbeb";
300 Triple.setArchName(ArchName + Suffix.str());
302 return Triple.getTriple();
307 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
308 types::ID InputType) const {
309 return ComputeLLVMTriple(Args, InputType);
312 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
313 ArgStringList &CC1Args) const {
314 // Each toolchain should provide the appropriate include flags.
317 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
318 ArgStringList &CC1Args) const {
321 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
323 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
324 const ArgList &Args) const
326 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
327 StringRef Value = A->getValue();
328 if (Value == "compiler-rt")
329 return ToolChain::RLT_CompilerRT;
330 if (Value == "libgcc")
331 return ToolChain::RLT_Libgcc;
332 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
333 << A->getAsString(Args);
336 return GetDefaultRuntimeLibType();
339 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
340 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
341 StringRef Value = A->getValue();
342 if (Value == "libc++")
343 return ToolChain::CST_Libcxx;
344 if (Value == "libstdc++")
345 return ToolChain::CST_Libstdcxx;
346 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
347 << A->getAsString(Args);
350 return ToolChain::CST_Libstdcxx;
353 /// \brief Utility function to add a system include directory to CC1 arguments.
354 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
355 ArgStringList &CC1Args,
357 CC1Args.push_back("-internal-isystem");
358 CC1Args.push_back(DriverArgs.MakeArgString(Path));
361 /// \brief Utility function to add a system include directory with extern "C"
362 /// semantics to CC1 arguments.
364 /// Note that this should be used rarely, and only for directories that
365 /// historically and for legacy reasons are treated as having implicit extern
366 /// "C" semantics. These semantics are *ignored* by and large today, but its
367 /// important to preserve the preprocessor changes resulting from the
369 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
370 ArgStringList &CC1Args,
372 CC1Args.push_back("-internal-externc-isystem");
373 CC1Args.push_back(DriverArgs.MakeArgString(Path));
376 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
377 ArgStringList &CC1Args,
379 if (llvm::sys::fs::exists(Path))
380 addExternCSystemInclude(DriverArgs, CC1Args, Path);
383 /// \brief Utility function to add a list of system include directories to CC1.
384 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
385 ArgStringList &CC1Args,
386 ArrayRef<StringRef> Paths) {
387 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
389 CC1Args.push_back("-internal-isystem");
390 CC1Args.push_back(DriverArgs.MakeArgString(*I));
394 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
395 ArgStringList &CC1Args) const {
396 // Header search paths should be handled by each of the subclasses.
397 // Historically, they have not been, and instead have been handled inside of
398 // the CC1-layer frontend. As the logic is hoisted out, this generic function
399 // will slowly stop being called.
401 // While it is being called, replicate a bit of a hack to propagate the
402 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
403 // header search paths with it. Once all systems are overriding this
404 // function, the CC1 flag and this line can be removed.
405 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
408 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
409 ArgStringList &CmdArgs) const {
410 CXXStdlibType Type = GetCXXStdlibType(Args);
413 case ToolChain::CST_Libcxx:
414 CmdArgs.push_back("-lc++");
417 case ToolChain::CST_Libstdcxx:
418 CmdArgs.push_back("-lstdc++");
423 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
424 ArgStringList &CmdArgs) const {
425 CmdArgs.push_back("-lcc_kext");
428 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
429 ArgStringList &CmdArgs) const {
430 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
431 // (to keep the linker options consistent with gcc and clang itself).
432 if (!isOptimizationLevelFast(Args)) {
433 // Check if -ffast-math or -funsafe-math.
435 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
436 options::OPT_funsafe_math_optimizations,
437 options::OPT_fno_unsafe_math_optimizations);
439 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
440 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
443 // If crtfastmath.o exists add it to the arguments.
444 std::string Path = GetFilePath("crtfastmath.o");
445 if (Path == "crtfastmath.o") // Not found.
448 CmdArgs.push_back(Args.MakeArgString(Path));