Coverage Report

Created: 2021-03-06 07:03

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