]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/configure.pm
Write generated templates to the .configure directory.
[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    => '.configure.cache';
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+)="(.*)"$/);
224                 $config{$key} = $value;
225         }
226         close(CACHE);
227         return %config;
228 }
229
230 sub write_configure_cache(%) {
231         print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
232         my %config = @_;
233         open(CACHE, '>', CONFIGURE_CACHE_FILE) or print_error "unable to write ${\CONFIGURE_CACHE_FILE}: $!";
234         while (my ($key, $value) = each %config) {
235                 $value //= '';
236                 say CACHE "$key=\"$value\"";
237         }
238         close(CACHE);
239 }
240
241 sub get_compiler_info($) {
242         my $binary = shift;
243         my $version = `$binary -v 2>&1`;
244         if ($version =~ /Apple\sLLVM\sversion\s(\d+\.\d+)/i) {
245                 # Apple version their LLVM releases slightly differently to the mainline LLVM.
246                 # See https://trac.macports.org/wiki/XcodeVersionInfo for more information.
247                 return (NAME => 'AppleClang', VERSION => $1);
248         } elsif ($version =~ /clang\sversion\s(\d+\.\d+)/i) {
249                 return (NAME => 'Clang', VERSION => $1);
250         } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
251                 return (NAME => 'GCC', VERSION => $1);
252         } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
253                 return (NAME => 'ICC', VERSION => $1);
254         }
255         return (NAME => $binary, VERSION => '0.0');
256 }
257
258 sub find_compiler {
259         my @compilers = qw(c++ g++ clang++ icpc);
260         foreach my $compiler (shift // @compilers) {
261                 return $compiler if __test_compiler $compiler;
262                 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
263         }
264 }
265
266 sub get_property($$;$)
267 {
268         my ($file, $property, $default) = @_;
269         open(MODULE, $file) or return $default;
270         while (<MODULE>) {
271                 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/) {
272                         next unless $1 eq $property;
273                         close(MODULE);
274                         return translate_functions($2, $file);
275                 }
276         }
277         close(MODULE);
278         return $default // '';
279 }
280
281 sub parse_templates($$$) {
282
283         # These are actually hash references
284         my ($config, $compiler, $version) = @_;
285
286         # Collect settings to be used when generating files
287         my %settings = __get_template_settings($config, $compiler, $version);
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/\Q$variable\E/$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} = catfile(CONFIGURE_DIRECTORY, 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                                 # Create the directory if it doesn't already exist.
424                                 my $directory = dirname $target;
425                                 unless (-e $directory) {
426                                         print_format "Creating <|GREEN $directory|> ...\n";
427                                         create_directory $directory, 0750 or print_error "unable to create $directory: $!";
428                                 };
429
430                                 # Write the template file.
431                                 print_format "Writing <|GREEN $target|> ...\n";
432                                 open(TARGET, '>', $target) or print_error "unable to write $target: $!";
433                                 foreach (@final_lines) {
434                                         say TARGET $_;
435                                 }
436                                 close(TARGET);
437
438                                 # Set file permissions.
439                                 if (defined $mode) {
440                                         chmod $mode, $target;
441                                 }
442                         }
443                 }
444         }
445 }
446
447 1;