install_paths.cpp 8.3 KB

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