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/>.
34 use warnings FATAL => qw(all);
36 use File::Basename qw(basename);
38 use File::Spec::Functions qw(rel2abs);
39 use Getopt::Long qw(GetOptions);
40 use POSIX qw(getgid getuid);
49 $opt_disable_interactive,
61 sub enable_extras (@);
63 sub disable_extras (@);
66 my @opt_disableextras;
69 'clean' => \&cmd_clean,
71 'update' => \&cmd_update,
73 'disable-interactive' => \$opt_disable_interactive,
74 'binary-dir=s' => \$opt_binary_dir,
75 'config-dir=s' => \$opt_config_dir,
76 'data-dir=s' => \$opt_data_dir,
78 'log-dir=s' => \$opt_log_dir,
79 'manual-dir=s' => \$opt_manual_dir,
80 'module-dir=s' => \$opt_module_dir,
81 'prefix=s' => \$opt_prefix,
82 'socketengine=s' => \$opt_socketengine,
83 'system' => \$opt_system,
86 # TODO: when the modulemanager rewrite is done these should be removed.
87 'disable-extras=s@' => \@opt_disableextras,
88 'enable-extras=s@' => \@opt_enableextras,
89 'list-extras' => sub { list_extras; exit 0; },
92 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
93 @opt_enableextras = split /,/, join(',', @opt_enableextras);
94 @opt_disableextras = split /,/, join(',', @opt_disableextras);
95 enable_extras(@opt_enableextras);
96 disable_extras(@opt_disableextras);
98 print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
102 our $interactive = !(
105 defined $opt_binary_dir ||
106 defined $opt_config_dir ||
107 defined $opt_data_dir ||
108 defined $opt_disable_interactive ||
110 defined $opt_log_dir ||
111 defined $opt_manual_dir ||
112 defined $opt_module_dir ||
113 defined $opt_prefix ||
114 defined $opt_socketengine ||
115 defined $opt_system ||
119 my %version = get_version();
120 print_format "<|BOLD Configuring InspIRCd $version{MAJOR}.$version{MINOR}.$version{PATCH}+$version{LABEL} on $^O.|>\n";
124 %config = read_configure_cache();
125 run_test CONFIGURE_CACHE_FILE, %config;
126 if (!defined $config{VERSION}) {
127 $config{VERSION} = CONFIGURE_CACHE_VERSION;
128 } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
129 print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
130 %config = ('VERSION', CONFIGURE_CACHE_VERSION);
134 $config{CXX} = find_compiler($config{CXX} || $ENV{CXX});
135 unless ($config{CXX}) {
136 print "A suitable C++ compiler could not be detected on your system!\n";
137 print "Set the CXX environment variable to the compiler binary path if this is incorrect.\n";
140 my %compiler = get_compiler_info($config{CXX});
142 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', '-lrt');
143 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
145 if ($config{HAS_EPOLL} = run_test 'epoll', test_header($config{CXX}, 'sys/epoll.h')) {
146 $config{SOCKETENGINE} ||= 'epoll';
149 if ($config{HAS_KQUEUE} = run_test 'kqueue', test_file($config{CXX}, 'kqueue.cpp')) {
150 $config{SOCKETENGINE} ||= 'kqueue';
153 if ($config{HAS_PORTS} = run_test 'Solaris IOCP', test_header($config{CXX}, 'port.h')) {
154 $config{SOCKETENGINE} ||= 'ports';
157 if ($config{HAS_POLL} = run_test 'poll', test_header($config{CXX}, 'poll.h')) {
158 $config{SOCKETENGINE} ||= 'poll';
161 # Select is available on all platforms
162 $config{HAS_SELECT} = 1;
163 $config{SOCKETENGINE} ||= 'select';
165 if (defined $opt_socketengine) {
166 my $cfgkey = 'HAS_' . uc $opt_socketengine;
167 if ($config{$cfgkey} && -f "src/socketengines/socketengine_$opt_socketengine.cpp") {
168 $config{SOCKETENGINE} = $opt_socketengine;
170 print "Unable to use a socket engine which is not supported on this platform ($opt_socketengine)!\n";
171 print "Available socket engines are:";
172 foreach (<src/socketengines/socketengine_*.cpp>) {
173 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
174 print " $1" if $config{'HAS_' . uc $1};
181 if (defined $opt_system) {
182 $config{BASE_DIR} = $opt_prefix || '/var/lib/inspircd';
183 $config{BINARY_DIR} = $opt_binary_dir || '/usr/sbin';
184 $config{CONFIG_DIR} = $opt_config_dir || '/etc/inspircd';
185 $config{DATA_DIR} = $opt_data_dir || '/var/inspircd';
186 $config{LOG_DIR} = $opt_module_dir || '/var/log/inspircd';
187 $config{MANUAL_DIR} = $opt_manual_dir || '/usr/share/man/man1';
188 $config{MODULE_DIR} = $opt_module_dir || '/usr/lib/inspircd';
190 $config{BASE_DIR} = $opt_prefix || $config{BASE_DIR} || rel2abs 'run';
191 $config{BINARY_DIR} = $opt_binary_dir || $config{BINARY_DIR} || rel2abs $config{BASE_DIR} . '/bin';
192 $config{CONFIG_DIR} = $opt_config_dir || $config{CONFIG_DIR} || rel2abs $config{BASE_DIR} . '/conf';
193 $config{DATA_DIR} = $opt_data_dir || $config{DATA_DIR} || rel2abs $config{BASE_DIR} . '/data';
194 $config{LOG_DIR} = $opt_log_dir || $config{LOG_DIR} || rel2abs $config{BASE_DIR} . '/logs';
195 $config{MANUAL_DIR} = $opt_manual_dir || $config{MANUAL_DIR} || rel2abs $config{BASE_DIR} . '/manuals';
196 $config{MODULE_DIR} = $opt_module_dir || $config{MODULE_DIR} || rel2abs $config{BASE_DIR} . '/modules';
199 # Parse --gid=123 or --gid=foo and extract the group id.
201 if (defined $opt_gid) {
202 @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
203 print_error "there is no '$opt_gid' group on this system!" unless @group;
205 @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} || getgid());
206 print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
208 $config{GROUP} = $group[0];
209 $config{GID} = $group[2];
211 # Parse --uid=123 or --uid=foo and extract the user id.
213 if (defined $opt_uid) {
214 @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
215 print_error "there is no '$opt_uid' user on this system!" unless @user;
217 @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} || getuid());
218 print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
220 $config{USER} = $user[0];
221 $config{UID} = $user[2];
224 system 'tput', 'clear' if $interactive;
226 # Check that the user actually wants this version.
227 if ($version{LABEL} ne 'release' && $interactive) {
228 print_warning <<'EOW';
229 You are building a development version. This contains code which has
230 not been tested as heavily and may contain various faults which could seriously
231 affect the running of your server. It is recommended that you use a stable
234 You can obtain the latest stable version from http://www.inspircd.org/ or by
235 running `git checkout insp20` if you are installing from Git.
238 exit 1 unless prompt_bool $interactive, 'I understand this warning and want to continue anyway.', !$interactive;
241 # Configure directory settings.
242 my $question = <<"EOQ";
243 Currently, InspIRCd is configured with the following paths:
245 <|BOLD Base:|> $config{BASE_DIR}
246 <|BOLD Binary:|> $config{BINARY_DIR}
247 <|BOLD Config:|> $config{CONFIG_DIR}
248 <|BOLD Data:|> $config{DATA_DIR}
249 <|BOLD Log:|> $config{LOG_DIR}
250 <|BOLD Manual:|> $config{MANUAL_DIR}
251 <|BOLD Module:|> $config{MODULE_DIR}
253 Do you want to change these settings?
255 if (prompt_bool $interactive, $question, 0) {
256 my $original_base_dir = $config{BASE_DIR};
257 $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
258 foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR)) {
259 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
261 $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
262 $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
263 $config{DATA_DIR} = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
264 $config{LOG_DIR} = prompt_dir $interactive, 'In what directory are log files to be stored?', $config{LOG_DIR};
265 $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?', $config{MANUAL_DIR};
266 $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?', $config{MODULE_DIR};
269 # Configure module settings.
271 Currently, InspIRCd is configured to automatically enable all available extra modules.
273 Would you like to enable extra modules manually?
275 if (prompt_bool $interactive, $question, 0) {
276 foreach my $extra (<src/modules/extra/m_*.cpp>) {
277 my $module_name = basename $extra, '.cpp';
278 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
279 enable_extras "$module_name.cpp";
283 # TODO: finish modulemanager rewrite and replace this code with:
284 # system './modulemanager', 'enable', '--auto';
285 enable_extras 'm_ssl_gnutls.cpp' unless system 'gnutls-cli --version >/dev/null 2>&1';
286 enable_extras 'm_ssl_openssl.cpp' unless system 'openssl --version >/dev/null 2>&1';
289 # Generate SSL certificates.
290 if (<src/modules/m_ssl_*.cpp> && prompt_bool $interactive, 'Would you like to generate SSL certificates now?', $interactive) {
291 system './tools/genssl', 'auto';
294 write_configure_cache %config if $interactive;
295 parse_templates \%config, \%compiler;
297 print_format <<"EOM";
299 Configuration is complete! You have chosen to build with the following settings:
302 <|GREEN Binary:|> $config{CXX}
303 <|GREEN Name:|> $compiler{NAME}
304 <|GREEN Version:|> $compiler{VERSION}
306 <|GREEN Extra Modules:|> <<TODO>>
312 <|GREEN Base:|> $config{BASE_DIR}
313 <|GREEN Binary:|> $config{BINARY_DIR}
314 <|GREEN Config:|> $config{CONFIG_DIR}
315 <|GREEN Data:|> $config{DATA_DIR}
316 <|GREEN Log:|> $config{LOG_DIR}
317 <|GREEN Manual:|> $config{MANUAL_DIR}
318 <|GREEN Module:|> $config{MODULE_DIR}
320 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
321 <|GREEN Execution User:|> $config{USER} ($config{UID})
322 <|GREEN Socket Engine:|> $config{SOCKETENGINE}
324 To build with these settings run '<|GREEN make -j${\get_cpu_count}|>' now.
328 # Routine to list out the extra/ modules that have been enabled.
329 # Note: when getting any filenames out and comparing, it's important to lc it if the
330 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
331 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
332 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
336 my $srcdir = File::Spec->catdir("src", "modules");
337 my $abs_srcdir = File::Spec->rel2abs($srcdir);
340 opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
341 my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
344 opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
345 my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
348 my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
350 EXTRA: for my $extra (@extras) {
351 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
352 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
353 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
354 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
355 if (-l $abs_source) {
356 # Symlink, is it in the right place?
357 my $targ = readlink($abs_source);
358 my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
359 if ($abs_targ eq $abs_extra) {
360 $extras{$extra} = "\e[32;1menabled\e[0m";
362 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
364 } elsif (-e $abs_source) {
365 my ($devext, $inoext) = stat($abs_extra);
366 my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
368 if ($devsrc == $devext && $inosrc == $inoext) {
369 $extras{$extra} = "\e[32;1menabled\e[0m";
371 $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
374 open my $extfd, "<", $abs_extra;
375 open my $srcfd, "<", $abs_source;
377 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
378 $extras{$extra} = "\e[32;1menabled\e[0m";
380 $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
384 $extras{$extra} = "\e[33;1mdisabled\e[0m";
387 # Now let's add dependency info
388 for my $extra (keys(%extras)) {
389 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
390 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
391 my @deps = split /\s+/, get_property($abs_extra, 'ModDep');
392 for my $dep (@deps) {
393 if (exists($extras{$dep})) {
394 my $ref = \$extras{$dep}; # Take reference.
395 if ($$ref !~ m/needed by/) {
396 # First dependency found.
397 if ($$ref =~ m/enabled/) {
398 $$ref .= " (needed by \e[32;1m$extra\e[0m";
400 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
401 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
404 if ($$ref =~ m/enabled/) {
405 $$ref .= ", \e[32;1m$extra\e[0m";
407 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
413 for my $extra (sort {$a cmp $b} keys(%extras)) {
414 my $text = $extras{$extra};
415 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
416 printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
418 printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
421 return keys(%extras) if wantarray; # Can be used by manage_extras.
424 sub enable_extras (@) {
426 for my $extra (@extras) {
427 my $extrapath = "src/modules/extra/$extra";
428 if (!-e $extrapath) {
429 print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
432 my $source = "src/modules/$extra";
434 print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
437 # Get dependencies, and add them to be processed.
438 my @deps = split /\s+/, get_property($extrapath, 'ModDep');
439 for my $dep (@deps) {
440 next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
441 if (!-e "src/modules/$dep" && !-e "include/$dep") {
442 if (-e "src/modules/extra/$dep") {
443 print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
446 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";
450 print "Enabling $extra ... \n";
451 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
455 sub disable_extras (@)
457 opendir my $dd, "src/modules/extra/";
458 my @files = readdir($dd);
461 EXTRA: for my $extra (@extras) {
462 my $extrapath = "src/modules/extra/$extra";
463 my $source = "src/modules/$extra";
464 if (!-e $extrapath) {
465 print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
468 if ((! -l $source) || readlink($source) ne "extra/$extra") {
469 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";
472 # Check if anything needs this.
473 for my $file (@files) {
474 my @deps = split /\s+/, get_property("src/modules/extra/$file", 'ModDep');
475 # File depends on this extra...
476 if (scalar(grep { $_ eq $extra } @deps) > 0) {
477 # And is both enabled and not about to be disabled.
478 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
479 print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
485 print "Disabling $extra ... \n";
486 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";