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