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