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