]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/common.pm
Fix --distribution-label erroneously requiring --development.
[user/henk/code/inspircd.git] / make / common.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2013-2017 Peter Powell <petpow@saberuk.com>
5 #
6 # This file is part of InspIRCd.  InspIRCd is free software: you can
7 # redistribute it and/or modify it under the terms of the GNU General Public
8 # License as published by the Free Software Foundation, version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19
20 BEGIN {
21         require 5.10.0;
22 }
23
24 package make::common;
25
26 use feature ':5.10';
27 use strict;
28 use warnings FATAL => qw(all);
29
30 use Exporter              qw(import);
31 use File::Path            qw(mkpath);
32 use File::Spec::Functions qw(rel2abs);
33
34 use make::console;
35
36 our @EXPORT = qw(create_directory
37                  get_cpu_count
38                  get_version
39                  read_config_file
40                  write_config_file);
41
42 sub create_directory($$) {
43         my ($location, $permissions) = @_;
44         return eval {
45                 mkpath($location, 0, $permissions);
46                 return 1;
47         } // 0;
48 }
49
50 sub get_version {
51         state %version;
52         return %version if %version;
53
54         # Attempt to retrieve version information from src/version.sh
55         chomp(my $vf = `sh src/version.sh 2>/dev/null`);
56         if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:-(\w+))?$/) {
57                 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
58         }
59
60         # Attempt to retrieve missing version information from Git
61         chomp(my $gr = `git describe --tags 2>/dev/null`);
62         if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:[a-z]+\d+)?(?:-\d+-g(\w+))?$/) {
63                 $version{MAJOR} //= $1;
64                 $version{MINOR} //= $2;
65                 $version{PATCH} //= $3;
66                 $version{LABEL} = $4 if defined $4;
67         }
68
69         # If the user has specified a distribution label then we use it in
70         # place of the label from src/version.sh or Git.
71         $version{REAL_LABEL} = $version{LABEL};
72         $version{LABEL} = shift // $version{LABEL};
73
74         # If any of these fields are missing then the user has deleted the
75         # version file and is not running from Git. Fill in the fields with
76         # dummy data so we don't get into trouble with undef values later.
77         $version{MAJOR} //= '0';
78         $version{MINOR} //= '0';
79         $version{PATCH} //= '0';
80
81         # If there is no label then the user is using a stable release which
82         # does not have a label attached.
83         if (defined $version{LABEL}) {
84                 $version{FULL} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}-$version{LABEL}"
85         } else {
86                 $version{LABEL} = 'release';
87                 $version{FULL} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}"
88         }
89
90         return %version;
91 }
92
93 sub get_cpu_count {
94         my $count = 1;
95         if ($^O =~ /bsd/) {
96                 $count = `sysctl -n hw.ncpu 2>/dev/null` || 1;
97         } elsif ($^O eq 'darwin') {
98                 $count = `sysctl -n hw.activecpu 2>/dev/null` || 1;
99         } elsif ($^O eq 'linux') {
100                 $count = `getconf _NPROCESSORS_ONLN 2>/dev/null` || 1;
101         } elsif ($^O eq 'solaris') {
102                 $count = `psrinfo -p 2>/dev/null` || 1;
103         }
104         chomp($count);
105         return $count;
106 }
107
108 sub read_config_file($) {
109         my $path = shift;
110         my %config;
111         open(my $fh, $path) or return %config;
112         while (my $line = <$fh>) {
113                 next if $line =~ /^\s*($|\#)/;
114                 my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/);
115                 $config{$key} = $value;
116         }
117         close $fh;
118         return %config;
119 }
120
121 sub write_config_file($%) {
122         my $path = shift;
123         my %config = @_;
124         open(my $fh, '>', $path) or print_error "unable to write to $path: $!";
125         while (my ($key, $value) = each %config) {
126                 $value //= '';
127                 say $fh "$key $value";
128         }
129         close $fh;
130 }
131
132 1;