]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/configure.pm
Don't suppress error output when INSPIRCD_VERBOSE is set.
[user/henk/code/inspircd.git] / make / configure.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2012-2014 Peter Powell <petpow@saberuk.com>
5 #   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 #   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
7 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 #
10 # This file is part of InspIRCd.  InspIRCd is free software: you can
11 # redistribute it and/or modify it under the terms of the GNU General Public
12 # License as published by the Free Software Foundation, version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but WITHOUT
15 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23
24 BEGIN {
25         require 5.10.0;
26 }
27
28 package make::configure;
29
30 use feature ':5.10';
31 use strict;
32 use warnings FATAL => qw(all);
33
34 use Cwd                   qw(getcwd);
35 use Exporter              qw(import);
36 use File::Basename        qw(basename dirname);
37 use File::Spec::Functions qw(catfile);
38
39 use make::common;
40 use make::console;
41
42 use constant CONFIGURE_DIRECTORY     => '.configure';
43 use constant CONFIGURE_CACHE_FILE    => catfile(CONFIGURE_DIRECTORY, 'cache.cfg');
44 use constant CONFIGURE_CACHE_VERSION => '1';
45 use constant CONFIGURE_ERROR_PIPE    => $ENV{INSPIRCD_VERBOSE} ? '' : '1>/dev/null 2>/dev/null';
46
47 our @EXPORT = qw(CONFIGURE_CACHE_FILE
48                  CONFIGURE_CACHE_VERSION
49                  cmd_clean
50                  cmd_help
51                  cmd_update
52                  run_test
53                  test_file
54                  test_header
55                  read_configure_cache
56                  write_configure_cache
57                  get_compiler_info
58                  find_compiler
59                  parse_templates);
60
61 sub __get_socketengines {
62         my @socketengines;
63         foreach (<src/socketengines/socketengine_*.cpp>) {
64                 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
65                 push @socketengines, $1;
66         }
67         return @socketengines;
68 }
69
70 # TODO: when buildtool is done this can be mostly removed with
71 #       the remainder being merged into parse_templates.
72 sub __get_template_settings($$$) {
73
74         # These are actually hash references
75         my ($config, $compiler, $version) = @_;
76
77         # Start off by populating with the config
78         my %settings = %$config;
79
80         # Compiler information
81         while (my ($key, $value) = each %{$compiler}) {
82                 $settings{'COMPILER_' . $key} = $value;
83         }
84
85         # Version information
86         while (my ($key, $value) = each %{$version}) {
87                 $settings{'VERSION_' . $key} = $value;
88         }
89
90         # Miscellaneous information
91         $settings{CONFIGURE_DIRECTORY} = CONFIGURE_DIRECTORY;
92         $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
93         $settings{SYSTEM_NAME} = lc $^O;
94         chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
95
96         return %settings;
97 }
98
99 sub __test_compiler($) {
100         my $compiler = shift;
101         return 0 unless run_test("`$compiler`", !system "$compiler -v ${\CONFIGURE_ERROR_PIPE}");
102         return 0 unless run_test("`$compiler`", test_file($compiler, 'compiler.cpp', '-fno-rtti'), 'compatible');
103         return 1;
104 }
105
106 sub cmd_clean {
107         unlink CONFIGURE_CACHE_FILE;
108 }
109
110 sub cmd_help {
111         my $PWD = getcwd();
112         my $SELIST = join ', ', __get_socketengines();
113         print <<EOH;
114 Usage: $0 [options]
115
116 When no options are specified, configure runs in interactive mode and you must
117 specify any required values manually. If one or more options are specified,
118 non-interactive configuration is started and any omitted values are defaulted.
119
120 PATH OPTIONS
121
122   --system                      Automatically set up the installation paths
123                                 for system-wide installation.
124   --prefix=[dir]                The root install directory. If this is set then
125                                 all subdirectories will be adjusted accordingly.
126                                 [$PWD/run]
127   --binary-dir=[dir]            The location where the main server binary is
128                                 stored.
129                                 [$PWD/run/bin]
130   --config-dir=[dir]            The location where the configuration files and
131                                 SSL certificates are stored.
132                                 [$PWD/run/conf]
133   --data-dir=[dir]              The location where the data files, such as the
134                                 pid file, are stored.
135                                 [$PWD/run/data]
136   --log-dir=[dir]               The location where the log files are stored.
137                                 [$PWD/run/logs]
138   --manual-dir=[dir]            The location where the manual files are stored.
139                                 [$PWD/run/manuals]
140   --module-dir=[dir]            The location where the loadable modules are
141                                 stored.
142                                 [$PWD/run/modules]
143
144 EXTRA MODULE OPTIONS
145
146   --enable-extras=[extras]      Enables a comma separated list of extra modules.
147   --disable-extras=[extras]     Disables a comma separated list of extra modules.
148   --list-extras                 Shows the availability status of all extra
149                                 modules.
150
151 MISC OPTIONS
152
153   --clean                       Remove the configuration cache file and start
154                                 the interactive configuration wizard.
155   --disable-interactive         Disables the interactive configuration wizard.
156   --distribution-label=[text]   Sets a distribution specific version label in
157                                 the build configuration.
158   --gid=[id|name]               Sets the group to run InspIRCd as.
159   --help                        Show this message and exit.
160   --socketengine=[name]         Sets the socket engine to be used. Possible
161                                 values are $SELIST.
162   --uid=[id|name]               Sets the user to run InspIRCd as.
163   --update                      Updates the build environment with the settings
164                                 from the cache.
165
166
167 FLAGS
168
169   CXX=[name]                    Sets the C++ compiler to use when building the
170                                 server. If not specified then the build system
171                                 will search for c++, g++, clang++ or icpc.
172
173 If you have any problems with configuring InspIRCd then visit our IRC channel
174 at irc.inspircd.org #InspIRCd for support.
175
176 EOH
177         exit 0;
178 }
179
180 sub cmd_update {
181         print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
182         say 'Updating...';
183         my %config = read_configure_cache();
184         my %compiler = get_compiler_info($config{CXX});
185         my %version = get_version $config{DISTRIBUTION};
186         parse_templates(\%config, \%compiler, \%version);
187         say 'Update complete!';
188         exit 0;
189 }
190
191 sub run_test($$;$) {
192         my ($what, $result, $adjective) = @_;
193         $adjective //= 'available';
194         print_format "Checking whether <|GREEN $what|> is $adjective ... ";
195         print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
196         return $result;
197 }
198
199 sub test_file($$;$) {
200         my ($compiler, $file, $args) = @_;
201         my $status = 0;
202         $args //= '';
203         $status ||= system "$compiler -o __test_$file make/test/$file $args ${\CONFIGURE_ERROR_PIPE}";
204         $status ||= system "./__test_$file ${\CONFIGURE_ERROR_PIPE}";
205         unlink "./__test_$file";
206         return !$status;
207 }
208
209 sub test_header($$;$) {
210         my ($compiler, $header, $args) = @_;
211         $args //= '';
212         open(COMPILER, "| $compiler -E - $args ${\CONFIGURE_ERROR_PIPE}") or return 0;
213         print COMPILER "#include <$header>";
214         close(COMPILER);
215         return !$?;
216 }
217
218 sub read_configure_cache {
219         my %config;
220         open(CACHE, CONFIGURE_CACHE_FILE) or return %config;
221         while (my $line = <CACHE>) {
222                 next if $line =~ /^\s*($|\#)/;
223                 my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/);
224                 $config{$key} = $value;
225         }
226         close(CACHE);
227         return %config;
228 }
229
230 sub write_configure_cache(%) {
231         unless (-e CONFIGURE_DIRECTORY) {
232                 print_format "Creating <|GREEN ${\CONFIGURE_DIRECTORY}|> ...\n";
233                 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
234         }
235
236         print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
237         my %config = @_;
238         open(CACHE, '>', CONFIGURE_CACHE_FILE) or print_error "unable to write ${\CONFIGURE_CACHE_FILE}: $!";
239         while (my ($key, $value) = each %config) {
240                 $value //= '';
241                 say CACHE "$key $value";
242         }
243         close(CACHE);
244 }
245
246 sub get_compiler_info($) {
247         my $binary = shift;
248         my $version = `$binary -v 2>&1`;
249         if ($version =~ /Apple\sLLVM\sversion\s(\d+\.\d+)/i) {
250                 # Apple version their LLVM releases slightly differently to the mainline LLVM.
251                 # See https://trac.macports.org/wiki/XcodeVersionInfo for more information.
252                 return (NAME => 'AppleClang', VERSION => $1);
253         } elsif ($version =~ /clang\sversion\s(\d+\.\d+)/i) {
254                 return (NAME => 'Clang', VERSION => $1);
255         } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
256                 return (NAME => 'GCC', VERSION => $1);
257         } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
258                 return (NAME => 'ICC', VERSION => $1);
259         }
260         return (NAME => $binary, VERSION => '0.0');
261 }
262
263 sub find_compiler {
264         my @compilers = qw(c++ g++ clang++ icpc);
265         foreach my $compiler (shift // @compilers) {
266                 return $compiler if __test_compiler $compiler;
267                 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
268         }
269 }
270
271 sub parse_templates($$$) {
272
273         # These are actually hash references
274         my ($config, $compiler, $version) = @_;
275
276         # Collect settings to be used when generating files
277         my %settings = __get_template_settings($config, $compiler, $version);
278
279         # Iterate through files in make/template.
280         foreach (<make/template/*>) {
281                 print_format "Parsing <|GREEN $_|> ...\n";
282                 open(TEMPLATE, $_) or print_error "unable to read $_: $!";
283                 my (@lines, $mode, @platforms, %targets);
284
285                 # First pass: parse template variables and directives.
286                 while (my $line = <TEMPLATE>) {
287                         chomp $line;
288
289                         # Does this line match a variable?
290                         while ($line =~ /(@(\w+?)@)/) {
291                                 my ($variable, $name) = ($1, $2);
292                                 if (defined $settings{$name}) {
293                                         $line =~ s/\Q$variable\E/$settings{$name}/;
294                                 } else {
295                                         print_warning "unknown template variable '$name' in $_!";
296                                         last;
297                                 }
298                         }
299
300                         # Does this line match a directive?
301                         if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
302                                 if ($1 eq 'define') {
303                                         if ($settings{$2}) {
304                                                 push @lines, "#define $2";
305                                         } else {
306                                                 push @lines, "#undef $2";
307                                         }
308                                 } elsif ($1 eq 'mode') {
309                                         $mode = oct $2;
310                                 } elsif ($1 eq 'platform') {
311                                         push @platforms, $2;
312                                 } elsif ($1 eq 'target') {
313                                         if ($2 =~ /(\w+)\s(.+)/) {
314                                                 $targets{$1} = $2;
315                                         } else {
316                                                 $targets{DEFAULT} = $2;
317                                         }
318                                 } else {
319                                         print_warning "unknown template command '$1' in $_!";
320                                         push @lines, $line;
321                                 }
322                                 next;
323                         }
324                         push @lines, $line;
325                 }
326                 close(TEMPLATE);
327
328                 # Only proceed if this file should be templated on this platform.
329                 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
330
331                         # Add a default target if the template has not defined one.
332                         unless (scalar keys %targets) {
333                                 $targets{DEFAULT} = catfile(CONFIGURE_DIRECTORY, basename $_);
334                         }
335
336                         # Second pass: parse makefile junk and write files.
337                         while (my ($name, $target) = each %targets) {
338
339                                 # TODO: when buildtool is done this mess can be removed completely.
340                                 my @final_lines;
341                                 foreach my $line (@lines) {
342
343                                         # Are we parsing a makefile and does this line match a statement?
344                                         if ($name =~ /(?:BSD|GNU)_MAKE/ && $line =~ /^\s*\@(\w+)(?:\s+(.+))?$/) {
345                                                 my @tokens = split /\s/, $2 if defined $2;
346                                                 if ($1 eq 'DO_EXPORT' && defined $2) {
347                                                         if ($name eq 'BSD_MAKE') {
348                                                                 foreach my $variable (@tokens) {
349                                                                         push @final_lines, "MAKEENV += $variable='\${$variable}'";
350                                                                 }
351                                                         } elsif ($name eq 'GNU_MAKE') {
352                                                                 push @final_lines, "export $2";
353                                                         }
354                                                 } elsif ($1 eq 'ELSE') {
355                                                         if ($name eq 'BSD_MAKE') {
356                                                                 push @final_lines, ".else";
357                                                         } elsif ($name eq 'GNU_MAKE') {
358                                                                 push @final_lines, "else";
359                                                         }
360                                                 } elsif ($1 eq 'ENDIF') {
361                                                         if ($name eq 'BSD_MAKE') {
362                                                                 push @final_lines, ".endif";
363                                                         } elsif ($name eq 'GNU_MAKE') {
364                                                                 push @final_lines, "endif";
365                                                         }
366                                                 } elsif ($1 eq 'ELSIFEQ' && defined $2) {
367                                                         if ($name eq 'BSD_MAKE') {
368                                                                 push @final_lines, ".elif $tokens[0] == $tokens[1]";
369                                                         } elsif ($name eq 'GNU_MAKE') {
370                                                                 push @final_lines, "else ifeq ($tokens[0], $tokens[1])";
371                                                         }
372                                                 } elsif ($1 eq 'IFDEF' && defined $2) {
373                                                         if ($name eq 'BSD_MAKE') {
374                                                                 push @final_lines, ".if defined($2)";
375                                                         } elsif ($name eq 'GNU_MAKE') {
376                                                                 push @final_lines, "ifdef $2";
377                                                         }
378                                                 } elsif ($1 eq 'IFEQ' && defined $2) {
379                                                         if ($name eq 'BSD_MAKE') {
380                                                                 push @final_lines, ".if $tokens[0] == $tokens[1]";
381                                                         } elsif ($name eq 'GNU_MAKE') {
382                                                                 push @final_lines, "ifeq ($tokens[0],$tokens[1])";
383                                                         }
384                                                 } elsif ($1 eq 'IFNEQ' && defined $2) {
385                                                         if ($name eq 'BSD_MAKE') {
386                                                                 push @final_lines, ".if $tokens[0] != $tokens[1]";
387                                                         } elsif ($name eq 'GNU_MAKE') {
388                                                                 push @final_lines, "ifneq ($tokens[0],$tokens[1])";
389                                                         }
390                                                 } elsif ($1 eq 'IFNDEF' && defined $2) {
391                                                         if ($name eq 'BSD_MAKE') {
392                                                                 push @final_lines, ".if !defined($2)";
393                                                         } elsif ($name eq 'GNU_MAKE') {
394                                                                 push @final_lines, "ifndef $2";
395                                                         }
396                                                 } elsif ($1 eq 'TARGET' && defined $2) {
397                                                         if ($tokens[0] eq $name) {
398                                                                 push @final_lines, substr($2, length($tokens[0]) + 1);
399                                                         }
400                                                 } elsif ($1 !~ /[A-Z]/) {
401                                                         # HACK: silently ignore if lower case as these are probably make commands.
402                                                         push @final_lines, $line;
403                                                 } else {
404                                                         print_warning "unknown template command '$1' in $_!";
405                                                         push @final_lines, $line;
406                                                 }
407                                                 next;
408                                         }
409
410                                         push @final_lines, $line;
411                                 }
412
413                                 # Create the directory if it doesn't already exist.
414                                 my $directory = dirname $target;
415                                 unless (-e $directory) {
416                                         print_format "Creating <|GREEN $directory|> ...\n";
417                                         create_directory $directory, 0750 or print_error "unable to create $directory: $!";
418                                 };
419
420                                 # Write the template file.
421                                 print_format "Writing <|GREEN $target|> ...\n";
422                                 open(TARGET, '>', $target) or print_error "unable to write $target: $!";
423                                 foreach (@final_lines) {
424                                         say TARGET $_;
425                                 }
426                                 close(TARGET);
427
428                                 # Set file permissions.
429                                 if (defined $mode) {
430                                         chmod $mode, $target;
431                                 }
432                         }
433                 }
434         }
435 }
436
437 1;