Coverage Report

Created: 2022-01-18 06:27

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