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