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