2 # InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
28 package make::configure;
31 use warnings FATAL => qw(all);
34 use Exporter qw(import);
35 use File::Basename qw(basename);
40 use constant CONFIGURE_CACHE_FILE => '.configure.cache';
41 use constant CONFIGURE_CACHE_VERSION => '1';
43 our @EXPORT = qw(CONFIGURE_CACHE_FILE
44 CONFIGURE_CACHE_VERSION
58 sub __get_socketengines {
60 foreach (<src/socketengines/socketengine_*.cpp>) {
61 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
62 push @socketengines, $1;
64 return @socketengines;
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($$$) {
71 # These are actually hash references
72 my ($config, $compiler, $version) = @_;
74 # Start off by populating with the config
75 my %settings = %$config;
77 # Compiler information
78 while (my ($key, $value) = each %{$compiler}) {
79 $settings{'COMPILER_' . $key} = $value;
83 while (my ($key, $value) = each %{$version}) {
84 $settings{'VERSION_' . $key} = $value;
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`);
96 unlink CONFIGURE_CACHE_FILE;
101 my $SELIST = join ', ', __get_socketengines();
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.
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.
116 --binary-dir=[dir] The location where the main server binary is
119 --config-dir=[dir] The location where the configuration files and
120 SSL certificates are stored.
122 --data-dir=[dir] The location where the data files, such as the
123 pid file, are stored.
125 --log-dir=[dir] The location where the log files are stored.
127 --manual-dir=[dir] The location where the manual files are stored.
129 --module-dir=[dir] The location where the loadable modules are
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
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
151 --uid=[id|name] Sets the user to run InspIRCd as.
152 --update Updates the build environment.
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.
161 If you have any problems with configuring InspIRCd then visit our IRC channel
162 at irc.ChatSpike.net #InspIRCd.
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";
180 my ($what, $result) = @_;
181 print_format "Checking whether <|GREEN $what|> is available ... ";
182 print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
186 sub test_file($$;$) {
187 my ($compiler, $file, $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";
196 sub test_header($$;$) {
197 my ($compiler, $header, $args) = @_;
199 open(COMPILER, "| $compiler -E - $args >/dev/null 2>&1") or return 0;
200 print COMPILER "#include <$header>";
205 sub read_configure_cache {
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;
217 sub write_configure_cache(%) {
218 print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
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";
228 sub get_compiler_info($) {
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);
240 return (NAME => $binary, VERSION => '0.0');
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';
253 sub get_property($$;$)
255 my ($file, $property, $default) = @_;
256 open(MODULE, $file) or return $default;
258 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/) {
259 next unless $1 eq $property;
261 return translate_functions($2, $file);
265 return defined $default ? $default : '';
268 sub parse_templates($$$) {
270 # These are actually hash references
271 my ($config, $compiler, $version) = @_;
273 # Collect settings to be used when generating files
274 my %settings = __get_template_settings($config, $compiler, $version);
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);
282 # First pass: parse template variables and directives.
283 while (my $line = <TEMPLATE>) {
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}/;
292 print_warning "unknown template variable '$name' in $_!";
297 # Does this line match a directive?
298 if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
299 if ($1 eq 'define') {
301 push @lines, "#define $2";
303 push @lines, "#undef $2";
305 } elsif ($1 eq 'mode') {
307 } elsif ($1 eq 'platform') {
309 } elsif ($1 eq 'target') {
310 if ($2 =~ /(\w+)\s(.+)/) {
313 $targets{DEFAULT} = $2;
316 print_warning "unknown template command '$1' in $_!";
325 # Only proceed if this file should be templated on this platform.
326 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
328 # Add a default target if the template has not defined one.
329 unless (scalar keys %targets) {
330 $targets{DEFAULT} = basename $_;
333 # Second pass: parse makefile junk and write files.
334 while (my ($name, $target) = each %targets) {
336 # TODO: when buildtool is done this mess can be removed completely.
338 foreach my $line (@lines) {
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}'";
348 } elsif ($name eq 'GNU_MAKE') {
349 push @final_lines, "export $2";
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";
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";
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])";
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";
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])";
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])";
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";
393 } elsif ($1 eq 'TARGET' && defined $2) {
394 if ($tokens[0] eq $name) {
395 push @final_lines, substr($2, length($tokens[0]) + 1);
397 } elsif ($1 !~ /[A-Z]/) {
398 # HACK: silently ignore if lower case as these are probably make commands.
399 push @final_lines, $line;
401 print_warning "unknown template command '$1' in $_!";
402 push @final_lines, $line;
407 push @final_lines, $line;
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";
418 # Set file permissions.
420 chmod $mode, $target;