[PATCH] D25949: [Driver] Refactor distro detection & classification as a separate API

2016-11-28 Thread Michał Górny via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL288060: [Driver] Refactor distro detection & classification 
as a separate API (authored by mgorny).

Changed prior to commit:
  https://reviews.llvm.org/D25949?vs=78462=79442#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D25949

Files:
  cfe/trunk/include/clang/Driver/Distro.h
  cfe/trunk/lib/Driver/CMakeLists.txt
  cfe/trunk/lib/Driver/Distro.cpp
  cfe/trunk/lib/Driver/ToolChains.cpp

Index: cfe/trunk/include/clang/Driver/Distro.h
===
--- cfe/trunk/include/clang/Driver/Distro.h
+++ cfe/trunk/include/clang/Driver/Distro.h
@@ -0,0 +1,122 @@
+//===--- Distro.h - Linux distribution detection support *- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_DRIVER_DISTRO_H
+#define LLVM_CLANG_DRIVER_DISTRO_H
+
+#include "clang/Basic/VirtualFileSystem.h"
+
+namespace clang {
+namespace driver {
+
+/// Distro - Helper class for detecting and classifying Linux distributions.
+///
+/// This class encapsulates the clang Linux distribution detection mechanism
+/// as well as helper functions that match the specific (versioned) results
+/// into wider distribution classes.
+class Distro {
+public:
+  enum DistroType {
+// NB: Releases of a particular Linux distro should be kept together
+// in this enum, because some tests are done by integer comparison against
+// the first and last known member in the family, e.g. IsRedHat().
+ArchLinux,
+DebianLenny,
+DebianSqueeze,
+DebianWheezy,
+DebianJessie,
+DebianStretch,
+Exherbo,
+RHEL5,
+RHEL6,
+RHEL7,
+Fedora,
+OpenSUSE,
+UbuntuHardy,
+UbuntuIntrepid,
+UbuntuJaunty,
+UbuntuKarmic,
+UbuntuLucid,
+UbuntuMaverick,
+UbuntuNatty,
+UbuntuOneiric,
+UbuntuPrecise,
+UbuntuQuantal,
+UbuntuRaring,
+UbuntuSaucy,
+UbuntuTrusty,
+UbuntuUtopic,
+UbuntuVivid,
+UbuntuWily,
+UbuntuXenial,
+UbuntuYakkety,
+UbuntuZesty,
+UnknownDistro
+  };
+
+private:
+  /// The distribution, possibly with specific version.
+  DistroType DistroVal;
+
+public:
+  /// @name Constructors
+  /// @{
+
+  /// Default constructor leaves the distribution unknown.
+  Distro() : DistroVal() {}
+
+  /// Constructs a Distro type for specific distribution.
+  Distro(DistroType D) : DistroVal(D) {}
+
+  /// Detects the distribution using specified VFS.
+  explicit Distro(clang::vfs::FileSystem& VFS);
+
+  bool operator==(const Distro ) const {
+return DistroVal == Other.DistroVal;
+  }
+
+  bool operator!=(const Distro ) const {
+return DistroVal != Other.DistroVal;
+  }
+
+  bool operator>=(const Distro ) const {
+return DistroVal >= Other.DistroVal;
+  }
+
+  bool operator<=(const Distro ) const {
+return DistroVal <= Other.DistroVal;
+  }
+
+  /// @}
+  /// @name Convenience Predicates
+  /// @{
+
+  bool IsRedhat() const {
+return DistroVal == Fedora || (DistroVal >= RHEL5 && DistroVal <= RHEL7);
+  }
+
+  bool IsOpenSUSE() const {
+return DistroVal == OpenSUSE;
+  }
+
+  bool IsDebian() const {
+return DistroVal >= DebianLenny && DistroVal <= DebianStretch;
+  }
+
+  bool IsUbuntu() const {
+return DistroVal >= UbuntuHardy && DistroVal <= UbuntuZesty;
+  }
+ 
+  /// @}
+};
+
+} // end namespace driver
+} // end namespace clang
+
+#endif
Index: cfe/trunk/lib/Driver/CMakeLists.txt
===
--- cfe/trunk/lib/Driver/CMakeLists.txt
+++ cfe/trunk/lib/Driver/CMakeLists.txt
@@ -12,6 +12,7 @@
   Action.cpp
   Compilation.cpp
   CrossWindowsToolChain.cpp
