install_paths.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <filesystem>
  6. #include <memory>
  7. #include <string>
  8. #include "common/check.h"
  9. #include "common/filesystem.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/FileSystem.h"
  13. #include "llvm/Support/Path.h"
  14. #include "llvm/Support/VirtualFileSystem.h"
  15. #include "tools/cpp/runfiles/runfiles.h"
  16. namespace Carbon {
  17. // The location within our Bazel output tree of the prefix_root.
  18. static constexpr llvm::StringLiteral PrefixRoot =
  19. "carbon/toolchain/install/prefix_root/";
  20. // Path within an install prefix for our marker of a valid install.
  21. static constexpr llvm::StringLiteral MarkerPath =
  22. "lib/carbon/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 = (PrefixRoot.str() + MarkerPath).str();
  46. std::filesystem::path runtimes_marker_path =
  47. runfiles->Rlocation(relative_marker_path);
  48. // Start from the marker, remove that filename, and walk up to find the
  49. // install prefix.
  50. return MakeFromFile(std::move(runtimes_marker_path));
  51. }
  52. auto InstallPaths::Make(llvm::StringRef install_prefix) -> InstallPaths {
  53. InstallPaths paths(install_prefix.str());
  54. auto open_result = Filesystem::Cwd().OpenDir(paths.prefix_);
  55. if (!open_result.ok()) {
  56. paths.SetError(open_result.error().ToString());
  57. } else {
  58. paths.prefix_dir_ = *std::move(open_result);
  59. paths.CheckMarkerFile();
  60. }
  61. return paths;
  62. }
  63. auto InstallPaths::ReadPreludeManifest() const
  64. -> ErrorOr<llvm::SmallVector<std::string>> {
  65. return ReadManifest(core_package(), "prelude_manifest.txt");
  66. }
  67. auto InstallPaths::ReadClangHeadersManifest() const
  68. -> ErrorOr<llvm::SmallVector<std::string>> {
  69. return ReadManifest(prefix_ / "..", "clang_headers_manifest.txt");
  70. }
  71. auto InstallPaths::ReadManifest(std::filesystem::path manifest_path,
  72. std::filesystem::path manifest_file) const
  73. -> ErrorOr<llvm::SmallVector<std::string>> {
  74. // This is structured to avoid a vector copy on success.
  75. ErrorOr<llvm::SmallVector<std::string>> result =
  76. llvm::SmallVector<std::string>();
  77. // TODO: It would be nice to adjust the manifests to be within the install
  78. // prefix and use that open directory to access the manifest. Also to update
  79. // callers to be able to use the relative paths via an open directory rather
  80. // than having to form absolute paths for all the entries.
  81. auto read_result =
  82. Filesystem::Cwd().ReadFileToString(manifest_path / manifest_file);
  83. if (!read_result.ok()) {
  84. result = ErrorBuilder()
  85. << "Loading manifest `" << (manifest_path / manifest_file)
  86. << "`: " << read_result.error();
  87. return result;
  88. }
  89. // The manifest should have one file per line.
  90. llvm::StringRef buffer = *read_result;
  91. while (true) {
  92. auto [token, remainder] = llvm::getToken(buffer, "\n");
  93. if (token.empty()) {
  94. break;
  95. }
  96. result->push_back((manifest_path / std::string_view(token)).native());
  97. buffer = remainder;
  98. }
  99. if (result->empty()) {
  100. result = ErrorBuilder()
  101. << "Manifest `" << (manifest_path / manifest_file) << "` is empty";
  102. }
  103. return result;
  104. }
  105. auto InstallPaths::MakeFromFile(std::filesystem::path file_path)
  106. -> InstallPaths {
  107. // TODO: Detect a Windows executable path and use custom logic to map to the
  108. // correct install prefix for that platform.
  109. //
  110. // We assume an executable will be in a `bin` directory and this is a
  111. // FHS-like install prefix. We remove the filename and walk up to find the
  112. // expected install prefix.
  113. std::error_code ec;
  114. InstallPaths paths(std::filesystem::absolute(
  115. std::move(file_path).remove_filename() / "../..", ec));
  116. if (ec) {
  117. paths.SetError(ec.message());
  118. return paths;
  119. }
  120. auto open_result = Filesystem::Cwd().OpenDir(paths.prefix_);
  121. if (!open_result.ok()) {
  122. paths.SetError(open_result.error().ToString());
  123. return paths;
  124. }
  125. paths.prefix_dir_ = *std::move(open_result);
  126. paths.CheckMarkerFile();
  127. return paths;
  128. }
  129. auto InstallPaths::SetError(llvm::Twine message) -> void {
  130. // Use an empty prefix on error as that should use the working directory which
  131. // is the least likely problematic.
  132. prefix_ = "";
  133. prefix_dir_ = Filesystem::Dir();
  134. error_ = {message.str()};
  135. }
  136. auto InstallPaths::CheckMarkerFile() -> void {
  137. if (!prefix_.is_absolute()) {
  138. SetError(llvm::Twine("Not an absolute path: ") + prefix_.native());
  139. return;
  140. }
  141. auto access_result = prefix_dir_.Access(MarkerPath.str());
  142. if (!access_result.ok()) {
  143. SetError(access_result.error().ToString());
  144. return;
  145. }
  146. if (!*access_result) {
  147. SetError(llvm::Twine("No install marker at path: ") +
  148. (prefix_ / std::string_view(MarkerPath)).native());
  149. return;
  150. }
  151. // Success!
  152. }
  153. auto InstallPaths::core_package() const -> std::filesystem::path {
  154. // TODO: Adjust this to work equally well on Windows.
  155. return prefix_ / "lib/carbon/core";
  156. }
  157. auto InstallPaths::llvm_install_bin() const -> std::filesystem::path {
  158. // TODO: Adjust this to work equally well on Windows.
  159. return prefix_ / "lib/carbon/llvm/bin/";
  160. }
  161. auto InstallPaths::clang_path() const -> std::filesystem::path {
  162. // TODO: Adjust this to work equally well on Windows.
  163. return prefix_ / "lib/carbon/llvm/bin/clang";
  164. }
  165. auto InstallPaths::lld_path() const -> std::filesystem::path {
  166. // TODO: Adjust this to work equally well on Windows.
  167. return prefix_ / "lib/carbon/llvm/bin/lld";
  168. }
  169. auto InstallPaths::ld_lld_path() const -> std::filesystem::path {
  170. // TODO: Adjust this to work equally well on Windows.
  171. return prefix_ / "lib/carbon/llvm/bin/ld.lld";
  172. }
  173. auto InstallPaths::ld64_lld_path() const -> std::filesystem::path {
  174. // TODO: Adjust this to work equally well on Windows.
  175. return prefix_ / "lib/carbon/llvm/bin/ld64.lld";
  176. }
  177. auto InstallPaths::llvm_tool_path(LLVMTool tool) const
  178. -> std::filesystem::path {
  179. // TODO: Adjust this to work equally well on Windows.
  180. return prefix_ / "lib/carbon/llvm/bin" / std::string_view(tool.bin_name());
  181. }
  182. } // namespace Carbon