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