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"
17 using namespace clang::driver;
18 using namespace clang;
20 static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
21 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
22 VFS.getBufferForFile("/etc/lsb-release");
24 StringRef Data = File.get()->getBuffer();
25 SmallVector<StringRef, 16> Lines;
26 Data.split(Lines, "\n");
27 Distro::DistroType Version = Distro::UnknownDistro;
28 for (StringRef Line : Lines)
29 if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
30 Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
31 .Case("hardy", Distro::UbuntuHardy)
32 .Case("intrepid", Distro::UbuntuIntrepid)
33 .Case("jaunty", Distro::UbuntuJaunty)
34 .Case("karmic", Distro::UbuntuKarmic)
35 .Case("lucid", Distro::UbuntuLucid)
36 .Case("maverick", Distro::UbuntuMaverick)
37 .Case("natty", Distro::UbuntuNatty)
38 .Case("oneiric", Distro::UbuntuOneiric)
39 .Case("precise", Distro::UbuntuPrecise)
40 .Case("quantal", Distro::UbuntuQuantal)
41 .Case("raring", Distro::UbuntuRaring)
42 .Case("saucy", Distro::UbuntuSaucy)
43 .Case("trusty", Distro::UbuntuTrusty)
44 .Case("utopic", Distro::UbuntuUtopic)
45 .Case("vivid", Distro::UbuntuVivid)
46 .Case("wily", Distro::UbuntuWily)
47 .Case("xenial", Distro::UbuntuXenial)
48 .Case("yakkety", Distro::UbuntuYakkety)
49 .Case("zesty", Distro::UbuntuZesty)
50 .Case("artful", Distro::UbuntuArtful)
51 .Case("bionic", Distro::UbuntuBionic)
52 .Case("cosmic", Distro::UbuntuCosmic)
53 .Case("disco", Distro::UbuntuDisco)
54 .Case("eoan", Distro::UbuntuEoan)
55 .Default(Distro::UnknownDistro);
56 if (Version != Distro::UnknownDistro)
60 File = VFS.getBufferForFile("/etc/redhat-release");
62 StringRef Data = File.get()->getBuffer();
63 if (Data.startswith("Fedora release"))
64 return Distro::Fedora;
65 if (Data.startswith("Red Hat Enterprise Linux") ||
66 Data.startswith("CentOS") ||
67 Data.startswith("Scientific Linux")) {
68 if (Data.find("release 7") != StringRef::npos)
70 else if (Data.find("release 6") != StringRef::npos)
72 else if (Data.find("release 5") != StringRef::npos)
75 return Distro::UnknownDistro;
78 File = VFS.getBufferForFile("/etc/debian_version");
80 StringRef Data = File.get()->getBuffer();
81 // Contents: < major.minor > or < codename/sid >
83 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
84 switch (MajorVersion) {
86 return Distro::DebianLenny;
88 return Distro::DebianSqueeze;
90 return Distro::DebianWheezy;
92 return Distro::DebianJessie;
94 return Distro::DebianStretch;
96 return Distro::DebianBuster;
98 return Distro::DebianBullseye;
100 return Distro::UnknownDistro;
103 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
104 .Case("squeeze/sid", Distro::DebianSqueeze)
105 .Case("wheezy/sid", Distro::DebianWheezy)
106 .Case("jessie/sid", Distro::DebianJessie)
107 .Case("stretch/sid", Distro::DebianStretch)
108 .Case("buster/sid", Distro::DebianBuster)
109 .Case("bullseye/sid", Distro::DebianBullseye)
110 .Default(Distro::UnknownDistro);
113 File = VFS.getBufferForFile("/etc/SuSE-release");
115 StringRef Data = File.get()->getBuffer();
116 SmallVector<StringRef, 8> Lines;
117 Data.split(Lines, "\n");
118 for (const StringRef& Line : Lines) {
119 if (!Line.trim().startswith("VERSION"))
121 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
122 // Old versions have split VERSION and PATCHLEVEL
123 // Newer versions use VERSION = x.y
124 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
127 // OpenSUSE/SLES 10 and older are not supported and not compatible
128 // with our rules, so just treat them as Distro::UnknownDistro.
129 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
130 return Distro::OpenSUSE;
131 return Distro::UnknownDistro;
133 return Distro::UnknownDistro;
136 if (VFS.exists("/etc/exherbo-release"))
137 return Distro::Exherbo;
139 if (VFS.exists("/etc/alpine-release"))
140 return Distro::AlpineLinux;
142 if (VFS.exists("/etc/arch-release"))
143 return Distro::ArchLinux;
145 if (VFS.exists("/etc/gentoo-release"))
146 return Distro::Gentoo;
148 return Distro::UnknownDistro;
151 Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}