clang 10.0.0git

BareMetal.cpp
Go to the documentation of this file.
1 //===-- BareMetal.cpp - Bare Metal ToolChain --------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "BareMetal.h"
10
11 #include "CommonArgs.h"
12 #include "InputInfo.h"
13 #include "Gnu.h"
14
15 #include "clang/Driver/Compilation.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/DriverDiagnostic.h"
18 #include "clang/Driver/Options.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/VirtualFileSystem.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 using namespace llvm::opt;
25 using namespace clang;
26 using namespace clang::driver;
27 using namespace clang::driver::tools;
28 using namespace clang::driver::toolchains;
29
30 BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
31 const ArgList &Args)
32 : ToolChain(D, Triple, Args) {
33 getProgramPaths().push_back(getDriver().getInstalledDir());
34 if (getDriver().getInstalledDir() != getDriver().Dir)
35 getProgramPaths().push_back(getDriver().Dir);
36 }
37
38 BareMetal::~BareMetal() {}
39
40 /// Is the triple {arm,thumb}-none-none-{eabi,eabihf} ?
41 static bool isARMBareMetal(const llvm::Triple &Triple) {
42 if (Triple.getArch() != llvm::Triple::arm &&
43 Triple.getArch() != llvm::Triple::thumb)
44 return false;
45
46 if (Triple.getVendor() != llvm::Triple::UnknownVendor)
47 return false;
48
49 if (Triple.getOS() != llvm::Triple::UnknownOS)
50 return false;
51
52 if (Triple.getEnvironment() != llvm::Triple::EABI &&
53 Triple.getEnvironment() != llvm::Triple::EABIHF)
54 return false;
55
56 return true;
57 }
58
59 bool BareMetal::handlesTarget(const llvm::Triple &Triple) {
60 return isARMBareMetal(Triple);
61 }
62
63 Tool *BareMetal::buildLinker() const {
64 return new tools::baremetal::Linker(*this);
65 }
66
67 std::string BareMetal::getRuntimesDir() const {
68 SmallString<128> Dir(getDriver().ResourceDir);
69 llvm::sys::path::append(Dir, "lib", "baremetal");
70 return Dir.str();
71 }
72
73 void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
74 ArgStringList &CC1Args) const {
75 if (DriverArgs.hasArg(options::OPT_nostdinc))
76 return;
77
78 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
79 SmallString<128> Dir(getDriver().ResourceDir);
80 llvm::sys::path::append(Dir, "include");
81 addSystemInclude(DriverArgs, CC1Args, Dir.str());
82 }
83
84 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
85 SmallString<128> Dir(getDriver().SysRoot);
86 llvm::sys::path::append(Dir, "include");
87 addSystemInclude(DriverArgs, CC1Args, Dir.str());
88 }
89 }
90
91 void BareMetal::addClangTargetOptions(const ArgList &DriverArgs,
92 ArgStringList &CC1Args,
93 Action::OffloadKind) const {
94 CC1Args.push_back("-nostdsysteminc");
95 }
96
97 void BareMetal::AddClangCXXStdlibIncludeArgs(
98 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
99 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
100 DriverArgs.hasArg(options::OPT_nostdlibinc) ||
101 DriverArgs.hasArg(options::OPT_nostdincxx))
102 return;
103
104 StringRef SysRoot = getDriver().SysRoot;
105 if (SysRoot.empty())
106 return;
107
108 switch (GetCXXStdlibType(DriverArgs)) {
109 case ToolChain::CST_Libcxx: {
110 SmallString<128> Dir(SysRoot);
111 llvm::sys::path::append(Dir, "include", "c++", "v1");
112 addSystemInclude(DriverArgs, CC1Args, Dir.str());
113 break;
114 }
115 case ToolChain::CST_Libstdcxx: {
116 SmallString<128> Dir(SysRoot);
117 llvm::sys::path::append(Dir, "include", "c++");
118 std::error_code EC;
119 Generic_GCC::GCCVersion Version = {"", -1, -1, -1, "", "", ""};
120 // Walk the subdirs, and find the one with the newest gcc version:
121 for (llvm::vfs::directory_iterator
122 LI = getDriver().getVFS().dir_begin(Dir.str(), EC),
123 LE;
124 !EC && LI != LE; LI = LI.increment(EC)) {
125 StringRef VersionText = llvm::sys::path::filename(LI->path());
126 auto CandidateVersion = Generic_GCC::GCCVersion::Parse(VersionText);
127 if (CandidateVersion.Major == -1)
128 continue;
129 if (CandidateVersion <= Version)
130 continue;
131 Version = CandidateVersion;
132 }
133 if (Version.Major == -1)
134 return;
135 llvm::sys::path::append(Dir, Version.Text);
136 addSystemInclude(DriverArgs, CC1Args, Dir.str());
137 break;
138 }
139 }
140 }
141
142 void BareMetal::AddCXXStdlibLibArgs(const ArgList &Args,
143 ArgStringList &CmdArgs) const {
144 switch (GetCXXStdlibType(Args)) {
145 case ToolChain::CST_Libcxx:
146 CmdArgs.push_back("-lc++");
147 CmdArgs.push_back("-lc++abi");
148 break;
149 case ToolChain::CST_Libstdcxx:
150 CmdArgs.push_back("-lstdc++");
151 CmdArgs.push_back("-lsupc++");
152 break;
153 }
154 CmdArgs.push_back("-lunwind");
155 }
156
157 void BareMetal::AddLinkRuntimeLib(const ArgList &Args,
158 ArgStringList &CmdArgs) const {
159 CmdArgs.push_back(Args.MakeArgString("-lclang_rt.builtins-" +
160 getTriple().getArchName() + ".a"));
161 }
162
163 void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
164 const InputInfo &Output,
165 const InputInfoList &Inputs,
166 const ArgList &Args,
167 const char *LinkingOutput) const {
168 ArgStringList CmdArgs;
169
170 auto &TC = static_cast<const toolchains::BareMetal&>(getToolChain());
171
172 AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
173
174 CmdArgs.push_back("-Bstatic");
175
176 CmdArgs.push_back(Args.MakeArgString("-L" + TC.getRuntimesDir()));
177
178 Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
179 options::OPT_e, options::OPT_s, options::OPT_t,
180 options::OPT_Z_Flag, options::OPT_r});
181
182 if (TC.ShouldLinkCXXStdlib(Args))
183 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
184 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
185 CmdArgs.push_back("-lc");
186 CmdArgs.push_back("-lm");
187
188 TC.AddLinkRuntimeLib(Args, CmdArgs);
189 }
190
191 CmdArgs.push_back("-o");
192 CmdArgs.push_back(Output.getFilename());
193
194 C.addCommand(std::make_unique<Command>(JA, *this,
195 Args.MakeArgString(TC.GetLinkerPath()),
196 CmdArgs, Inputs));
197 }
clang::driver::toolchains::BareMetal::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: BareMetal.cpp:73
clang::driver::ToolChain::getArchName
StringRef getArchName() const
Definition: ToolChain.h:216
clang::driver::toolchains::Generic_GCC::GCCVersion::Parse
static GCCVersion Parse(StringRef VersionText)
Parse a GCCVersion object out of a string of text.
Definition: Gnu.cpp:1756
clang::driver::toolchains::Generic_GCC::GCCVersion
Struct to store and manipulate GCC versions.
Definition: Gnu.h:156
clang::driver::toolchains::BareMetal::AddLinkRuntimeLib
void AddLinkRuntimeLib(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: BareMetal.cpp:157
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:240
CommonArgs.h
clang::driver::JobAction
Definition: Action.h:375
clang::driver::ToolChain::CST_Libcxx
Definition: ToolChain.h:93
clang::driver::tools
Definition: AIX.h:17
BareMetal.h
clang::driver::toolchains::BareMetal::buildLinker
Tool * buildLinker() const override
Definition: BareMetal.cpp:63
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::toolchains::BareMetal::~BareMetal
~BareMetal() override
Definition: BareMetal.cpp:38
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
llvm::SmallString< 128 >
Driver.h
Compilation.h
llvm::opt
Definition: Action.h:23
clang::driver::Compilation::addCommand
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:199
clang::driver::toolchains::BareMetal::getRuntimesDir
std::string getRuntimesDir() const
Definition: BareMetal.cpp:67
llvm::SmallVector
Definition: LLVM.h:35
clang::driver::toolchains::BareMetal::addClangTargetOptions
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: BareMetal.cpp:91
clang::driver::ToolChain::addSystemInclude
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:820
Options.h
clang::driver::ToolChain::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:99
Gnu.h
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:84
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::driver::toolchains::BareMetal::AddCXXStdlibLibArgs
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: BareMetal.cpp:142
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
isARMBareMetal
static bool isARMBareMetal(const llvm::Triple &Triple)
Is the triple {arm,thumb}-none-none-{eabi,eabihf} ?
Definition: BareMetal.cpp:41
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
clang::driver::toolchains::BareMetal::handlesTarget
static bool handlesTarget(const llvm::Triple &Triple)
Definition: BareMetal.cpp:59
InputInfo.h
clang::driver::toolchains::BareMetal::AddClangCXXStdlibIncludeArgs
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: BareMetal.cpp:97
clang::driver::tools::baremetal::Linker
Definition: BareMetal.h:69
clang::driver::toolchains::Generic_GCC::GCCVersion::Text
std::string Text
The unparsed text of the version.
Definition: Gnu.h:158
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:201
clang::driver
Definition: Action.h:31
DriverDiagnostic.h
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::toolchains::BareMetal
Definition: BareMetal.h:22
clang::interp::LE
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:237
clang::driver::ToolChain::GetCXXStdlibType
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:801
clang::driver::toolchains::Generic_GCC::GCCVersion::Major
int Major
The parsed major, minor, and patch numbers.
Definition: Gnu.h:161
clang::driver::ToolChain::CST_Libstdcxx
Definition: ToolChain.h:94
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88