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
12360784Sdim#include "llvm/ADT/Triple.h"
13344779Sdim#include "llvm/Support/VirtualFileSystem.h"
14311118Sdim
15311118Sdimnamespace clang {
16311118Sdimnamespace driver {
17311118Sdim
18311118Sdim/// Distro - Helper class for detecting and classifying Linux distributions.
19311118Sdim///
20311118Sdim/// This class encapsulates the clang Linux distribution detection mechanism
21311118Sdim/// as well as helper functions that match the specific (versioned) results
22311118Sdim/// into wider distribution classes.
23311118Sdimclass Distro {
24311118Sdimpublic:
25311118Sdim  enum DistroType {
26311118Sdim    // NB: Releases of a particular Linux distro should be kept together
27311118Sdim    // in this enum, because some tests are done by integer comparison against
28311118Sdim    // the first and last known member in the family, e.g. IsRedHat().
29327952Sdim    AlpineLinux,
30311118Sdim    ArchLinux,
31311118Sdim    DebianLenny,
32311118Sdim    DebianSqueeze,
33311118Sdim    DebianWheezy,
34311118Sdim    DebianJessie,
35311118Sdim    DebianStretch,
36327952Sdim    DebianBuster,
37353358Sdim    DebianBullseye,
38311118Sdim    Exherbo,
39311118Sdim    RHEL5,
40311118Sdim    RHEL6,
41311118Sdim    RHEL7,
42311118Sdim    Fedora,
43344779Sdim    Gentoo,
44311118Sdim    OpenSUSE,
45311118Sdim    UbuntuHardy,
46311118Sdim    UbuntuIntrepid,
47311118Sdim    UbuntuJaunty,
48311118Sdim    UbuntuKarmic,
49311118Sdim    UbuntuLucid,
50311118Sdim    UbuntuMaverick,
51311118Sdim    UbuntuNatty,
52311118Sdim    UbuntuOneiric,
53311118Sdim    UbuntuPrecise,
54311118Sdim    UbuntuQuantal,
55311118Sdim    UbuntuRaring,
56311118Sdim    UbuntuSaucy,
57311118Sdim    UbuntuTrusty,
58311118Sdim    UbuntuUtopic,
59311118Sdim    UbuntuVivid,
60311118Sdim    UbuntuWily,
61311118Sdim    UbuntuXenial,
62311118Sdim    UbuntuYakkety,
63311118Sdim    UbuntuZesty,
64321369Sdim    UbuntuArtful,
65327952Sdim    UbuntuBionic,
66341825Sdim    UbuntuCosmic,
67344779Sdim    UbuntuDisco,
68353358Sdim    UbuntuEoan,
69360784Sdim    UbuntuFocal,
70311118Sdim    UnknownDistro
71311118Sdim  };
72311118Sdim
73311118Sdimprivate:
74311118Sdim  /// The distribution, possibly with specific version.
75311118Sdim  DistroType DistroVal;
76311118Sdim
77311118Sdimpublic:
78311118Sdim  /// @name Constructors
79311118Sdim  /// @{
80311118Sdim
81311118Sdim  /// Default constructor leaves the distribution unknown.
82311118Sdim  Distro() : DistroVal() {}
83311118Sdim
84311118Sdim  /// Constructs a Distro type for specific distribution.
85311118Sdim  Distro(DistroType D) : DistroVal(D) {}
86311118Sdim
87311118Sdim  /// Detects the distribution using specified VFS.
88360784Sdim  explicit Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost);
89311118Sdim
90311118Sdim  bool operator==(const Distro &Other) const {
91311118Sdim    return DistroVal == Other.DistroVal;
92311118Sdim  }
93311118Sdim
94311118Sdim  bool operator!=(const Distro &Other) const {
95311118Sdim    return DistroVal != Other.DistroVal;
96311118Sdim  }
97311118Sdim
98311118Sdim  bool operator>=(const Distro &Other) const {
99311118Sdim    return DistroVal >= Other.DistroVal;
100311118Sdim  }
101311118Sdim
102311118Sdim  bool operator<=(const Distro &Other) const {
103311118Sdim    return DistroVal <= Other.DistroVal;
104311118Sdim  }
105311118Sdim
106311118Sdim  /// @}
107311118Sdim  /// @name Convenience Predicates
108311118Sdim  /// @{
109311118Sdim
110311118Sdim  bool IsRedhat() const {
111311118Sdim    return DistroVal == Fedora || (DistroVal >= RHEL5 && DistroVal <= RHEL7);
112311118Sdim  }
113311118Sdim
114311118Sdim  bool IsOpenSUSE() const {
115311118Sdim    return DistroVal == OpenSUSE;
116311118Sdim  }
117311118Sdim
118311118Sdim  bool IsDebian() const {
119353358Sdim    return DistroVal >= DebianLenny && DistroVal <= DebianBullseye;
120311118Sdim  }
121311118Sdim
122311118Sdim  bool IsUbuntu() const {
123360784Sdim    return DistroVal >= UbuntuHardy && DistroVal <= UbuntuFocal;
124311118Sdim  }
125321369Sdim
126327952Sdim  bool IsAlpineLinux() const {
127327952Sdim    return DistroVal == AlpineLinux;
128327952Sdim  }
129327952Sdim
130344779Sdim  bool IsGentoo() const {
131344779Sdim    return DistroVal == Gentoo;
132344779Sdim  }
133344779Sdim
134311118Sdim  /// @}
135311118Sdim};
136311118Sdim
137311118Sdim} // end namespace driver
138311118Sdim} // end namespace clang
139311118Sdim
140311118Sdim#endif
141