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