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