MipsLinux.cpp 4.5 KB

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