clang  15.0.0git
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 "CommonArgs.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/InputInfo.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(std::make_unique<Command>(JA, *this,
44  Exec, CmdArgs, Inputs, Output));
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  options::OPT_r)) {
90  if (!Args.hasArg(options::OPT_shared)) {
91  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
92  }
93  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
94  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) {
95  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
96  } else {
97  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
98  }
99  }
100 
101  Args.AddAllArgs(CmdArgs, options::OPT_L);
102  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
103  Args.AddAllArgs(CmdArgs,
104  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
105  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
106 
107  if (D.isUsingLTO()) {
108  assert(!Inputs.empty() && "Must have at least one input.");
109  addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
110  D.getLTOMode() == LTOK_Thin);
111  }
112 
113  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
114 
115  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
116  options::OPT_r)) {
117  if (ToolChain.ShouldLinkCXXStdlib(Args))
118  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
119  CmdArgs.push_back("-lc");
120  }
121 
122  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
123  options::OPT_r)) {
124  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
125  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
126  else
127  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
128  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
129  }
130 
131  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
132  C.addCommand(std::make_unique<Command>(JA, *this,
134  Exec, CmdArgs, Inputs, Output));
135 }
136 
137 // Ananas - Ananas tool chain which can call as(1) and ld(1) directly.
138 
139 Ananas::Ananas(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
140  : Generic_ELF(D, Triple, Args) {
141  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
142 }
143 
145  return new tools::ananas::Assembler(*this);
146 }
147 
148 Tool *Ananas::buildLinker() const { return new tools::ananas::Linker(*this); }
clang::driver::toolchains
Definition: AIX.h:55
Driver.h
clang::driver::tools::ananas::Linker::ConstructJob
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
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::tools
Definition: AIX.h:17
clang::driver::toolchains::Ananas::buildLinker
Tool * buildLinker() const override
Definition: Ananas.cpp:148
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:228
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:605
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::tools::ananas::Assembler::ConstructJob
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
InputInfo.h
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
Ananas.h
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:266
clang::driver::Driver::getLTOMode
LTOKind getLTOMode(bool IsOffload=false) const
Get the specific kind of LTO being performed.
Definition: Driver.h:658
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:362
clang::driver::tools::claimNoWarnArgs
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
Options.h
clang::driver::InputInfo::isFilename
bool isFilename() const
Definition: InputInfo.h:75
clang::driver::LTOK_Thin
@ LTOK_Thin
Definition: Driver.h:56
clang::driver::tools::ananas::Linker
Definition: Ananas.h:34
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::ResponseFileSupport::AtFileCurCP
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:74
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:178
clang::driver::ToolChain::AddFilePathLibArgs
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:1024
clang::driver::ToolChain::ShouldLinkCXXStdlib
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1001
clang::driver::tools::addLTOOptions
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
clang::driver::Driver::isUsingLTO
bool isUsingLTO(bool IsOffload=false) const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:653
clang::driver::ToolChain::AddCXXStdlibLibArgs
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:1007
Compilation.h
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::ToolChain::GetLinkerPath
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:613
clang::driver::toolchains::Ananas::buildAssembler
Tool * buildAssembler() const override
Definition: Ananas.cpp:144
clang::driver::tools::ananas::Assembler
Definition: Ananas.h:22
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:72
clang::driver
Definition: Action.h:31
clang::driver::JobAction
Definition: Action.h:395