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