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