install_paths.cpp 5.8 KB

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