Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/Distro.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "llvm/Support/Threading.h"
19
20
using namespace clang::driver;
21
using namespace clang;
22
23
2.20k
static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
24
2.20k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
25
2.20k
      VFS.getBufferForFile("/etc/os-release");
26
2.20k
  if (!File)
27
2.19k
    File = VFS.getBufferForFile("/usr/lib/os-release");
28
2.20k
  if (!File)
29
2.19k
    return Distro::UnknownDistro;
30
31
11
  SmallVector<StringRef, 16> Lines;
32
11
  File.get()->getBuffer().split(Lines, "\n");
33
11
  Distro::DistroType Version = Distro::UnknownDistro;
34
35
  // Obviously this can be improved a lot.
36
11
  for (StringRef Line : Lines)
37
116
    if (Version == Distro::UnknownDistro && 
Line.startswith("ID=")80
)
38
11
      Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(3))
39
11
                    .Case("fedora", Distro::Fedora)
40
11
                    .Case("gentoo", Distro::Gentoo)
41
11
                    .Case("arch", Distro::ArchLinux)
42
                    // On SLES, /etc/os-release was introduced in SLES 11.
43
11
                    .Case("sles", Distro::OpenSUSE)
44
11
                    .Case("opensuse", Distro::OpenSUSE)
45
11
                    .Case("exherbo", Distro::Exherbo)
46
11
                    .Default(Distro::UnknownDistro);
47
11
  return Version;
48
11
}
49
50
2.20k
static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
51
2.20k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
52
2.20k
      VFS.getBufferForFile("/etc/lsb-release");
53
2.20k
  if (!File)
54
2.20k
    return Distro::UnknownDistro;
55
56
2
  SmallVector<StringRef, 16> Lines;
57
2
  File.get()->getBuffer().split(Lines, "\n");
58
2
  Distro::DistroType Version = Distro::UnknownDistro;
59
60
2
  for (StringRef Line : Lines)
61
10
    if (Version == Distro::UnknownDistro &&
62
6
        Line.startswith("DISTRIB_CODENAME="))
63
2
      Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
64
2
                    .Case("hardy", Distro::UbuntuHardy)
65
2
                    .Case("intrepid", Distro::UbuntuIntrepid)
66
2
                    .Case("jaunty", Distro::UbuntuJaunty)
67
2
                    .Case("karmic", Distro::UbuntuKarmic)
68
2
                    .Case("lucid", Distro::UbuntuLucid)
69
2
                    .Case("maverick", Distro::UbuntuMaverick)
70
2
                    .Case("natty", Distro::UbuntuNatty)
71
2
                    .Case("oneiric", Distro::UbuntuOneiric)
72
2
                    .Case("precise", Distro::UbuntuPrecise)
73
2
                    .Case("quantal", Distro::UbuntuQuantal)
74
2
                    .Case("raring", Distro::UbuntuRaring)
75
2
                    .Case("saucy", Distro::UbuntuSaucy)
76
2
                    .Case("trusty", Distro::UbuntuTrusty)
77
2
                    .Case("utopic", Distro::UbuntuUtopic)
78
2
                    .Case("vivid", Distro::UbuntuVivid)
79
2
                    .Case("wily", Distro::UbuntuWily)
80
2
                    .Case("xenial", Distro::UbuntuXenial)
81
2
                    .Case("yakkety", Distro::UbuntuYakkety)
82
2
                    .Case("zesty", Distro::UbuntuZesty)
83
2
                    .Case("artful", Distro::UbuntuArtful)
84
2
                    .Case("bionic", Distro::UbuntuBionic)
85
2
                    .Case("cosmic", Distro::UbuntuCosmic)
86
2
                    .Case("disco", Distro::UbuntuDisco)
87
2
                    .Case("eoan", Distro::UbuntuEoan)
88
2
                    .Case("focal", Distro::UbuntuFocal)
89
2
                    .Case("groovy", Distro::UbuntuGroovy)
90
2
                    .Case("hirsute", Distro::UbuntuHirsute)
91
2
                    .Default(Distro::UnknownDistro);
92
2
  return Version;
93
2
}
94
95
2.20k
static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
96
2.20k
  Distro::DistroType Version = Distro::UnknownDistro;
97
98
  // Newer freedesktop.org's compilant systemd-based systems
99
  // should provide /etc/os-release or /usr/lib/os-release.
100
2.20k
  Version = DetectOsRelease(VFS);
101
2.20k
  if (Version != Distro::UnknownDistro)
102
5
    return Version;
103
104
  // Older systems might provide /etc/lsb-release.
105
2.20k
  Version = DetectLsbRelease(VFS);
106
2.20k
  if (Version != Distro::UnknownDistro)
107
2
    return Version;
108
109
  // Otherwise try some distro-specific quirks for RedHat...
110
2.20k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
111
2.20k
      VFS.getBufferForFile("/etc/redhat-release");
