]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - configure
Refactor duplicate test code into a run_test subroutine.
[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::Copy ();
37 use File::Spec::Functions qw(rel2abs);
38 use Cwd;
39 use Getopt::Long;
40
41 use make::configure;
42 use make::utilities;
43
44 our ($opt_use_gnutls, $opt_use_openssl, $opt_nointeractive, $opt_socketengine, $opt_freebsd_port,
45      $opt_system, $opt_uid, $opt_base_dir, $opt_config_dir, $opt_module_dir, $opt_binary_dir,
46      $opt_data_dir, $opt_log_dir);
47
48 sub list_extras ();
49
50 sub enable_extras (@);
51
52 sub disable_extras (@);
53
54 my @opt_enableextras;
55 my @opt_disableextras;
56
57 GetOptions (
58         'enable-gnutls' => \$opt_use_gnutls,
59         'system' => \$opt_system,
60         'uid=s' => \$opt_uid,
61         'enable-openssl' => \$opt_use_openssl,
62         'disable-interactive' => \$opt_nointeractive,
63         'socketengine=s' => \$opt_socketengine,
64         'enable-freebsd-ports-openssl' => \$opt_freebsd_port,
65         'prefix=s' => \$opt_base_dir,
66         'config-dir=s' => \$opt_config_dir,
67         'module-dir=s' => \$opt_module_dir,
68         'binary-dir=s' => \$opt_binary_dir,
69         'data-dir=s' => \$opt_data_dir,
70         'log-dir=s' => \$opt_log_dir,
71         'help'  => sub { showhelp(); },
72         'update' => sub { update(); },
73         'clean' => sub { clean(); },
74         'list-extras' => sub { list_extras; exit 0; }, # This, --enable-extras, and --disable-extras are for non-interactive managing.
75         'enable-extras=s@' => \@opt_enableextras, # ^
76         'disable-extras=s@' => \@opt_disableextras, # ^
77 );
78
79 if (scalar(@opt_enableextras) + scalar(@opt_disableextras) > 0) {
80         @opt_enableextras = split /,/, join(',', @opt_enableextras);
81         @opt_disableextras = split /,/, join(',', @opt_disableextras);
82         enable_extras(@opt_enableextras);
83         disable_extras(@opt_disableextras);
84         list_extras;
85         print "Remember: YOU are responsible for making sure any libraries needed have been installed!\n";
86         exit 0;
87 }
88
89 our $interactive = !(
90         (defined $opt_base_dir) ||
91         (defined $opt_config_dir) ||
92         (defined $opt_module_dir) ||
93         (defined $opt_base_dir) ||
94         (defined $opt_binary_dir) ||
95         (defined $opt_data_dir) ||
96         (defined $opt_log_dir) ||
97         (defined $opt_nointeractive) ||
98         (defined $opt_socketengine) ||
99         (defined $opt_use_openssl) ||
100         (defined $opt_system) ||
101         (defined $opt_uid) ||
102         (defined $opt_use_gnutls) ||
103         (defined $opt_freebsd_port)
104 );
105
106 our $topdir = getcwd();
107 our @modlist = ();
108 our %config = ();
109 our $cache_loaded = getcache();
110
111 $config{BASE_DIR} = $topdir."/run";
112
113 if (defined $opt_base_dir) {
114         $config{BASE_DIR} = $opt_base_dir;
115 } elsif (defined $opt_system) {
116         $config{BASE_DIR} = '/var/lib/inspircd';
117 }
118
119 if (defined $opt_system) {
120         $config{UID} = $opt_uid || 'ircd';
121         $config{CONFIG_DIR} = '/etc/inspircd';
122         $config{MODULE_DIR} = '/usr/lib/inspircd';
123         $config{BINARY_DIR} = '/usr/sbin/';
124         $config{BUILD_DIR} = $topdir."/build";
125         $config{DATA_DIR} = '/var/inspircd';
126         $config{LOG_DIR} = '/var/log/inspircd';
127 } else {
128         $config{UID} = $opt_uid || $<;
129         $config{CONFIG_DIR} = rel2abs($config{BASE_DIR}."/conf");
130         $config{MODULE_DIR} = rel2abs($config{BASE_DIR}."/modules");
131         $config{BINARY_DIR} = rel2abs($config{BASE_DIR}."/bin");
132         $config{BUILD_DIR} = rel2abs($topdir."/build");
133         $config{DATA_DIR} = rel2abs($config{BASE_DIR}."/data");
134         $config{LOG_DIR} = rel2abs($config{BASE_DIR}."/logs");  
135 }
136
137 if (defined $opt_config_dir) {
138         $config{CONFIG_DIR} = $opt_config_dir;
139 }
140 if (defined $opt_module_dir) {
141         $config{MODULE_DIR} = $opt_module_dir;
142 }
143 if (defined $opt_binary_dir) {
144         $config{BINARY_DIR} = $opt_binary_dir;
145 }
146 if (defined $opt_data_dir) {
147         $config{DATA_DIR} = $opt_data_dir;
148 }
149 if (defined $opt_log_dir) {
150         $config{LOG_DIR} = $opt_log_dir;
151 }
152 chomp($config{HAS_GNUTLS}   = `pkg-config --modversion gnutls 2>/dev/null`); # GNUTLS Version.
153
154 if (defined $opt_freebsd_port)
155 {
156         chomp($config{HAS_OPENSSL} = `pkg-config --modversion openssl 2>/dev/null`);
157         chomp($config{HAS_OPENSSL_PORT}  = `pkg-config --modversion openssl 2>/dev/null`);
158         $config{USE_FREEBSD_BASE_SSL} = "n";
159 }
160 else
161 {
162         if ($^O eq "freebsd")
163         {
164                 # default: use base ssl
165                 chomp($config{HAS_OPENSSL} = `openssl version | cut -d ' ' -f 2`);                      # OpenSSL version, freebsd specific
166                 chomp($config{HAS_OPENSSL_PORT}  = `pkg-config --modversion openssl 2>/dev/null`);      # Port version, may be different
167         }
168         else
169         {
170                 chomp($config{HAS_OPENSSL}  = `pkg-config --modversion openssl 2>/dev/null`);           # Openssl version, others
171                 $config{HAS_OPENSSL_PORT} = "";
172         }
173 }
174
175 chomp(our $gnutls_ver = $config{HAS_GNUTLS});
176 chomp(our $openssl_ver = $config{HAS_OPENSSL});
177 $config{USE_GNUTLS}         = "n";
178 if (defined $opt_use_gnutls)
179 {
180         $config{USE_GNUTLS} = "y";                                      # Use gnutls.
181 }
182 $config{USE_OPENSSL}    = "n";                                          # Use openssl.
183 if (defined $opt_use_openssl)
184 {
185         $config{USE_OPENSSL} = "y";
186 }
187
188 $config{STARTSCRIPT} = $^O eq 'darwin' ? 'org.inspircd.plist' : 'inspircd';
189
190 $config{CXX} = defined $ENV{CXX} && !system("$ENV{CXX} -v > /dev/null 2>&1") ? $ENV{CXX} : find_compiler();
191 if ($config{CXX} eq "") {
192         print "A C++ compiler could not be detected on your system!\n";
193         print "Set the CXX environment variable to the full path if this is incorrect.\n";
194         exit 1; 
195 }
196
197 our %cxx = get_compiler_info($config{CXX});
198 if ($cxx{UNSUPPORTED}) {
199         print "Your C++ compiler is too old to build InspIRCd!\n";
200         print "Reason: $cxx{REASON}\n";
201         exit 1;
202 }
203
204 if ($config{HAS_OPENSSL} =~ /^([-[:digit:].]+)(?:[a-z])?(?:\-[a-z][0-9])?/) {
205         $config{HAS_OPENSSL} = $1;
206 } else {
207         $config{HAS_OPENSSL} = "";
208 }
209
210 # Get and Set some important vars..
211 getmodules();
212
213 sub clean
214 {
215         unlink(".config.cache");
216 }
217
218 sub update
219 {
220         eval {
221                 # Does the cache file exist?
222                 if (!getcache()) {
223                         # No, No it doesn't.. *BASH*
224                         print "You have not run ./configure before. Please do this before trying to run the update script.\n";
225                         exit 1;
226                 } else {
227                         # We've Loaded the cache file and all our variables..
228                         $topdir = getcwd();
229                         getmodules();
230                         print "Updating files...\n";
231                         %cxx = get_compiler_info($config{CXX});
232                         writefiles();
233                         makecache();
234                         print "Complete.\n";
235                         exit;
236                 }
237         };
238         if ($@)
239         {
240                 print "Configure update failed: $@\n";
241         }
242         exit;
243 }
244
245 print "Running non-interactive configure...\n" unless $interactive;
246 print "Checking for cache from previous configure... ";
247 print ($cache_loaded ? "found\n" : "not found\n");
248
249 $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', '-lrt');
250 $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
251
252 if ($config{HAS_EPOLL} = run_test 'epoll', test_header($config{CXX}, 'sys/epoll.h')) {
253         $config{SOCKETENGINE} ||= 'epoll';
254 }
255
256 if ($config{HAS_KQUEUE} = run_test 'kqueue', test_file($config{CXX}, 'kqueue.cpp')) {
257         $config{SOCKETENGINE} ||= 'kqueue';
258 }
259
260 if ($config{HAS_PORTS} = run_test 'Solaris IOCP', test_header($config{CXX}, 'port.h')) {
261         $config{SOCKETENGINE} ||= 'ports';
262 }
263
264 if ($config{HAS_POLL} = run_test 'poll', test_header($config{CXX}, 'poll.h')) {
265         $config{SOCKETENGINE} ||= 'poll';
266 }
267
268 # Select is available on all platforms
269 $config{HAS_SELECT} = 1;
270 $config{SOCKETENGINE} ||= "select";
271
272 if (defined $opt_socketengine) {
273         my $cfgkey = "HAS_" . uc $opt_socketengine;
274         if ($config{$cfgkey} && -f "src/socketengines/socketengine_$opt_socketengine.cpp") {
275                 $config{SOCKETENGINE} = $opt_socketengine;
276         } else {
277                 print "Unable to use a socket engine which is not supported on this platform ($opt_socketengine)!\n";
278                 print "Available socket engines are:";
279                 foreach (<src/socketengines/socketengine_*.cpp>) {
280                         s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
281                         print " $1" if $config{"HAS_" . uc $1};
282                 }
283                 print "\n";     
284                 exit 1;
285         }
286 }
287
288 print "Checking for libgnutls... ";
289 if (defined($config{HAS_GNUTLS}) && (($config{HAS_GNUTLS}) || ($config{HAS_GNUTLS} eq "y"))) {
290         if (defined($gnutls_ver) && ($gnutls_ver ne "")) {
291                 print "yes\n";
292                 $config{HAS_GNUTLS} = "y";
293         } else {
294                 print "no\n";
295                 $config{HAS_GNUTLS} = "n";
296         }
297 } else {
298         print "no\n";
299         $config{HAS_GNUTLS} = "n";
300 }
301
302 print "Checking for openssl... ";
303 if (defined($config{HAS_OPENSSL}) && (($config{HAS_OPENSSL}) || ($config{HAS_OPENSSL} eq "y"))) {
304         if (defined($openssl_ver) && ($openssl_ver ne "")) {
305                 print "yes\n";
306                 $config{HAS_OPENSSL} = "y";
307         } else {
308                 print "no\n";
309                 $config{HAS_OPENSSL} = "n";
310         }
311 } else {
312         print "no\n";
313         $config{HAS_OPENSSL} = "n";
314 }
315
316 if ($interactive)
317 {
318         # Clear the screen.
319         system 'tput', 'clear';
320
321         my $revision = getrevision();
322         chomp(my $version = `sh src/version.sh`);
323
324         # Display Introduction Message..
325         print <<"STOP" ;
326 Welcome to the \e[1mInspIRCd\e[0m Configuration program! (\e[1minteractive mode\e[0m)
327 \e[1mPackage maintainers: Type ./configure --help for non-interactive help\e[0m
328
329 *** If you are unsure of any of these values, leave it blank for    ***
330 *** standard settings that will work, and your server will run      ***
331 *** using them. Please consult your IRC network admin if in doubt.  ***
332
333 Press \e[1m<RETURN>\e[0m to accept the default for any option, or enter
334 a new value. Please note: You will \e[1mHAVE\e[0m to read the docs
335 dir, otherwise you won't have a config file!
336
337 Your operating system is: \e[1;32m$^O\e[0m 
338 STOP
339         print "Your InspIRCd version is: \e[1;32m";
340         print $revision eq 'release' ? substr($version, 9) : substr($revision, 1);
341         print "\e[0m\n\n";
342         print "The following compiler has been detected: \e[1;32m$cxx{NAME} $cxx{VERSION}\e[0m ($config{CXX})\n\n";
343
344         # Directory Settings..
345         my $tmpbase = $config{BASE_DIR};
346         dir_check("do you wish to install the InspIRCd base", "BASE_DIR");
347         if ($tmpbase ne $config{BASE_DIR}) {
348                 $config{CONFIG_DIR} = rel2abs($config{BASE_DIR}."/conf");
349                 $config{MODULE_DIR} = rel2abs($config{BASE_DIR}."/modules");
350                 $config{DATA_DIR} = rel2abs($config{BASE_DIR}."/data");
351                 $config{LOG_DIR} = rel2abs($config{BASE_DIR}."/logs");
352                 $config{BINARY_DIR} = rel2abs($config{BASE_DIR}."/bin");
353         }
354
355         dir_check("are the configuration files", "CONFIG_DIR");
356         dir_check("are the modules to be compiled to", "MODULE_DIR");
357         dir_check("is the IRCd binary to be placed", "BINARY_DIR");
358         dir_check("are variable data files to be located in", "DATA_DIR");
359         dir_check("are the logs to be stored in", "LOG_DIR");
360         dir_check("do you want the build to take place", "BUILD_DIR");
361                 
362         my $chose_hiperf = 0;
363         if ($config{HAS_KQUEUE}) {
364                 $config{USE_KQUEUE} = "y";
365                 yesno('USE_KQUEUE',"You are running a BSD operating system, and kqueue\nwas detected. Would you like to enable kqueue support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable kqueue?");
366                 print "\n";
367                 if ($config{USE_KQUEUE} eq "y") {
368                         $config{SOCKETENGINE} = "kqueue";
369                         $chose_hiperf = 1;
370                 }
371         }
372         if ($config{HAS_EPOLL}) {
373                 $config{USE_EPOLL} = "y";
374                 yesno('USE_EPOLL',"You are running a Linux 2.6+ operating system, and epoll\nwas detected. Would you like to enable epoll support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable epoll?");
375                 print "\n";
376                 if ($config{USE_EPOLL} eq "y") {
377                         $config{SOCKETENGINE} = "epoll";
378                         $chose_hiperf = 1;
379                 }
380         }
381         if ($config{HAS_PORTS}) {
382                 $config{USE_PORTS} = "y";
383                 yesno('USE_PORTS',"You are running Solaris 10.\nWould you like to enable I/O completion ports support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable support for I/O completion ports?");
384                 print "\n";
385                 if ($config{USE_PORTS} eq "y") {
386                         $config{SOCKETENGINE} = "ports";
387                         $chose_hiperf = 1;
388                 }
389         }
390
391         if (!$chose_hiperf && $config{HAS_POLL}) {
392                 $config{USE_POLL} = "y";
393                 yesno('USE_POLL', "Would you like to use poll?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable poll?");
394                 if ($config{USE_POLL} eq "y") {
395                         $config{SOCKETENGINE} = "poll";
396                 }
397         }
398         if (!$chose_hiperf && $config{USE_POLL} ne "y")
399         {
400                 print "No high-performance socket engines are available, or you chose\n";
401                 print "not to enable one. Defaulting to select() engine.\n\n";
402                 $config{SOCKETENGINE} = "select";
403         }
404
405         $config{USE_FREEBSD_BASE_SSL} = "n";
406         $config{USE_FREEBSD_PORTS_SSL} = "n";
407         if ($config{HAS_OPENSSL_PORT} ne "")
408         {
409                 $config{USE_FREEBSD_PORTS_SSL} = "y";
410                 print "I have detected the OpenSSL FreeBSD port installed on your system,\n";
411                 print "version \e[1;32m".$config{HAS_OPENSSL_PORT}."\e[0m. Your base system OpenSSL is version \e[1;32m".$openssl_ver."\e[0m.\n\n";
412                 yesno('USE_FREEBSD_PORTS_SSL', "Do you want to use the FreeBSD ports version?");
413                 print "\n";
414                 $config{USE_FREEBSD_BASE_SSL} = "y" if ($config{USE_FREEBSD_PORTS_SSL} eq "n");
415
416                 if ($config{USE_FREEBSD_BASE_SSL} eq "n")
417                 {
418                         # update to port version
419                         $openssl_ver = $config{HAS_OPENSSL_PORT};
420                 }
421         }
422         else
423         {
424                 $config{USE_FREEBSD_BASE_SSL} = "y" if ($^O eq "freebsd");
425         }
426
427         $config{USE_SSL} = "n";
428
429         if ($config{HAS_GNUTLS} eq "y" || $config{HAS_OPENSSL} eq "y")
430         {
431                 print "Detected GnuTLS version: \e[1;32m" . $gnutls_ver . "\e[0m\n";
432                 print "Detected OpenSSL version: \e[1;32m" . $openssl_ver . "\e[0m\n\n";
433
434                 yesno('USE_SSL', "One or more SSL libraries detected. Would you like to enable SSL support?");
435                 if ($config{USE_SSL} eq "y")
436                 {
437                         if ($config{HAS_GNUTLS} eq "y")
438                         {
439                                 yesno('USE_GNUTLS',"Would you like to enable SSL with m_ssl_gnutls? (recommended)");
440                                 if ($config{USE_GNUTLS} eq "y")
441                                 {
442                                         print "\nUsing GnuTLS SSL module.\n";
443                                 }
444                         }
445
446                         if ($config{HAS_OPENSSL} eq "y")
447                         {
448                                 yesno('USE_OPENSSL', "Would you like to enable SSL with m_ssl_openssl?");
449                                 if ($config{USE_OPENSSL} eq "y")
450                                 {
451                                         print "\nUsing OpenSSL SSL module.\n";
452                                 }
453                         }
454                 }
455         }
456         else
457         {
458                 print "\nCould not detect OpenSSL or GnuTLS. Make sure pkg-config is installed and\n";
459                 print "is in your path.\n\n";
460         }
461 }
462
463 # We are on a POSIX system, we can enable POSIX extras without asking
464 symlink "extra/m_regex_posix.cpp", "src/modules/m_regex_posix.cpp";
465
466 if (($config{USE_GNUTLS} eq "y") && ($config{HAS_GNUTLS} ne "y"))
467 {
468         print "Sorry, but i couldn't detect gnutls. Make sure gnutls-config is in your path.\n";
469         exit 1;
470 }
471 if (($config{USE_OPENSSL} eq "y") && ($config{HAS_OPENSSL} ne "y"))
472 {
473         print "Sorry, but i couldn't detect openssl. Make sure openssl is in your path.\n";
474         exit 1;
475 }
476
477 $config{CERTGEN} ||= 'y';
478 yesno('CERTGEN',"Would you like generate SSL certificates now?") if ($interactive && ($config{USE_GNUTLS} eq "y" || $config{USE_OPENSSL} eq "y"));
479
480 if ($config{USE_GNUTLS} eq "y") {
481         unless (-r "src/modules/m_ssl_gnutls.cpp") {
482                 print "Symlinking src/modules/m_ssl_gnutls.cpp from extra/\n";
483                 symlink "extra/m_ssl_gnutls.cpp", "src/modules/m_ssl_gnutls.cpp" or print STDERR "Symlink failed: $!";
484         }
485         if ($interactive && $config{CERTGEN} eq 'y')
486         {
487                 unless (-r "$config{CONFIG_DIR}/key.pem" && -r "$config{CONFIG_DIR}/cert.pem" && -r "$config{CONFIG_DIR}/dhparams.pem") {
488                         unless (system './tools/genssl gnutls') {
489                                 print "\nCertificate generation complete, copying to config directory... ";
490                                 File::Copy::move("key.pem", "$config{CONFIG_DIR}/key.pem") or print STDERR "Could not copy key.pem!\n";
491                                 File::Copy::move("cert.pem", "$config{CONFIG_DIR}/cert.pem") or print STDERR "Could not copy cert.pem!\n";
492                                 File::Copy::move("dhparams.pem", "$config{CONFIG_DIR}/dhparams.pem") or print STDERR "Could not copy dhparams.pem!\n";
493                                 print "Done.\n\n";
494                         }
495                 }
496                 else {
497                         print "SSL Certificates found, skipping.\n\n";
498                 }
499         }
500         else
501         {
502                 print "Skipping SSL certificate generation\nin non-interactive mode.\n\n";
503         }
504 }
505
506 if ($config{USE_OPENSSL} eq "y") {
507         unless (-r "src/modules/m_ssl_openssl.cpp") {
508                 print "Symlinking src/modules/m_ssl_openssl.cpp from extra/\n";
509                 symlink "extra/m_ssl_openssl.cpp", "src/modules/m_ssl_openssl.cpp" or print STDERR "Symlink failed: $!";
510         }
511         if ($interactive && $config{CERTGEN} eq 'y')
512         {
513                 unless (-r "$config{CONFIG_DIR}/key.pem" && -r "$config{CONFIG_DIR}/cert.pem" && -r "$config{CONFIG_DIR}/dhparams.pem") {
514                         unless (system './tools/genssl openssl') {
515                                 print "\nCertificate generation complete, copying to config directory... ";
516                                 File::Copy::move("key.pem", "$config{CONFIG_DIR}/key.pem") or print STDERR "Could not copy key.pem!\n";
517                                 File::Copy::move("cert.pem", "$config{CONFIG_DIR}/cert.pem") or print STDERR "Could not copy cert.pem!\n";
518                                 File::Copy::move("dhparams.pem", "$config{CONFIG_DIR}/dhparams.pem") or print STDERR "Could not copy dhparams.pem!\n";
519                                 print "Done.\n\n";
520                         }
521                 } else {
522                         print "SSL Certificates found, skipping.\n\n"
523                 }
524         }
525         else
526         {
527                 print "Skipping SSL certificate generation\nin non-interactive mode.\n\n";
528         }
529 }
530 if (($config{USE_GNUTLS} eq "n") && ($config{USE_OPENSSL} eq "n")) {
531         print "Skipping SSL Certificate generation, SSL support is not available.\n\n";
532 }
533
534 depcheck();
535 writefiles();
536 makecache();
537 dumphash();
538
539 print "\n";
540 print "To build your server with these settings, please run '\e[1;32mmake\e[0m' now.\n";
541 if (($config{USE_GNUTLS} eq "y") || ($config{USE_OPENSSL} eq "y")) {
542         print "Please note: for \e[1;32mSSL support\e[0m you will need to load required\n";
543         print "modules in your config. This configure script has added those modules to the\n";
544         print "build process. For more info please refer to:\n";
545         print "\e[1;32mhttp://wiki.inspircd.org/Installation_From_Tarball\e[0m\n";
546 }
547 print "*** \e[1;32mRemember to edit your configuration files!!!\e[0m ***\n\n";
548
549 sub getcache {
550         # Retrieves the .config.cache file, and loads values into the main config hash.
551         open(CACHE, ".config.cache") or return 0;
552         while (<CACHE>) {
553                 chomp;
554                 # Ignore Blank lines, and comments..
555                 next if /^\s*$/;
556                 next if /^\s*#/;
557                 my ($key, $value) = split("=", $_, 2);
558                 $value =~ /^\"(.*)\"$/;
559                 # Do something with data here!
560                 $config{$key} = $1;
561         }
562         close(CACHE);
563         return 1;
564 }
565
566 sub makecache {
567         # Dump the contents of %config
568         print "Writing \e[1;32mcache file\e[0m for future ./configures ...\n";
569         open(FILEHANDLE, ">.config.cache");
570         foreach my $key (keys %config) {
571                 print FILEHANDLE "$key=\"$config{$key}\"\n";
572         }
573         close(FILEHANDLE);
574 }
575
576 sub dir_check {
577         my ($desc, $hash_key) = @_;
578         my $complete = 0;
579         while (!$complete) {
580                 print "In what directory $desc?\n";
581                 print "[\e[1;32m$config{$hash_key}\e[0m] -> ";
582                 chomp(my $var = <STDIN>);
583                 if ($var eq "") {
584                         $var = $config{$hash_key};
585                 }
586                 if ($var =~ /^\~\/(.+)$/) {
587                         # Convert it to a full path..
588                         $var = rel2abs($ENV{HOME} . "/" . $1);
589                 }
590                 $var = rel2abs($var);
591                 if (! -e $var) {
592                         print "$var does not exist. Create it?\n[\e[1;32my\e[0m] ";
593                         chomp(my $tmp = <STDIN>);
594                         if (($tmp eq "") || ($tmp =~ /^y/i)) {
595                                 # Attempt to Create the Dir..
596                                 my $chk = eval {
597                                         use File::Path ();
598                                         File::Path::mkpath($var, 0, 0777);
599                                         1;
600                                 };
601                                 unless (defined($chk) && -d $var) {
602                                         print "Unable to create directory. ($var)\n\n";
603                                         # Restart Loop..
604                                         next;
605                                 }
606                         } else {
607                                 # They said they don't want to create, and we can't install there.
608                                 print "\n\n";
609                                 next;
610                         }
611                 } else {
612                         if (! -d $var) {
613                                 # Target exists, but is not a directory.
614                                 print "File $var exists, but is not a directory.\n\n";
615                                 next;
616                         }
617                 }
618                 # Either Dir Exists, or was created fine.
619                 $config{$hash_key} = $var;
620                 $complete = 1;
621                 print "\n";
622         }
623 }
624
625 sub writefiles {
626         chomp(my $incos = `uname -n -s -r`);
627         chomp(my $version = `sh src/version.sh`);
628         my $revision = getrevision();
629         my $branch = "InspIRCd-0.0";
630         if ($version =~ /^(InspIRCd-[0-9]+\.[0-9]+)\.[0-9]+/)
631         {
632                 $branch = $1;
633         }
634         print "Writing \e[1;32mconfig.h\e[0m\n";
635         open(FILEHANDLE, ">include/config.h.tmp");
636         print FILEHANDLE <<EOF;
637 /* Auto generated by configure, do not modify! */
638 #pragma once
639
640 #define BRANCH "$branch"
641 #define VERSION "$version"
642 #define REVISION "$revision"
643 #define SYSTEM "$incos"
644
645 #define CONFIG_PATH "$config{CONFIG_DIR}"
646 #define DATA_PATH "$config{DATA_DIR}"
647 #define LOG_PATH "$config{LOG_DIR}"
648 #define MOD_PATH "$config{MODULE_DIR}"
649
650 EOF
651
652         if ($config{HAS_EVENTFD}) {
653                 print FILEHANDLE "#define HAS_EVENTFD\n";
654         }
655         if ($config{HAS_CLOCK_GETTIME}) {
656                 print FILEHANDLE "#define HAS_CLOCK_GETTIME\n";
657         }
658
659         print FILEHANDLE "\n#include \"threadengines/threadengine_pthread.h\"\n";
660         close(FILEHANDLE);
661
662         unlink 'include/config.h';
663         rename 'include/config.h.tmp', 'include/config.h';
664
665         # Do this once here, and cache it in the .*.inc files,
666         # rather than attempting to read src/version.sh from
667         # compiled code -- we might not have the source to hand.
668
669         my @dotfiles = qw(main.mk inspircd);
670         push @dotfiles, 'org.inspircd.plist' if $^O eq 'darwin';
671
672         foreach my $file (@dotfiles) {
673                 open(FILEHANDLE, "make/template/$file") or die "Can't open make/template/$file: $!";
674                 $_ = join '', <FILEHANDLE>;
675                 close(FILEHANDLE);
676
677                 $config{BUILD_DIR} ||= rel2abs($topdir."/build");
678                 $config{COMPILER} = lc $cxx{NAME};
679                 $config{SYSTEM} = lc $^O;
680
681                 for my $var (qw(
682                         CXX COMPILER SYSTEM BASE_DIR CONFIG_DIR MODULE_DIR BINARY_DIR BUILD_DIR DATA_DIR UID
683                         STARTSCRIPT SOCKETENGINE
684                 )) {
685                         s/\@$var\@/$config{$var}/g;
686                 }
687
688                 s/\@VERSION\@/$version/ if defined $version;
689
690                 if ($file eq 'main.mk') {
691                         print "Writing \e[1;32mGNUmakefile\e[0m ...\n";
692
693                         my $mk_tmp = $_;
694                         s/\@IFDEF (\S+)/ifdef $1/g;
695                         s/\@IFNDEF (\S+)/ifndef $1/g;
696                         s/\@IFEQ (\S+) (\S+)/ifeq ($1,$2)/g;
697                         s/\@IFNEQ (\S+) (\S+)/ifneq ($1,$2)/g;
698                         s/\@ELSIFEQ (\S+) (\S+)/else ifeq ($1,$2)/g;
699                         s/\@ELSE/else/g;
700                         s/\@ENDIF/endif/g;
701                         s/ *\@BSD_ONLY .*\n//g;
702                         s/\@GNU_ONLY //g;
703                         s/\@DO_EXPORT (.*)/export $1/g;
704                         open MKF, '>GNUmakefile' or die "Can't write to GNUmakefile: $!";
705                         print MKF $_;
706                         close MKF;
707
708                         print "Writing \e[1;32mBSDmakefile\e[0m ...\n";
709                         $_ = $mk_tmp;
710                         s/\@IFDEF (\S+)/.if defined($1)/g;
711                         s/\@IFNDEF (\S+)/.if !defined($1)/g;
712                         s/\@IFEQ (\S+) (\S+)/.if $1 == $2/g;
713                         s/\@IFNEQ (\S+) (\S+)/.if $1 != $2/g;
714                         s/\@ELSIFEQ (\S+) (\S+)/.elif $1 == $2/g;
715                         s/\@ELSE/.else/g;
716                         s/\@ENDIF/.endif/g;
717                         s/\@BSD_ONLY //g;
718                         s/ *\@GNU_ONLY .*\n//g;
719                         $mk_tmp = $_;
720                         $mk_tmp =~ s#\@DO_EXPORT (.*)#"MAKEENV += ".join ' ', map "$_='\${$_}'", split /\s/, $1#eg;
721                         open MKF, '>BSDmakefile' or die "Can't write to BSDmakefile: $!";
722                         print MKF $mk_tmp;
723                         close MKF;
724                 } else {
725                         print "Writing \e[1;32m$file\e[0m ...\n";
726                         open(FILEHANDLE, ">$file") or die("Can't write to $file: $!\n");
727                         print FILEHANDLE $_;
728                         close(FILEHANDLE);
729                 }
730         }
731
732         chmod 0755, 'inspircd';
733 }
734
735 sub depcheck
736 {
737         getmodules();
738         for my $mod (@modlist) {
739                 getcompilerflags("src/modules/m_$mod.cpp");
740                 getlinkerflags("src/modules/m_$mod.cpp");
741         }
742 }
743
744 # Routine to list out the extra/ modules that have been enabled.
745 # Note: when getting any filenames out and comparing, it's important to lc it if the
746 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
747 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
748 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
749 sub list_extras () {
750         use File::Spec;
751         # @_ not used
752         my $srcdir = File::Spec->catdir("src", "modules");
753         my $abs_srcdir = File::Spec->rel2abs($srcdir);
754         local $_;
755         my $dd;
756         opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
757         my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
758         closedir $dd;
759         undef $dd;
760         opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
761         my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
762         closedir $dd;
763         undef $dd;
764         my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
765         my %extras = ();
766 EXTRA:  for my $extra (@extras) {
767                 next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
768                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
769                 my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
770                 next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
771                 if (-l $abs_source) {
772                         # Symlink, is it in the right place?
773                         my $targ = readlink($abs_source);
774                         my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
775                         if ($abs_targ eq $abs_extra) {
776                                 $extras{$extra} = "\e[32;1menabled\e[0m";
777                         } else {
778                                 $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
779                         }
780                 } elsif (-e $abs_source) {
781                         my ($devext, $inoext) = stat($abs_extra);
782                         my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
783                         if ($lnksrc > 1) {
784                                 if ($devsrc == $devext && $inosrc == $inoext) {
785                                         $extras{$extra} = "\e[32;1menabled\e[0m";
786                                 } else {
787                                         $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
788                                 }
789                         } else {
790                                 open my $extfd, "<", $abs_extra;
791                                 open my $srcfd, "<", $abs_source;
792                                 local $/ = undef;
793                                 if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
794                                         $extras{$extra} = "\e[32;1menabled\e[0m";
795                                 } else {
796                                         $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
797                                 }
798                         }
799                 } else {
800                         $extras{$extra} = "\e[33;1mdisabled\e[0m";
801                 }
802         }
803         # Now let's add dependency info
804         for my $extra (keys(%extras)) {
805                 next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
806                 my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
807                 my @deps = split / +/, getdependencies($abs_extra);
808                 for my $dep (@deps) {
809                         if (exists($extras{$dep})) {
810                                 my $ref = \$extras{$dep}; # Take reference.
811                                 if ($$ref !~ m/needed by/) {
812                                         # First dependency found.
813                                         if ($$ref =~ m/enabled/) {
814                                                 $$ref .= " (needed by \e[32;1m$extra\e[0m";
815                                         } else {
816                                                 $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
817                                                 $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
818                                         }
819                                 } else {
820                                         if ($$ref =~ m/enabled/) {
821                                                 $$ref .= ", \e[32;1m$extra\e[0m";
822                                         } else {
823                                                 $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
824                                         }
825                                 }
826                         }
827                 }
828         }
829         for my $extra (sort {$a cmp $b} keys(%extras)) {
830                 my $text = $extras{$extra};
831                 if ($text =~ m/needed by/ && $text !~ m/enabled/) {
832                         printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
833                 } else {
834                         printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
835                 }
836         }
837         return keys(%extras) if wantarray; # Can be used by manage_extras.
838 }
839
840 sub enable_extras (@) {
841         my (@extras) = @_;
842         for my $extra (@extras) {
843                 my $extrapath = "src/modules/extra/$extra";
844                 if (!-e $extrapath) {
845                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
846                         next;
847                 }
848                 my $source = "src/modules/$extra";
849                 if (-e $source) {
850                         print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
851                         next;
852                 }
853                 # Get dependencies, and add them to be processed.
854                 my @deps = split / +/, getdependencies($extrapath);
855                 for my $dep (@deps) {
856                         next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
857                         if (!-e "src/modules/$dep" && !-e "include/$dep") {
858                                 if (-e "src/modules/extra/$dep") {
859                                         print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
860                                         push @extras, $dep;
861                                 } else {
862                                         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";
863                                 }
864                         }
865                 }
866                 print "Enabling $extra ... \n";
867                 symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
868         }
869 }
870
871 sub disable_extras (@)
872 {
873         opendir my $dd, "src/modules/extra/";
874         my @files = readdir($dd);
875         closedir $dd;
876         my (@extras) = @_;
877 EXTRA:  for my $extra (@extras) {
878                 my $extrapath = "src/modules/extra/$extra";
879                 my $source = "src/modules/$extra";
880                 if (!-e $extrapath) {
881                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
882                         next;
883                 }
884                 if ((! -l $source) || readlink($source) ne "extra/$extra") {
885                         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";
886                         next;
887                 }
888                 # Check if anything needs this.
889                 for my $file (@files) {
890                         my @deps = split / +/, getdependencies("src/modules/extra/$file");
891                         # File depends on this extra...
892                         if (scalar(grep { $_ eq $extra } @deps) > 0) {
893                                 # And is both enabled and not about to be disabled.
894                                 if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
895                                         print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
896                                         next EXTRA;
897                                 }
898                         }
899                 }
900                 # Now remove.
901                 print "Disabling $extra ... \n";
902                 unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
903         }
904 }