Coverage Report

Created: 2020-02-25 14:32

/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
214
    : Generic_ELF(D, Triple, Args) {
38
214
  GCCInstallation.init(Triple, Args);
39
214
  if (GCCInstallation.isValid()) {
40
15
    Multilibs = GCCInstallation.getMultilibs();
41
15
    SelectedMultilib = GCCInstallation.getMultilib();
42
15
    path_list &Paths = getFilePaths();
43
15
    // 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
15
    // Multilib cross-compiler GCC installations put ld in a triple-prefixed
49
15
    // 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
199
  } else {
55
199
    getProgramPaths().push_back(D.Dir);
56
199
  }
57
214
  getFilePaths().push_back(computeSysRoot() + "/lib");
58
214
}
59
60
108
Tool *RISCVToolChain::buildLinker() const {
61
108
  return new tools::RISCV::Linker(*this);
62
108
}
63
64
208
ToolChain::RuntimeLibType RISCVToolChain::GetDefaultRuntimeLibType() const {
65
208
  return GCCInstallation.isValid() ?
66
178
    
ToolChain::RLT_Libgcc30
: ToolChain::RLT_CompilerRT;
67
208
}
68
69
ToolChain::UnwindLibType
70
108
RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
71
108
  return ToolChain::UNW_None;
72
108
}
73
74
void RISCVToolChain::addClangTargetOptions(
75
    const llvm::opt::ArgList &DriverArgs,
76
    llvm::opt::ArgStringList &CC1Args,
77
210
    Action::OffloadKind) const {
78
210
  CC1Args.push_back("-nostdsysteminc");
79
210
}
80
81
void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
82
210
                                               ArgStringList &CC1Args) const {
83
210
  if (DriverArgs.hasArg(options::OPT_nostdinc))
84
0
    return;
85
210
86
210
  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
87
210
    SmallString<128> Dir(computeSysRoot());
88
210
    llvm::sys::path::append(Dir, "include");
89
210
    addSystemInclude(DriverArgs, CC1Args, Dir.str());
90
210
  }
91
210
}
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
428
std::string RISCVToolChain::computeSysRoot() const {
104
428
  if (!getDriver().SysRoot.empty())
105
10
    return getDriver().SysRoot;
106
418
107
418
  SmallString<128> SysRootDir;
108
418
  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
394
  } else {
113
394
    // Use the triple as provided to the driver. Unlike the parsed triple
114
394
    // this has not been normalized to always contain every field.
115
394
    llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
116
394
                            getDriver().getTargetTriple());
117
394
  }
118
418
119
418
  if (!llvm::sys::fs::exists(SysRootDir))
120
390
    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
108
                                 const char *LinkingOutput) const {
130
108
  const ToolChain &ToolChain = getToolChain();
131
108
  const Driver &D = ToolChain.getDriver();
132
108
  ArgStringList CmdArgs;
133
108
134
108
  if (!D.SysRoot.empty())
135
4
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
136
108
137
108
  bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
138
108
  CmdArgs.push_back("-m");
139
108
  if (IsRV64) {
140
50
    CmdArgs.push_back("elf64lriscv");
141
58
  } else {
142
58
    CmdArgs.push_back("elf32lriscv");
143
58
  }
144
108
145
108
  std::string Linker = getToolChain().GetProgramPath(getShortName());
146
108
147
108
  bool WantCRTs =
148
108
      !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
149
108
150
108
  const char *crtbegin, *crtend;
151
108
  auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
152
108
  if (RuntimeLib == ToolChain::RLT_Libgcc) {
153
17
    crtbegin = "crtbegin.o";
154
17
    crtend = "crtend.o";
155
91
  } else {
156
91
    assert (RuntimeLib == ToolChain::RLT_CompilerRT);
157
91
    crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
158
91
                                                ToolChain::FT_Object);
159
91
    crtend = ToolChain.getCompilerRTArgString(Args, "crtend",
160
91
                                              ToolChain::FT_Object);
161
91
  }
162
108
163
108
  if (WantCRTs) {
164
108
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
165
108
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
166
108
  }
167
108
168
108
  Args.AddAllArgs(CmdArgs, options::OPT_L);
169
108
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
170
108
  Args.AddAllArgs(CmdArgs,
171
108
                  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
172
108
                   options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
173
108
174
108
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
175
108
176
108
  // TODO: add C++ includes and libs if compiling C++.
177
108
178
108
  if (!Args.hasArg(options::OPT_nostdlib) &&
179
108
      !Args.hasArg(options::OPT_nodefaultlibs)) {
180
108
    if (ToolChain.ShouldLinkCXXStdlib(Args))
181
4
      ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
182
108
    CmdArgs.push_back("--start-group");
183
108
    CmdArgs.push_back("-lc");
184
108
    CmdArgs.push_back("-lgloss");
185
108
    CmdArgs.push_back("--end-group");
186
108
    AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
187
108
  }
188
108
189
108
  if (WantCRTs)
190
108
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
191
108
192
108
  CmdArgs.push_back("-o");
193
108
  CmdArgs.push_back(Output.getFilename());
194
108
  C.addCommand(std::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
195
108
                                          CmdArgs, Inputs));
196
108
}
197
// RISCV tools end.