daemon: improve warning on missing SO_ACCEPTFILTER
[public-inbox.git] / install / os.perl
blob00edbadfd29588a0fe0eee2ea2037dddca1cfddd
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Helper library for detecting distro info and mapping to package manager.
5 # This should NOT be installed via `make install'.
6 # This is used by install/deps.perl and ci/profiles.perl
7 package PublicInbox::InstallOS;
8 use v5.12;
9 use parent qw(Exporter);
10 our ($ID, $PRETTY_NAME, $VERSION_ID); # same vars as os-release(5)
11 our @EXPORT = qw($ID $VERSION_ID pkg_fmt);
13 my ($release, $version); # from uname
14 if ($^O eq 'linux') { # try using os-release(5)
15 for my $f (qw(/etc/os-release /usr/lib/os-release)) {
16 next unless -f $f;
17 my @echo = map {
18 qq{echo "\$"$_" = qq[\$$_];"; }
19 } qw(ID PRETTY_NAME VERSION_ID);
20 # rely on sh(1) to handle interpolation and such:
21 my $vars = `sh -c '. $f; @echo'`;
22 die "sh \$?=$?" if $?;
23 eval $vars;
24 die $@ if $@;
25 $VERSION_ID //= '';
26 $ID //= '';
27 if ($ID eq 'debian' && $VERSION_ID eq '') {
28 if ($PRETTY_NAME =~ m!/sid\z!) {
29 $VERSION_ID = 'sid';
30 } else {
31 open my $fh, '<', $f or die "open($f): $!";
32 my $msg = do { local $/; <$fh> };
33 die <<EOM;
34 ID=$ID, but no VERSION_ID
35 ==> $f <==
36 $msg
37 EOM
40 last if $ID ne '' && $VERSION_ID ne '';
42 $ID = 'linux' if $ID eq ''; # cf. os-release(5)
43 } elsif ($^O =~ m!\A(?:free|net|open)bsd\z! || $^O eq 'dragonfly') {
44 $ID = $^O;
45 require POSIX;
46 (undef, undef, $release, $version) = POSIX::uname();
47 $VERSION_ID = lc $release;
48 $VERSION_ID =~ s/[^0-9a-z\.\_\-]//sg; # cf. os-release(5)
49 } else { # only support POSIX-like and Free systems:
50 die "$^O unsupported";
52 $VERSION_ID //= 0; # numeric? could be 'sid', actually...
53 my %MIN_VER = ( # likely older versions work for many of these...
54 alpine => v3.19,
55 dragonfly => v6.4,
56 freebsd => v11,
57 netbsd => v9.3,
58 openbsd => v7.3,
61 if (defined(my $min_ver = $MIN_VER{$^O})) {
62 my $vid = $VERSION_ID;
63 $vid =~ s/-.*\z//s; # no dashes in v-strings
64 my $vstr = eval "v$vid";
65 die "can't convert VERSION_ID=$VERSION_ID to v-string" if $@;
66 die <<EOM if $vstr lt $min_ver;
67 ID=$ID VERSION_ID=$VERSION_ID release=$release ($version) too old to support
68 EOM
71 sub pkg_fmt () {
72 if ($ID eq 'alpine') { 'apk' }
73 elsif ($ID =~ /\A(?:freebsd|dragonfly)\z/) { 'pkg' }
74 # *shrug*, as long as the (Net|Open)BSD names don't conflict w/ FreeBSD
75 elsif ($ID eq 'netbsd') { 'pkgin' }
76 elsif ($ID eq 'openbsd') { 'pkg_add' }
77 elsif ($ID =~ m!\A(?:debian|ubuntu)\z!) { 'deb' }
78 elsif ($ID =~ m!\A(?:centos|redhat|fedora)\z!) { 'rpm' }
79 else { warn "PKG_FMT undefined for ID=$ID"; undef }
82 package main;
83 PublicInbox::InstallOS->import;