install_paths.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. return ReadManifest(core_package(), "prelude_manifest.txt");
  77. }
  78. auto InstallPaths::ReadClangHeadersManifest() const
  79. -> ErrorOr<llvm::SmallVector<std::string>> {
  80. llvm::SmallString<256> manifest_path(prefix_);
  81. llvm::sys::path::append(manifest_path, llvm::sys::path::Style::posix, "..");
  82. return ReadManifest(manifest_path, "clang_headers_manifest.txt");
  83. }
  84. auto InstallPaths::ReadManifest(llvm::StringRef manifest_path,
  85. llvm::StringRef manifest_file) const
  86. -> ErrorOr<llvm::SmallVector<std::string>> {
  87. // This is structured to avoid a vector copy on success.
  88. ErrorOr<llvm::SmallVector<std::string>> result =
  89. llvm::SmallVector<std::string>();
  90. llvm::SmallString<256> manifest;
  91. llvm::sys::path::append(manifest, llvm::sys::path::Style::posix,
  92. manifest_path, manifest_file);
  93. auto fs = llvm::vfs::getRealFileSystem();
  94. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
  95. fs->getBufferForFile(manifest);
  96. if (!file) {
  97. result = ErrorBuilder() << "Loading manifest `" << manifest
  98. << "`: " << file.getError().message();
  99. return result;
  100. }
  101. // The manifest should have one file per line.
  102. llvm::StringRef buffer = file.get()->getBuffer();
  103. while (true) {
  104. auto [token, remainder] = llvm::getToken(buffer, "\n");
  105. if (token.empty()) {
  106. break;
  107. }
  108. llvm::SmallString<256> path;
  109. llvm::sys::path::append(path, llvm::sys::path::Style::posix, manifest_path,
  110. token);
  111. result->push_back(path.str().str());
  112. buffer = remainder;
  113. }
  114. if (result->empty()) {
  115. result = ErrorBuilder() << "Manifest `" << manifest << "` is empty";
  116. }
  117. return result;
  118. }
  119. auto InstallPaths::SetError(llvm::Twine message) -> void {
  120. // Use an empty prefix on error as that should use the working directory which
  121. // is the least likely problematic.
  122. prefix_ = "";
  123. error_ = {message.str()};
  124. }
  125. auto InstallPaths::CheckMarkerFile() -> void {
  126. if (!llvm::sys::path::is_absolute(prefix_)) {
  127. SetError(llvm::Twine("Not an absolute path: ") + prefix_);
  128. }
  129. llvm::SmallString<256> path(prefix_);
  130. llvm::sys::path::append(path, llvm::sys::path::Style::posix, MarkerPath);
  131. if (!llvm::sys::fs::exists(path)) {
  132. SetError(llvm::Twine("No install marker at path: ") + path);
  133. }
  134. }
  135. auto InstallPaths::core_package() const -> std::string {
  136. llvm::SmallString<256> path(prefix_);
  137. // TODO: Adjust this to work equally well on Windows.
  138. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  139. "lib/carbon/core");
  140. return path.str().str();
  141. }
  142. auto InstallPaths::llvm_install_bin() const -> std::string {
  143. llvm::SmallString<256> path(prefix_);
  144. // TODO: Adjust this to work equally well on Windows.
  145. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  146. "lib/carbon/llvm/bin/");
  147. return path.str().str();
  148. }
  149. auto InstallPaths::clang_path() const -> std::string {
  150. llvm::SmallString<256> path(prefix_);
  151. // TODO: Adjust this to work equally well on Windows.
  152. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  153. "lib/carbon/llvm/bin/clang");
  154. return path.str().str();
  155. }
  156. auto InstallPaths::lld_path() const -> std::string {
  157. llvm::SmallString<256> path(prefix_);
  158. // TODO: Adjust this to work equally well on Windows.
  159. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  160. "lib/carbon/llvm/bin/lld");
  161. return path.str().str();
  162. }
  163. auto InstallPaths::ld_lld_path() const -> std::string {
  164. llvm::SmallString<256> path(prefix_);
  165. // TODO: Adjust this to work equally well on Windows.
  166. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  167. "lib/carbon/llvm/bin/ld.lld");
  168. return path.str().str();
  169. }
  170. auto InstallPaths::ld64_lld_path() const -> std::string {
  171. llvm::SmallString<256> path(prefix_);
  172. // TODO: Adjust this to work equally well on Windows.
  173. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  174. "lib/carbon/llvm/bin/ld64.lld");
  175. return path.str().str();
  176. }
  177. auto InstallPaths::llvm_tool_path(LLVMTool tool) const -> std::string {
  178. llvm::SmallString<256> path(prefix_);
  179. // TODO: Adjust this to work equally well on Windows.
  180. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  181. "lib/carbon/llvm/bin", tool.bin_name());
  182. return path.str().str();
  183. }
  184. } // namespace Carbon