Coverage Report

Created: 2020-09-19 12:23

/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
19
using namespace clang::driver;
20
using namespace clang;
21
22
static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS,
23
68.9k
                                       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
68.9k
  if (!TargetOrHost.isOSLinux())
27
58.9k
    return Distro::UnknownDistro;
28
9.99k
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
9.99k
  IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS =
34
9.99k
      llvm::vfs::getRealFileSystem();
35
9.99k
  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
36
9.99k
  if (!HostTriple.isOSLinux() && &VFS == RealFS.get())
37
8.06k
    return Distro::UnknownDistro;
38
1.93k
39
1.93k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
40
1.93k
      VFS.getBufferForFile("/etc/lsb-release");
41
1.93k
  if (File) {
42
2
    StringRef Data = File.get()->getBuffer();
43
2
    SmallVector<StringRef, 16> Lines;
44
2
    Data.split(Lines, "\n");
45
2
    Distro::DistroType Version = Distro::UnknownDistro;
46
2
    for (StringRef Line : Lines)
47
10
      if (Version == Distro::UnknownDistro && 
Line.startswith("DISTRIB_CODENAME=")6
)
48
2
        Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
49
2
                      .Case("hardy", Distro::UbuntuHardy)
50
2
                      .Case("intrepid", Distro::UbuntuIntrepid)
51
2
                      .Case("jaunty", Distro::UbuntuJaunty)
52
2
                      .Case("karmic", Distro::UbuntuKarmic)
53
2
                      .Case("lucid", Distro::UbuntuLucid)
54
2
                      .Case("maverick", Distro::UbuntuMaverick)
55
2
                      .Case("natty", Distro::UbuntuNatty)
56
2
                      .Case("oneiric", Distro::UbuntuOneiric)
57
2
                      .Case("precise", Distro::UbuntuPrecise)
58
2
                      .Case("quantal", Distro::UbuntuQuantal)
59
2
                      .Case("raring", Distro::UbuntuRaring)
60
2
                      .Case("saucy", Distro::UbuntuSaucy)
61
2
                      .Case("trusty", Distro::UbuntuTrusty)
62
2
                      .Case("utopic", Distro::UbuntuUtopic)
63
2
                      .Case("vivid", Distro::UbuntuVivid)
64
2
                      .Case("wily", Distro::UbuntuWily)
65
2
                      .Case("xenial", Distro::UbuntuXenial)
66
2
                      .Case("yakkety", Distro::UbuntuYakkety)
67
2
                      .Case("zesty", Distro::UbuntuZesty)
68
2
                      .Case("artful", Distro::UbuntuArtful)
69
2
                      .Case("bionic", Distro::UbuntuBionic)
70
2
                      .Case("cosmic", Distro::UbuntuCosmic)
71
2
                      .Case("disco", Distro::UbuntuDisco)
72
2
                      .Case("eoan", Distro::UbuntuEoan)
73
2
                      .Case("focal", Distro::UbuntuFocal)
74
2
                      .Case("groovy", Distro::UbuntuGroovy)
75
2
                      .Default(Distro::UnknownDistro);
76
2
    if (Version != Distro::UnknownDistro)
77
2
      return Version;
78
1.93k
  }
79
1.93k
80
1.93k
  File = VFS.getBufferForFile("/etc/redhat-release");
81
1.93k
  if (File) {
82
2
    StringRef Data = File.get()->getBuffer();
83
2
    if (Data.startswith("Fedora release"))
84
1
      return Distro::Fedora;
85
1
    if (Data.startswith("Red Hat Enterprise Linux") ||
86
1
        Data.startswith("CentOS") ||
87
1
        
Data.startswith("Scientific Linux")0
) {
88
1
      if (Data.find("release 7") != StringRef::npos)
89
1
        return Distro::RHEL7;
90
0
      else if (Data.find("release 6") != StringRef::npos)
91
0
        return Distro::RHEL6;
92
0
      else if (Data.find("release 5") != StringRef::npos)
93
0
        return Distro::RHEL5;
94
0
    }
95
0
    return Distro::UnknownDistro;
96
0
  }
97
1.92k
98
1.92k
  File = VFS.getBufferForFile("/etc/debian_version");
99
1.92k
  if (File) {
100
2
    StringRef Data = File.get()->getBuffer();
101
    // Contents: < major.minor > or < codename/sid >
102
2
    int MajorVersion;
103
2
    if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
104
1
      switch (MajorVersion) {
105
0
      case 5:
106
0
        return Distro::DebianLenny;
107
0
      case 6:
108
0
        return Distro::DebianSqueeze;
109
0
      case 7:
110
0
        return Distro::DebianWheezy;
111
1
      case 8:
112
1
        return Distro::DebianJessie;
113
0
      case 9:
114
0
        return Distro::DebianStretch;
115
0
      case 10:
116
0
        return Distro::DebianBuster;
117
0
      case 11:
118
0
        return Distro::DebianBullseye;
119
0
      default:
120
0
        return Distro::UnknownDistro;
121
1
      }
122
1
    }
123
1
    return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
124
1
        .Case("squeeze/sid", Distro::DebianSqueeze)
125
1
        .Case("wheezy/sid", Distro::DebianWheezy)
126
1
        .Case("jessie/sid", Distro::DebianJessie)
127
1
        .Case("stretch/sid", Distro::DebianStretch)
128
1
        .Case("buster/sid", Distro::DebianBuster)
129
1
        .Case("bullseye/sid", Distro::DebianBullseye)
130
1
        .Default(Distro::UnknownDistro);
131
1
  }
132
1.92k
133
1.92k
  File = VFS.getBufferForFile("/etc/SuSE-release");
134
1.92k
  if (File) {
135
3
    StringRef Data = File.get()->getBuffer();
136
3
    SmallVector<StringRef, 8> Lines;
137
3
    Data.split(Lines, "\n");
138
6
    for (const StringRef& Line : Lines) {
139
6
      if (!Line.trim().startswith("VERSION"))
140
3
        continue;
141
3
      std::pair<StringRef, StringRef> SplitLine = Line.split('=');
142
      // Old versions have split VERSION and PATCHLEVEL
143
      // Newer versions use VERSION = x.y
144
3
      std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
145
3
      int Version;
146
3
147
      // OpenSUSE/SLES 10 and older are not supported and not compatible
148
      // with our rules, so just treat them as Distro::UnknownDistro.
149
3
      if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
150
2
        return Distro::OpenSUSE;
151
1
      return Distro::UnknownDistro;
152
1
    }
153
0
    return Distro::UnknownDistro;
154
1.92k
  }
155
1.92k
156
1.92k
  if (VFS.exists("/etc/exherbo-release"))
157
1
    return Distro::Exherbo;
158
1.92k
159
1.92k
  if (VFS.exists("/etc/alpine-release"))
160
0
    return Distro::AlpineLinux;
161
1.92k
162
1.92k
  if (VFS.exists("/etc/arch-release"))
163
1
    return Distro::ArchLinux;
164
1.92k
165
1.92k
  if (VFS.exists("/etc/gentoo-release"))
166
1
    return Distro::Gentoo;
167
1.92k
168
1.92k
  return Distro::UnknownDistro;
169
1.92k
}
170
171
Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
172
68.9k
    : DistroVal(DetectDistro(VFS, TargetOrHost)) {}