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