+  Distro.cpp
   Driver.cpp
   DriverOptions.cpp
   Job.cpp
Index: cfe/trunk/lib/Driver/Distro.cpp
===
--- cfe/trunk/lib/Driver/Distro.cpp
+++ cfe/trunk/lib/Driver/Distro.cpp
@@ -0,0 +1,131 @@
+//===--- Distro.cpp - Linux distribution detection support --*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#include "clang/Driver/Distro.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(vfs::FileSystem ) {
+  llvm::ErrorOr File =
+  VFS.getBufferForFile("/etc/lsb-release");
+  if (File) {
+

[PATCH] D25949: [Driver] Refactor distro detection & classification as a separate API

2016-11-17 Thread Michał Górny via cfe-commits
mgorny updated this revision to Diff 78462.
mgorny added a comment.

Thanks for the review. I've rebased on top of current master (UbuntuZesty 
added), and now I will update the unit tests.


https://reviews.llvm.org/D25949

Files:
  include/clang/Driver/Distro.h
  lib/Driver/CMakeLists.txt
  lib/Driver/Distro.cpp
  lib/Driver/ToolChains.cpp

Index: lib/Driver/ToolChains.cpp
===
--- lib/Driver/ToolChains.cpp
+++ lib/Driver/ToolChains.cpp
@@ -14,6 +14,7 @@
 #include "clang/Basic/VirtualFileSystem.h"
 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
 #include "clang/Driver/Compilation.h"
+#include "clang/Driver/Distro.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
@@ -3885,171 +3886,6 @@
   }
 }
 
