]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/common.pm
Fix not having a CPU count when the lookup command is missing.
[user/henk/code/inspircd.git] / make / common.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2013-2014 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::Spec::Functions qw(rel2abs);
32
33 our @EXPORT = qw(get_cpu_count
34                  get_version
35                  module_installed);
36
37 sub get_version {
38         state %version;
39         return %version if %version;
40
41         # Attempt to retrieve version information from src/version.sh
42         chomp(my $vf = `sh src/version.sh 2>/dev/null`);
43         if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:\+(\w+))?$/) {
44                 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
45         }
46
47         # Attempt to retrieve missing version information from Git
48         chomp(my $gr = `git describe --tags 2>/dev/null`);
49         if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-\d+-g(\w+))?$/) {
50                 $version{MAJOR} //= $1;
51                 $version{MINOR} //= $2;
52                 $version{PATCH} //= $3;
53                 $version{LABEL} = $4 if defined $4;
54         }
55
56         # The user is using a stable release which does not have
57         # a label attached.
58         $version{LABEL} //= 'release';
59
60         # If any of these fields are missing then the user has deleted the
61         # version file and is not running from Git. Fill in the fields with
62         # dummy data so we don't get into trouble with undef values later.
63         $version{MAJOR} //= '0';
64         $version{MINOR} //= '0';
65         $version{PATCH} //= '0';
66
67         return %version;
68 }
69
70 sub module_installed($) {
71         my $module = shift;
72         eval("use $module;");
73         return !$@;
74 }
75
76 sub get_cpu_count {
77         my $count = 1;
78         if ($^O =~ /bsd/) {
79                 $count = `sysctl -n hw.ncpu 2>/dev/null` || 1;
80         } elsif ($^O eq 'darwin') {
81                 $count = `sysctl -n hw.activecpu 2>/dev/null` || 1;
82         } elsif ($^O eq 'linux') {
83                 $count = `getconf _NPROCESSORS_ONLN 2>/dev/null` || 1;
84         } elsif ($^O eq 'solaris') {
85                 $count = `psrinfo -p 2>/dev/null` || 1;
86         }
87         chomp($count);
88         return $count;
89 }
90
91 1;