head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC4:1.1.1.2
	netbsd-11-0-RC3:1.1.1.2
	netbsd-11-0-RC2:1.1.1.2
	netbsd-11-0-RC1:1.1.1.2
	perseant-exfatfs-base-20250801:1.1.1.2
	netbsd-11:1.1.1.2.0.8
	netbsd-11-base:1.1.1.2
	netbsd-10-1-RELEASE:1.1.1.2
	perseant-exfatfs-base-20240630:1.1.1.2
	perseant-exfatfs:1.1.1.2.0.6
	perseant-exfatfs-base:1.1.1.2
	netbsd-10-0-RELEASE:1.1.1.2
	netbsd-10-0-RC6:1.1.1.2
	netbsd-10-0-RC5:1.1.1.2
	netbsd-10-0-RC4:1.1.1.2
	netbsd-10-0-RC3:1.1.1.2
	netbsd-10-0-RC2:1.1.1.2
	netbsd-10-0-RC1:1.1.1.2
	netbsd-10:1.1.1.2.0.4
	netbsd-10-base:1.1.1.2
	cjep_sun2x-base1:1.1.1.2
	cjep_sun2x:1.1.1.2.0.2
	cjep_sun2x-base:1.1.1.2
	cjep_staticlib_x-base1:1.1.1.2
	LLVM-249b40b558955afe5ac2b549edcf2d7f859c8cc9:1.1.1.2
	cjep_staticlib_x:1.1.1.1.0.4
	cjep_staticlib_x-base:1.1.1.1
	phil-wifi-20200421:1.1.1.1
	phil-wifi-20200411:1.1.1.1
	is-mlppp:1.1.1.1.0.2
	is-mlppp-base:1.1.1.1
	phil-wifi-20200406:1.1.1.1
	phil-wifi-20191119:1.1.1.1
	LLVM-01f3a59fb3e2542fce74c768718f594d0debd0da:1.1.1.1
	LLVM:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2019.11.08.14.30.02;	author joerg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	lIv69tfiG0KHw3KB;

1.1.1.1
date	2019.11.08.14.30.02;	author joerg;	state Exp;
branches
	1.1.1.1.4.1;
next	1.1.1.2;
commitid	lIv69tfiG0KHw3KB;

1.1.1.2
date	2021.05.30.01.25.34;	author joerg;	state Exp;
branches;
next	;
commitid	uhgdinROdC6tU6VC;

1.1.1.1.4.1
date	2021.05.31.22.07.14;	author cjep;	state Exp;
branches;
next	;
commitid	eWz9SBW0XqKjJlVC;


desc
@@


1.1
log
@Initial revision
@
text
@//===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Driver/Distro.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"

using namespace clang::driver;
using namespace clang;

