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';
40 our @EXPORT = qw(cmd_clean cmd_help cmd_update
41 read_configure_cache write_configure_cache
42 get_compiler_info find_compiler
43 run_test test_file test_header
44 dump_hash get_property parse_templates);
46 sub __get_socketengines() {
48 foreach (<src/socketengines/socketengine_*.cpp>) {
49 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
50 push @socketengines, $1;
52 return @socketengines;
55 # TODO: when buildtool is done this can be mostly removed with
56 # the remainder being merged into parse_templates.
57 sub __get_template_settings($$) {
59 # These are actually hash references
60 my ($config, $compiler) = @_;
62 # Start off by populating with the config
63 my %settings = %$config;
65 # Compiler information
66 while (my ($key, $value) = each %{$compiler}) {
67 $settings{'COMPILER_' . $key} = $value;
71 my %version = get_version();
72 while (my ($key, $value) = each %version) {
73 $settings{'VERSION_' . $key} = $value;
76 # Miscellaneous information
77 $settings{SYSTEM_NAME} = lc $^O;
78 chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
84 unlink '.config.cache';
89 my $SELIST = join ', ', __get_socketengines();
93 When no options are specified, configure runs in interactive mode and you must
94 specify any required values manually. If one or more options are specified,
95 non-interactive configuration is started and any omitted values are defaulted.
99 --system Automatically set up the installation paths
100 for system-wide installation.
101 --prefix=[dir] The root install directory. If this is set then
102 all subdirectories will be adjusted accordingly.
104 --binary-dir=[dir] The location where the main server binary is
107 --config-dir=[dir] The location where the configuration files and
108 SSL certificates are stored.
110 --data-dir=[dir] The location where the data files, such as the
111 pid file, are stored.
113 --log-dir=[dir] The location where the log files are stored.
115 --module-dir=[dir] The location where the loadable modules are
118 --build-dir=[dir] The location to store files in while building.
123 --enable-extras=[extras] Enables a comma separated list of extra modules.
124 --disable-extras=[extras] Disables a comma separated list of extra modules.
125 --list-extras Shows the availability status of all extra
130 --clean Remove the configuration cache file and start
131 the interactive configuration wizard.
132 --disable-interactive Disables the interactive configuration wizard.
133 --help Show this message and exit.
134 --uid=[name] Sets the user to run InspIRCd as.
135 --socketengine=[name] Sets the socket engine to be used. Possible
137 --update Updates the build environment.
142 CXX=[name] Sets the C++ compiler to use when building the
143 server. If not specified then the build system
144 will search for c++, g++, clang++ or icpc.
146 If you have any problems with configuring InspIRCd then visit our IRC channel
147 at irc.ChatSpike.net #InspIRCd.
154 print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f '.config.cache';
155 print "Updating...\n";
156 my %config = read_configure_cache();
157 my %compiler = get_compiler_info($config{CXX});
158 parse_templates(\%config, \%compiler);
159 print "Update complete!\n";
163 sub read_configure_cache {
165 open(CACHE, '.config.cache') or return %cfg;
166 while (my $line = <CACHE>) {
167 next if $line =~ /^\s*($|\#)/;
168 my ($key, $value) = ($line =~ /^(\S+)="(.*)"$/);
175 sub write_configure_cache(%) {
177 open(CACHE, ">.config.cache") or return 0;
178 while (my ($key, $value) = each %cfg) {
179 $value = "" unless defined $value;
180 print CACHE "$key=\"$value\"\n";
186 sub get_compiler_info($) {
188 my $version = `$binary -v 2>&1`;
189 if ($version =~ /(?:clang|llvm)\sversion\s(\d+\.\d+)/i) {
193 UNSUPPORTED => $1 lt '3.0',
194 REASON => 'Clang 2.9 and older do not have adequate C++ support.'
196 } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
200 UNSUPPORTED => $1 lt '4.1',
201 REASON => 'GCC 4.0 and older do not have adequate C++ support.'
203 } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
207 UNSUPPORTED => $2 lt '4.1',
208 REASON => "ICC $1 (GCC $2 compatibility mode) does not have adequate C++ support."
218 foreach my $compiler ('c++', 'g++', 'clang++', 'icpc') {
219 return $compiler unless system "$compiler -v > /dev/null 2>&1";
220 if ($^O eq 'Darwin') {
221 return $compiler unless system "xcrun $compiler -v > /dev/null 2>&1";
228 my ($what, $result) = @_;
229 print "Checking whether $what is available... ";
230 print $result ? "yes\n" : "no\n";
234 sub test_file($$;$) {
235 my ($cc, $file, $args) = @_;
238 $status ||= system "$cc -o __test_$file make/test/$file $args >/dev/null 2>&1";
239 $status ||= system "./__test_$file >/dev/null 2>&1";
240 unlink "./__test_$file";
244 sub test_header($$;$) {
245 my ($cc, $header, $args) = @_;
247 open(CC, "| $cc -E - $args >/dev/null 2>&1") or return 0;
248 print CC "#include <$header>";
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 : '';
269 print "\n\e[1;32mPre-build configuration is complete!\e[0m\n\n";
270 print "\e[0mBase install path:\e[1;32m\t\t$main::config{BASE_DIR}\e[0m\n";
271 print "\e[0mConfig path:\e[1;32m\t\t\t$main::config{CONFIG_DIR}\e[0m\n";
272 print "\e[0mData path:\e[1;32m\t\t\t$main::config{DATA_DIR}\e[0m\n";
273 print "\e[0mLog path:\e[1;32m\t\t\t$main::config{LOG_DIR}\e[0m\n";
274 print "\e[0mModule path:\e[1;32m\t\t\t$main::config{MODULE_DIR}\e[0m\n";
275 print "\e[0mCompiler:\e[1;32m\t\t\t$main::cxx{NAME} $main::cxx{VERSION}\e[0m\n";
276 print "\e[0mSocket engine:\e[1;32m\t\t\t$main::config{SOCKETENGINE}\e[0m\n";
277 print "\e[0mGnuTLS support:\e[1;32m\t\t\t$main::config{USE_GNUTLS}\e[0m\n";
278 print "\e[0mOpenSSL support:\e[1;32m\t\t$main::config{USE_OPENSSL}\e[0m\n";
281 sub parse_templates($$) {
283 # These are actually hash references
284 my ($config, $compiler) = @_;
286 # Collect settings to be used when generating files
287 my %settings = __get_template_settings($config, $compiler);
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);
295 # First pass: parse template variables and directives.
296 while (my $line = <TEMPLATE>) {
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/$variable/$settings{$name}/;
305 print_warning "unknown template variable '$name' in $_!";
310 # Does this line match a directive?
311 if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
312 if ($1 eq 'define') {
314 push @lines, "#define $2";
316 push @lines, "#undef $2";
318 } elsif ($1 eq 'mode') {
320 } elsif ($1 eq 'platform') {
322 } elsif ($1 eq 'target') {
323 if ($2 =~ /(\w+)\s(.+)/) {
326 $targets{DEFAULT} = $2;
329 print_warning "unknown template command '$1' in $_!";
338 # Only proceed if this file should be templated on this platform.
339 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
341 # Add a default target if the template has not defined one.
342 unless (scalar keys %targets) {
343 $targets{DEFAULT} = basename $_;
346 # Second pass: parse makefile junk and write files.
347 while (my ($name, $target) = each %targets) {
349 # TODO: when buildtool is done this mess can be removed completely.
351 foreach my $line (@lines) {
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}'";
361 } elsif ($name eq 'GNU_MAKE') {
362 push @final_lines, "export $2";
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";
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";
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])";
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";
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])";
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])";
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";
406 } elsif ($1 eq 'TARGET' && defined $2) {
407 if ($tokens[0] eq $name) {
408 push @final_lines, substr($2, length($tokens[0]) + 1);
410 } elsif ($1 !~ /[A-Z]/) {
411 # HACK: silently ignore if lower case as these are probably make commands.
412 push @final_lines, $line;
414 print_warning "unknown template command '$1' in $_!";
415 push @final_lines, $line;
420 push @final_lines, $line;
423 # Write the template file.
424 print_format "Writing <|GREEN $target|> ...\n";
425 open(TARGET, '>', $target) or print_error "unable to write $_: $!";
426 foreach (@final_lines) {
427 print TARGET $_, "\n";
431 # Set file permissions.
433 chmod $mode, $target;