install_paths.cpp 8.0 KB

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