-/// Distribution (very bare-bones at the moment).
-
-enum Distro {
-  // NB: Releases of a particular Linux distro should be kept together
-  // in this enum, because some tests are done by integer comparison against
-  // the first and last known member in the family, e.g. IsRedHat().
-  ArchLinux,
-  DebianLenny,
-  DebianSqueeze,
-  DebianWheezy,
-  DebianJessie,
-  DebianStretch,
-  Exherbo,
-  RHEL5,
-  RHEL6,
-  RHEL7,
-  Fedora,
-  OpenSUSE,
-  UbuntuHardy,
-  UbuntuIntrepid,
-  UbuntuJaunty,
-  UbuntuKarmic,
-  UbuntuLucid,
-  UbuntuMaverick,
-  UbuntuNatty,
-  UbuntuOneiric,
-  UbuntuPrecise,
-  UbuntuQuantal,
-  UbuntuRaring,
-  UbuntuSaucy,
-  UbuntuTrusty,
-  UbuntuUtopic,
-  UbuntuVivid,
-  UbuntuWily,
-  UbuntuXenial,
-  UbuntuYakkety,
-  UbuntuZesty,
-  UnknownDistro
-};
-
-static bool IsRedhat(enum Distro Distro) {
-  return Distro == Fedora || (Distro >= RHEL5 && Distro <= RHEL7);
-}
-
-static bool IsOpenSUSE(enum Distro Distro) { return Distro == OpenSUSE; }
-
-static bool IsDebian(enum Distro Distro) {
-  return Distro >= DebianLenny && Distro <= DebianStretch;
-}
-
-static bool IsUbuntu(enum Distro Distro) {
-  return Distro >= UbuntuHardy && Distro <= UbuntuZesty;
-}
-
-static Distro DetectDistro(vfs::FileSystem ) {
-  llvm::ErrorOr File =
-  VFS.getBufferForFile("/etc/lsb-release");
-  if (File) {
-StringRef Data = File.get()->getBuffer();
-SmallVector Lines;
-Data.split(Lines, "\n");
-Distro Version = UnknownDistro;
-for (StringRef Line : Lines)
-  if (Version == UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
-Version = llvm::StringSwitch(Line.substr(17))
-  .Case("hardy", UbuntuHardy)
-  .Case("intrepid", UbuntuIntrepid)
-  .Case("jaunty", UbuntuJaunty)
-  .Case("karmic", UbuntuKarmic)
-  .Case("lucid", UbuntuLucid)
-  .Case("maverick", UbuntuMaverick)
-  .Case("natty", UbuntuNatty)
-  .Case("oneiric", UbuntuOneiric)
-  .Case("precise", UbuntuPrecise)
-  .Case("quantal", UbuntuQuantal)
-  .Case("raring", UbuntuRaring)
-  .Case("saucy", UbuntuSaucy)
-  .Case("trusty", UbuntuTrusty)
-  .Case("utopic", UbuntuUtopic)
-  .Case("vivid", UbuntuVivid)
-  .Case("wily", UbuntuWily)
-  .Case("xenial", UbuntuXenial)
-  .Case("yakkety", UbuntuYakkety)
-  .Case("zesty", UbuntuZesty)
-  .Default(UnknownDistro);
-if (Version != UnknownDistro)
-  return Version;
-  }
-
-  File = VFS.getBufferForFile("/etc/redhat-release");
-  if (File) {
-StringRef Data = File.get()->getBuffer();
-if (Data.startswith("Fedora release"))
-  return Fedora;
-if (Data.startswith("Red Hat Enterprise Linux") ||
-Data.startswith("CentOS") ||
-Data.startswith("Scientific Linux")) {
-  if (Data.find("release 7") != StringRef::npos)
-return RHEL7;
-  else if (Data.find("release 6") != StringRef::npos)
-return RHEL6;
-  else if (Data.find("release 5") != StringRef::npos)
-return RHEL5;
-}
-return 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 DebianLenny;
-  case 6:
-return DebianSqueeze;
-  case 7:
-return DebianWheezy;
-  case 8:
-return DebianJessie;
-  case 9:
-return DebianStretch;
-  default:
-return UnknownDistro;
-  }
-}
-return llvm::StringSwitch(Data.split("\n").first)
-.Case("squeeze/sid", DebianSqueeze)
-.Case("wheezy/sid", DebianWheezy)
-

[PATCH] D25949: [Driver] Refactor distro detection & classification as a separate API

2016-11-17 Thread Bruno Cardoso Lopes via cfe-commits
bruno accepted this revision.
bruno added a comment.
This revision is now accepted and ready to land.

Very nice! LGTM


https://reviews.llvm.org/D25949



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D25949: [Driver] Refactor distro detection & classification as a separate API

2016-11-11 Thread Michał Górny via cfe-commits
mgorny added reviewers: rnk, hfinkel.
mgorny added a comment.

Ping.


https://reviews.llvm.org/D25949



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D25949: [Driver] Refactor distro detection & classification as a separate API

2016-10-25 Thread Michał Górny via cfe-commits
mgorny created this revision.
mgorny added reviewers: bruno, bkramer.
mgorny added a subscriber: cfe-commits.
Herald added subscribers: modocache, beanz.

Refactor the Distro enum along with helper functions into a full-fledged
Distro class, inspired by llvm::Triple, and make it a public API.
The new class wraps the enum with necessary comparison operators, adding
the convenience Is*() methods and a constructor performing
the detection. The public API is needed to run the unit tests 
(https://reviews.llvm.org/D25869).


https://reviews.llvm.org/D25949

Files:
  include/clang/Driver/Distro.h
  lib/Driver/CMakeLists.txt
  lib/Driver/Distro.cpp
  lib/Driver/ToolChains.cpp

Index: lib/Driver/ToolChains.cpp
===
--- lib/Driver/ToolChains.cpp
+++ lib/Driver/ToolChains.cpp
@@ -14,6 +14,7 @@
 #include "clang/Basic/VirtualFileSystem.h"
 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
 #include "clang/Driver/Compilation.h"
+#include "clang/Driver/Distro.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
@@ -3834,169 +3835,6 @@
   }
 }
 
