install_paths.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "llvm/ADT/SmallString.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "llvm/ADT/Twine.h"
  9. namespace Carbon {
  10. // Locates the toolchain installation and provides paths to various components.
  11. //
  12. // The Carbon toolchain expects to be installed into some install prefix. For
  13. // example, this is expected to be similar to the CMake install prefix:
  14. //
  15. // - `C:/Program Files/Carbon` or similar on Windows.
  16. // - `/usr` or `/usr/local` on Linux and most BSDs.
  17. // - `/opt/homebrew` or similar on macOS with Homebrew.
  18. // - `bazel-bin/some/bazel/target.runfiles/_main/toolchain/install/prefix_root`
  19. // for unit tests and just-built binaries during development.
  20. //
  21. // See https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html
  22. // for more details. While we don't build the toolchain with CMake, we expect
  23. // our installation to behave in a similar and compatible way.
  24. //
  25. // There are multiple ways of locating an install's prefix:
  26. // - MakeExeRelative for command line tools in an install.
  27. // - MakeForBazelRunfiles for locating through Bazel's runfile tree.
  28. // - Make for an explicit path, for example in tests.
  29. //
  30. // When locating an install, we verify it by
  31. // looking for the `carbon_install.txt` marker file at a specific location
  32. // below. When errors occur, the install prefix is made empty, and error() can
  33. // be used for diagnostics; InstallPaths remains minimally functional.
  34. //
  35. // Within this prefix, we expect a hierarchy on Unix-y platforms:
  36. //
  37. // - `prefix_root/bin/carbon` - the main CLI driver
  38. // - `prefix_root/lib/carbon/carbon_install.txt` - a marker for the install
  39. // - `prefix_root/lib/carbon/...` - private data & binaries
  40. //
  41. // This is loosely based on the FHS (Filesystem Hierarchy Standard).
  42. //
  43. // An instance of this class provides methods that query for specific paths
  44. // within the install. Note that we want to abstract away any platform
  45. // differences in the installation layout, and so while there are some broad
  46. // paths available here, like the `prefix` method, those should primarily be
  47. // used for logging or debugging. When a specific part of the install is needed,
  48. // a dedicated accessor should be added that computes the path for that
  49. // component.
  50. //
  51. // Path accessor methods on the class return `llvm::StringRef` for any paths
  52. // that are stored in the class, and a `std::string` for any that are computed
  53. // on demand.
  54. //
  55. // TODO: Need to check the installation structure of LLVM on Windows and figure
  56. // out what Carbon's should be within a Windows prefix and how much of the
  57. // structure we can share with the Unix-y layout of the prefix.
  58. class InstallPaths {
  59. public:
  60. // Provide the current executable's path to detect the correct installation
  61. // prefix path. This assumes the toolchain to be in its installed layout.
  62. //
  63. // If detection fails, this reverts to using the current working directory as
  64. // the install prefix, and the error detected can be checked with `errors()`.
  65. static auto MakeExeRelative(llvm::StringRef exe_path) -> InstallPaths;
  66. // Provide the current executable's path, and use that to detect a Bazel or
  67. // Bazel-compatible runfiles install prefix path. This should only be used
  68. // where it is reasonable to rely on this rather than a fixed install location
  69. // such as for internal development purposes or other Bazel users of the
  70. // Carbon library.
  71. //
  72. // This method of construction also ensures the result is valid. If detection
  73. // fails for any reason, it will `CARBON_CHECK` fail with the error message.
  74. static auto MakeForBazelRunfiles(llvm::StringRef exe_path) -> InstallPaths;
  75. // Provide an explicit install paths prefix. This is useful for testing or for
  76. // using Carbon in an environment with an unusual path to the installed files.
  77. static auto Make(llvm::StringRef install_prefix) -> InstallPaths;
  78. // Check for an error detecting the install paths correctly.
  79. //
  80. // A nullopt return means no errors encountered and the paths should work
  81. // correctly.
  82. //
  83. // A string return means there was an error, and details of the error are
  84. // in the `StringRef` for inclusion in any user report.
  85. [[nodiscard]] auto error() const -> std::optional<llvm::StringRef> {
  86. return error_;
  87. }
  88. // The computed installation prefix. This should correspond to the
  89. // `prefix_root` directory in Bazel's output, or to some prefix the toolchain
  90. // is installed into on a system such as `/usr/local` or `/home/$USER`.
  91. //
  92. // In the event of an error, this will be the empty string.
  93. auto prefix() const -> llvm::StringRef { return prefix_; }
  94. auto driver() const -> std::string;
  95. auto core_package() const -> std::string;
  96. auto llvm_install_bin() const -> std::string;
  97. private:
  98. InstallPaths() { SetError("No prefix provided!"); }
  99. explicit InstallPaths(llvm::StringRef prefix) : prefix_(prefix) {}
  100. // Set an error message on the install paths and reset the prefix to empty,
  101. // which should use the current working directory.
  102. auto SetError(llvm::Twine message) -> void;
  103. // Check that the install paths have a marker file at the expected location,
  104. // and if not calls `SetError` with the relevant error message.
  105. auto CheckMarkerFile() -> void;
  106. llvm::SmallString<256> prefix_;
  107. std::optional<std::string> error_;
  108. };
  109. } // namespace Carbon
  110. #endif // CARBON_TOOLCHAIN_INSTALL_INSTALL_PATHS_H_