xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/RISCVToolchain.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
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"
13 #include "clang/Driver/Compilation.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 
39 Tool *RISCVToolChain::buildLinker() const {
40   return new tools::RISCV::Linker(*this);
41 }
42 
43 void RISCVToolChain::addClangTargetOptions(
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 
63 void RISCVToolChain::addLibStdCxxIncludePaths(
64     const llvm::opt::ArgList &DriverArgs,
65     llvm::opt::ArgStringList &CC1Args) const {
66   const GCCVersion &Version = GCCInstallation.getVersion();
67   StringRef TripleStr = GCCInstallation.getTriple().str();
68   const Multilib &Multilib = GCCInstallation.getMultilib();
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.
148