/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/RISCV.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- RISCV.cpp - RISCV ToolChain Implementations ------------*- C++ -*-===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | |
10 | | #include "RISCV.h" |
11 | | #include "CommonArgs.h" |
12 | | #include "InputInfo.h" |
13 | | #include "clang/Driver/Compilation.h" |
14 | | #include "clang/Driver/Options.h" |
15 | | #include "llvm/Option/ArgList.h" |
16 | | #include "llvm/Support/FileSystem.h" |
17 | | #include "llvm/Support/Path.h" |
18 | | #include "llvm/Support/raw_ostream.h" |
19 | | |
20 | | using namespace clang::driver; |
21 | | using namespace clang::driver::toolchains; |
22 | | using namespace clang::driver::tools; |
23 | | using namespace clang; |
24 | | using namespace llvm::opt; |
25 | | |
26 | | /// RISCV Toolchain |
27 | | RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple, |
28 | | const ArgList &Args) |
29 | 121 | : Generic_ELF(D, Triple, Args) { |
30 | 121 | GCCInstallation.init(Triple, Args); |
31 | 121 | getFilePaths().push_back(computeSysRoot() + "/lib"); |
32 | 121 | if (GCCInstallation.isValid()) { |
33 | 4 | getFilePaths().push_back(GCCInstallation.getInstallPath().str()); |
34 | 4 | getProgramPaths().push_back( |
35 | 4 | (GCCInstallation.getParentLibPath() + "/../bin").str()); |
36 | 4 | } |
37 | 121 | } |
38 | | |
39 | 19 | Tool *RISCVToolChain::buildLinker() const { |
40 | 19 | return new tools::RISCV::Linker(*this); |
41 | 19 | } |
42 | | |
43 | | void RISCVToolChain::addClangTargetOptions( |
44 | | const llvm::opt::ArgList &DriverArgs, |
45 | | llvm::opt::ArgStringList &CC1Args, |
46 | 121 | Action::OffloadKind) const { |
47 | 121 | CC1Args.push_back("-nostdsysteminc"); |
48 | 121 | CC1Args.push_back("-fuse-init-array"); |
49 | 121 | } |
50 | | |
51 | | void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
52 | 121 | ArgStringList &CC1Args) const { |
53 | 121 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
54 | 0 | return; |
55 | 121 | |
56 | 121 | if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) { |
57 | 121 | SmallString<128> Dir(computeSysRoot()); |
58 | 121 | llvm::sys::path::append(Dir, "include"); |
59 | 121 | addSystemInclude(DriverArgs, CC1Args, Dir.str()); |
60 | 121 | } |
61 | 121 | } |
62 | | |
63 | | void RISCVToolChain::addLibStdCxxIncludePaths( |
64 | | const llvm::opt::ArgList &DriverArgs, |
65 | 2 | llvm::opt::ArgStringList &CC1Args) const { |
66 | 2 | const GCCVersion &Version = GCCInstallation.getVersion(); |
67 | 2 | StringRef TripleStr = GCCInstallation.getTriple().str(); |
68 | 2 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
69 | 2 | addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text, |
70 | 2 | "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args); |
71 | 2 | } |
72 | | |
73 | 244 | std::string RISCVToolChain::computeSysRoot() const { |
74 | 244 | if (!getDriver().SysRoot.empty()) |
75 | 5 | return getDriver().SysRoot; |
76 | 239 | |
77 | 239 | if (!GCCInstallation.isValid()) |
78 | 234 | return std::string(); |
79 | 5 | |
80 | 5 | StringRef LibDir = GCCInstallation.getParentLibPath(); |
81 | 5 | StringRef TripleStr = GCCInstallation.getTriple().str(); |
82 | 5 | std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str(); |
83 | 5 | |
84 | 5 | if (!llvm::sys::fs::exists(SysRootDir)) |
85 | 0 | return std::string(); |
86 | 5 | |
87 | 5 | return SysRootDir; |
88 | 5 | } |
89 | | |
90 | | void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
91 | | const InputInfo &Output, |
92 | | const InputInfoList &Inputs, |
93 | | const ArgList &Args, |
94 | 19 | const char *LinkingOutput) const { |
95 | 19 | const ToolChain &ToolChain = getToolChain(); |
96 | 19 | const Driver &D = ToolChain.getDriver(); |
97 | 19 | ArgStringList CmdArgs; |
98 | 19 | |
99 | 19 | if (!D.SysRoot.empty()) |
100 | 2 | CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); |
101 | 19 | |
102 | 19 | std::string Linker = getToolChain().GetProgramPath(getShortName()); |
103 | 19 | |
104 | 19 | bool WantCRTs = |
105 | 19 | !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles); |
106 | 19 | |
107 | 19 | if (WantCRTs) { |
108 | 19 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o"))); |
109 | 19 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o"))); |
110 | 19 | } |
111 | 19 | |
112 | 19 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
113 | 19 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
114 | 19 | Args.AddAllArgs(CmdArgs, |
115 | 19 | {options::OPT_T_Group, options::OPT_e, options::OPT_s, |
116 | 19 | options::OPT_t, options::OPT_Z_Flag, options::OPT_r}); |
117 | 19 | |
118 | 19 | AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); |
119 | 19 | |
120 | 19 | // TODO: add C++ includes and libs if compiling C++. |
121 | 19 | |
122 | 19 | if (!Args.hasArg(options::OPT_nostdlib) && |
123 | 19 | !Args.hasArg(options::OPT_nodefaultlibs)) { |
124 | 19 | if (ToolChain.ShouldLinkCXXStdlib(Args)) |
125 | 2 | ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); |
126 | 19 | CmdArgs.push_back("--start-group"); |
127 | 19 | CmdArgs.push_back("-lc"); |
128 | 19 | CmdArgs.push_back("-lgloss"); |
129 | 19 | CmdArgs.push_back("--end-group"); |
130 | 19 | CmdArgs.push_back("-lgcc"); |
131 | 19 | } |
132 | 19 | |
133 | 19 | if (WantCRTs) |
134 | 19 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); |
135 | 19 | |
136 | 19 | CmdArgs.push_back("-o"); |
137 | 19 | CmdArgs.push_back(Output.getFilename()); |
138 | 19 | C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker), |
139 | 19 | CmdArgs, Inputs)); |
140 | 19 | } |
141 | | // RISCV tools end. |