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