xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/MipsLinux.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
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 "clang/Driver/Driver.h"
12 #include "clang/Driver/Options.h"
13 #include "llvm/Option/ArgList.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace clang::driver;
18 using namespace clang::driver::toolchains;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 /// Mips Toolchain
23 MipsLLVMToolChain::MipsLLVMToolChain(const Driver &D,
24                                      const llvm::Triple &Triple,
25                                      const ArgList &Args)
26     : Linux(D, Triple, Args) {
27   // Select the correct multilib according to the given arguments.
28   DetectedMultilibs Result;
29   findMIPSMultilibs(D, Triple, "", Args, Result);
30   Multilibs = Result.Multilibs;
31   SelectedMultilibs = Result.SelectedMultilibs;
32 
33   // Find out the library suffix based on the ABI.
34   LibSuffix = tools::mips::getMipsABILibSuffix(Args, Triple);
35   getFilePaths().clear();
36   getFilePaths().push_back(computeSysRoot() + "/usr/lib" + LibSuffix);
37 }
38 
39 void MipsLLVMToolChain::AddClangSystemIncludeArgs(
40     const ArgList &DriverArgs, ArgStringList &CC1Args) const {
41   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
42     return;
43 
44   const Driver &D = getDriver();
45 
46   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
47     SmallString<128> P(D.ResourceDir);
48     llvm::sys::path::append(P, "include");
49     addSystemInclude(DriverArgs, CC1Args, P);
50   }
51 
52   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
53     return;
54 
55   const auto &Callback = Multilibs.includeDirsCallback();
56   if (Callback) {
57     for (const auto &Path : Callback(SelectedMultilibs.back()))
58       addExternCSystemIncludeIfExists(DriverArgs, CC1Args, D.Dir + Path);
59   }
60 }
61 
62 Tool *MipsLLVMToolChain::buildLinker() const {
63   return new tools::gnutools::Linker(*this);
64 }
65 
66 std::string MipsLLVMToolChain::computeSysRoot() const {
67   if (!getDriver().SysRoot.empty())
68     return getDriver().SysRoot + SelectedMultilibs.back().osSuffix();
69 
70   const std::string InstalledDir(getDriver().Dir);
71   std::string SysRootPath =
72       InstalledDir + "/../sysroot" + SelectedMultilibs.back().osSuffix();
73   if (llvm::sys::fs::exists(SysRootPath))
74     return SysRootPath;
75 
76   return std::string();
77 }
78 
79 ToolChain::CXXStdlibType
80 MipsLLVMToolChain::GetCXXStdlibType(const ArgList &Args) const {
81   Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
82   if (A) {
83     StringRef Value = A->getValue();
84     if (Value != "libc++")
85       getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name)
86           << A->getAsString(Args);
87   }
88 
89   return ToolChain::CST_Libcxx;
90 }
91 
92 void MipsLLVMToolChain::addLibCxxIncludePaths(
93     const llvm::opt::ArgList &DriverArgs,
94     llvm::opt::ArgStringList &CC1Args) const {
95   if (const auto &Callback = Multilibs.includeDirsCallback()) {
96     for (std::string Path : Callback(SelectedMultilibs.back())) {
97       Path = getDriver().Dir + Path + "/c++/v1";
98       if (llvm::sys::fs::exists(Path)) {
99         addSystemInclude(DriverArgs, CC1Args, Path);
100         return;
101       }
102     }
103   }
104 }
105 
106 void MipsLLVMToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
107                                             ArgStringList &CmdArgs) const {
108   assert((GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) &&
109          "Only -lc++ (aka libxx) is supported in this toolchain.");
110 
111   CmdArgs.push_back("-lc++");
112   if (Args.hasArg(options::OPT_fexperimental_library))
113     CmdArgs.push_back("-lc++experimental");
114   CmdArgs.push_back("-lc++abi");
115   CmdArgs.push_back("-lunwind");
116 }
117 
118 std::string MipsLLVMToolChain::getCompilerRT(const ArgList &Args,
119                                              StringRef Component, FileType Type,
120                                              bool IsFortran) const {
121   SmallString<128> Path(getDriver().ResourceDir);
122   llvm::sys::path::append(Path, SelectedMultilibs.back().osSuffix(), "lib" + LibSuffix,
123                           getOS());
124   const char *Suffix;
125   switch (Type) {
126   case ToolChain::FT_Object:
127     Suffix = ".o";
128     break;
129   case ToolChain::FT_Static:
130     Suffix = ".a";
131     break;
132   case ToolChain::FT_Shared:
133     Suffix = ".so";
134     break;
135   }
136   llvm::sys::path::append(
137       Path, Twine("libclang_rt." + Component + "-" + "mips" + Suffix));
138   return std::string(Path);
139 }
140