4 # InspIRCd -- Internet Relay Chat Daemon
6 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
7 # Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
8 # Copyright (C) 2003, 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
9 # Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
10 # Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
11 # Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
12 # Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
13 # Copyright (C) 2003-2006 Craig McLure <craig@chatspike.net>
15 # This file is part of InspIRCd. InspIRCd is free software: you can
16 # redistribute it and/or modify it under the terms of the GNU General Public
17 # License as published by the Free Software Foundation, version 2.
19 # This program is distributed in the hope that it will be useful, but WITHOUT
20 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
35 use warnings FATAL => qw(all);
37 use File::Basename qw(basename);
39 use File::Spec::Functions qw(rel2abs);
40 use FindBin qw($RealDir);
41 use Getopt::Long qw(GetOptions);
42 use POSIX qw(getgid getuid);
54 $opt_disable_interactive,
55 $opt_distribution_label,
67 sub enable_extras (@);
69 sub disable_extras (@);
72 my @opt_disableextras;
75 'clean' => \&cmd_clean,
77 'update' => \&cmd_update,
79 'development' => \$opt_development,
80 'disable-interactive' => \$opt_disable_interactive,
81 'distribution-label=s' => \$opt_distribution_label,
82 'binary-dir=s' => \$opt_binary_dir,
83 'config-dir=s' => \$opt_config_dir,
84 'data-dir=s' => \$opt_data_dir,
86 'log-dir=s' => \$opt_log_dir,
87 'manual-dir=s' => \$opt_manual_dir,
88 'module-dir=s' => \$opt_module_dir,
89 'prefix=s' => \$opt_prefix,
90 'socketengine=s' => \$opt_socketengine,
91 'system' => \$opt_system,
94 # TODO: when the modulemanager rewrite is done these should be removed.
95 'disable-extras=s@' => \@opt_disableextras,
96 'enable-extras=s@' => \@opt_enableextras,
97 'list-extras' => sub { list_extras; exit 0; },
100 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
101 @opt_enableextras = split /,/, join(',', @opt_enableextras);
102 @opt_disableextras = split /,/, join(',', @opt_disableextras);
103 enable_extras(@opt_enableextras);
104 disable_extras(@opt_disableextras);
106 print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
110 our $interactive = !(
113 defined $opt_binary_dir ||
114 defined $opt_config_dir ||
115 defined $opt_data_dir ||
116 defined $opt_development ||
117 defined $opt_disable_interactive ||
118 defined $opt_distribution_label ||
120 defined $opt_log_dir ||
121 defined $opt_manual_dir ||
122 defined $opt_module_dir ||
123 defined $opt_prefix ||
124 defined $opt_socketengine ||
125 defined $opt_system ||
129 my %version = get_version $opt_distribution_label;
130 print_format "<|BOLD Configuring InspIRCd $version{FULL} on $^O.|>\n";
134 %config = read_configure_cache();
135 run_test CONFIGURE_CACHE_FILE, %config;
136 if (!defined $config{VERSION}) {
137 $config{VERSION} = CONFIGURE_CACHE_VERSION;
138 } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
139 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
140 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
144 $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
145 unless ($config{CXX}) {
146 say 'A suitable C++ compiler could not be detected on your system!';
147 unless ($interactive) {
148 say 'Set the CXX environment variable to the path to a C++ compiler binary if this is incorrect.';
151 until ($config{CXX}) {
152 my $compiler_path = prompt_string 1, 'Please enter the path to a C++ compiler binary:', 'c++';
153 $config{CXX} = find_compiler $compiler_path;
156 my %compiler = get_compiler_info($config{CXX});
158 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', '-lrt');
159 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
161 if ($config{HAS_EPOLL} = run_test 'epoll', test_header($config{CXX}, 'sys/epoll.h')) {
162 $config{SOCKETENGINE} //= 'epoll';
165 if ($config{HAS_KQUEUE} = run_test 'kqueue', test_file($config{CXX}, 'kqueue.cpp')) {
166 $config{SOCKETENGINE} //= 'kqueue';
169 if ($config{HAS_PORTS} = run_test 'Solaris IOCP', test_header($config{CXX}, 'port.h')) {
170 $config{SOCKETENGINE} //= 'ports';
173 if ($config{HAS_POLL} = run_test 'poll', test_header($config{CXX}, 'poll.h')) {
174 $config{SOCKETENGINE} //= 'poll';
177 # Select is available on all platforms
178 $config{HAS_SELECT} = 1;
179 $config{SOCKETENGINE} //= 'select';
181 if (defined $opt_socketengine) {
182 my $cfgkey = 'HAS_' . uc $opt_socketengine;
183 if ($config{$cfgkey} && -f "src/socketengines/socketengine_$opt_socketengine.cpp") {
184 $config{SOCKETENGINE} = $opt_socketengine;
186 print "Unable to use a socket engine which is not supported on this platform ($opt_socketengine)!\n";
187 print "Available socket engines are:";
188 foreach (<src/socketengines/socketengine_*.cpp>) {
189 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
190 print " $1" if $config{'HAS_' . uc $1};
197 if (defined $opt_system) {
198 $config{BASE_DIR} = $opt_prefix // '/var/lib/inspircd';
199 $config{BINARY_DIR} = $opt_binary_dir // '/usr/sbin';
200 $config{CONFIG_DIR} = $opt_config_dir // '/etc/inspircd';
201 $config{DATA_DIR} = $opt_data_dir // '/var/inspircd';
202 $config{LOG_DIR} = $opt_module_dir // '/var/log/inspircd';
203 $config{MANUAL_DIR} = $opt_manual_dir // '/usr/share/man/man1';
204 $config{MODULE_DIR} = $opt_module_dir // '/usr/lib/inspircd';
206 $config{BASE_DIR} = $opt_prefix // $config{BASE_DIR} // rel2abs 'run';
207 $config{BINARY_DIR} = $opt_binary_dir // $config{BINARY_DIR} // rel2abs $config{BASE_DIR} . '/bin';
208 $config{CONFIG_DIR} = $opt_config_dir // $config{CONFIG_DIR} // rel2abs $config{BASE_DIR} . '/conf';
209 $config{DATA_DIR} = $opt_data_dir // $config{DATA_DIR} // rel2abs $config{BASE_DIR} . '/data';
210 $config{LOG_DIR} = $opt_log_dir // $config{LOG_DIR} // rel2abs $config{BASE_DIR} . '/logs';
211 $config{MANUAL_DIR} = $opt_manual_dir // $config{MANUAL_DIR} // rel2abs $config{BASE_DIR} . '/manuals';
212 $config{MODULE_DIR} = $opt_module_dir // $config{MODULE_DIR} // rel2abs $config{BASE_DIR} . '/modules';
215 # Parse --gid=123 or --gid=foo and extract the group id.
217 if (defined $opt_gid) {
218 @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
219 print_error "there is no '$opt_gid' group on this system!" unless @group;
221 @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
222 print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
224 $config{GROUP} = $group[0];
225 $config{GID} = $group[2];
227 # Parse --uid=123 or --uid=foo and extract the user id.
229 if (defined $opt_uid) {
230 @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
231 print_error "there is no '$opt_uid' user on this system!" unless @user;
233 @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
234 print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
236 $config{USER} = $user[0];
237 $config{UID} = $user[2];
240 system 'tput', 'clear' if $interactive;
242 # Check that the user actually wants this version.
243 if ($version{LABEL} ne 'release') {
244 print_warning <<'EOW';
245 You are building a development version. This contains code which has
246 not been tested as heavily and may contain various faults which could seriously
247 affect the running of your server. It is recommended that you use a stable
250 You can obtain the latest stable version from http://www.inspircd.org/ or by
251 running `git checkout insp20` if you are installing from Git.
253 if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
254 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
259 # Configure directory settings.
260 my $question = <<"EOQ";
261 Currently, InspIRCd is configured with the following paths:
263 <|BOLD Base:|> $config{BASE_DIR}
264 <|BOLD Binary:|> $config{BINARY_DIR}
265 <|BOLD Config:|> $config{CONFIG_DIR}
266 <|BOLD Data:|> $config{DATA_DIR}
267 <|BOLD Log:|> $config{LOG_DIR}
268 <|BOLD Manual:|> $config{MANUAL_DIR}
269 <|BOLD Module:|> $config{MODULE_DIR}
271 Do you want to change these settings?
273 if (prompt_bool $interactive, $question, 0) {
274 my $original_base_dir = $config{BASE_DIR};
275 $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
276 foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR)) {
277 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
279 $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
280 $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
281 $config{DATA_DIR} = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
282 $config{LOG_DIR} = prompt_dir $interactive, 'In what directory are log files to be stored?', $config{LOG_DIR};
283 $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?', $config{MANUAL_DIR};
284 $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?', $config{MODULE_DIR};
287 # Configure module settings.
289 Currently, InspIRCd is configured to automatically enable all available extra modules.
291 Would you like to enable extra modules manually?
293 if (prompt_bool $interactive, $question, 0) {
294 foreach my $extra (<src/modules/extra/m_*.cpp>) {
295 my $module_name = basename $extra, '.cpp';
296 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
297 enable_extras "$module_name.cpp";
301 # TODO: finish modulemanager rewrite and replace this code with:
302 # system './modulemanager', 'enable', '--auto';
303 enable_extras 'm_ssl_gnutls.cpp' unless system 'pkg-config --exists gnutls >/dev/null 2>&1';
304 enable_extras 'm_ssl_mbedtls.cpp' if -e '/usr/include/mbedtls/ssl.h';
305 enable_extras 'm_ssl_openssl.cpp' unless system 'pkg-config --exists openssl >/dev/null 2>&1';
308 # Generate SSL certificates.
309 if (<src/modules/m_ssl_*.cpp> && prompt_bool $interactive, 'Would you like to generate SSL certificates now?', $interactive) {
310 system './tools/genssl', 'auto';
313 # Cache the distribution label so that its not lost when --update is run.
314 $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
316 write_configure_cache %config;
317 parse_templates \%config, \%compiler, \%version;
319 print_format <<"EOM";
321 Configuration is complete! You have chosen to build with the following settings:
324 <|GREEN Binary:|> $config{CXX}
325 <|GREEN Name:|> $compiler{NAME}
326 <|GREEN Version:|> $compiler{VERSION}
328 <|GREEN Extra Modules:|>
331 for my $file (<src/modules/m_*>) {
332 my $module = basename $file, '.cpp';
333 say " * $module" if -l $file;
336 print_format <<"EOM";
339 <|GREEN Base:|> $config{BASE_DIR}
340 <|GREEN Binary:|> $config{BINARY_DIR}
341 <|GREEN Config:|> $config{CONFIG_DIR}
342 <|GREEN Data:|> $config{DATA_DIR}
343 <|GREEN Log:|> $config{LOG_DIR}
344 <|GREEN Manual:|> $config{MANUAL_DIR}
345 <|GREEN Module:|> $config{MODULE_DIR}
347 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
348 <|GREEN Execution User:|> $config{USER} ($config{UID})
349 <|GREEN Socket Engine:|> $config{SOCKETENGINE}
351 To build with these settings run '<|GREEN make -j${\get_cpu_count}|>' now.
355 # Routine to list out the extra/ modules that have been enabled.
356 # Note: when getting any filenames out and comparing, it's important to lc it if the
357 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
358 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
359 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
363 my $srcdir = File::Spec->catdir("src", "modules");
364 my $abs_srcdir = File::Spec->rel2abs($srcdir);
367 opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
368 my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
371 opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
372 my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
375 my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
377 EXTRA: for my $extra (@extras) {
378 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
379 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
380 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
381 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
382 if (-l $abs_source) {
383 # Symlink, is it in the right place?
384 my $targ = readlink($abs_source);
385 my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
386 if ($abs_targ eq $abs_extra) {
387 $extras{$extra} = "\e[32;1menabled\e[0m";
389 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
391 } elsif (-e $abs_source) {
392 my ($devext, $inoext) = stat($abs_extra);
393 my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
395 if ($devsrc == $devext && $inosrc == $inoext) {
396 $extras{$extra} = "\e[32;1menabled\e[0m";
398 $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
401 open my $extfd, "<", $abs_extra;
402 open my $srcfd, "<", $abs_source;
404 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
405 $extras{$extra} = "\e[32;1menabled\e[0m";
407 $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
411 $extras{$extra} = "\e[33;1mdisabled\e[0m";
414 # Now let's add dependency info
415 for my $extra (keys(%extras)) {
416 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
417 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
418 my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
419 for my $dep (@deps) {
420 if (exists($extras{$dep})) {
421 my $ref = \$extras{$dep}; # Take reference.
422 if ($$ref !~ m/needed by/) {
423 # First dependency found.
424 if ($$ref =~ m/enabled/) {
425 $$ref .= " (needed by \e[32;1m$extra\e[0m";
427 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
428 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
431 if ($$ref =~ m/enabled/) {
432 $$ref .= ", \e[32;1m$extra\e[0m";
434 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
440 for my $extra (sort {$a cmp $b} keys(%extras)) {
441 my $text = $extras{$extra};
442 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
443 printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
445 printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
448 return keys(%extras) if wantarray; # Can be used by manage_extras.
451 sub enable_extras (@) {
453 for my $extra (@extras) {
454 my $extrapath = "src/modules/extra/$extra";
455 if (!-e $extrapath) {
456 print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
459 my $source = "src/modules/$extra";
461 print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
464 # Get dependencies, and add them to be processed.
465 my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
466 for my $dep (@deps) {
467 next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
468 if (!-e "src/modules/$dep" && !-e "include/$dep") {
469 if (-e "src/modules/extra/$dep") {
470 print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
473 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";
477 print "Enabling $extra ... \n";
478 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
482 sub disable_extras (@)
484 opendir my $dd, "src/modules/extra/";
485 my @files = readdir($dd);
488 EXTRA: for my $extra (@extras) {
489 my $extrapath = "src/modules/extra/$extra";
490 my $source = "src/modules/$extra";
491 if (!-e $extrapath) {
492 print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
495 if ((! -l $source) || readlink($source) ne "extra/$extra") {
496 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";
499 # Check if anything needs this.
500 for my $file (@files) {
501 my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
502 # File depends on this extra...
503 if (scalar(grep { $_ eq $extra } @deps) > 0) {
504 # And is both enabled and not about to be disabled.
505 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
506 print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
512 print "Disabling $extra ... \n";
513 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";