2016-11-29 05:11:14 +08:00
|
|
|
//===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-11-29 05:11:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/Distro.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2016-11-29 05:11:14 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
|
2016-11-29 05:11:14 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
|
|
|
|
VFS.getBufferForFile("/etc/lsb-release");
|
|
|
|
if (File) {
|
|
|
|
StringRef Data = File.get()->getBuffer();
|
|
|
|
SmallVector<StringRef, 16> Lines;
|
|
|
|
Data.split(Lines, "\n");
|
2018-07-20 16:19:20 +08:00
|
|
|
Distro::DistroType Version = Distro::UnknownDistro;
|
2016-11-29 05:11:14 +08:00
|
|
|
for (StringRef Line : Lines)
|
|
|
|
if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
|
|
|
|
Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
|
|
|
|
.Case("hardy", Distro::UbuntuHardy)
|
|
|
|
.Case("intrepid", Distro::UbuntuIntrepid)
|
|
|
|
.Case("jaunty", Distro::UbuntuJaunty)
|
|
|
|
.Case("karmic", Distro::UbuntuKarmic)
|
|
|
|
.Case("lucid", Distro::UbuntuLucid)
|
|
|
|
.Case("maverick", Distro::UbuntuMaverick)
|
|
|
|
.Case("natty", Distro::UbuntuNatty)
|
|
|
|
.Case("oneiric", Distro::UbuntuOneiric)
|
|
|
|
.Case("precise", Distro::UbuntuPrecise)
|
|
|
|
.Case("quantal", Distro::UbuntuQuantal)
|
|
|
|
.Case("raring", Distro::UbuntuRaring)
|
|
|
|
.Case("saucy", Distro::UbuntuSaucy)
|
|
|
|
.Case("trusty", Distro::UbuntuTrusty)
|
|
|
|
.Case("utopic", Distro::UbuntuUtopic)
|
|
|
|
.Case("vivid", Distro::UbuntuVivid)
|
|
|
|
.Case("wily", Distro::UbuntuWily)
|
|
|
|
.Case("xenial", Distro::UbuntuXenial)
|
|
|
|
.Case("yakkety", Distro::UbuntuYakkety)
|
|
|
|
.Case("zesty", Distro::UbuntuZesty)
|
2017-05-04 20:46:38 +08:00
|
|
|
.Case("artful", Distro::UbuntuArtful)
|
2017-10-25 22:21:33 +08:00
|
|
|
.Case("bionic", Distro::UbuntuBionic)
|
2018-05-10 16:45:43 +08:00
|
|
|
.Case("cosmic", Distro::UbuntuCosmic)
|
2018-11-05 01:41:41 +08:00
|
|
|
.Case("disco", Distro::UbuntuDisco)
|
2019-04-19 21:43:28 +08:00
|
|
|
.Case("eoan", Distro::UbuntuEoan)
|
2016-11-29 05:11:14 +08:00
|
|
|
.Default(Distro::UnknownDistro);
|
|
|
|
if (Version != Distro::UnknownDistro)
|
|
|
|
return Version;
|
|
|
|
}
|
|
|
|
|
|
|
|
File = VFS.getBufferForFile("/etc/redhat-release");
|
|
|
|
if (File) {
|
|
|
|
StringRef Data = File.get()->getBuffer();
|
|
|
|
if (Data.startswith("Fedora release"))
|
|
|
|
return Distro::Fedora;
|
|
|
|
if (Data.startswith("Red Hat Enterprise Linux") ||
|
|
|
|
Data.startswith("CentOS") ||
|
|
|
|
Data.startswith("Scientific Linux")) {
|
|
|
|
if (Data.find("release 7") != StringRef::npos)
|
|
|
|
return Distro::RHEL7;
|
|
|
|
else if (Data.find("release 6") != StringRef::npos)
|
|
|
|
return Distro::RHEL6;
|
|
|
|
else if (Data.find("release 5") != StringRef::npos)
|
|
|
|
return Distro::RHEL5;
|
|
|
|
}
|
|
|
|
return Distro::UnknownDistro;
|
|
|
|
}
|
|
|
|
|
|
|
|
File = VFS.getBufferForFile("/etc/debian_version");
|
|
|
|
if (File) {
|
|
|
|
StringRef Data = File.get()->getBuffer();
|
|
|
|
// Contents: < major.minor > or < codename/sid >
|
|
|
|
int MajorVersion;
|
|
|
|
if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
|
|
|
|
switch (MajorVersion) {
|
|
|
|
case 5:
|
|
|
|
return Distro::DebianLenny;
|
|
|
|
case 6:
|
|
|
|
return Distro::DebianSqueeze;
|
|
|
|
case 7:
|
|
|
|
return Distro::DebianWheezy;
|
|
|
|
case 8:
|
|
|
|
return Distro::DebianJessie;
|
|
|
|
case 9:
|
|
|
|
return Distro::DebianStretch;
|
2017-10-25 22:25:28 +08:00
|
|
|
case 10:
|
|
|
|
return Distro::DebianBuster;
|
2016-11-29 05:11:14 +08:00
|
|
|
default:
|
|
|
|
return Distro::UnknownDistro;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
|
|
|
|
.Case("squeeze/sid", Distro::DebianSqueeze)
|
|
|
|
.Case("wheezy/sid", Distro::DebianWheezy)
|
|
|
|
.Case("jessie/sid", Distro::DebianJessie)
|
|
|
|
.Case("stretch/sid", Distro::DebianStretch)
|
|
|
|
.Default(Distro::UnknownDistro);
|
|
|
|
}
|
|
|
|
|
|
|
|
File = VFS.getBufferForFile("/etc/SuSE-release");
|
|
|
|
if (File) {
|
|
|
|
StringRef Data = File.get()->getBuffer();
|
|
|
|
SmallVector<StringRef, 8> Lines;
|
|
|
|
Data.split(Lines, "\n");
|
|
|
|
for (const StringRef& Line : Lines) {
|
|
|
|
if (!Line.trim().startswith("VERSION"))
|
|
|
|
continue;
|
|
|
|
std::pair<StringRef, StringRef> SplitLine = Line.split('=');
|
2016-11-29 05:11:18 +08:00
|
|
|
// Old versions have split VERSION and PATCHLEVEL
|
|
|
|
// Newer versions use VERSION = x.y
|
|
|
|
std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
|
2016-11-29 05:11:14 +08:00
|
|
|
int Version;
|
2016-11-29 05:11:18 +08:00
|
|
|
|
2016-11-29 05:11:14 +08:00
|
|
|
// OpenSUSE/SLES 10 and older are not supported and not compatible
|
|
|
|
// with our rules, so just treat them as Distro::UnknownDistro.
|
2016-11-29 05:11:18 +08:00
|
|
|
if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
|
2016-11-29 05:11:14 +08:00
|
|
|
return Distro::OpenSUSE;
|
|
|
|
return Distro::UnknownDistro;
|
|
|
|
}
|
|
|
|
return Distro::UnknownDistro;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VFS.exists("/etc/exherbo-release"))
|
|
|
|
return Distro::Exherbo;
|
|
|
|
|
2017-11-19 08:08:12 +08:00
|
|
|
if (VFS.exists("/etc/alpine-release"))
|
|
|
|
return Distro::AlpineLinux;
|
|
|
|
|
2016-11-29 05:11:14 +08:00
|
|
|
if (VFS.exists("/etc/arch-release"))
|
|
|
|
return Distro::ArchLinux;
|
|
|
|
|
2018-12-23 23:07:19 +08:00
|
|
|
if (VFS.exists("/etc/gentoo-release"))
|
|
|
|
return Distro::Gentoo;
|
|
|
|
|
2016-11-29 05:11:14 +08:00
|
|
|
return Distro::UnknownDistro;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}
|