install_paths.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "_main/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::SetError(llvm::Twine message) -> void {
  63. // Use an empty prefix on error as that should use the working directory which
  64. // is the least likely problematic.
  65. prefix_ = "";
  66. error_ = {message.str()};
  67. }
  68. auto InstallPaths::CheckMarkerFile() -> void {
  69. llvm::SmallString<256> path(prefix_);
  70. llvm::sys::path::append(path, llvm::sys::path::Style::posix, MarkerPath);
  71. if (!llvm::sys::fs::exists(path)) {
  72. SetError(llvm::Twine("No install marker at path: ") + path);
  73. }
  74. }
  75. auto InstallPaths::driver() const -> std::string {
  76. llvm::SmallString<256> path(prefix_);
  77. // TODO: Adjust this to work equally well on Windows.
  78. llvm::sys::path::append(path, llvm::sys::path::Style::posix, "bin/carbon");
  79. return path.str().str();
  80. }
  81. auto InstallPaths::llvm_install_bin() const -> std::string {
  82. llvm::SmallString<256> path(prefix_);
  83. // TODO: Adjust this to work equally well on Windows.
  84. llvm::sys::path::append(path, llvm::sys::path::Style::posix,
  85. "lib/carbon/llvm/bin/");
  86. return path.str().str();
  87. }
  88. } // namespace Carbon