-/// Distribution (very bare-bones at the moment).
-
-enum Distro {
-  // NB: Releases of a particular Linux distro should be kept together
-  // in this enum, because some tests are done by integer comparison against
-  // the first and last known member in the family, e.g. IsRedHat().
-  ArchLinux,
-  DebianLenny,
-  DebianSqueeze,
-  DebianWheezy,
-  DebianJessie,
-  DebianStretch,
-  Exherbo,
-  RHEL5,
-  RHEL6,
-  RHEL7,
-  Fedora,
-  OpenSUSE,
-  UbuntuHardy,
-  UbuntuIntrepid,
-  UbuntuJaunty,
-  UbuntuKarmic,
-  UbuntuLucid,
-  UbuntuMaverick,
-  UbuntuNatty,
-  UbuntuOneiric,
-  UbuntuPrecise,
-  UbuntuQuantal,
-  UbuntuRaring,
-  UbuntuSaucy,
-  UbuntuTrusty,
-  UbuntuUtopic,
-  UbuntuVivid,
-  UbuntuWily,
-  UbuntuXenial,
-  UbuntuYakkety,
-  UnknownDistro
-};
-
-static bool IsRedhat(enum Distro Distro) {
-  return Distro == Fedora || (Distro >= RHEL5 && Distro <= RHEL7);
-}
-
-static bool IsOpenSUSE(enum Distro Distro) { return Distro == OpenSUSE; }
-
-static bool IsDebian(enum Distro Distro) {
-  return Distro >= DebianLenny && Distro <= DebianStretch;
-}
-
-static bool IsUbuntu(enum Distro Distro) {
-  return Distro >= UbuntuHardy && Distro <= UbuntuYakkety;
-}
-
-static Distro DetectDistro(vfs::FileSystem ) {
-  llvm::ErrorOr File =
-  VFS.getBufferForFile("/etc/lsb-release");
-  if (File) {
-StringRef Data = File.get()->getBuffer();
-SmallVector Lines;
-Data.split(Lines, "\n");
-Distro Version = UnknownDistro;
-for (StringRef Line : Lines)
-  if (Version == UnknownDistro && Line.startswith("DISTRIB_CODENAME="))
-Version = llvm::StringSwitch(Line.substr(17))
-  .Case("hardy", UbuntuHardy)
-  .Case("intrepid", UbuntuIntrepid)
-  .Case("jaunty", UbuntuJaunty)
-  .Case("karmic", UbuntuKarmic)
-  .Case("lucid", UbuntuLucid)
-  .Case("maverick", UbuntuMaverick)
-  .Case("natty", UbuntuNatty)
-  .Case("oneiric", UbuntuOneiric)
-  .Case("precise", UbuntuPrecise)
-  .Case("quantal", UbuntuQuantal)
-  .Case("raring", UbuntuRaring)
-  .Case("saucy", UbuntuSaucy)
-  .Case("trusty", UbuntuTrusty)
-  .Case("utopic", UbuntuUtopic)
-  .Case("vivid", UbuntuVivid)
-  .Case("wily", UbuntuWily)
-  .Case("xenial", UbuntuXenial)
-  .Case("yakkety", UbuntuYakkety)
-  .Default(UnknownDistro);
-if (Version != UnknownDistro)
-  return Version;
-  }
-
-  File = VFS.getBufferForFile("/etc/redhat-release");
-  if (File) {
-StringRef Data = File.get()->getBuffer();
-if (Data.startswith("Fedora release"))
-  return Fedora;
-if (Data.startswith("Red Hat Enterprise Linux") ||
-Data.startswith("CentOS") ||
-Data.startswith("Scientific Linux")) {
-  if (Data.find("release 7") != StringRef::npos)
-return RHEL7;
-  else if (Data.find("release 6") != StringRef::npos)
-return RHEL6;
-  else if (Data.find("release 5") != StringRef::npos)
-return RHEL5;
-}
-return 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 DebianLenny;
-  case 6:
-return DebianSqueeze;
-  case 7:
-return DebianWheezy;
-  case 8:
-return DebianJessie;
-