1 //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Distro.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(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 .Default(Distro::UnknownDistro);
51 if (Version != Distro::UnknownDistro)
55 File = VFS.getBufferForFile("/etc/redhat-release");
57 StringRef Data = File.get()->getBuffer();
58 if (Data.startswith("Fedora release"))
59 return Distro::Fedora;
60 if (Data.startswith("Red Hat Enterprise Linux") ||
61 Data.startswith("CentOS") ||
62 Data.startswith("Scientific Linux")) {
63 if (Data.find("release 7") != StringRef::npos)
65 else if (Data.find("release 6") != StringRef::npos)
67 else if (Data.find("release 5") != StringRef::npos)
70 return Distro::UnknownDistro;
73 File = VFS.getBufferForFile("/etc/debian_version");
75 StringRef Data = File.get()->getBuffer();
76 // Contents: < major.minor > or < codename/sid >
78 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
79 switch (MajorVersion) {
81 return Distro::DebianLenny;
83 return Distro::DebianSqueeze;
85 return Distro::DebianWheezy;
87 return Distro::DebianJessie;
89 return Distro::DebianStretch;
91 return Distro::UnknownDistro;
94 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
95 .Case("squeeze/sid", Distro::DebianSqueeze)
96 .Case("wheezy/sid", Distro::DebianWheezy)
97 .Case("jessie/sid", Distro::DebianJessie)
98 .Case("stretch/sid", Distro::DebianStretch)
99 .Default(Distro::UnknownDistro);
102 File = VFS.getBufferForFile("/etc/SuSE-release");
104 StringRef Data = File.get()->getBuffer();
105 SmallVector<StringRef, 8> Lines;
106 Data.split(Lines, "\n");
107 for (const StringRef& Line : Lines) {
108 if (!Line.trim().startswith("VERSION"))
110 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
111 // Old versions have split VERSION and PATCHLEVEL
112 // Newer versions use VERSION = x.y
113 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
116 // OpenSUSE/SLES 10 and older are not supported and not compatible
117 // with our rules, so just treat them as Distro::UnknownDistro.
118 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
119 return Distro::OpenSUSE;
120 return Distro::UnknownDistro;
122 return Distro::UnknownDistro;
125 if (VFS.exists("/etc/exherbo-release"))
126 return Distro::Exherbo;
128 if (VFS.exists("/etc/arch-release"))
129 return Distro::ArchLinux;
131 return Distro::UnknownDistro;
134 Distro::Distro(vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}