install_paths.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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::move(file_path).remove_filename() / "../..");
  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. auto access_result = prefix_dir_.Access(MarkerPath.str());
  138. if (!access_result.ok()) {
  139. SetError(access_result.error().ToString());
  140. return;
  141. }
  142. if (!*access_result) {
  143. SetError(llvm::Twine("No install marker at path: ") +
  144. (prefix_ / std::string_view(MarkerPath)).native());
  145. return;
  146. }
  147. // Success!
  148. }
  149. auto InstallPaths::core_package() const -> std::filesystem::path {
  150. // TODO: Adjust this to work equally well on Windows.
  151. return prefix_ / "lib/carbon/core";
  152. }
  153. auto InstallPaths::llvm_install_bin() const -> std::filesystem::path {
  154. // TODO: Adjust this to work equally well on Windows.
  155. return prefix_ / "lib/carbon/llvm/bin/";
  156. }
  157. auto InstallPaths::clang_path() const -> std::filesystem::path {
  158. // TODO: Adjust this to work equally well on Windows.
  159. return prefix_ / "lib/carbon/llvm/bin/clang";
  160. }
  161. auto InstallPaths::lld_path() const -> std::filesystem::path {
  162. // TODO: Adjust this to work equally well on Windows.
  163. return prefix_ / "lib/carbon/llvm/bin/lld";
  164. }
  165. auto InstallPaths::ld_lld_path() const -> std::filesystem::path {
  166. // TODO: Adjust this to work equally well on Windows.
  167. return prefix_ / "lib/carbon/llvm/bin/ld.lld";
  168. }
  169. auto InstallPaths::ld64_lld_path() const -> std::filesystem::path {
  170. // TODO: Adjust this to work equally well on Windows.
  171. return prefix_ / "lib/carbon/llvm/bin/ld64.lld";
  172. }
  173. auto InstallPaths::llvm_tool_path(LLVMTool tool) const
  174. -> std::filesystem::path {
  175. // TODO: Adjust this to work equally well on Windows.
  176. return prefix_ / "lib/carbon/llvm/bin" / std::string_view(tool.bin_name());
  177. }
  178. auto InstallPaths::clang_resource_path() const -> std::filesystem::path {
  179. // TODO: Adjust this to work equally well on Windows.
  180. return prefix_ / "lib/carbon/llvm/lib/clang/" CLANG_VERSION_MAJOR_STRING;
  181. }
  182. auto InstallPaths::runtimes_root() const -> std::filesystem::path {
  183. // TODO: Adjust this to work equally well on Windows.
  184. return prefix_ / "lib/carbon/runtimes";
  185. }
  186. auto InstallPaths::libunwind_path() const -> std::filesystem::path {
  187. // TODO: Adjust this to work equally well on Windows.
  188. return prefix_ / "lib/carbon/runtimes/libunwind";
  189. }
  190. auto InstallPaths::libcxx_path() const -> std::filesystem::path {
  191. // TODO: Adjust this to work equally well on Windows.
  192. return prefix_ / "lib/carbon/runtimes/libcxx";
  193. }
  194. auto InstallPaths::libcxxabi_path() const -> std::filesystem::path {
  195. // TODO: Adjust this to work equally well on Windows.
  196. return prefix_ / "lib/carbon/runtimes/libcxxabi";
  197. }
  198. auto InstallPaths::libc_path() const -> std::filesystem::path {
  199. // TODO: Adjust this to work equally well on Windows.
  200. return prefix_ / "lib/carbon/runtimes/libc";
  201. }
  202. auto InstallPaths::digest_path() const -> std::filesystem::path {
  203. // TODO: Adjust this to work equally well on Windows.
  204. return prefix_ / "lib/carbon/install_digest.txt";
  205. }
  206. } // namespace Carbon