Coverage Report

Created: 2023-09-12 09:32

/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/Support/ErrorOr.h"
15
#include "llvm/Support/MemoryBuffer.h"
16
#include "llvm/Support/Threading.h"
17
#include "llvm/TargetParser/Host.h"
18
#include "llvm/TargetParser/Triple.h"
19
20
using namespace clang::driver;
21
using namespace clang;
22
23
2.29k
static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
24
2.29k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
25
2.29k
      VFS.getBufferForFile("/etc/os-release");
26
2.29k
  if (!File)
27
2.28k
    File = VFS.getBufferForFile("/usr/lib/os-release");
28
2.29k
  if (!File)
29
2.28k
    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.29k
}
50
51
2.29k
static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
52
2.29k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
53
2.29k
      VFS.getBufferForFile("/etc/lsb-release");
54
2.29k
  if (!File)
55
2.28k
    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
                    .Case("kinetic", Distro::UbuntuKinetic)
95
2
                    .Case("lunar", Distro::UbuntuLunar)
96
2
                    .Case("mantic", Distro::UbuntuMantic)
97
2
                    .Default(Distro::UnknownDistro);
98
2
  return Version;
99
2.29k
}
100
101
2.29k
static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
102
2.29k
  Distro::DistroType Version = Distro::UnknownDistro;
103
104
  // Newer freedesktop.org's compilant systemd-based systems
105
  // should provide /etc/os-release or /usr/lib/os-release.
106
2.29k
  Version = DetectOsRelease(VFS);
107
2.29k
  if (Version != Distro::UnknownDistro)
108
6
    return Version;
109
110
  // Older systems might provide /etc/lsb-release.
111
2.29k
  Version = DetectLsbRelease(VFS);
112
2.29k
  if (Version != Distro::UnknownDistro)
113
2
    return Version;
114
115
  // Otherwise try some distro-specific quirks for Red Hat...
116
2.28k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
117
2.28k
      VFS.getBufferForFile("/etc/redhat-release");
118
119
2.28k
  if (File) {
120
1
    StringRef Data = File.get()->getBuffer();
121
1
    if (Data.startswith("Fedora release"))
122
0
      return Distro::Fedora;
123
1
    if (Data.startswith("Red Hat Enterprise Linux") ||
124
1
        Data.startswith("CentOS") || 
Data.startswith("Scientific Linux")0
) {
125
1
      if (Data.contains("release 7"))
126
1
        return Distro::RHEL7;
127
0
      else if (Data.contains("release 6"))
128
0
        return Distro::RHEL6;
129
0
      else if (Data.contains("release 5"))
130
0
        return Distro::RHEL5;
131
1
    }
132
0
    return Distro::UnknownDistro;
133
1
  }
134
135
  // ...for Debian
136
2.28k
  File = VFS.getBufferForFile("/etc/debian_version");
137
2.28k
  if (File) {
138
2
    StringRef Data = File.get()->getBuffer();
139
    // Contents: < major.minor > or < codename/sid >
140
2
    int MajorVersion;
141
2
    if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
142
1
      switch (MajorVersion) {
143
0
      case 5:
144
0
        return Distro::DebianLenny;
145
0
      case 6:
146
0
        return Distro::DebianSqueeze;
147
0
      case 7:
148
0
        return Distro::DebianWheezy;
149
1
      case 8:
150
1
        return Distro::DebianJessie;
151
0
      case 9:
152
0
        return Distro::DebianStretch;
153
0
      case 10:
154
0
        return Distro::DebianBuster;
155
0
      case 11:
156
0
        return Distro::DebianBullseye;
157
0
      case 12:
158
0
        return Distro::DebianBookworm;
159
0
      case 13:
160
0
        return Distro::DebianTrixie;
161
0
      default:
162
0
        return Distro::UnknownDistro;
163
1
      }
164
1
    }
165
1
    return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
166
1
        .Case("squeeze/sid", Distro::DebianSqueeze)
167
1
        .Case("wheezy/sid", Distro::DebianWheezy)
168
1
        .Case("jessie/sid", Distro::DebianJessie)
169
1
        .Case("stretch/sid", Distro::DebianStretch)
170
1
        .Case("buster/sid", Distro::DebianBuster)
171
1
        .Case("bullseye/sid", Distro::DebianBullseye)
172
1
        .Case("bookworm/sid", Distro::DebianBookworm)
173
1
        .Case("trixie/sid", Distro::DebianTrixie)
174
1
        .Default(Distro::UnknownDistro);
175
2
  }
176
177
  // ...for SUSE
178
2.28k
  File = VFS.getBufferForFile("/etc/SuSE-release");
179
2.28k
  if (File) {
180
1
    StringRef Data = File.get()->getBuffer();
181
1
    SmallVector<StringRef, 8> Lines;
182
1
    Data.split(Lines, "\n");
183
2
    for (const StringRef &Line : Lines) {
184
2
      if (!Line.trim().startswith("VERSION"))
185
1
        continue;
186
1
      std::pair<StringRef, StringRef> SplitLine = Line.split('=');
187
      // Old versions have split VERSION and PATCHLEVEL
188
      // Newer versions use VERSION = x.y
189
1
      std::pair<StringRef, StringRef> SplitVer =
190
1
          SplitLine.second.trim().split('.');
191
1
      int Version;
192
193
      // OpenSUSE/SLES 10 and older are not supported and not compatible
194
      // with our rules, so just treat them as Distro::UnknownDistro.
195
1
      if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
196
0
        return Distro::OpenSUSE;
197
1
      return Distro::UnknownDistro;
198
1
    }
199
0
    return Distro::UnknownDistro;
200
1
  }
201
202
  // ...and others.
203
2.28k
  if (VFS.exists("/etc/gentoo-release"))
204
0
    return Distro::Gentoo;
205
206
2.28k
  return Distro::UnknownDistro;
207
2.28k
}
208
209
static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
210
58.0k
                                    const llvm::Triple &TargetOrHost) {
211
  // If we don't target Linux, no need to check the distro. This saves a few
212
  // OS calls.
213
58.0k
  if (!TargetOrHost.isOSLinux())
214
46.5k
    return Distro::UnknownDistro;
215
216
  // True if we're backed by a real file system.
217
11.5k
  const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);
218
219
  // If the host is not running Linux, and we're backed by a real file
220
  // system, no need to check the distro. This is the case where someone
221
  // is cross-compiling from BSD or Windows to Linux, and it would be
222
  // meaningless to try to figure out the "distro" of the non-Linux host.
223
11.5k
  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
224
11.5k
  if (
!HostTriple.isOSLinux()11.5k
&& onRealFS)
225
9.23k
    return Distro::UnknownDistro;
226
227
2.29k
  if (onRealFS) {
228
    // If we're backed by a real file system, perform
229
    // the detection only once and save the result.
230
0
    static Distro::DistroType LinuxDistro = DetectDistro(VFS);
231
0
    return LinuxDistro;
232
0
  }
233
  // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
234
  // which is not "real".
235
2.29k
  return DetectDistro(VFS);
236
2.29k
}
237
238
Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
239
58.0k
    : DistroVal(GetDistro(VFS, TargetOrHost)) {}