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 Getopt::Long qw(GetOptions);
41 use POSIX qw(getgid getuid);
51 $opt_disable_interactive,
52 $opt_distribution_label,
64 sub enable_extras (@);
66 sub disable_extras (@);
69 my @opt_disableextras;
72 'clean' => \&cmd_clean,
74 'update' => \&cmd_update,
76 'development' => \$opt_development,
77 'disable-interactive' => \$opt_disable_interactive,
78 'distribution-label=s' => \$opt_distribution_label,
79 'binary-dir=s' => \$opt_binary_dir,
80 'config-dir=s' => \$opt_config_dir,
81 'data-dir=s' => \$opt_data_dir,
83 'log-dir=s' => \$opt_log_dir,
84 'manual-dir=s' => \$opt_manual_dir,
85 'module-dir=s' => \$opt_module_dir,
86 'prefix=s' => \$opt_prefix,
87 'socketengine=s' => \$opt_socketengine,
88 'system' => \$opt_system,
91 # TODO: when the modulemanager rewrite is done these should be removed.
92 'disable-extras=s@' => \@opt_disableextras,
93 'enable-extras=s@' => \@opt_enableextras,
94 'list-extras' => sub { list_extras; exit 0; },
97 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
98 @opt_enableextras = split /,/, join(',', @opt_enableextras);
99 @opt_disableextras = split /,/, join(',', @opt_disableextras);
100 enable_extras(@opt_enableextras);
101 disable_extras(@opt_disableextras);
103 print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
107 our $interactive = !(
110 defined $opt_binary_dir ||
111 defined $opt_config_dir ||
112 defined $opt_data_dir ||
113 defined $opt_development ||
114 defined $opt_disable_interactive ||
115 defined $opt_distribution_label ||
117 defined $opt_log_dir ||
118 defined $opt_manual_dir ||
119 defined $opt_module_dir ||
120 defined $opt_prefix ||
121 defined $opt_socketengine ||
122 defined $opt_system ||
126 my %version = get_version();
127 print_format "<|BOLD Configuring InspIRCd $version{MAJOR}.$version{MINOR}.$version{PATCH}+$version{LABEL} on $^O.|>\n";
131 %config = read_configure_cache();
132 run_test CONFIGURE_CACHE_FILE, %config;
133 if (!defined $config{VERSION}) {
134 $config{VERSION} = CONFIGURE_CACHE_VERSION;
135 } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
136 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
137 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
141 $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
142 unless ($config{CXX}) {
143 print "A suitable C++ compiler could not be detected on your system!\n";
144 print "Set the CXX environment variable to the compiler binary path if this is incorrect.\n";
147 my %compiler = get_compiler_info($config{CXX});
149 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', '-lrt');
150 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
152 if ($config{HAS_EPOLL} = run_test 'epoll', test_header($config{CXX}, 'sys/epoll.h')) {
153 $config{SOCKETENGINE} //= 'epoll';
156 if ($config{HAS_KQUEUE} = run_test 'kqueue', test_file($config{CXX}, 'kqueue.cpp')) {
157 $config{SOCKETENGINE} //= 'kqueue';
160 if ($config{HAS_PORTS} = run_test 'Solaris IOCP', test_header($config{CXX}, 'port.h')) {
161 $config{SOCKETENGINE} //= 'ports';
164 if ($config{HAS_POLL} = run_test 'poll', test_header($config{CXX}, 'poll.h')) {
165 $config{SOCKETENGINE} //= 'poll';
168 # Select is available on all platforms
169 $config{HAS_SELECT} = 1;
170 $config{SOCKETENGINE} //= 'select';
172 if (defined $opt_socketengine) {
173 my $cfgkey = 'HAS_' . uc $opt_socketengine;
174 if ($config{$cfgkey} && -f "src/socketengines/socketengine_$opt_socketengine.cpp") {
175 $config{SOCKETENGINE} = $opt_socketengine;
177 print "Unable to use a socket engine which is not supported on this platform ($opt_socketengine)!\n";
178 print "Available socket engines are:";
179 foreach (<src/socketengines/socketengine_*.cpp>) {
180 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
181 print " $1" if $config{'HAS_' . uc $1};
188 # If the user has specified a distribution label then we use it in
189 # place of the label from src/version.sh or Git.
190 if (defined $opt_distribution_label) {
191 $version{LABEL} = $opt_distribution_label;
194 if (defined $opt_system) {
195 $config{BASE_DIR} = $opt_prefix // '/var/lib/inspircd';
196 $config{BINARY_DIR} = $opt_binary_dir // '/usr/sbin';
197 $config{CONFIG_DIR} = $opt_config_dir // '/etc/inspircd';
198 $config{DATA_DIR} = $opt_data_dir // '/var/inspircd';
199 $config{LOG_DIR} = $opt_module_dir // '/var/log/inspircd';
200 $config{MANUAL_DIR} = $opt_manual_dir // '/usr/share/man/man1';
201 $config{MODULE_DIR} = $opt_module_dir // '/usr/lib/inspircd';
203 $config{BASE_DIR} = $opt_prefix // $config{BASE_DIR} // rel2abs 'run';
204 $config{BINARY_DIR} = $opt_binary_dir // $config{BINARY_DIR} // rel2abs $config{BASE_DIR} . '/bin';
205 $config{CONFIG_DIR} = $opt_config_dir // $config{CONFIG_DIR} // rel2abs $config{BASE_DIR} . '/conf';
206 $config{DATA_DIR} = $opt_data_dir // $config{DATA_DIR} // rel2abs $config{BASE_DIR} . '/data';
207 $config{LOG_DIR} = $opt_log_dir // $config{LOG_DIR} // rel2abs $config{BASE_DIR} . '/logs';
208 $config{MANUAL_DIR} = $opt_manual_dir // $config{MANUAL_DIR} // rel2abs $config{BASE_DIR} . '/manuals';
209 $config{MODULE_DIR} = $opt_module_dir // $config{MODULE_DIR} // rel2abs $config{BASE_DIR} . '/modules';
212 # Parse --gid=123 or --gid=foo and extract the group id.
214 if (defined $opt_gid) {
215 @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
216 print_error "there is no '$opt_gid' group on this system!" unless @group;
218 @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
219 print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
221 $config{GROUP} = $group[0];
222 $config{GID} = $group[2];
224 # Parse --uid=123 or --uid=foo and extract the user id.
226 if (defined $opt_uid) {
227 @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
228 print_error "there is no '$opt_uid' user on this system!" unless @user;
230 @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
231 print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
233 $config{USER} = $user[0];
234 $config{UID} = $user[2];
237 system 'tput', 'clear' if $interactive;
239 # Check that the user actually wants this version.
240 if ($version{LABEL} ne 'release') {
241 print_warning <<'EOW';
242 You are building a development version. This contains code which has
243 not been tested as heavily and may contain various faults which could seriously
244 affect the running of your server. It is recommended that you use a stable
247 You can obtain the latest stable version from http://www.inspircd.org/ or by
248 running `git checkout insp20` if you are installing from Git.
250 if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
251 say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
256 # Configure directory settings.
257 my $question = <<"EOQ";
258 Currently, InspIRCd is configured with the following paths:
260 <|BOLD Base:|> $config{BASE_DIR}
261 <|BOLD Binary:|> $config{BINARY_DIR}
262 <|BOLD Config:|> $config{CONFIG_DIR}
263 <|BOLD Data:|> $config{DATA_DIR}
264 <|BOLD Log:|> $config{LOG_DIR}
265 <|BOLD Manual:|> $config{MANUAL_DIR}
266 <|BOLD Module:|> $config{MODULE_DIR}
268 Do you want to change these settings?
270 if (prompt_bool $interactive, $question, 0) {
271 my $original_base_dir = $config{BASE_DIR};
272 $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
273 foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR)) {
274 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
276 $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
277 $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
278 $config{DATA_DIR} = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
279 $config{LOG_DIR} = prompt_dir $interactive, 'In what directory are log files to be stored?', $config{LOG_DIR};
280 $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?', $config{MANUAL_DIR};
281 $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?', $config{MODULE_DIR};
284 # Configure module settings.
286 Currently, InspIRCd is configured to automatically enable all available extra modules.
288 Would you like to enable extra modules manually?
290 if (prompt_bool $interactive, $question, 0) {
291 foreach my $extra (<src/modules/extra/m_*.cpp>) {
292 my $module_name = basename $extra, '.cpp';
293 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
294 enable_extras "$module_name.cpp";
298 # TODO: finish modulemanager rewrite and replace this code with:
299 # system './modulemanager', 'enable', '--auto';
300 enable_extras 'm_ssl_gnutls.cpp' unless system 'gnutls-cli --version >/dev/null 2>&1';
301 enable_extras 'm_ssl_openssl.cpp' unless system 'openssl --version >/dev/null 2>&1';
304 # Generate SSL certificates.
305 if (<src/modules/m_ssl_*.cpp> && prompt_bool $interactive, 'Would you like to generate SSL certificates now?', $interactive) {
306 system './tools/genssl', 'auto';
309 write_configure_cache %config;
310 parse_templates \%config, \%compiler, \%version;
312 print_format <<"EOM";
314 Configuration is complete! You have chosen to build with the following settings:
317 <|GREEN Binary:|> $config{CXX}
318 <|GREEN Name:|> $compiler{NAME}
319 <|GREEN Version:|> $compiler{VERSION}
321 <|GREEN Extra Modules:|>
324 for my $file (<src/modules/m_*>) {
325 my $module = basename $file, '.cpp';
326 say " * $module" if -l $file;
329 print_format <<"EOM";
332 <|GREEN Base:|> $config{BASE_DIR}
333 <|GREEN Binary:|> $config{BINARY_DIR}
334 <|GREEN Config:|> $config{CONFIG_DIR}
335 <|GREEN Data:|> $config{DATA_DIR}
336 <|GREEN Log:|> $config{LOG_DIR}
337 <|GREEN Manual:|> $config{MANUAL_DIR}
338 <|GREEN Module:|> $config{MODULE_DIR}
340 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
341 <|GREEN Execution User:|> $config{USER} ($config{UID})
342 <|GREEN Socket Engine:|> $config{SOCKETENGINE}
344 To build with these settings run '<|GREEN make -j${\get_cpu_count}|>' now.
348 # Routine to list out the extra/ modules that have been enabled.
349 # Note: when getting any filenames out and comparing, it's important to lc it if the
350 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
351 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
352 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
356 my $srcdir = File::Spec->catdir("src", "modules");
357 my $abs_srcdir = File::Spec->rel2abs($srcdir);
360 opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
361 my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
364 opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
365 my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
368 my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
370 EXTRA: for my $extra (@extras) {
371 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
372 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
373 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
374 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
375 if (-l $abs_source) {
376 # Symlink, is it in the right place?
377 my $targ = readlink($abs_source);
378 my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
379 if ($abs_targ eq $abs_extra) {
380 $extras{$extra} = "\e[32;1menabled\e[0m";
382 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
384 } elsif (-e $abs_source) {
385 my ($devext, $inoext) = stat($abs_extra);
386 my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
388 if ($devsrc == $devext && $inosrc == $inoext) {
389 $extras{$extra} = "\e[32;1menabled\e[0m";
391 $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
394 open my $extfd, "<", $abs_extra;
395 open my $srcfd, "<", $abs_source;
397 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
398 $extras{$extra} = "\e[32;1menabled\e[0m";
400 $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
404 $extras{$extra} = "\e[33;1mdisabled\e[0m";
407 # Now let's add dependency info
408 for my $extra (keys(%extras)) {
409 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
410 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
411 my @deps = split /\s+/, get_property($abs_extra, 'ModDep');
412 for my $dep (@deps) {
413 if (exists($extras{$dep})) {
414 my $ref = \$extras{$dep}; # Take reference.
415 if ($$ref !~ m/needed by/) {
416 # First dependency found.
417 if ($$ref =~ m/enabled/) {
418 $$ref .= " (needed by \e[32;1m$extra\e[0m";
420 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
421 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
424 if ($$ref =~ m/enabled/) {
425 $$ref .= ", \e[32;1m$extra\e[0m";
427 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
433 for my $extra (sort {$a cmp $b} keys(%extras)) {
434 my $text = $extras{$extra};
435 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
436 printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
438 printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
441 return keys(%extras) if wantarray; # Can be used by manage_extras.
444 sub enable_extras (@) {
446 for my $extra (@extras) {
447 my $extrapath = "src/modules/extra/$extra";
448 if (!-e $extrapath) {
449 print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
452 my $source = "src/modules/$extra";
454 print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
457 # Get dependencies, and add them to be processed.
458 my @deps = split /\s+/, get_property($extrapath, 'ModDep');
459 for my $dep (@deps) {
460 next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
461 if (!-e "src/modules/$dep" && !-e "include/$dep") {
462 if (-e "src/modules/extra/$dep") {
463 print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
466 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";
470 print "Enabling $extra ... \n";
471 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
475 sub disable_extras (@)
477 opendir my $dd, "src/modules/extra/";
478 my @files = readdir($dd);
481 EXTRA: for my $extra (@extras) {
482 my $extrapath = "src/modules/extra/$extra";
483 my $source = "src/modules/$extra";
484 if (!-e $extrapath) {
485 print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
488 if ((! -l $source) || readlink($source) ne "extra/$extra") {
489 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";
492 # Check if anything needs this.
493 for my $file (@files) {
494 my @deps = split /\s+/, get_property("src/modules/extra/$file", 'ModDep');
495 # File depends on this extra...
496 if (scalar(grep { $_ eq $extra } @deps) > 0) {
497 # And is both enabled and not about to be disabled.
498 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
499 print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
505 print "Disabling $extra ... \n";
506 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";