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