clang  7.0.0svn
Ananas.cpp
Go to the documentation of this file.
1 //===--- Ananas.cpp - Ananas 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 "Ananas.h"
11 #include "InputInfo.h"
12 #include "CommonArgs.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Options.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/Path.h"
19 
20 using namespace clang::driver;
21 using namespace clang::driver::tools;
22 using namespace clang::driver::toolchains;
23 using namespace clang;
24 using namespace llvm::opt;
25 
27  const InputInfo &Output,
28  const InputInfoList &Inputs,
29  const ArgList &Args,
30  const char *LinkingOutput) const {
31  claimNoWarnArgs(Args);
32  ArgStringList CmdArgs;
33 
34  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
35 
36  CmdArgs.push_back("-o");
37  CmdArgs.push_back(Output.getFilename());
38 
39  for (const auto &II : Inputs)
40  CmdArgs.push_back(II.getFilename());
41 
42  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
43  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
44 }
45 
47  const InputInfo &Output,
48  const InputInfoList &Inputs,
49  const ArgList &Args,
50  const char *LinkingOutput) const {
51  const ToolChain &ToolChain = getToolChain();
52  const Driver &D = ToolChain.getDriver();
53  ArgStringList CmdArgs;
54 
55  // Silence warning for "clang -g foo.o -o foo"
56  Args.ClaimAllArgs(options::OPT_g_Group);
57  // and "clang -emit-llvm foo.o -o foo"
58  Args.ClaimAllArgs(options::OPT_emit_llvm);
59  // and for "clang -w foo.o -o foo". Other warning options are already
60  // handled somewhere else.
61  Args.ClaimAllArgs(options::OPT_w);
62 
63  if (!D.SysRoot.empty())
64  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
65 
66  if (Args.hasArg(options::OPT_static)) {
67  CmdArgs.push_back("-Bstatic");
68  } else {
69  if (Args.hasArg(options::OPT_rdynamic))
70  CmdArgs.push_back("-export-dynamic");
71  if (Args.hasArg(options::OPT_shared)) {
72  CmdArgs.push_back("-Bshareable");
73  } else {
74  Args.AddAllArgs(CmdArgs, options::OPT_pie);
75  CmdArgs.push_back("-dynamic-linker");
76  CmdArgs.push_back("/lib/ld-ananas.so");
77  }
78  }
79 
80  if (Output.isFilename()) {
81  CmdArgs.push_back("-o");
82  CmdArgs.push_back(Output.getFilename());
83  } else {
84  assert(Output.isNothing() && "Invalid output.");
85  }
86 
87  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
88  if (!Args.hasArg(options::OPT_shared)) {
89  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
90  }
91  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
92  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) {
93  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
94  } else {
95  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
96  }
97  }
98 
99  Args.AddAllArgs(CmdArgs, options::OPT_L);
100  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
101  Args.AddAllArgs(CmdArgs,
102  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
103  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
104 
105  if (D.isUsingLTO()) {
106  assert(!Inputs.empty() && "Must have at least one input.");
107  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
108  D.getLTOMode() == LTOK_Thin);
109  }
110 
111  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
112 
113  if (ToolChain.ShouldLinkCXXStdlib(Args))
114  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
115  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
116  CmdArgs.push_back("-lc");
117 
118  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
119  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
120  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
121  else
122  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
123  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
124  }
125 
126  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
127  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
128 }
129 
130 // Ananas - Ananas tool chain which can call as(1) and ld(1) directly.
131 
132 Ananas::Ananas(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
133  : Generic_ELF(D, Triple, Args) {
134  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
135 }
136 
138  return new tools::ananas::Assembler(*this);
139 }
140 
141 Tool *Ananas::buildLinker() const { return new tools::ananas::Linker(*this); }
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:519
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:522
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:411
const char * getFilename() const
Definition: InputInfo.h:84
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:403
path_list & getFilePaths()
Definition: ToolChain.h:216
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:206
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: Ananas.cpp:26
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:750
const Driver & getDriver() const
Definition: ToolChain.h:181
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:727
Dataflow Directional Tag Classes.
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 claimNoWarnArgs(const llvm::opt::ArgList &Args)
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
Tool * buildAssembler() const override
Definition: Ananas.cpp:137
bool isNothing() const
Definition: InputInfo.h:75
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: Ananas.cpp:46
bool isFilename() const
Definition: InputInfo.h:76
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:733
Tool * buildLinker() const override
Definition: Ananas.cpp:141
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88