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