]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
Improve manually enabling extra modules.
[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-2019 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(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}    = $opt_prefix      // $config{BASE_DIR}    // rel2abs 'run';
201         $config{BINARY_DIR}  = $opt_binary_dir  // $config{BINARY_DIR}  // rel2abs $config{BASE_DIR} . '/bin';
202         $config{CONFIG_DIR}  = $opt_config_dir  // $config{CONFIG_DIR}  // rel2abs $config{BASE_DIR} . '/conf';
203         $config{DATA_DIR}    = $opt_data_dir    // $config{DATA_DIR}    // rel2abs $config{BASE_DIR} . '/data';
204         $config{EXAMPLE_DIR} = $opt_example_dir // $config{EXAMPLE_DIR} // $config{CONFIG_DIR} . '/examples';
205         $config{LOG_DIR}     = $opt_log_dir     // $config{LOG_DIR}     // rel2abs $config{BASE_DIR} . '/logs';
206         $config{MANUAL_DIR}  = $opt_manual_dir  // $config{MANUAL_DIR}  // rel2abs $config{BASE_DIR} . '/manuals';
207         $config{MODULE_DIR}  = $opt_module_dir  // $config{MODULE_DIR}  // rel2abs $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                         say STDERR "If you are sure you want to build as the $group[0] group pass the --gid $group[2] flag." unless $interactive;
230                         exit 1;
231                 }
232         }
233 }
234 $config{GROUP} = $group[0];
235 $config{GID}   = $group[2];
236
237 # Parse --uid=123 or --uid=foo and extract the user id.
238 my @user;
239 if (defined $opt_uid) {
240         @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
241         print_error "there is no '$opt_uid' user on this system!" unless @user;
242 } else {
243         @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
244         print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
245         unless ($user[2]) {
246                 print_warning <<"EOW";
247 You are building as the privileged $user[0] user and have not specified
248 an unprivileged user to run InspIRCd as.
249
250 This is almost never what you should do. You should probably either create a new
251 unprivileged user/group to build and run as or pass the '--uid [id|name]' flag
252 to specify an unprivileged user to run as.
253 EOW
254                 if (!prompt_bool $interactive, "Are you sure you want to build as the $user[0] user?", 0) {
255                         say STDERR "If you are sure you want to build as the $user[0] user pass the --uid $user[2] flag." unless $interactive;
256                         exit 1;
257                 }
258         }
259 }
260 $config{USER} = $user[0];
261 $config{UID}  = $user[2];
262
263 # Warn the user about clock drifting when running on OpenVZ.
264 if (-e '/proc/user_beancounters' || -e '/proc/vz/vzaquota') {
265         print_warning <<'EOW';
266 You are building InspIRCd inside of an OpenVZ container. If you
267 plan to use InspIRCd in this container then you should make sure that NTP is
268 configured on the Hardware Node. Failure to do so may result in clock drifting!
269 EOW
270 }
271
272 # Warn the user about OpenBSD shipping incredibly broken compilers/linkers.
273 if ($^O eq 'openbsd') {
274         print_warning <<'EOW';
275 You are building InspIRCd on OpenBSD. The C++ compilers and linkers
276 that OpenBSD ship are incredibly broken. You may have strange linker errors
277 and crashes. Please consider using a different OS like FreeBSD/NetBSD instead.
278 EOW
279 }
280
281 # Check that the user actually wants this version.
282 if (defined $version{REAL_LABEL}) {
283         print_warning <<'EOW';
284 You are building a development version. This contains code which has
285 not been tested as heavily and may contain various faults which could seriously
286 affect the running of your server. It is recommended that you use a stable
287 version instead.
288
289 You can obtain the latest stable version from https://www.inspircd.org or by
290 running `<|GREEN git checkout $(git describe --abbrev=0 --tags insp3)|>` if you are
291 installing from Git.
292 EOW
293         if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
294                 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
295                 exit 1;
296         }
297 }
298
299 # Configure directory settings.
300 my $question = <<"EOQ";
301 Currently, InspIRCd is configured with the following paths:
302
303 <|BOLD Base:|>   $config{BASE_DIR}
304 <|BOLD Binary:|> $config{BINARY_DIR}
305 <|BOLD Config:|> $config{CONFIG_DIR}
306 <|BOLD Data:|>   $config{DATA_DIR}
307 <|BOLD Log:|>    $config{LOG_DIR}
308 <|BOLD Manual:|> $config{MANUAL_DIR}
309 <|BOLD Module:|> $config{MODULE_DIR}
310 <|BOLD Script:|> $config{SCRIPT_DIR}
311
312 Do you want to change these settings?
313 EOQ
314 if (prompt_bool $interactive, $question, 0) {
315         my $original_base_dir = $config{BASE_DIR};
316         $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
317         foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR SCRIPT_DIR)) {
318                 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
319         }
320         $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
321         $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
322         $config{DATA_DIR}   = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
323         $config{LOG_DIR}    = prompt_dir $interactive, 'In what directory are log files to be stored?',           $config{LOG_DIR};
324         $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?',        $config{MANUAL_DIR};
325         $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?',             $config{MODULE_DIR};
326         $config{SCRIPT_DIR} = prompt_dir $interactive, 'In what directory are scripts to be placed?',             $config{SCRIPT_DIR};
327         $config{EXAMPLE_DIR} = $config{CONFIG_DIR} . '/examples';
328 }
329
330 # Configure module settings.
331 $question = <<'EOQ';
332 Currently, InspIRCd is configured to automatically enable all available extra modules.
333
334 Would you like to enable extra modules manually?
335 EOQ
336 if (prompt_bool $interactive, $question, 0) {
337         foreach my $extra (<src/modules/extra/m_*.cpp>) {
338                 my $module_name = basename $extra, '.cpp';
339                 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
340                         enable_extras "$module_name.cpp";
341                 }
342         }
343 } elsif (!defined $opt_disable_auto_extras) {
344         # TODO: finish modulemanager rewrite and replace this code with:
345         # system './modulemanager', 'enable', '--auto';
346         my %modules = (
347                 # Missing: m_ldap, m_regex_stdlib, m_ssl_mbedtls
348                 'm_geo_maxmind.cpp'     => 'pkg-config --exists libmaxminddb',
349                 'm_mysql.cpp'           => 'mysql_config --version',
350                 'm_pgsql.cpp'           => 'pg_config --version',
351                 'm_regex_pcre.cpp'      => 'pcre-config --version',
352                 'm_regex_posix.cpp'     => undef,
353                 'm_regex_re2.cpp'       => 'pkg-config --exists re2',
354                 'm_regex_tre.cpp'       => 'pkg-config --exists tre',
355                 'm_sqlite3.cpp'         => 'pkg-config --exists sqlite3',
356                 'm_ssl_gnutls.cpp'      => 'pkg-config --exists gnutls',
357                 'm_ssl_openssl.cpp'     => 'pkg-config --exists openssl',
358                 'm_sslrehashsignal.cpp' => undef,
359         );
360         while (my ($module, $command) = each %modules) {
361                 unless (defined $command && system "$command 1>/dev/null 2>/dev/null") {
362                         enable_extras $module;
363                 }
364         }
365 }
366
367 # Generate SSL certificates.
368 $question = <<EOQ;
369 Would you like to generate a self-signed SSL certificate now? This certificate
370 can be used for testing but <|BOLD should not|> be used on a production network.
371
372 Note: you can get a <|BOLD free|> CA-signed certificate from Let's Encrypt. See
373 https://letsencrypt.org/getting-started/ for more details.
374 EOQ
375
376 if (<src/modules/m_ssl_*.cpp>) {
377         if (prompt_bool $interactive, $question, $interactive) {
378                 system './tools/genssl', 'auto';
379         }
380 } else {
381         print_warning <<"EOM";
382 You are building without enabling any SSL modules. This is not
383 recommended as SSL greatly enhances the security and privacy of your IRC server
384 and in a future version will be <|BOLD required|> for linking servers.
385
386 Please read the following documentation pages on how to enable SSL support:
387
388 GnuTLS (recommended): https://docs.inspircd.org/3/modules/ssl_gnutls
389 mbedTLS:              https://docs.inspircd.org/3/modules/ssl_mbedtls
390 OpenSSL:              https://docs.inspircd.org/3/modules/ssl_openssl
391 EOM
392 }
393
394 # Cache the distribution label so that its not lost when --update is run.
395 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
396
397 write_configure_cache %config;
398 parse_templates \%config, \%compiler, \%version;
399
400 print_format <<"EOM";
401
402 Configuration is complete! You have chosen to build with the following settings:
403
404 <|GREEN Compiler:|>
405   <|GREEN Binary:|>  $config{CXX}
406   <|GREEN Name:|>    $compiler{NAME}
407   <|GREEN Version:|> $compiler{VERSION}
408
409 <|GREEN Extra Modules:|>
410 EOM
411
412 for my $file (<src/modules/m_*>) {
413         my $module = basename $file, '.cpp';
414         say "  * $module" if -l $file;
415 }
416
417 print_format <<"EOM";
418
419 <|GREEN Paths:|>
420   <|GREEN Base:|>    $config{BASE_DIR}
421   <|GREEN Binary:|>  $config{BINARY_DIR}
422   <|GREEN Config:|>  $config{CONFIG_DIR}
423   <|GREEN Data:|>    $config{DATA_DIR}
424   <|GREEN Example:|> $config{EXAMPLE_DIR}
425   <|GREEN Log:|>     $config{LOG_DIR}
426   <|GREEN Manual:|>  $config{MANUAL_DIR}
427   <|GREEN Module:|>  $config{MODULE_DIR}
428   <|GREEN Script:|>  $config{SCRIPT_DIR}
429
430 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
431 <|GREEN Execution User:|>  $config{USER} ($config{UID})
432 <|GREEN Socket Engine:|>   $config{SOCKETENGINE}
433
434 To build with these settings run '<|GREEN make -j${\get_cpu_count} install|>' now.
435
436 EOM
437
438 # Routine to list out the extra/ modules that have been enabled.
439 # Note: when getting any filenames out and comparing, it's important to lc it if the
440 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
441 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
442 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
443 sub list_extras () {
444         use File::Spec;
445         # @_ not used
446         my $srcdir = File::Spec->catdir("src", "modules");
447         my $abs_srcdir = File::Spec->rel2abs($srcdir);
448         local $_;
449         my $dd;
450         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
451         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
452         closedir $dd;
453         undef $dd;
454         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
455         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
456         closedir $dd;
457         undef $dd;
458         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
459         my %extras = ();
460 EXTRA:  for my $extra (@extras) {
461                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
462                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
463                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
464                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
465                 if (-l $abs_source) {
466                         # Symlink, is it in the right place?
467                         my $targ = readlink($abs_source);
468                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
469                         if ($abs_targ eq $abs_extra) {
470                                 $extras{$extra} = "\e[32;1menabled\e[0m";
471                         } else {
472                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
473                         }
474                 } elsif (-e $abs_source) {
475                         my ($devext, $inoext) = stat($abs_extra);
476                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
477                         if ($lnksrc > 1) {
478                                 if ($devsrc == $devext && $inosrc == $inoext) {
479                                         $extras{$extra} = "\e[32;1menabled\e[0m";
480                                 } else {
481                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
482                                 }
483                         } else {
484                                 open my $extfd, "<", $abs_extra;
485                                 open my $srcfd, "<", $abs_source;
486                                 local $/ = undef;
487                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
488                                         $extras{$extra} = "\e[32;1menabled\e[0m";
489                                 } else {
490                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
491                                 }
492                         }
493                 } else {
494                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
495                 }
496         }
497         # Now let's add dependency info
498         for my $extra (keys(%extras)) {
499                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
500                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
501                 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
502                 for my $dep (@deps) {
503                         if (exists($extras{$dep})) {
504                                 my $ref = \$extras{$dep}; # Take reference.
505                                 if ($$ref !~ m/needed by/) {
506                                         # First dependency found.
507                                         if ($$ref =~ m/enabled/) {
508                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
509                                         } else {
510                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
511                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
512                                         }
513                                 } else {
514                                         if ($$ref =~ m/enabled/) {
515                                                 $$ref .= ", \e[32;1m$extra\e[0m";
516                                         } else {
517                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
518                                         }
519                                 }
520                         }
521                 }
522         }
523         for my $extra (sort {$a cmp $b} keys(%extras)) {
524                 my $text = $extras{$extra};
525                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
526                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
527                 } else {
528                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
529                 }
530         }
531         return keys(%extras) if wantarray; # Can be used by manage_extras.
532 }
533
534 sub enable_extras (@) {
535         my (@extras) = @_;
536         for my $extra (@extras) {
537                 $extra = "m_$extra" unless $extra =~ /^m_/;
538                 $extra = "$extra.cpp" unless $extra =~ /\.cpp$/;
539                 my $extrapath = "src/modules/extra/$extra";
540                 if (!-e $extrapath) {
541                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
542                         next;
543                 }
544                 my $source = "src/modules/$extra";
545                 if (-e $source) {
546                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
547                         next;
548                 }
549                 # Get dependencies, and add them to be processed.
550                 my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
551                 for my $dep (@deps) {
552                         next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
553                         if (!-e "src/modules/$dep" && !-e "include/$dep") {
554                                 if (-e "src/modules/extra/$dep") {
555                                         print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
556                                         push @extras, $dep;
557                                 } else {
558                                         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";
559                                 }
560                         }
561                 }
562                 print "Enabling $extra ... \n";
563                 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
564         }
565 }
566
567 sub disable_extras (@)
568 {
569         opendir my $dd, "src/modules/extra/";
570         my @files = readdir($dd);
571         closedir $dd;
572         my (@extras) = @_;
573 EXTRA:  for my $extra (@extras) {
574                 $extra = "m_$extra" unless $extra =~ /^m_/;
575                 $extra = "$extra.cpp" unless $extra =~ /\.cpp$/;
576                 my $extrapath = "src/modules/extra/$extra";
577                 my $source = "src/modules/$extra";
578                 if (!-e $extrapath) {
579                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
580                         next;
581                 }
582                 if ((! -l $source) || readlink($source) ne "extra/$extra") {
583                         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";
584                         next;
585                 }
586                 # Check if anything needs this.
587                 for my $file (@files) {
588                         my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
589                         # File depends on this extra...
590                         if (scalar(grep { $_ eq $extra } @deps) > 0) {
591                                 # And is both enabled and not about to be disabled.
592                                 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
593                                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
594                                         next EXTRA;
595                                 }
596                         }
597                 }
598                 # Now remove.
599                 print "Disabling $extra ... \n";
600                 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
601         }
602 }