install_paths.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. paths.CheckMarkerFile();
  37. return paths;
  38. }
  39. auto InstallPaths::MakeForBazelRunfiles(llvm::StringRef exe_path)
  40. -> InstallPaths {
  41. using bazel::tools::cpp::runfiles::Runfiles;
  42. std::string runtimes_error;
  43. std::unique_ptr<Runfiles> runfiles(
  44. Runfiles::Create(exe_path.str(), &runtimes_error));
  45. CARBON_CHECK(runfiles != nullptr, "Failed to find runtimes tree: {0}",
  46. runtimes_error);
  47. std::string relative_marker_path = (PrefixRoot.str() + MarkerPath).str();
  48. std::string runtimes_marker_path = runfiles->Rlocation(relative_marker_path);
  49. // Start from the marker, remove that filename, and walk up to find the
  50. // install prefix.
  51. InstallPaths paths(runtimes_marker_path);
  52. llvm::sys::path::remove_filename(paths.prefix_);
  53. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix,
  54. "../../");
  55. paths.CheckMarkerFile();
  56. CARBON_CHECK(!paths.error(), "{0}", *paths.error());
  57. return paths;
  58. }
  59. auto InstallPaths::Make(llvm::StringRef install_prefix) -> InstallPaths {
  60. InstallPaths paths(install_prefix);
  61. paths.CheckMarkerFile();
  62. return paths;
  63. }
  64. auto InstallPaths::ReadPreludeManifest() const
  65. -> ErrorOr<llvm::SmallVector<std::string>> {
  66. // This is structured to avoid a vector copy on success.
  67. ErrorOr<llvm::SmallVector<std::string>> result =
  68. llvm::SmallVector<std::string>();
  69. llvm::SmallString<256> manifest;
  70. llvm::sys::path::append(manifest, llvm::sys::path::Style::posix,
  71. core_package(), "prelude_manifest.txt");
  72. auto fs = llvm::vfs::getRealFileSystem();
  73. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
  74. fs->getBufferForFile(manifest);
  75. if (!file) {
  76. result = ErrorBuilder() << "Loading prelude manifest `" << manifest
  77. << "`: " << file.getError().message();
  78. return result;
  79. }
  80. // The manifest should have one file per line.
  81. llvm::StringRef buffer = file.get()->getBuffer();
  82. while (true) {
  83. auto [token, remainder] = llvm::getToken(buffer, "\n");
  84. if (token.empty()) {
  85. break;
  86. }
  87. llvm::SmallString<256> path;
  88. llvm::sys::path::append(path, llvm::sys::path::Style::posix, core_package(),
  89. token);
  90. result->push_back(path.str().str());
  91. buffer = remainder;
  92. }
  93. if (result->empty()) {
  94. result = ErrorBuilder() << "Prelude manifest `" << manifest << "` is empty";
  95. }
  96. return result;
  97. }
  98. auto InstallPaths::SetError(llvm::Twine message) -> void {
  99. // Use an empty prefix on error as that should use the working directory which
  100. // is the least likely problematic.
  101. prefix_ = "";
  102. error_ = {message.str()};
  103. }
  104. auto InstallPaths::CheckMarkerFile() -> void {
  105. llvm::SmallString<256> path(prefix_);
  106. llvm::sys::path::append(path, llvm::sys::path::Style::posix, MarkerPath);
  107. if (!llvm::sys::fs::exists(path)) {
  108. SetError(llvm::Twine("No install marker at path: ") + path);
  109. }
  110. }
  111. auto InstallPaths::driver() const -> std::string {
  112. llvm::SmallString<256> path(prefix_);
  113. // TODO: Adjust this to work equally well on Windows.
  114. llvm::sys::path::append(path, llvm::sys::path::Style::posix, "bin/carbon");
  115. return path.str().str();
  116. }
  117. auto InstallPaths::core_package() const -> std::string {
  118. llvm::SmallString<256> path(prefix_);
  119. // TODO: Adjust this to work equally well on Windows.
  120. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  121. "lib/carbon/core");
  122. return path.str().str();
  123. }
  124. auto InstallPaths::llvm_install_bin() const -> std::string {
  125. llvm::SmallString<256> path(prefix_);
  126. // TODO: Adjust this to work equally well on Windows.
  127. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  128. "lib/carbon/llvm/bin/");
  129. return path.str().str();
  130. }
  131. } // namespace Carbon