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 "Arch/RISCV.h"
11 #include "CommonArgs.h"
12 #include "InputInfo.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace clang::driver;
21 using namespace clang::driver::toolchains;
22 using namespace clang::driver::tools;
23 using namespace clang;
24 using namespace llvm::opt;
25 
26 /// RISCV Toolchain
27 RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
28  const ArgList &Args)
29  : Generic_ELF(D, Triple, Args) {
30  GCCInstallation.init(Triple, Args);
31  getFilePaths().push_back(computeSysRoot() + "/lib");
32  if (GCCInstallation.isValid()) {
33  getFilePaths().push_back(GCCInstallation.getInstallPath().str());
34  getProgramPaths().push_back(
35  (GCCInstallation.getParentLibPath() + "/../bin").str());
36  }
37 }
38 
40  return new tools::RISCV::Linker(*this);
41 }
42 
44  const llvm::opt::ArgList &DriverArgs,
45  llvm::opt::ArgStringList &CC1Args,
46  Action::OffloadKind) const {
47  CC1Args.push_back("-nostdsysteminc");
48  CC1Args.push_back("-fuse-init-array");
49 }
50 
51 void RISCVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
52  ArgStringList &CC1Args) const {
53  if (DriverArgs.hasArg(options::OPT_nostdinc))
54  return;
55 
56  if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
57  SmallString<128> Dir(computeSysRoot());
58  llvm::sys::path::append(Dir, "include");
59  addSystemInclude(DriverArgs, CC1Args, Dir.str());
60  }
61 }
62 
64  const llvm::opt::ArgList &DriverArgs,
65  llvm::opt::ArgStringList &CC1Args) const {
66  const GCCVersion &Version = GCCInstallation.getVersion();
67  StringRef TripleStr = GCCInstallation.getTriple().str();
69  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
70  "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
71 }
72 
73 std::string RISCVToolChain::computeSysRoot() const {
74  if (!getDriver().SysRoot.empty())
75  return getDriver().SysRoot;
76 
77  if (!GCCInstallation.isValid())
78  return std::string();
79 
80  StringRef LibDir = GCCInstallation.getParentLibPath();
81  StringRef TripleStr = GCCInstallation.getTriple().str();
82  std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str();
83 
84  if (!llvm::sys::fs::exists(SysRootDir))
85  return std::string();
86 
87  return SysRootDir;
88 }
89 
90 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
91  const InputInfo &Output,
92  const InputInfoList &Inputs,
93  const ArgList &Args,
94  const char *LinkingOutput) const {
95  const ToolChain &ToolChain = getToolChain();
96  const Driver &D = ToolChain.getDriver();
97  ArgStringList CmdArgs;
98 
99  if (!D.SysRoot.empty())
100  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
101 
102  std::string Linker = getToolChain().GetProgramPath(getShortName());
103 
104  if (D.isUsingLTO()) {
105  assert(!Inputs.empty() && "Must have at least one input.");
106  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
107  D.getLTOMode() == LTOK_Thin);
108  }
109 
110  bool WantCRTs =
111  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
112 
113  if (WantCRTs) {
114  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
115  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
116  }
117 
118  Args.AddAllArgs(CmdArgs, options::OPT_L);
119  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
120  Args.AddAllArgs(CmdArgs,
121  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
122  options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
123 
124  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
125 
126  // TODO: add C++ includes and libs if compiling C++.
127 
128  if (!Args.hasArg(options::OPT_nostdlib) &&
129  !Args.hasArg(options::OPT_nodefaultlibs)) {
130  if (ToolChain.ShouldLinkCXXStdlib(Args))
131  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
132  CmdArgs.push_back("--start-group");
133  CmdArgs.push_back("-lc");
134  CmdArgs.push_back("-lgloss");
135  CmdArgs.push_back("--end-group");
136  CmdArgs.push_back("-lgcc");
137  }
138 
139  if (WantCRTs)
140  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
141 
142  CmdArgs.push_back("-o");
143  CmdArgs.push_back(Output.getFilename());
144  C.addCommand(std::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
145  CmdArgs, Inputs));
146 }
147 // RISCV tools end.
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:219
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:538
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:541
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:238
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:67
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
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:503
path_list & getFilePaths()
Definition: ToolChain.h:235
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:887
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:197
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:801
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:864
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:2635
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:1759
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:870
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