Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
Line
Count
Source (jump to first uncovered line)
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"
11
#include "clang/Driver/Compilation.h"
12
#include "clang/Driver/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
0
                                  ToolChain::path_list &Paths) {
29
0
  if (const auto &PathsCallback = Multilibs.filePathsCallback())
30
0
    for (const auto &Path : PathsCallback(Multilib))
31
0
      addPathIfExists(D, InstallPath + Path, Paths);
32
0
}
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.
38
bool RISCVToolChain::hasGCCToolchain(const Driver &D,
39
0
                                     const llvm::opt::ArgList &Args) {
40
0
  if (Args.getLastArg(options::OPT_gcc_toolchain))
41
0
    return true;
42
43
0
  SmallString<128> GCCDir;
44
0
  llvm::sys::path::append(GCCDir, D.Dir, "..", D.getTargetTriple(),
45
0
                          "lib/crt0.o");
46
0
  return llvm::sys::fs::exists(GCCDir);
47
0
}
48
49
/// RISC-V Toolchain
50
RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
51
                               const ArgList &Args)
52
0
    : Generic_ELF(D, Triple, Args) {
53
0
  GCCInstallation.init(Triple, Args);
54
0
  if (GCCInstallation.isValid()) {
55
0
    Multilibs = GCCInstallation.getMultilibs();
56
0
    SelectedMultilibs.assign({GCCInstallation.getMultilib()});
57
0
    path_list &Paths = getFilePaths();
58
    // Add toolchain/multilib specific file paths.
59
0
    addMultilibsFilePaths(D, Multilibs, SelectedMultilibs.back(),
60
0
                          GCCInstallation.getInstallPath(), Paths);
61
0
    getFilePaths().push_back(GCCInstallation.getInstallPath().str());
62
0
    ToolChain::path_list &PPaths = getProgramPaths();
63
    // Multilib cross-compiler GCC installations put ld in a triple-prefixed
64
    // directory off of the parent of the GCC installation.
65
0
    PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
66
0
                           GCCInstallation.getTriple().str() + "/bin")
67
0
                         .str());
68
0
    PPaths.push_back((GCCInstallation.getParentLibPath() + "/../bin").str());
69
0
  } else {
70
0
    getProgramPaths().push_back(D.Dir);
71
0
  }
72
0
  getFilePaths().push_back(computeSysRoot() + "/lib");
73
0
}
74
75
0
Tool *RISCVToolChain::buildLinker() const {
76
0
  return new tools::RISCV::Linker(*this);
77
0
}
78
79
0
ToolChain::RuntimeLibType RISCVToolChain::GetDefaultRuntimeLibType() const {
80
0
  return GCCInstallation.isValid() ?
81
0
    ToolChain::RLT_Libgcc : ToolChain::RLT_CompilerRT;
82
0
}
83
84
ToolChain::UnwindLibType
85
0
RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
86
0
  return ToolChain::UNW_None;
87
0
}
88
89
void RISCVToolChain::addClangTargetOptions(
90
    const llvm::opt::ArgList &DriverArgs,
91
    llvm::opt::ArgStringList &CC1Args,
92
0
    Action::OffloadKind) const {
93
0
  CC1Args.push_back("-nostdsysteminc");
94
0
}
95
96
void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
97
0
                                               ArgStringList &CC1Args) const {
98
0
  if (DriverArgs.hasArg(options::OPT_nostdinc))
99
0
    return;
100
101
0
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
102
0
    SmallString<128> Dir(getDriver().ResourceDir);
103
0
    llvm::sys::path::append(Dir, "include");
104
0
    addSystemInclude(DriverArgs, CC1Args, Dir.str());
105
0
  }
106
107
0
  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
108
0
    SmallString<128> Dir(computeSysRoot());
109
0
    llvm::sys::path::append(Dir, "include");
110
0
    addSystemInclude(DriverArgs, CC1Args, Dir.str());
111
0
  }
