1 //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
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
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/Support/ErrorOr.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/ADT/Triple.h"
18 using namespace clang::driver;
19 using namespace clang;
21 static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS,
22 const llvm::Triple &TargetOrHost) {
23 // If we don't target Linux, no need to check the distro. This saves a few
25 if (!TargetOrHost.isOSLinux())
26 return Distro::UnknownDistro;
28 // If the host is not running Linux, and we're backed by a real file system,
29 // no need to check the distro. This is the case where someone is
30 // cross-compiling from BSD or Windows to Linux, and it would be meaningless
31 // to try to figure out the "distro" of the non-Linux host.
32 IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS =
33 llvm::vfs::getRealFileSystem();
34 llvm::Triple HostTriple(llvm::sys::getProcessTriple());
35 if (!HostTriple.isOSLinux() && &VFS == RealFS.get())
36 return Distro::UnknownDistro;
38 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
39 VFS.getBufferForFile("/etc/lsb-release");
41 StringRef Data = File.get()->getBuffer();
42 SmallVector<StringRef, 16> Lines;
43 Data.split(Lines, "\n");
44 Distro::DistroType Version = Distro::UnknownDistro;
45 for (StringRef Line : Lines)
46 if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
47 Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
48 .Case("hardy", Distro::UbuntuHardy)
49 .Case("intrepid", Distro::UbuntuIntrepid)
50 .Case("jaunty", Distro::UbuntuJaunty)
51 .Case("karmic", Distro::UbuntuKarmic)
52 .Case("lucid", Distro::UbuntuLucid)
53 .Case("maverick", Distro::UbuntuMaverick)
54 .Case("natty", Distro::UbuntuNatty)
55 .Case("oneiric", Distro::UbuntuOneiric)
56 .Case("precise", Distro::UbuntuPrecise)
57 .Case("quantal", Distro::UbuntuQuantal)
58 .Case("raring", Distro::UbuntuRaring)
59 .Case("saucy", Distro::UbuntuSaucy)
60 .Case("trusty", Distro::UbuntuTrusty)
61 .Case("utopic", Distro::UbuntuUtopic)
62 .Case("vivid", Distro::UbuntuVivid)
63 .Case("wily", Distro::UbuntuWily)
64 .Case("xenial", Distro::UbuntuXenial)
65 .Case("yakkety", Distro::UbuntuYakkety)
66 .Case("zesty", Distro::UbuntuZesty)
67 .Case("artful", Distro::UbuntuArtful)
68 .Case("bionic", Distro::UbuntuBionic)
69 .Case("cosmic", Distro::UbuntuCosmic)
70 .Case("disco", Distro::UbuntuDisco)
71 .Case("eoan", Distro::UbuntuEoan)
72 .Case("focal", Distro::UbuntuFocal)
73 .Default(Distro::UnknownDistro);
74 if (Version != Distro::UnknownDistro)
78 File = VFS.getBufferForFile("/etc/redhat-release");
80 StringRef Data = File.get()->getBuffer();
81 if (Data.startswith("Fedora release"))
82 return Distro::Fedora;
83 if (Data.startswith("Red Hat Enterprise Linux") ||
84 Data.startswith("CentOS") ||
85 Data.startswith("Scientific Linux")) {
86 if (Data.find("release 7") != StringRef::npos)
88 else if (Data.find("release 6") != StringRef::npos)
90 else if (Data.find("release 5") != StringRef::npos)
93 return Distro::UnknownDistro;
96 File = VFS.getBufferForFile("/etc/debian_version");
98 StringRef Data = File.get()->getBuffer();
99 // Contents: < major.minor > or < codename/sid >
101 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
102 switch (MajorVersion) {
104 return Distro::DebianLenny;
106 return Distro::DebianSqueeze;
108 return Distro::DebianWheezy;
110 return Distro::DebianJessie;
112 return Distro::DebianStretch;
114 return Distro::DebianBuster;
116 return Distro::DebianBullseye;
118 return Distro::UnknownDistro;
121 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
122 .Case("squeeze/sid", Distro::DebianSqueeze)
123 .Case("wheezy/sid", Distro::DebianWheezy)
124 .Case("jessie/sid", Distro::DebianJessie)
125 .Case("stretch/sid", Distro::DebianStretch)
126 .Case("buster/sid", Distro::DebianBuster)
127 .Case("bullseye/sid", Distro::DebianBullseye)
128 .Default(Distro::UnknownDistro);
131 File = VFS.getBufferForFile("/etc/SuSE-release");
133 StringRef Data = File.get()->getBuffer();
134 SmallVector<StringRef, 8> Lines;
135 Data.split(Lines, "\n");
136 for (const StringRef& Line : Lines) {
137 if (!Line.trim().startswith("VERSION"))
139 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
140 // Old versions have split VERSION and PATCHLEVEL
141 // Newer versions use VERSION = x.y
142 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
145 // OpenSUSE/SLES 10 and older are not supported and not compatible
146 // with our rules, so just treat them as Distro::UnknownDistro.
147 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
148 return Distro::OpenSUSE;
149 return Distro::UnknownDistro;
151 return Distro::UnknownDistro;
154 if (VFS.exists("/etc/exherbo-release"))
155 return Distro::Exherbo;
157 if (VFS.exists("/etc/alpine-release"))
158 return Distro::AlpineLinux;
160 if (VFS.exists("/etc/arch-release"))
161 return Distro::ArchLinux;
163 if (VFS.exists("/etc/gentoo-release"))
164 return Distro::Gentoo;
166 return Distro::UnknownDistro;
169 Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
170 : DistroVal(DetectDistro(VFS, TargetOrHost)) {}