clang  12.0.0git
RISCVToolchain.cpp
Go to the documentation of this file.
1 //===--- RISCVToolchain.cpp - RISCV 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 "RISCVToolchain.h"
10 #include "CommonArgs.h"
11 #include "InputInfo.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 using namespace clang::driver;
20 using namespace clang::driver::toolchains;
21 using namespace clang::driver::tools;
22 using namespace clang;
23 using namespace llvm::opt;
24 
25 static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
26  const Multilib &Multilib,
27  StringRef InstallPath,
28  ToolChain::path_list &Paths) {
29  if (const auto &PathsCallback = Multilibs.filePathsCallback())
30  for (const auto &Path : PathsCallback(Multilib))
31  addPathIfExists(D, InstallPath + Path, Paths);
32 }
33 
34 /// RISCV Toolchain
35 RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
36  const ArgList &Args)
37  : Generic_ELF(D, Triple, Args) {
38  GCCInstallation.init(Triple, Args);
39  if (GCCInstallation.isValid()) {
42  path_list &Paths = getFilePaths();
43  // Add toolchain/multilib specific file paths.
46  getFilePaths().push_back(GCCInstallation.getInstallPath().str());
48  // Multilib cross-compiler GCC installations put ld in a triple-prefixed
49  // directory off of the parent of the GCC installation.
50  PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
51  GCCInstallation.getTriple().str() + "/bin")
52  .str());
53  PPaths.push_back((GCCInstallation.getParentLibPath() + "/../bin").str());
54  } else {
55  getProgramPaths().push_back(D.Dir);
56  }
57  getFilePaths().push_back(computeSysRoot() + "/lib");
58 }
59 
61  return new tools::RISCV::Linker(*this);
62 }
63 
65  return GCCInstallation.isValid() ?
67 }
68 
70 RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
71  return ToolChain::UNW_None;
72 }
73 
75  const llvm::opt::ArgList &DriverArgs,
76  llvm::opt::ArgStringList &CC1Args,
77  Action::OffloadKind) const {
78  CC1Args.push_back("-nostdsysteminc");
79 }
80 
81 void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
82  ArgStringList &CC1Args) const {
83  if (DriverArgs.hasArg(options::OPT_nostdinc))
84  return;
85 
86  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
87  SmallString<128> Dir(computeSysRoot());
88  llvm::sys::path::append(Dir, "include");
89  addSystemInclude(DriverArgs, CC1Args, Dir.str());
90  }
91 }
92 
94  const llvm::opt::ArgList &DriverArgs,
95  llvm::opt::ArgStringList &CC1Args) const {
96  const GCCVersion &Version = GCCInstallation.getVersion();
97  StringRef TripleStr = GCCInstallation.getTriple().str();
99  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
100  "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
101 }
102 
103 std::string RISCVToolChain::computeSysRoot() const {
104  if (!getDriver().SysRoot.empty())
105  return getDriver().SysRoot;
106 
107  SmallString<128> SysRootDir;
108  if (GCCInstallation.isValid()) {
109  StringRef LibDir = GCCInstallation.getParentLibPath();
110  StringRef TripleStr = GCCInstallation.getTriple().str();
111  llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
112  } else {
113  // Use the triple as provided to the driver. Unlike the parsed triple
114  // this has not been normalized to always contain every field.
115  llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
116  getDriver().getTargetTriple());
117  }
118 
119  if (!llvm::sys::fs::exists(SysRootDir))
120  return std::string();
121 
122  return std::string(SysRootDir.str());
123 }
124 
125 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
126  const InputInfo &Output,
127  const InputInfoList &Inputs,
128  const ArgList &Args,
129  const char *LinkingOutput) const {
130  const ToolChain &ToolChain = getToolChain();
131  const Driver &D = ToolChain.getDriver();
132  ArgStringList CmdArgs;
133 
134  if (!D.SysRoot.empty())
135  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
136 
137  bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
138  CmdArgs.push_back("-m");
139  if (IsRV64) {
140  CmdArgs.push_back("elf64lriscv");
141  } else {
142  CmdArgs.push_back("elf32lriscv");
143  }
144 
145  std::string Linker = getToolChain().GetLinkerPath();
146 
147  bool WantCRTs =
148  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
149 
150  const char *crtbegin, *crtend;
151  auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
152  if (RuntimeLib == ToolChain::RLT_Libgcc) {
153  crtbegin = "crtbegin.o";
154  crtend = "crtend.o";
155  } else {
156  assert (RuntimeLib == ToolChain::RLT_CompilerRT);
157  crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
159  crtend = ToolChain.getCompilerRTArgString(Args, "crtend",
161  }
162 
163  if (WantCRTs) {
164  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
165  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
166  }
167 
168  Args.AddAllArgs(CmdArgs, options::OPT_L);
169  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
170  Args.AddAllArgs(CmdArgs,
171  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
172  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
173 
174  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
175 
176  // TODO: add C++ includes and libs if compiling C++.
177 
178  if (!Args.hasArg(options::OPT_nostdlib) &&
179  !Args.hasArg(options::OPT_nodefaultlibs)) {
180  if (ToolChain.ShouldLinkCXXStdlib(Args))
181  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
182  CmdArgs.push_back("--start-group");
183  CmdArgs.push_back("-lc");
184  CmdArgs.push_back("-lgloss");
185  CmdArgs.push_back("--end-group");
186  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
187  }
188 
189  if (WantCRTs)
190  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
191 
192  CmdArgs.push_back("-o");
193  CmdArgs.push_back(Output.getFilename());
194  C.addCommand(
195  std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileCurCP(),
196  Args.MakeArgString(Linker), CmdArgs, Inputs));
197 }
198 // RISCV tools end.
UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const override
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:224
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:186
Struct to store and manipulate GCC versions.
Definition: Gnu.h:161
const char * getFilename() const
Definition: InputInfo.h:83
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:284
path_list & getProgramPaths()
Definition: ToolChain.h:245
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:67
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:120
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:541
path_list & getFilePaths()
Definition: ToolChain.h:242
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:243
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:28
static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, const Multilib &Multilib, StringRef InstallPath, ToolChain::path_list &Paths)
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:220
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:945
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind) const override
Add options that need to be passed to cc1 for this target.
const Driver & getDriver() const
Definition: ToolChain.h:204
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:859
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:230
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:794
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:922
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:148
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
std::string Text
The unparsed text of the version.
Definition: Gnu.h:163
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:462
RISCVToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
RISCV Toolchain.
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:91
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:63
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:2920
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:1878
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:236
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:227
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:233
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:928
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:90
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:221