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