clang  7.0.0svn
BareMetal.cpp
Go to the documentation of this file.
1 //===--- BaremMetal.cpp - Bare Metal ToolChain ------------------*- C++ -*-===//
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 "BareMetal.h"
11 
12 #include "CommonArgs.h"
13 #include "InputInfo.h"
14 #include "Gnu.h"
15 
18 #include "clang/Driver/Driver.h"
20 #include "clang/Driver/Options.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/raw_ostream.h"
24 
25 using namespace llvm::opt;
26 using namespace clang;
27 using namespace clang::driver;
28 using namespace clang::driver::tools;
29 using namespace clang::driver::toolchains;
30 
31 BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
32  const ArgList &Args)
33  : ToolChain(D, Triple, Args) {
34  getProgramPaths().push_back(getDriver().getInstalledDir());
35  if (getDriver().getInstalledDir() != getDriver().Dir)
36  getProgramPaths().push_back(getDriver().Dir);
37 }
38 
40 
41 /// Is the triple {arm,thumb}-none-none-{eabi,eabihf} ?
42 static bool isARMBareMetal(const llvm::Triple &Triple) {
43  if (Triple.getArch() != llvm::Triple::arm &&
44  Triple.getArch() != llvm::Triple::thumb)
45  return false;
46 
47  if (Triple.getVendor() != llvm::Triple::UnknownVendor)
48  return false;
49 
50  if (Triple.getOS() != llvm::Triple::UnknownOS)
51  return false;
52 
53  if (Triple.getEnvironment() != llvm::Triple::EABI &&
54  Triple.getEnvironment() != llvm::Triple::EABIHF)
55  return false;
56 
57  return true;
58 }
59 
60 bool BareMetal::handlesTarget(const llvm::Triple &Triple) {
61  return isARMBareMetal(Triple);
62 }
63 
65  return new tools::baremetal::Linker(*this);
66 }
67 
68 std::string BareMetal::getRuntimesDir() const {
69  SmallString<128> Dir(getDriver().ResourceDir);
70  llvm::sys::path::append(Dir, "lib", "baremetal");
71  return Dir.str();
72 }
73 
74 void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
75  ArgStringList &CC1Args) const {
76  if (DriverArgs.hasArg(options::OPT_nostdinc))
77  return;
78 
79  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
80  SmallString<128> Dir(getDriver().ResourceDir);
81  llvm::sys::path::append(Dir, "include");
82  addSystemInclude(DriverArgs, CC1Args, Dir.str());
83  }
84 
85  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
86  SmallString<128> Dir(getDriver().SysRoot);
87  llvm::sys::path::append(Dir, "include");
88  addSystemInclude(DriverArgs, CC1Args, Dir.str());
89  }
90 }
91 
92 void BareMetal::addClangTargetOptions(const ArgList &DriverArgs,
93  ArgStringList &CC1Args,
94  Action::OffloadKind) const {
95  CC1Args.push_back("-nostdsysteminc");
96 }
97 
99  const ArgList &DriverArgs, ArgStringList &CC1Args) const {
100  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
101  DriverArgs.hasArg(options::OPT_nostdlibinc) ||
102  DriverArgs.hasArg(options::OPT_nostdincxx))
103  return;
104 
105  StringRef SysRoot = getDriver().SysRoot;
106  if (SysRoot.empty())
107  return;
108 
109  switch (GetCXXStdlibType(DriverArgs)) {
110  case ToolChain::CST_Libcxx: {
111  SmallString<128> Dir(SysRoot);
112  llvm::sys::path::append(Dir, "include", "c++", "v1");
113  addSystemInclude(DriverArgs, CC1Args, Dir.str());
114  break;
115  }
117  SmallString<128> Dir(SysRoot);
118  llvm::sys::path::append(Dir, "include", "c++");
119  std::error_code EC;
120  Generic_GCC::GCCVersion Version = {"", -1, -1, -1, "", "", ""};
121  // Walk the subdirs, and find the one with the newest gcc version:
122  for (vfs::directory_iterator LI =
123  getDriver().getVFS().dir_begin(Dir.str(), EC), LE;
124  !EC && LI != LE; LI = LI.increment(EC)) {
125  StringRef VersionText = llvm::sys::path::filename(LI->getName());
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)) {
146  CmdArgs.push_back("-lc++");
147  CmdArgs.push_back("-lc++abi");
148  break;
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(llvm::make_unique<Command>(JA, *this,
195  Args.MakeArgString(TC.GetLinkerPath()),
196  CmdArgs, Inputs));
197 }
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:74
StringRef getArchName() const
Definition: ToolChain.h:196
static GCCVersion Parse(StringRef VersionText)
Parse a GCCVersion object out of a string of text.
Definition: Gnu.cpp:1583
Struct to store and manipulate GCC versions.
Definition: Gnu.h:157
void AddLinkRuntimeLib(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: BareMetal.cpp:157
const char * getFilename() const
Definition: InputInfo.h:84
path_list & getProgramPaths()
Definition: ToolChain.h:217
Tool * buildLinker() const override
Definition: BareMetal.cpp:64
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:103
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:203
const Driver & getDriver() const
Definition: ToolChain.h:179
std::string getRuntimesDir() const
Definition: BareMetal.cpp:68
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:92
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:689
Dataflow Directional Tag Classes.
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
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
static bool isARMBareMetal(const llvm::Triple &Triple)
Is the triple {arm,thumb}-none-none-{eabi,eabihf} ?
Definition: BareMetal.cpp:42
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Defines the virtual file system interface vfs::FileSystem.
static bool handlesTarget(const llvm::Triple &Triple)
Definition: BareMetal.cpp:60
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:98
std::string Text
The unparsed text of the version.
Definition: Gnu.h:159
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
const llvm::Triple & getTriple() const
Definition: ToolChain.h:181
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:670
int Major
The parsed major, minor, and patch numbers.
Definition: Gnu.h:162
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:84