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