static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
      VFS.getBufferForFile("/etc/lsb-release");
  if (File) {
    StringRef Data = File.get()->getBuffer();
    SmallVector<StringRef, 16> Lines;
    Data.split(Lines, "\n");
    Distro::DistroType Version = Distro::UnknownDistro;
    for (StringRef Line : Lines)
      if (Version == Distro::UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
        Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
                      .Case("hardy", Distro::UbuntuHardy)
                      .Case("intrepid", Distro::UbuntuIntrepid)
                      .Case("jaunty", Distro::UbuntuJaunty)
                      .Case("karmic", Distro::UbuntuKarmic)
                      .Case("lucid", Distro::UbuntuLucid)
                      .Case("maverick", Distro::UbuntuMaverick)
                      .Case("natty", Distro::UbuntuNatty)
                      .Case("oneiric", Distro::UbuntuOneiric)
                      .Case("precise", Distro::UbuntuPrecise)
                      .Case("quantal", Distro::UbuntuQuantal)
                      .Case("raring", Distro::UbuntuRaring)
                      .Case("saucy", Distro::UbuntuSaucy)
                      .Case("trusty", Distro::UbuntuTrusty)
                      .Case("utopic", Distro::UbuntuUtopic)
                      .Case("vivid", Distro::UbuntuVivid)
                      .Case("wily", Distro::UbuntuWily)
                      .Case("xenial", Distro::UbuntuXenial)
                      .Case("yakkety", Distro::UbuntuYakkety)
                      .Case("zesty", Distro::UbuntuZesty)
                      .Case("artful", Distro::UbuntuArtful)
                      .Case("bionic", Distro::UbuntuBionic)
                      .Case("cosmic", Distro::UbuntuCosmic)
                      .Case("disco", Distro::UbuntuDisco)
                      .Case("eoan", Distro::UbuntuEoan)
                      .Default(Distro::UnknownDistro);
    if (Version != Distro::UnknownDistro)
      return Version;
  }

  File = VFS.getBufferForFile("/etc/redhat-release");
  if (File) {
    StringRef Data = File.get()->getBuffer();
    if (Data.startswith("Fedora release"))
      return Distro::Fedora;
    if (Data.startswith("Red Hat Enterprise Linux") ||
        Data.startswith("CentOS") ||
        Data.startswith("Scientific Linux")) {
      if (Data.find("release 7") != StringRef::npos)
        return Distro::RHEL7;
      else if (Data.find("release 6") != StringRef::npos)
        return Distro::RHEL6;
      else if (Data.find("release 5") != StringRef::npos)
        return Distro::RHEL5;
    }
    return Distro::UnknownDistro;
  }

  File = VFS.getBufferForFile("/etc/debian_version");
  if (File) {
    StringRef Data = File.get()->getBuffer();
    // Contents: < major.minor > or < codename/sid >
    int MajorVersion;
    if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
      switch (MajorVersion) {
      case 5:
        return Distro::DebianLenny;
      case 6:
        return Distro::DebianSqueeze;
      case 7:
        return Distro::DebianWheezy;
      case 8:
        return Distro::DebianJessie;
      case 9:
        return Distro::DebianStretch;
      case 10:
        return Distro::DebianBuster;
      case 11:
        return Distro::DebianBullseye;
      default:
        return Distro::UnknownDistro;
      }
    }
    return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
        .Case("squeeze/sid", Distro::DebianSqueeze)
        .Case("wheezy/sid", Distro::DebianWheezy)
        .Case("jessie/sid", Distro::DebianJessie)
        .Case("stretch/sid", Distro::DebianStretch)
        .Case("buster/sid", Distro::DebianBuster)
        .Case("bullseye/sid", Distro::DebianBullseye)
        .Default(Distro::UnknownDistro);
  }

  File = VFS.getBufferForFile("/etc/SuSE-release");
  if (File) {
    StringRef Data = File.get()->getBuffer();
    SmallVector<StringRef, 8> Lines;
    Data.split(Lines, "\n");
    for (const StringRef& Line : Lines) {
      if (!Line.trim().startswith("VERSION"))
        continue;
      std::pair<StringRef, StringRef> SplitLine = Line.split('=');
      // Old versions have split VERSION and PATCHLEVEL
      // Newer versions use VERSION = x.y
      std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
      int Version;

      // OpenSUSE/SLES 10 and older are not supported and not compatible
      // with our rules, so just treat them as Distro::UnknownDistro.
      if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
        return Distro::OpenSUSE;
      return Distro::UnknownDistro;
    }
    return Distro::UnknownDistro;
  }

  if (VFS.exists("/etc/exherbo-release"))
    return Distro::Exherbo;

  if (VFS.exists("/etc/alpine-release"))
    return Distro::AlpineLinux;

  if (VFS.exists("/etc/arch-release"))
    return Distro::ArchLinux;

  if (VFS.exists("/etc/gentoo-release"))
    return Distro::Gentoo;

  return Distro::UnknownDistro;
}

Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}
@


