clang  7.0.0svn
Distro.cpp
Go to the documentation of this file.
1 //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Driver/Distro.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 
17 using namespace clang::driver;
18 using namespace clang;
19 
21  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
22  VFS.getBufferForFile("/etc/lsb-release");
23  if (File) {
24  StringRef Data = File.get()->getBuffer();
26  Data.split(Lines, "\n");
28  for (StringRef Line : Lines)
29  if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
30  Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
31  .Case("hardy", Distro::UbuntuHardy)
32  .Case("intrepid", Distro::UbuntuIntrepid)
33  .Case("jaunty", Distro::UbuntuJaunty)
34  .Case("karmic", Distro::UbuntuKarmic)
35  .Case("lucid", Distro::UbuntuLucid)
36  .Case("maverick", Distro::UbuntuMaverick)
37  .Case("natty", Distro::UbuntuNatty)
38  .Case("oneiric", Distro::UbuntuOneiric)
39  .Case("precise", Distro::UbuntuPrecise)
40  .Case("quantal", Distro::UbuntuQuantal)
41  .Case("raring", Distro::UbuntuRaring)
42  .Case("saucy", Distro::UbuntuSaucy)
43  .Case("trusty", Distro::UbuntuTrusty)
44  .Case("utopic", Distro::UbuntuUtopic)
45  .Case("vivid", Distro::UbuntuVivid)
46  .Case("wily", Distro::UbuntuWily)
47  .Case("xenial", Distro::UbuntuXenial)
48  .Case("yakkety", Distro::UbuntuYakkety)
49  .Case("zesty", Distro::UbuntuZesty)
50  .Case("artful", Distro::UbuntuArtful)
51  .Case("bionic", Distro::UbuntuBionic)
52  .Case("cosmic", Distro::UbuntuCosmic)
53  .Default(Distro::UnknownDistro);
54  if (Version != Distro::UnknownDistro)
55  return Version;
56  }
57 
58  File = VFS.getBufferForFile("/etc/redhat-release");
59  if (File) {
60  StringRef Data = File.get()->getBuffer();
61  if (Data.startswith("Fedora release"))
62  return Distro::Fedora;
63  if (Data.startswith("Red Hat Enterprise Linux") ||
64  Data.startswith("CentOS") ||
65  Data.startswith("Scientific Linux")) {
66  if (Data.find("release 7") != StringRef::npos)
67  return Distro::RHEL7;
68  else if (Data.find("release 6") != StringRef::npos)
69  return Distro::RHEL6;
70  else if (Data.find("release 5") != StringRef::npos)
71  return Distro::RHEL5;
72  }
73  return Distro::UnknownDistro;
74  }
75 
76  File = VFS.getBufferForFile("/etc/debian_version");
77  if (File) {
78  StringRef Data = File.get()->getBuffer();
79  // Contents: < major.minor > or < codename/sid >
80  int MajorVersion;
81  if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
82  switch (MajorVersion) {
83  case 5:
84  return Distro::DebianLenny;
85  case 6:
86  return Distro::DebianSqueeze;
87  case 7:
88  return Distro::DebianWheezy;
89  case 8:
90  return Distro::DebianJessie;
91  case 9:
92  return Distro::DebianStretch;
93  case 10:
94  return Distro::DebianBuster;
95  default:
96  return Distro::UnknownDistro;
97  }
98  }
99  return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
100  .Case("squeeze/sid", Distro::DebianSqueeze)
101  .Case("wheezy/sid", Distro::DebianWheezy)
102  .Case("jessie/sid", Distro::DebianJessie)
103  .Case("stretch/sid", Distro::DebianStretch)
104  .Default(Distro::UnknownDistro);
105  }
106 
107  File = VFS.getBufferForFile("/etc/SuSE-release");
108  if (File) {
109  StringRef Data = File.get()->getBuffer();
111  Data.split(Lines, "\n");
112  for (const StringRef& Line : Lines) {
113  if (!Line.trim().startswith("VERSION"))
114  continue;
115  std::pair<StringRef, StringRef> SplitLine = Line.split('=');
116  // Old versions have split VERSION and PATCHLEVEL
117  // Newer versions use VERSION = x.y
118  std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
119  int Version;
120 
121  // OpenSUSE/SLES 10 and older are not supported and not compatible
122  // with our rules, so just treat them as Distro::UnknownDistro.
123  if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
124  return Distro::OpenSUSE;
125  return Distro::UnknownDistro;
126  }
127  return Distro::UnknownDistro;
128  }
129 
130  if (VFS.exists("/etc/exherbo-release"))
131  return Distro::Exherbo;
132 
133  if (VFS.exists("/etc/alpine-release"))
134  return Distro::AlpineLinux;
135 
136  if (VFS.exists("/etc/arch-release"))
137  return Distro::ArchLinux;
138 
139  return Distro::UnknownDistro;
140 }
141 
142 Distro::Distro(vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}
static Distro::DistroType DetectDistro(vfs::FileSystem &VFS)
Definition: Distro.cpp:20
The virtual file system interface.
const AnnotatedLine * Line
Distro()
Default constructor leaves the distribution unknown.
Definition: Distro.h:77
Dataflow Directional Tag Classes.
bool exists(const Twine &Path)
Check whether a file exists. Provided for convenience.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
This is a convenience method that opens a file, gets its content and then closes the file...