clang  13.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,
116  CC1Args);
117 }
118 
119 std::string RISCVToolChain::computeSysRoot() const {
120  if (!getDriver().SysRoot.empty())
121  return getDriver().SysRoot;
122 
123  SmallString<128> SysRootDir;
124  if (GCCInstallation.isValid()) {
125  StringRef LibDir = GCCInstallation.getParentLibPath();
126  StringRef TripleStr = GCCInstallation.getTriple().str();
127  llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
128  } else {
129  // Use the triple as provided to the driver. Unlike the parsed triple
130  // this has not been normalized to always contain every field.
131  llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
132  getDriver().getTargetTriple());
133  }
134 
135  if (!llvm::sys::fs::exists(SysRootDir))
136  return std::string();
137 
138  return std::string(SysRootDir.str());
139 }
140 
141 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
142  const InputInfo &Output,
143  const InputInfoList &Inputs,
144  const ArgList &Args,
145  const char *LinkingOutput) const {
146  const ToolChain &ToolChain = getToolChain();
147  const Driver &D = ToolChain.getDriver();
148  ArgStringList CmdArgs;
149 
150  if (!D.SysRoot.empty())
151  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
152 
153  bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
154  CmdArgs.push_back("-m");
155  if (IsRV64) {
156  CmdArgs.push_back("elf64lriscv");
157  } else {
158  CmdArgs.push_back("elf32lriscv");
159  }
160 
161  std::string Linker = getToolChain().GetLinkerPath();
162 
163  bool WantCRTs =
164  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
165 
166  const char *crtbegin, *crtend;
167  auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
168  if (RuntimeLib == ToolChain::RLT_Libgcc) {
169  crtbegin = "crtbegin.o";
170  crtend = "crtend.o";
171  } else {
172  assert (RuntimeLib == ToolChain::RLT_CompilerRT);
173  crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
175  crtend = ToolChain.getCompilerRTArgString(Args, "crtend",
177  }
178 
179  if (WantCRTs) {
180  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
181  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
182  }
183 
184  Args.AddAllArgs(CmdArgs, options::OPT_L);
185  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
186  Args.AddAllArgs(CmdArgs,
187  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
188  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
189 
190  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
191 
192  // TODO: add C++ includes and libs if compiling C++.
193 
194  if (!Args.hasArg(options::OPT_nostdlib) &&
195  !Args.hasArg(options::OPT_nodefaultlibs)) {
196  if (ToolChain.ShouldLinkCXXStdlib(Args))
197  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
198  CmdArgs.push_back("--start-group");
199  CmdArgs.push_back("-lc");
200  CmdArgs.push_back("-lgloss");
201  CmdArgs.push_back("--end-group");
202  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
203  }
204 
205  if (WantCRTs)
206  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
207 
208  CmdArgs.push_back("-o");
209  CmdArgs.push_back(Output.getFilename());
210  C.addCommand(std::make_unique<Command>(
211  JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
212  CmdArgs, Inputs, Output));
213 }
214 // RISCV tools end.
clang::driver::toolchains::RISCVToolChain::hasGCCToolchain
static bool hasGCCToolchain(const Driver &D, const llvm::opt::ArgList &Args)
Definition: RISCVToolchain.cpp:38
clang::driver::toolchains::RISCVToolChain::GetUnwindLibType
UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const override
Definition: RISCVToolchain.cpp:85
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:255
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::Driver::Dir
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:120
clang::driver::MultilibSet
Definition: Multilib.h:114
llvm::SmallVector< std::string, 16 >
clang::driver::toolchains::Generic_GCC::GCCInstallation
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
clang::driver::tools
Definition: AIX.h:17
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:214
clang::driver::ToolChain::FT_Object
@ FT_Object
Definition: ToolChain.h:116
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::isValid
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
clang::driver::toolchains::RISCVToolChain::addLibStdCxxIncludePaths
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: RISCVToolchain.cpp:108
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:557
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::init
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:1896
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::ToolChain::RuntimeLibType
RuntimeLibType
Definition: ToolChain.h:100
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)
clang::driver::toolchains::Generic_GCC::GCCVersion
Struct to store and manipulate GCC versions.
Definition: Gnu.h:161
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:252
RISCVToolchain.h
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:367
Options.h
clang::driver::toolchains::Generic_GCC::GCCVersion::Text
std::string Text
The unparsed text of the version.
Definition: Gnu.h:163
clang::driver::ToolChain::UNW_None
@ UNW_None
Definition: ToolChain.h:106
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::ResponseFileSupport::AtFileCurCP
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:91
clang::driver::toolchains::RISCVToolChain::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: RISCVToolchain.cpp:96
clang::driver::ToolChain::RLT_Libgcc
@ RLT_Libgcc
Definition: ToolChain.h:102
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getTriple
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getParentLibPath
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
clang::driver::tools::addPathIfExists
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:121
llvm::SmallString< 128 >
clang::driver::ToolChain::SelectedMultilib
Multilib SelectedMultilib
Definition: ToolChain.h:175
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
clang::driver::Multilib
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:28
clang::driver::MultilibSet::filePathsCallback
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:186
clang::driver::toolchains::RISCVToolChain::buildLinker
Tool * buildLinker() const override
Definition: RISCVToolchain.cpp:75
clang::driver::ToolChain::getArch
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:230
addMultilibsFilePaths
static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, const Multilib &Multilib, StringRef InstallPath, ToolChain::path_list &Paths)
Definition: RISCVToolchain.cpp:25
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:972
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:949
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:955
clang::driver::ToolChain::GetRuntimeLibType
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:774
Compilation.h
clang::driver::ToolChain::getCompilerRTArgString
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:478
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::ToolChain::RLT_CompilerRT
@ RLT_CompilerRT
Definition: ToolChain.h:101
clang::driver::tools::AddRunTimeLibs
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getInstallPath
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getVersion
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:246
clang::driver::toolchains::RISCVToolChain::addClangTargetOptions
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.
Definition: RISCVToolchain.cpp:89
clang::driver::tools::RISCV::Linker
Definition: RISCVToolchain.h:50
clang::driver::toolchains::Generic_GCC::addLibStdCXXIncludePaths
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:2926
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
CommonArgs.h
clang
Dataflow Directional Tag Classes.
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:59
clang::driver::Driver::getTargetTriple
std::string getTargetTriple() const
Definition: Driver.h:337
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getMultilib
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
clang::driver::toolchains::RISCVToolChain::GetDefaultRuntimeLibType
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: RISCVToolchain.cpp:79
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getMultilibs
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
clang::driver::ToolChain::Multilibs
MultilibSet Multilibs
Definition: ToolChain.h:174
clang::driver::ToolChain::UnwindLibType
UnwindLibType
Definition: ToolChain.h:105
clang::driver::Multilib::includeSuffix
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:67
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:85
clang::driver
Definition: Action.h:31
clang::driver::ToolChain::addSystemInclude
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:862
clang::driver::JobAction
Definition: Action.h:380
clang::driver::toolchains::RISCVToolChain::RISCVToolChain
RISCVToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
RISCV Toolchain.
Definition: RISCVToolchain.cpp:50