1 //===--- Distro.h - Linux distribution detection support --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_DRIVER_DISTRO_H
10 #define LLVM_CLANG_DRIVER_DISTRO_H
11 
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/Support/VirtualFileSystem.h"
14 
15 namespace clang {
16 namespace driver {
17 
18 /// Distro - Helper class for detecting and classifying Linux distributions.
19 ///
20 /// This class encapsulates the clang Linux distribution detection mechanism
21 /// as well as helper functions that match the specific (versioned) results
22 /// into wider distribution classes.
23 class Distro {
24 public:
25   enum DistroType {
26     // Special value means that no detection was performed yet.
27     UninitializedDistro,
28     // NB: Releases of a particular Linux distro should be kept together
29     // in this enum, because some tests are done by integer comparison against
30     // the first and last known member in the family, e.g. IsRedHat().
31     AlpineLinux,
32     ArchLinux,
33     DebianLenny,
34     DebianSqueeze,
35     DebianWheezy,
36     DebianJessie,
37     DebianStretch,
38     DebianBuster,
39     DebianBullseye,
40     Exherbo,
41     RHEL5,
42     RHEL6,
43     RHEL7,
44     Fedora,
45     Gentoo,
46     OpenSUSE,
47     UbuntuHardy,
48     UbuntuIntrepid,
49     UbuntuJaunty,
50     UbuntuKarmic,
51     UbuntuLucid,
52     UbuntuMaverick,
53     UbuntuNatty,
54     UbuntuOneiric,
55     UbuntuPrecise,
56     UbuntuQuantal,
57     UbuntuRaring,
58     UbuntuSaucy,
59     UbuntuTrusty,
60     UbuntuUtopic,
61     UbuntuVivid,
62     UbuntuWily,
63     UbuntuXenial,
64     UbuntuYakkety,
65     UbuntuZesty,
66     UbuntuArtful,
67     UbuntuBionic,
68     UbuntuCosmic,
69     UbuntuDisco,
70     UbuntuEoan,
71     UbuntuFocal,
72     UbuntuGroovy,
73     UbuntuHirsute,
74     UnknownDistro
75   };
76 
77 private:
78   /// The distribution, possibly with specific version.
79   DistroType DistroVal;
80 
81 public:
82   /// @name Constructors
83   /// @{
84 
85   /// Default constructor leaves the distribution unknown.
Distro()86   Distro() : DistroVal() {}
87 
88   /// Constructs a Distro type for specific distribution.
Distro(DistroType D)89   Distro(DistroType D) : DistroVal(D) {}
90 
91   /// Detects the distribution using specified VFS.
92   explicit Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost);
93 
94   bool operator==(const Distro &Other) const {
95     return DistroVal == Other.DistroVal;
96   }
97 
98   bool operator!=(const Distro &Other) const {
99     return DistroVal != Other.DistroVal;
100   }
101 
102   bool operator>=(const Distro &Other) const {
103     return DistroVal >= Other.DistroVal;
104   }
105 
106   bool operator<=(const Distro &Other) const {
107     return DistroVal <= Other.DistroVal;
108   }
109 
110   /// @}
111   /// @name Convenience Predicates
112   /// @{
113 
IsRedhat()114   bool IsRedhat() const {
115     return DistroVal == Fedora || (DistroVal >= RHEL5 && DistroVal <= RHEL7);
116   }
117 
IsOpenSUSE()118   bool IsOpenSUSE() const { return DistroVal == OpenSUSE; }
119 
IsDebian()120   bool IsDebian() const {
121     return DistroVal >= DebianLenny && DistroVal <= DebianBullseye;
122   }
123 
IsUbuntu()124   bool IsUbuntu() const {
125     return DistroVal >= UbuntuHardy && DistroVal <= UbuntuHirsute;
126   }
127 
IsAlpineLinux()128   bool IsAlpineLinux() const { return DistroVal == AlpineLinux; }
129 
IsGentoo()130   bool IsGentoo() const { return DistroVal == Gentoo; }
131 
132   /// @}
133 };
134 
135 } // end namespace driver
136 } // end namespace clang
137 
138 #endif
139