install_paths.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef CARBON_TOOLCHAIN_INSTALL_INSTALL_PATHS_H_
  5. #define CARBON_TOOLCHAIN_INSTALL_INSTALL_PATHS_H_
  6. #include "common/error.h"
  7. #include "llvm/ADT/SmallString.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "toolchain/base/llvm_tools.h"
  11. namespace Carbon {
  12. // Locates the toolchain installation and provides paths to various components.
  13. //
  14. // The Carbon toolchain expects to be installed into some install prefix; see
  15. // `prefix_` for details. When locating an install, we verify it with
  16. // `CheckMarkerFile`. When errors occur, `SetError` makes `error()`
  17. // available for diagnostics and clears the install prefix (leaving things
  18. // minimally functional).
  19. //
  20. // The factory methods locate the install prefix based on their use-case:
  21. //
  22. // - `MakeExeRelative` for command line tools in an install.
  23. // - `MakeForBazelRunfiles` for locating through Bazel's runfile tree.
  24. // - `Make` for an explicit path, for example in tests.
  25. //
  26. // An instance of this class provides methods that query for specific paths
  27. // within the install. Note that we want to abstract away any platform
  28. // differences in the installation layout. When a specific part of the install
  29. // is needed, a dedicated accessor should be added that computes the path for
  30. // that component.
  31. //
  32. // TODO: Need to check the installation structure of LLVM on Windows and figure
  33. // out what Carbon's should be within a Windows prefix and how much of the
  34. // structure we can share with the Unix-y layout of the prefix.
  35. //
  36. // TODO: InstallPaths is typically called from places using a VFS (both tests
  37. // and the Driver), but does not use a VFS itself. It currently only supports
  38. // using the real filesystem, but should probably support a VFS.
  39. class InstallPaths {
  40. public:
  41. // Provide the current executable's path to detect the correct installation
  42. // prefix path. This assumes the toolchain to be in its installed layout.
  43. //
  44. // If detection fails, this reverts to using the current working directory as
  45. // the install prefix, and the error detected can be checked with `errors()`.
  46. static auto MakeExeRelative(llvm::StringRef exe_path) -> InstallPaths;
  47. // Provide the current executable's path, and use that to detect a Bazel or
  48. // Bazel-compatible runfiles install prefix path. This should only be used
  49. // where it is reasonable to rely on this rather than a fixed install location
  50. // such as for internal development purposes or other Bazel users of the
  51. // Carbon library.
  52. //
  53. // This method of construction also ensures the result is valid. If detection
  54. // fails for any reason, it will `CARBON_CHECK` fail with the error message.
  55. static auto MakeForBazelRunfiles(llvm::StringRef exe_path) -> InstallPaths;
  56. // Provide an explicit install paths prefix, which must be absolute. This is
  57. // useful for testing or for using Carbon in an environment with an unusual
  58. // path to the installed files.
  59. static auto Make(llvm::StringRef install_prefix) -> InstallPaths;
  60. // Returns the contents of the prelude manifest file. This is the list of
  61. // files that define the prelude, and will always be non-empty on success.
  62. auto ReadPreludeManifest() const -> ErrorOr<llvm::SmallVector<std::string>>;
  63. // Returns the contents of the clang builtin headers manifest file. This is
  64. // the list of header files that are installed as part of the clang compiler,
  65. // and will always be non-empty on success.
  66. auto ReadClangHeadersManifest() const
  67. -> ErrorOr<llvm::SmallVector<std::string>>;
  68. // Check for an error detecting the install paths correctly.
  69. //
  70. // A nullopt return means no errors encountered and the paths should work
  71. // correctly.
  72. //
  73. // A string return means there was an error, and details of the error are
  74. // in the `StringRef` for inclusion in any user report.
  75. [[nodiscard]] auto error() const -> std::optional<llvm::StringRef> {
  76. return error_;
  77. }
  78. // The directory containing the `Core` package. Computed on demand.
  79. auto core_package() const -> std::string;
  80. // The directory containing LLVM install binaries. Computed on demand.
  81. auto llvm_install_bin() const -> std::string;
  82. // The path to `clang`.
  83. auto clang_path() const -> std::string;
  84. // The path to `lld' and various aliases of `lld`.
  85. auto lld_path() const -> std::string;
  86. auto ld_lld_path() const -> std::string;
  87. auto ld64_lld_path() const -> std::string;
  88. // The path to any of the LLVM tools.
  89. auto llvm_tool_path(LLVMTool tool) const -> std::string;
  90. private:
  91. friend class InstallPathsTestPeer;
  92. InstallPaths() { SetError("No prefix provided!"); }
  93. explicit InstallPaths(llvm::StringRef prefix) : prefix_(prefix) {}
  94. // Set an error message on the install paths and reset the prefix to empty,
  95. // which should use the current working directory.
  96. auto SetError(llvm::Twine message) -> void;
  97. // Check that the install paths have a marker file at
  98. // `prefix()/lib/carbon/carbon_install.txt". If not, calls `SetError` with the
  99. // relevant error message.
  100. auto CheckMarkerFile() -> void;
  101. // Read a manifest file.
  102. auto ReadManifest(llvm::StringRef manifest_path,
  103. llvm::StringRef manifest_file) const
  104. -> ErrorOr<llvm::SmallVector<std::string>>;
  105. // The computed installation prefix. This will be an absolute path. We keep an
  106. // absolute path for when the command line uses a relative path
  107. // (`./bin/carbon`) and the working directory changes after initialization
  108. // (for example, to Bazel's working directory). In the event of an error, this
  109. // will be the empty string.
  110. //
  111. // When run from bazel (for example, in unit tests or development binaries)
  112. // this will look like:
  113. // `bazel-bin/some/bazel/target.runfiles/_main/toolchain/install/prefix_root`
  114. //
  115. // When installed, it's expected to be similar to the CMake install prefix:
  116. //
  117. // - `C:/Program Files/Carbon` or similar on Windows.
  118. // - `/usr` or `/usr/local` on Linux and most BSDs.
  119. // - `/opt/homebrew` or similar on macOS with Homebrew.
  120. //
  121. // See https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html
  122. // for more details. While we don't build the toolchain with CMake, we expect
  123. // our installation to behave in a similar and compatible way.
  124. //
  125. // The hierarchy of files beneath the install prefix can be found in the
  126. // BUILD's `install_dirs`.
  127. llvm::SmallString<256> prefix_;
  128. std::optional<std::string> error_;
  129. };
  130. } // namespace Carbon
  131. #endif // CARBON_TOOLCHAIN_INSTALL_INSTALL_PATHS_H_