Michal Gorny | 67e199e | 2016-11-28 21:11:14 +0000 | [diff] [blame^] | 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 | |
| 20 | static Distro::DistroType DetectDistro(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 | .Default(Distro::UnknownDistro); |
| 51 | if (Version != Distro::UnknownDistro) |
| 52 | return Version; |
| 53 | } |
| 54 | |
| 55 | File = VFS.getBufferForFile("/etc/redhat-release"); |
| 56 | if (File) { |
| 57 | StringRef Data = File.get()->getBuffer(); |
| 58 | if (Data.startswith("Fedora release")) |
| 59 | return Distro::Fedora; |
| 60 | if (Data.startswith("Red Hat Enterprise Linux") || |
| 61 | Data.startswith("CentOS") || |
| 62 | Data.startswith("Scientific Linux")) { |
| 63 | if (Data.find("release 7") != StringRef::npos) |
| 64 | return Distro::RHEL7; |
| 65 | else if (Data.find("release 6") != StringRef::npos) |
| 66 | return Distro::RHEL6; |
| 67 | else if (Data.find("release 5") != StringRef::npos) |
| 68 | return Distro::RHEL5; |
| 69 | } |
| 70 | return Distro::UnknownDistro; |
| 71 | } |
| 72 | |
| 73 | File = VFS.getBufferForFile("/etc/debian_version"); |
| 74 | if (File) { |
| 75 | StringRef Data = File.get()->getBuffer(); |
| 76 | // Contents: < major.minor > or < codename/sid > |
| 77 | int MajorVersion; |
| 78 | if (!Data.split('.').first.getAsInteger(10, MajorVersion)) { |
| 79 | switch (MajorVersion) { |
| 80 | case 5: |
| 81 | return Distro::DebianLenny; |
| 82 | case 6: |
| 83 | return Distro::DebianSqueeze; |
| 84 | case 7: |
| 85 | return Distro::DebianWheezy; |
| 86 | case 8: |
| 87 | return Distro::DebianJessie; |
| 88 | case 9: |
| 89 | return Distro::DebianStretch; |
| 90 | default: |
| 91 | return Distro::UnknownDistro; |
| 92 | } |
| 93 | } |
| 94 | return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first) |
| 95 | .Case("squeeze/sid", Distro::DebianSqueeze) |
| 96 | .Case("wheezy/sid", Distro::DebianWheezy) |
| 97 | .Case("jessie/sid", Distro::DebianJessie) |
| 98 | .Case("stretch/sid", Distro::DebianStretch) |
| 99 | .Default(Distro::UnknownDistro); |
| 100 | } |
| 101 | |
| 102 | File = VFS.getBufferForFile("/etc/SuSE-release"); |
| 103 | if (File) { |
| 104 | StringRef Data = File.get()->getBuffer(); |
| 105 | SmallVector<StringRef, 8> Lines; |
| 106 | Data.split(Lines, "\n"); |
| 107 | for (const StringRef& Line : Lines) { |
| 108 | if (!Line.trim().startswith("VERSION")) |
| 109 | continue; |
| 110 | std::pair<StringRef, StringRef> SplitLine = Line.split('='); |
| 111 | int Version; |
| 112 | // OpenSUSE/SLES 10 and older are not supported and not compatible |
| 113 | // with our rules, so just treat them as Distro::UnknownDistro. |
| 114 | if (!SplitLine.second.trim().getAsInteger(10, Version) && |
| 115 | Version > 10) |
| 116 | return Distro::OpenSUSE; |
| 117 | return Distro::UnknownDistro; |
| 118 | } |
| 119 | return Distro::UnknownDistro; |
| 120 | } |
| 121 | |
| 122 | if (VFS.exists("/etc/exherbo-release")) |
| 123 | return Distro::Exherbo; |
| 124 | |
| 125 | if (VFS.exists("/etc/arch-release")) |
| 126 | return Distro::ArchLinux; |
| 127 | |
| 128 | return Distro::UnknownDistro; |
| 129 | } |
| 130 | |
| 131 | Distro::Distro(vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {} |