Coverage Report

Created: 2023-09-30 09:22

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