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