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