clang 24.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("quantal", Distro::UbuntuQuantal)
65 .Case("raring", Distro::UbuntuRaring)
66 .Case("saucy", Distro::UbuntuSaucy)
67 .Case("trusty", Distro::UbuntuTrusty)
68 .Case("utopic", Distro::UbuntuUtopic)
69 .Case("vivid", Distro::UbuntuVivid)
70 .Case("wily", Distro::UbuntuWily)
71 .Case("xenial", Distro::UbuntuXenial)
72 .Case("yakkety", Distro::UbuntuYakkety)
73 .Case("zesty", Distro::UbuntuZesty)
74 .Case("artful", Distro::UbuntuArtful)
75 .Case("bionic", Distro::UbuntuBionic)
76 .Case("cosmic", Distro::UbuntuCosmic)
77 .Case("disco", Distro::UbuntuDisco)
78 .Case("eoan", Distro::UbuntuEoan)
79 .Case("focal", Distro::UbuntuFocal)
80 .Case("groovy", Distro::UbuntuGroovy)
81 .Case("hirsute", Distro::UbuntuHirsute)
82 .Case("impish", Distro::UbuntuImpish)
83 .Case("jammy", Distro::UbuntuJammy)
84 .Case("kinetic", Distro::UbuntuKinetic)
85 .Case("lunar", Distro::UbuntuLunar)
86 .Case("mantic", Distro::UbuntuMantic)
87 .Case("noble", Distro::UbuntuNoble)
88 .Case("oracular", Distro::UbuntuOracular)
89 .Case("plucky", Distro::UbuntuPlucky)
90 .Case("questing", Distro::UbuntuQuesting)
91 .Case("resolute", Distro::UbuntuResolute)
92 .Case("stonking", Distro::UbuntuStonking)
93 .Case("tenacious", Distro::UbuntuTenacious)
94 .Default(Distro::UnknownDistro);
95 return Version;
96}
97
98static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
100
101 // Newer freedesktop.org's compilant systemd-based systems
102 // should provide /etc/os-release or /usr/lib/os-release.
103 Version = DetectOsRelease(VFS);
104 if (Version != Distro::UnknownDistro)
105 return Version;
106
107 // Older systems might provide /etc/lsb-release.
108 Version = DetectLsbRelease(VFS);
109 if (Version != Distro::UnknownDistro)
110 return Version;
111
112 // Otherwise try some distro-specific quirks for Red Hat...
113 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
114 VFS.getBufferForFile("/etc/redhat-release");
115
116 if (File) {
117 StringRef Data = File.get()->getBuffer();
118 if (Data.starts_with("Fedora release"))
119 return Distro::Fedora;
120 if (Data.starts_with("Red Hat Enterprise Linux") ||
121 Data.starts_with("CentOS") || Data.starts_with("AlmaLinux") ||
122 Data.starts_with("Rocky Linux") ||
123 Data.starts_with("Scientific Linux")) {
124 if (Data.contains("release 10"))
125 return Distro::RHEL10;
126 if (Data.contains("release 9"))
127 return Distro::RHEL9;
128 if (Data.contains("release 8"))
129 return Distro::RHEL8;
130 if (Data.contains("release 7"))
131 return Distro::RHEL7;
132 }
134 }
135
136 // ...for Debian
137 File = VFS.getBufferForFile("/etc/debian_version");
138 if (File) {
139 StringRef Data = File.get()->getBuffer();
140 // Contents: < major.minor > or < codename/sid >
141 int MajorVersion;
142 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
143 switch (MajorVersion) {
144 case 8:
146 case 9:
148 case 10:
150 case 11:
152 case 12:
154 case 13:
156 case 14:
157 return Distro::DebianForky;
158 case 15:
159 return Distro::DebianDuke;
160 default:
162 }
163 }
164 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
165 .Case("jessie/sid", Distro::DebianJessie)
166 .Case("stretch/sid", Distro::DebianStretch)
167 .Case("buster/sid", Distro::DebianBuster)
168 .Case("bullseye/sid", Distro::DebianBullseye)
169 .Case("bookworm/sid", Distro::DebianBookworm)
170 .Case("trixie/sid", Distro::DebianTrixie)
171 .Case("forky/sid", Distro::DebianForky)
172 .Case("duke/sid", Distro::DebianDuke)
173 .Default(Distro::UnknownDistro);
174 }
175
176 // ...for SUSE
177 File = VFS.getBufferForFile("/etc/SuSE-release");
178 if (File) {
179 StringRef Data = File.get()->getBuffer();
181 Data.split(Lines, "\n");
182 for (const StringRef &Line : Lines) {
183 if (!Line.trim().starts_with("VERSION"))
184 continue;
185 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
186 // Old versions have split VERSION and PATCHLEVEL
187 // Newer versions use VERSION = x.y
188 std::pair<StringRef, StringRef> SplitVer =
189 SplitLine.second.trim().split('.');
190 int Version;
191
192 // OpenSUSE/SLES 10 and older are not supported and not compatible
193 // with our rules, so just treat them as Distro::UnknownDistro.
194 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
195 return Distro::OpenSUSE;
197 }
199 }
200
201 // ...and others.
202 if (VFS.exists("/etc/gentoo-release"))
203 return Distro::Gentoo;
204
206}
207
208static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
209 const llvm::Triple &TargetOrHost) {
210 // If we don't target Linux, no need to check the distro. This saves a few
211 // OS calls.
212 if (!TargetOrHost.isOSLinux())
214
215 // True if we're backed by a real file system.
216 const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);
217
218 // If the host is not running Linux, and we're backed by a real file
219 // system, no need to check the distro. This is the case where someone
220 // is cross-compiling from BSD or Windows to Linux, and it would be
221 // meaningless to try to figure out the "distro" of the non-Linux host.
222 llvm::Triple HostTriple(llvm::sys::getProcessTriple());
223 if (!HostTriple.isOSLinux() && onRealFS)
225
226 if (onRealFS) {
227 // If we're backed by a real file system, perform
228 // the detection only once and save the result.
229 static const Distro::DistroType LinuxDistro = DetectDistro(VFS);
230 return LinuxDistro;
231 }
232 // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
233 // which is not "real".
234 return DetectDistro(VFS);
235}
236
237Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
238 : 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:98
static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
Definition Distro.cpp:208
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Distro()
Default constructor leaves the distribution unknown.
Definition Distro.h:91
Top level wrappers for InstallAPI frontend operations.