]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
Miscellaneous improvements to configure.
[user/henk/code/inspircd.git] / configure
1 #!/usr/bin/env perl
2
3 #
4 # InspIRCd -- Internet Relay Chat Daemon
5 #
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>
14 #
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.
18 #
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
22 # details.
23 #
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/>.
26 #
27
28
29 BEGIN {
30         require 5.8.0;
31 }
32
33 use strict;
34 use warnings FATAL => qw(all);
35
36 use File::Basename        qw(basename);
37 use File::Copy            ();
38 use File::Spec::Functions qw(rel2abs);
39 use Getopt::Long          qw(GetOptions);
40 use POSIX                 qw(getgid getuid);
41
42 use make::configure;
43 use make::console;
44 use make::utilities;
45
46 my ($opt_binary_dir,
47     $opt_config_dir,
48     $opt_data_dir,
49     $opt_disable_interactive,
50     $opt_gid,
51     $opt_log_dir,
52     $opt_manual_dir,
53     $opt_module_dir,
54     $opt_prefix,
55     $opt_socketengine,
56     $opt_system,
57     $opt_uid);
58
59 sub list_extras ();
60
61 sub enable_extras (@);
62
63 sub disable_extras (@);
64
65 my @opt_enableextras;
66 my @opt_disableextras;
67
68 GetOptions(
69         'clean'  => \&cmd_clean,
70         'help'   => \&cmd_help,
71         'update' => \&cmd_update,
72
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,
77         'gid=s'               => \$opt_gid,
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,
84         'uid=s'               => \$opt_uid,
85
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; },
90 );
91
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);
97         list_extras;
98         print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
99         exit 0;
100 }
101
102 our $interactive = !(
103         !-t STDIN ||
104         !-t STDOUT ||
105         defined $opt_binary_dir ||
106         defined $opt_config_dir ||
107         defined $opt_data_dir ||
108         defined $opt_disable_interactive ||
109         defined $opt_gid ||
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 ||
116         defined $opt_uid
117 );
118
119 my %version = get_version();
120 print_format "<|BOLD Configuring InspIRCd $version{MAJOR}.$version{MINOR}.$version{PATCH}+$version{LABEL} on $^O.|>\n";
121
122 our %config = read_configure_cache();
123
124 print "Checking for cache from previous configure... ";
125 print %config ? "found\n" : "not found\n";
126
127 $config{CXX} = defined $ENV{CXX} && !system("$ENV{CXX} -v > /dev/null 2>&1") ? $ENV{CXX} : find_compiler();
128 if ($config{CXX} eq "") {
129         print "A C++ compiler could not be detected on your system!\n";
130         print "Set the CXX environment variable to the full path if this is incorrect.\n";
131         exit 1; 
132 }
133
134 my %compiler = get_compiler_info($config{CXX});
135 if ($compiler{UNSUPPORTED}) {
136         print "Your C++ compiler is too old to build InspIRCd!\n";
137         print "Reason: $compiler{REASON}\n";
138         exit 1;
139 }
140
141 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', '-lrt');
142 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
143
144 if ($config{HAS_EPOLL} = run_test 'epoll', test_header($config{CXX}, 'sys/epoll.h')) {
145         $config{SOCKETENGINE} ||= 'epoll';
146 }
147
148 if ($config{HAS_KQUEUE} = run_test 'kqueue', test_file($config{CXX}, 'kqueue.cpp')) {
149         $config{SOCKETENGINE} ||= 'kqueue';
150 }
151
152 if ($config{HAS_PORTS} = run_test 'Solaris IOCP', test_header($config{CXX}, 'port.h')) {
153         $config{SOCKETENGINE} ||= 'ports';
154 }
155
156 if ($config{HAS_POLL} = run_test 'poll', test_header($config{CXX}, 'poll.h')) {
157         $config{SOCKETENGINE} ||= 'poll';
158 }
159
160 # Select is available on all platforms
161 $config{HAS_SELECT} = 1;
162 $config{SOCKETENGINE} ||= 'select';
163
164 if (defined $opt_socketengine) {
165         my $cfgkey = 'HAS_' . uc $opt_socketengine;
166         if ($config{$cfgkey} && -f "src/socketengines/socketengine_$opt_socketengine.cpp") {
167                 $config{SOCKETENGINE} = $opt_socketengine;
168         } else {
169                 print "Unable to use a socket engine which is not supported on this platform ($opt_socketengine)!\n";
170                 print "Available socket engines are:";
171                 foreach (<src/socketengines/socketengine_*.cpp>) {
172                         s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
173                         print " $1" if $config{'HAS_' . uc $1};
174                 }
175                 print "\n";     
176                 exit 1;
177         }
178 }
179
180 if (defined $opt_system) {
181         $config{BASE_DIR}   = $opt_prefix     || '/var/lib/inspircd';
182         $config{BINARY_DIR} = $opt_binary_dir || '/usr/sbin';
183         $config{CONFIG_DIR} = $opt_config_dir || '/etc/inspircd';
184         $config{DATA_DIR}   = $opt_data_dir   || '/var/inspircd';
185         $config{LOG_DIR}    = $opt_module_dir || '/var/log/inspircd';
186         $config{MANUAL_DIR} = $opt_manual_dir || '/usr/share/man/man1';
187         $config{MODULE_DIR} = $opt_module_dir || '/usr/lib/inspircd';
188 } else {
189         $config{BASE_DIR}   = $opt_prefix     || $config{BASE_DIR}   || rel2abs 'run';
190         $config{BINARY_DIR} = $opt_binary_dir || $config{BINARY_DIR} || rel2abs $config{BASE_DIR} . '/bin';
191         $config{CONFIG_DIR} = $opt_config_dir || $config{CONFIG_DIR} || rel2abs $config{BASE_DIR} . '/conf';
192         $config{DATA_DIR}   = $opt_data_dir   || $config{DATA_DIR}   || rel2abs $config{BASE_DIR} . '/data';
193         $config{LOG_DIR}    = $opt_log_dir    || $config{LOG_DIR}    || rel2abs $config{BASE_DIR} . '/logs';
194         $config{MANUAL_DIR} = $opt_manual_dir || $config{MANUAL_DIR} || rel2abs $config{BASE_DIR} . '/manuals';
195         $config{MODULE_DIR} = $opt_module_dir || $config{MODULE_DIR} || rel2abs $config{BASE_DIR} . '/modules';
196 }
197
198 # Parse --gid=123 or --gid=foo and extract the group id.
199 my @group;
200 if (defined $opt_gid) {
201         @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
202         print_error "there is no '$opt_gid' group on this system!" unless @group;
203 } else {
204         @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} || getgid());
205         print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
206 }
207 $config{GROUP} = $group[0];
208 $config{GID}   = $group[2];
209
210 # Parse --uid=123 or --uid=foo and extract the user id.
211 my @user;
212 if (defined $opt_uid) {
213         @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
214         print_error "there is no '$opt_uid' user on this system!" unless @user;
215 } else {
216         @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} || getuid());
217         print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
218 }
219 $config{USER} = $user[0];
220 $config{UID}  = $user[2];
221
222 # Clear the screen.
223 system 'tput', 'clear' if $interactive;
224
225 # Check that the user actually wants this version.
226 if ($version{LABEL} ne 'release' && $interactive) {
227         print_warning <<'EOW';
228 You are building a development version. This contains code which has
229 not been tested as heavily and may contain various faults which could seriously
230 affect the running of your server. It is recommended that you use a stable
231 version instead.
232
233 You can obtain the latest stable version from http://www.inspircd.org/ or by
234 running `git checkout insp20` if you are installing from Git.
235
236 EOW
237 exit 1 unless prompt_bool $interactive, 'I understand this warning and want to continue anyway.', !$interactive;
238 }
239
240 # Configure directory settings.
241 my $question = <<"EOQ";
242 Currently, InspIRCd is configured with the following paths:
243
244 <|BOLD Base:|>   $config{BASE_DIR}
245 <|BOLD Binary:|> $config{BINARY_DIR}
246 <|BOLD Config:|> $config{CONFIG_DIR}
247 <|BOLD Data:|>   $config{DATA_DIR}
248 <|BOLD Log:|>    $config{LOG_DIR}
249 <|BOLD Manual:|> $config{MANUAL_DIR}
250 <|BOLD Module:|> $config{MODULE_DIR}
251
252 Do you want to change these settings?
253 EOQ
254 if (prompt_bool $interactive, $question, 0) {
255         my $original_base_dir = $config{BASE_DIR};
256         $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
257         foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR)) {
258                 $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
259         }
260         $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?',     $config{BINARY_DIR};
261         $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
262         $config{DATA_DIR}   = prompt_dir $interactive, 'In what directory are variable data files to be stored?',     $config{DATA_DIR};
263         $config{LOG_DIR}    = prompt_dir $interactive, 'In what directory are log files to be stored?',               $config{LOG_DIR};
264         $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?',        $config{MANUAL_DIR};
265         $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?',             $config{MODULE_DIR};
266 }
267
268 # Configure module settings.
269 $question = <<'EOQ';
270 Currently, InspIRCd is configured to automatically enable all available extra modules.
271
272 Would you like to enable extra modules manually?
273 EOQ
274 if (prompt_bool $interactive, $question, 0) {
275         foreach my $extra (<src/modules/extra/m_*.cpp>) {
276                 my $module_name = basename $extra, '.cpp';
277                 if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
278                         enable_extras "$module_name.cpp";
279                 }
280         }
281 } else {
282         # TODO: finish modulemanager rewrite and replace this code with:
283         # system './modulemanager', 'enable', '--auto';
284         enable_extras 'm_ssl_gnutls.cpp' unless system 'gnutls-cli --version >/dev/null 2>&1';
285         enable_extras 'm_ssl_openssl.cpp' unless system 'openssl --version >/dev/null 2>&1';
286 }
287
288 # Generate SSL certificates.
289 if (<src/modules/m_ssl_*.cpp> && prompt_bool $interactive, 'Would you like to generate SSL certificates now?', $interactive) {
290         system './tools/genssl', 'auto';
291 }
292
293 write_configure_cache %config;
294 parse_templates \%config, \%compiler;
295
296 print_format <<"EOM";
297
298 Configuration is complete! You have chosen to build with the following settings:
299
300 <|GREEN Compiler:|>
301   <|GREEN Binary:|>  $config{CXX}
302   <|GREEN Name:|>    $compiler{NAME}
303   <|GREEN Version:|> $compiler{VERSION}
304
305 <|GREEN Extra Modules:|> <<TODO>>
306   * m_foo
307   * m_bar
308   * m_baz
309
310 <|GREEN Paths:|>
311   <|GREEN Base:|>   $config{BASE_DIR}
312   <|GREEN Binary:|> $config{BINARY_DIR}
313   <|GREEN Config:|> $config{CONFIG_DIR}
314   <|GREEN Data:|>   $config{DATA_DIR}
315   <|GREEN Log:|>    $config{LOG_DIR}
316   <|GREEN Manual:|> $config{MANUAL_DIR}
317   <|GREEN Module:|> $config{MODULE_DIR}
318
319 <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
320 <|GREEN Execution User:|>  $config{USER} ($config{UID})
321 <|GREEN Socket Engine:|>   $config{SOCKETENGINE}
322
323 To build with these settings run '<|GREEN make -j${\get_cpu_count}|>' now.
324
325 EOM
326
327 # Routine to list out the extra/ modules that have been enabled.
328 # Note: when getting any filenames out and comparing, it's important to lc it if the
329 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
330 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
331 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
332 sub list_extras () {
333         use File::Spec;
334         # @_ not used
335         my $srcdir = File::Spec->catdir("src", "modules");
336         my $abs_srcdir = File::Spec->rel2abs($srcdir);
337         local $_;
338         my $dd;
339         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
340         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
341         closedir $dd;
342         undef $dd;
343         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
344         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
345         closedir $dd;
346         undef $dd;
347         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
348         my %extras = ();
349 EXTRA:  for my $extra (@extras) {
350                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
351                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
352                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
353                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
354                 if (-l $abs_source) {
355                         # Symlink, is it in the right place?
356                         my $targ = readlink($abs_source);
357                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
358                         if ($abs_targ eq $abs_extra) {
359                                 $extras{$extra} = "\e[32;1menabled\e[0m";
360                         } else {
361                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
362                         }
363                 } elsif (-e $abs_source) {
364                         my ($devext, $inoext) = stat($abs_extra);
365                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
366                         if ($lnksrc > 1) {
367                                 if ($devsrc == $devext && $inosrc == $inoext) {
368                                         $extras{$extra} = "\e[32;1menabled\e[0m";
369                                 } else {
370                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
371                                 }
372                         } else {
373                                 open my $extfd, "<", $abs_extra;
374                                 open my $srcfd, "<", $abs_source;
375                                 local $/ = undef;
376                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
377                                         $extras{$extra} = "\e[32;1menabled\e[0m";
378                                 } else {
379                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
380                                 }
381                         }
382                 } else {
383                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
384                 }
385         }
386         # Now let's add dependency info
387         for my $extra (keys(%extras)) {
388                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
389                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
390                 my @deps = split /\s+/, get_property($abs_extra, 'ModDep');
391                 for my $dep (@deps) {
392                         if (exists($extras{$dep})) {
393                                 my $ref = \$extras{$dep}; # Take reference.
394                                 if ($$ref !~ m/needed by/) {
395                                         # First dependency found.
396                                         if ($$ref =~ m/enabled/) {
397                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
398                                         } else {
399                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
400                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
401                                         }
402                                 } else {
403                                         if ($$ref =~ m/enabled/) {
404                                                 $$ref .= ", \e[32;1m$extra\e[0m";
405                                         } else {
406                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
407                                         }
408                                 }
409                         }
410                 }
411         }
412         for my $extra (sort {$a cmp $b} keys(%extras)) {
413                 my $text = $extras{$extra};
414                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
415                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
416                 } else {
417                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
418                 }
419         }
420         return keys(%extras) if wantarray; # Can be used by manage_extras.
421 }
422
423 sub enable_extras (@) {
424         my (@extras) = @_;
425         for my $extra (@extras) {
426                 my $extrapath = "src/modules/extra/$extra";
427                 if (!-e $extrapath) {
428                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
429                         next;
430                 }
431                 my $source = "src/modules/$extra";
432                 if (-e $source) {
433                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
434                         next;
435                 }
436                 # Get dependencies, and add them to be processed.
437                 my @deps = split /\s+/, get_property($extrapath, 'ModDep');
438                 for my $dep (@deps) {
439                         next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
440                         if (!-e "src/modules/$dep" && !-e "include/$dep") {
441                                 if (-e "src/modules/extra/$dep") {
442                                         print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
443                                         push @extras, $dep;
444                                 } else {
445                                         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";
446                                 }
447                         }
448                 }
449                 print "Enabling $extra ... \n";
450                 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
451         }
452 }
453
454 sub disable_extras (@)
455 {
456         opendir my $dd, "src/modules/extra/";
457         my @files = readdir($dd);
458         closedir $dd;
459         my (@extras) = @_;
460 EXTRA:  for my $extra (@extras) {
461                 my $extrapath = "src/modules/extra/$extra";
462                 my $source = "src/modules/$extra";
463                 if (!-e $extrapath) {
464                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
465                         next;
466                 }
467                 if ((! -l $source) || readlink($source) ne "extra/$extra") {
468                         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";
469                         next;
470                 }
471                 # Check if anything needs this.
472                 for my $file (@files) {
473                         my @deps = split /\s+/, get_property("src/modules/extra/$file", 'ModDep');
474                         # File depends on this extra...
475                         if (scalar(grep { $_ eq $extra } @deps) > 0) {
476                                 # And is both enabled and not about to be disabled.
477                                 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
478                                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
479                                         next EXTRA;
480                                 }
481                         }
482                 }
483                 # Now remove.
484                 print "Disabling $extra ... \n";
485                 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
486         }
487 }