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 'import';
35 use File::Basename 'basename';
39 our @EXPORT = qw(cmd_clean cmd_help cmd_update
40 read_configure_cache write_configure_cache
41 get_compiler_info find_compiler
42 run_test test_file test_header
43 dump_hash get_property parse_templates);
45 sub __get_socketengines() {
47 foreach (<src/socketengines/socketengine_*.cpp>) {
48 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
49 push @socketengines, $1;
51 return @socketengines;
54 # TODO: when buildtool is done this can be mostly removed with
55 # the remainder being merged into parse_templates.
56 sub __get_template_settings($$) {
58 # These are actually hash references
59 my ($config, $compiler) = @_;
61 # Start off by populating with the config
62 my %settings = %$config;
64 # Compiler information
65 while (my ($key, $value) = each %{$compiler}) {
66 $settings{'COMPILER_' . $key} = $value;
70 my %version = get_version();
71 while (my ($key, $value) = each %version) {
72 $settings{'VERSION_' . $key} = $value;
75 # Miscellaneous information
76 $settings{SYSTEM_NAME} = lc $^O;
77 chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
83 unlink '.config.cache';
88 my $SELIST = join ', ', __get_socketengines();
92 When no options are specified, configure runs in interactive mode and you must
93 specify any required values manually. If one or more options are specified,
94 non-interactive configuration is started and any omitted values are defaulted.
98 --system Automatically set up the installation paths
99 for system-wide installation.
100 --prefix=[dir] The root install directory. If this is set then
101 all subdirectories will be adjusted accordingly.
103 --binary-dir=[dir] The location where the main server binary is
106 --config-dir=[dir] The location where the configuration files and
107 SSL certificates are stored.
109 --data-dir=[dir] The location where the data files, such as the
110 pid file, are stored.
112 --log-dir=[dir] The location where the log files are stored.
114 --module-dir=[dir] The location where the loadable modules are
117 --build-dir=[dir] The location to store files in while building.
122 --enable-extras=[extras] Enables a comma separated list of extra modules.
123 --disable-extras=[extras] Disables a comma separated list of extra modules.
124 --list-extras Shows the availability status of all extra
129 --clean Remove the configuration cache file and start
130 the interactive configuration wizard.
131 --disable-interactive Disables the interactive configuration wizard.
132 --help Show this message and exit.
133 --uid=[name] Sets the user to run InspIRCd as.
134 --socketengine=[name] Sets the socket engine to be used. Possible
136 --update Updates the build environment.
141 CXX=[name] Sets the C++ compiler to use when building the
142 server. If not specified then the build system
143 will search for c++, g++, clang++ or icpc.
145 If you have any problems with configuring InspIRCd then visit our IRC channel
146 at irc.ChatSpike.net #InspIRCd.
153 unless (-f '.config.cache') {
154 print "You have not run $0 before. Please do this before trying to update the build files.\n";
157 print "Updating...\n";
158 my %config = read_configure_cache();
159 my %compiler = get_compiler_info($config{CXX});
160 parse_templates(\%config, \%compiler);
161 print "Update complete!\n";
165 sub read_configure_cache {
167 open(CACHE, '.config.cache') or return %cfg;
168 while (my $line = <CACHE>) {
169 next if $line =~ /^\s*($|\#)/;
170 my ($key, $value) = ($line =~ /^(\S+)="(.*)"$/);
177 sub write_configure_cache(%) {
179 open(CACHE, ">.config.cache") or return 0;
180 while (my ($key, $value) = each %cfg) {
181 $value = "" unless defined $value;
182 print CACHE "$key=\"$value\"\n";
188 sub get_compiler_info($) {
190 my $version = `$binary -v 2>&1`;
191 if ($version =~ /(?:clang|llvm)\sversion\s(\d+\.\d+)/i) {
195 UNSUPPORTED => $1 lt '3.0',
196 REASON => 'Clang 2.9 and older do not have adequate C++ support.'
198 } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
202 UNSUPPORTED => $1 lt '4.1',
203 REASON => 'GCC 4.0 and older do not have adequate C++ support.'
205 } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
209 UNSUPPORTED => $2 lt '4.1',
210 REASON => "ICC $1 (GCC $2 compatibility mode) does not have adequate C++ support."
220 foreach my $compiler ('c++', 'g++', 'clang++', 'icpc') {
221 return $compiler unless system "$compiler -v > /dev/null 2>&1";
222 if ($^O eq 'Darwin') {
223 return $compiler unless system "xcrun $compiler -v > /dev/null 2>&1";
230 my ($what, $result) = @_;
231 print "Checking whether $what is available... ";
232 print $result ? "yes\n" : "no\n";
236 sub test_file($$;$) {
237 my ($cc, $file, $args) = @_;
240 $status ||= system "$cc -o __test_$file make/test/$file $args >/dev/null 2>&1";
241 $status ||= system "./__test_$file >/dev/null 2>&1";
242 unlink "./__test_$file";
246 sub test_header($$;$) {
247 my ($cc, $header, $args) = @_;
249 open(CC, "| $cc -E - $args >/dev/null 2>&1") or return 0;
250 print CC "#include <$header>";
255 sub get_property($$;$)
257 my ($file, $property, $default) = @_;
258 open(MODULE, $file) or return $default;
260 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/) {
261 next unless $1 eq $property;
263 return translate_functions($2, $file);
267 return defined $default ? $default : '';
271 print "\n\e[1;32mPre-build configuration is complete!\e[0m\n\n";
272 print "\e[0mBase install path:\e[1;32m\t\t$main::config{BASE_DIR}\e[0m\n";
273 print "\e[0mConfig path:\e[1;32m\t\t\t$main::config{CONFIG_DIR}\e[0m\n";
274 print "\e[0mData path:\e[1;32m\t\t\t$main::config{DATA_DIR}\e[0m\n";
275 print "\e[0mLog path:\e[1;32m\t\t\t$main::config{LOG_DIR}\e[0m\n";
276 print "\e[0mModule path:\e[1;32m\t\t\t$main::config{MODULE_DIR}\e[0m\n";
277 print "\e[0mCompiler:\e[1;32m\t\t\t$main::cxx{NAME} $main::cxx{VERSION}\e[0m\n";
278 print "\e[0mSocket engine:\e[1;32m\t\t\t$main::config{SOCKETENGINE}\e[0m\n";
279 print "\e[0mGnuTLS support:\e[1;32m\t\t\t$main::config{USE_GNUTLS}\e[0m\n";
280 print "\e[0mOpenSSL support:\e[1;32m\t\t$main::config{USE_OPENSSL}\e[0m\n";
283 sub parse_templates($$) {
285 # These are actually hash references
286 my ($config, $compiler) = @_;
288 # Collect settings to be used when generating files
289 my %settings = __get_template_settings($config, $compiler);
291 # Iterate through files in make/template.
292 foreach (<make/template/*>) {
293 print "Parsing $_...\n";
295 my (@lines, $mode, @platforms, %targets);
297 # First pass: parse template variables and directives.
298 while (my $line = <TEMPLATE>) {
301 # Does this line match a variable?
302 while ($line =~ /(@(\w+?)@)/) {
303 my ($variable, $name) = ($1, $2);
304 if (defined $settings{$name}) {
305 $line =~ s/$variable/$settings{$name}/;
307 print STDERR "Warning: unknown template variable '$name' in $_!\n";
312 # Does this line match a directive?
313 if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
314 if ($1 eq 'define') {
316 push @lines, "#define $2";
318 push @lines, "#undef $2";
320 } elsif ($1 eq 'mode') {
322 } elsif ($1 eq 'platform') {
324 } elsif ($1 eq 'target') {
325 if ($2 =~ /(\w+)\s(.+)/) {
328 $targets{DEFAULT} = $2;
331 print STDERR "Warning: unknown template command '$1' in $_!\n";
340 # Only proceed if this file should be templated on this platform.
341 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
343 # Add a default target if the template has not defined one.
344 unless (scalar keys %targets) {
345 $targets{DEFAULT} = basename $_;
348 # Second pass: parse makefile junk and write files.
349 while (my ($name, $target) = each %targets) {
351 # TODO: when buildtool is done this mess can be removed completely.
353 foreach my $line (@lines) {
355 # Are we parsing a makefile and does this line match a statement?
356 if ($name =~ /(?:BSD|GNU)_MAKE/ && $line =~ /^\s*\@(\w+)(?:\s+(.+))?$/) {
357 my @tokens = split /\s/, $2 if defined $2;
358 if ($1 eq 'DO_EXPORT' && defined $2) {
359 if ($name eq 'BSD_MAKE') {
360 foreach my $variable (@tokens) {
361 push @final_lines, "MAKEENV += $variable='\${$variable}'";
363 } elsif ($name eq 'GNU_MAKE') {
364 push @final_lines, "export $2";
366 } elsif ($1 eq 'ELSE') {
367 if ($name eq 'BSD_MAKE') {
368 push @final_lines, ".else";
369 } elsif ($name eq 'GNU_MAKE') {
370 push @final_lines, "else";
372 } elsif ($1 eq 'ENDIF') {
373 if ($name eq 'BSD_MAKE') {
374 push @final_lines, ".endif";
375 } elsif ($name eq 'GNU_MAKE') {
376 push @final_lines, "endif";
378 } elsif ($1 eq 'ELSIFEQ' && defined $2) {
379 if ($name eq 'BSD_MAKE') {
380 push @final_lines, ".elif $tokens[0] == $tokens[1]";
381 } elsif ($name eq 'GNU_MAKE') {
382 push @final_lines, "else ifeq ($tokens[0], $tokens[1])";
384 } elsif ($1 eq 'IFDEF' && defined $2) {
385 if ($name eq 'BSD_MAKE') {
386 push @final_lines, ".if defined($2)";
387 } elsif ($name eq 'GNU_MAKE') {
388 push @final_lines, "ifdef $2";
390 } elsif ($1 eq 'IFEQ' && defined $2) {
391 if ($name eq 'BSD_MAKE') {
392 push @final_lines, ".if $tokens[0] == $tokens[1]";
393 } elsif ($name eq 'GNU_MAKE') {
394 push @final_lines, "ifeq ($tokens[0],$tokens[1])";
396 } elsif ($1 eq 'IFNEQ' && defined $2) {
397 if ($name eq 'BSD_MAKE') {
398 push @final_lines, ".if $tokens[0] != $tokens[1]";
399 } elsif ($name eq 'GNU_MAKE') {
400 push @final_lines, "ifneq ($tokens[0],$tokens[1])";
402 } elsif ($1 eq 'IFNDEF' && defined $2) {
403 if ($name eq 'BSD_MAKE') {
404 push @final_lines, ".if !defined($2)";
405 } elsif ($name eq 'GNU_MAKE') {
406 push @final_lines, "ifndef $2";
408 } elsif ($1 eq 'TARGET' && defined $2) {
409 if ($tokens[0] eq $name) {
410 push @final_lines, substr($2, length($tokens[0]) + 1);
412 } elsif ($1 !~ /[A-Z]/) {
413 # HACK: silently ignore if lower case as these are probably make commands.
414 push @final_lines, $line;
416 print STDERR "Warning: unknown template command '$1' in $_!\n";
417 push @final_lines, $line;
422 push @final_lines, $line;
425 # Write the template file.
426 print "Writing $target...\n";
427 open(TARGET, ">$target");
428 foreach (@final_lines) {
429 print TARGET $_, "\n";
433 # Set file permissions.
435 chmod $mode, $target;