configure 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #!/usr/bin/env perl
  2. #
  3. # InspIRCd -- Internet Relay Chat Daemon
  4. #
  5. # Copyright (C) 2012-2017 Peter Powell <petpow@saberuk.com>
  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. BEGIN {
  28. require 5.10.0;
  29. }
  30. use feature ':5.10';
  31. use strict;
  32. use warnings FATAL => qw(all);
  33. use File::Basename qw(basename);
  34. use File::Copy ();
  35. use File::Spec::Functions qw(rel2abs);
  36. use FindBin qw($RealDir);
  37. use Getopt::Long qw(GetOptions);
  38. use POSIX qw(getgid getuid);
  39. use lib $RealDir;
  40. use make::common;
  41. use make::configure;
  42. use make::console;
  43. use make::directive;
  44. my ($opt_binary_dir,
  45. $opt_config_dir,
  46. $opt_data_dir,
  47. $opt_development,
  48. $opt_disable_auto_extras,
  49. $opt_disable_interactive,
  50. $opt_distribution_label,
  51. $opt_example_dir,
  52. $opt_gid,
  53. $opt_log_dir,
  54. $opt_manual_dir,
  55. $opt_module_dir,
  56. $opt_prefix,
  57. $opt_script_dir,
  58. $opt_socketengine,
  59. $opt_system,
  60. $opt_uid);
  61. sub list_extras ();
  62. sub enable_extras (@);
  63. sub disable_extras (@);
  64. my @opt_enableextras;
  65. my @opt_disableextras;
  66. exit 1 unless GetOptions(
  67. 'clean' => \&cmd_clean,
  68. 'help' => \&cmd_help,
  69. 'update' => \&cmd_update,
  70. 'binary-dir=s' => \$opt_binary_dir,
  71. 'config-dir=s' => \$opt_config_dir,
  72. 'data-dir=s' => \$opt_data_dir,
  73. 'development' => \$opt_development,
  74. 'disable-auto-extras' => \$opt_disable_auto_extras,
  75. 'disable-interactive' => \$opt_disable_interactive,
  76. 'distribution-label=s' => \$opt_distribution_label,
  77. 'example-dir=s' => \$opt_example_dir,
  78. 'gid=s' => \$opt_gid,
  79. 'log-dir=s' => \$opt_log_dir,
  80. 'manual-dir=s' => \$opt_manual_dir,
  81. 'module-dir=s' => \$opt_module_dir,
  82. 'prefix=s' => \$opt_prefix,
  83. 'script-dir=s' => \$opt_script_dir,
  84. 'socketengine=s' => \$opt_socketengine,
  85. 'system' => \$opt_system,
  86. 'uid=s' => \$opt_uid,
  87. # TODO: when the modulemanager rewrite is done these should be removed.
  88. 'disable-extras=s@' => \@opt_disableextras,
  89. 'enable-extras=s@' => \@opt_enableextras,
  90. 'list-extras' => sub { list_extras; exit 0; },
  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. our $interactive = !(
  102. !-t STDIN ||
  103. !-t STDOUT ||
  104. defined $opt_binary_dir ||
  105. defined $opt_config_dir ||
  106. defined $opt_data_dir ||
  107. defined $opt_development ||
  108. defined $opt_disable_auto_extras ||
  109. defined $opt_disable_interactive ||
  110. defined $opt_distribution_label ||
  111. defined $opt_example_dir ||
  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_script_dir ||
  118. defined $opt_socketengine ||
  119. defined $opt_system ||
  120. defined $opt_uid
  121. );
  122. my %version = get_version $opt_distribution_label;
  123. print_format "<|BOLD Configuring InspIRCd $version{FULL} on $^O.|>\n";
  124. my %config;
  125. if ($interactive) {
  126. %config = read_config_file(CONFIGURE_CACHE_FILE);
  127. run_test CONFIGURE_CACHE_FILE, %config;
  128. if (!defined $config{VERSION}) {
  129. $config{VERSION} = CONFIGURE_CACHE_VERSION;
  130. } elsif ($config{VERSION} != CONFIGURE_CACHE_VERSION) {
  131. print_warning "ignoring contents of ${\CONFIGURE_CACHE_FILE} as it was generated by an incompatible version of $0!";
  132. %config = ('VERSION', CONFIGURE_CACHE_VERSION);
  133. }
  134. }
  135. $config{CXX} = find_compiler($config{CXX} // $ENV{CXX});
  136. unless ($config{CXX}) {
  137. say 'A suitable C++ compiler could not be detected on your system!';
  138. unless ($interactive) {
  139. say 'Set the CXX environment variable to the path to a C++ compiler binary if this is incorrect.';
  140. exit 1;
  141. }
  142. until ($config{CXX}) {
  143. my $compiler_path = prompt_string 1, 'Please enter the path to a C++ compiler binary:', 'c++';
  144. $config{CXX} = find_compiler $compiler_path;
  145. }
  146. }
  147. my %compiler = get_compiler_info($config{CXX});
  148. $config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
  149. $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
  150. $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');
  151. my @socketengines;
  152. push @socketengines, 'epoll' if run_test 'epoll', test_header $config{CXX}, 'sys/epoll.h';
  153. push @socketengines, 'kqueue' if run_test 'kqueue', test_file $config{CXX}, 'kqueue.cpp';
  154. push @socketengines, 'poll' if run_test 'poll', test_header $config{CXX}, 'poll.h';
  155. push @socketengines, 'select';
  156. if (defined $opt_socketengine) {
  157. unless (grep { $_ eq $opt_socketengine } @socketengines) {
  158. my $reason = -f "src/socketengines/socketengine_$opt_socketengine.cpp" ? 'is not available on this platform' : 'does not exist';
  159. print_error "The socket engine you requested ($opt_socketengine) $reason!",
  160. 'Available socket engines are:',
  161. map { " * $_" } @socketengines;
  162. }
  163. }
  164. $config{SOCKETENGINE} = $opt_socketengine // $socketengines[0];
  165. if (defined $opt_system) {
  166. $config{BASE_DIR} = $opt_prefix // '/var/lib/inspircd';
  167. $config{BINARY_DIR} = $opt_binary_dir // '/usr/sbin';
  168. $config{CONFIG_DIR} = $opt_config_dir // '/etc/inspircd';
  169. $config{DATA_DIR} = $opt_data_dir // '/var/inspircd';
  170. $config{EXAMPLE_DIR} = $opt_example_dir // '/usr/share/doc/inspircd';
  171. $config{LOG_DIR} = $opt_log_dir // '/var/log/inspircd';
  172. $config{MANUAL_DIR} = $opt_manual_dir // '/usr/share/man/man1';
  173. $config{MODULE_DIR} = $opt_module_dir // '/usr/lib/inspircd';
  174. $config{SCRIPT_DIR} = $opt_script_dir // '/usr/share/inspircd'
  175. } else {
  176. $config{BASE_DIR} = $opt_prefix // $config{BASE_DIR} // rel2abs 'run';
  177. $config{BINARY_DIR} = $opt_binary_dir // $config{BINARY_DIR} // rel2abs $config{BASE_DIR} . '/bin';
  178. $config{CONFIG_DIR} = $opt_config_dir // $config{CONFIG_DIR} // rel2abs $config{BASE_DIR} . '/conf';
  179. $config{DATA_DIR} = $opt_data_dir // $config{DATA_DIR} // rel2abs $config{BASE_DIR} . '/data';
  180. $config{EXAMPLE_DIR} = $opt_example_dir // $config{EXAMPLE_DIR} // $config{CONFIG_DIR} . '/examples';
  181. $config{LOG_DIR} = $opt_log_dir // $config{LOG_DIR} // rel2abs $config{BASE_DIR} . '/logs';
  182. $config{MANUAL_DIR} = $opt_manual_dir // $config{MANUAL_DIR} // rel2abs $config{BASE_DIR} . '/manuals';
  183. $config{MODULE_DIR} = $opt_module_dir // $config{MODULE_DIR} // rel2abs $config{BASE_DIR} . '/modules';
  184. $config{SCRIPT_DIR} = $opt_script_dir // $config{SCRIPT_DIR} // $config{BASE_DIR};
  185. }
  186. # Parse --gid=123 or --gid=foo and extract the group id.
  187. my @group;
  188. if (defined $opt_gid) {
  189. @group = $opt_gid =~ /^\d+$/ ? getgrgid($opt_gid) : getgrnam($opt_gid);
  190. print_error "there is no '$opt_gid' group on this system!" unless @group;
  191. } else {
  192. @group = $opt_system ? getgrnam('irc') : getgrgid($config{GID} // getgid());
  193. print_error "you need to specify a group to run as using '--gid [id|name]'!" unless @group;
  194. unless ($group[2]) {
  195. print_warning <<"EOW";
  196. You are building as the privileged $group[0] group and have not specified
  197. an unprivileged group to run InspIRCd as.
  198. This is almost never what you should do. You should probably either create a new
  199. unprivileged user/group to build and run as or pass the '--gid [id|name]' flag
  200. to specify an unprivileged group to run as.
  201. EOW
  202. if (!prompt_bool $interactive, "Are you sure you want to build as the $group[0] group?", 0) {
  203. say STDERR "If you are sure you want to build as the $group[0] group pass the --gid $group[2] flag." unless $interactive;
  204. exit 1;
  205. }
  206. }
  207. }
  208. $config{GROUP} = $group[0];
  209. $config{GID} = $group[2];
  210. # Parse --uid=123 or --uid=foo and extract the user id.
  211. my @user;
  212. if (defined $opt_uid) {
  213. @user = $opt_uid =~ /^\d+$/ ? getpwuid($opt_uid) : getpwnam($opt_uid);
  214. print_error "there is no '$opt_uid' user on this system!" unless @user;
  215. } else {
  216. @user = $opt_system ? getpwnam('irc') : getpwuid($config{UID} // getuid());
  217. print_error "you need to specify a user to run as using '--uid [id|name]'!" unless @user;
  218. unless ($user[2]) {
  219. print_warning <<"EOW";
  220. You are building as the privileged $user[0] user and have not specified
  221. an unprivileged user to run InspIRCd as.
  222. This is almost never what you should do. You should probably either create a new
  223. unprivileged user/group to build and run as or pass the '--uid [id|name]' flag
  224. to specify an unprivileged user to run as.
  225. EOW
  226. if (!prompt_bool $interactive, "Are you sure you want to build as the $user[0] user?", 0) {
  227. say STDERR "If you are sure you want to build as the $user[0] user pass the --uid $user[2] flag." unless $interactive;
  228. exit 1;
  229. }
  230. }
  231. }
  232. $config{USER} = $user[0];
  233. $config{UID} = $user[2];
  234. # Warn the user about clock drifting when running on OpenVZ.
  235. if (-e '/proc/user_beancounters' || -e '/proc/vz/vzaquota') {
  236. print_warning <<'EOW';
  237. You are building InspIRCd inside of an OpenVZ container. If you
  238. plan to use InspIRCd in this container then you should make sure that NTP is
  239. configured on the Hardware Node. Failure to do so may result in clock drifting!
  240. EOW
  241. }
  242. # Warn the user about OpenBSD shipping incredibly broken compilers/linkers.
  243. if ($^O eq 'openbsd') {
  244. print_warning <<'EOW';
  245. You are building InspIRCd on OpenBSD. The C++ compilers and linkers
  246. that OpenBSD ship are incredibly broken. You may have strange linker errors
  247. and crashes. Please consider using a different OS like FreeBSD/NetBSD instead.
  248. EOW
  249. }
  250. # Check that the user actually wants this version.
  251. if (defined $version{REAL_LABEL}) {
  252. print_warning <<'EOW';
  253. You are building a development version. This contains code which has
  254. not been tested as heavily and may contain various faults which could seriously
  255. affect the running of your server. It is recommended that you use a stable
  256. version instead.
  257. You can obtain the latest stable version from https://www.inspircd.org or by
  258. running `<|GREEN git checkout $(git describe --abbrev=0 --tags insp3)|>` if you are
  259. installing from Git.
  260. EOW
  261. if (!prompt_bool $interactive, 'I understand this warning and want to continue anyway.', $opt_development // 0) {
  262. say STDERR 'If you understand this warning and still want to continue pass the --development flag.' unless $interactive;
  263. exit 1;
  264. }
  265. }
  266. # Configure directory settings.
  267. my $question = <<"EOQ";
  268. Currently, InspIRCd is configured with the following paths:
  269. <|BOLD Base:|> $config{BASE_DIR}
  270. <|BOLD Binary:|> $config{BINARY_DIR}
  271. <|BOLD Config:|> $config{CONFIG_DIR}
  272. <|BOLD Data:|> $config{DATA_DIR}
  273. <|BOLD Log:|> $config{LOG_DIR}
  274. <|BOLD Manual:|> $config{MANUAL_DIR}
  275. <|BOLD Module:|> $config{MODULE_DIR}
  276. <|BOLD Script:|> $config{SCRIPT_DIR}
  277. Do you want to change these settings?
  278. EOQ
  279. if (prompt_bool $interactive, $question, 0) {
  280. my $original_base_dir = $config{BASE_DIR};
  281. $config{BASE_DIR} = prompt_dir $interactive, 'In what directory do you wish to install the InspIRCd base?', $config{BASE_DIR};
  282. foreach my $key (qw(BINARY_DIR CONFIG_DIR DATA_DIR LOG_DIR MANUAL_DIR MODULE_DIR SCRIPT_DIR)) {
  283. $config{$key} =~ s/^\Q$original_base_dir\E/$config{BASE_DIR}/;
  284. }
  285. $config{BINARY_DIR} = prompt_dir $interactive, 'In what directory should the InspIRCd binary be placed?', $config{BINARY_DIR};
  286. $config{CONFIG_DIR} = prompt_dir $interactive, 'In what directory are configuration files to be stored?', $config{CONFIG_DIR};
  287. $config{DATA_DIR} = prompt_dir $interactive, 'In what directory are variable data files to be stored?', $config{DATA_DIR};
  288. $config{LOG_DIR} = prompt_dir $interactive, 'In what directory are log files to be stored?', $config{LOG_DIR};
  289. $config{MANUAL_DIR} = prompt_dir $interactive, 'In what directory are manual pages to be placed?', $config{MANUAL_DIR};
  290. $config{MODULE_DIR} = prompt_dir $interactive, 'In what directory are modules to be placed?', $config{MODULE_DIR};
  291. $config{SCRIPT_DIR} = prompt_dir $interactive, 'In what directory are scripts to be placed?', $config{SCRIPT_DIR};
  292. $config{EXAMPLE_DIR} = $config{CONFIG_DIR} . '/examples';
  293. }
  294. # Configure module settings.
  295. $question = <<'EOQ';
  296. Currently, InspIRCd is configured to automatically enable all available extra modules.
  297. Would you like to enable extra modules manually?
  298. EOQ
  299. if (prompt_bool $interactive, $question, 0) {
  300. foreach my $extra (<src/modules/extra/m_*.cpp>) {
  301. my $module_name = basename $extra, '.cpp';
  302. if (prompt_bool $interactive, "Would you like to enable $module_name?", 0) {
  303. enable_extras "$module_name.cpp";
  304. }
  305. }
  306. } elsif (!defined $opt_disable_auto_extras) {
  307. # TODO: finish modulemanager rewrite and replace this code with:
  308. # system './modulemanager', 'enable', '--auto';
  309. my %modules = (
  310. # Missing: m_ldap, m_regex_stdlib, m_ssl_mbedtls
  311. 'm_geo_maxmind.cpp' => 'pkg-config --exists libmaxminddb',
  312. 'm_mysql.cpp' => 'mysql_config --version',
  313. 'm_pgsql.cpp' => 'pg_config --version',
  314. 'm_regex_pcre.cpp' => 'pcre-config --version',
  315. 'm_regex_posix.cpp' => undef,
  316. 'm_regex_re2.cpp' => 'pkg-config --exists re2',
  317. 'm_regex_tre.cpp' => 'pkg-config --exists tre',
  318. 'm_sqlite3.cpp' => 'pkg-config --exists sqlite3',
  319. 'm_ssl_gnutls.cpp' => 'pkg-config --exists gnutls',
  320. 'm_ssl_openssl.cpp' => 'pkg-config --exists openssl',
  321. 'm_sslrehashsignal.cpp' => undef,
  322. );
  323. while (my ($module, $command) = each %modules) {
  324. unless (defined $command && system "$command 1>/dev/null 2>/dev/null") {
  325. enable_extras $module;
  326. }
  327. }
  328. }
  329. # Generate SSL certificates.
  330. $question = <<EOQ;
  331. Would you like to generate a self-signed SSL certificate now? This certificate
  332. can be used for testing but <|BOLD should not|> be used on a production network.
  333. Note: you can get a <|BOLD free|> CA-signed certificate from Let's Encrypt. See
  334. https://letsencrypt.org/getting-started/ for more details.
  335. EOQ
  336. if (<src/modules/m_ssl_*.cpp>) {
  337. if (prompt_bool $interactive, $question, $interactive) {
  338. system './tools/genssl', 'auto';
  339. }
  340. } else {
  341. print_warning <<"EOM";
  342. You are building without enabling any SSL modules. This is not
  343. recommended as SSL greatly enhances the security and privacy of your IRC server
  344. and in a future version will be <|BOLD required|> for linking servers.
  345. Please read the following documentation pages on how to enable SSL support:
  346. GnuTLS (recommended): https://docs.inspircd.org/3/modules/ssl_gnutls
  347. mbedTLS: https://docs.inspircd.org/3/modules/ssl_mbedtls
  348. OpenSSL: https://docs.inspircd.org/3/modules/ssl_openssl
  349. EOM
  350. }
  351. # Cache the distribution label so that its not lost when --update is run.
  352. $config{DISTRIBUTION} = $opt_distribution_label if $opt_distribution_label;
  353. write_configure_cache %config;
  354. parse_templates \%config, \%compiler, \%version;
  355. print_format <<"EOM";
  356. Configuration is complete! You have chosen to build with the following settings:
  357. <|GREEN Compiler:|>
  358. <|GREEN Binary:|> $config{CXX}
  359. <|GREEN Name:|> $compiler{NAME}
  360. <|GREEN Version:|> $compiler{VERSION}
  361. <|GREEN Extra Modules:|>
  362. EOM
  363. for my $file (<src/modules/m_*>) {
  364. my $module = basename $file, '.cpp';
  365. say " * $module" if -l $file;
  366. }
  367. print_format <<"EOM";
  368. <|GREEN Paths:|>
  369. <|GREEN Base:|> $config{BASE_DIR}
  370. <|GREEN Binary:|> $config{BINARY_DIR}
  371. <|GREEN Config:|> $config{CONFIG_DIR}
  372. <|GREEN Data:|> $config{DATA_DIR}
  373. <|GREEN Example:|> $config{EXAMPLE_DIR}
  374. <|GREEN Log:|> $config{LOG_DIR}
  375. <|GREEN Manual:|> $config{MANUAL_DIR}
  376. <|GREEN Module:|> $config{MODULE_DIR}
  377. <|GREEN Script:|> $config{SCRIPT_DIR}
  378. <|GREEN Execution Group:|> $config{GROUP} ($config{GID})
  379. <|GREEN Execution User:|> $config{USER} ($config{UID})
  380. <|GREEN Socket Engine:|> $config{SOCKETENGINE}
  381. To build with these settings run '<|GREEN make -j${\get_cpu_count} install|>' now.
  382. EOM
  383. # Routine to list out the extra/ modules that have been enabled.
  384. # Note: when getting any filenames out and comparing, it's important to lc it if the
  385. # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
  386. # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
  387. # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
  388. sub list_extras () {
  389. use File::Spec;
  390. # @_ not used
  391. my $srcdir = File::Spec->catdir("src", "modules");
  392. my $abs_srcdir = File::Spec->rel2abs($srcdir);
  393. local $_;
  394. my $dd;
  395. opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
  396. my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
  397. closedir $dd;
  398. undef $dd;
  399. opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
  400. my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
  401. closedir $dd;
  402. undef $dd;
  403. my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
  404. my %extras = ();
  405. EXTRA: for my $extra (@extras) {
  406. next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
  407. my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
  408. my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
  409. next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
  410. if (-l $abs_source) {
  411. # Symlink, is it in the right place?
  412. my $targ = readlink($abs_source);
  413. my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
  414. if ($abs_targ eq $abs_extra) {
  415. $extras{$extra} = "\e[32;1menabled\e[0m";
  416. } else {
  417. $extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
  418. }
  419. } elsif (-e $abs_source) {
  420. my ($devext, $inoext) = stat($abs_extra);
  421. my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
  422. if ($lnksrc > 1) {
  423. if ($devsrc == $devext && $inosrc == $inoext) {
  424. $extras{$extra} = "\e[32;1menabled\e[0m";
  425. } else {
  426. $extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
  427. }
  428. } else {
  429. open my $extfd, "<", $abs_extra;
  430. open my $srcfd, "<", $abs_source;
  431. local $/ = undef;
  432. if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
  433. $extras{$extra} = "\e[32;1menabled\e[0m";
  434. } else {
  435. $extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
  436. }
  437. }
  438. } else {
  439. $extras{$extra} = "\e[33;1mdisabled\e[0m";
  440. }
  441. }
  442. # Now let's add dependency info
  443. for my $extra (keys(%extras)) {
  444. next unless $extras{$extra} =~ m/enabled/; # only process enabled extras.
  445. my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
  446. my @deps = split /\s+/, get_directive($abs_extra, 'ModDep', '');
  447. for my $dep (@deps) {
  448. if (exists($extras{$dep})) {
  449. my $ref = \$extras{$dep}; # Take reference.
  450. if ($$ref !~ m/needed by/) {
  451. # First dependency found.
  452. if ($$ref =~ m/enabled/) {
  453. $$ref .= " (needed by \e[32;1m$extra\e[0m";
  454. } else {
  455. $$ref =~ s/\e\[.*?m//g; # Strip out previous coloring. Will be set in bold+red+blink later.
  456. $$ref .= " (needed by \e[0;32;1;5m$extra\e[0;31;1;5m";
  457. }
  458. } else {
  459. if ($$ref =~ m/enabled/) {
  460. $$ref .= ", \e[32;1m$extra\e[0m";
  461. } else {
  462. $$ref .= ", \e[0;32;1;5m$extra\e[0;31;1;5m";
  463. }
  464. }
  465. }
  466. }
  467. }
  468. for my $extra (sort {$a cmp $b} keys(%extras)) {
  469. my $text = $extras{$extra};
  470. if ($text =~ m/needed by/ && $text !~ m/enabled/) {
  471. printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
  472. } else {
  473. printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
  474. }
  475. }
  476. return keys(%extras) if wantarray; # Can be used by manage_extras.
  477. }
  478. sub enable_extras (@) {
  479. my (@extras) = @_;
  480. for my $extra (@extras) {
  481. my $extrapath = "src/modules/extra/$extra";
  482. if (!-e $extrapath) {
  483. print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in src/modules/extra\n";
  484. next;
  485. }
  486. my $source = "src/modules/$extra";
  487. if (-e $source) {
  488. print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in src/modules exists (might already be enabled?)\n";
  489. next;
  490. }
  491. # Get dependencies, and add them to be processed.
  492. my @deps = split /\s+/, get_directive($extrapath, 'ModDep', '');
  493. for my $dep (@deps) {
  494. next if scalar(grep { $_ eq $dep } (@extras)) > 0; # Skip if we're going to be enabling it anyway.
  495. if (!-e "src/modules/$dep" && !-e "include/$dep") {
  496. if (-e "src/modules/extra/$dep") {
  497. print STDERR "Will also enable extra \e[32;1m$dep\e[0m (needed by \e[32;1m$extra\e[0m)\n";
  498. push @extras, $dep;
  499. } else {
  500. 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";
  501. }
  502. }
  503. }
  504. print "Enabling $extra ... \n";
  505. symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
  506. }
  507. }
  508. sub disable_extras (@)
  509. {
  510. opendir my $dd, "src/modules/extra/";
  511. my @files = readdir($dd);
  512. closedir $dd;
  513. my (@extras) = @_;
  514. EXTRA: for my $extra (@extras) {
  515. my $extrapath = "src/modules/extra/$extra";
  516. my $source = "src/modules/$extra";
  517. if (!-e $extrapath) {
  518. print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
  519. next;
  520. }
  521. if ((! -l $source) || readlink($source) ne "extra/$extra") {
  522. 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";
  523. next;
  524. }
  525. # Check if anything needs this.
  526. for my $file (@files) {
  527. my @deps = split /\s+/, get_directive("src/modules/extra/$file", 'ModDep', '');
  528. # File depends on this extra...
  529. if (scalar(grep { $_ eq $extra } @deps) > 0) {
  530. # And is both enabled and not about to be disabled.
  531. if (-e "src/modules/$file" && scalar(grep { $_ eq $file } @extras) < 1) {
  532. print STDERR "Cannot disable \e[32;1m$extra\e[0m : is needed by \e[32;1m$file\e[0m\n";
  533. next EXTRA;
  534. }
  535. }
  536. }
  537. # Now remove.
  538. print "Disabling $extra ... \n";
  539. unlink "src/modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
  540. }
  541. }