install_paths.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <memory>
  6. #include "common/check.h"
  7. #include "llvm/ADT/StringExtras.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/Path.h"
  11. #include "llvm/Support/VirtualFileSystem.h"
  12. #include "tools/cpp/runfiles/runfiles.h"
  13. namespace Carbon {
  14. // The location within our Bazel output tree of the prefix_root.
  15. static constexpr llvm::StringLiteral PrefixRoot =
  16. "carbon/toolchain/install/prefix_root/";
  17. // Path within an install prefix for our marker of a valid install.
  18. static constexpr llvm::StringLiteral MarkerPath =
  19. "lib/carbon/carbon_install.txt";
  20. auto InstallPaths::MakeExeRelative(llvm::StringRef exe_path) -> InstallPaths {
  21. InstallPaths paths;
  22. // Map from the executable path from the executable path to an install
  23. // prefix path.
  24. if (!llvm::sys::fs::exists(exe_path)) {
  25. paths.SetError(llvm::Twine("No file at executable path: ") + exe_path);
  26. return paths;
  27. }
  28. paths = InstallPaths(exe_path);
  29. // TODO: Detect a Windows executable path and use custom logic to map to the
  30. // correct install prefix for that platform.
  31. // We assume an executable will be in a `bin` directory and this is a
  32. // FHS-like install prefix. We remove the filename and walk up to find the
  33. // expected install prefix.
  34. llvm::sys::path::remove_filename(paths.prefix_);
  35. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix, "../");
  36. if (auto error = llvm::sys::fs::make_absolute(paths.prefix_)) {
  37. paths.SetError(error.message());
  38. return paths;
  39. }
  40. paths.CheckMarkerFile();
  41. return paths;
  42. }
  43. auto InstallPaths::MakeForBazelRunfiles(llvm::StringRef exe_path)
  44. -> InstallPaths {
  45. using bazel::tools::cpp::runfiles::Runfiles;
  46. std::string runtimes_error;
  47. std::unique_ptr<Runfiles> runfiles(
  48. Runfiles::Create(exe_path.str(), &runtimes_error));
  49. CARBON_CHECK(runfiles != nullptr, "Failed to find runtimes tree: {0}",
  50. runtimes_error);
  51. std::string relative_marker_path = (PrefixRoot.str() + MarkerPath).str();
  52. std::string runtimes_marker_path = runfiles->Rlocation(relative_marker_path);
  53. // Start from the marker, remove that filename, and walk up to find the
  54. // install prefix.
  55. InstallPaths paths(runtimes_marker_path);
  56. llvm::sys::path::remove_filename(paths.prefix_);
  57. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix,
  58. "../../");
  59. if (auto error = llvm::sys::fs::make_absolute(paths.prefix_)) {
  60. paths.SetError(error.message());
  61. return paths;
  62. }
  63. paths.CheckMarkerFile();
  64. CARBON_CHECK(!paths.error(), "{0}", *paths.error());
  65. return paths;
  66. }
  67. auto InstallPaths::Make(llvm::StringRef install_prefix) -> InstallPaths {
  68. InstallPaths paths(install_prefix);
  69. paths.CheckMarkerFile();
  70. return paths;
  71. }
  72. auto InstallPaths::ReadPreludeManifest() 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. llvm::SmallString<256> manifest;
  78. llvm::sys::path::append(manifest, llvm::sys::path::Style::posix,
  79. core_package(), "prelude_manifest.txt");
  80. auto fs = llvm::vfs::getRealFileSystem();
  81. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
  82. fs->getBufferForFile(manifest);
  83. if (!file) {
  84. result = ErrorBuilder() << "Loading prelude manifest `" << manifest
  85. << "`: " << file.getError().message();
  86. return result;
  87. }
  88. // The manifest should have one file per line.
  89. llvm::StringRef buffer = file.get()->getBuffer();
  90. while (true) {
  91. auto [token, remainder] = llvm::getToken(buffer, "\n");
  92. if (token.empty()) {
  93. break;
  94. }
  95. llvm::SmallString<256> path;
  96. llvm::sys::path::append(path, llvm::sys::path::Style::posix, core_package(),
  97. token);
  98. result->push_back(path.str().str());
  99. buffer = remainder;
  100. }
  101. if (result->empty()) {
  102. result = ErrorBuilder() << "Prelude manifest `" << manifest << "` is empty";
  103. }
  104. return result;
  105. }
  106. auto InstallPaths::SetError(llvm::Twine message) -> void {
  107. // Use an empty prefix on error as that should use the working directory which
  108. // is the least likely problematic.
  109. prefix_ = "";
  110. error_ = {message.str()};
  111. }
  112. auto InstallPaths::CheckMarkerFile() -> void {
  113. if (!llvm::sys::path::is_absolute(prefix_)) {
  114. SetError(llvm::Twine("Not an absolute path: ") + prefix_);
  115. }
  116. llvm::SmallString<256> path(prefix_);
  117. llvm::sys::path::append(path, llvm::sys::path::Style::posix, MarkerPath);
  118. if (!llvm::sys::fs::exists(path)) {
  119. SetError(llvm::Twine("No install marker at path: ") + path);
  120. }
  121. }
  122. auto InstallPaths::core_package() const -> std::string {
  123. llvm::SmallString<256> path(prefix_);
  124. // TODO: Adjust this to work equally well on Windows.
  125. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  126. "lib/carbon/core");
  127. return path.str().str();
  128. }
  129. auto InstallPaths::llvm_install_bin() const -> std::string {
  130. llvm::SmallString<256> path(prefix_);
  131. // TODO: Adjust this to work equally well on Windows.
  132. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  133. "lib/carbon/llvm/bin/");
  134. return path.str().str();
  135. }
  136. } // namespace Carbon