blob: 659aa06e3ffbc0993db7a3904102c7f41f166bde [file] [log] [blame]
Michal Gorny67e199e2016-11-28 21:11:14 +00001//===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michal Gorny67e199e2016-11-28 21:11:14 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Driver/Distro.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000010#include "clang/Basic/LLVM.h"
Michal Gorny67e199e2016-11-28 21:11:14 +000011#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
Jonas Devliegherefc514902018-10-10 13:27:25 +000020static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
Michal Gorny67e199e2016-11-28 21:11:14 +000021 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");
Fangrui Song99337e22018-07-20 08:19:20 +000027 Distro::DistroType Version = Distro::UnknownDistro;
Michal Gorny67e199e2016-11-28 21:11:14 +000028 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)
Sylvestre Ledru99682d02018-11-04 17:41:41 +000053 .Case("disco", Distro::UbuntuDisco)
Sylvestre Ledru45401412019-04-19 13:43:28 +000054 .Case("eoan", Distro::UbuntuEoan)
Michal Gorny67e199e2016-11-28 21:11:14 +000055 .Default(Distro::UnknownDistro);
56 if (Version != Distro::UnknownDistro)
57 return Version;
58 }
59
60 File = VFS.getBufferForFile("/etc/redhat-release");
61 if (File) {
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)
69 return Distro::RHEL7;
70 else if (Data.find("release 6") != StringRef::npos)
71 return Distro::RHEL6;
72 else if (Data.find("release 5") != StringRef::npos)
73 return Distro::RHEL5;
74 }
75 return Distro::UnknownDistro;
76 }
77
78 File = VFS.getBufferForFile("/etc/debian_version");
79 if (File) {
80 StringRef Data = File.get()->getBuffer();
81 // Contents: < major.minor > or < codename/sid >
82 int MajorVersion;
83 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
84 switch (MajorVersion) {
85 case 5:
86 return Distro::DebianLenny;
87 case 6:
88 return Distro::DebianSqueeze;
89 case 7:
90 return Distro::DebianWheezy;
91 case 8:
92 return Distro::DebianJessie;
93 case 9:
94 return Distro::DebianStretch;
Sylvestre Ledrua3436bb2017-10-25 14:25:28 +000095 case 10:
96 return Distro::DebianBuster;
Sylvestre Ledru54a93a32019-04-19 13:46:58 +000097 case 11:
98 return Distro::DebianBullseye;
Michal Gorny67e199e2016-11-28 21:11:14 +000099 default:
100 return Distro::UnknownDistro;
101 }
102 }
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 .Default(Distro::UnknownDistro);
109 }
110
111 File = VFS.getBufferForFile("/etc/SuSE-release");
112 if (File) {
113 StringRef Data = File.get()->getBuffer();
114 SmallVector<StringRef, 8> Lines;
115 Data.split(Lines, "\n");
116 for (const StringRef& Line : Lines) {
117 if (!Line.trim().startswith("VERSION"))
118 continue;
119 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
Michal Gorny047e0992016-11-28 21:11:18 +0000120 // Old versions have split VERSION and PATCHLEVEL
121 // Newer versions use VERSION = x.y
122 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
Michal Gorny67e199e2016-11-28 21:11:14 +0000123 int Version;
Michal Gorny047e0992016-11-28 21:11:18 +0000124
Michal Gorny67e199e2016-11-28 21:11:14 +0000125 // OpenSUSE/SLES 10 and older are not supported and not compatible
126 // with our rules, so just treat them as Distro::UnknownDistro.
Michal Gorny047e0992016-11-28 21:11:18 +0000127 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
Michal Gorny67e199e2016-11-28 21:11:14 +0000128 return Distro::OpenSUSE;
129 return Distro::UnknownDistro;
130 }
131 return Distro::UnknownDistro;
132 }
133
134 if (VFS.exists("/etc/exherbo-release"))
135 return Distro::Exherbo;
136
Martell Malone13c5d732017-11-19 00:08:12 +0000137 if (VFS.exists("/etc/alpine-release"))
138 return Distro::AlpineLinux;
139
Michal Gorny67e199e2016-11-28 21:11:14 +0000140 if (VFS.exists("/etc/arch-release"))
141 return Distro::ArchLinux;
142
Michal Gornyf2412282018-12-23 15:07:19 +0000143 if (VFS.exists("/etc/gentoo-release"))
144 return Distro::Gentoo;
145
Michal Gorny67e199e2016-11-28 21:11:14 +0000146 return Distro::UnknownDistro;
147}
148
Jonas Devliegherefc514902018-10-10 13:27:25 +0000149Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}