install_paths.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/install/install_paths.h"
  5. #include <memory>
  6. #include <string>
  7. #include "common/check.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/Support/FileSystem.h"
  11. #include "llvm/Support/Path.h"
  12. #include "llvm/Support/VirtualFileSystem.h"
  13. #include "tools/cpp/runfiles/runfiles.h"
  14. namespace Carbon {
  15. // The location within our Bazel output tree of the prefix_root.
  16. static constexpr llvm::StringLiteral PrefixRoot =
  17. "carbon/toolchain/install/prefix_root/";
  18. // Path within an install prefix for our marker of a valid install.
  19. static constexpr llvm::StringLiteral MarkerPath =
  20. "lib/carbon/carbon_install.txt";
  21. auto InstallPaths::MakeExeRelative(llvm::StringRef exe_path) -> InstallPaths {
  22. InstallPaths paths;
  23. // Map from the executable path from the executable path to an install
  24. // prefix path.
  25. if (!llvm::sys::fs::exists(exe_path)) {
  26. paths.SetError(llvm::Twine("No file at executable path: ") + exe_path);
  27. return paths;
  28. }
  29. paths = InstallPaths(exe_path);
  30. // TODO: Detect a Windows executable path and use custom logic to map to the
  31. // correct install prefix for that platform.
  32. // We assume an executable will be in a `bin` directory and this is a
  33. // FHS-like install prefix. We remove the filename and walk up to find the
  34. // expected install prefix.
  35. llvm::sys::path::remove_filename(paths.prefix_);
  36. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix,
  37. "../../");
  38. if (auto error = llvm::sys::fs::make_absolute(paths.prefix_)) {
  39. paths.SetError(error.message());
  40. return paths;
  41. }
  42. paths.CheckMarkerFile();
  43. return paths;
  44. }
  45. auto InstallPaths::MakeForBazelRunfiles(llvm::StringRef exe_path)
  46. -> InstallPaths {
  47. using bazel::tools::cpp::runfiles::Runfiles;
  48. std::string runtimes_error;
  49. std::unique_ptr<Runfiles> runfiles(
  50. Runfiles::Create(exe_path.str(), &runtimes_error));
  51. CARBON_CHECK(runfiles != nullptr, "Failed to find runtimes tree: {0}",
  52. runtimes_error);
  53. std::string relative_marker_path = (PrefixRoot.str() + MarkerPath).str();
  54. std::string runtimes_marker_path = runfiles->Rlocation(relative_marker_path);
  55. // Start from the marker, remove that filename, and walk up to find the
  56. // install prefix.
  57. InstallPaths paths(runtimes_marker_path);
  58. llvm::sys::path::remove_filename(paths.prefix_);
  59. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix,
  60. "../../");
  61. if (auto error = llvm::sys::fs::make_absolute(paths.prefix_)) {
  62. paths.SetError(error.message());
  63. return paths;
  64. }
  65. paths.CheckMarkerFile();
  66. CARBON_CHECK(!paths.error(), "{0}", *paths.error());
  67. return paths;
  68. }
  69. auto InstallPaths::Make(llvm::StringRef install_prefix) -> InstallPaths {
  70. InstallPaths paths(install_prefix);
  71. paths.CheckMarkerFile();
  72. return paths;
  73. }
  74. auto InstallPaths::ReadPreludeManifest() const
  75. -> ErrorOr<llvm::SmallVector<std::string>> {
  76. // This is structured to avoid a vector copy on success.
  77. ErrorOr<llvm::SmallVector<std::string>> result =
  78. llvm::SmallVector<std::string>();
  79. llvm::SmallString<256> manifest;
  80. llvm::sys::path::append(manifest, llvm::sys::path::Style::posix,
  81. core_package(), "prelude_manifest.txt");
  82. auto fs = llvm::vfs::getRealFileSystem();
  83. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
  84. fs->getBufferForFile(manifest);
  85. if (!file) {
  86. result = ErrorBuilder() << "Loading prelude manifest `" << manifest
  87. << "`: " << file.getError().message();
  88. return result;
  89. }
  90. // The manifest should have one file per line.
  91. llvm::StringRef buffer = file.get()->getBuffer();
  92. while (true) {
  93. auto [token, remainder] = llvm::getToken(buffer, "\n");
  94. if (token.empty()) {
  95. break;
  96. }
  97. llvm::SmallString<256> path;
  98. llvm::sys::path::append(path, llvm::sys::path::Style::posix, core_package(),
  99. token);
  100. result->push_back(path.str().str());
  101. buffer = remainder;
  102. }
  103. if (result->empty()) {
  104. result = ErrorBuilder() << "Prelude manifest `" << manifest << "` is empty";
  105. }
  106. return result;
  107. }
  108. auto InstallPaths::SetError(llvm::Twine message) -> void {
  109. // Use an empty prefix on error as that should use the working directory which
  110. // is the least likely problematic.
  111. prefix_ = "";
  112. error_ = {message.str()};
  113. }
  114. auto InstallPaths::CheckMarkerFile() -> void {
  115. if (!llvm::sys::path::is_absolute(prefix_)) {
  116. SetError(llvm::Twine("Not an absolute path: ") + prefix_);
  117. }
  118. llvm::SmallString<256> path(prefix_);
  119. llvm::sys::path::append(path, llvm::sys::path::Style::posix, MarkerPath);
  120. if (!llvm::sys::fs::exists(path)) {
  121. SetError(llvm::Twine("No install marker at path: ") + path);
  122. }
  123. }
  124. auto InstallPaths::core_package() const -> std::string {
  125. llvm::SmallString<256> path(prefix_);
  126. // TODO: Adjust this to work equally well on Windows.
  127. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  128. "lib/carbon/core");
  129. return path.str().str();
  130. }
  131. auto InstallPaths::llvm_install_bin() const -> std::string {
  132. llvm::SmallString<256> path(prefix_);
  133. // TODO: Adjust this to work equally well on Windows.
  134. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  135. "lib/carbon/llvm/bin/");
  136. return path.str().str();
  137. }
  138. auto InstallPaths::clang_path() const -> std::string {
  139. llvm::SmallString<256> path(prefix_);
  140. // TODO: Adjust this to work equally well on Windows.
  141. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  142. "lib/carbon/llvm/bin/clang");
  143. return path.str().str();
  144. }
  145. auto InstallPaths::lld_path() const -> std::string {
  146. llvm::SmallString<256> path(prefix_);
  147. // TODO: Adjust this to work equally well on Windows.
  148. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  149. "lib/carbon/llvm/bin/lld");
  150. return path.str().str();
  151. }
  152. auto InstallPaths::ld_lld_path() const -> std::string {
  153. llvm::SmallString<256> path(prefix_);
  154. // TODO: Adjust this to work equally well on Windows.
  155. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  156. "lib/carbon/llvm/bin/ld.lld");
  157. return path.str().str();
  158. }
  159. auto InstallPaths::ld64_lld_path() const -> std::string {
  160. llvm::SmallString<256> path(prefix_);
  161. // TODO: Adjust this to work equally well on Windows.
  162. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  163. "lib/carbon/llvm/bin/ld64.lld");
  164. return path.str().str();
  165. }
  166. auto InstallPaths::llvm_tool_path(LLVMTool tool) const -> std::string {
  167. llvm::SmallString<256> path(prefix_);
  168. // TODO: Adjust this to work equally well on Windows.
  169. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  170. "lib/carbon/llvm/bin", tool.bin_name());
  171. return path.str().str();
  172. }
  173. } // namespace Carbon