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;
32 use warnings FATAL => qw(all);
35 use Exporter qw(import);
36 use File::Basename qw(basename);
42 use constant CONFIGURE_CACHE_FILE => '.configure.cache';
43 use constant CONFIGURE_CACHE_VERSION => '1';
45 our @EXPORT = qw(CONFIGURE_CACHE_FILE
46 CONFIGURE_CACHE_VERSION
60 sub __get_socketengines {
62 foreach (<src/socketengines/socketengine_*.cpp>) {
63 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
64 push @socketengines, $1;
66 return @socketengines;
69 # TODO: when buildtool is done this can be mostly removed with
70 # the remainder being merged into parse_templates.
71 sub __get_template_settings($$$) {
73 # These are actually hash references
74 my ($config, $compiler, $version) = @_;
76 # Start off by populating with the config
77 my %settings = %$config;
79 # Compiler information
80 while (my ($key, $value) = each %{$compiler}) {
81 $settings{'COMPILER_' . $key} = $value;
85 while (my ($key, $value) = each %{$version}) {
86 $settings{'VERSION_' . $key} = $value;
89 # Miscellaneous information
90 $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
91 $settings{SYSTEM_NAME} = lc $^O;
92 chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
97 sub __test_compiler($) {
99 return 0 unless run_test("`$compiler`", !system "$compiler -v >/dev/null 2>&1");
100 return 0 unless run_test("`$compiler`", test_file($compiler, 'compiler.cpp', '-fno-rtti'), 'compatible');
105 unlink CONFIGURE_CACHE_FILE;
110 my $SELIST = join ', ', __get_socketengines();
114 When no options are specified, configure runs in interactive mode and you must
115 specify any required values manually. If one or more options are specified,
116 non-interactive configuration is started and any omitted values are defaulted.
120 --system Automatically set up the installation paths
121 for system-wide installation.
122 --prefix=[dir] The root install directory. If this is set then
123 all subdirectories will be adjusted accordingly.
125 --binary-dir=[dir] The location where the main server binary is
128 --config-dir=[dir] The location where the configuration files and
129 SSL certificates are stored.
131 --data-dir=[dir] The location where the data files, such as the
132 pid file, are stored.
134 --log-dir=[dir] The location where the log files are stored.
136 --manual-dir=[dir] The location where the manual files are stored.
138 --module-dir=[dir] The location where the loadable modules are
144 --enable-extras=[extras] Enables a comma separated list of extra modules.
145 --disable-extras=[extras] Disables a comma separated list of extra modules.
146 --list-extras Shows the availability status of all extra
151 --clean Remove the configuration cache file and start
152 the interactive configuration wizard.
153 --disable-interactive Disables the interactive configuration wizard.
154 --distribution-label=[text] Sets a distribution specific version label in
155 the build configuration.
156 --gid=[id|name] Sets the group to run InspIRCd as.
157 --help Show this message and exit.
158 --socketengine=[name] Sets the socket engine to be used. Possible
160 --uid=[id|name] Sets the user to run InspIRCd as.
161 --update Updates the build environment.
166 CXX=[name] Sets the C++ compiler to use when building the
167 server. If not specified then the build system
168 will search for c++, g++, clang++ or icpc.
170 If you have any problems with configuring InspIRCd then visit our IRC channel
171 at irc.inspircd.org #InspIRCd for support.
178 print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
180 my %config = read_configure_cache();
181 my %compiler = get_compiler_info($config{CXX});
182 my %version = get_version();
183 parse_templates(\%config, \%compiler, \%version);
184 say 'Update complete!';
189 my ($what, $result, $adjective) = @_;
190 $adjective //= 'available';
191 print_format "Checking whether <|GREEN $what|> is $adjective ... ";
192 print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
196 sub test_file($$;$) {
197 my ($compiler, $file, $args) = @_;
200 $status ||= system "$compiler -o __test_$file make/test/$file $args >/dev/null 2>&1";
201 $status ||= system "./__test_$file >/dev/null 2>&1";
202 unlink "./__test_$file";
206 sub test_header($$;$) {
207 my ($compiler, $header, $args) = @_;
209 open(COMPILER, "| $compiler -E - $args >/dev/null 2>&1") or return 0;
210 print COMPILER "#include <$header>";
215 sub read_configure_cache {
217 open(CACHE, CONFIGURE_CACHE_FILE) or return %config;
218 while (my $line = <CACHE>) {
219 next if $line =~ /^\s*($|\#)/;
220 my ($key, $value) = ($line =~ /^(\S+)="(.*)"$/);
221 $config{$key} = $value;
227 sub write_configure_cache(%) {
228 print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
230 open(CACHE, '>', CONFIGURE_CACHE_FILE) or print_error "unable to write ${\CONFIGURE_CACHE_FILE}: $!";
231 while (my ($key, $value) = each %config) {
233 say CACHE "$key=\"$value\"";
238 sub get_compiler_info($) {
240 my $version = `$binary -v 2>&1`;
241 if ($version =~ /Apple\sLLVM\sversion\s(\d+\.\d+)/i) {
242 # Apple version their LLVM releases slightly differently to the mainline LLVM.
243 # See https://trac.macports.org/wiki/XcodeVersionInfo for more information.
244 return (NAME => 'AppleClang', VERSION => $1);
245 } elsif ($version =~ /clang\sversion\s(\d+\.\d+)/i) {
246 return (NAME => 'Clang', VERSION => $1);
247 } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
248 return (NAME => 'GCC', VERSION => $1);
249 } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
250 return (NAME => 'ICC', VERSION => $1);
252 return (NAME => $binary, VERSION => '0.0');
256 my @compilers = qw(c++ g++ clang++ icpc);
257 foreach my $compiler (shift // @compilers) {
258 return $compiler if __test_compiler $compiler;
259 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
263 sub get_property($$;$)
265 my ($file, $property, $default) = @_;
266 open(MODULE, $file) or return $default;
268 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/) {
269 next unless $1 eq $property;
271 return translate_functions($2, $file);
275 return $default // '';
278 sub parse_templates($$$) {
280 # These are actually hash references
281 my ($config, $compiler, $version) = @_;
283 # Collect settings to be used when generating files
284 my %settings = __get_template_settings($config, $compiler, $version);
286 # Iterate through files in make/template.
287 foreach (<make/template/*>) {
288 print_format "Parsing <|GREEN $_|> ...\n";
289 open(TEMPLATE, $_) or print_error "unable to read $_: $!";
290 my (@lines, $mode, @platforms, %targets);
292 # First pass: parse template variables and directives.
293 while (my $line = <TEMPLATE>) {
296 # Does this line match a variable?
297 while ($line =~ /(@(\w+?)@)/) {
298 my ($variable, $name) = ($1, $2);
299 if (defined $settings{$name}) {
300 $line =~ s/\Q$variable\E/$settings{$name}/;
302 print_warning "unknown template variable '$name' in $_!";
307 # Does this line match a directive?
308 if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
309 if ($1 eq 'define') {
311 push @lines, "#define $2";
313 push @lines, "#undef $2";
315 } elsif ($1 eq 'mode') {
317 } elsif ($1 eq 'platform') {
319 } elsif ($1 eq 'target') {
320 if ($2 =~ /(\w+)\s(.+)/) {
323 $targets{DEFAULT} = $2;
326 print_warning "unknown template command '$1' in $_!";
335 # Only proceed if this file should be templated on this platform.
336 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
338 # Add a default target if the template has not defined one.
339 unless (scalar keys %targets) {
340 $targets{DEFAULT} = basename $_;
343 # Second pass: parse makefile junk and write files.
344 while (my ($name, $target) = each %targets) {
346 # TODO: when buildtool is done this mess can be removed completely.
348 foreach my $line (@lines) {
350 # Are we parsing a makefile and does this line match a statement?
351 if ($name =~ /(?:BSD|GNU)_MAKE/ && $line =~ /^\s*\@(\w+)(?:\s+(.+))?$/) {
352 my @tokens = split /\s/, $2 if defined $2;
353 if ($1 eq 'DO_EXPORT' && defined $2) {
354 if ($name eq 'BSD_MAKE') {
355 foreach my $variable (@tokens) {
356 push @final_lines, "MAKEENV += $variable='\${$variable}'";
358 } elsif ($name eq 'GNU_MAKE') {
359 push @final_lines, "export $2";
361 } elsif ($1 eq 'ELSE') {
362 if ($name eq 'BSD_MAKE') {
363 push @final_lines, ".else";
364 } elsif ($name eq 'GNU_MAKE') {
365 push @final_lines, "else";
367 } elsif ($1 eq 'ENDIF') {
368 if ($name eq 'BSD_MAKE') {
369 push @final_lines, ".endif";
370 } elsif ($name eq 'GNU_MAKE') {
371 push @final_lines, "endif";
373 } elsif ($1 eq 'ELSIFEQ' && defined $2) {
374 if ($name eq 'BSD_MAKE') {
375 push @final_lines, ".elif $tokens[0] == $tokens[1]";
376 } elsif ($name eq 'GNU_MAKE') {
377 push @final_lines, "else ifeq ($tokens[0], $tokens[1])";
379 } elsif ($1 eq 'IFDEF' && defined $2) {
380 if ($name eq 'BSD_MAKE') {
381 push @final_lines, ".if defined($2)";
382 } elsif ($name eq 'GNU_MAKE') {
383 push @final_lines, "ifdef $2";
385 } elsif ($1 eq 'IFEQ' && defined $2) {
386 if ($name eq 'BSD_MAKE') {
387 push @final_lines, ".if $tokens[0] == $tokens[1]";
388 } elsif ($name eq 'GNU_MAKE') {
389 push @final_lines, "ifeq ($tokens[0],$tokens[1])";
391 } elsif ($1 eq 'IFNEQ' && defined $2) {
392 if ($name eq 'BSD_MAKE') {
393 push @final_lines, ".if $tokens[0] != $tokens[1]";
394 } elsif ($name eq 'GNU_MAKE') {
395 push @final_lines, "ifneq ($tokens[0],$tokens[1])";
397 } elsif ($1 eq 'IFNDEF' && defined $2) {
398 if ($name eq 'BSD_MAKE') {
399 push @final_lines, ".if !defined($2)";
400 } elsif ($name eq 'GNU_MAKE') {
401 push @final_lines, "ifndef $2";
403 } elsif ($1 eq 'TARGET' && defined $2) {
404 if ($tokens[0] eq $name) {
405 push @final_lines, substr($2, length($tokens[0]) + 1);
407 } elsif ($1 !~ /[A-Z]/) {
408 # HACK: silently ignore if lower case as these are probably make commands.
409 push @final_lines, $line;
411 print_warning "unknown template command '$1' in $_!";
412 push @final_lines, $line;
417 push @final_lines, $line;
420 # Write the template file.
421 print_format "Writing <|GREEN $target|> ...\n";
422 open(TARGET, '>', $target) or print_error "unable to write $_: $!";
423 foreach (@final_lines) {
428 # Set file permissions.
430 chmod $mode, $target;