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 17 using namespace clang::driver; 18 using namespace clang; 19 20 static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) { 21 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File = 22 VFS.getBufferForFile("/etc/lsb-release"); 23 if (File) { 24 StringRef Data = File.get()->getBuffer(); 25 SmallVector<StringRef, 16> Lines; 26 Data.split(Lines, "\n"); 27 Distro::DistroType Version = Distro::UnknownDistro; 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 .Case("disco", Distro::UbuntuDisco) 54 .Case("eoan", Distro::UbuntuEoan) 55 .Default(Distro::UnknownDistro); 56 if (Version != Distro::UnknownDistro) 57 return Version; 58 } 59 60 File = VFS.getBufferForFile("/etc/redhat-release"); 61 if (File) { 62 StringRef Data = File.get()->getBuffer(); 63 if (Data.startswith("Fedora release")) 64 return Distro::Fedora; 65 if (Data.startswith("Red Hat Enterprise Linux") || 66 Data.startswith("CentOS") || 67 Data.startswith("Scientific Linux")) { 68 if (Data.find("release 7") != StringRef::npos) 69 return Distro::RHEL7; 70 else if (Data.find("release 6") != StringRef::npos) 71 return Distro::RHEL6; 72 else if (Data.find("release 5") != StringRef::npos) 73 return Distro::RHEL5; 74 } 75 return Distro::UnknownDistro; 76 } 77 78 File = VFS.getBufferForFile("/etc/debian_version"); 79 if (File) { 80 StringRef Data = File.get()->getBuffer(); 81 // Contents: < major.minor > or < codename/sid > 82 int MajorVersion; 83 if (!Data.split('.').first.getAsInteger(10, MajorVersion)) { 84 switch (MajorVersion) { 85 case 5: 86 return Distro::DebianLenny; 87 case 6: 88 return Distro::DebianSqueeze; 89 case 7: 90 return Distro::DebianWheezy; 91 case 8: 92 return Distro::DebianJessie; 93 case 9: 94 return Distro::DebianStretch; 95 case 10: 96 return Distro::DebianBuster; 97 case 11: 98 return Distro::DebianBullseye; 99 default: 100 return Distro::UnknownDistro; 101 } 102 } 103 return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first) 104 .Case("squeeze/sid", Distro::DebianSqueeze) 105 .Case("wheezy/sid", Distro::DebianWheezy) 106 .Case("jessie/sid", Distro::DebianJessie) 107 .Case("stretch/sid", Distro::DebianStretch) 108 .Case("buster/sid", Distro::DebianBuster) 109 .Case("bullseye/sid", Distro::DebianBullseye) 110 .Default(Distro::UnknownDistro); 111 } 112 113 File = VFS.getBufferForFile("/etc/SuSE-release"); 114 if (File) { 115 StringRef Data = File.get()->getBuffer(); 116 SmallVector<StringRef, 8> Lines; 117 Data.split(Lines, "\n"); 118 for (const StringRef& Line : Lines) { 119 if (!Line.trim().startswith("VERSION")) 120 continue; 121 std::pair<StringRef, StringRef> SplitLine = Line.split('='); 122 // Old versions have split VERSION and PATCHLEVEL 123 // Newer versions use VERSION = x.y 124 std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.'); 125 int Version; 126 127 // OpenSUSE/SLES 10 and older are not supported and not compatible 128 // with our rules, so just treat them as Distro::UnknownDistro. 129 if (!SplitVer.first.getAsInteger(10, Version) && Version > 10) 130 return Distro::OpenSUSE; 131 return Distro::UnknownDistro; 132 } 133 return Distro::UnknownDistro; 134 } 135 136 if (VFS.exists("/etc/exherbo-release")) 137 return Distro::Exherbo; 138 139 if (VFS.exists("/etc/alpine-release")) 140 return Distro::AlpineLinux; 141 142 if (VFS.exists("/etc/arch-release")) 143 return Distro::ArchLinux; 144 145 if (VFS.exists("/etc/gentoo-release")) 146 return Distro::Gentoo; 147 148 return Distro::UnknownDistro; 149 } 150 151 Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {} 152