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     // NB: Releases of a particular Linux distro should be kept together
27     // in this enum, because some tests are done by integer comparison against
28     // the first and last known member in the family, e.g. IsRedHat().
29     AlpineLinux,
30     ArchLinux,
31     DebianLenny,
32     DebianSqueeze,
33     DebianWheezy,
34     DebianJessie,
35     DebianStretch,
36     DebianBuster,
37     DebianBullseye,
38     Exherbo,
39     RHEL5,
40     RHEL6,
41     RHEL7,
42     Fedora,
43     Gentoo,
44     OpenSUSE,
45     UbuntuHardy,
46     UbuntuIntrepid,
47     UbuntuJaunty,
48     UbuntuKarmic,
49     UbuntuLucid,
50     UbuntuMaverick,
51     UbuntuNatty,
52     UbuntuOneiric,
53     UbuntuPrecise,
54     UbuntuQuantal,
55     UbuntuRaring,
56     UbuntuSaucy,
57     UbuntuTrusty,
58     UbuntuUtopic,
59     UbuntuVivid,
60     UbuntuWily,
61     UbuntuXenial,
62     UbuntuYakkety,
63     UbuntuZesty,
64     UbuntuArtful,
65     UbuntuBionic,
66     UbuntuCosmic,
67     UbuntuDisco,
68     UbuntuEoan,
69     UbuntuFocal,
70     UnknownDistro
71   };
72 
73 private:
74   /// The distribution, possibly with specific version.
75   DistroType DistroVal;
76 
77 public:
78   /// @name Constructors
79   /// @{
80 
81   /// Default constructor leaves the distribution unknown.
82   Distro() : DistroVal() {}
83 
84   /// Constructs a Distro type for specific distribution.
85   Distro(DistroType D) : DistroVal(D) {}
86 
87   /// Detects the distribution using specified VFS.
88   explicit Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost);
89 
90   bool operator==(const Distro &Other) const {
91     return DistroVal == Other.DistroVal;
92   }
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   /// @}
107   /// @name Convenience Predicates
108   /// @{
109 
110   bool IsRedhat() const {
111     return DistroVal == Fedora || (DistroVal >= RHEL5 && DistroVal <= RHEL7);
112   }
113 
114   bool IsOpenSUSE() const {
115     return DistroVal == OpenSUSE;
116   }
117 
118   bool IsDebian() const {
119     return DistroVal >= DebianLenny && DistroVal <= DebianBullseye;
120   }
121 
122   bool IsUbuntu() const {
123     return DistroVal >= UbuntuHardy && DistroVal <= UbuntuFocal;
124   }
125 
126   bool IsAlpineLinux() const {
127     return DistroVal == AlpineLinux;
128   }
129 
130   bool IsGentoo() const {
131     return DistroVal == Gentoo;
132   }
133 
134   /// @}
135 };
136 
137 } // end namespace driver
138 } // end namespace clang
139 
140 #endif
141