Coverage Report

Created: 2024-01-17 10:31

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