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