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