clang 20.0.0git
CSKYToolChain.cpp
Go to the documentation of this file.
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"
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
19using namespace clang::driver;
20using namespace clang::driver::toolchains;
21using namespace clang::driver::tools;
22using namespace clang;
23using namespace llvm::opt;
24
25static 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
35CSKYToolChain::CSKYToolChain(const Driver &D, const llvm::Triple &Triple,
36 const ArgList &Args)
37 : Generic_ELF(D, Triple, Args) {
38 GCCInstallation.init(Triple, Args);
42 path_list &Paths = getFilePaths();
43 // Add toolchain/multilib specific file paths.
46 getFilePaths().push_back(GCCInstallation.getInstallPath().str() +
47 SelectedMultilibs.back().osSuffix());
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
64 return new tools::CSKY::Linker(*this);
65}
66
70}
71
73CSKYToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
75}
76
77void CSKYToolChain::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
78 llvm::opt::ArgStringList &CC1Args,
79 Action::OffloadKind) const {
80 CC1Args.push_back("-nostdsysteminc");
81}
82
83void 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
99 const llvm::opt::ArgList &DriverArgs,
100 llvm::opt::ArgStringList &CC1Args) const {
101 const GCCVersion &Version = GCCInstallation.getVersion();
102 StringRef TripleStr = GCCInstallation.getTriple().str();
104 addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
105 TripleStr, 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(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);
129}
130
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",
160 crtend =
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, {options::OPT_T_Group, options::OPT_s,
173 options::OPT_t, options::OPT_r});
174
175 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
176
177 // TODO: add C++ includes and libs if compiling C++.
178
179 if (!Args.hasArg(options::OPT_nostdlib) &&
180 !Args.hasArg(options::OPT_nodefaultlibs)) {
182 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
183 CmdArgs.push_back("--start-group");
184 CmdArgs.push_back("-lc");
185 if (Args.hasArg(options::OPT_msim))
186 CmdArgs.push_back("-lsemi");
187 else
188 CmdArgs.push_back("-lnosys");
189 CmdArgs.push_back("--end-group");
190 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
191 }
192
193 if (WantCRTs) {
194 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
195 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
196 }
197
198 CmdArgs.push_back("-o");
199 CmdArgs.push_back(Output.getFilename());
200 C.addCommand(std::make_unique<Command>(
201 JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
202 CmdArgs, Inputs, Output));
203}
204// CSKY tools end.
static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, const Multilib &Multilib, StringRef InstallPath, ToolChain::path_list &Paths)
const Decl * D
IndirectLocalPath & Path
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
std::string SysRoot
sysroot, if present
Definition: Driver.h:180
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
See also MultilibSetBuilder for combining multilibs into a set.
Definition: Multilib.h:92
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:157
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:32
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:69
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1209
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1121
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:737
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1311
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:899
path_list & getFilePaths()
Definition: ToolChain.h:294
const Driver & getDriver() const
Definition: ToolChain.h:252
path_list & getProgramPaths()
Definition: ToolChain.h:297
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:1317
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:1336
llvm::SmallVector< Multilib > SelectedMultilibs
Definition: ToolChain.h:201
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const override
CSKYToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
CSKY Toolchain.
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind) const override
Add options that need to be passed to cc1 for this target.
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:2219
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:246
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3325
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:341
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92
Struct to store and manipulate GCC versions.
Definition: Gnu.h:162
std::string Text
The unparsed text of the version.
Definition: Gnu.h:164