Distro.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Driver/Distro.h"
  9. #include "clang/Basic/LLVM.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/ADT/StringSwitch.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/Support/ErrorOr.h"
  15. #include "llvm/Support/Host.h"
  16. #include "llvm/Support/MemoryBuffer.h"
  17. #include "llvm/Support/Threading.h"
  18. using namespace clang::driver;
  19. using namespace clang;
  20. static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
  21. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
  22. VFS.getBufferForFile("/etc/os-release");
  23. if (!File)
  24. File = VFS.getBufferForFile("/usr/lib/os-release");
  25. if (!File)
  26. return Distro::UnknownDistro;
  27. SmallVector<StringRef, 16> Lines;
  28. File.get()->getBuffer().split(Lines, "\n");
  29. Distro::DistroType Version = Distro::UnknownDistro;
  30. // Obviously this can be improved a lot.
  31. for (StringRef Line : Lines)
  32. if (Version == Distro::UnknownDistro && Line.startswith("ID="))
  33. Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(3))
  34. .Case("alpine", Distro::AlpineLinux)
  35. .Case("fedora", Distro::Fedora)
  36. .Case("gentoo", Distro::Gentoo)
  37. .Case("arch", Distro::ArchLinux)
  38. // On SLES, /etc/os-release was introduced in SLES 11.
  39. .Case("sles", Distro::OpenSUSE)
  40. .Case("opensuse", Distro::OpenSUSE)
  41. .Case("exherbo", Distro::Exherbo)
  42. .Default(Distro::UnknownDistro);
  43. return Version;
  44. }
  45. static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
  46. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
  47. VFS.getBufferForFile("/etc/lsb-release");
  48. if (!File)
  49. return Distro::UnknownDistro;
  50. SmallVector<StringRef, 16> Lines;
  51. File.get()->getBuffer().split(Lines, "\n");
  52. Distro::DistroType Version = Distro::UnknownDistro;
  53. for (StringRef Line : Lines)
  54. if (Version == Distro::UnknownDistro &&
  55. Line.startswith("DISTRIB_CODENAME="))
  56. Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
  57. .Case("hardy", Distro::UbuntuHardy)
  58. .Case("intrepid", Distro::UbuntuIntrepid)
  59. .Case("jaunty", Distro::UbuntuJaunty)
  60. .Case("karmic", Distro::UbuntuKarmic)
  61. .Case("lucid", Distro::UbuntuLucid)
  62. .Case("maverick", Distro::UbuntuMaverick)
  63. .Case("natty", Distro::UbuntuNatty)
  64. .Case("oneiric", Distro::UbuntuOneiric)
  65. .Case("precise", Distro::UbuntuPrecise)
  66. .Case("quantal", Distro::UbuntuQuantal)
  67. .Case("raring", Distro::UbuntuRaring)
  68. .Case("saucy", Distro::UbuntuSaucy)
  69. .Case("trusty", Distro::UbuntuTrusty)
  70. .Case("utopic", Distro::UbuntuUtopic)
  71. .Case("vivid", Distro::UbuntuVivid)
  72. .Case("wily", Distro::UbuntuWily)
  73. .Case("xenial", Distro::UbuntuXenial)
  74. .Case("yakkety", Distro::UbuntuYakkety)
  75. .Case("zesty", Distro::UbuntuZesty)
  76. .Case("artful", Distro::UbuntuArtful)
  77. .Case("bionic", Distro::UbuntuBionic)
  78. .Case("cosmic", Distro::UbuntuCosmic)
  79. .Case("disco", Distro::UbuntuDisco)
  80. .Case("eoan", Distro::UbuntuEoan)
  81. .Case("focal", Distro::UbuntuFocal)
  82. .Case("groovy", Distro::UbuntuGroovy)
  83. .Case("hirsute", Distro::UbuntuHirsute)
  84. .Case("impish", Distro::UbuntuImpish)
  85. .Case("jammy", Distro::UbuntuJammy)
  86. .Default(Distro::UnknownDistro);
  87. return Version;
  88. }
  89. static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
  90. Distro::DistroType Version = Distro::UnknownDistro;
  91. // Newer freedesktop.org's compilant systemd-based systems
  92. // should provide /etc/os-release or /usr/lib/os-release.
  93. Version = DetectOsRelease(VFS);
  94. if (Version != Distro::UnknownDistro)
  95. return Version;
  96. // Older systems might provide /etc/lsb-release.
  97. Version = DetectLsbRelease(VFS);
  98. if (Version != Distro::UnknownDistro)
  99. return Version;
  100. // Otherwise try some distro-specific quirks for RedHat...
  101. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
  102. VFS.getBufferForFile("/etc/redhat-release");
  103. if (File) {
  104. StringRef Data = File.get()->getBuffer();
  105. if (Data.startswith("Fedora release"))
  106. return Distro::Fedora;
  107. if (Data.startswith("Red Hat Enterprise Linux") ||
  108. Data.startswith("CentOS") || Data.startswith("Scientific Linux")) {
  109. if (Data.contains("release 7"))
  110. return Distro::RHEL7;
  111. else if (Data.contains("release 6"))
  112. return Distro::RHEL6;
  113. else if (Data.contains("release 5"))
  114. return Distro::RHEL5;
  115. }
  116. return Distro::UnknownDistro;
  117. }
  118. // ...for Debian
  119. File = VFS.getBufferForFile("/etc/debian_version");
  120. if (File) {
  121. StringRef Data = File.get()->getBuffer();
  122. // Contents: < major.minor > or < codename/sid >
  123. int MajorVersion;
  124. if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
  125. switch (MajorVersion) {
  126. case 5:
  127. return Distro::DebianLenny;
  128. case 6:
  129. return Distro::DebianSqueeze;
  130. case 7:
  131. return Distro::DebianWheezy;
  132. case 8:
  133. return Distro::DebianJessie;
  134. case 9:
  135. return Distro::DebianStretch;
  136. case 10:
  137. return Distro::DebianBuster;
  138. case 11:
  139. return Distro::DebianBullseye;
  140. case 12:
  141. return Distro::DebianBookworm;
  142. default:
  143. return Distro::UnknownDistro;
  144. }
  145. }
  146. return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
  147. .Case("squeeze/sid", Distro::DebianSqueeze)
  148. .Case("wheezy/sid", Distro::DebianWheezy)
  149. .Case("jessie/sid", Distro::DebianJessie)
  150. .Case("stretch/sid", Distro::DebianStretch)
  151. .Case("buster/sid", Distro::DebianBuster)
  152. .Case("bullseye/sid", Distro::DebianBullseye)
  153. .Case("bookworm/sid", Distro::DebianBookworm)
  154. .Default(Distro::UnknownDistro);
  155. }
  156. // ...for SUSE
  157. File = VFS.getBufferForFile("/etc/SuSE-release");
  158. if (File) {
  159. StringRef Data = File.get()->getBuffer();
  160. SmallVector<StringRef, 8> Lines;
  161. Data.split(Lines, "\n");
  162. for (const StringRef &Line : Lines) {
  163. if (!Line.trim().startswith("VERSION"))
  164. continue;
  165. std::pair<StringRef, StringRef> SplitLine = Line.split('=');
  166. // Old versions have split VERSION and PATCHLEVEL
  167. // Newer versions use VERSION = x.y
  168. std::pair<StringRef, StringRef> SplitVer =
  169. SplitLine.second.trim().split('.');
  170. int Version;
  171. // OpenSUSE/SLES 10 and older are not supported and not compatible
  172. // with our rules, so just treat them as Distro::UnknownDistro.
  173. if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
  174. return Distro::OpenSUSE;
  175. return Distro::UnknownDistro;
  176. }
  177. return Distro::UnknownDistro;
  178. }
  179. // ...and others.
  180. if (VFS.exists("/etc/gentoo-release"))
  181. return Distro::Gentoo;
  182. return Distro::UnknownDistro;
  183. }
  184. static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
  185. const llvm::Triple &TargetOrHost) {
  186. // If we don't target Linux, no need to check the distro. This saves a few
  187. // OS calls.
  188. if (!TargetOrHost.isOSLinux())
  189. return Distro::UnknownDistro;
  190. // True if we're backed by a real file system.
  191. const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);
  192. // If the host is not running Linux, and we're backed by a real file
  193. // system, no need to check the distro. This is the case where someone
  194. // is cross-compiling from BSD or Windows to Linux, and it would be
  195. // meaningless to try to figure out the "distro" of the non-Linux host.
  196. llvm::Triple HostTriple(llvm::sys::getProcessTriple());
  197. if (!HostTriple.isOSLinux() && onRealFS)
  198. return Distro::UnknownDistro;
  199. if (onRealFS) {
  200. // If we're backed by a real file system, perform
  201. // the detection only once and save the result.
  202. static Distro::DistroType LinuxDistro = DetectDistro(VFS);
  203. return LinuxDistro;
  204. }
  205. // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
  206. // which is not "real".
  207. return DetectDistro(VFS);
  208. }
  209. Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
  210. : DistroVal(GetDistro(VFS, TargetOrHost)) {}