Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
/// RISCV Toolchain
26
RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
27
                               const ArgList &Args)
28
131
    : Generic_ELF(D, Triple, Args) {
29
131
  GCCInstallation.init(Triple, Args);
30
131
  getFilePaths().push_back(computeSysRoot() + "/lib");
31
131
  if (GCCInstallation.isValid()) {
32
8
    getFilePaths().push_back(GCCInstallation.getInstallPath().str());
33
8
    getProgramPaths().push_back(
34
8
        (GCCInstallation.getParentLibPath() + "/../bin").str());
35
8
  }
36
131
}
37
38
29
Tool *RISCVToolChain::buildLinker() const {
39
29
  return new tools::RISCV::Linker(*this);
40
29
}
41
42
void RISCVToolChain::addClangTargetOptions(
43
    const llvm::opt::ArgList &DriverArgs,
44
    llvm::opt::ArgStringList &CC1Args,
45
127
    Action::OffloadKind) const {
46
127
  CC1Args.push_back("-nostdsysteminc");
47
127
  CC1Args.push_back("-fuse-init-array");
48
127
}
49
50
void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
51
127
                                               ArgStringList &CC1Args) const {
52
127
  if (DriverArgs.hasArg(options::OPT_nostdinc))
53
0
    return;
54
127
55
127
  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
56
127
    SmallString<128> Dir(computeSysRoot());
57
127
    llvm::sys::path::append(Dir, "include");
58
127
    addSystemInclude(DriverArgs, CC1Args, Dir.str());
59
127
  }
60
127
}
61
62
void RISCVToolChain::addLibStdCxxIncludePaths(
63
    const llvm::opt::ArgList &DriverArgs,
64
4
    llvm::opt::ArgStringList &CC1Args) const {
65
4
  const GCCVersion &Version = GCCInstallation.getVersion();
66
4
  StringRef TripleStr = GCCInstallation.getTriple().str();
67
4
  const Multilib &Multilib = GCCInstallation.getMultilib();
68
4
  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
69
4
      "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
70
4
}
71
72
262
std::string RISCVToolChain::computeSysRoot() const {
73
262
  if (!getDriver().SysRoot.empty())
74
10
    return getDriver().SysRoot;
75
252
76
252
  if (!GCCInstallation.isValid())
77
242
    return std::string();
78
10
79
10
  StringRef LibDir = GCCInstallation.getParentLibPath();
80
10
  StringRef TripleStr = GCCInstallation.getTriple().str();
81
10
  std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str();
82
10
83
10
  if (!llvm::sys::fs::exists(SysRootDir))
84
0
    return std::string();
85
10
86
10
  return SysRootDir;
87
10
}
88
89
void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
90
                                 const InputInfo &Output,
91
                                 const InputInfoList &Inputs,
92
                                 const ArgList &Args,
93
29
                                 const char *LinkingOutput) const {
94
29
  const ToolChain &ToolChain = getToolChain();
95
29
  const Driver &D = ToolChain.getDriver();
96
29
  ArgStringList CmdArgs;
97
29
98
29
  if (!D.SysRoot.empty())
99
4
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
100
29
101
29
  std::string Linker = getToolChain().GetProgramPath(getShortName());
102
29
103
29
  bool WantCRTs =
104
29
      !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
105
29
106
29
  if (WantCRTs) {
107
29
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
108
29
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
109
29
  }
110
29
111
29
  Args.AddAllArgs(CmdArgs, options::OPT_L);
112
29
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
113
29
  Args.AddAllArgs(CmdArgs,
114
29
                  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
115
29
                   options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
116
29
117
29
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
118
29
119
29
  // TODO: add C++ includes and libs if compiling C++.
120
29
121
29
  if (!Args.hasArg(options::OPT_nostdlib) &&
122
29
      !Args.hasArg(options::OPT_nodefaultlibs)) {
123
29
    if (ToolChain.ShouldLinkCXXStdlib(Args))
124
4
      ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
125
29
    CmdArgs.push_back("--start-group");
126
29
    CmdArgs.push_back("-lc");
127
29
    CmdArgs.push_back("-lgloss");
128
29
    CmdArgs.push_back("--end-group");
129
29
    CmdArgs.push_back("-lgcc");
130
29
  }
131
29
132
29
  if (WantCRTs)
133
29
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
134
29
135
29
  CmdArgs.push_back("-o");
136
29
  CmdArgs.push_back(Output.getFilename());
137
29
  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
138
29
                                          CmdArgs, Inputs));
139
29
}
140
// RISCV tools end.