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