blob: cfda7f4bb4dfddce1c0161145c25c4ac8fd1a90b [file] [log] [blame]
Fangrui Song524b3c12019-03-01 06:49:51 +00001//===-- MipsLinux.cpp - Mips ToolChain Implementations ----------*- C++ -*-===//
David L. Jonesf561aba2017-03-08 01:02:16 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David L. Jonesf561aba2017-03-08 01:02:16 +00006//
7//===----------------------------------------------------------------------===//
8
Konstantin Zhuravlyove37b32c2017-03-08 22:36:04 +00009#include "MipsLinux.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000010#include "Arch/Mips.h"
11#include "CommonArgs.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000012#include "clang/Driver/Driver.h"
13#include "clang/Driver/DriverDiagnostic.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
19using namespace clang::driver;
20using namespace clang::driver::toolchains;
21using namespace clang;
22using namespace llvm::opt;
23
24/// Mips Toolchain
25MipsLLVMToolChain::MipsLLVMToolChain(const Driver &D,
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
41void MipsLLVMToolChain::AddClangSystemIncludeArgs(
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)) {
49 SmallString<128> P(D.ResourceDir);
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
65Tool *MipsLLVMToolChain::buildLinker() const {
66 return new tools::gnutools::Linker(*this);
67}
68
69std::string MipsLLVMToolChain::computeSysRoot() const {
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
82ToolChain::CXXStdlibType
83MipsLLVMToolChain::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
Petr Hosek8d612142018-04-10 19:55:55 +000095void MipsLLVMToolChain::addLibCxxIncludePaths(
96 const llvm::opt::ArgList &DriverArgs,
97 llvm::opt::ArgStringList &CC1Args) const {
David L. Jonesf561aba2017-03-08 01:02:16 +000098 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)) {
Petr Hosek8d612142018-04-10 19:55:55 +0000102 addSystemInclude(DriverArgs, CC1Args, Path);
103 return;
David L. Jonesf561aba2017-03-08 01:02:16 +0000104 }
105 }
106 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000107}
108
109void MipsLLVMToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
110 ArgStringList &CmdArgs) const {
111 assert((GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) &&
Hiroshi Inoue3170de02017-07-01 08:46:43 +0000112 "Only -lc++ (aka libxx) is supported in this toolchain.");
David L. Jonesf561aba2017-03-08 01:02:16 +0000113
114 CmdArgs.push_back("-lc++");
115 CmdArgs.push_back("-lc++abi");
116 CmdArgs.push_back("-lunwind");
117}
118
119std::string MipsLLVMToolChain::getCompilerRT(const ArgList &Args,
120 StringRef Component,
Petr Hosek930d46a2019-03-12 02:12:48 +0000121 FileType Type) const {
David L. Jonesf561aba2017-03-08 01:02:16 +0000122 SmallString<128> Path(getDriver().ResourceDir);
123 llvm::sys::path::append(Path, SelectedMultilib.osSuffix(), "lib" + LibSuffix,
124 getOS());
Petr Hosek930d46a2019-03-12 02:12:48 +0000125 const char *Suffix;
126 switch (Type) {
127 case ToolChain::FT_Object:
128 Suffix = ".o";
129 break;
130 case ToolChain::FT_Static:
131 Suffix = ".a";
132 break;
133 case ToolChain::FT_Shared:
134 Suffix = ".so";
135 break;
136 }
137 llvm::sys::path::append(
138 Path, Twine("libclang_rt." + Component + "-" + "mips" + Suffix));
David L. Jonesf561aba2017-03-08 01:02:16 +0000139 return Path.str();
140}