]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
Improve building the install paths in non-system mode.
[user/henk/code/inspircd.git] / configure
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
6 #   Copyright (C) 2019 Anatole Denis <natolumin@rezel.net>
7 #   Copyright (C) 2017 emerson <github@emersonveenstra.net>
8 #   Copyright (C) 2013-2020 Sadie Powell <sadie@witchery.services>
9 #   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10 #   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
11 #   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
12 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
13 #   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
14 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
15 #   Copyright (C) 2006-2008 Craig Edwards <brain@inspircd.org>
16 #
17 # This file is part of InspIRCd.  InspIRCd is free software: you can
18 # redistribute it and/or modify it under the terms of the GNU General Public
19 # License as published by the Free Software Foundation, version 2.
20 #
21 # This program is distributed in the hope that it will be useful, but WITHOUT
22 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
24 # details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 #
29
30
31 BEGIN {
32         require 5.10.0;
33 }
34
35 use feature ':5.10';
36 use strict;
37 use warnings FATAL => qw(all);
38
39 use File::Basename        qw(basename);
40 use File::Copy            ();
41 use File::Spec::Functions qw(catdir rel2abs);
42 use FindBin               qw($RealDir);
43 use Getopt::Long          qw(GetOptions);
44 use POSIX                 qw(getgid getuid);
45
46 use lib $RealDir;
47 use make::common;
48 use make::configure;
49 use make::console;
50 use make::directive;
51
52 my ($opt_binary_dir,
53     $opt_config_dir,
54     $opt_data_dir,
55     $opt_development,
56     $opt_disable_auto_extras,
57     $opt_disable_interactive,
58     $opt_distribution_label,
59     $opt_example_dir,
60     $opt_gid,
61     $opt_log_dir,
62     $opt_manual_dir,
63     $opt_module_dir,
64     $opt_prefix,
65     $opt_script_dir,
66     $opt_socketengine,
67     $opt_system,
68     $opt_uid);
69
70 sub list_extras ();
71
72 sub enable_extras (@);
73
74 sub disable_extras (@);
75
76 my @opt_enableextras;
77 my @opt_disableextras;
78
79 exit 1 unless GetOptions(
80         'clean'  => \&cmd_clean,
81         'help'   => \&cmd_help,
82         'update' => \&cmd_update,
83
84         'binary-dir=s'         => \$opt_binary_dir,
85         'config-dir=s'         => \$opt_config_dir,
86         'data-dir=s'           => \$opt_data_dir,
87         'development'          => \$opt_development,
88         'disable-auto-extras'  => \$opt_disable_auto_extras,
89         'disable-interactive'  => \$opt_disable_interactive,
90         'distribution-label=s' => \$opt_distribution_label,
91         'example-dir=s'        => \$opt_example_dir,
92         'gid=s'                => \$opt_gid,
93         'log-dir=s'            => \$opt_log_dir,
94         'manual-dir=s'         => \$opt_manual_dir,
95         'module-dir=s'         => \$opt_module_dir,
96         'prefix=s'             => \$opt_prefix,
97         'script-dir=s'         => \$opt_script_dir,
98         'socketengine=s'       => \$opt_socketengine,
99         'system'               => \$opt_system,
100         'uid=s'                => \$opt_uid,
101
102         # TODO: when the modulemanager rewrite is done these should be removed.
103         'disable-extras=s@' => \@opt_disableextras,
104         'enable-extras=s@'  => \@opt_enableextras,
105         'list-extras'       => sub { list_extras; exit 0; },
106 );
107
108 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
109         @opt_enableextras = split /[, ]+/, join(',', @opt_enableextras);
110         @opt_disableextras = split /[, ]+/, join(',', @opt_disableextras);
111         enable_extras(@opt_enableextras);
112         disable_extras(@opt_disableextras);
113         list_extras;
114         print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
115         exit 0;
116 }
117
118 our $interactive = !(
119         !-t STDIN ||
120         !-t STDOUT ||
121         defined $opt_binary_dir ||
122         defined $opt_config_dir ||
123         defined $opt_data_dir ||
124         defined $opt_development ||
125         defined $opt_disable_auto_extras ||
126         defined $opt_disable_interactive ||
127         defined $opt_distribution_label ||
128         defined $opt_example_dir ||
129         defined $opt_gid ||
130         defined $opt_log_dir ||
131         defined $opt_manual_dir ||
132         defined $opt_module_dir ||
133         defined $opt_prefix ||
134         defined $opt_script_dir ||
135         defined $opt_socketengine ||
136         defined $opt_system ||
137         defined $opt_uid
138 );
139
140 my %version = get_version $opt_distribution_label;
141 print_format "<|BOLD Configuring InspIRCd $version{FULL} on $^O.|>\n";
142
143 my %config;
144 if ($interactive) {
145         %config = read_config_file(CONFIGURE_CACHE_FILE);
146         run_test CONFIGURE_CACHE_FILE, %config;
147         if (!defined $config{VERSION}) {
148                 $config{VERSION} = CONFIGURE_CACHE_VERSION;
149         } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
150                 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
151                 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
152         }
153 }
154
155 $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
156 unless ($config{CXX}) {
157         say 'A suitable C++ compiler could not be detected on your system!';
158         unless ($interactive) {
159                 say 'Set the CXX environment variable to the path to a C++ compiler binary if this is incorrect.';
160                 exit 1;
161         }
162         until ($config{CXX}) {
163                 my $compiler_path = prompt_string 1, 'Please enter the path to a C++ compiler binary:', 'c++';
164                 $config{CXX} = find_compiler $compiler_path;
165         }
166 }
167 my %compiler = get_compiler_info($config{CXX});
168
169 $config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
170 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
171 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
172
173 my @socketengines;
174 push @socketengines, 'epoll'  if run_test 'epoll', test_header $config{CXX}, 'sys/epoll.h';
175 push @socketengines, 'kqueue' if run_test 'kqueue', test_file $config{CXX}, 'kqueue.cpp';
176 push @socketengines, 'poll'   if run_test 'poll', test_header $config{CXX}, 'poll.h';
177 push @socketengines, 'select';
178
179 if (defined $opt_socketengine) {
180         unless (grep { $_ eq $opt_socketengine } @socketengines) {
181                 my $reason = -f "src/socketengines/socketengine_$opt_socketengine.cpp" ? 'is not available on this platform' : 'does not exist';
182                 print_error "The socket engine you requested ($opt_socketengine) $reason!",
183                         'Available socket engines are:',
184                         map { "  * $_" } @socketengines;
185         }
186 }
187 $config{SOCKETENGINE} = $opt_socketengine // $socketengines[0];
188
189 if (defined $opt_system) {
190         $config{BASE_DIR}    = $opt_prefix      // '/var/lib/inspircd';
191         $config{BINARY_DIR}  = $opt_binary_dir  // '/usr/sbin';
192         $config{CONFIG_DIR}  = $opt_config_dir  // '/etc/inspircd';
193         $config{DATA_DIR}    = $opt_data_dir    // '/var/inspircd';
194         $config{EXAMPLE_DIR} = $opt_example_dir // '/usr/share/doc/inspircd';
195         $config{LOG_DIR}     = $opt_log_dir     // '/var/log/inspircd';
196         $config{MANUAL_DIR}  = $opt_manual_dir  // '/usr/share/man/man1';
197         $config{MODULE_DIR}  = $opt_module_dir  // '/usr/lib/inspircd';
198         $config{SCRIPT_DIR}  = $opt_script_dir  // '/usr/share/inspircd'
199 } else {
200         $config{BASE_DIR}    = rel2abs $opt_prefix // $config{BASE_DIR}    // catdir $RealDir,            'run';
201         $config{BINARY_DIR}  = $opt_binary_dir     // $config{BINARY_DIR}  // catdir $config{BASE_DIR},   'bin';
202         $config{CONFIG_DIR}  = $opt_config_dir     // $config{CONFIG_DIR}  // catdir $config{BASE_DIR},   'conf';
203         $config{DATA_DIR}    = $opt_data_dir       // $config{DATA_DIR}    // catdir $config{BASE_DIR},   'data';
204         $config{EXAMPLE_DIR} = $opt_example_dir    // $config{EXAMPLE_DIR} // catdir $config{CONFIG_DIR}, 'examples';
205         $config{LOG_DIR}     = $opt_log_dir        // $config{LOG_DIR}     // catdir $config{BASE_DIR},   'logs';
206         $config{MANUAL_DIR}  = $opt_manual_dir     // $config{MANUAL_DIR}  // catdir $config{BASE_DIR},   'manuals';
207         $config{MODULE_DIR}  = $opt_module_dir     // $config{MODULE_DIR}  // catdir $config{BASE_DIR},   'modules';
208         $config{SCRIPT_DIR}  = $opt_script_dir     // $config{SCRIPT_DIR}  // $config{BASE_DIR};
209 }
210
211 # Parse --gid=123 or --gid=foo and extract the group id.
212 my @group;
213 if (defined $opt_gid) {
214         @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
215         print_error "there is no '$opt_gid' group on this system!" unless @group;
216 } else {
217         @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
218         print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
219         unless ($group[2]) {
220                 print_warning <<"EOW";
221 You are building as the privileged $group[0] group and have not specified
222 an unprivileged group to run InspIRCd as.
223
224 This is almost never what you should do. You should probably either create a new
225 unprivileged user/group to build and run as or pass the '--gid [id|name]' flag
226 to specify an unprivileged group to run as.
227 EOW
228                 if (!prompt_bool $interactive, "Are you sure you want to build as the $group[0] group?", 0) {
229                         # PACKAGERS: You do not need to delete this check. Use `--gid $(id -g)` or `--gid 0` instead.
230                         say STDERR "If you are sure you want to build as the $group[0] group pass the --gid $group[2] flag." unless $interactive;
231                         exit 1;
232                 }
233         }
234 }
235 $config{GROUP} = $group[0];
236 $config{GID}   = $group[2];
237
238 # Parse --uid=123 or --uid=foo and extract the user id.
239 my @user;
240 if (defined $opt_uid) {
241         @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
242         print_error "there is no '$opt_uid' user on this system!" unless @user;
243 } else {
244         @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
245         print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
246         unless ($user[2]) {
247                 print_warning <<"EOW";
248 You are building as the privileged $user[0] user and have not specified
249 an unprivileged user to run InspIRCd as.
250
251 This is almost never what you should do. You should probably either create a new
252 unprivileged user/group to build and run as or pass the '--uid [id|name]' flag
253 to specify an unprivileged user to run as.
254 EOW
255                 if (!prompt_bool $interactive, "Are you sure you want to build as the $user[0] user?", 0) {
256                         # PACKAGERS: You do not need to delete this check. Use `--uid $(id -u)` or `--uid 0` instead.
257                         say STDERR "If you are sure you want to build as the $user[0] user pass the --uid $user[2] flag." unless $interactive;
258                         exit 1;
259                 }
260         }
261 }
262 $config{USER} = $user[0];
263 $config{UID}  = $user[2];
264
265 # Warn the user about clock drifting when running on OpenVZ.
266 if (-e '/proc/user_beancounters' || -e '/proc/vz/vzaquota') {
267         print_warning <<'EOW';
268 You are building InspIRCd inside of an OpenVZ container. If you
269 plan to use InspIRCd in this container then you should make sure that NTP is
270 configured on the Hardware Node. Failure to do so may result in clock drifting!
271 EOW
272 }
273
274 # Warn the user about OpenBSD shipping incredibly broken compilers/linkers.
275 if ($^O eq 'openbsd') {
276         print_warning <<'EOW';
277 You are building InspIRCd on OpenBSD. The C++ compilers and linkers
278 that OpenBSD ship are incredibly broken. You may have strange linker errors
279 and crashes. Please consider using a different OS like FreeBSD/NetBSD instead.
280 EOW
281 }
282
283 # Check that the user actually wants this version.
284 if (defined $version{REAL_LABEL}) {
285         print_warning <<'EOW';
286 You are building a development version. This contains code which has
287 not been tested as heavily and may contain various faults which could seriously
288 affect the running of your server. It is recommended that you use a stable
289 version instead.
290
291 You can obtain the latest stable version from https://www.inspircd.org or by
292 running `<|GREEN git checkout $(git describe --abbrev=0 --tags insp3)|>` if you are
293 installing from Git.
294 EOW
295         if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
296                 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
297                 exit 1;
298         }
299 }
300
301 # Configure directory settings.
302 my $question = <<"EOQ";
303 Currently, InspIRCd is configured with the following paths:
304
305 <|BOLD Base:|>   $config{BASE_DIR}
306 <|BOLD Binary:|> $config{BINARY_DIR}
307 <|BOLD Config:|> $config{CONFIG_DIR}
308 <|BOLD Data:|>   $config{DATA_DIR}
309 <|BOLD Log:|>    $config{LOG_DIR}
310 <|BOLD Manual:|> $config{MANUAL_DIR}
311 <|BOLD Module:|> $config{MODULE_DIR}
312 <|BOLD Script:|> $config{SCRIPT_DIR}
313
314 Do you want to change these settings?
315 EOQ
316 if (prompt_bool $interactive, $question, 0) {
317         my $original_base_dir = $config{BASE_DIR};
318         $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
319         foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR SCRIPT_DIR)) {
320                 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
321         }
322         $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
323         $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
324         $config{DATA_DIR}   = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
325         $config{LOG_DIR}    = prompt_dir $interactive, 'In what directory are log files to be stored?',           $config{LOG_DIR};
326         $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?',        $config{MANUAL_DIR};
327         $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?',             $config{MODULE_DIR};
328         $config{SCRIPT_DIR} = prompt_dir $interactive, 'In what directory are scripts to be placed?',             $config{SCRIPT_DIR};
329         $config{EXAMPLE_DIR} = $config{CONFIG_DIR} . '/examples';
330 }
331
332 # Configure module settings.
333 $question = <<'EOQ';
334 Currently, InspIRCd is configured to automatically enable all available extra modules.
335
336 Would you like to enable extra modules manually?
337 EOQ
338 if (prompt_bool $interactive, $question, 0) {
339         foreach my $extra (<src/modules/extra/m_*.cpp>) {
340                 my $module_name = basename $extra, '.cpp';
341                 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
342                         enable_extras "$module_name.cpp";
343                 }
344         }
345 } elsif (!defined $opt_disable_auto_extras) {
346         # TODO: finish modulemanager rewrite and replace this code with:
347         # system './modulemanager', 'enable', '--auto';
348         my %modules = (
349                 # Missing: m_ldap, m_regex_stdlib, m_ssl_mbedtls
350                 'm_geo_maxmind.cpp'     => 'pkg-config --exists libmaxminddb',
351                 'm_mysql.cpp'           => 'mysql_config --version',
352                 'm_pgsql.cpp'           => 'pg_config --version',
353                 'm_regex_pcre.cpp'      => 'pcre-config --version',
354                 'm_regex_posix.cpp'     => undef,
355                 'm_regex_re2.cpp'       => 'pkg-config --exists re2',
356                 'm_regex_tre.cpp'       => 'pkg-config --exists tre',
357                 'm_sqlite3.cpp'         => 'pkg-config --exists sqlite3',
358                 'm_ssl_gnutls.cpp'      => 'pkg-config --exists gnutls',
359                 'm_ssl_openssl.cpp'     => 'pkg-config --exists openssl',
360                 'm_sslrehashsignal.cpp' => undef,
361         );
362         while (my ($module, $command) = each %modules) {
363                 unless (defined $command && system "$command 1>/dev/null 2>/dev/null") {
364                         enable_extras $module;
365                 }
366         }
367 }
368
369 # Generate SSL certificates.
370 $question = <<EOQ;
371 Would you like to generate a self-signed SSL certificate now? This certificate
372 can be used for testing but <|BOLD should not|> be used on a production network.
373
374 Note: you can get a <|BOLD free|> CA-signed certificate from Let's Encrypt. See
375 https://letsencrypt.org/getting-started/ for more details.
376 EOQ
377
378 if (<src/modules/m_ssl_*.cpp>) {
379         if (prompt_bool $interactive, $question, $interactive) {
380                 system './tools/genssl', 'auto';
381         }
382 } else {
383         print_warning <<"EOM";
384 You are building without enabling any SSL modules. This is not
385 recommended as SSL greatly enhances the security and privacy of your IRC server
386 and in a future version will be <|BOLD required|> for linking servers.
387
388 Please read the following documentation pages on how to enable SSL support:
389
390 GnuTLS (recommended): https://docs.inspircd.org/3/modules/ssl_gnutls
391 mbedTLS:              https://docs.inspircd.org/3/modules/ssl_mbedtls
392 OpenSSL:              https://docs.inspircd.org/3/modules/ssl_openssl
393 EOM
394 }
395
396 # Cache the distribution label so that its not lost when --update is run.
397 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
398
399 write_configure_cache %config;
400 parse_templates \%config, \%compiler, \%version;
401
402 print_format <<"EOM";
403
404 Configuration is complete! You have chosen to build with the following settings:
405
406 <|GREEN Compiler:|>
407   <|GREEN Binary:|>  $config{CXX}
408   <|GREEN Name:|>    $compiler{NAME}
409   <|GREEN Version:|> $compiler{VERSION}
410
411 <|GREEN Extra Modules:|>
412 EOM
413
414 for my $file (<src/modules/m_*>) {
415         my $module = basename $file, '.cpp';
416         say "  * $module" if -l $file;
417 }
418
419 print_format <<"EOM";
420
421 <|GREEN Paths:|>
422   <|GREEN Base:|>    $config{BASE_DIR}
423   <|GREEN Binary:|>  $config{BINARY_DIR}
424   <|GREEN Config:|>  $config{CONFIG_DIR}
425   <|GREEN Data:|>    $config{DATA_DIR}
426   <|GREEN Example:|> $config{EXAMPLE_DIR}
427   <|GREEN Log:|>     $config{LOG_DIR}
428   <|GREEN Manual:|>  $config{MANUAL_DIR}
429   <|GREEN Module:|>  $config{MODULE_DIR}
430   <|GREEN Script:|>  $config{SCRIPT_DIR}
431
432 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
433 <|GREEN Execution User:|>  $config{USER} ($config{UID})
434 <|GREEN Socket Engine:|>   $config{SOCKETENGINE}
435
436 To build with these settings run '<|GREEN make -j${\(get_cpu_count() + 1)} install|>' now.
437
438 EOM
439
440 # Routine to list out the extra/ modules that have been enabled.
441 # Note: when getting any filenames out and comparing, it's important to lc it if the
442 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
443 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
444 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
445 sub list_extras () {
446         use File::Spec;
447         # @_ not used
448         my $srcdir = File::Spec->catdir("src", "modules");
449         my $abs_srcdir = File::Spec->rel2abs($srcdir);
450         local $_;
451         my $dd;
452         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
453         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
454         closedir $dd;
455         undef $dd;
456         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
457         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
458         closedir $dd;
459         undef $dd;
460         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
461         my %extras = ();
462 EXTRA:  for my $extra (@extras) {
463                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
464                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
465                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
466                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
467                 if (-l $abs_source) {
468                         # Symlink, is it in the right place?
469                         my $targ = readlink($abs_source);
470                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
471                         if ($abs_targ eq $abs_extra) {
472                                 $extras{$extra} = "\e[32;1menabled\e[0m";
473                         } else {
474                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
475                         }
476                 } elsif (-e $abs_source) {
477                         my ($devext, $inoext) = stat($abs_extra);
478                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
479                         if ($lnksrc > 1) {
480                                 if ($devsrc == $devext && $inosrc == $inoext) {
481                                         $extras{$extra} = "\e[32;1menabled\e[0m";
482                                 } else {
483                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
484                                 }
485                         } else {
486                                 open my $extfd, "<", $abs_extra;
487                                 open my $srcfd, "<", $abs_source;
488                                 local $/ = undef;
489                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
490                                         $extras{$extra} = "\e[32;1menabled\e[0m";
491                                 } else {
492                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
493                                 }
494                         }
495                 } else {
496                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
497                 }
498         }
499         # Now let's add dependency info
500         for my $extra (keys(%extras)) {
501                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
502                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
503                 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
504                 for my $dep (@deps) {
505                         if (exists($extras{$dep})) {
506                                 my $ref = \$extras{$dep}; # Take reference.
507                                 if ($$ref !~ m/needed by/) {
508                                         # First dependency found.
509                                         if ($$ref =~ m/enabled/) {
510                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
511                                         } else {
512                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
513                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
514                                         }
515                                 } else {
516                                         if ($$ref =~ m/enabled/) {
517                                                 $$ref .= ", \e[32;1m$extra\e[0m";
518                                         } else {
519                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
520                                         }
521                                 }
522                         }
523                 }
524         }
525         for my $extra (sort {$a cmp $b} keys(%extras)) {
526                 my $text = $extras{$extra};
527                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
528                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
529                 } else {
530                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
531                 }
532         }
533         return keys(%extras) if wantarray; # Can be used by manage_extras.
534 }
535
536 sub enable_extras (@) {
537         my (@extras) = @_;
538         for my $extra (@extras) {
539                 $extra = "m_$extra" unless $extra =~ /^m_/;
540                 $extra = "$extra.cpp" unless $extra =~ /\.cpp$/;
541                 my $extrapath = "src/modules/extra/$extra";
542                 if (!-e $extrapath) {
543                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
544                         next;
545                 }
546                 my $source = "src/modules/$extra";
547                 if (-e $source) {
548                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
549                         next;
550                 }
551                 # Get dependencies, and add them to be processed.
552                 my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
553                 for my $dep (@deps) {
554                         next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
555                         if (!-e "src/modules/$dep" && !-e "include/$dep") {
556                                 if (-e "src/modules/extra/$dep") {
557                                         print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
558                                         push @extras, $dep;
559                                 } else {
560                                         print STDERR "\e[33;1mWARNING:\e[0m module \e[32;1m$extra\e[0m might be missing dependency \e[32;1m$dep\e[0m - YOU are responsible for satisfying it!\n";
561                                 }
562                         }
563                 }
564                 print "Enabling $extra ... \n";
565                 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
566         }
567 }
568
569 sub disable_extras (@)
570 {
571         opendir my $dd, "src/modules/extra/";
572         my @files = readdir($dd);
573         closedir $dd;
574         my (@extras) = @_;
575 EXTRA:  for my $extra (@extras) {
576                 $extra = "m_$extra" unless $extra =~ /^m_/;
577                 $extra = "$extra.cpp" unless $extra =~ /\.cpp$/;
578                 my $extrapath = "src/modules/extra/$extra";
579                 my $source = "src/modules/$extra";
580                 if (!-e $extrapath) {
581                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
582                         next;
583                 }
584                 if ((! -l $source) || readlink($source) ne "extra/$extra") {
585                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Source is not a link or doesn't refer to the right file. Remove manually if this is in error.\n";
586                         next;
587                 }
588                 # Check if anything needs this.
589                 for my $file (@files) {
590                         my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
591                         # File depends on this extra...
592                         if (scalar(grep { $_ eq $extra } @deps) > 0) {
593                                 # And is both enabled and not about to be disabled.
594                                 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
595                                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
596                                         next EXTRA;
597                                 }
598                         }
599                 }
600                 # Now remove.
601                 print "Disabling $extra ... \n";
602                 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
603         }
604 }