clang  14.0.0git
MipsLinux.cpp
Go to the documentation of this file.
1 //===-- MipsLinux.cpp - Mips 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 "MipsLinux.h"
10 #include "Arch/Mips.h"
11 #include "CommonArgs.h"
12 #include "clang/Driver/Driver.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 
19 using namespace clang::driver;
20 using namespace clang::driver::toolchains;
21 using namespace clang;
22 using namespace llvm::opt;
23 
24 /// Mips Toolchain
26  const llvm::Triple &Triple,
27  const ArgList &Args)
28  : Linux(D, Triple, Args) {
29  // Select the correct multilib according to the given arguments.
30  DetectedMultilibs Result;
31  findMIPSMultilibs(D, Triple, "", Args, Result);
32  Multilibs = Result.Multilibs;
33  SelectedMultilib = Result.SelectedMultilib;
34 
35  // Find out the library suffix based on the ABI.
36  LibSuffix = tools::mips::getMipsABILibSuffix(Args, Triple);
37  getFilePaths().clear();
38  getFilePaths().push_back(computeSysRoot() + "/usr/lib" + LibSuffix);
39 }
40 
42  const ArgList &DriverArgs, ArgStringList &CC1Args) const {
43  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
44  return;
45 
46  const Driver &D = getDriver();
47 
48  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
50  llvm::sys::path::append(P, "include");
51  addSystemInclude(DriverArgs, CC1Args, P);
52  }
53 
54  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
55  return;
56 
57  const auto &Callback = Multilibs.includeDirsCallback();
58  if (Callback) {
59  for (const auto &Path : Callback(SelectedMultilib))
60  addExternCSystemIncludeIfExists(DriverArgs, CC1Args,
61  D.getInstalledDir() + Path);
62  }
63 }
64 
66  return new tools::gnutools::Linker(*this);
67 }
68 
70  if (!getDriver().SysRoot.empty())
71  return getDriver().SysRoot + SelectedMultilib.osSuffix();
72 
73  const std::string InstalledDir(getDriver().getInstalledDir());
74  std::string SysRootPath =
75  InstalledDir + "/../sysroot" + SelectedMultilib.osSuffix();
76  if (llvm::sys::fs::exists(SysRootPath))
77  return SysRootPath;
78 
79  return std::string();
80 }
81 
83 MipsLLVMToolChain::GetCXXStdlibType(const ArgList &Args) const {
84  Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
85  if (A) {
86  StringRef Value = A->getValue();
87  if (Value != "libc++")
88  getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name)
89  << A->getAsString(Args);
90  }
91 
92  return ToolChain::CST_Libcxx;
93 }
94 
96  const llvm::opt::ArgList &DriverArgs,
97  llvm::opt::ArgStringList &CC1Args) const {
98  if (const auto &Callback = Multilibs.includeDirsCallback()) {
99  for (std::string Path : Callback(SelectedMultilib)) {
100  Path = getDriver().getInstalledDir() + Path + "/c++/v1";
101  if (llvm::sys::fs::exists(Path)) {
102  addSystemInclude(DriverArgs, CC1Args, Path);
103  return;
104  }
105  }
106  }
107 }
108 
109 void MipsLLVMToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
110  ArgStringList &CmdArgs) const {
111  assert((GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) &&
112  "Only -lc++ (aka libxx) is supported in this toolchain.");
113 
114  CmdArgs.push_back("-lc++");
115  CmdArgs.push_back("-lc++abi");
116  CmdArgs.push_back("-lunwind");
117 }
118 
120  StringRef Component,
121  FileType Type) const {
122  SmallString<128> Path(getDriver().ResourceDir);
123  llvm::sys::path::append(Path, SelectedMultilib.osSuffix(), "lib" + LibSuffix,
124  getOS());
125  const char *Suffix;
126  switch (Type) {
128  Suffix = ".o";
129  break;
131  Suffix = ".a";
132  break;
134  Suffix = ".so";
135  break;
136  }
137  llvm::sys::path::append(
138  Path, Twine("libclang_rt." + Component + "-" + "mips" + Suffix));
139  return std::string(Path.str());
140 }
clang::driver::toolchains
Definition: AIX.h:55
Mips.h
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::findMIPSMultilibs
bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const llvm::opt::ArgList &Args, DetectedMultilibs &Result)
clang::driver::toolchains::MipsLLVMToolChain::buildLinker
Tool * buildLinker() const override
Definition: MipsLinux.cpp:65
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:221
clang::driver::ToolChain::FT_Object
@ FT_Object
Definition: ToolChain.h:123
clang::driver::toolchains::MipsLLVMToolChain::MipsLLVMToolChain
MipsLLVMToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Mips Toolchain.
Definition: MipsLinux.cpp:25
MipsLinux.h
clang::driver::ToolChain::addExternCSystemIncludeIfExists
static void addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Definition: ToolChain.cpp:859
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
clang::driver::toolchains::MipsLLVMToolChain::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: MipsLinux.cpp:41
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:259
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:112
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::toolchains::MipsLLVMToolChain::computeSysRoot
std::string computeSysRoot() const override
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: MipsLinux.cpp:69
Options.h
clang::driver::Multilib::osSuffix
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:56
clang::driver::tools::gnutools::Linker
Definition: Gnu.h:53
clang::driver::ToolChain::getOS
StringRef getOS() const
Definition: ToolChain.h:240
clang::driver::MultilibSet::includeDirsCallback
const IncludeDirsFunc & includeDirsCallback() const
Definition: Multilib.h:179
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
clang::driver::ToolChain::CXXStdlibType
CXXStdlibType
Definition: ToolChain.h:95
llvm::SmallString< 128 >
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:151
clang::driver::toolchains::Linux
Definition: Linux.h:19
clang::driver::ToolChain::CST_Libcxx
@ CST_Libcxx
Definition: ToolChain.h:96
clang::driver::toolchains::MipsLLVMToolChain::getCompilerRT
std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const override
Definition: MipsLinux.cpp:119
clang::driver::toolchains::MipsLLVMToolChain::GetCXXStdlibType
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: MipsLinux.cpp:83
clang::driver::Driver::ResourceDir
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:135
clang::driver::ToolChain::FT_Static
@ FT_Static
Definition: ToolChain.h:123
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
Value
Value
Definition: UninitializedValues.cpp:102
clang::driver::ToolChain::FileType
FileType
Definition: ToolChain.h:123
clang::driver::tools::mips::getMipsABILibSuffix
std::string getMipsABILibSuffix(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::ToolChain::Multilibs
MultilibSet Multilibs
Definition: ToolChain.h:181
clang::driver::DetectedMultilibs
Definition: Gnu.h:21
clang::driver
Definition: Action.h:31
clang::driver::ToolChain::addSystemInclude
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:837
clang::driver::toolchains::MipsLLVMToolChain::AddCXXStdlibLibArgs
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: MipsLinux.cpp:109
clang::driver::ToolChain::FT_Shared
@ FT_Shared
Definition: ToolChain.h:123
clang::driver::toolchains::MipsLLVMToolChain::addLibCxxIncludePaths
void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: MipsLinux.cpp:95
clang::driver::Driver::getInstalledDir
const char * getInstalledDir() const
Get the path to where the clang executable was installed.
Definition: Driver.h:348