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 dirname);
37 use File::Spec::Functions qw(catfile);
43 use constant CONFIGURE_DIRECTORY => '.configure';
44 use constant CONFIGURE_CACHE_FILE => catfile(CONFIGURE_DIRECTORY, 'cache.cfg');
45 use constant CONFIGURE_CACHE_VERSION => '1';
47 our @EXPORT = qw(CONFIGURE_CACHE_FILE
48 CONFIGURE_CACHE_VERSION
62 sub __get_socketengines {
64 foreach (<src/socketengines/socketengine_*.cpp>) {
65 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
66 push @socketengines, $1;
68 return @socketengines;
71 # TODO: when buildtool is done this can be mostly removed with
72 # the remainder being merged into parse_templates.
73 sub __get_template_settings($$$) {
75 # These are actually hash references
76 my ($config, $compiler, $version) = @_;
78 # Start off by populating with the config
79 my %settings = %$config;
81 # Compiler information
82 while (my ($key, $value) = each %{$compiler}) {
83 $settings{'COMPILER_' . $key} = $value;
87 while (my ($key, $value) = each %{$version}) {
88 $settings{'VERSION_' . $key} = $value;
91 # Miscellaneous information
92 $settings{CONFIGURE_DIRECTORY} = CONFIGURE_DIRECTORY;
93 $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
94 $settings{SYSTEM_NAME} = lc $^O;
95 chomp($settings{SYSTEM_NAME_VERSION} = `uname -sr 2>/dev/null`);
100 sub __test_compiler($) {
101 my $compiler = shift;
102 return 0 unless run_test("`$compiler`", !system "$compiler -v >/dev/null 2>&1");
103 return 0 unless run_test("`$compiler`", test_file($compiler, 'compiler.cpp', '-fno-rtti'), 'compatible');
108 unlink CONFIGURE_CACHE_FILE;
113 my $SELIST = join ', ', __get_socketengines();
117 When no options are specified, configure runs in interactive mode and you must
118 specify any required values manually. If one or more options are specified,
119 non-interactive configuration is started and any omitted values are defaulted.
123 --system Automatically set up the installation paths
124 for system-wide installation.
125 --prefix=[dir] The root install directory. If this is set then
126 all subdirectories will be adjusted accordingly.
128 --binary-dir=[dir] The location where the main server binary is
131 --config-dir=[dir] The location where the configuration files and
132 SSL certificates are stored.
134 --data-dir=[dir] The location where the data files, such as the
135 pid file, are stored.
137 --log-dir=[dir] The location where the log files are stored.
139 --manual-dir=[dir] The location where the manual files are stored.
141 --module-dir=[dir] The location where the loadable modules are
147 --enable-extras=[extras] Enables a comma separated list of extra modules.
148 --disable-extras=[extras] Disables a comma separated list of extra modules.
149 --list-extras Shows the availability status of all extra
154 --clean Remove the configuration cache file and start
155 the interactive configuration wizard.
156 --disable-interactive Disables the interactive configuration wizard.
157 --distribution-label=[text] Sets a distribution specific version label in
158 the build configuration.
159 --gid=[id|name] Sets the group to run InspIRCd as.
160 --help Show this message and exit.
161 --socketengine=[name] Sets the socket engine to be used. Possible
163 --uid=[id|name] Sets the user to run InspIRCd as.
164 --update Updates the build environment.
169 CXX=[name] Sets the C++ compiler to use when building the
170 server. If not specified then the build system
171 will search for c++, g++, clang++ or icpc.
173 If you have any problems with configuring InspIRCd then visit our IRC channel
174 at irc.inspircd.org #InspIRCd for support.
181 print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
183 my %config = read_configure_cache();
184 my %compiler = get_compiler_info($config{CXX});
185 my %version = get_version();
186 parse_templates(\%config, \%compiler, \%version);
187 say 'Update complete!';
192 my ($what, $result, $adjective) = @_;
193 $adjective //= 'available';
194 print_format "Checking whether <|GREEN $what|> is $adjective ... ";
195 print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
199 sub test_file($$;$) {
200 my ($compiler, $file, $args) = @_;
203 $status ||= system "$compiler -o __test_$file make/test/$file $args >/dev/null 2>&1";
204 $status ||= system "./__test_$file >/dev/null 2>&1";
205 unlink "./__test_$file";
209 sub test_header($$;$) {
210 my ($compiler, $header, $args) = @_;
212 open(COMPILER, "| $compiler -E - $args >/dev/null 2>&1") or return 0;
213 print COMPILER "#include <$header>";
218 sub read_configure_cache {
220 open(CACHE, CONFIGURE_CACHE_FILE) or return %config;
221 while (my $line = <CACHE>) {
222 next if $line =~ /^\s*($|\#)/;
223 my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/);
224 $config{$key} = $value;
230 sub write_configure_cache(%) {
231 unless (-e CONFIGURE_DIRECTORY) {
232 print_format "Creating <|GREEN ${\CONFIGURE_DIRECTORY}|> ...\n";
233 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
236 print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
238 open(CACHE, '>', CONFIGURE_CACHE_FILE) or print_error "unable to write ${\CONFIGURE_CACHE_FILE}: $!";
239 while (my ($key, $value) = each %config) {
241 say CACHE "$key $value";
246 sub get_compiler_info($) {
248 my $version = `$binary -v 2>&1`;
249 if ($version =~ /Apple\sLLVM\sversion\s(\d+\.\d+)/i) {
250 # Apple version their LLVM releases slightly differently to the mainline LLVM.
251 # See https://trac.macports.org/wiki/XcodeVersionInfo for more information.
252 return (NAME => 'AppleClang', VERSION => $1);
253 } elsif ($version =~ /clang\sversion\s(\d+\.\d+)/i) {
254 return (NAME => 'Clang', VERSION => $1);
255 } elsif ($version =~ /gcc\sversion\s(\d+\.\d+)/i) {
256 return (NAME => 'GCC', VERSION => $1);
257 } elsif ($version =~ /(?:icc|icpc)\sversion\s(\d+\.\d+).\d+\s\(gcc\sversion\s(\d+\.\d+).\d+/i) {
258 return (NAME => 'ICC', VERSION => $1);
260 return (NAME => $binary, VERSION => '0.0');
264 my @compilers = qw(c++ g++ clang++ icpc);
265 foreach my $compiler (shift // @compilers) {
266 return $compiler if __test_compiler $compiler;
267 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
271 sub get_property($$;$)
273 my ($file, $property, $default) = @_;
274 open(MODULE, $file) or return $default;
276 if ($_ =~ /^\/\* \$(\S+): (.+) \*\/$/) {
277 next unless $1 eq $property;
279 return translate_functions($2, $file);
283 return $default // '';
286 sub parse_templates($$$) {
288 # These are actually hash references
289 my ($config, $compiler, $version) = @_;
291 # Collect settings to be used when generating files
292 my %settings = __get_template_settings($config, $compiler, $version);
294 # Iterate through files in make/template.
295 foreach (<make/template/*>) {
296 print_format "Parsing <|GREEN $_|> ...\n";
297 open(TEMPLATE, $_) or print_error "unable to read $_: $!";
298 my (@lines, $mode, @platforms, %targets);
300 # First pass: parse template variables and directives.
301 while (my $line = <TEMPLATE>) {
304 # Does this line match a variable?
305 while ($line =~ /(@(\w+?)@)/) {
306 my ($variable, $name) = ($1, $2);
307 if (defined $settings{$name}) {
308 $line =~ s/\Q$variable\E/$settings{$name}/;
310 print_warning "unknown template variable '$name' in $_!";
315 # Does this line match a directive?
316 if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
317 if ($1 eq 'define') {
319 push @lines, "#define $2";
321 push @lines, "#undef $2";
323 } elsif ($1 eq 'mode') {
325 } elsif ($1 eq 'platform') {
327 } elsif ($1 eq 'target') {
328 if ($2 =~ /(\w+)\s(.+)/) {
331 $targets{DEFAULT} = $2;
334 print_warning "unknown template command '$1' in $_!";
343 # Only proceed if this file should be templated on this platform.
344 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
346 # Add a default target if the template has not defined one.
347 unless (scalar keys %targets) {
348 $targets{DEFAULT} = catfile(CONFIGURE_DIRECTORY, basename $_);
351 # Second pass: parse makefile junk and write files.
352 while (my ($name, $target) = each %targets) {
354 # TODO: when buildtool is done this mess can be removed completely.
356 foreach my $line (@lines) {
358 # Are we parsing a makefile and does this line match a statement?
359 if ($name =~ /(?:BSD|GNU)_MAKE/ && $line =~ /^\s*\@(\w+)(?:\s+(.+))?$/) {
360 my @tokens = split /\s/, $2 if defined $2;
361 if ($1 eq 'DO_EXPORT' && defined $2) {
362 if ($name eq 'BSD_MAKE') {
363 foreach my $variable (@tokens) {
364 push @final_lines, "MAKEENV += $variable='\${$variable}'";
366 } elsif ($name eq 'GNU_MAKE') {
367 push @final_lines, "export $2";
369 } elsif ($1 eq 'ELSE') {
370 if ($name eq 'BSD_MAKE') {
371 push @final_lines, ".else";
372 } elsif ($name eq 'GNU_MAKE') {
373 push @final_lines, "else";
375 } elsif ($1 eq 'ENDIF') {
376 if ($name eq 'BSD_MAKE') {
377 push @final_lines, ".endif";
378 } elsif ($name eq 'GNU_MAKE') {
379 push @final_lines, "endif";
381 } elsif ($1 eq 'ELSIFEQ' && defined $2) {
382 if ($name eq 'BSD_MAKE') {
383 push @final_lines, ".elif $tokens[0] == $tokens[1]";
384 } elsif ($name eq 'GNU_MAKE') {
385 push @final_lines, "else ifeq ($tokens[0], $tokens[1])";
387 } elsif ($1 eq 'IFDEF' && 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, "ifdef $2";
393 } elsif ($1 eq 'IFEQ' && defined $2) {
394 if ($name eq 'BSD_MAKE') {
395 push @final_lines, ".if $tokens[0] == $tokens[1]";
396 } elsif ($name eq 'GNU_MAKE') {
397 push @final_lines, "ifeq ($tokens[0],$tokens[1])";
399 } elsif ($1 eq 'IFNEQ' && defined $2) {
400 if ($name eq 'BSD_MAKE') {
401 push @final_lines, ".if $tokens[0] != $tokens[1]";
402 } elsif ($name eq 'GNU_MAKE') {
403 push @final_lines, "ifneq ($tokens[0],$tokens[1])";
405 } elsif ($1 eq 'IFNDEF' && defined $2) {
406 if ($name eq 'BSD_MAKE') {
407 push @final_lines, ".if !defined($2)";
408 } elsif ($name eq 'GNU_MAKE') {
409 push @final_lines, "ifndef $2";
411 } elsif ($1 eq 'TARGET' && defined $2) {
412 if ($tokens[0] eq $name) {
413 push @final_lines, substr($2, length($tokens[0]) + 1);
415 } elsif ($1 !~ /[A-Z]/) {
416 # HACK: silently ignore if lower case as these are probably make commands.
417 push @final_lines, $line;
419 print_warning "unknown template command '$1' in $_!";
420 push @final_lines, $line;
425 push @final_lines, $line;
428 # Create the directory if it doesn't already exist.
429 my $directory = dirname $target;
430 unless (-e $directory) {
431 print_format "Creating <|GREEN $directory|> ...\n";
432 create_directory $directory, 0750 or print_error "unable to create $directory: $!";
435 # Write the template file.
436 print_format "Writing <|GREEN $target|> ...\n";
437 open(TARGET, '>', $target) or print_error "unable to write $target: $!";
438 foreach (@final_lines) {
443 # Set file permissions.
445 chmod $mode, $target;