Distro.h revision 353358
1311118Sdim//===--- Distro.h - Linux distribution detection support --------*- C++ -*-===//
2311118Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6311118Sdim//
7311118Sdim//===----------------------------------------------------------------------===//
8311118Sdim
9311118Sdim#ifndef LLVM_CLANG_DRIVER_DISTRO_H
10311118Sdim#define LLVM_CLANG_DRIVER_DISTRO_H
11311118Sdim
12344779Sdim#include "llvm/Support/VirtualFileSystem.h"
13311118Sdim
14311118Sdimnamespace clang {
15311118Sdimnamespace driver {
16311118Sdim
17311118Sdim/// Distro - Helper class for detecting and classifying Linux distributions.
18311118Sdim///
19311118Sdim/// This class encapsulates the clang Linux distribution detection mechanism
20311118Sdim/// as well as helper functions that match the specific (versioned) results
21311118Sdim/// into wider distribution classes.
22311118Sdimclass Distro {
23311118Sdimpublic:
24311118Sdim  enum DistroType {
25311118Sdim    // NB: Releases of a particular Linux distro should be kept together
26311118Sdim    // in this enum, because some tests are done by integer comparison against
27311118Sdim    // the first and last known member in the family, e.g. IsRedHat().
28327952Sdim    AlpineLinux,
29311118Sdim    ArchLinux,
30311118Sdim    DebianLenny,
31311118Sdim    DebianSqueeze,
32311118Sdim    DebianWheezy,
33311118Sdim    DebianJessie,
34311118Sdim    DebianStretch,
35327952Sdim    DebianBuster,
36353358Sdim    DebianBullseye,
37311118Sdim    Exherbo,
38311118Sdim    RHEL5,
39311118Sdim    RHEL6,
40311118Sdim    RHEL7,
41311118Sdim    Fedora,
42344779Sdim    Gentoo,
43311118Sdim    OpenSUSE,
44311118Sdim    UbuntuHardy,
45311118Sdim    UbuntuIntrepid,
46311118Sdim    UbuntuJaunty,
47311118Sdim    UbuntuKarmic,
48311118Sdim    UbuntuLucid,
49311118Sdim    UbuntuMaverick,
50311118Sdim    UbuntuNatty,
51311118Sdim    UbuntuOneiric,
52311118Sdim    UbuntuPrecise,
53311118Sdim    UbuntuQuantal,
54311118Sdim    UbuntuRaring,
55311118Sdim    UbuntuSaucy,
56311118Sdim    UbuntuTrusty,
57311118Sdim    UbuntuUtopic,
58311118Sdim    UbuntuVivid,
59311118Sdim    UbuntuWily,
60311118Sdim    UbuntuXenial,
61311118Sdim    UbuntuYakkety,
62311118Sdim    UbuntuZesty,
63321369Sdim    UbuntuArtful,
64327952Sdim    UbuntuBionic,
65341825Sdim    UbuntuCosmic,
66344779Sdim    UbuntuDisco,
67353358Sdim    UbuntuEoan,
68311118Sdim    UnknownDistro
69311118Sdim  };
70311118Sdim
71311118Sdimprivate:
72311118Sdim  /// The distribution, possibly with specific version.
73311118Sdim  DistroType DistroVal;
74311118Sdim
75311118Sdimpublic:
76311118Sdim  /// @name Constructors
77311118Sdim  /// @{
78311118Sdim
79311118Sdim  /// Default constructor leaves the distribution unknown.
80311118Sdim  Distro() : DistroVal() {}
81311118Sdim
82311118Sdim  /// Constructs a Distro type for specific distribution.
83311118Sdim  Distro(DistroType D) : DistroVal(D) {}
84311118Sdim
85311118Sdim  /// Detects the distribution using specified VFS.
86344779Sdim  explicit Distro(llvm::vfs::FileSystem &VFS);
87311118Sdim
88311118Sdim  bool operator==(const Distro &Other) const {
89311118Sdim    return DistroVal == Other.DistroVal;
90311118Sdim  }
91311118Sdim
92311118Sdim  bool operator!=(const Distro &Other) const {
93311118Sdim    return DistroVal != Other.DistroVal;
94311118Sdim  }
95311118Sdim
96311118Sdim  bool operator>=(const Distro &Other) const {
97311118Sdim    return DistroVal >= Other.DistroVal;
98311118Sdim  }
99311118Sdim
100311118Sdim  bool operator<=(const Distro &Other) const {
101311118Sdim    return DistroVal <= Other.DistroVal;
102311118Sdim  }
103311118Sdim
104311118Sdim  /// @}
105311118Sdim  /// @name Convenience Predicates
106311118Sdim  /// @{
107311118Sdim
108311118Sdim  bool IsRedhat() const {
109311118Sdim    return DistroVal == Fedora || (DistroVal >= RHEL5 && DistroVal <= RHEL7);
110311118Sdim  }
111311118Sdim
112311118Sdim  bool IsOpenSUSE() const {
113311118Sdim    return DistroVal == OpenSUSE;
114311118Sdim  }
115311118Sdim
116311118Sdim  bool IsDebian() const {
117353358Sdim    return DistroVal >= DebianLenny && DistroVal <= DebianBullseye;
118311118Sdim  }
119311118Sdim
120311118Sdim  bool IsUbuntu() const {
121353358Sdim    return DistroVal >= UbuntuHardy && DistroVal <= UbuntuEoan;
122311118Sdim  }
123321369Sdim
124327952Sdim  bool IsAlpineLinux() const {
125327952Sdim    return DistroVal == AlpineLinux;
126327952Sdim  }
127327952Sdim
128344779Sdim  bool IsGentoo() const {
129344779Sdim    return DistroVal == Gentoo;
130344779Sdim  }
131344779Sdim
132311118Sdim  /// @}
133311118Sdim};
134311118Sdim
135311118Sdim} // end namespace driver
136311118Sdim} // end namespace clang
137311118Sdim
138311118Sdim#endif
139