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 .Case("artful", Distro::UbuntuArtful)
51 .Default(Distro::UnknownDistro);
52 if (Version != Distro::UnknownDistro)
56 File = VFS.getBufferForFile("/etc/redhat-release");
58 StringRef Data = File.get()->getBuffer();
59 if (Data.startswith("Fedora release"))
60 return Distro::Fedora;
61 if (Data.startswith("Red Hat Enterprise Linux") ||
62 Data.startswith("CentOS") ||
63 Data.startswith("Scientific Linux")) {
64 if (Data.find("release 7") != StringRef::npos)
66 else if (Data.find("release 6") != StringRef::npos)
68 else if (Data.find("release 5") != StringRef::npos)
71 return Distro::UnknownDistro;
74 File = VFS.getBufferForFile("/etc/debian_version");
76 StringRef Data = File.get()->getBuffer();
77 // Contents: < major.minor > or < codename/sid >
79 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
80 switch (MajorVersion) {
82 return Distro::DebianLenny;
84 return Distro::DebianSqueeze;
86 return Distro::DebianWheezy;
88 return Distro::DebianJessie;
90 return Distro::DebianStretch;
92 return Distro::UnknownDistro;
95 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
96 .Case("squeeze/sid", Distro::DebianSqueeze)
97 .Case("wheezy/sid", Distro::DebianWheezy)
98 .Case("jessie/sid", Distro::DebianJessie)
99 .Case("stretch/sid", Distro::DebianStretch)
100 .Default(Distro::UnknownDistro);
103 File = VFS.getBufferForFile("/etc/SuSE-release");
105 StringRef Data = File.get()->getBuffer();
106 SmallVector<StringRef, 8> Lines;
107 Data.split(Lines, "\n");
108 for (const StringRef& Line : Lines) {
109 if (!Line.trim().startswith("VERSION"))
111 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
112 // Old versions have split VERSION and PATCHLEVEL
113 // Newer versions use VERSION = x.y
114 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
117 // OpenSUSE/SLES 10 and older are not supported and not compatible
118 // with our rules, so just treat them as Distro::UnknownDistro.
119 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
120 return Distro::OpenSUSE;
121 return Distro::UnknownDistro;
123 return Distro::UnknownDistro;
126 if (VFS.exists("/etc/exherbo-release"))
127 return Distro::Exherbo;
129 if (VFS.exists("/etc/arch-release"))
130 return Distro::ArchLinux;
132 return Distro::UnknownDistro;
135 Distro::Distro(vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}