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