112
0
}
113
114
void RISCVToolChain::addLibStdCxxIncludePaths(
115
    const llvm::opt::ArgList &DriverArgs,
116
0
    llvm::opt::ArgStringList &CC1Args) const {
117
0
  const GCCVersion &Version = GCCInstallation.getVersion();
118
0
  StringRef TripleStr = GCCInstallation.getTriple().str();
119
0
  const Multilib &Multilib = GCCInstallation.getMultilib();
120
0
  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
121
0
                           TripleStr, Multilib.includeSuffix(), DriverArgs,
122
0
                           CC1Args);
123
0
}
124
125
0
std::string RISCVToolChain::computeSysRoot() const {
126
0
  if (!getDriver().SysRoot.empty())
127
0
    return getDriver().SysRoot;
128
129
0
  SmallString<128> SysRootDir;
130
0
  if (GCCInstallation.isValid()) {
131
0
    StringRef LibDir = GCCInstallation.getParentLibPath();
132
0
    StringRef TripleStr = GCCInstallation.getTriple().str();
133
0
    llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
134
0
  } else {
135
    // Use the triple as provided to the driver. Unlike the parsed triple
136
    // this has not been normalized to always contain every field.
137
0
    llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
138
0
                            getDriver().getTargetTriple());
139
0
  }
140
141
0
  if (!llvm::sys::fs::exists(SysRootDir))
142
0
    return std::string();
143
144
0
  return std::string(SysRootDir.str());
145
0
}
146
147
void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
148
                                 const InputInfo &Output,
149
                                 const InputInfoList &Inputs,
150
                                 const ArgList &Args,
151
0
                                 const char *LinkingOutput) const {
152
0
  const ToolChain &ToolChain = getToolChain();
153
0
  const Driver &D = ToolChain.getDriver();
154
0
  ArgStringList CmdArgs;
155
156
0
  if (!D.SysRoot.empty())
157
0
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
158
159
0
  bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
160
0
  CmdArgs.push_back("-m");
161
0
  if (IsRV64) {
162
0
    CmdArgs.push_back("elf64lriscv");
163
0
  } else {
164
0
    CmdArgs.push_back("elf32lriscv");
165
0
  }
166
0
  CmdArgs.push_back("-X");
167
168
0
  std::string Linker = getToolChain().GetLinkerPath();
169
170
0
  bool WantCRTs =
171
0
      !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
172
173
0
  const char *crtbegin, *crtend;
174
0
  auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
175
0
  if (RuntimeLib == ToolChain::RLT_Libgcc) {
176
0
    crtbegin = "crtbegin.o";
177
0
    crtend = "crtend.o";
178
0
  } else {
179
0
    assert (RuntimeLib == ToolChain::RLT_CompilerRT);
180
0
    crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
181
0
                                                ToolChain::FT_Object);
182
0
    crtend = ToolChain.getCompilerRTArgString(Args, "crtend",
183
0
                                              ToolChain::FT_Object);
184
0
  }
185
186
0
  if (WantCRTs) {
187
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
188
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
189
0
  }
190
191
0
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
192
193
0
  Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
194
195
0
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
196
0
  Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
197
0
                            options::OPT_t, options::OPT_r});
198
199
  // TODO: add C++ includes and libs if compiling C++.
200
201
0
  if (!Args.hasArg(options::OPT_nostdlib) &&
202
0
      !Args.hasArg(options::OPT_nodefaultlibs)) {
203
0
    if (D.CCCIsCXX()) {
204
0
      if (ToolChain.ShouldLinkCXXStdlib(Args))
205
0
        ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
206
0
      CmdArgs.push_back("-lm");
207
0
    }
208
0
    CmdArgs.push_back("--start-group");
209
0
    CmdArgs.push_back("-lc");
210
0
    CmdArgs.push_back("-lgloss");
211
0
    CmdArgs.push_back("--end-group");
212
0
    AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
213
0
  }
214
215
0
  if (WantCRTs)
216
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
217
218
0
  CmdArgs.push_back("-o");
219
0
  CmdArgs.push_back(Output.getFilename());
220
0
  C.addCommand(std::make_unique<Command>(
221
0
      JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
222
0
      CmdArgs, Inputs, Output));
223
0
}
224
// RISCV tools end.