1.1.1.1
log
@Import 01f3a59fb3e2542fce74c768718f594d0debd0da from the LLVM mono-repo:
clang (without test/, unittests/, www/)
llvm (without test/, unittests/)
@
text
@@


1.1.1.1.4.1
log
@sync with head
@
text
@a13 1
#include "llvm/ADT/Triple.h"
a14 1
#include "llvm/Support/Host.h"
a15 1
#include "llvm/Support/Threading.h"
d20 1
a20 29
static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
      VFS.getBufferForFile("/etc/os-release");
  if (!File)
    File = VFS.getBufferForFile("/usr/lib/os-release");
  if (!File)
    return Distro::UnknownDistro;

  SmallVector<StringRef, 16> Lines;
  File.get()->getBuffer().split(Lines, "\n");
  Distro::DistroType Version = Distro::UnknownDistro;

  // Obviously this can be improved a lot.
  for (StringRef Line : Lines)
    if (Version == Distro::UnknownDistro && Line.startswith("ID="))
      Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(3))
                    .Case("alpine", Distro::AlpineLinux)
                    .Case("fedora", Distro::Fedora)
                    .Case("gentoo", Distro::Gentoo)
                    .Case("arch", Distro::ArchLinux)
                    // On SLES, /etc/os-release was introduced in SLES 11.
                    .Case("sles", Distro::OpenSUSE)
                    .Case("opensuse", Distro::OpenSUSE)
                    .Case("exherbo", Distro::Exherbo)
                    .Default(Distro::UnknownDistro);
  return Version;
}

static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
d23 36
a58 60
  if (!File)
    return Distro::UnknownDistro;

  SmallVector<StringRef, 16> Lines;
  File.get()->getBuffer().split(Lines, "\n");
  Distro::DistroType Version = Distro::UnknownDistro;

  for (StringRef Line : Lines)
    if (Version == Distro::UnknownDistro &&
        Line.startswith("DISTRIB_CODENAME="))
      Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
                    .Case("hardy", Distro::UbuntuHardy)
                    .Case("intrepid", Distro::UbuntuIntrepid)
                    .Case("jaunty", Distro::UbuntuJaunty)
                    .Case("karmic", Distro::UbuntuKarmic)
                    .Case("lucid", Distro::UbuntuLucid)
                    .Case("maverick", Distro::UbuntuMaverick)
                    .Case("natty", Distro::UbuntuNatty)
                    .Case("oneiric", Distro::UbuntuOneiric)
                    .Case("precise", Distro::UbuntuPrecise)
                    .Case("quantal", Distro::UbuntuQuantal)
                    .Case("raring", Distro::UbuntuRaring)
                    .Case("saucy", Distro::UbuntuSaucy)
                    .Case("trusty", Distro::UbuntuTrusty)
                    .Case("utopic", Distro::UbuntuUtopic)
                    .Case("vivid", Distro::UbuntuVivid)
                    .Case("wily", Distro::UbuntuWily)
                    .Case("xenial", Distro::UbuntuXenial)
                    .Case("yakkety", Distro::UbuntuYakkety)
                    .Case("zesty", Distro::UbuntuZesty)
                    .Case("artful", Distro::UbuntuArtful)
                    .Case("bionic", Distro::UbuntuBionic)
                    .Case("cosmic", Distro::UbuntuCosmic)
                    .Case("disco", Distro::UbuntuDisco)
                    .Case("eoan", Distro::UbuntuEoan)
                    .Case("focal", Distro::UbuntuFocal)
                    .Case("groovy", Distro::UbuntuGroovy)
                    .Case("hirsute", Distro::UbuntuHirsute)
                    .Case("impish", Distro::UbuntuImpish)
                    .Default(Distro::UnknownDistro);
  return Version;
}

