1f4a2713aSLionel Sambuc //===--- ToolChain.cpp - Collections of tools for one platform ------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "Tools.h"
11f4a2713aSLionel Sambuc #include "clang/Basic/ObjCRuntime.h"
12f4a2713aSLionel Sambuc #include "clang/Driver/Action.h"
13f4a2713aSLionel Sambuc #include "clang/Driver/Driver.h"
14f4a2713aSLionel Sambuc #include "clang/Driver/DriverDiagnostic.h"
15f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
16f4a2713aSLionel Sambuc #include "clang/Driver/SanitizerArgs.h"
17f4a2713aSLionel Sambuc #include "clang/Driver/ToolChain.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallString.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
20f4a2713aSLionel Sambuc #include "llvm/Option/Arg.h"
21f4a2713aSLionel Sambuc #include "llvm/Option/ArgList.h"
22f4a2713aSLionel Sambuc #include "llvm/Option/Option.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
24f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
25f4a2713aSLionel Sambuc using namespace clang::driver;
26f4a2713aSLionel Sambuc using namespace clang;
27f4a2713aSLionel Sambuc using namespace llvm::opt;
28f4a2713aSLionel Sambuc
ToolChain(const Driver & D,const llvm::Triple & T,const ArgList & Args)29f4a2713aSLionel Sambuc ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
30*0a6a1f1dSLionel Sambuc const ArgList &Args)
31*0a6a1f1dSLionel Sambuc : D(D), Triple(T), Args(Args) {
32*0a6a1f1dSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
33*0a6a1f1dSLionel Sambuc if (!isThreadModelSupported(A->getValue()))
34*0a6a1f1dSLionel Sambuc D.Diag(diag::err_drv_invalid_thread_model_for_target)
35*0a6a1f1dSLionel Sambuc << A->getValue()
36*0a6a1f1dSLionel Sambuc << A->getAsString(Args);
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc
~ToolChain()39f4a2713aSLionel Sambuc ToolChain::~ToolChain() {
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc
getDriver() const42f4a2713aSLionel Sambuc const Driver &ToolChain::getDriver() const {
43f4a2713aSLionel Sambuc return D;
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc
useIntegratedAs() const46f4a2713aSLionel Sambuc bool ToolChain::useIntegratedAs() const {
47*0a6a1f1dSLionel Sambuc return Args.hasFlag(options::OPT_fintegrated_as,
48*0a6a1f1dSLionel Sambuc options::OPT_fno_integrated_as,
49f4a2713aSLionel Sambuc IsIntegratedAssemblerDefault());
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc
getSanitizerArgs() const52f4a2713aSLionel Sambuc const SanitizerArgs& ToolChain::getSanitizerArgs() const {
53f4a2713aSLionel Sambuc if (!SanitizerArguments.get())
54f4a2713aSLionel Sambuc SanitizerArguments.reset(new SanitizerArgs(*this, Args));
55f4a2713aSLionel Sambuc return *SanitizerArguments.get();
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc
getDefaultUniversalArchName() const58*0a6a1f1dSLionel Sambuc StringRef ToolChain::getDefaultUniversalArchName() const {
59f4a2713aSLionel Sambuc // In universal driver terms, the arch name accepted by -arch isn't exactly
60f4a2713aSLionel Sambuc // the same as the ones that appear in the triple. Roughly speaking, this is
61f4a2713aSLionel Sambuc // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
62f4a2713aSLionel Sambuc // only interesting special case is powerpc.
63f4a2713aSLionel Sambuc switch (Triple.getArch()) {
64f4a2713aSLionel Sambuc case llvm::Triple::ppc:
65f4a2713aSLionel Sambuc return "ppc";
66f4a2713aSLionel Sambuc case llvm::Triple::ppc64:
67f4a2713aSLionel Sambuc return "ppc64";
68f4a2713aSLionel Sambuc case llvm::Triple::ppc64le:
69f4a2713aSLionel Sambuc return "ppc64le";
70f4a2713aSLionel Sambuc default:
71f4a2713aSLionel Sambuc return Triple.getArchName();
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc
IsUnwindTablesDefault() const75f4a2713aSLionel Sambuc bool ToolChain::IsUnwindTablesDefault() const {
76f4a2713aSLionel Sambuc return false;
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc
getClang() const79f4a2713aSLionel Sambuc Tool *ToolChain::getClang() const {
80f4a2713aSLionel Sambuc if (!Clang)
81f4a2713aSLionel Sambuc Clang.reset(new tools::Clang(*this));
82f4a2713aSLionel Sambuc return Clang.get();
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc
buildAssembler() const85f4a2713aSLionel Sambuc Tool *ToolChain::buildAssembler() const {
86f4a2713aSLionel Sambuc return new tools::ClangAs(*this);
87f4a2713aSLionel Sambuc }
88f4a2713aSLionel Sambuc
buildLinker() const89f4a2713aSLionel Sambuc Tool *ToolChain::buildLinker() const {
90f4a2713aSLionel Sambuc llvm_unreachable("Linking is not supported by this toolchain");
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc
getAssemble() const93f4a2713aSLionel Sambuc Tool *ToolChain::getAssemble() const {
94f4a2713aSLionel Sambuc if (!Assemble)
95f4a2713aSLionel Sambuc Assemble.reset(buildAssembler());
96f4a2713aSLionel Sambuc return Assemble.get();
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
getClangAs() const99f4a2713aSLionel Sambuc Tool *ToolChain::getClangAs() const {
100f4a2713aSLionel Sambuc if (!Assemble)
101f4a2713aSLionel Sambuc Assemble.reset(new tools::ClangAs(*this));
102f4a2713aSLionel Sambuc return Assemble.get();
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc
getLink() const105f4a2713aSLionel Sambuc Tool *ToolChain::getLink() const {
106f4a2713aSLionel Sambuc if (!Link)
107f4a2713aSLionel Sambuc Link.reset(buildLinker());
108f4a2713aSLionel Sambuc return Link.get();
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc
getTool(Action::ActionClass AC) const111f4a2713aSLionel Sambuc Tool *ToolChain::getTool(Action::ActionClass AC) const {
112f4a2713aSLionel Sambuc switch (AC) {
113f4a2713aSLionel Sambuc case Action::AssembleJobClass:
114f4a2713aSLionel Sambuc return getAssemble();
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc case Action::LinkJobClass:
117f4a2713aSLionel Sambuc return getLink();
118f4a2713aSLionel Sambuc
119f4a2713aSLionel Sambuc case Action::InputClass:
120f4a2713aSLionel Sambuc case Action::BindArchClass:
121f4a2713aSLionel Sambuc case Action::LipoJobClass:
122f4a2713aSLionel Sambuc case Action::DsymutilJobClass:
123*0a6a1f1dSLionel Sambuc case Action::VerifyDebugInfoJobClass:
124f4a2713aSLionel Sambuc llvm_unreachable("Invalid tool kind.");
125f4a2713aSLionel Sambuc
126f4a2713aSLionel Sambuc case Action::CompileJobClass:
127f4a2713aSLionel Sambuc case Action::PrecompileJobClass:
128f4a2713aSLionel Sambuc case Action::PreprocessJobClass:
129f4a2713aSLionel Sambuc case Action::AnalyzeJobClass:
130f4a2713aSLionel Sambuc case Action::MigrateJobClass:
131*0a6a1f1dSLionel Sambuc case Action::VerifyPCHJobClass:
132*0a6a1f1dSLionel Sambuc case Action::BackendJobClass:
133f4a2713aSLionel Sambuc return getClang();
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc
136f4a2713aSLionel Sambuc llvm_unreachable("Invalid tool kind.");
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc
SelectTool(const JobAction & JA) const139f4a2713aSLionel Sambuc Tool *ToolChain::SelectTool(const JobAction &JA) const {
140f4a2713aSLionel Sambuc if (getDriver().ShouldUseClangCompiler(JA))
141f4a2713aSLionel Sambuc return getClang();
142f4a2713aSLionel Sambuc Action::ActionClass AC = JA.getKind();
143f4a2713aSLionel Sambuc if (AC == Action::AssembleJobClass && useIntegratedAs())
144f4a2713aSLionel Sambuc return getClangAs();
145f4a2713aSLionel Sambuc return getTool(AC);
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc
GetFilePath(const char * Name) const148f4a2713aSLionel Sambuc std::string ToolChain::GetFilePath(const char *Name) const {
149f4a2713aSLionel Sambuc return D.GetFilePath(Name, *this);
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc }
152f4a2713aSLionel Sambuc
GetProgramPath(const char * Name) const153f4a2713aSLionel Sambuc std::string ToolChain::GetProgramPath(const char *Name) const {
154f4a2713aSLionel Sambuc return D.GetProgramPath(Name, *this);
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc
GetLinkerPath() const157*0a6a1f1dSLionel Sambuc std::string ToolChain::GetLinkerPath() const {
158*0a6a1f1dSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
159*0a6a1f1dSLionel Sambuc StringRef Suffix = A->getValue();
160*0a6a1f1dSLionel Sambuc
161*0a6a1f1dSLionel Sambuc // If we're passed -fuse-ld= with no argument, or with the argument ld,
162*0a6a1f1dSLionel Sambuc // then use whatever the default system linker is.
163*0a6a1f1dSLionel Sambuc if (Suffix.empty() || Suffix == "ld")
164*0a6a1f1dSLionel Sambuc return GetProgramPath("ld");
165*0a6a1f1dSLionel Sambuc
166*0a6a1f1dSLionel Sambuc llvm::SmallString<8> LinkerName("ld.");
167*0a6a1f1dSLionel Sambuc LinkerName.append(Suffix);
168*0a6a1f1dSLionel Sambuc
169*0a6a1f1dSLionel Sambuc std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
170*0a6a1f1dSLionel Sambuc if (llvm::sys::fs::exists(LinkerPath))
171*0a6a1f1dSLionel Sambuc return LinkerPath;
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
174*0a6a1f1dSLionel Sambuc return "";
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc
177*0a6a1f1dSLionel Sambuc return GetProgramPath("ld");
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc
LookupTypeForExtension(const char * Ext) const181f4a2713aSLionel Sambuc types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
182f4a2713aSLionel Sambuc return types::lookupTypeForExtension(Ext);
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc
HasNativeLLVMSupport() const185f4a2713aSLionel Sambuc bool ToolChain::HasNativeLLVMSupport() const {
186f4a2713aSLionel Sambuc return false;
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc
isCrossCompiling() const189*0a6a1f1dSLionel Sambuc bool ToolChain::isCrossCompiling() const {
190*0a6a1f1dSLionel Sambuc llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
191*0a6a1f1dSLionel Sambuc switch (HostTriple.getArch()) {
192*0a6a1f1dSLionel Sambuc // The A32/T32/T16 instruction sets are not separate architectures in this
193*0a6a1f1dSLionel Sambuc // context.
194*0a6a1f1dSLionel Sambuc case llvm::Triple::arm:
195*0a6a1f1dSLionel Sambuc case llvm::Triple::armeb:
196*0a6a1f1dSLionel Sambuc case llvm::Triple::thumb:
197*0a6a1f1dSLionel Sambuc case llvm::Triple::thumbeb:
198*0a6a1f1dSLionel Sambuc return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
199*0a6a1f1dSLionel Sambuc getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
200*0a6a1f1dSLionel Sambuc default:
201*0a6a1f1dSLionel Sambuc return HostTriple.getArch() != getArch();
202*0a6a1f1dSLionel Sambuc }
203*0a6a1f1dSLionel Sambuc }
204*0a6a1f1dSLionel Sambuc
getDefaultObjCRuntime(bool isNonFragile) const205f4a2713aSLionel Sambuc ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
206f4a2713aSLionel Sambuc return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
207f4a2713aSLionel Sambuc VersionTuple());
208f4a2713aSLionel Sambuc }
209f4a2713aSLionel Sambuc
isThreadModelSupported(const StringRef Model) const210*0a6a1f1dSLionel Sambuc bool ToolChain::isThreadModelSupported(const StringRef Model) const {
211*0a6a1f1dSLionel Sambuc if (Model == "single") {
212*0a6a1f1dSLionel Sambuc // FIXME: 'single' is only supported on ARM so far.
213*0a6a1f1dSLionel Sambuc return Triple.getArch() == llvm::Triple::arm ||
214*0a6a1f1dSLionel Sambuc Triple.getArch() == llvm::Triple::armeb ||
215*0a6a1f1dSLionel Sambuc Triple.getArch() == llvm::Triple::thumb ||
216*0a6a1f1dSLionel Sambuc Triple.getArch() == llvm::Triple::thumbeb;
217*0a6a1f1dSLionel Sambuc } else if (Model == "posix")
218*0a6a1f1dSLionel Sambuc return true;
219f4a2713aSLionel Sambuc
220*0a6a1f1dSLionel Sambuc return false;
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc
ComputeLLVMTriple(const ArgList & Args,types::ID InputType) const223f4a2713aSLionel Sambuc std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
224f4a2713aSLionel Sambuc types::ID InputType) const {
225f4a2713aSLionel Sambuc switch (getTriple().getArch()) {
226f4a2713aSLionel Sambuc default:
227f4a2713aSLionel Sambuc return getTripleString();
228f4a2713aSLionel Sambuc
229f4a2713aSLionel Sambuc case llvm::Triple::x86_64: {
230f4a2713aSLionel Sambuc llvm::Triple Triple = getTriple();
231*0a6a1f1dSLionel Sambuc if (!Triple.isOSBinFormatMachO())
232f4a2713aSLionel Sambuc return getTripleString();
233f4a2713aSLionel Sambuc
234f4a2713aSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
235f4a2713aSLionel Sambuc // x86_64h goes in the triple. Other -march options just use the
236f4a2713aSLionel Sambuc // vanilla triple we already have.
237f4a2713aSLionel Sambuc StringRef MArch = A->getValue();
238f4a2713aSLionel Sambuc if (MArch == "x86_64h")
239f4a2713aSLionel Sambuc Triple.setArchName(MArch);
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc return Triple.getTriple();
242f4a2713aSLionel Sambuc }
243*0a6a1f1dSLionel Sambuc case llvm::Triple::aarch64: {
244*0a6a1f1dSLionel Sambuc llvm::Triple Triple = getTriple();
245*0a6a1f1dSLionel Sambuc if (!Triple.isOSBinFormatMachO())
246*0a6a1f1dSLionel Sambuc return getTripleString();
247*0a6a1f1dSLionel Sambuc
248*0a6a1f1dSLionel Sambuc // FIXME: older versions of ld64 expect the "arm64" component in the actual
249*0a6a1f1dSLionel Sambuc // triple string and query it to determine whether an LTO file can be
250*0a6a1f1dSLionel Sambuc // handled. Remove this when we don't care any more.
251*0a6a1f1dSLionel Sambuc Triple.setArchName("arm64");
252*0a6a1f1dSLionel Sambuc return Triple.getTriple();
253*0a6a1f1dSLionel Sambuc }
254f4a2713aSLionel Sambuc case llvm::Triple::arm:
255*0a6a1f1dSLionel Sambuc case llvm::Triple::armeb:
256*0a6a1f1dSLionel Sambuc case llvm::Triple::thumb:
257*0a6a1f1dSLionel Sambuc case llvm::Triple::thumbeb: {
258f4a2713aSLionel Sambuc // FIXME: Factor into subclasses.
259f4a2713aSLionel Sambuc llvm::Triple Triple = getTriple();
260*0a6a1f1dSLionel Sambuc bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
261*0a6a1f1dSLionel Sambuc getTriple().getArch() == llvm::Triple::thumbeb;
262*0a6a1f1dSLionel Sambuc
263*0a6a1f1dSLionel Sambuc // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
264*0a6a1f1dSLionel Sambuc // '-mbig-endian'/'-EB'.
265*0a6a1f1dSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
266*0a6a1f1dSLionel Sambuc options::OPT_mbig_endian)) {
267*0a6a1f1dSLionel Sambuc if (A->getOption().matches(options::OPT_mlittle_endian))
268*0a6a1f1dSLionel Sambuc IsBigEndian = false;
269*0a6a1f1dSLionel Sambuc else
270*0a6a1f1dSLionel Sambuc IsBigEndian = true;
271*0a6a1f1dSLionel Sambuc }
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc // Thumb2 is the default for V7 on Darwin.
274f4a2713aSLionel Sambuc //
275f4a2713aSLionel Sambuc // FIXME: Thumb should just be another -target-feaure, not in the triple.
276*0a6a1f1dSLionel Sambuc #if defined(__minix) || 1
2775ae1a533SBen Gras // Minix/ARM-specific force to ARMv7 and EABI.
2785ae1a533SBen Gras StringRef Suffix = "v7";
2795ae1a533SBen Gras Triple.setEnvironment(llvm::Triple::EABI);
2805ae1a533SBen Gras #else
281*0a6a1f1dSLionel Sambuc StringRef Suffix = Triple.isOSBinFormatMachO()
282*0a6a1f1dSLionel Sambuc ? tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMCPUForMArch(Args, Triple))
283*0a6a1f1dSLionel Sambuc : tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMTargetCPU(Args, Triple));
284*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) || 1 */
285*0a6a1f1dSLionel Sambuc bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
286*0a6a1f1dSLionel Sambuc Suffix.startswith("v7em") ||
287*0a6a1f1dSLionel Sambuc (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
288*0a6a1f1dSLionel Sambuc // FIXME: this is invalid for WindowsCE
289*0a6a1f1dSLionel Sambuc if (getTriple().isOSWindows())
290*0a6a1f1dSLionel Sambuc ThumbDefault = true;
291*0a6a1f1dSLionel Sambuc std::string ArchName;
292*0a6a1f1dSLionel Sambuc if (IsBigEndian)
293*0a6a1f1dSLionel Sambuc ArchName = "armeb";
294*0a6a1f1dSLionel Sambuc else
295*0a6a1f1dSLionel Sambuc ArchName = "arm";
296f4a2713aSLionel Sambuc
297f4a2713aSLionel Sambuc // Assembly files should start in ARM mode.
298f4a2713aSLionel Sambuc if (InputType != types::TY_PP_Asm &&
299f4a2713aSLionel Sambuc Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
300*0a6a1f1dSLionel Sambuc {
301*0a6a1f1dSLionel Sambuc if (IsBigEndian)
302*0a6a1f1dSLionel Sambuc ArchName = "thumbeb";
303*0a6a1f1dSLionel Sambuc else
304f4a2713aSLionel Sambuc ArchName = "thumb";
305*0a6a1f1dSLionel Sambuc }
306f4a2713aSLionel Sambuc Triple.setArchName(ArchName + Suffix.str());
307f4a2713aSLionel Sambuc
308f4a2713aSLionel Sambuc return Triple.getTriple();
309f4a2713aSLionel Sambuc }
310f4a2713aSLionel Sambuc }
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc
ComputeEffectiveClangTriple(const ArgList & Args,types::ID InputType) const313f4a2713aSLionel Sambuc std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
314f4a2713aSLionel Sambuc types::ID InputType) const {
315f4a2713aSLionel Sambuc return ComputeLLVMTriple(Args, InputType);
316f4a2713aSLionel Sambuc }
317f4a2713aSLionel Sambuc
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const318f4a2713aSLionel Sambuc void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
319f4a2713aSLionel Sambuc ArgStringList &CC1Args) const {
320f4a2713aSLionel Sambuc // Each toolchain should provide the appropriate include flags.
321f4a2713aSLionel Sambuc }
322f4a2713aSLionel Sambuc
addClangTargetOptions(const ArgList & DriverArgs,ArgStringList & CC1Args) const323f4a2713aSLionel Sambuc void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
324f4a2713aSLionel Sambuc ArgStringList &CC1Args) const {
325f4a2713aSLionel Sambuc }
326f4a2713aSLionel Sambuc
addClangWarningOptions(ArgStringList & CC1Args) const327*0a6a1f1dSLionel Sambuc void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
328*0a6a1f1dSLionel Sambuc
GetRuntimeLibType(const ArgList & Args) const329f4a2713aSLionel Sambuc ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
330f4a2713aSLionel Sambuc const ArgList &Args) const
331f4a2713aSLionel Sambuc {
332f4a2713aSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
333f4a2713aSLionel Sambuc StringRef Value = A->getValue();
334f4a2713aSLionel Sambuc if (Value == "compiler-rt")
335f4a2713aSLionel Sambuc return ToolChain::RLT_CompilerRT;
336f4a2713aSLionel Sambuc if (Value == "libgcc")
337f4a2713aSLionel Sambuc return ToolChain::RLT_Libgcc;
338f4a2713aSLionel Sambuc getDriver().Diag(diag::err_drv_invalid_rtlib_name)
339f4a2713aSLionel Sambuc << A->getAsString(Args);
340f4a2713aSLionel Sambuc }
341f4a2713aSLionel Sambuc
342f4a2713aSLionel Sambuc return GetDefaultRuntimeLibType();
343f4a2713aSLionel Sambuc }
344f4a2713aSLionel Sambuc
GetCXXStdlibType(const ArgList & Args) const345f4a2713aSLionel Sambuc ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
346f4a2713aSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
347f4a2713aSLionel Sambuc StringRef Value = A->getValue();
348f4a2713aSLionel Sambuc if (Value == "libc++")
349f4a2713aSLionel Sambuc return ToolChain::CST_Libcxx;
350f4a2713aSLionel Sambuc if (Value == "libstdc++")
351f4a2713aSLionel Sambuc return ToolChain::CST_Libstdcxx;
352f4a2713aSLionel Sambuc getDriver().Diag(diag::err_drv_invalid_stdlib_name)
353f4a2713aSLionel Sambuc << A->getAsString(Args);
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc
356f4a2713aSLionel Sambuc return ToolChain::CST_Libstdcxx;
357f4a2713aSLionel Sambuc }
358f4a2713aSLionel Sambuc
359f4a2713aSLionel Sambuc /// \brief Utility function to add a system include directory to CC1 arguments.
addSystemInclude(const ArgList & DriverArgs,ArgStringList & CC1Args,const Twine & Path)360f4a2713aSLionel Sambuc /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
361f4a2713aSLionel Sambuc ArgStringList &CC1Args,
362f4a2713aSLionel Sambuc const Twine &Path) {
363f4a2713aSLionel Sambuc CC1Args.push_back("-internal-isystem");
364f4a2713aSLionel Sambuc CC1Args.push_back(DriverArgs.MakeArgString(Path));
365f4a2713aSLionel Sambuc }
366f4a2713aSLionel Sambuc
367f4a2713aSLionel Sambuc /// \brief Utility function to add a system include directory with extern "C"
368f4a2713aSLionel Sambuc /// semantics to CC1 arguments.
369f4a2713aSLionel Sambuc ///
370f4a2713aSLionel Sambuc /// Note that this should be used rarely, and only for directories that
371f4a2713aSLionel Sambuc /// historically and for legacy reasons are treated as having implicit extern
372f4a2713aSLionel Sambuc /// "C" semantics. These semantics are *ignored* by and large today, but its
373f4a2713aSLionel Sambuc /// important to preserve the preprocessor changes resulting from the
374f4a2713aSLionel Sambuc /// classification.
addExternCSystemInclude(const ArgList & DriverArgs,ArgStringList & CC1Args,const Twine & Path)375f4a2713aSLionel Sambuc /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
376f4a2713aSLionel Sambuc ArgStringList &CC1Args,
377f4a2713aSLionel Sambuc const Twine &Path) {
378f4a2713aSLionel Sambuc CC1Args.push_back("-internal-externc-isystem");
379f4a2713aSLionel Sambuc CC1Args.push_back(DriverArgs.MakeArgString(Path));
380f4a2713aSLionel Sambuc }
381f4a2713aSLionel Sambuc
addExternCSystemIncludeIfExists(const ArgList & DriverArgs,ArgStringList & CC1Args,const Twine & Path)382f4a2713aSLionel Sambuc void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
383f4a2713aSLionel Sambuc ArgStringList &CC1Args,
384f4a2713aSLionel Sambuc const Twine &Path) {
385f4a2713aSLionel Sambuc if (llvm::sys::fs::exists(Path))
386f4a2713aSLionel Sambuc addExternCSystemInclude(DriverArgs, CC1Args, Path);
387f4a2713aSLionel Sambuc }
388f4a2713aSLionel Sambuc
389f4a2713aSLionel Sambuc /// \brief Utility function to add a list of system include directories to CC1.
addSystemIncludes(const ArgList & DriverArgs,ArgStringList & CC1Args,ArrayRef<StringRef> Paths)390f4a2713aSLionel Sambuc /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
391f4a2713aSLionel Sambuc ArgStringList &CC1Args,
392f4a2713aSLionel Sambuc ArrayRef<StringRef> Paths) {
393f4a2713aSLionel Sambuc for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
394f4a2713aSLionel Sambuc I != E; ++I) {
395f4a2713aSLionel Sambuc CC1Args.push_back("-internal-isystem");
396f4a2713aSLionel Sambuc CC1Args.push_back(DriverArgs.MakeArgString(*I));
397f4a2713aSLionel Sambuc }
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc
AddClangCXXStdlibIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const400f4a2713aSLionel Sambuc void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
401f4a2713aSLionel Sambuc ArgStringList &CC1Args) const {
402f4a2713aSLionel Sambuc // Header search paths should be handled by each of the subclasses.
403f4a2713aSLionel Sambuc // Historically, they have not been, and instead have been handled inside of
404f4a2713aSLionel Sambuc // the CC1-layer frontend. As the logic is hoisted out, this generic function
405f4a2713aSLionel Sambuc // will slowly stop being called.
406f4a2713aSLionel Sambuc //
407f4a2713aSLionel Sambuc // While it is being called, replicate a bit of a hack to propagate the
408f4a2713aSLionel Sambuc // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
409f4a2713aSLionel Sambuc // header search paths with it. Once all systems are overriding this
410f4a2713aSLionel Sambuc // function, the CC1 flag and this line can be removed.
411f4a2713aSLionel Sambuc DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
412f4a2713aSLionel Sambuc }
413f4a2713aSLionel Sambuc
AddCXXStdlibLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const414f4a2713aSLionel Sambuc void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
415f4a2713aSLionel Sambuc ArgStringList &CmdArgs) const {
416f4a2713aSLionel Sambuc CXXStdlibType Type = GetCXXStdlibType(Args);
417f4a2713aSLionel Sambuc
418f4a2713aSLionel Sambuc switch (Type) {
419f4a2713aSLionel Sambuc case ToolChain::CST_Libcxx:
420f4a2713aSLionel Sambuc CmdArgs.push_back("-lc++");
421f4a2713aSLionel Sambuc break;
422f4a2713aSLionel Sambuc
423f4a2713aSLionel Sambuc case ToolChain::CST_Libstdcxx:
424f4a2713aSLionel Sambuc CmdArgs.push_back("-lstdc++");
425f4a2713aSLionel Sambuc break;
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc }
428f4a2713aSLionel Sambuc
AddCCKextLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const429f4a2713aSLionel Sambuc void ToolChain::AddCCKextLibArgs(const ArgList &Args,
430f4a2713aSLionel Sambuc ArgStringList &CmdArgs) const {
431f4a2713aSLionel Sambuc CmdArgs.push_back("-lcc_kext");
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc
AddFastMathRuntimeIfAvailable(const ArgList & Args,ArgStringList & CmdArgs) const434f4a2713aSLionel Sambuc bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
435f4a2713aSLionel Sambuc ArgStringList &CmdArgs) const {
436*0a6a1f1dSLionel Sambuc // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
437*0a6a1f1dSLionel Sambuc // (to keep the linker options consistent with gcc and clang itself).
438*0a6a1f1dSLionel Sambuc if (!isOptimizationLevelFast(Args)) {
439*0a6a1f1dSLionel Sambuc // Check if -ffast-math or -funsafe-math.
440*0a6a1f1dSLionel Sambuc Arg *A =
441*0a6a1f1dSLionel Sambuc Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
442f4a2713aSLionel Sambuc options::OPT_funsafe_math_optimizations,
443f4a2713aSLionel Sambuc options::OPT_fno_unsafe_math_optimizations);
444f4a2713aSLionel Sambuc
445f4a2713aSLionel Sambuc if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
446f4a2713aSLionel Sambuc A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
447f4a2713aSLionel Sambuc return false;
448*0a6a1f1dSLionel Sambuc }
449f4a2713aSLionel Sambuc // If crtfastmath.o exists add it to the arguments.
450f4a2713aSLionel Sambuc std::string Path = GetFilePath("crtfastmath.o");
451f4a2713aSLionel Sambuc if (Path == "crtfastmath.o") // Not found.
452f4a2713aSLionel Sambuc return false;
453f4a2713aSLionel Sambuc
454f4a2713aSLionel Sambuc CmdArgs.push_back(Args.MakeArgString(Path));
455f4a2713aSLionel Sambuc return true;
456f4a2713aSLionel Sambuc }
457