clang  6.0.0svn
Myriad.cpp
Go to the documentation of this file.
1 //===--- Myriad.cpp - Myriad ToolChain Implementations ----------*- 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 "Myriad.h"
11 #include "CommonArgs.h"
13 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Options.h"
16 #include "llvm/Option/ArgList.h"
17 
18 using namespace clang::driver;
19 using namespace clang::driver::toolchains;
20 using namespace clang;
21 using namespace llvm::opt;
22 
24 
26  const InputInfo &Output,
27  const InputInfoList &Inputs,
28  const ArgList &Args,
29  const char *LinkingOutput) const {
30  ArgStringList CmdArgs;
31  assert(Inputs.size() == 1);
32  const InputInfo &II = Inputs[0];
33  assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX ||
34  II.getType() == types::TY_PP_CXX);
35 
36  if (JA.getKind() == Action::PreprocessJobClass) {
37  Args.ClaimAllArgs();
38  CmdArgs.push_back("-E");
39  } else {
40  assert(Output.getType() == types::TY_PP_Asm); // Require preprocessed asm.
41  CmdArgs.push_back("-S");
42  CmdArgs.push_back("-fno-exceptions"); // Always do this even if unspecified.
43  }
44  CmdArgs.push_back("-DMYRIAD2");
45 
46  // Append all -I, -iquote, -isystem paths, defines/undefines, 'f'
47  // flags, 'g' flags, 'M' flags, optimize flags, warning options,
48  // mcpu flags, mllvm flags, and Xclang flags.
49  // These are spelled the same way in clang and moviCompile.
50  Args.AddAllArgsExcept(
51  CmdArgs,
52  {options::OPT_I_Group, options::OPT_clang_i_Group, options::OPT_std_EQ,
53  options::OPT_D, options::OPT_U, options::OPT_f_Group,
54  options::OPT_f_clang_Group, options::OPT_g_Group, options::OPT_M_Group,
55  options::OPT_O_Group, options::OPT_W_Group, options::OPT_mcpu_EQ,
56  options::OPT_mllvm, options::OPT_Xclang},
57  {options::OPT_fno_split_dwarf_inlining});
58  Args.hasArg(options::OPT_fno_split_dwarf_inlining); // Claim it if present.
59 
60  // If we're producing a dependency file, and assembly is the final action,
61  // then the name of the target in the dependency file should be the '.o'
62  // file, not the '.s' file produced by this step. For example, instead of
63  // /tmp/mumble.s: mumble.c .../someheader.h
64  // the filename on the lefthand side should be "mumble.o"
65  if (Args.getLastArg(options::OPT_MF) && !Args.getLastArg(options::OPT_MT) &&
66  C.getActions().size() == 1 &&
67  C.getActions()[0]->getKind() == Action::AssembleJobClass) {
68  Arg *A = Args.getLastArg(options::OPT_o);
69  if (A) {
70  CmdArgs.push_back("-MT");
71  CmdArgs.push_back(Args.MakeArgString(A->getValue()));
72  }
73  }
74 
75  CmdArgs.push_back(II.getFilename());
76  CmdArgs.push_back("-o");
77  CmdArgs.push_back(Output.getFilename());
78 
79  std::string Exec =
80  Args.MakeArgString(getToolChain().GetProgramPath("moviCompile"));
81  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
82  CmdArgs, Inputs));
83 }
84 
86  const InputInfo &Output,
87  const InputInfoList &Inputs,
88  const ArgList &Args,
89  const char *LinkingOutput) const {
90  ArgStringList CmdArgs;
91 
92  assert(Inputs.size() == 1);
93  const InputInfo &II = Inputs[0];
94  assert(II.getType() == types::TY_PP_Asm); // Require preprocessed asm input.
95  assert(Output.getType() == types::TY_Object);
96 
97  CmdArgs.push_back("-no6thSlotCompression");
98  const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
99  if (CPUArg)
100  CmdArgs.push_back(
101  Args.MakeArgString("-cv:" + StringRef(CPUArg->getValue())));
102  CmdArgs.push_back("-noSPrefixing");
103  CmdArgs.push_back("-a"); // Mystery option.
104  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
105  for (const Arg *A : Args.filtered(options::OPT_I, options::OPT_isystem)) {
106  A->claim();
107  CmdArgs.push_back(
108  Args.MakeArgString(std::string("-i:") + A->getValue(0)));
109  }
110  CmdArgs.push_back("-elf"); // Output format.
111  CmdArgs.push_back(II.getFilename());
112  CmdArgs.push_back(
113  Args.MakeArgString(std::string("-o:") + Output.getFilename()));
114 
115  std::string Exec =
116  Args.MakeArgString(getToolChain().GetProgramPath("moviAsm"));
117  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
118  CmdArgs, Inputs));
119 }
120 
122  const InputInfo &Output,
123  const InputInfoList &Inputs,
124  const ArgList &Args,
125  const char *LinkingOutput) const {
126  const auto &TC =
127  static_cast<const toolchains::MyriadToolChain &>(getToolChain());
128  const llvm::Triple &T = TC.getTriple();
129  ArgStringList CmdArgs;
130  bool UseStartfiles =
131  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
132  bool UseDefaultLibs =
133  !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs);
134  // Silence warning if the args contain both -nostdlib and -stdlib=.
135  Args.getLastArg(options::OPT_stdlib_EQ);
136 
137  if (T.getArch() == llvm::Triple::sparc)
138  CmdArgs.push_back("-EB");
139  else // SHAVE assumes little-endian, and sparcel is expressly so.
140  CmdArgs.push_back("-EL");
141 
142  // The remaining logic is mostly like gnutools::Linker::ConstructJob,
143  // but we never pass through a --sysroot option and various other bits.
144  // For example, there are no sanitizers (yet) nor gold linker.
145 
146  // Eat some arguments that may be present but have no effect.
147  Args.ClaimAllArgs(options::OPT_g_Group);
148  Args.ClaimAllArgs(options::OPT_w);
149  Args.ClaimAllArgs(options::OPT_static_libgcc);
150 
151  if (Args.hasArg(options::OPT_s)) // Pass the 'strip' option.
152  CmdArgs.push_back("-s");
153 
154  CmdArgs.push_back("-o");
155  CmdArgs.push_back(Output.getFilename());
156 
157  if (UseStartfiles) {
158  // If you want startfiles, it means you want the builtin crti and crtbegin,
159  // but not crt0. Myriad link commands provide their own crt0.o as needed.
160  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crti.o")));
161  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o")));
162  }
163 
164  Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
165  options::OPT_e, options::OPT_s, options::OPT_t,
166  options::OPT_Z_Flag, options::OPT_r});
167 
168  TC.AddFilePathLibArgs(Args, CmdArgs);
169 
170  bool NeedsSanitizerDeps = addSanitizerRuntimes(TC, Args, CmdArgs);
171  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
172 
173  if (UseDefaultLibs) {
174  if (NeedsSanitizerDeps)
175  linkSanitizerRuntimeDeps(TC, CmdArgs);
176  if (C.getDriver().CCCIsCXX()) {
177  if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) {
178  CmdArgs.push_back("-lc++");
179  CmdArgs.push_back("-lc++abi");
180  } else
181  CmdArgs.push_back("-lstdc++");
182  }
183  if (T.getOS() == llvm::Triple::RTEMS) {
184  CmdArgs.push_back("--start-group");
185  CmdArgs.push_back("-lc");
186  CmdArgs.push_back("-lgcc"); // circularly dependent on rtems
187  // You must provide your own "-L" option to enable finding these.
188  CmdArgs.push_back("-lrtemscpu");
189  CmdArgs.push_back("-lrtemsbsp");
190  CmdArgs.push_back("--end-group");
191  } else {
192  CmdArgs.push_back("-lc");
193  CmdArgs.push_back("-lgcc");
194  }
195  }
196  if (UseStartfiles) {
197  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o")));
198  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtn.o")));
199  }
200 
201  std::string Exec =
202  Args.MakeArgString(TC.GetProgramPath("sparc-myriad-elf-ld"));
203  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
204  CmdArgs, Inputs));
205 }
206 
207 MyriadToolChain::MyriadToolChain(const Driver &D, const llvm::Triple &Triple,
208  const ArgList &Args)
209  : Generic_ELF(D, Triple, Args) {
210  // If a target of 'sparc-myriad-elf' is specified to clang, it wants to use
211  // 'sparc-myriad--elf' (note the unknown OS) as the canonical triple.
212  // This won't work to find gcc. Instead we give the installation detector an
213  // extra triple, which is preferable to further hacks of the logic that at
214  // present is based solely on getArch(). In particular, it would be wrong to
215  // choose the myriad installation when targeting a non-myriad sparc install.
216  switch (Triple.getArch()) {
217  default:
218  D.Diag(clang::diag::err_target_unsupported_arch)
219  << Triple.getArchName() << "myriad";
220  LLVM_FALLTHROUGH;
221  case llvm::Triple::sparc:
222  case llvm::Triple::sparcel:
223  case llvm::Triple::shave:
224  GCCInstallation.init(Triple, Args, {"sparc-myriad-elf"});
225  }
226 
227  if (GCCInstallation.isValid()) {
228  // This directory contains crt{i,n,begin,end}.o as well as libgcc.
229  // These files are tied to a particular version of gcc.
230  SmallString<128> CompilerSupportDir(GCCInstallation.getInstallPath());
231  addPathIfExists(D, CompilerSupportDir, getFilePaths());
232  }
233  // libstd++ and libc++ must both be found in this one place.
234  addPathIfExists(D, D.Dir + "/../sparc-myriad-elf/lib", getFilePaths());
235 }
236 
238 
239 void MyriadToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
240  ArgStringList &CC1Args) const {
241  if (!DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
242  addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
243 }
244 
246  std::string Path(getDriver().getInstalledDir());
247  return Path + "/../include/c++/v1";
248 }
249 
251  const llvm::opt::ArgList &DriverArgs,
252  llvm::opt::ArgStringList &CC1Args) const {
253  StringRef LibDir = GCCInstallation.getParentLibPath();
254  const GCCVersion &Version = GCCInstallation.getVersion();
255  StringRef TripleStr = GCCInstallation.getTriple().str();
258  LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
259  "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
260 }
261 
262 // MyriadToolChain handles several triples:
263 // {shave,sparc{,el}}-myriad-{rtems,unknown}-elf
265  // The inherited method works fine if not targeting the SHAVE.
267  return ToolChain::SelectTool(JA);
268  switch (JA.getKind()) {
271  if (!Compiler)
272  Compiler.reset(new tools::SHAVE::Compiler(*this));
273  return Compiler.get();
275  if (!Assembler)
276  Assembler.reset(new tools::SHAVE::Assembler(*this));
277  return Assembler.get();
278  default:
279  return ToolChain::getTool(JA.getKind());
280  }
281 }
282 
284  return new tools::Myriad::Linker(*this);
285 }
286 
288  return SanitizerKind::Address;
289 }
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:220
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
Struct to store and manipulate GCC versions.
Definition: Gnu.h:157
MyriadToolChain - A tool chain using either clang or the external compiler installed by the Movidius ...
Definition: Myriad.h:70
const char * getFilename() const
Definition: InputInfo.h:84
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:277
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:61
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:127
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
path_list & getFilePaths()
Definition: ToolChain.h:202
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
types::ID getType() const
Definition: InputInfo.h:78
bool isShaveCompilation(const llvm::Triple &T) const
Definition: Myriad.h:89
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:239
ActionClass getKind() const
Definition: Action.h:131
const FunctionProtoType * T
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:25
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Myriad.cpp:250
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Myriad.cpp:239
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Myriad.cpp:85
const Driver & getDriver() const
Definition: ToolChain.h:167
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
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:664
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:226
MyriadToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Myriad.cpp:207
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Myriad.cpp:121
std::string findLibCxxIncludePath() const override
Definition: Myriad.cpp:245
Dataflow Directional Tag Classes.
uint64_t SanitizerMask
Definition: Sanitizers.h:24
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Myriad.cpp:25
ActionList & getActions()
Definition: Compilation.h:174
Tool * SelectTool(const JobAction &JA) const override
Choose a tool to use to handle the action JA.
Definition: Myriad.cpp:264
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:34
const Driver & getDriver() const
Definition: Compilation.h:117
const llvm::Triple & getTriple() const
Definition: ToolChain.h:169
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:276
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:59
virtual Tool * SelectTool(const JobAction &JA) const
Choose a tool to use to handle the action JA.
Definition: ToolChain.cpp:383
bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple, StringRef TargetMultiarchTriple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Helper to add the variant paths of a libstdc++ installation.
Definition: Gnu.cpp:2294
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1587
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Myriad.cpp:287
Tool * buildLinker() const override
Definition: Myriad.cpp:283
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:223
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:229
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:217