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 "clang/Driver/CommonArgs.h"
11#include "clang/Driver/Compilation.h"
12#include "clang/Driver/InputInfo.h"
13#include "clang/Options/Options.h"
14#include "llvm/Option/ArgList.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
17
18using namespace clang::driver;
19using namespace clang::driver::toolchains;
20using namespace clang::driver::tools;
21using namespace clang;
22using namespace llvm::opt;
23
24static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
25 const Multilib &Multilib,
26 StringRef InstallPath,
27 ToolChain::path_list &Paths) {
28 if (const auto &PathsCallback = Multilibs.filePathsCallback())
29 for (const auto &Path : PathsCallback(Multilib))
30 addPathIfExists(D, Path: InstallPath + Path, Paths);
31}
32
33/// CSKY Toolchain
34CSKYToolChain::CSKYToolChain(const Driver &D, const llvm::Triple &Triple,
35 const ArgList &Args)
36 : Generic_ELF(D, Triple, Args) {
37 GCCInstallation.init(TargetTriple: Triple, Args);
38 if (GCCInstallation.isValid()) {
39 Multilibs = GCCInstallation.getMultilibs();
40 SelectedMultilibs.assign(IL: {GCCInstallation.getMultilib()});
41 path_list &Paths = getFilePaths();
42 // Add toolchain/multilib specific file paths.
43 addMultilibsFilePaths(D, Multilibs, Multilib: SelectedMultilibs.back(),
44 InstallPath: GCCInstallation.getInstallPath(), Paths);
45 getFilePaths().push_back(Elt: GCCInstallation.getInstallPath().str() +
46 SelectedMultilibs.back().osSuffix());
47 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 PPaths.push_back(Elt: Twine(GCCInstallation.getParentLibPath() + "/../" +
51 GCCInstallation.getTriple().str() + "/bin")
52 .str());
53 PPaths.push_back(Elt: (GCCInstallation.getParentLibPath() + "/../bin").str());
54 getFilePaths().push_back(Elt: computeSysRoot() + "/lib" +
55 SelectedMultilibs.back().osSuffix());
56 } else {
57 getProgramPaths().push_back(Elt: D.Dir);
58 getFilePaths().push_back(Elt: computeSysRoot() + "/lib");
59 }
60}
61
62Tool *CSKYToolChain::buildLinker() const {
63 return new tools::CSKY::Linker(*this);
64}
65
66ToolChain::RuntimeLibType CSKYToolChain::GetDefaultRuntimeLibType() const {
67 return GCCInstallation.isValid() ? ToolChain::RLT_Libgcc
68 : ToolChain::RLT_CompilerRT;
69}
70
71ToolChain::UnwindLibType
72CSKYToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
73 return ToolChain::UNW_None;
74}
75
76void CSKYToolChain::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
77 llvm::opt::ArgStringList &CC1Args,
78 BoundArch BA,
79 Action::OffloadKind) const {
80 CC1Args.push_back(Elt: "-nostdsysteminc");
81}
82
83void CSKYToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
84 ArgStringList &CC1Args) const {
85 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc))
86 return;
87
88 if (!DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) {
89 SmallString<128> Dir(computeSysRoot());
90 llvm::sys::path::append(path&: Dir, a: "include");
91 addSystemInclude(DriverArgs, CC1Args, Path: Dir.str());
92 SmallString<128> Dir2(computeSysRoot());
93 llvm::sys::path::append(path&: Dir2, a: "sys-include");
94 addSystemInclude(DriverArgs, CC1Args, Path: Dir2.str());
95 }
96}
97
98void CSKYToolChain::addLibStdCxxIncludePaths(
99 const llvm::opt::ArgList &DriverArgs,
100 llvm::opt::ArgStringList &CC1Args) const {
101 const GCCVersion &Version = GCCInstallation.getVersion();
102 StringRef TripleStr = GCCInstallation.getTriple().str();
103 const Multilib &Multilib = GCCInstallation.getMultilib();
104 addLibStdCXXIncludePaths(IncludeDir: computeSysRoot() + "/include/c++/" + Version.Text,
105 Triple: TripleStr, IncludeSuffix: Multilib.includeSuffix(), DriverArgs,
106 CC1Args);
107}
108
109std::string CSKYToolChain::computeSysRoot() const {
110 if (!getDriver().SysRoot.empty())
111 return getDriver().SysRoot;
112
113 SmallString<128> SysRootDir;
114 if (GCCInstallation.isValid()) {
115 StringRef LibDir = GCCInstallation.getParentLibPath();
116 StringRef TripleStr = GCCInstallation.getTriple().str();
117 llvm::sys::path::append(path&: SysRootDir, a: LibDir, b: "..", c: TripleStr);
118 } 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 llvm::sys::path::append(path&: SysRootDir, a: getDriver().Dir, b: "..",
122 c: getDriver().getTargetTriple());
123 }
124
125 if (!llvm::sys::fs::exists(Path: SysRootDir))
126 return std::string();
127
128 return std::string(SysRootDir);
129}
130
131void CSKY::Linker::ConstructJob(Compilation &C, const JobAction &JA,
132 const InputInfo &Output,
133 const InputInfoList &Inputs,
134 const ArgList &Args,
135 const char *LinkingOutput) const {
136 const ToolChain &ToolChain = getToolChain();
137 const Driver &D = ToolChain.getDriver();
138 ArgStringList CmdArgs;
139
140 if (!D.SysRoot.empty())
141 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--sysroot=" + D.SysRoot));
142
143 CmdArgs.push_back(Elt: "-m");
144 CmdArgs.push_back(Elt: "cskyelf");
145
146 std::string Linker = getToolChain().GetLinkerPath();
147
148 bool WantCRTs =
149 !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles);
150
151 const char *crtbegin, *crtend;
152 auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
153 if (RuntimeLib == ToolChain::RLT_Libgcc) {
154 crtbegin = "crtbegin.o";
155 crtend = "crtend.o";
156 } else {
157 assert(RuntimeLib == ToolChain::RLT_CompilerRT);
158 crtbegin = ToolChain.getCompilerRTArgString(Args, Component: "crtbegin",
159 Type: ToolChain::FT_Object);
160 crtend =
161 ToolChain.getCompilerRTArgString(Args, Component: "crtend", Type: ToolChain::FT_Object);
162 }
163
164 if (WantCRTs) {
165 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crt0.o")));
166 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crti.o")));
167 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: crtbegin)));
168 }
169
170 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L);
171 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
172 Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_T_Group, options::OPT_s,
173 options::OPT_t, options::OPT_r});
174
175 AddLinkerInputs(TC: ToolChain, Inputs, Args, CmdArgs, JA);
176
177 // TODO: add C++ includes and libs if compiling C++.
178
179 if (!Args.hasArg(Ids: options::OPT_nostdlib) &&
180 !Args.hasArg(Ids: options::OPT_nodefaultlibs)) {
181 if (ToolChain.ShouldLinkCXXStdlib(Args))
182 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
183 CmdArgs.push_back(Elt: "--start-group");
184 CmdArgs.push_back(Elt: "-lc");
185 if (Args.hasArg(Ids: options::OPT_msim))
186 CmdArgs.push_back(Elt: "-lsemi");
187 else
188 CmdArgs.push_back(Elt: "-lnosys");
189 CmdArgs.push_back(Elt: "--end-group");
190 AddRunTimeLibs(TC: ToolChain, D: ToolChain.getDriver(), CmdArgs, Args);
191 }
192
193 if (WantCRTs) {
194 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: crtend)));
195 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crtn.o")));
196 }
197
198 CmdArgs.push_back(Elt: "-o");
199 CmdArgs.push_back(Elt: Output.getFilename());
200 C.addCommand(Cmd: std::make_unique<Command>(
201 args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args: Args.MakeArgString(Str: Linker),
202 args&: CmdArgs, args: Inputs, args: Output));
203}
204// CSKY tools end.
205