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