CSKYToolChain.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //===--- CSKYToolchain.cpp - CSKY 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 "CSKYToolChain.h"
  9. #include "CommonArgs.h"
  10. #include "clang/Driver/Compilation.h"
  11. #include "clang/Driver/InputInfo.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. #include "llvm/Support/raw_ostream.h"
  17. using namespace clang::driver;
  18. using namespace clang::driver::toolchains;
  19. using namespace clang::driver::tools;
  20. using namespace clang;
  21. using namespace llvm::opt;
  22. static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
  23. const Multilib &Multilib,
  24. StringRef InstallPath,
  25. ToolChain::path_list &Paths) {
  26. if (const auto &PathsCallback = Multilibs.filePathsCallback())
  27. for (const auto &Path : PathsCallback(Multilib))
  28. addPathIfExists(D, InstallPath + Path, Paths);
  29. }
  30. /// CSKY Toolchain
  31. CSKYToolChain::CSKYToolChain(const Driver &D, const llvm::Triple &Triple,
  32. const ArgList &Args)
  33. : Generic_ELF(D, Triple, Args) {
  34. GCCInstallation.init(Triple, Args);
  35. if (GCCInstallation.isValid()) {
  36. Multilibs = GCCInstallation.getMultilibs();
  37. SelectedMultilib = GCCInstallation.getMultilib();
  38. path_list &Paths = getFilePaths();
  39. // Add toolchain/multilib specific file paths.
  40. addMultilibsFilePaths(D, Multilibs, SelectedMultilib,
  41. GCCInstallation.getInstallPath(), Paths);
  42. getFilePaths().push_back(GCCInstallation.getInstallPath().str() +
  43. SelectedMultilib.osSuffix());
  44. ToolChain::path_list &PPaths = getProgramPaths();
  45. // Multilib cross-compiler GCC installations put ld in a triple-prefixed
  46. // directory off of the parent of the GCC installation.
  47. PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
  48. GCCInstallation.getTriple().str() + "/bin")
  49. .str());
  50. PPaths.push_back((GCCInstallation.getParentLibPath() + "/../bin").str());
  51. } else {
  52. getProgramPaths().push_back(D.Dir);
  53. }
  54. getFilePaths().push_back(computeSysRoot() + "/lib" +
  55. SelectedMultilib.osSuffix());
  56. }
  57. Tool *CSKYToolChain::buildLinker() const {
  58. return new tools::CSKY::Linker(*this);
  59. }
  60. ToolChain::RuntimeLibType CSKYToolChain::GetDefaultRuntimeLibType() const {
  61. return GCCInstallation.isValid() ? ToolChain::RLT_Libgcc
  62. : ToolChain::RLT_CompilerRT;
  63. }
  64. ToolChain::UnwindLibType
  65. CSKYToolChain::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
  66. return ToolChain::UNW_None;
  67. }
  68. void CSKYToolChain::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
  69. llvm::opt::ArgStringList &CC1Args,
  70. Action::OffloadKind) const {
  71. CC1Args.push_back("-nostdsysteminc");
  72. }
  73. void CSKYToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  74. ArgStringList &CC1Args) const {
  75. if (DriverArgs.hasArg(options::OPT_nostdinc))
  76. return;
  77. if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
  78. SmallString<128> Dir(computeSysRoot());
  79. llvm::sys::path::append(Dir, "include");
  80. addSystemInclude(DriverArgs, CC1Args, Dir.str());
  81. SmallString<128> Dir2(computeSysRoot());
  82. llvm::sys::path::append(Dir2, "sys-include");
  83. addSystemInclude(DriverArgs, CC1Args, Dir2.str());
  84. }
  85. }
  86. void CSKYToolChain::addLibStdCxxIncludePaths(
  87. const llvm::opt::ArgList &DriverArgs,
  88. llvm::opt::ArgStringList &CC1Args) const {
  89. const GCCVersion &Version = GCCInstallation.getVersion();
  90. StringRef TripleStr = GCCInstallation.getTriple().str();
  91. const Multilib &Multilib = GCCInstallation.getMultilib();
  92. addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
  93. TripleStr, Multilib.includeSuffix(), DriverArgs,
  94. CC1Args);
  95. }
  96. std::string CSKYToolChain::computeSysRoot() const {
  97. if (!getDriver().SysRoot.empty())
  98. return getDriver().SysRoot;
  99. SmallString<128> SysRootDir;
  100. if (GCCInstallation.isValid()) {
  101. StringRef LibDir = GCCInstallation.getParentLibPath();
  102. StringRef TripleStr = GCCInstallation.getTriple().str();
  103. llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
  104. } else {
  105. // Use the triple as provided to the driver. Unlike the parsed triple
  106. // this has not been normalized to always contain every field.
  107. llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
  108. getDriver().getTargetTriple());
  109. }
  110. if (!llvm::sys::fs::exists(SysRootDir))
  111. return std::string();
  112. return std::string(SysRootDir.str());
  113. }
  114. void CSKY::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  115. const InputInfo &Output,
  116. const InputInfoList &Inputs,
  117. const ArgList &Args,
  118. const char *LinkingOutput) const {
  119. const ToolChain &ToolChain = getToolChain();
  120. const Driver &D = ToolChain.getDriver();
  121. ArgStringList CmdArgs;
  122. if (!D.SysRoot.empty())
  123. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  124. CmdArgs.push_back("-m");
  125. CmdArgs.push_back("cskyelf");
  126. std::string Linker = getToolChain().GetLinkerPath();
  127. bool WantCRTs =
  128. !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
  129. const char *crtbegin, *crtend;
  130. auto RuntimeLib = ToolChain.GetRuntimeLibType(Args);
  131. if (RuntimeLib == ToolChain::RLT_Libgcc) {
  132. crtbegin = "crtbegin.o";
  133. crtend = "crtend.o";
  134. } else {
  135. assert(RuntimeLib == ToolChain::RLT_CompilerRT);
  136. crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin",
  137. ToolChain::FT_Object);
  138. crtend =
  139. ToolChain.getCompilerRTArgString(Args, "crtend", ToolChain::FT_Object);
  140. }
  141. if (WantCRTs) {
  142. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
  143. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  144. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  145. }
  146. Args.AddAllArgs(CmdArgs, options::OPT_L);
  147. ToolChain.AddFilePathLibArgs(Args, CmdArgs);
  148. Args.AddAllArgs(CmdArgs,
  149. {options::OPT_T_Group, options::OPT_e, options::OPT_s,
  150. options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
  151. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
  152. // TODO: add C++ includes and libs if compiling C++.
  153. if (!Args.hasArg(options::OPT_nostdlib) &&
  154. !Args.hasArg(options::OPT_nodefaultlibs)) {
  155. if (ToolChain.ShouldLinkCXXStdlib(Args))
  156. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  157. CmdArgs.push_back("--start-group");
  158. CmdArgs.push_back("-lc");
  159. if (Args.hasArg(options::OPT_msim))
  160. CmdArgs.push_back("-lsemi");
  161. else
  162. CmdArgs.push_back("-lnosys");
  163. CmdArgs.push_back("--end-group");
  164. AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
  165. }
  166. if (WantCRTs) {
  167. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
  168. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  169. }
  170. CmdArgs.push_back("-o");
  171. CmdArgs.push_back(Output.getFilename());
  172. C.addCommand(std::make_unique<Command>(
  173. JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
  174. CmdArgs, Inputs, Output));
  175. }
  176. // CSKY tools end.