]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/clang/lib/Driver/Distro.cpp
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / clang / lib / Driver / Distro.cpp
1 //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Driver/Distro.h"
10 #include "clang/Basic/LLVM.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Support/ErrorOr.h"
16 #include "llvm/Support/Host.h"
17 #include "llvm/Support/MemoryBuffer.h"
18
19 using namespace clang::driver;
20 using namespace clang;
21
22 static 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
39   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");
45     Distro::DistroType Version = Distro::UnknownDistro;
46     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)
68                       .Case("artful", Distro::UbuntuArtful)
69                       .Case("bionic", Distro::UbuntuBionic)
70                       .Case("cosmic", Distro::UbuntuCosmic)
71                       .Case("disco", Distro::UbuntuDisco)
72                       .Case("eoan", Distro::UbuntuEoan)
73                       .Case("focal", Distro::UbuntuFocal)
74                       .Case("groovy", Distro::UbuntuGroovy)
75                       .Default(Distro::UnknownDistro);
76     if (Version != Distro::UnknownDistro)
77       return Version;
78   }
79
80   File = VFS.getBufferForFile("/etc/redhat-release");
81   if (File) {
82     StringRef Data = File.get()->getBuffer();
83     if (Data.startswith("Fedora release"))
84       return Distro::Fedora;
85     if (Data.startswith("Red Hat Enterprise Linux") ||
86         Data.startswith("CentOS") ||
87         Data.startswith("Scientific Linux")) {
88       if (Data.find("release 7") != StringRef::npos)
89         return Distro::RHEL7;
90       else if (Data.find("release 6") != StringRef::npos)
91         return Distro::RHEL6;
92       else if (Data.find("release 5") != StringRef::npos)
93         return Distro::RHEL5;
94     }
95     return Distro::UnknownDistro;
96   }
97
98   File = VFS.getBufferForFile("/etc/debian_version");
99   if (File) {
100     StringRef Data = File.get()->getBuffer();
101     // Contents: < major.minor > or < codename/sid >
102     int MajorVersion;
103     if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
104       switch (MajorVersion) {
105       case 5:
106         return Distro::DebianLenny;
107       case 6:
108         return Distro::DebianSqueeze;
109       case 7:
110         return Distro::DebianWheezy;
111       case 8:
112         return Distro::DebianJessie;
113       case 9:
114         return Distro::DebianStretch;
115       case 10:
116         return Distro::DebianBuster;
117       case 11:
118         return Distro::DebianBullseye;
119       default:
120         return Distro::UnknownDistro;
121       }
122     }
123     return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
124         .Case("squeeze/sid", Distro::DebianSqueeze)
125         .Case("wheezy/sid", Distro::DebianWheezy)
126         .Case("jessie/sid", Distro::DebianJessie)
127         .Case("stretch/sid", Distro::DebianStretch)
128         .Case("buster/sid", Distro::DebianBuster)
129         .Case("bullseye/sid", Distro::DebianBullseye)
130         .Default(Distro::UnknownDistro);
131   }
132
133   File = VFS.getBufferForFile("/etc/SuSE-release");
134   if (File) {
135     StringRef Data = File.get()->getBuffer();
136     SmallVector<StringRef, 8> Lines;
137     Data.split(Lines, "\n");
138     for (const StringRef& Line : Lines) {
139       if (!Line.trim().startswith("VERSION"))
140         continue;
141       std::pair<StringRef, StringRef> SplitLine = Line.split('=');
142       // Old versions have split VERSION and PATCHLEVEL
143       // Newer versions use VERSION = x.y
144       std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
145       int Version;
146
147       // OpenSUSE/SLES 10 and older are not supported and not compatible
148       // with our rules, so just treat them as Distro::UnknownDistro.
149       if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
150         return Distro::OpenSUSE;
151       return Distro::UnknownDistro;
152     }
153     return Distro::UnknownDistro;
154   }
155
156   if (VFS.exists("/etc/exherbo-release"))
157     return Distro::Exherbo;
158
159   if (VFS.exists("/etc/alpine-release"))
160     return Distro::AlpineLinux;
161
162   if (VFS.exists("/etc/arch-release"))
163     return Distro::ArchLinux;
164
165   if (VFS.exists("/etc/gentoo-release"))
166     return Distro::Gentoo;
167
168   return Distro::UnknownDistro;
169 }
170
171 Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
172     : DistroVal(DetectDistro(VFS, TargetOrHost)) {}