install_paths.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/StringRef.h"
  8. #include "llvm/Support/FileSystem.h"
  9. #include "llvm/Support/Path.h"
  10. #include "tools/cpp/runfiles/runfiles.h"
  11. namespace Carbon {
  12. // The location within our Bazel output tree of the prefix_root.
  13. static constexpr llvm::StringLiteral PrefixRoot =
  14. "carbon/toolchain/install/prefix_root/";
  15. // Path within an install prefix for our marker of a valid install.
  16. static constexpr llvm::StringLiteral MarkerPath =
  17. "lib/carbon/carbon_install.txt";
  18. auto InstallPaths::MakeExeRelative(llvm::StringRef exe_path) -> InstallPaths {
  19. InstallPaths paths;
  20. // Map from the executable path from the executable path to an install
  21. // prefix path.
  22. if (!llvm::sys::fs::exists(exe_path)) {
  23. paths.SetError(llvm::Twine("No file at executable path: ") + exe_path);
  24. return paths;
  25. }
  26. paths = InstallPaths(exe_path);
  27. // TODO: Detect a Windows executable path and use custom logic to map to the
  28. // correct install prefix for that platform.
  29. // We assume an executable will be in a `bin` directory and this is a
  30. // FHS-like install prefix. We remove the filename and walk up to find the
  31. // expected install prefix.
  32. llvm::sys::path::remove_filename(paths.prefix_);
  33. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix, "../");
  34. paths.CheckMarkerFile();
  35. return paths;
  36. }
  37. auto InstallPaths::MakeForBazelRunfiles(llvm::StringRef exe_path)
  38. -> InstallPaths {
  39. using bazel::tools::cpp::runfiles::Runfiles;
  40. std::string runtimes_error;
  41. std::unique_ptr<Runfiles> runfiles(
  42. Runfiles::Create(exe_path.str(), &runtimes_error));
  43. CARBON_CHECK(runfiles != nullptr)
  44. << "Failed to find runtimes tree: " << runtimes_error;
  45. std::string relative_marker_path = (PrefixRoot.str() + MarkerPath).str();
  46. std::string runtimes_marker_path = runfiles->Rlocation(relative_marker_path);
  47. // Start from the marker, remove that filename, and walk up to find the
  48. // install prefix.
  49. InstallPaths paths(runtimes_marker_path);
  50. llvm::sys::path::remove_filename(paths.prefix_);
  51. llvm::sys::path::append(paths.prefix_, llvm::sys::path::Style::posix,
  52. "../../");
  53. paths.CheckMarkerFile();
  54. CARBON_CHECK(!paths.error()) << *paths.error();
  55. return paths;
  56. }
  57. auto InstallPaths::Make(llvm::StringRef install_prefix) -> InstallPaths {
  58. InstallPaths paths(install_prefix);
  59. paths.CheckMarkerFile();
  60. return paths;
  61. }
  62. auto InstallPaths::FindPreludeFiles() const
  63. -> ErrorOr<llvm::SmallVector<std::string>> {
  64. // This is structured to avoid a vector copy on success.
  65. ErrorOr<llvm::SmallVector<std::string>> result =
  66. llvm::SmallVector<std::string>();
  67. std::string dir = core_package();
  68. // Include <data>/core/prelude.carbon, which is the entry point into the
  69. // prelude.
  70. {
  71. llvm::SmallString<256> prelude_file(dir);
  72. llvm::sys::path::append(prelude_file, llvm::sys::path::Style::posix,
  73. "prelude.carbon");
  74. result->push_back(prelude_file.str().str());
  75. }
  76. // Glob for <data>/core/prelude/**/*.carbon and add all the files we find.
  77. llvm::SmallString<256> prelude_dir(dir);
  78. llvm::sys::path::append(prelude_dir, llvm::sys::path::Style::posix,
  79. "prelude");
  80. std::error_code ec;
  81. for (llvm::sys::fs::recursive_directory_iterator prelude_files_it(
  82. prelude_dir, ec, /*follow_symlinks=*/false);
  83. prelude_files_it != llvm::sys::fs::recursive_directory_iterator();
  84. prelude_files_it.increment(ec)) {
  85. if (ec) {
  86. result = ErrorBuilder() << "Could not find prelude: " << ec.message();
  87. return result;
  88. }
  89. auto prelude_file = prelude_files_it->path();
  90. if (llvm::sys::path::extension(prelude_file) == ".carbon") {
  91. result->push_back(prelude_file);
  92. }
  93. }
  94. return result;
  95. }
  96. auto InstallPaths::SetError(llvm::Twine message) -> void {
  97. // Use an empty prefix on error as that should use the working directory which
  98. // is the least likely problematic.
  99. prefix_ = "";
  100. error_ = {message.str()};
  101. }
  102. auto InstallPaths::CheckMarkerFile() -> void {
  103. llvm::SmallString<256> path(prefix_);
  104. llvm::sys::path::append(path, llvm::sys::path::Style::posix, MarkerPath);
  105. if (!llvm::sys::fs::exists(path)) {
  106. SetError(llvm::Twine("No install marker at path: ") + path);
  107. }
  108. }
  109. auto InstallPaths::driver() const -> std::string {
  110. llvm::SmallString<256> path(prefix_);
  111. // TODO: Adjust this to work equally well on Windows.
  112. llvm::sys::path::append(path, llvm::sys::path::Style::posix, "bin/carbon");
  113. return path.str().str();
  114. }
  115. auto InstallPaths::core_package() const -> std::string {
  116. llvm::SmallString<256> path(prefix_);
  117. // TODO: Adjust this to work equally well on Windows.
  118. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  119. "lib/carbon/core");
  120. return path.str().str();
  121. }
  122. auto InstallPaths::llvm_install_bin() 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/llvm/bin/");
  127. return path.str().str();
  128. }
  129. } // namespace Carbon