]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/common.pm
Remove an obsolete comment about BSD Make.
[user/henk/code/inspircd.git] / make / common.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2014-2017, 2019-2020 Sadie Powell <sadie@witchery.services>
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                  execute
38                  get_cpu_count
39                  get_version
40                  read_config_file
41                  write_config_file);
42
43 sub create_directory($$) {
44         my ($location, $permissions) = @_;
45         return eval {
46                 mkpath($location, 0, $permissions);
47                 return 1;
48         } // 0;
49 }
50
51 sub execute(@) {
52         print_format "<|BOLD \$|> @_\n";
53         return system @_;
54 }
55
56 sub get_version {
57         state %version;
58         return %version if %version;
59
60         # Attempt to retrieve version information from src/version.sh
61         chomp(my $vf = `sh src/version.sh 2>/dev/null`);
62         if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:-(\w+))?$/) {
63                 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
64         }
65
66         # Attempt to retrieve missing version information from Git
67         chomp(my $gr = `git describe --tags 2>/dev/null`);
68         if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:[a-z]+\d+)?(?:-\d+-g(\w+))?$/) {
69                 $version{MAJOR} //= $1;
70                 $version{MINOR} //= $2;
71                 $version{PATCH} //= $3;
72                 $version{LABEL} = $4 if defined $4;
73         }
74
75         # If the user has specified a distribution label then we use it in
76         # place of the label from src/version.sh or Git.
77         $version{REAL_LABEL} = $version{LABEL};
78         $version{LABEL} = shift // $version{LABEL};
79
80         # If any of these fields are missing then the user has deleted the
81         # version file and is not running from Git. Fill in the fields with
82         # dummy data so we don't get into trouble with undef values later.
83         $version{MAJOR} //= '0';
84         $version{MINOR} //= '0';
85         $version{PATCH} //= '0';
86
87         # If there is no label then the user is using a stable release which
88         # does not have a label attached.
89         if (defined $version{LABEL}) {
90                 $version{FULL} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}-$version{LABEL}"
91         } else {
92                 $version{LABEL} = 'release';
93                 $version{FULL} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}"
94         }
95
96         return %version;
97 }
98
99 sub get_cpu_count {
100         my $count = 1;
101         if ($^O =~ /bsd/) {
102                 $count = `sysctl -n hw.ncpu 2>/dev/null` || 1;
103         } elsif ($^O eq 'darwin') {
104                 $count = `sysctl -n hw.activecpu 2>/dev/null` || 1;
105         } elsif ($^O eq 'linux') {
106                 $count = `getconf _NPROCESSORS_ONLN 2>/dev/null` || 1;
107         } elsif ($^O eq 'solaris') {
108                 $count = `psrinfo -p 2>/dev/null` || 1;
109         }
110         chomp($count);
111         return $count;
112 }
113
114 sub read_config_file($) {
115         my $path = shift;
116         my %config;
117         open(my $fh, $path) or return %config;
118         while (my $line = <$fh>) {
119                 next if $line =~ /^\s*($|\#)/;
120                 my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/);
121                 $config{$key} = $value;
122         }
123         close $fh;
124         return %config;
125 }
126
127 sub write_config_file($%) {
128         my $path = shift;
129         my %config = @_;
130         open(my $fh, '>', $path) or print_error "unable to write to $path: $!";
131         while (my ($key, $value) = each %config) {
132                 $value //= '';
133                 say $fh "$key $value";
134         }
135         close $fh;
136 }
137
138 1;