2 # InspIRCd -- Internet Relay Chat Daemon
4 # Copyright (C) 2016 Peter Powell <petpow@saberuk.com>
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.
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
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/>.
20 package make::directive;
28 use warnings FATAL => qw(all);
30 use File::Basename qw(basename dirname);
31 use File::Spec::Functions qw(catdir);
32 use Exporter qw(import);
37 use constant DIRECTIVE_ERROR_PIPE => $ENV{INSPIRCD_VERBOSE} ? '' : '2>/dev/null';
38 use constant VENDOR_DIRECTORY => catdir(dirname(dirname(__FILE__)), 'vendor');
40 our @EXPORT = qw(get_directive
43 sub get_directive($$;$)
45 my ($file, $property, $default) = @_;
46 open(my $fh, $file) or return $default;
50 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/ || $_ =~ /^\/\/\/ \$(\S+): (.+)/) {
51 next unless $1 eq $property;
52 $value .= ' ' . execute_functions($file, $1, $2);
57 # Strip all extraneous whitespace.
58 $value =~ s/^\s+|\s+$//g;
59 return $value || $default;
62 sub execute_functions($$$) {
63 my ($file, $name, $line) = @_;
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;
70 # Call the subroutine and replace the function.
71 my $result = $sub->($file, @parameters);
72 if (defined $result) {
73 $line = $` . $result . $';
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.
86 my ($prefix, $suffix) = @_;
87 $suffix =~ s/[-.]/_/g;
88 $suffix =~ s/[^A-Za-z0-9_]//g;
89 return $prefix . uc $suffix;
94 my $name = basename $file, '.cpp';
100 my ($file, @message) = @_;
103 # If we have package details then suggest to the user that they check
104 # that they have the packages installed.=
105 my $dependencies = get_directive($file, 'PackageInfo');
106 if (defined $dependencies) {
107 my @packages = sort grep { /^\S+$/ } split /\s/, $dependencies;
108 push @message, 'You should make sure you have the following packages installed:';
110 push @message, " * $_";
113 push @message, 'You should make sure that you have all of the required dependencies';
114 push @message, 'for this module installed.';
118 # If we have author information then tell the user to report the bug
119 # to them. Otherwise, assume it is a bundled module and tell the user
120 # to report it to the InspIRCd issue tracker.
121 my $author = get_directive($file, 'ModAuthor');
122 if (defined $author) {
123 push @message, 'If you believe this error to be a bug then you can try to contact the';
124 push @message, 'author of this module:';
125 my $author_mail = get_directive($file, 'ModAuthorMail');
126 if (defined $author_mail) {
127 push @message, " * $author <$author_mail>";
129 push @message, " * $author";
132 push @message, 'If you believe this error to be a bug then you can file a bug report';
133 push @message, 'at https://github.com/inspircd/inspircd/issues';
137 push @message, 'If you would like help with fixing this problem then visit our IRC';
138 push @message, 'channel at irc.inspircd.org #InspIRCd for support.';
141 print_error @message;
144 sub __function_error {
145 my ($file, @messages) = @_;
146 __error $file, @messages;
149 sub __function_execute {
150 my ($file, $command, $environment, $defaults) = @_;
152 # Try to execute the command...
153 chomp(my $result = `$command ${\DIRECTIVE_ERROR_PIPE}`);
155 print_format "Execution of `<|GREEN $command|>` succeeded: <|BOLD $result|>\n";
159 # If looking up with pkg-config fails then check the environment...
160 if (defined $environment && $environment ne '') {
161 $environment = __environment 'INSPIRCD_', $environment;
162 if (defined $ENV{$environment}) {
163 print_format "Execution of `<|GREEN $command|>` failed; using the environment: <|BOLD $ENV{$environment}|>\n";
164 return $ENV{$environment};
168 # If all else fails then look for the defaults..
169 if (defined $defaults) {
170 print_format "Execution of `<|GREEN $command|>` failed; using the defaults: <|BOLD $defaults|>\n";
174 # Executing the command failed and we don't have any defaults so give up.
175 __error $file, "`<|GREEN $command|>` exited with a non-zero exit code!";
178 sub __function_find_compiler_flags {
179 my ($file, $name, $defaults) = @_;
181 # Try to look up the compiler flags with pkg-config...
182 chomp(my $flags = `pkg-config --cflags $name ${\DIRECTIVE_ERROR_PIPE}`);
184 print_format "Found the <|GREEN $name|> compiler flags for <|GREEN ${\__module $file}|> using pkg-config: <|BOLD $flags|>\n";
188 # If looking up with pkg-config fails then check the environment...
189 my $key = __environment 'INSPIRCD_CXXFLAGS_', $name;
190 if (defined $ENV{$key}) {
191 print_format "Found the <|GREEN $name|> compiler flags for <|GREEN ${\__module $file}|> using the environment: <|BOLD $ENV{$key}|>\n";
195 # If all else fails then look for the defaults..
196 if (defined $defaults) {
197 print_format "Using the default <|GREEN $name|> compiler flags for <|GREEN ${\__module $file}|>: <|BOLD $defaults|>\n";
201 # We can't find it via pkg-config, via the environment, or via the defaults so give up.
202 __error $file, "unable to find the <|GREEN $name|> compiler flags for <|GREEN ${\__module $file}|>!";
205 sub __function_find_linker_flags {
206 my ($file, $name, $defaults) = @_;
208 # Try to look up the linker flags with pkg-config...
209 chomp(my $flags = `pkg-config --libs $name ${\DIRECTIVE_ERROR_PIPE}`);
211 print_format "Found the <|GREEN $name|> linker flags for <|GREEN ${\__module $file}|> using pkg-config: <|BOLD $flags|>\n";
215 # If looking up with pkg-config fails then check the environment...
216 my $key = __environment 'INSPIRCD_CXXFLAGS_', $name;
217 if (defined $ENV{$key}) {
218 print_format "Found the <|GREEN $name|> linker flags for <|GREEN ${\__module $file}|> using the environment: <|BOLD $ENV{$key}|>\n";
222 # If all else fails then look for the defaults..
223 if (defined $defaults) {
224 print_format "Using the default <|GREEN $name|> linker flags for <|GREEN ${\__module $file}|>: <|BOLD $defaults|>\n";
228 # We can't find it via pkg-config, via the environment, or via the defaults so give up.
229 __error $file, "unable to find the <|GREEN $name|> linker flags for <|GREEN ${\__module $file}|>!";
232 sub __function_require_compiler {
233 my ($file, $name, $minimum, $maximum) = @_;
235 # Look up information about the compiler.
236 return undef unless $ENV{CXX};
237 my %compiler = get_compiler_info($ENV{CXX});
239 # Check whether the current compiler is suitable.
240 return undef unless $compiler{NAME} eq $name;
241 return undef if defined $minimum && $compiler{VERSION} < $minimum;
242 return undef if defined $maximum && $compiler{VERSION} > $maximum;
244 # Requirement directives don't change anything directly.
248 sub __function_require_system {
249 my ($file, $name, $minimum, $maximum) = @_;
250 my ($system, $version);
252 # Linux is special and can be compared by distribution names.
253 if ($^O eq 'linux' && $name ne 'linux') {
254 chomp($system = lc `lsb_release --id --short 2>/dev/null`);
255 chomp($version = lc `lsb_release --release --short 2>/dev/null`);
258 # Gather information on the system if we don't have it already.
259 chomp($system ||= lc `uname -s 2>/dev/null`);
260 chomp($version ||= lc `uname -r 2>/dev/null`);
262 # We only care about the important bit of the version number so trim the rest.
263 $version =~ s/^(\d+\.\d+).+/$1/;
265 # Check whether the current system is suitable.
266 return undef if $name ne $system;
267 return undef if defined $minimum && $version < $minimum;
268 return undef if defined $maximum && $version > $maximum;
270 # Requirement directives don't change anything directly.
274 sub __function_require_version {
275 my ($file, $name, $minimum, $maximum) = @_;
277 # If pkg-config isn't installed then we can't do anything here.
278 if (system "pkg-config --exists $name ${\DIRECTIVE_ERROR_PIPE}") {
279 print_warning "unable to look up the version of <|GREEN $name|> using pkg-config!";
283 # Check with pkg-config whether we have the required version.
284 return undef if defined $minimum && system "pkg-config --atleast-version $minimum $name";
285 return undef if defined $maximum && system "pkg-config --max-version $maximum $name";
287 # Requirement directives don't change anything directly.
291 sub __function_vendor_directory {
292 my ($file, $name) = @_;
294 # Try to look the directory up in the environment...
295 my $key = __environment 'INSPIRCD_VENDOR_', $name;
296 if (defined $ENV{$key}) {
297 print_format "Found the <|GREEN $name|> vendor directory for <|GREEN ${\__module $file}|> using the environment: <|BOLD $ENV{$key}|>\n";
301 my $directory = catdir(VENDOR_DIRECTORY, $name);
303 print_format "Using the default <|GREEN $name|> vendor directory for <|GREEN ${\__module $file}|>: <|BOLD $directory|>\n";
307 # We can't find it via the environment or via the filesystem so give up.
308 __error $file, "unable to find the <|GREEN $name|> vendor directory for <|GREEN ${\__module $file}|>!";
311 sub __function_warning {
312 my ($file, @messages) = @_;
313 print_warning @messages;