blob: 06707fefc9d08bacda301246982af99cfc536360 [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"
Alexandre Ganea1abd4c92019-11-28 15:56:00 -050016#include "llvm/ADT/Triple.h"
Michal Gorny67e199e2016-11-28 21:11:14 +000017
18using namespace clang::driver;
19using namespace clang;
20
Alexandre Ganea1abd4c92019-11-28 15:56:00 -050021static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS,
22 const llvm::Triple &TargetOrHost) {
23 // If we don't target Linux, no need to check the distro. This saves a few
24 // OS calls.
25 if (!TargetOrHost.isOSLinux())
26 return Distro::UnknownDistro;
27
28 // If the host is not running Linux, and we're backed by a real file system,
29 // no need to check the distro. This is the case where someone is
30 // cross-compiling from BSD or Windows to Linux, and it would be meaningless
31 // to try to figure out the "distro" of the non-Linux host.
32 IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS =
33 llvm::vfs::getRealFileSystem();
34 llvm::Triple HostTriple(llvm::sys::getProcessTriple());
35 if (!HostTriple.isOSLinux() && &VFS == RealFS.get())
36 return Distro::UnknownDistro;
37
Michal Gorny67e199e2016-11-28 21:11:14 +000038 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
39 VFS.getBufferForFile("/etc/lsb-release");
40 if (File) {
41 StringRef Data = File.get()->getBuffer();
42 SmallVector<StringRef, 16> Lines;
43 Data.split(Lines, "\n");
Fangrui Song99337e22018-07-20 08:19:20 +000044 Distro::DistroType Version = Distro::UnknownDistro;
Michal Gorny67e199e2016-11-28 21:11:14 +000045 for (StringRef Line : Lines)
46 if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
47 Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
48 .Case("hardy", Distro::UbuntuHardy)
49 .Case("intrepid", Distro::UbuntuIntrepid)
50 .Case("jaunty", Distro::UbuntuJaunty)
51 .Case("karmic", Distro::UbuntuKarmic)
52 .Case("lucid", Distro::UbuntuLucid)
53 .Case("maverick", Distro::UbuntuMaverick)
54 .Case("natty", Distro::UbuntuNatty)
55 .Case("oneiric", Distro::UbuntuOneiric)
56 .Case("precise", Distro::UbuntuPrecise)
57 .Case("quantal", Distro::UbuntuQuantal)
58 .Case("raring", Distro::UbuntuRaring)
59 .Case("saucy", Distro::UbuntuSaucy)
60 .Case("trusty", Distro::UbuntuTrusty)
61 .Case("utopic", Distro::UbuntuUtopic)
62 .Case("vivid", Distro::UbuntuVivid)
63 .Case("wily", Distro::UbuntuWily)
64 .Case("xenial", Distro::UbuntuXenial)
65 .Case("yakkety", Distro::UbuntuYakkety)
66 .Case("zesty", Distro::UbuntuZesty)
Sylvestre Ledru6cf800a2017-05-04 12:46:38 +000067 .Case("artful", Distro::UbuntuArtful)
Sylvestre Ledruf8e9ffa2017-10-25 14:21:33 +000068 .Case("bionic", Distro::UbuntuBionic)
Sylvestre Ledru93dd5dc2018-05-10 08:45:43 +000069 .Case("cosmic", Distro::UbuntuCosmic)
Sylvestre Ledru99682d02018-11-04 17:41:41 +000070 .Case("disco", Distro::UbuntuDisco)
Sylvestre Ledru45401412019-04-19 13:43:28 +000071 .Case("eoan", Distro::UbuntuEoan)
Sylvestre Ledru42effc12019-11-16 12:20:47 +010072 .Case("focal", Distro::UbuntuFocal)
Michal Gorny67e199e2016-11-28 21:11:14 +000073 .Default(Distro::UnknownDistro);
74 if (Version != Distro::UnknownDistro)
75 return Version;
76 }
77
78 File = VFS.getBufferForFile("/etc/redhat-release");
79 if (File) {
80 StringRef Data = File.get()->getBuffer();
81 if (Data.startswith("Fedora release"))
82 return Distro::Fedora;
83 if (Data.startswith("Red Hat Enterprise Linux") ||
84 Data.startswith("CentOS") ||
85 Data.startswith("Scientific Linux")) {
86 if (Data.find("release 7") != StringRef::npos)
87 return Distro::RHEL7;
88 else if (Data.find("release 6") != StringRef::npos)
89 return Distro::RHEL6;
90 else if (Data.find("release 5") != StringRef::npos)
91 return Distro::RHEL5;
92 }
93 return Distro::UnknownDistro;
94 }
95
96 File = VFS.getBufferForFile("/etc/debian_version");
97 if (File) {
98 StringRef Data = File.get()->getBuffer();
99 // Contents: < major.minor > or < codename/sid >
100 int MajorVersion;
101 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
102 switch (MajorVersion) {
103 case 5:
104 return Distro::DebianLenny;
105 case 6:
106 return Distro::DebianSqueeze;
107 case 7:
108 return Distro::DebianWheezy;
109 case 8:
110 return Distro::DebianJessie;
111 case 9:
112 return Distro::DebianStretch;
Sylvestre Ledrua3436bb2017-10-25 14:25:28 +0000113 case 10:
114 return Distro::DebianBuster;
Sylvestre Ledru54a93a32019-04-19 13:46:58 +0000115 case 11:
116 return Distro::DebianBullseye;
Michal Gorny67e199e2016-11-28 21:11:14 +0000117 default:
118 return Distro::UnknownDistro;
119 }
120 }
121 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
122 .Case("squeeze/sid", Distro::DebianSqueeze)
123 .Case("wheezy/sid", Distro::DebianWheezy)
124 .Case("jessie/sid", Distro::DebianJessie)
125 .Case("stretch/sid", Distro::DebianStretch)
Sylvestre Ledru5505ad32019-04-19 13:48:52 +0000126 .Case("buster/sid", Distro::DebianBuster)
127 .Case("bullseye/sid", Distro::DebianBullseye)
Michal Gorny67e199e2016-11-28 21:11:14 +0000128 .Default(Distro::UnknownDistro);
129 }
130
131 File = VFS.getBufferForFile("/etc/SuSE-release");
132 if (File) {
133 StringRef Data = File.get()->getBuffer();
134 SmallVector<StringRef, 8> Lines;
135 Data.split(Lines, "\n");
136 for (const StringRef& Line : Lines) {
137 if (!Line.trim().startswith("VERSION"))
138 continue;
139 std::pair<StringRef, StringRef> SplitLine = Line.split('=');
Michal Gorny047e0992016-11-28 21:11:18 +0000140 // Old versions have split VERSION and PATCHLEVEL
141 // Newer versions use VERSION = x.y
142 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
Michal Gorny67e199e2016-11-28 21:11:14 +0000143 int Version;
Michal Gorny047e0992016-11-28 21:11:18 +0000144
Michal Gorny67e199e2016-11-28 21:11:14 +0000145 // OpenSUSE/SLES 10 and older are not supported and not compatible
146 // with our rules, so just treat them as Distro::UnknownDistro.
Michal Gorny047e0992016-11-28 21:11:18 +0000147 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
Michal Gorny67e199e2016-11-28 21:11:14 +0000148 return Distro::OpenSUSE;
149 return Distro::UnknownDistro;
150 }
151 return Distro::UnknownDistro;
152 }
153
154 if (VFS.exists("/etc/exherbo-release"))
155 return Distro::Exherbo;
156
Martell Malone13c5d732017-11-19 00:08:12 +0000157 if (VFS.exists("/etc/alpine-release"))
158 return Distro::AlpineLinux;
159
Michal Gorny67e199e2016-11-28 21:11:14 +0000160 if (VFS.exists("/etc/arch-release"))
161 return Distro::ArchLinux;
162
Michal Gornyf2412282018-12-23 15:07:19 +0000163 if (VFS.exists("/etc/gentoo-release"))
164 return Distro::Gentoo;
165
Michal Gorny67e199e2016-11-28 21:11:14 +0000166 return Distro::UnknownDistro;
167}
168
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500169Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
170 : DistroVal(DetectDistro(VFS, TargetOrHost)) {}