static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
  Distro::DistroType Version = Distro::UnknownDistro;

  // Newer freedesktop.org's compilant systemd-based systems
  // should provide /etc/os-release or /usr/lib/os-release.
  Version = DetectOsRelease(VFS);
  if (Version != Distro::UnknownDistro)
    return Version;

  // Older systems might provide /etc/lsb-release.
  Version = DetectLsbRelease(VFS);
  if (Version != Distro::UnknownDistro)
    return Version;

  // Otherwise try some distro-specific quirks for RedHat...
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
      VFS.getBufferForFile("/etc/redhat-release");
d60 1
d66 2
a67 1
        Data.startswith("CentOS") || Data.startswith("Scientific Linux")) {
a77 1
  // ...for Debian
a112 1
  // ...for SUSE
d118 1
a118 1
    for (const StringRef &Line : Lines) {
d124 1
a124 2
      std::pair<StringRef, StringRef> SplitVer =
          SplitLine.second.trim().split('.');
d136 9
a144 1
  // ...and others.
d151 1
a151 31
static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
                                    const llvm::Triple &TargetOrHost) {
  // If we don't target Linux, no need to check the distro. This saves a few
  // OS calls.
  if (!TargetOrHost.isOSLinux())
    return Distro::UnknownDistro;

  // True if we're backed by a real file system.
  const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);

  // If the host is not running Linux, and we're backed by a real file
  // system, no need to check the distro. This is the case where someone
  // is cross-compiling from BSD or Windows to Linux, and it would be
  // meaningless to try to figure out the "distro" of the non-Linux host.
  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
  if (!HostTriple.isOSLinux() && onRealFS)
    return Distro::UnknownDistro;

  if (onRealFS) {
    // If we're backed by a real file system, perform
    // the detection only once and save the result.
    static Distro::DistroType LinuxDistro = DetectDistro(VFS);
    return LinuxDistro;
  }
  // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
  // which is not "real".
  return DetectDistro(VFS);
}

Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
    : DistroVal(GetDistro(VFS, TargetOrHost)) {}
@


1.1.1.2
log
@Import clang 249b40b558955afe5ac2b549edcf2d7f859c8cc9.
@
text
@a13 1
#include "llvm/ADT/Triple.h"
a14 1
#include "llvm/Support/Host.h"
a15 1
#include "llvm/Support/Threading.h"
d20 1
a20 29
static Distro::DistroType DetectOsRelease(llvm::vfs::FileSystem &VFS) {
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
      VFS.getBufferForFile("/etc/os-release");
  if (!File)
    File = VFS.getBufferForFile("/usr/lib/os-release");
  if (!File)
    return Distro::UnknownDistro;

  SmallVector<StringRef, 16> Lines;
  File.get()->getBuffer().split(Lines, "\n");
  Distro::DistroType Version = Distro::UnknownDistro;

  // Obviously this can be improved a lot.
  for (StringRef Line : Lines)
    if (Version == Distro::UnknownDistro && Line.startswith("ID="))
      Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(3))
                    .Case("alpine", Distro::AlpineLinux)
                    .Case("fedora", Distro::Fedora)
                    .Case("gentoo", Distro::Gentoo)
                    .Case("arch", Distro::ArchLinux)
                    // On SLES, /etc/os-release was introduced in SLES 11.
                    .Case("sles", Distro::OpenSUSE)
                    .Case("opensuse", Distro::OpenSUSE)
                    .Case("exherbo", Distro::Exherbo)
                    .Default(Distro::UnknownDistro);
  return Version;
}

