install_paths.h 5.8 KB

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