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) = @_;
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 my %version = get_version();
84 while (my ($key, $value) = each %version) {
85 $settings{'VERSION_' . $key} = $value;
88 # Miscellaneous information
89 $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
90 $settings{SYSTEM_NAME} = lc $^O;
91 chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
97 unlink CONFIGURE_CACHE_FILE;
102 my $SELIST = join ', ', __get_socketengines();
106 When no options are specified, configure runs in interactive mode and you must
107 specify any required values manually. If one or more options are specified,
108 non-interactive configuration is started and any omitted values are defaulted.
112 --system Automatically set up the installation paths
113 for system-wide installation.
114 --prefix=[dir] The root install directory. If this is set then
115 all subdirectories will be adjusted accordingly.
117 --binary-dir=[dir] The location where the main server binary is
120 --config-dir=[dir] The location where the configuration files and
121 SSL certificates are stored.
123 --data-dir=[dir] The location where the data files, such as the
124 pid file, are stored.
126 --log-dir=[dir] The location where the log files are stored.
128 --manual-dir=[dir] The location where the manual files are stored.
130 --module-dir=[dir] The location where the loadable modules are
136 --enable-extras=[extras] Enables a comma separated list of extra modules.
137 --disable-extras=[extras] Disables a comma separated list of extra modules.
138 --list-extras Shows the availability status of all extra
143 --clean Remove the configuration cache file and start
144 the interactive configuration wizard.
145 --disable-interactive Disables the interactive configuration wizard.
146 --gid=[id|name] Sets the group to run InspIRCd as.
147 --help Show this message and exit.
148 --socketengine=[name] Sets the socket engine to be used. Possible
150 --uid=[id|name] Sets the user to run InspIRCd as.
151 --update Updates the build environment.
156 CXX=[name] Sets the C++ compiler to use when building the
157 server. If not specified then the build system
158 will search for c++, g++, clang++ or icpc.
160 If you have any problems with configuring InspIRCd then visit our IRC channel
161 at irc.ChatSpike.net #InspIRCd.
168 print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
169 print "Updating...\n";
170 my %config = read_configure_cache();
171 my %compiler = get_compiler_info($config{CXX});
172 parse_templates(\%config, \%compiler);
173 print "Update complete!\n";
178 my ($what, $result) = @_;
179 print_format "Checking whether <|GREEN $what|> is available ... ";
180 print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
184 sub test_file($$;$) {
185 my ($compiler, $file, $args) = @_;
188 $status ||= system "$compiler -o __test_$file make/test/$file $args >/dev/null 2>&1";
189 $status ||= system "./__test_$file >/dev/null 2>&1";
190 unlink "./__test_$file";
194 sub test_header($$;$) {
195 my ($compiler, $header, $args) = @_;
197 open(COMPILER, "| $compiler -E - $args >/dev/null 2>&1") or return 0;
198 print COMPILER "#include <$header>";
203 sub read_configure_cache {
205 open(CACHE, CONFIGURE_CACHE_FILE) or return %config;
206 while (my $line = <CACHE>) {
207 next if $line =~ /^\s*($|\#)/;
208 my ($key, $value) = ($line =~ /^(\S+)="(.*)"$/);
209 $config{$key} = $value;
215 sub write_configure_cache(%) {
216 print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
218 open(CACHE, '>', CONFIGURE_CACHE_FILE) or print_error "unable to write ${\CONFIGURE_CACHE_FILE}: $!";
219 while (my ($key, $value) = each %config) {
220 $value = '' unless defined $value;
221 print CACHE "$key=\"$value\"\n";
226 sub get_compiler_info($) {
228 my $version = `$binary -v 2>&1`;
229 if ($version =~ /clang\sversion\s(\d+\.\d+)/i || $version =~ /^apple.+\(based\son\sllvm\s(\d+\.\d+)/i) {
230 # Apple version their LLVM releases slightly differently to the mainline LLVM.
231 # See https://trac.macports.org/wiki/XcodeVersionInfo for more information.
232 return (NAME => 'Clang', VERSION => $1);
233 } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
234 return (NAME => 'GCC', VERSION => $1);
235 } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
236 return (NAME => 'ICC', VERSION => $1);
238 return (NAME => $binary, VERSION => '0.0');
242 my @compilers = qw(c++ g++ clang++ icpc);
243 foreach my $compiler (shift || @compilers) {
244 return $compiler if run_test "`$compiler`", test_file $compiler, 'compiler.cpp';
245 if ($^O eq 'darwin') {
246 return $compiler if run_test "`xcrun $compiler`", test_file "xcrun $compiler", 'compiler.cpp';
251 sub get_property($$;$)
253 my ($file, $property, $default) = @_;
254 open(MODULE, $file) or return $default;
256 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/) {
257 next unless $1 eq $property;
259 return translate_functions($2, $file);
263 return defined $default ? $default : '';
266 sub parse_templates($$) {
268 # These are actually hash references
269 my ($config, $compiler) = @_;
271 # Collect settings to be used when generating files
272 my %settings = __get_template_settings($config, $compiler);
274 # Iterate through files in make/template.
275 foreach (<make/template/*>) {
276 print_format "Parsing <|GREEN $_|> ...\n";
277 open(TEMPLATE, $_) or print_error "unable to read $_: $!";
278 my (@lines, $mode, @platforms, %targets);
280 # First pass: parse template variables and directives.
281 while (my $line = <TEMPLATE>) {
284 # Does this line match a variable?
285 while ($line =~ /(@(\w+?)@)/) {
286 my ($variable, $name) = ($1, $2);
287 if (defined $settings{$name}) {
288 $line =~ s/\Q$variable\E/$settings{$name}/;
290 print_warning "unknown template variable '$name' in $_!";
295 # Does this line match a directive?
296 if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
297 if ($1 eq 'define') {
299 push @lines, "#define $2";
301 push @lines, "#undef $2";
303 } elsif ($1 eq 'mode') {
305 } elsif ($1 eq 'platform') {
307 } elsif ($1 eq 'target') {
308 if ($2 =~ /(\w+)\s(.+)/) {
311 $targets{DEFAULT} = $2;
314 print_warning "unknown template command '$1' in $_!";
323 # Only proceed if this file should be templated on this platform.
324 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
326 # Add a default target if the template has not defined one.
327 unless (scalar keys %targets) {
328 $targets{DEFAULT} = basename $_;
331 # Second pass: parse makefile junk and write files.
332 while (my ($name, $target) = each %targets) {
334 # TODO: when buildtool is done this mess can be removed completely.
336 foreach my $line (@lines) {
338 # Are we parsing a makefile and does this line match a statement?
339 if ($name =~ /(?:BSD|GNU)_MAKE/ && $line =~ /^\s*\@(\w+)(?:\s+(.+))?$/) {
340 my @tokens = split /\s/, $2 if defined $2;
341 if ($1 eq 'DO_EXPORT' && defined $2) {
342 if ($name eq 'BSD_MAKE') {
343 foreach my $variable (@tokens) {
344 push @final_lines, "MAKEENV += $variable='\${$variable}'";
346 } elsif ($name eq 'GNU_MAKE') {
347 push @final_lines, "export $2";
349 } elsif ($1 eq 'ELSE') {
350 if ($name eq 'BSD_MAKE') {
351 push @final_lines, ".else";
352 } elsif ($name eq 'GNU_MAKE') {
353 push @final_lines, "else";
355 } elsif ($1 eq 'ENDIF') {
356 if ($name eq 'BSD_MAKE') {
357 push @final_lines, ".endif";
358 } elsif ($name eq 'GNU_MAKE') {
359 push @final_lines, "endif";
361 } elsif ($1 eq 'ELSIFEQ' && defined $2) {
362 if ($name eq 'BSD_MAKE') {
363 push @final_lines, ".elif $tokens[0] == $tokens[1]";
364 } elsif ($name eq 'GNU_MAKE') {
365 push @final_lines, "else ifeq ($tokens[0], $tokens[1])";
367 } elsif ($1 eq 'IFDEF' && defined $2) {
368 if ($name eq 'BSD_MAKE') {
369 push @final_lines, ".if defined($2)";
370 } elsif ($name eq 'GNU_MAKE') {
371 push @final_lines, "ifdef $2";
373 } elsif ($1 eq 'IFEQ' && defined $2) {
374 if ($name eq 'BSD_MAKE') {
375 push @final_lines, ".if $tokens[0] == $tokens[1]";
376 } elsif ($name eq 'GNU_MAKE') {
377 push @final_lines, "ifeq ($tokens[0],$tokens[1])";
379 } elsif ($1 eq 'IFNEQ' && defined $2) {
380 if ($name eq 'BSD_MAKE') {
381 push @final_lines, ".if $tokens[0] != $tokens[1]";
382 } elsif ($name eq 'GNU_MAKE') {
383 push @final_lines, "ifneq ($tokens[0],$tokens[1])";
385 } elsif ($1 eq 'IFNDEF' && defined $2) {
386 if ($name eq 'BSD_MAKE') {
387 push @final_lines, ".if !defined($2)";
388 } elsif ($name eq 'GNU_MAKE') {
389 push @final_lines, "ifndef $2";
391 } elsif ($1 eq 'TARGET' && defined $2) {
392 if ($tokens[0] eq $name) {
393 push @final_lines, substr($2, length($tokens[0]) + 1);
395 } elsif ($1 !~ /[A-Z]/) {
396 # HACK: silently ignore if lower case as these are probably make commands.
397 push @final_lines, $line;
399 print_warning "unknown template command '$1' in $_!";
400 push @final_lines, $line;
405 push @final_lines, $line;
408 # Write the template file.
409 print_format "Writing <|GREEN $target|> ...\n";
410 open(TARGET, '>', $target) or print_error "unable to write $_: $!";
411 foreach (@final_lines) {
412 print TARGET $_, "\n";
416 # Set file permissions.
418 chmod $mode, $target;