static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
d23 36
a58 60
  if (!File)
    return Distro::UnknownDistro;

  SmallVector<StringRef, 16> Lines;
  File.get()->getBuffer().split(Lines, "\n");
  Distro::DistroType Version = Distro::UnknownDistro;

  for (StringRef Line : Lines)
    if (Version == Distro::UnknownDistro &&
        Line.startswith("DISTRIB_CODENAME="))
      Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
                    .Case("hardy", Distro::UbuntuHardy)
                    .Case("intrepid", Distro::UbuntuIntrepid)
                    .Case("jaunty", Distro::UbuntuJaunty)
                    .Case("karmic", Distro::UbuntuKarmic)
                    .Case("lucid", Distro::UbuntuLucid)
                    .Case("maverick", Distro::UbuntuMaverick)
                    .Case("natty", Distro::UbuntuNatty)
                    .Case("oneiric", Distro::UbuntuOneiric)
                    .Case("precise", Distro::UbuntuPrecise)
                    .Case("quantal", Distro::UbuntuQuantal)
                    .Case("raring", Distro::UbuntuRaring)
                    .Case("saucy", Distro::UbuntuSaucy)
                    .Case("trusty", Distro::UbuntuTrusty)
                    .Case("utopic", Distro::UbuntuUtopic)
                    .Case("vivid", Distro::UbuntuVivid)
                    .Case("wily", Distro::UbuntuWily)
                    .Case("xenial", Distro::UbuntuXenial)
                    .Case("yakkety", Distro::UbuntuYakkety)
                    .Case("zesty", Distro::UbuntuZesty)
                    .Case("artful", Distro::UbuntuArtful)
                    .Case("bionic", Distro::UbuntuBionic)
                    .Case("cosmic", Distro::UbuntuCosmic)
                    .Case("disco", Distro::UbuntuDisco)
                    .Case("eoan", Distro::UbuntuEoan)
                    .Case("focal", Distro::UbuntuFocal)
                    .Case("groovy", Distro::UbuntuGroovy)
                    .Case("hirsute", Distro::UbuntuHirsute)
                    .Case("impish", Distro::UbuntuImpish)
                    .Default(Distro::UnknownDistro);
  return Version;
}

static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
  Distro::DistroType Version = Distro::UnknownDistro;

  // Newer freedesktop.org's compilant systemd-based systems
  // should provide /etc/os-release or /usr/lib/os-release.
  Version = DetectOsRelease(VFS);
  if (Version != Distro::UnknownDistro)
    return Version;

  // Older systems might provide /etc/lsb-release.
  Version = DetectLsbRelease(VFS);
  if (Version != Distro::UnknownDistro)
    return Version;

  // Otherwise try some distro-specific quirks for RedHat...
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
      VFS.getBufferForFile("/etc/redhat-release");
d60 1
d66 2
a67 1
        Data.startswith("CentOS") || Data.startswith("Scientific Linux")) {
a77 1
  // ...for Debian
a112 1
  // ...for SUSE
d118 1
a118 1
    for (const StringRef &Line : Lines) {
d124 1
a124 2
      std::pair<StringRef, StringRef> SplitVer =
          SplitLine.second.trim().split('.');
d136 9
a144 1
  // ...and others.
d151 1
a151 31
static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS,
                                    const llvm::Triple &TargetOrHost) {
  // If we don't target Linux, no need to check the distro. This saves a few
  // OS calls.
  if (!TargetOrHost.isOSLinux())
    return Distro::UnknownDistro;

  // True if we're backed by a real file system.
  const bool onRealFS = (llvm::vfs::getRealFileSystem() == &VFS);

  // If the host is not running Linux, and we're backed by a real file
  // system, no need to check the distro. This is the case where someone
  // is cross-compiling from BSD or Windows to Linux, and it would be
  // meaningless to try to figure out the "distro" of the non-Linux host.
  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
  if (!HostTriple.isOSLinux() && onRealFS)
    return Distro::UnknownDistro;

  if (onRealFS) {
    // If we're backed by a real file system, perform
    // the detection only once and save the result.
    static Distro::DistroType LinuxDistro = DetectDistro(VFS);
    return LinuxDistro;
  }
  // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem,
  // which is not "real".
  return DetectDistro(VFS);
}

Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
    : DistroVal(GetDistro(VFS, TargetOrHost)) {}
@

