install_paths.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/base/install_paths.h"
  5. #include <filesystem>
  6. #include <memory>
  7. #include <string>
  8. #include "clang/Basic/Version.h"
  9. #include "common/check.h"
  10. #include "common/filesystem.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Support/FileSystem.h"
  14. #include "llvm/Support/Path.h"
  15. #include "llvm/Support/VirtualFileSystem.h"
  16. #include "tools/cpp/runfiles/runfiles.h"
  17. namespace Carbon {
  18. // The location within our Bazel output tree of the install root.
  19. static constexpr llvm::StringLiteral BazelRoot =
  20. "carbon/toolchain/install/prefix/lib/carbon/";
  21. // Path within an install root for our marker of a valid install.
  22. static constexpr llvm::StringLiteral MarkerPath = "carbon_install.txt";
  23. auto InstallPaths::MakeExeRelative(llvm::StringRef exe_path) -> InstallPaths {
  24. InstallPaths paths;
  25. // Double check the exe was present.
  26. auto exe_access_result = Filesystem::Cwd().Access(exe_path.str());
  27. if (!exe_access_result.ok()) {
  28. paths.SetError(llvm::Twine("Failed to test for access executable: ") +
  29. exe_access_result.error().ToString());
  30. return paths;
  31. } else if (!*exe_access_result) {
  32. paths.SetError(llvm::Twine("Unable to access executable: ") + exe_path);
  33. return paths;
  34. }
  35. return MakeFromFile(exe_path.str());
  36. }
  37. auto InstallPaths::MakeForBazelRunfiles(llvm::StringRef exe_path)
  38. -> InstallPaths {
  39. using bazel::tools::cpp::runfiles::Runfiles;
  40. std::string runtimes_error;
  41. std::unique_ptr<Runfiles> runfiles(
  42. Runfiles::Create(exe_path.str(), &runtimes_error));
  43. CARBON_CHECK(runfiles != nullptr, "Failed to find runtimes tree: {0}",
  44. runtimes_error);
  45. std::string relative_marker_path = (BazelRoot.str() + MarkerPath).str();
  46. std::filesystem::path runtimes_marker_path =
  47. runfiles->Rlocation(relative_marker_path);
  48. // Directly use the marker file's path.
  49. return MakeFromFile(std::move(runtimes_marker_path));
  50. }
  51. auto InstallPaths::Make(llvm::StringRef install_root) -> InstallPaths {
  52. InstallPaths paths(install_root.str());
  53. auto open_result = Filesystem::Cwd().OpenDir(paths.root_);
  54. if (!open_result.ok()) {
  55. paths.SetError(open_result.error().ToString());
  56. } else {
  57. paths.root_dir_ = *std::move(open_result);
  58. paths.CheckMarkerFile();
  59. }
  60. return paths;
  61. }
  62. auto InstallPaths::ReadPreludeManifest() const
  63. -> ErrorOr<llvm::SmallVector<std::string>> {
  64. return ReadManifest(core_package(), "prelude_manifest.txt");
  65. }
  66. auto InstallPaths::ReadClangHeadersManifest() const
  67. -> ErrorOr<llvm::SmallVector<std::string>> {
  68. // TODO: This is the only place where we read from outside of the install
  69. // root. Consider whether this manifest should be within the install or
  70. // consider moving the code to access it to be separate and specific to the
  71. // infrastructure needing it.
  72. return ReadManifest(root_ / "../../..", "clang_headers_manifest.txt");
  73. }
  74. auto InstallPaths::ReadManifest(std::filesystem::path manifest_path,
  75. std::filesystem::path manifest_file) const
  76. -> ErrorOr<llvm::SmallVector<std::string>> {
  77. // This is structured to avoid a vector copy on success.
  78. ErrorOr<llvm::SmallVector<std::string>> result =
  79. llvm::SmallVector<std::string>();
  80. // TODO: It would be nice to adjust the manifests to be within the install
  81. // root and use that open directory to access the manifest. Also to update
  82. // callers to be able to use the relative paths via an open directory rather
  83. // than having to form absolute paths for all the entries.
  84. auto read_result =
  85. Filesystem::Cwd().ReadFileToString(manifest_path / manifest_file);
  86. if (!read_result.ok()) {
  87. result = ErrorBuilder()
  88. << "Loading manifest `" << (manifest_path / manifest_file)
  89. << "`: " << read_result.error();
  90. return result;
  91. }
  92. // The manifest should have one file per line.
  93. llvm::StringRef buffer = *read_result;
  94. while (true) {
  95. auto [token, remainder] = llvm::getToken(buffer, "\n");
  96. if (token.empty()) {
  97. break;
  98. }
  99. result->push_back((manifest_path / std::string_view(token)).native());
  100. buffer = remainder;
  101. }
  102. if (result->empty()) {
  103. result = ErrorBuilder()
  104. << "Manifest `" << (manifest_path / manifest_file) << "` is empty";
  105. }
  106. return result;
  107. }
  108. auto InstallPaths::MakeFromFile(std::filesystem::path file_path)
  109. -> InstallPaths {
  110. // TODO: Add any custom logic needed to detect the correct install root on
  111. // Windows once we have support for that platform.
  112. //
  113. // We assume the provided path is either the marker file itself or the busybox
  114. // executable that is adjacent to the marker file. That means the root is just
  115. // the directory of this path.
  116. InstallPaths paths(std::move(file_path).remove_filename());
  117. auto open_result = Filesystem::Cwd().OpenDir(paths.root_);
  118. if (!open_result.ok()) {
  119. paths.SetError(open_result.error().ToString());
  120. return paths;
  121. }
  122. paths.root_dir_ = *std::move(open_result);
  123. paths.CheckMarkerFile();
  124. return paths;
  125. }
  126. auto InstallPaths::SetError(llvm::Twine message) -> void {
  127. // Use an empty root on error as that should use the working directory which
  128. // is the least likely problematic.
  129. root_ = "";
  130. root_dir_ = Filesystem::Dir();
  131. error_ = {message.str()};
  132. }
  133. auto InstallPaths::CheckMarkerFile() -> void {
  134. auto access_result = root_dir_.Access(MarkerPath.str());
  135. if (!access_result.ok()) {
  136. SetError(access_result.error().ToString());
  137. return;
  138. }
  139. if (!*access_result) {
  140. SetError(llvm::Twine("No install marker at path: ") +
  141. (root_ / std::string_view(MarkerPath)).native());
  142. return;
  143. }
  144. // Success!
  145. }
  146. auto InstallPaths::core_package() const -> std::filesystem::path {
  147. // TODO: Adjust this to work equally well on Windows.
  148. return root_ / "core";
  149. }
  150. auto InstallPaths::llvm_install_bin() const -> std::filesystem::path {
  151. // TODO: Adjust this to work equally well on Windows.
  152. return root_ / "llvm/bin";
  153. }
  154. auto InstallPaths::clang_path() const -> std::filesystem::path {
  155. // TODO: Adjust this to work equally well on Windows.
  156. return llvm_install_bin() / "clang";
  157. }
  158. auto InstallPaths::lld_path() const -> std::filesystem::path {
  159. // TODO: Adjust this to work equally well on Windows.
  160. return llvm_install_bin() / "lld";
  161. }
  162. auto InstallPaths::ld_lld_path() const -> std::filesystem::path {
  163. // TODO: Adjust this to work equally well on Windows.
  164. return llvm_install_bin() / "ld.lld";
  165. }
  166. auto InstallPaths::ld64_lld_path() const -> std::filesystem::path {
  167. // TODO: Adjust this to work equally well on Windows.
  168. return llvm_install_bin() / "ld64.lld";
  169. }
  170. auto InstallPaths::llvm_tool_path(LLVMTool tool) const
  171. -> std::filesystem::path {
  172. // TODO: Adjust this to work equally well on Windows.
  173. return llvm_install_bin() / std::string_view(tool.bin_name());
  174. }
  175. auto InstallPaths::clang_resource_path() const -> std::filesystem::path {
  176. // TODO: Adjust this to work equally well on Windows.
  177. return root_ / "llvm/lib/clang/" CLANG_VERSION_MAJOR_STRING;
  178. }
  179. auto InstallPaths::runtimes_root() const -> std::filesystem::path {
  180. // TODO: Adjust this to work equally well on Windows.
  181. return root_ / "runtimes";
  182. }
  183. auto InstallPaths::libunwind_path() const -> std::filesystem::path {
  184. // TODO: Adjust this to work equally well on Windows.
  185. return runtimes_root() / "libunwind";
  186. }
  187. auto InstallPaths::libcxx_path() const -> std::filesystem::path {
  188. // TODO: Adjust this to work equally well on Windows.
  189. return runtimes_root() / "libcxx";
  190. }
  191. auto InstallPaths::libcxxabi_path() const -> std::filesystem::path {
  192. // TODO: Adjust this to work equally well on Windows.
  193. return runtimes_root() / "libcxxabi";
  194. }
  195. auto InstallPaths::libc_path() const -> std::filesystem::path {
  196. // TODO: Adjust this to work equally well on Windows.
  197. return runtimes_root() / "libc";
  198. }
  199. auto InstallPaths::digest_path() const -> std::filesystem::path {
  200. // TODO: Adjust this to work equally well on Windows.
  201. return root_ / "install_digest.txt";
  202. }
  203. } // namespace Carbon