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 // This function tests whether a gcc installation is present either
35 // through gcc-toolchain argument or in the same prefix where clang
36 // is installed. This helps decide whether to instantiate this toolchain
37 // or Baremetal toolchain.
39  const llvm::opt::ArgList &Args) {
40  if (Args.getLastArg(options::OPT_gcc_toolchain))
41  return true;
42 
43  SmallString<128> GCCDir;
44  llvm::sys::path::append(GCCDir, D.Dir, "..", D.getTargetTriple(),
45  "lib/crt0.o");
46  return llvm::sys::fs::exists(GCCDir);
47 }
48 
49 /// RISCV Toolchain
50 RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
51  const ArgList &Args)
52  : Generic_ELF(D, Triple, Args) {
53  GCCInstallation.init(Triple, Args);
54  if (GCCInstallation.isValid()) {
57  path_list &Paths = getFilePaths();
58  // Add toolchain/multilib specific file paths.
61  getFilePaths().push_back(GCCInstallation.getInstallPath().str());
63  // Multilib cross-compiler GCC installations put ld in a triple-prefixed
64  // directory off of the parent of the GCC installation.
65  PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
66  GCCInstallation.getTriple().str() + "/bin")
67  .str());
68  PPaths.push_back((GCCInstallation.getParentLibPath() + "/../bin").str());
69  } else {
70  getProgramPaths().push_back(D.Dir);
71  }
72  getFilePaths().push_back(computeSysRoot() + "/lib");
73 }
74 
76  return new tools::RISCV::Linker(*this);
77 }
78 
80  return GCCInstallation.isValid() ?
82 }
83 
85 RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
86  return ToolChain::UNW_None;
87 }
88 
90  const llvm::opt::ArgList &DriverArgs,
91  llvm::opt::ArgStringList &CC1Args,
92  Action::OffloadKind) const {
93  CC1Args.push_back("-nostdsysteminc");
94 }
95 
96 void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
97  ArgStringList &CC1Args) const {
98  if (DriverArgs.hasArg(options::OPT_nostdinc))
99  return;
100 
101  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
102  SmallString<128> Dir(computeSysRoot());
103  llvm::sys::path::append(Dir, "include");
104  addSystemInclude(DriverArgs, CC1Args, Dir.str());
105  }
106 }
107 
109  const llvm::opt::ArgList &DriverArgs,
110  llvm::opt::ArgStringList &CC1Args) const {
111  const GCCVersion &Version = GCCInstallation.getVersion();
112  StringRef TripleStr = GCCInstallation.getTriple().str();
114  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
115  "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
116 }
117 
118 std::string RISCVToolChain::computeSysRoot() const {
119  if (!getDriver().SysRoot.empty())
120  return getDriver().SysRoot;
121 
122  SmallString<128> SysRootDir;
123  if (GCCInstallation.isValid()) {
124  StringRef LibDir = GCCInstallation.getParentLibPath();
125  StringRef TripleStr = GCCInstallation.getTriple().str();
126  llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
127  } else {
128  // Use the triple as provided to the driver. Unlike the parsed triple
129  // this has not been normalized to always contain every field.
130  llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
131  getDriver().getTargetTriple());
132  }
133 
134  if (!llvm::sys::fs::exists(SysRootDir))
135  return std::string();
136 
137  return std::string(SysRootDir.str());
138 }
139 
140 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
141  const InputInfo &Output,
142  const InputInfoList &Inputs,
143  const ArgList &Args,
144  const char *LinkingOutput) const {
145  const ToolChain &ToolChain = getToolChain();
146  const Driver &D = ToolChain.getDriver();
147  ArgStringList CmdArgs;
148 
149  if (!D.SysRoot.empty())
150  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
151 
152  bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
153  CmdArgs.push_back("-m");
154  if (IsRV64) {
155  CmdArgs.push_back("elf64lriscv");
156  } else {
157  CmdArgs.push_back("elf32lriscv");
158  }
159 
160  std::string Linker = getToolChain().GetLinkerPath();
161 
162  bool WantCRTs =
163  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
164 
165  const char *crtbegin, *crtend;
166  auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
167  if (RuntimeLib == ToolChain::RLT_Libgcc) {
168  crtbegin = "crtbegin.o";
169  crtend = "crtend.o";
170  } else {
171  assert (RuntimeLib == ToolChain::RLT_CompilerRT);
172  crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
174  crtend = ToolChain.getCompilerRTArgString(Args, "crtend",
176  }
177 
178  if (WantCRTs) {
179  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
180  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
181  }
182 
183  Args.AddAllArgs(CmdArgs, options::OPT_L);
184  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
185  Args.AddAllArgs(CmdArgs,
186  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
187  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
188 
189  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
190 
191  // TODO: add C++ includes and libs if compiling C++.
192 
193  if (!Args.hasArg(options::OPT_nostdlib) &&
194  !Args.hasArg(options::OPT_nodefaultlibs)) {
195  if (ToolChain.ShouldLinkCXXStdlib(Args))
196  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
197  CmdArgs.push_back("--start-group");
198  CmdArgs.push_back("-lc");
199  CmdArgs.push_back("-lgloss");
200  CmdArgs.push_back("--end-group");
201  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
202  }
203 
204  if (WantCRTs)
205  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
206 
207  CmdArgs.push_back("-o");
208  CmdArgs.push_back(Output.getFilename());
209  C.addCommand(std::make_unique<Command>(
210  JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
211  CmdArgs, Inputs, Output));
212 }
213 // 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:227
static bool hasGCCToolchain(const Driver &D, const llvm::opt::ArgList &Args)
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
std::string getTargetTriple() const
Definition: Driver.h:330
Struct to store and manipulate GCC versions.
Definition: Gnu.h:161
const char * getFilename() const
Definition: InputInfo.h:83
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
path_list & getProgramPaths()
Definition: ToolChain.h:246
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:548
path_list & getFilePaths()
Definition: ToolChain.h:243
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:246
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:221
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:1025
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:205
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:939
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:874
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1002
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:469
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:120
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:2973
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:1894
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
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:230
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
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:1008
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224