Coverage Report

Created: 2020-09-19 12:23

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