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