blob: 3fbf1637dd5dc8e0edc90377969dac680321d88d [file] [log] [blame]
Michal Gorny67e199e2016-11-28 21:11:14 +00001//===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
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"
16
17using namespace clang::driver;
18using namespace clang;
19
20static Distro::DistroType DetectDistro(vfs::FileSystem &VFS) {
21 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
22 VFS.getBufferForFile("/etc/lsb-release");
23 if (File) {
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)
Sylvestre Ledru6cf800a2017-05-04 12:46:38 +000050 .Case("artful", Distro::UbuntuArtful)
Sylvestre Ledruf8e9ffa2017-10-25 14:21:33 +000051 .Case("bionic", Distro::UbuntuBionic)
Sylvestre Ledru93dd5dc2018-05-10 08:45:43 +000052 .Case("cosmic", Distro::UbuntuCosmic)
Michal Gorny67e199e2016-11-28 21:11:14 +000053 .Default(Distro::UnknownDistro);
54 if (Version != Distro::UnknownDistro)
55 return Version;
56 }
57
58 File = VFS.getBufferForFile("/etc/redhat-release");
59 if (File) {
60 StringRef Data = File.get()->getBuffer();
61 if (Data.startswith("Fedora release"))
62 return Distro::Fedora;
63 if (Data.startswith("Red Hat Enterprise Linux") ||
64 Data.startswith("CentOS") ||
65 Data.startswith("Scientific Linux")) {
66 if (Data.find("release 7") != StringRef::npos)
67 return Distro::RHEL7;
68 else if (Data.find("release 6") != StringRef::npos)
69 return Distro::RHEL6;
70 else if (Data.find("release 5") != StringRef::npos)
71 return Distro::RHEL5;
72 }
73 return Distro::UnknownDistro;
74 }
75
76 File = VFS.getBufferForFile("/etc/debian_version");
77 if (File) {
78 StringRef Data = File.get()->getBuffer();
79 // Contents: < major.minor > or < codename/sid >
80 int MajorVersion;
81 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
82 switch (MajorVersion) {
83 case 5:
84 return Distro::DebianLenny;
85 case 6:
86 return Distro::DebianSqueeze;
87 case 7:
88 return Distro::DebianWheezy;
89 case 8:
90 return Distro::DebianJessie;
91 case 9:
92 return Distro::DebianStretch;
Sylvestre Ledrua3436bb2017-10-25 14:25:28 +000093 case 10:
94 return Distro::DebianBuster;
Michal Gorny67e199e2016-11-28 21:11:14 +000095 default:
96 return Distro::UnknownDistro;
97 }
98 }
99 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
100 .Case("squeeze/sid", Distro::DebianSqueeze)
101 .Case("wheezy/sid", Distro::DebianWheezy)
102 .Case("jessie/sid", Distro::DebianJessie)
103 .Case("stretch/sid", Distro::DebianStretch)
104 .Default(Distro::UnknownDistro);
105 }
106
107 File = VFS.getBufferForFile("/etc/SuSE-release");
108 if (File) {
109 StringRef Data = File.get()->getBuffer();
110 SmallVector<StringRef, 8> Lines;
111 Data.split(Lines, "\n");
112 for (const StringRef& Line : Lines) {
113 if (!Line.trim().startswith("VERSION"))
114 continue;
115 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
Michal Gorny047e0992016-11-28 21:11:18 +0000116 // Old versions have split VERSION and PATCHLEVEL
117 // Newer versions use VERSION = x.y
118 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
Michal Gorny67e199e2016-11-28 21:11:14 +0000119 int Version;
Michal Gorny047e0992016-11-28 21:11:18 +0000120
Michal Gorny67e199e2016-11-28 21:11:14 +0000121 // OpenSUSE/SLES 10 and older are not supported and not compatible
122 // with our rules, so just treat them as Distro::UnknownDistro.
Michal Gorny047e0992016-11-28 21:11:18 +0000123 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
Michal Gorny67e199e2016-11-28 21:11:14 +0000124 return Distro::OpenSUSE;
125 return Distro::UnknownDistro;
126 }
127 return Distro::UnknownDistro;
128 }
129
130 if (VFS.exists("/etc/exherbo-release"))
131 return Distro::Exherbo;
132
Martell Malone13c5d732017-11-19 00:08:12 +0000133 if (VFS.exists("/etc/alpine-release"))
134 return Distro::AlpineLinux;
135
Michal Gorny67e199e2016-11-28 21:11:14 +0000136 if (VFS.exists("/etc/arch-release"))
137 return Distro::ArchLinux;
138
139 return Distro::UnknownDistro;
140}
141
142Distro::Distro(vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}