clang  10.0.0svn
RISCVToolchain.cpp
Go to the documentation of this file.
1 //===--- RISCVToolchain.cpp - RISCV 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 "RISCVToolchain.h"
10 #include "CommonArgs.h"
11 #include "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 /// RISCV Toolchain
26 RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
27  const ArgList &Args)
28  : Generic_ELF(D, Triple, Args) {
29  GCCInstallation.init(Triple, Args);
30  getFilePaths().push_back(computeSysRoot() + "/lib");
31  if (GCCInstallation.isValid()) {
32  getFilePaths().push_back(GCCInstallation.getInstallPath().str());
33  getProgramPaths().push_back(
34  (GCCInstallation.getParentLibPath() + "/../bin").str());
35  }
36 }
37 
39  return new tools::RISCV::Linker(*this);
40 }
41 
43  const llvm::opt::ArgList &DriverArgs,
44  llvm::opt::ArgStringList &CC1Args,
45  Action::OffloadKind) const {
46  CC1Args.push_back("-nostdsysteminc");
47  CC1Args.push_back("-fuse-init-array");
48 }
49 
50 void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
51  ArgStringList &CC1Args) const {
52  if (DriverArgs.hasArg(options::OPT_nostdinc))
53  return;
54 
55  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
56  SmallString<128> Dir(computeSysRoot());
57  llvm::sys::path::append(Dir, "include");
58  addSystemInclude(DriverArgs, CC1Args, Dir.str());
59  }
60 }
61 
63  const llvm::opt::ArgList &DriverArgs,
64  llvm::opt::ArgStringList &CC1Args) const {
65  const GCCVersion &Version = GCCInstallation.getVersion();
66  StringRef TripleStr = GCCInstallation.getTriple().str();
68  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
69  "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
70 }
71 
72 std::string RISCVToolChain::computeSysRoot() const {
73  if (!getDriver().SysRoot.empty())
74  return getDriver().SysRoot;
75 
76  if (!GCCInstallation.isValid())
77  return std::string();
78 
79  StringRef LibDir = GCCInstallation.getParentLibPath();
80  StringRef TripleStr = GCCInstallation.getTriple().str();
81  std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str();
82 
83  if (!llvm::sys::fs::exists(SysRootDir))
84  return std::string();
85 
86  return SysRootDir;
87 }
88 
89 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
90  const InputInfo &Output,
91  const InputInfoList &Inputs,
92  const ArgList &Args,
93  const char *LinkingOutput) const {
94  const ToolChain &ToolChain = getToolChain();
95  const Driver &D = ToolChain.getDriver();
96  ArgStringList CmdArgs;
97 
98  if (!D.SysRoot.empty())
99  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
100 
101  std::string Linker = getToolChain().GetProgramPath(getShortName());
102 
103  bool WantCRTs =
104  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
105 
106  if (WantCRTs) {
107  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
108  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
109  }
110 
111  Args.AddAllArgs(CmdArgs, options::OPT_L);
112  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
113  Args.AddAllArgs(CmdArgs,
114  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
115  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
116 
117  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
118 
119  // TODO: add C++ includes and libs if compiling C++.
120 
121  if (!Args.hasArg(options::OPT_nostdlib) &&
122  !Args.hasArg(options::OPT_nodefaultlibs)) {
123  if (ToolChain.ShouldLinkCXXStdlib(Args))
124  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
125  CmdArgs.push_back("--start-group");
126  CmdArgs.push_back("-lc");
127  CmdArgs.push_back("-lgloss");
128  CmdArgs.push_back("--end-group");
129  CmdArgs.push_back("-lgcc");
130  }
131 
132  if (WantCRTs)
133  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
134 
135  CmdArgs.push_back("-o");
136  CmdArgs.push_back(Output.getFilename());
137  C.addCommand(std::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
138  CmdArgs, Inputs));
139 }
140 // RISCV tools end.
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:219
Struct to store and manipulate GCC versions.
Definition: Gnu.h:156
const char * getFilename() const
Definition: InputInfo.h:83
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:279
path_list & getProgramPaths()
Definition: ToolChain.h:234
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:67
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:484
path_list & getFilePaths()
Definition: ToolChain.h:231
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:238
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:28
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:868
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.
const Driver & getDriver() const
Definition: ToolChain.h:193
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:782
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:225
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:845
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:147
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
std::string Text
The unparsed text of the version.
Definition: Gnu.h:158
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
RISCVToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
RISCV Toolchain.
bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple, StringRef TargetMultiarchTriple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Helper to add the variant paths of a libstdc++ installation.
Definition: Gnu.cpp:2637
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1761
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:222
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:228
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:851
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:216