clang 20.0.0git
RISCVToolchain.cpp
Go to the documentation of this file.
1//===--- RISCVToolchain.cpp - RISC-V 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"
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
19using namespace clang::driver;
20using namespace clang::driver::toolchains;
21using namespace clang::driver::tools;
22using namespace clang;
23using namespace llvm::opt;
24
25static 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/// RISC-V Toolchain
50RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
51 const ArgList &Args)
52 : Generic_ELF(D, Triple, Args) {
53 GCCInstallation.init(Triple, Args);
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
85RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
87}
88
90 const llvm::opt::ArgList &Args) const {
92}
93
95 const llvm::opt::ArgList &DriverArgs,
96 llvm::opt::ArgStringList &CC1Args,
97 Action::OffloadKind) const {
98 CC1Args.push_back("-nostdsysteminc");
99}
100
101void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
102 ArgStringList &CC1Args) const {
103 if (DriverArgs.hasArg(options::OPT_nostdinc))
104 return;
105
106 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
107 SmallString<128> Dir(getDriver().ResourceDir);
108 llvm::sys::path::append(Dir, "include");
109 addSystemInclude(DriverArgs, CC1Args, Dir.str());
110 }
111
112 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
113 SmallString<128> Dir(computeSysRoot());
114 llvm::sys::path::append(Dir, "include");
115 addSystemInclude(DriverArgs, CC1Args, Dir.str());
116 }
117}
118
120 const llvm::opt::ArgList &DriverArgs,
121 llvm::opt::ArgStringList &CC1Args) const {
122 const GCCVersion &Version = GCCInstallation.getVersion();
123 StringRef TripleStr = GCCInstallation.getTriple().str();
125 addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
126 TripleStr, Multilib.includeSuffix(), DriverArgs,
127 CC1Args);
128}
129
130std::string RISCVToolChain::computeSysRoot() const {
131 if (!getDriver().SysRoot.empty())
132 return getDriver().SysRoot;
133
134 SmallString<128> SysRootDir;
135 if (GCCInstallation.isValid()) {
136 StringRef LibDir = GCCInstallation.getParentLibPath();
137 StringRef TripleStr = GCCInstallation.getTriple().str();
138 llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
139 } else {
140 // Use the triple as provided to the driver. Unlike the parsed triple
141 // this has not been normalized to always contain every field.
142 llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
143 getDriver().getTargetTriple());
144 }
145
146 if (!llvm::sys::fs::exists(SysRootDir))
147 return std::string();
148
149 return std::string(SysRootDir);
150}
151
153 const InputInfo &Output,
154 const InputInfoList &Inputs,
155 const ArgList &Args,
156 const char *LinkingOutput) const {
157 const ToolChain &ToolChain = getToolChain();
158 const Driver &D = ToolChain.getDriver();
159 ArgStringList CmdArgs;
160
161 if (!D.SysRoot.empty())
162 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
163
164 if (Args.hasArg(options::OPT_mno_relax))
165 CmdArgs.push_back("--no-relax");
166
167 bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
168 CmdArgs.push_back("-m");
169 if (IsRV64) {
170 CmdArgs.push_back("elf64lriscv");
171 } else {
172 CmdArgs.push_back("elf32lriscv");
173 }
174 CmdArgs.push_back("-X");
175
176 std::string Linker = getToolChain().GetLinkerPath();
177
178 bool WantCRTs =
179 !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
180
181 const char *crtbegin, *crtend;
182 auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
183 if (RuntimeLib == ToolChain::RLT_Libgcc) {
184 crtbegin = "crtbegin.o";
185 crtend = "crtend.o";
186 } else {
187 assert (RuntimeLib == ToolChain::RLT_CompilerRT);
188 crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
190 crtend = ToolChain.getCompilerRTArgString(Args, "crtend",
192 }
193
194 if (WantCRTs) {
195 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
196 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
197 }
198
199 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
200
201 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
202
203 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
204 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
205 options::OPT_t, options::OPT_r});
206
207 // TODO: add C++ includes and libs if compiling C++.
208
209 if (!Args.hasArg(options::OPT_nostdlib) &&
210 !Args.hasArg(options::OPT_nodefaultlibs)) {
211 if (D.CCCIsCXX()) {
213 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
214 CmdArgs.push_back("-lm");
215 }
216 CmdArgs.push_back("--start-group");
217 CmdArgs.push_back("-lc");
218 CmdArgs.push_back("-lgloss");
219 CmdArgs.push_back("--end-group");
220 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
221 }
222
223 if (WantCRTs)
224 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
225
226 CmdArgs.push_back("-o");
227 CmdArgs.push_back(Output.getFilename());
228 C.addCommand(std::make_unique<Command>(
229 JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
230 CmdArgs, Inputs, Output));
231}
232// RISCV tools end.
static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, const Multilib &Multilib, StringRef InstallPath, ToolChain::path_list &Paths)
const Decl * D
IndirectLocalPath & Path
static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, const Multilib &Multilib, StringRef InstallPath, ToolChain::path_list &Paths)
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
std::string SysRoot
sysroot, if present
Definition: Driver.h:180
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
See also MultilibSetBuilder for combining multilibs into a set.
Definition: Multilib.h:92
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:157
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:32
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:69
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
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:1209
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1121
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:737
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1311
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:899
path_list & getFilePaths()
Definition: ToolChain.h:294
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:268
const Driver & getDriver() const
Definition: ToolChain.h:252
path_list & getProgramPaths()
Definition: ToolChain.h:297
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:1317
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:1336
llvm::SmallVector< Multilib > SelectedMultilibs
Definition: ToolChain.h:201
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:2219
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:246
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3325
UnwindTableLevel getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override
How detailed should the unwind tables be by default.
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.
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const override
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
static bool hasGCCToolchain(const Driver &D, const llvm::opt::ArgList &Args)
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
RISCVToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
RISC-V Toolchain.
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,...
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:341
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92
Struct to store and manipulate GCC versions.
Definition: Gnu.h:162
std::string Text
The unparsed text of the version.
Definition: Gnu.h:164