Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 1 | //===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Driver/Distro.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 10 | #include "clang/Basic/LLVM.h" |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/ADT/StringSwitch.h" |
Alexandre Ganea | 1abd4c9 | 2019-11-28 15:56:00 -0500 | [diff] [blame] | 14 | #include "llvm/ADT/Triple.h" |
Reid Kleckner | 213aea4 | 2020-03-11 15:39:28 -0700 | [diff] [blame] | 15 | #include "llvm/Support/ErrorOr.h" |
| 16 | #include "llvm/Support/Host.h" |
| 17 | #include "llvm/Support/MemoryBuffer.h" |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang::driver; |
| 20 | using namespace clang; |
| 21 | |
Alexandre Ganea | 1abd4c9 | 2019-11-28 15:56:00 -0500 | [diff] [blame] | 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 | |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 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"); |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame] | 45 | Distro::DistroType Version = Distro::UnknownDistro; |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 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) |
Sylvestre Ledru | 6cf800a | 2017-05-04 12:46:38 +0000 | [diff] [blame] | 68 | .Case("artful", Distro::UbuntuArtful) |
Sylvestre Ledru | f8e9ffa | 2017-10-25 14:21:33 +0000 | [diff] [blame] | 69 | .Case("bionic", Distro::UbuntuBionic) |
Sylvestre Ledru | 93dd5dc | 2018-05-10 08:45:43 +0000 | [diff] [blame] | 70 | .Case("cosmic", Distro::UbuntuCosmic) |
Sylvestre Ledru | 99682d0 | 2018-11-04 17:41:41 +0000 | [diff] [blame] | 71 | .Case("disco", Distro::UbuntuDisco) |
Sylvestre Ledru | 4540141 | 2019-04-19 13:43:28 +0000 | [diff] [blame] | 72 | .Case("eoan", Distro::UbuntuEoan) |
Sylvestre Ledru | 42effc1 | 2019-11-16 12:20:47 +0100 | [diff] [blame] | 73 | .Case("focal", Distro::UbuntuFocal) |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 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; |
Sylvestre Ledru | a3436bb | 2017-10-25 14:25:28 +0000 | [diff] [blame] | 114 | case 10: |
| 115 | return Distro::DebianBuster; |
Sylvestre Ledru | 54a93a3 | 2019-04-19 13:46:58 +0000 | [diff] [blame] | 116 | case 11: |
| 117 | return Distro::DebianBullseye; |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 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) |
Sylvestre Ledru | 5505ad3 | 2019-04-19 13:48:52 +0000 | [diff] [blame] | 127 | .Case("buster/sid", Distro::DebianBuster) |
| 128 | .Case("bullseye/sid", Distro::DebianBullseye) |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 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('='); |
Michal Gorny | 047e099 | 2016-11-28 21:11:18 +0000 | [diff] [blame] | 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('.'); |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 144 | int Version; |
Michal Gorny | 047e099 | 2016-11-28 21:11:18 +0000 | [diff] [blame] | 145 | |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 146 | // OpenSUSE/SLES 10 and older are not supported and not compatible |
| 147 | // with our rules, so just treat them as Distro::UnknownDistro. |
Michal Gorny | 047e099 | 2016-11-28 21:11:18 +0000 | [diff] [blame] | 148 | if (!SplitVer.first.getAsInteger(10, Version) && Version > 10) |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 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 | |
Martell Malone | 13c5d73 | 2017-11-19 00:08:12 +0000 | [diff] [blame] | 158 | if (VFS.exists("/etc/alpine-release")) |
| 159 | return Distro::AlpineLinux; |
| 160 | |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 161 | if (VFS.exists("/etc/arch-release")) |
| 162 | return Distro::ArchLinux; |
| 163 | |
Michal Gorny | f241228 | 2018-12-23 15:07:19 +0000 | [diff] [blame] | 164 | if (VFS.exists("/etc/gentoo-release")) |
| 165 | return Distro::Gentoo; |
| 166 | |
Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame] | 167 | return Distro::UnknownDistro; |
| 168 | } |
| 169 | |
Alexandre Ganea | 1abd4c9 | 2019-11-28 15:56:00 -0500 | [diff] [blame] | 170 | Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost) |
| 171 | : DistroVal(DetectDistro(VFS, TargetOrHost)) {} |