]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/directive.pm
Make people aware that they can get support on GitHub now.
[user/henk/code/inspircd.git] / make / directive.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2016-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 package make::directive;
21
22 BEGIN {
23         require 5.10.0;
24 }
25
26 use feature ':5.10';
27 use strict;
28 use warnings FATAL => qw(all);
29
30 use File::Basename        qw(dirname);
31 use File::Spec::Functions qw(catdir);
32 use Exporter              qw(import);
33
34 use make::configure;
35 use make::console;
36
37 use constant DIRECTIVE_ERROR_PIPE => $ENV{INSPIRCD_VERBOSE} ? '' : '2>/dev/null';
38 use constant VENDOR_DIRECTORY     => catdir(dirname(dirname(__FILE__)), 'vendor');
39
40 our @EXPORT = qw(get_directive
41                  execute_functions);
42
43 sub get_directive($$;$)
44 {
45         my ($file, $property, $default) = @_;
46         open(my $fh, $file) or return $default;
47
48         my $value = '';
49         while (<$fh>) {
50                 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/ || $_ =~ /^\/\/\/ \$(\S+): (.+)/) {
51                         next unless $1 eq $property;
52                         $value .= ' ' . execute_functions($file, $1, $2);
53                 }
54         }
55         close $fh;
56
57         # Strip all extraneous whitespace.
58         $value =~ s/^\s+|\s+$//g;
59         return $value || $default;
60 }
61
62 sub execute_functions($$$) {
63         my ($file, $name, $line) = @_;
64
65         # NOTE: we have to use 'our' instead of 'my' here because of a Perl bug.
66         for (our @parameters = (); $line =~ /([a-z_]+)\((?:\s*"([^"]*)(?{push @parameters, $2})"\s*)*\)/; undef @parameters) {
67                 my $sub = make::directive->can("__function_$1");
68                 print_error "unknown $name directive '$1' in $file!" unless $sub;
69
70                 # Call the subroutine and replace the function.
71                 my $result = $sub->($file, @parameters);
72                 if (defined $result) {
73                         $line = $` . $result . $';
74                         next;
75                 }
76
77                 # If the subroutine returns undef then it is a sign that we should
78                 # disregard the rest of the line and stop processing it.
79                 $line = $`;
80         }
81
82         return $line;
83 }
84
85 sub __environment {
86         my ($prefix, $suffix) = @_;
87         $suffix =~ s/[-.]/_/g;
88         $suffix =~ s/[^A-Za-z0-9_]//g;
89         return $prefix . uc $suffix;
90 }
91
92 sub __error {
93         my ($file, @message) = @_;
94         push @message, '';
95
96         # If we have package details then suggest to the user that they check
97         # that they have the packages installed.=
98         my $dependencies = get_directive($file, 'PackageInfo');
99         if (defined $dependencies) {
100                 my @packages = sort grep { /^\S+$/ } split /\s/, $dependencies;
101                 push @message, 'You should make sure you have the following packages installed:';
102                 for (@packages) {
103                         push @message, " * $_";
104                 }
105         } else {
106                 push @message, 'You should make sure that you have all of the required dependencies';
107                 push @message, 'for this module installed.';
108         }
109         push @message, '';
110
111         # If we have author information then tell the user to report the bug
112         # to them. Otherwise, assume it is a bundled module and tell the user
113         # to report it to the InspIRCd issue tracker.
114         my $author = get_directive($file, 'ModAuthor');
115         if (defined $author) {
116                 push @message, 'If you believe this error to be a bug then you can try to contact the';
117                 push @message, 'author of this module:';
118                 my $author_mail = get_directive($file, 'ModAuthorMail');
119                 if (defined $author_mail) {
120                         push @message, " * $author <$author_mail>";
121                 } else {
122                         push @message, " * $author";
123                 }
124         } else {
125                 push @message, 'If you believe this error to be a bug then you can file a bug report';
126                 push @message, 'at https://github.com/inspircd/inspircd/issues';
127                 push @message, '';
128                 push @message, 'You can also refer to the documentation page for this module at';
129                 push @message, "https://docs.inspircd.org/3/modules/${\module_shrink $file}";
130         }
131         push @message, '';
132
133         push @message, 'If you would like help with fixing this problem then visit our IRC';
134         push @message, 'channel at irc.inspircd.org #InspIRCd or create a support discussion';
135         push @message, 'at https://github.com/inspircd/inspircd/discussions.';
136         push @message, '';
137
138         print_error @message;
139 }
140
141 sub __function_error {
142         my ($file, @messages) = @_;
143         __error $file, @messages;
144 }
145
146 sub __function_execute {
147         my ($file, $command, $environment, $defaults) = @_;
148
149         # Try to execute the command...
150         chomp(my $result = `$command ${\DIRECTIVE_ERROR_PIPE}`);
151         unless ($?) {
152                 print_format "Execution of `<|GREEN $command|>` succeeded: <|BOLD $result|>\n";
153                 return $result;
154         }
155
156         # If looking up with pkg-config fails then check the environment...
157         if (defined $environment && $environment ne '') {
158                 $environment = __environment 'INSPIRCD_', $environment;
159                 if (defined $ENV{$environment}) {
160                         print_format "Execution of `<|GREEN $command|>` failed; using the environment: <|BOLD $ENV{$environment}|>\n";
161                         return $ENV{$environment};
162                 }
163         }
164
165         # If all else fails then look for the defaults..
166         if (defined $defaults) {
167                 print_format "Execution of `<|GREEN $command|>` failed; using the defaults: <|BOLD $defaults|>\n";
168                 return $defaults;
169         }
170
171         # Executing the command failed and we don't have any defaults so give up.
172         __error $file, "`<|GREEN $command|>` exited with a non-zero exit code!";
173 }
174
175 sub __function_find_compiler_flags {
176         my ($file, $name, $defaults) = @_;
177
178         # Try to look up the compiler flags with pkg-config...
179         chomp(my $flags = `pkg-config --cflags $name ${\DIRECTIVE_ERROR_PIPE}`);
180         unless ($?) {
181                 print_format "Found the <|GREEN $name|> compiler flags for <|GREEN ${\module_shrink $file}|> using pkg-config: <|BOLD $flags|>\n";
182                 return $flags;
183         }
184
185         # If looking up with pkg-config fails then check the environment...
186         my $key = __environment 'INSPIRCD_CXXFLAGS_', $name;
187         if (defined $ENV{$key}) {
188                 print_format "Found the <|GREEN $name|> compiler flags for <|GREEN ${\module_shrink $file}|> using the environment: <|BOLD $ENV{$key}|>\n";
189                 return $ENV{$key};
190         }
191
192         # If all else fails then look for the defaults..
193         if (defined $defaults) {
194                 print_format "Using the default <|GREEN $name|> compiler flags for <|GREEN ${\module_shrink $file}|>: <|BOLD $defaults|>\n";
195                 return $defaults;
196         }
197
198         # We can't find it via pkg-config, via the environment, or via the defaults so give up.
199         __error $file, "unable to find the <|GREEN $name|> compiler flags for <|GREEN ${\module_shrink $file}|>!";
200 }
201
202 sub __function_find_linker_flags {
203         my ($file, $name, $defaults) = @_;
204
205         # Try to look up the linker flags with pkg-config...
206         chomp(my $flags = `pkg-config --libs $name ${\DIRECTIVE_ERROR_PIPE}`);
207         unless ($?) {
208                 print_format "Found the <|GREEN $name|> linker flags for <|GREEN ${\module_shrink $file}|> using pkg-config: <|BOLD $flags|>\n";
209                 return $flags;
210         }
211
212         # If looking up with pkg-config fails then check the environment...
213         my $key = __environment 'INSPIRCD_CXXFLAGS_', $name;
214         if (defined $ENV{$key}) {
215                 print_format "Found the <|GREEN $name|> linker flags for <|GREEN ${\module_shrink $file}|> using the environment: <|BOLD $ENV{$key}|>\n";
216                 return $ENV{$key};
217         }
218
219         # If all else fails then look for the defaults..
220         if (defined $defaults) {
221                 print_format "Using the default <|GREEN $name|> linker flags for <|GREEN ${\module_shrink $file}|>: <|BOLD $defaults|>\n";
222                 return $defaults;
223         }
224
225         # We can't find it via pkg-config, via the environment, or via the defaults so give up.
226         __error $file, "unable to find the <|GREEN $name|> linker flags for <|GREEN ${\module_shrink $file}|>!";
227 }
228
229 sub __function_require_compiler {
230         my ($file, $name, $minimum, $maximum) =  @_;
231
232         # Look up information about the compiler.
233         return undef unless $ENV{CXX};
234         my %compiler = get_compiler_info($ENV{CXX});
235
236         # Check whether the current compiler is suitable.
237         return undef unless $compiler{NAME} eq $name;
238         return undef if defined $minimum && $compiler{VERSION} < $minimum;
239         return undef if defined $maximum && $compiler{VERSION} > $maximum;
240
241         # Requirement directives don't change anything directly.
242         return "";
243 }
244
245 sub __function_require_system {
246         my ($file, $name, $minimum, $maximum) = @_;
247         my ($system, $version);
248
249         # Linux is special and can be compared by distribution names.
250         if ($^O eq 'linux' && $name ne 'linux') {
251                 chomp($system = lc `lsb_release --id --short 2>/dev/null`);
252                 chomp($version = lc `lsb_release --release --short 2>/dev/null`);
253         }
254
255         # Gather information on the system if we don't have it already.
256         chomp($system ||= lc `uname -s 2>/dev/null`);
257         chomp($version ||= lc `uname -r 2>/dev/null`);
258
259         # We only care about the important bit of the version number so trim the rest.
260         $version =~ s/^(\d+\.\d+).+/$1/;
261
262         # Check whether the current system is suitable.
263         return undef if $name ne $system;
264         return undef if defined $minimum && $version < $minimum;
265         return undef if defined $maximum && $version > $maximum;
266
267         # Requirement directives don't change anything directly.
268         return "";
269 }
270
271 sub __function_require_version {
272         my ($file, $name, $minimum, $maximum) = @_;
273
274         # If pkg-config isn't installed then we can't do anything here.
275         if (system "pkg-config --exists $name ${\DIRECTIVE_ERROR_PIPE}") {
276                 print_warning "unable to look up the version of <|GREEN $name|> using pkg-config!";
277                 return undef;
278         }
279
280         # Check with pkg-config whether we have the required version.
281         return undef if defined $minimum && system "pkg-config --atleast-version $minimum $name";
282         return undef if defined $maximum && system "pkg-config --max-version $maximum $name";
283
284         # Requirement directives don't change anything directly.
285         return "";
286 }
287
288 sub __function_vendor_directory {
289         my ($file, $name) = @_;
290
291         # Try to look the directory up in the environment...
292         my $key = __environment 'INSPIRCD_VENDOR_', $name;
293         if (defined $ENV{$key}) {
294                 print_format "Found the <|GREEN $name|> vendor directory for <|GREEN ${\module_shrink $file}|> using the environment: <|BOLD $ENV{$key}|>\n";
295                 return $ENV{$key};
296         }
297
298         my $directory = catdir(VENDOR_DIRECTORY, $name);
299         if (-d $directory) {
300                 print_format "Using the default <|GREEN $name|> vendor directory for <|GREEN ${\module_shrink $file}|>: <|BOLD $directory|>\n";
301                 return $directory;
302         }
303
304         # We can't find it via the environment or via the filesystem so give up.
305         __error $file, "unable to find the <|GREEN $name|> vendor directory for <|GREEN ${\module_shrink $file}|>!";
306 }
307
308 sub __function_warning {
309         my ($file, @messages) = @_;
310         print_warning @messages;
311 }
312
313 1;