install_paths.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // Check for an error detecting the install paths correctly.
  64. //
  65. // A nullopt return means no errors encountered and the paths should work
  66. // correctly.
  67. //
  68. // A string return means there was an error, and details of the error are
  69. // in the `StringRef` for inclusion in any user report.
  70. [[nodiscard]] auto error() const -> std::optional<llvm::StringRef> {
  71. return error_;
  72. }
  73. // The directory containing the `Core` package. Computed on demand.
  74. auto core_package() const -> std::string;
  75. // The directory containing LLVM install binaries. Computed on demand.
  76. auto llvm_install_bin() const -> std::string;
  77. // The path to `clang`.
  78. auto clang_path() const -> std::string;
  79. // The path to `lld' and various aliases of `lld`.
  80. auto lld_path() const -> std::string;
  81. auto ld_lld_path() const -> std::string;
  82. auto ld64_lld_path() const -> std::string;
  83. // The path to any of the LLVM tools.
  84. auto llvm_tool_path(LLVMTool tool) const -> std::string;
  85. private:
  86. friend class InstallPathsTestPeer;
  87. InstallPaths() { SetError("No prefix provided!"); }
  88. explicit InstallPaths(llvm::StringRef prefix) : prefix_(prefix) {}
  89. // Set an error message on the install paths and reset the prefix to empty,
  90. // which should use the current working directory.
  91. auto SetError(llvm::Twine message) -> void;
  92. // Check that the install paths have a marker file at
  93. // `prefix()/lib/carbon/carbon_install.txt". If not, calls `SetError` with the
  94. // relevant error message.
  95. auto CheckMarkerFile() -> void;
  96. // The computed installation prefix. This will be an absolute path. We keep an
  97. // absolute path for when the command line uses a relative path
  98. // (`./bin/carbon`) and the working directory changes after initialization
  99. // (for example, to Bazel's working directory). In the event of an error, this
  100. // will be the empty string.
  101. //
  102. // When run from bazel (for example, in unit tests or development binaries)
  103. // this will look like:
  104. // `bazel-bin/some/bazel/target.runfiles/_main/toolchain/install/prefix_root`
  105. //
  106. // When installed, it's expected to be similar to the CMake install prefix:
  107. //
  108. // - `C:/Program Files/Carbon` or similar on Windows.
  109. // - `/usr` or `/usr/local` on Linux and most BSDs.
  110. // - `/opt/homebrew` or similar on macOS with Homebrew.
  111. //
  112. // See https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html
  113. // for more details. While we don't build the toolchain with CMake, we expect
  114. // our installation to behave in a similar and compatible way.
  115. //
  116. // The hierarchy of files beneath the install prefix can be found in the
  117. // BUILD's `install_dirs`.
  118. llvm::SmallString<256> prefix_;
  119. std::optional<std::string> error_;
  120. };
  121. } // namespace Carbon
  122. #endif // CARBON_TOOLCHAIN_INSTALL_INSTALL_PATHS_H_