]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/configure.pm
Miscellaneous improvements to configure.
[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 our @EXPORT = qw(cmd_clean
41                  cmd_help
42                  cmd_update
43                  read_configure_cache
44                  write_configure_cache
45                  get_compiler_info
46                  find_compiler
47                  run_test
48                  test_file
49                  test_header
50                  get_property
51                  parse_templates);
52
53 sub __get_socketengines {
54         my @socketengines;
55         foreach (<src/socketengines/socketengine_*.cpp>) {
56                 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
57                 push @socketengines, $1;
58         }
59         return @socketengines;
60 }
61
62 # TODO: when buildtool is done this can be mostly removed with
63 #       the remainder being merged into parse_templates.
64 sub __get_template_settings($$) {
65
66         # These are actually hash references
67         my ($config, $compiler) = @_;
68
69         # Start off by populating with the config
70         my %settings = %$config;
71
72         # Compiler information
73         while (my ($key, $value) = each %{$compiler}) {
74                 $settings{'COMPILER_' . $key} = $value;
75         }
76
77         # Version information
78         my %version = get_version();
79         while (my ($key, $value) = each %version) {
80                 $settings{'VERSION_' . $key} = $value;
81         }
82
83         # Miscellaneous information
84         $settings{SYSTEM_NAME} = lc $^O;
85         chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
86
87         return %settings;
88 }
89
90 sub cmd_clean {
91         unlink '.config.cache';
92 }
93
94 sub cmd_help {
95         my $PWD = getcwd();
96         my $SELIST = join ', ', __get_socketengines();
97         print <<EOH;
98 Usage: $0 [options]
99
100 When no options are specified, configure runs in interactive mode and you must
101 specify any required values manually. If one or more options are specified,
102 non-interactive configuration is started and any omitted values are defaulted.
103
104 PATH OPTIONS
105
106   --system                      Automatically set up the installation paths
107                                 for system-wide installation.
108   --prefix=[dir]                The root install directory. If this is set then
109                                 all subdirectories will be adjusted accordingly.
110                                 [$PWD/run]
111   --binary-dir=[dir]            The location where the main server binary is
112                                 stored.
113                                 [$PWD/run/bin]
114   --config-dir=[dir]            The location where the configuration files and
115                                 SSL certificates are stored.
116                                 [$PWD/run/conf]
117   --data-dir=[dir]              The location where the data files, such as the
118                                 pid file, are stored.
119                                 [$PWD/run/data]
120   --log-dir=[dir]               The location where the log files are stored.
121                                 [$PWD/run/logs]
122   --manual-dir=[dir]            The location where the manual files are stored.
123                                 [$PWD/run/manuals]
124   --module-dir=[dir]            The location where the loadable modules are
125                                 stored.
126                                 [$PWD/run/modules]
127
128 EXTRA MODULE OPTIONS
129
130   --enable-extras=[extras]      Enables a comma separated list of extra modules.
131   --disable-extras=[extras]     Disables a comma separated list of extra modules.
132   --list-extras                 Shows the availability status of all extra
133                                 modules.
134
135 MISC OPTIONS
136
137   --clean                       Remove the configuration cache file and start
138                                 the interactive configuration wizard.
139   --disable-interactive         Disables the interactive configuration wizard.
140   --gid=[id|name]               Sets the group to run InspIRCd as.
141   --help                        Show this message and exit.
142   --socketengine=[name]         Sets the socket engine to be used. Possible
143                                 values are $SELIST.
144   --uid=[id|name]               Sets the user to run InspIRCd as.
145   --update                      Updates the build environment.
146
147
148 FLAGS
149
150   CXX=[name]                    Sets the C++ compiler to use when building the
151                                 server. If not specified then the build system
152                                 will search for c++, g++, clang++ or icpc.
153
154 If you have any problems with configuring InspIRCd then visit our IRC channel
155 at irc.ChatSpike.net #InspIRCd.
156
157 EOH
158         exit 0;
159 }
160
161 sub cmd_update {
162         print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f '.config.cache';
163         print "Updating...\n";
164         my %config = read_configure_cache();
165         my %compiler = get_compiler_info($config{CXX});
166         parse_templates(\%config, \%compiler);
167         print "Update complete!\n";
168         exit 0;
169 }
170
171 sub read_configure_cache {
172         my %cfg = ();
173         open(CACHE, '.config.cache') or return %cfg;
174         while (my $line = <CACHE>) {
175                 next if $line =~ /^\s*($|\#)/;
176                 my ($key, $value) = ($line =~ /^(\S+)="(.*)"$/);
177                 $cfg{$key} = $value;
178         }
179         close(CACHE);
180         return %cfg;
181 }
182
183 sub write_configure_cache(%) {
184         print_format "Writing <|GREEN .config.cache|> ...\n";
185         my %cfg = @_;
186         open(CACHE, '>.config.cache') or print_error "unable to write .config.cache: $!";
187         while (my ($key, $value) = each %cfg) {
188                 $value = "" unless defined $value;
189                 print CACHE "$key=\"$value\"\n";
190         }
191         close(CACHE);
192 }
193
194 sub get_compiler_info($) {
195         my $binary = shift;
196         my $version = `$binary -v 2>&1`;
197         if ($version =~ /(?:clang|llvm)\sversion\s(\d+\.\d+)/i) {
198                 return (
199                         NAME => 'Clang',
200                         VERSION => $1,
201                         UNSUPPORTED => $1 lt '3.0',
202                         REASON => 'Clang 2.9 and older do not have adequate C++ support.'
203                 );
204         } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
205                 return (
206                         NAME => 'GCC',
207                         VERSION => $1,
208                         UNSUPPORTED => $1 lt '4.1',
209                         REASON => 'GCC 4.0 and older do not have adequate C++ support.'
210                 );
211         } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
212                 return (
213                         NAME => 'ICC',
214                         VERSION => $1,
215                         UNSUPPORTED => $2 lt '4.1',
216                         REASON => "ICC $1 (GCC $2 compatibility mode) does not have adequate C++ support."
217                 );
218         }
219         return (
220                 NAME => $binary,
221                 VERSION => '0.0'
222         );
223 }
224
225 sub find_compiler {
226         foreach my $compiler ('c++', 'g++', 'clang++', 'icpc') {
227                 return $compiler unless system "$compiler -v > /dev/null 2>&1";
228                 if ($^O eq 'Darwin') {
229                         return $compiler unless system "xcrun $compiler -v > /dev/null 2>&1";
230                 }
231         }
232         return "";
233 }
234
235 sub run_test($$) {
236         my ($what, $result) = @_;
237         print_format "Checking whether <|GREEN $what|> is available ... ";
238         print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
239         return $result;
240 }
241
242 sub test_file($$;$) {
243         my ($cc, $file, $args) = @_;
244         my $status = 0;
245         $args ||= '';
246         $status ||= system "$cc -o __test_$file make/test/$file $args >/dev/null 2>&1";
247         $status ||= system "./__test_$file >/dev/null 2>&1";
248         unlink  "./__test_$file";
249         return !$status;
250 }
251
252 sub test_header($$;$) {
253         my ($cc, $header, $args) = @_;
254         $args ||= '';
255         open(CC, "| $cc -E - $args >/dev/null 2>&1") or return 0;
256         print CC "#include <$header>";
257         close(CC);
258         return !$?;
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 defined $default ? $default : '';
274 }
275
276 sub parse_templates($$) {
277
278         # These are actually hash references
279         my ($config, $compiler) = @_;
280
281         # Collect settings to be used when generating files
282         my %settings = __get_template_settings($config, $compiler);
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                                         print TARGET $_, "\n";
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;