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