]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Driver/Distro.cpp
Vendor import of clang trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / 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 "clang/Basic/LLVM.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/Support/ErrorOr.h"
16 #include "llvm/Support/MemoryBuffer.h"
17
18 using namespace clang::driver;
19 using namespace clang;
20
21 static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
22   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
23       VFS.getBufferForFile("/etc/lsb-release");
24   if (File) {
25     StringRef Data = File.get()->getBuffer();
26     SmallVector<StringRef, 16> Lines;
27     Data.split(Lines, "\n");
28     Distro::DistroType Version = Distro::UnknownDistro;
29     for (StringRef Line : Lines)
30       if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
31         Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
32                       .Case("hardy", Distro::UbuntuHardy)
33                       .Case("intrepid", Distro::UbuntuIntrepid)
34                       .Case("jaunty", Distro::UbuntuJaunty)
35                       .Case("karmic", Distro::UbuntuKarmic)
36                       .Case("lucid", Distro::UbuntuLucid)
37                       .Case("maverick", Distro::UbuntuMaverick)
38                       .Case("natty", Distro::UbuntuNatty)
39                       .Case("oneiric", Distro::UbuntuOneiric)
40                       .Case("precise", Distro::UbuntuPrecise)
41                       .Case("quantal", Distro::UbuntuQuantal)
42                       .Case("raring", Distro::UbuntuRaring)
43                       .Case("saucy", Distro::UbuntuSaucy)
44                       .Case("trusty", Distro::UbuntuTrusty)
45                       .Case("utopic", Distro::UbuntuUtopic)
46                       .Case("vivid", Distro::UbuntuVivid)
47                       .Case("wily", Distro::UbuntuWily)
48                       .Case("xenial", Distro::UbuntuXenial)
49                       .Case("yakkety", Distro::UbuntuYakkety)
50                       .Case("zesty", Distro::UbuntuZesty)
51                       .Case("artful", Distro::UbuntuArtful)
52                       .Case("bionic", Distro::UbuntuBionic)
53                       .Case("cosmic", Distro::UbuntuCosmic)
54                       .Case("disco", Distro::UbuntuDisco)
55                       .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;
95       case 10:
96         return Distro::DebianBuster;
97       default:
98         return Distro::UnknownDistro;
99       }
100     }
101     return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
102         .Case("squeeze/sid", Distro::DebianSqueeze)
103         .Case("wheezy/sid", Distro::DebianWheezy)
104         .Case("jessie/sid", Distro::DebianJessie)
105         .Case("stretch/sid", Distro::DebianStretch)
106         .Default(Distro::UnknownDistro);
107   }
108
109   File = VFS.getBufferForFile("/etc/SuSE-release");
110   if (File) {
111     StringRef Data = File.get()->getBuffer();
112     SmallVector<StringRef, 8> Lines;
113     Data.split(Lines, "\n");
114     for (const StringRef& Line : Lines) {
115       if (!Line.trim().startswith("VERSION"))
116         continue;
117       std::pair<StringRef, StringRef> SplitLine = Line.split('=');
118       // Old versions have split VERSION and PATCHLEVEL
119       // Newer versions use VERSION = x.y
120       std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
121       int Version;
122
123       // OpenSUSE/SLES 10 and older are not supported and not compatible
124       // with our rules, so just treat them as Distro::UnknownDistro.
125       if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
126         return Distro::OpenSUSE;
127       return Distro::UnknownDistro;
128     }
129     return Distro::UnknownDistro;
130   }
131
132   if (VFS.exists("/etc/exherbo-release"))
133     return Distro::Exherbo;
134
135   if (VFS.exists("/etc/alpine-release"))
136     return Distro::AlpineLinux;
137
138   if (VFS.exists("/etc/arch-release"))
139     return Distro::ArchLinux;
140
141   if (VFS.exists("/etc/gentoo-release"))
142     return Distro::Gentoo;
143
144   return Distro::UnknownDistro;
145 }
146
147 Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}