]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/Distro.cpp
Pull in r336008 from upstream clang trunk:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Driver / Distro.cpp
1 //===--- 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
17 using namespace clang::driver;
18 using namespace clang;
19
20 static 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)
50                       .Case("artful", Distro::UbuntuArtful)
51                       .Case("bionic", Distro::UbuntuBionic)
52                       .Default(Distro::UnknownDistro);
53     if (Version != Distro::UnknownDistro)
54       return Version;
55   }
56
57   File = VFS.getBufferForFile("/etc/redhat-release");
58   if (File) {
59     StringRef Data = File.get()->getBuffer();
60     if (Data.startswith("Fedora release"))
61       return Distro::Fedora;
62     if (Data.startswith("Red Hat Enterprise Linux") ||
63         Data.startswith("CentOS") ||
64         Data.startswith("Scientific Linux")) {
65       if (Data.find("release 7") != StringRef::npos)
66         return Distro::RHEL7;
67       else if (Data.find("release 6") != StringRef::npos)
68         return Distro::RHEL6;
69       else if (Data.find("release 5") != StringRef::npos)
70         return Distro::RHEL5;
71     }
72     return Distro::UnknownDistro;
73   }
74
75   File = VFS.getBufferForFile("/etc/debian_version");
76   if (File) {
77     StringRef Data = File.get()->getBuffer();
78     // Contents: < major.minor > or < codename/sid >
79     int MajorVersion;
80     if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
81       switch (MajorVersion) {
82       case 5:
83         return Distro::DebianLenny;
84       case 6:
85         return Distro::DebianSqueeze;
86       case 7:
87         return Distro::DebianWheezy;
88       case 8:
89         return Distro::DebianJessie;
90       case 9:
91         return Distro::DebianStretch;
92       case 10:
93         return Distro::DebianBuster;
94       default:
95         return Distro::UnknownDistro;
96       }
97     }
98     return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
99         .Case("squeeze/sid", Distro::DebianSqueeze)
100         .Case("wheezy/sid", Distro::DebianWheezy)
101         .Case("jessie/sid", Distro::DebianJessie)
102         .Case("stretch/sid", Distro::DebianStretch)
103         .Default(Distro::UnknownDistro);
104   }
105
106   File = VFS.getBufferForFile("/etc/SuSE-release");
107   if (File) {
108     StringRef Data = File.get()->getBuffer();
109     SmallVector<StringRef, 8> Lines;
110     Data.split(Lines, "\n");
111     for (const StringRef& Line : Lines) {
112       if (!Line.trim().startswith("VERSION"))
113         continue;
114       std::pair<StringRef, StringRef> SplitLine = Line.split('=');
115       // Old versions have split VERSION and PATCHLEVEL
116       // Newer versions use VERSION = x.y
117       std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
118       int Version;
119
120       // OpenSUSE/SLES 10 and older are not supported and not compatible
121       // with our rules, so just treat them as Distro::UnknownDistro.
122       if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
123         return Distro::OpenSUSE;
124       return Distro::UnknownDistro;
125     }
126     return Distro::UnknownDistro;
127   }
128
129   if (VFS.exists("/etc/exherbo-release"))
130     return Distro::Exherbo;
131
132   if (VFS.exists("/etc/alpine-release"))
133     return Distro::AlpineLinux;
134
135   if (VFS.exists("/etc/arch-release"))
136     return Distro::ArchLinux;
137
138   return Distro::UnknownDistro;
139 }
140
141 Distro::Distro(vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}