xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/CSKYToolChain.cpp (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
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                                   ToolChain::path_list &Paths) {
29   if (const auto &PathsCallback = Multilibs.filePathsCallback())
30     for (const auto &Path : PathsCallback(Multilib))
31       addPathIfExists(D, InstallPath + Path, Paths);
32 }
33 
34 /// CSKY Toolchain
35 CSKYToolChain::CSKYToolChain(const Driver &D, const llvm::Triple &Triple,
36                              const ArgList &Args)
37     : Generic_ELF(D, Triple, Args) {
38   GCCInstallation.init(Triple, Args);
39   if (GCCInstallation.isValid()) {
40     Multilibs = GCCInstallation.getMultilibs();
41     SelectedMultilibs.assign({GCCInstallation.getMultilib()});
42     path_list &Paths = getFilePaths();
43     // Add toolchain/multilib specific file paths.
44     addMultilibsFilePaths(D, Multilibs, SelectedMultilibs.back(),
45                           GCCInstallation.getInstallPath(), Paths);
46     getFilePaths().push_back(GCCInstallation.getInstallPath().str() +
47                              SelectedMultilibs.back().osSuffix());
48     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     PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
52                            GCCInstallation.getTriple().str() + "/bin")
53                          .str());
54     PPaths.push_back((GCCInstallation.getParentLibPath() + "/../bin").str());
55     getFilePaths().push_back(computeSysRoot() + "/lib" +
56                              SelectedMultilibs.back().osSuffix());
57   } else {
58     getProgramPaths().push_back(D.Dir);
59     getFilePaths().push_back(computeSysRoot() + "/lib");
60   }
61 }
62 
63 Tool *CSKYToolChain::buildLinker() const {
64   return new tools::CSKY::Linker(*this);
65 }
66 
67 ToolChain::RuntimeLibType CSKYToolChain::GetDefaultRuntimeLibType() const {
68   return GCCInstallation.isValid() ? ToolChain::RLT_Libgcc
69                                    : ToolChain::RLT_CompilerRT;
70 }
71 
72 ToolChain::UnwindLibType
73 CSKYToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
74   return ToolChain::UNW_None;
75 }
76 
77 void CSKYToolChain::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
78                                           llvm::opt::ArgStringList &CC1Args,
79                                           Action::OffloadKind) const {
80   CC1Args.push_back("-nostdsysteminc");
81 }
82 
83 void CSKYToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
84                                               ArgStringList &CC1Args) const {
85   if (DriverArgs.hasArg(options::OPT_nostdinc))
86     return;
87 
88   if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
89     SmallString<128> Dir(computeSysRoot());
90     llvm::sys::path::append(Dir, "include");
91     addSystemInclude(DriverArgs, CC1Args, Dir.str());
92     SmallString<128> Dir2(computeSysRoot());
93     llvm::sys::path::append(Dir2, "sys-include");
94     addSystemInclude(DriverArgs, CC1Args, Dir2.str());
95   }
96 }
97 
98 void 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(computeSysRoot() + "/include/c++/" + Version.Text,
105                            TripleStr, Multilib.includeSuffix(), DriverArgs,
106                            CC1Args);
107 }
108 
109 std::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(SysRootDir, LibDir, "..", 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(SysRootDir, getDriver().Dir, "..",
122                             getDriver().getTargetTriple());
123   }
124 
125   if (!llvm::sys::fs::exists(SysRootDir))
126     return std::string();
127 
128   return std::string(SysRootDir.str());
129 }
130 
131 void 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(Args.MakeArgString("--sysroot=" + D.SysRoot));
142 
143   CmdArgs.push_back("-m");
144   CmdArgs.push_back("cskyelf");
145 
146   std::string Linker = getToolChain().GetLinkerPath();
147 
148   bool WantCRTs =
149       !Args.hasArg(options::OPT_nostdlib, 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, "crtbegin",
159                                                 ToolChain::FT_Object);
160     crtend =
161         ToolChain.getCompilerRTArgString(Args, "crtend", ToolChain::FT_Object);
162   }
163 
164   if (WantCRTs) {
165     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
166     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
167     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
168   }
169 
170   Args.AddAllArgs(CmdArgs, options::OPT_L);
171   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
172   Args.AddAllArgs(CmdArgs,
173                   {options::OPT_T_Group, options::OPT_s, options::OPT_t,
174                    options::OPT_Z_Flag, options::OPT_r});
175 
176   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
177 
178   // TODO: add C++ includes and libs if compiling C++.
179 
180   if (!Args.hasArg(options::OPT_nostdlib) &&
181       !Args.hasArg(options::OPT_nodefaultlibs)) {
182     if (ToolChain.ShouldLinkCXXStdlib(Args))
183       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
184     CmdArgs.push_back("--start-group");
185     CmdArgs.push_back("-lc");
186     if (Args.hasArg(options::OPT_msim))
187       CmdArgs.push_back("-lsemi");
188     else
189       CmdArgs.push_back("-lnosys");
190     CmdArgs.push_back("--end-group");
191     AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
192   }
193 
194   if (WantCRTs) {
195     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
196     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
197   }
198 
199   CmdArgs.push_back("-o");
200   CmdArgs.push_back(Output.getFilename());
201   C.addCommand(std::make_unique<Command>(
202       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
203       CmdArgs, Inputs, Output));
204 }
205 // CSKY tools end.
206