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