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