112
113
2.20k
  if (File) {
114
1
    StringRef Data = File.get()->getBuffer();
115
1
    if (Data.startswith("Fedora release"))
116
0
      return Distro::Fedora;
117
1
    if (Data.startswith("Red Hat Enterprise Linux") ||
118
1
        Data.startswith("CentOS") || 
Data.startswith("Scientific Linux")0
) {
119
1
      if (Data.find("release 7") != StringRef::npos)
120
1
        return Distro::RHEL7;
121
0
      else if (Data.find("release 6") != StringRef::npos)
122
0
        return Distro::RHEL6;
123
0
      else if (Data.find("release 5") != StringRef::npos)
124
0
        return Distro::RHEL5;
125
0
    }
126
0
    return Distro::UnknownDistro;
127
0
  }
128
129
  // ...for Debian
130
2.20k
  File = VFS.getBufferForFile("/etc/debian_version");
131
2.20k
  if (File) {
132
2
    StringRef Data = File.get()->getBuffer();
133
    // Contents: < major.minor > or < codename/sid >
134
2
    int MajorVersion;
135
2
    if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
136
1
      switch (MajorVersion) {
137
0
      case 5:
138
0
        return Distro::DebianLenny;
139
0
      case 6:
140
0
        return Distro::DebianSqueeze;
141
0
      case 7:
142
0
        return Distro::DebianWheezy;
143
1
      case 8:
144
1
        return Distro::DebianJessie;
145
0
      case 9:
146
0
        return Distro::DebianStretch;
147
0
      case 10:
148
0
        return Distro::DebianBuster;
149
0
      case 11:
150
0
        return Distro::DebianBullseye;
151
0
      default:
152
0
        return Distro::UnknownDistro;
153
1
      }
154
1
    }
155
1
    return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
156
1
        .Case("squeeze/sid", Distro::DebianSqueeze)
157
1
        .Case("wheezy/sid", Distro::DebianWheezy)
158
1
        .Case("jessie/sid", Distro::DebianJessie)
159
1
        .Case("stretch/sid", Distro::DebianStretch)
160
1
        .Case("buster/sid", Distro::DebianBuster)
161
1
        .Case("bullseye/sid", Distro::DebianBullseye)
162
1
        .Default(Distro::UnknownDistro);
163
1
  }
164
165
  // ...for SUSE
166
2.19k
  File = VFS.getBufferForFile("/etc/SuSE-release");
167
2.19k
  if (File) {
168
1
    StringRef Data = File.get()->getBuffer();
169
1
    SmallVector<StringRef, 8> Lines;
170
1
    Data.split(Lines, "\n");
171
2
    for (const StringRef &Line : Lines) {
172
2
      if (!Line.trim().startswith("VERSION"))
173
1
        continue;
174
1
      std::pair<StringRef, StringRef> SplitLine = Line.split('=');
175
      // Old versions have split VERSION and PATCHLEVEL
176
      // Newer versions use VERSION = x.y
177
1
      std::pair<StringRef, StringRef> SplitVer =
178
1
          SplitLine.second.trim().split('.');
179
1
      int Version;
180
181
      // OpenSUSE/SLES 10 and older are not supported and not compatible
182
      // with our rules, so just treat them as Distro::UnknownDistro.
183
1
      if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
184
0
        return Distro::OpenSUSE;
185
1
      return Distro::UnknownDistro;
186
1
    }
187
0
    return Distro::UnknownDistro;
188
2.19k
  }
189
190
  // ...and others.
191
2.19k
  if (VFS.exists("/etc/exherbo-release"))
192
1
    return Distro::Exherbo;
193
194
2.19k
  if (VFS.exists("/etc/alpine-release"))
195
0
    return Distro::AlpineLinux;
196
197
2.19k
  if (VFS.exists("/etc/arch-release"))
198
0
    return Distro::ArchLinux;
199
200
2.19k
  if (VFS.exists("/etc/gentoo-release"))
201
0
    return Distro::Gentoo;
202
203
2.19k
  return Distro::UnknownDistro;
204
2.19k
}
205
206
static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
207
68.7k
                                    const llvm::Triple &TargetOrHost) {
208
  // If we don't target Linux, no need to check the distro. This saves a few
209
  // OS calls.
210
68.7k
  if (!TargetOrHost.isOSLinux())
211
58.2k
    return Distro::UnknownDistro;
212
213
  // True if we're backed by a real file system.
214
10.4k
  const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);
215
216
  // If the host is not running Linux, and we're backed by a real file
217
  // system, no need to check the distro. This is the case where someone
218
  // is cross-compiling from BSD or Windows to Linux, and it would be
219
  // meaningless to try to figure out the "distro" of the non-Linux host.
220
10.4k
  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
221
10.4k
  if (!HostTriple.isOSLinux() && 
onRealFS10.4k
)
222
8.27k
    return Distro::UnknownDistro;
223
224
2.21k
  if (onRealFS) {
225
    // If we're backed by a real file system, perform
226
    // the detection only once and save the result.
227
0
    static Distro::DistroType LinuxDistro = DetectDistro(VFS);
228
0
    return LinuxDistro;
229
0
  }
230
  // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
231
  // which is not "real".
232
2.21k
  return DetectDistro(VFS);
233
2.21k
}
234
235
Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
236
68.7k
    : DistroVal(GetDistro(VFS, TargetOrHost)) {}