]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/configure.pm
Fix a bunch of harmless compiler warnings on recent GCC releases.
[user/henk/code/inspircd.git] / make / configure.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2012-2017 Peter Powell <petpow@saberuk.com>
5 #   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 #   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
7 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 #
10 # This file is part of InspIRCd.  InspIRCd is free software: you can
11 # redistribute it and/or modify it under the terms of the GNU General Public
12 # License as published by the Free Software Foundation, version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but WITHOUT
15 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23
24 BEGIN {
25         require 5.10.0;
26 }
27
28 package make::configure;
29
30 use feature ':5.10';
31 use strict;
32 use warnings FATAL => qw(all);
33
34 use Cwd                   qw(getcwd);
35 use Exporter              qw(import);
36 use File::Basename        qw(basename dirname);
37 use File::Spec::Functions qw(catdir catfile);
38
39 use make::common;
40 use make::console;
41
42 use constant CONFIGURE_ROOT          => dirname dirname __FILE__; 
43 use constant CONFIGURE_DIRECTORY     => catdir(CONFIGURE_ROOT, '.configure');
44 use constant CONFIGURE_CACHE_FILE    => catfile(CONFIGURE_DIRECTORY, 'cache.cfg');
45 use constant CONFIGURE_CACHE_VERSION => '1';
46 use constant CONFIGURE_ERROR_PIPE    => $ENV{INSPIRCD_VERBOSE} ? '' : '1>/dev/null 2>/dev/null';
47
48 our @EXPORT = qw(CONFIGURE_CACHE_FILE
49                  CONFIGURE_CACHE_VERSION
50                  cmd_clean
51                  cmd_help
52                  cmd_update
53                  run_test
54                  test_file
55                  test_header
56                  write_configure_cache
57                  get_compiler_info
58                  find_compiler
59                  parse_templates);
60
61 sub __get_socketengines {
62         my @socketengines;
63         foreach (<src/socketengines/socketengine_*.cpp>) {
64                 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
65                 push @socketengines, $1;
66         }
67         return @socketengines;
68 }
69
70 # TODO: when buildtool is done this can be mostly removed with
71 #       the remainder being merged into parse_templates.
72 sub __get_template_settings($$$) {
73
74         # These are actually hash references
75         my ($config, $compiler, $version) = @_;
76
77         # Start off by populating with the config
78         my %settings = %$config;
79
80         # Compiler information
81         while (my ($key, $value) = each %{$compiler}) {
82                 $settings{'COMPILER_' . $key} = $value;
83         }
84
85         # Version information
86         while (my ($key, $value) = each %{$version}) {
87                 $settings{'VERSION_' . $key} = $value;
88         }
89
90         # Miscellaneous information
91         $settings{CONFIGURE_DIRECTORY} = CONFIGURE_DIRECTORY;
92         $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
93         $settings{SYSTEM_NAME} = lc $^O;
94
95         return %settings;
96 }
97
98 sub __test_compiler($) {
99         my $compiler = shift;
100         return 0 unless run_test("`$compiler`", !system "$compiler -v ${\CONFIGURE_ERROR_PIPE}");
101         return 0 unless run_test("`$compiler`", test_file($compiler, 'compiler.cpp', '-fno-rtti'), 'compatible');
102         return 1;
103 }
104
105 sub cmd_clean {
106         unlink CONFIGURE_CACHE_FILE;
107 }
108
109 sub cmd_help {
110         my $PWD = getcwd();
111         my $SELIST = join ', ', __get_socketengines();
112         print <<EOH;
113 Usage: $0 [options]
114
115 When no options are specified, configure runs in interactive mode and you must
116 specify any required values manually. If one or more options are specified,
117 non-interactive configuration is started and any omitted values are defaulted.
118
119 PATH OPTIONS
120
121   --system                      Automatically set up the installation paths
122                                 for system-wide installation.
123   --prefix=[dir]                The root install directory. If this is set then
124                                 all subdirectories will be adjusted accordingly.
125                                 [$PWD/run]
126   --binary-dir=[dir]            The location where the main server binary is
127                                 stored.
128                                 [$PWD/run/bin]
129   --config-dir=[dir]            The location where the configuration files and
130                                 SSL certificates are stored.
131                                 [$PWD/run/conf]
132   --data-dir=[dir]              The location where the data files, such as the
133                                 pid file, are stored.
134                                 [$PWD/run/data]
135   --log-dir=[dir]               The location where the log files are stored.
136                                 [$PWD/run/logs]
137   --manual-dir=[dir]            The location where the manual files are stored.
138                                 [$PWD/run/manuals]
139   --module-dir=[dir]            The location where the loadable modules are
140                                 stored.
141                                 [$PWD/run/modules]
142   --script-dir=[dir]            The location where the scripts, such as the
143                                 init scripts, are stored.
144                                 [$PWD/run]
145
146 EXTRA MODULE OPTIONS
147
148   --enable-extras=[extras]      Enables a comma separated list of extra modules.
149   --disable-extras=[extras]     Disables a comma separated list of extra modules.
150   --list-extras                 Shows the availability status of all extra
151                                 modules.
152
153 MISC OPTIONS
154
155   --clean                       Remove the configuration cache file and start
156                                 the interactive configuration wizard.
157   --disable-interactive         Disables the interactive configuration wizard.
158   --distribution-label=[text]   Sets a distribution specific version label in
159                                 the build configuration.
160   --gid=[id|name]               Sets the group to run InspIRCd as.
161   --help                        Show this message and exit.
162   --socketengine=[name]         Sets the socket engine to be used. Possible
163                                 values are $SELIST.
164   --uid=[id|name]               Sets the user to run InspIRCd as.
165   --update                      Updates the build environment with the settings
166                                 from the cache.
167
168
169 FLAGS
170
171   CXX=[name]                    Sets the C++ compiler to use when building the
172                                 server. If not specified then the build system
173                                 will search for c++, g++, clang++ or icpc.
174
175 If you have any problems with configuring InspIRCd then visit our IRC channel
176 at irc.inspircd.org #InspIRCd for support.
177
178 EOH
179         exit 0;
180 }
181
182 sub cmd_update {
183         print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
184         say 'Updating...';
185         my %config = read_config_file(CONFIGURE_CACHE_FILE);
186         my %compiler = get_compiler_info($config{CXX});
187         my %version = get_version $config{DISTRIBUTION};
188         parse_templates(\%config, \%compiler, \%version);
189         say 'Update complete!';
190         exit 0;
191 }
192
193 sub run_test($$;$) {
194         my ($what, $result, $adjective) = @_;
195         $adjective //= 'available';
196         print_format "Checking whether <|GREEN $what|> is $adjective ... ";
197         print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
198         return $result;
199 }
200
201 sub test_file($$;$) {
202         my ($compiler, $file, $args) = @_;
203         my $status = 0;
204         $args //= '';
205         $status ||= system "$compiler -o __test_$file ${\CONFIGURE_ROOT}/make/test/$file $args ${\CONFIGURE_ERROR_PIPE}";
206         $status ||= system "./__test_$file ${\CONFIGURE_ERROR_PIPE}";
207         unlink "./__test_$file";
208         return !$status;
209 }
210
211 sub test_header($$;$) {
212         my ($compiler, $header, $args) = @_;
213         $args //= '';
214         open(my $fh, "| $compiler -E - $args ${\CONFIGURE_ERROR_PIPE}") or return 0;
215         print $fh "#include <$header>";
216         close $fh;
217         return !$?;
218 }
219
220 sub write_configure_cache(%) {
221         unless (-e CONFIGURE_DIRECTORY) {
222                 print_format "Creating <|GREEN ${\CONFIGURE_DIRECTORY}|> ...\n";
223                 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
224         }
225
226         print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
227         my %config = @_;
228         write_config_file CONFIGURE_CACHE_FILE, %config;
229 }
230
231 sub get_compiler_info($) {
232         my $binary = shift;
233         my %info = (NAME => 'Unknown', VERSION => '0.0');
234         return %info if system "$binary -o __compiler_info ${\CONFIGURE_ROOT}/make/test/compiler_info.cpp ${\CONFIGURE_ERROR_PIPE}";
235         open(my $fh, '-|', './__compiler_info 2>/dev/null');
236         while (my $line = <$fh>) {
237                 $info{$1} = $2 if $line =~ /^([A-Z]+)\s(.+)$/;
238         }
239         close $fh;
240         unlink './__compiler_info';
241         return %info;
242 }
243
244 sub find_compiler {
245         my @compilers = qw(c++ g++ clang++ icpc);
246         foreach my $compiler (shift // @compilers) {
247                 return $compiler if __test_compiler $compiler;
248                 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
249         }
250 }
251
252 sub parse_templates($$$) {
253
254         # These are actually hash references
255         my ($config, $compiler, $version) = @_;
256
257         # Collect settings to be used when generating files
258         my %settings = __get_template_settings($config, $compiler, $version);
259
260         # Iterate through files in make/template.
261         foreach (<make/template/*>) {
262                 print_format "Parsing <|GREEN $_|> ...\n";
263                 open(my $fh, $_) or print_error "unable to read $_: $!";
264                 my (@lines, $mode, @platforms, @targets);
265
266                 # First pass: parse template variables and directives.
267                 while (my $line = <$fh>) {
268                         chomp $line;
269
270                         # Does this line match a variable?
271                         while ($line =~ /(@(\w+?)@)/) {
272                                 my ($variable, $name) = ($1, $2);
273                                 if (defined $settings{$name}) {
274                                         $line =~ s/\Q$variable\E/$settings{$name}/;
275                                 } else {
276                                         print_warning "unknown template variable '$name' in $_!";
277                                         last;
278                                 }
279                         }
280
281                         # Does this line match a directive?
282                         if ($line =~ /^\s*%(\w+)\s+(.+)$/) {
283                                 if ($1 eq 'define') {
284                                         if ($settings{$2}) {
285                                                 push @lines, "#define $2";
286                                         } else {
287                                                 push @lines, "#undef $2";
288                                         }
289                                 } elsif ($1 eq 'mode') {
290                                         $mode = oct $2;
291                                 } elsif ($1 eq 'platform') {
292                                         push @platforms, $2;
293                                 } elsif ($1 eq 'target') {
294                                         push @targets, $2
295                                 } else {
296                                         print_warning "unknown template command '$1' in $_!";
297                                         push @lines, $line;
298                                 }
299                                 next;
300                         }
301                         push @lines, $line;
302                 }
303                 close $fh;
304
305                 # Only proceed if this file should be templated on this platform.
306                 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
307
308                         # Add a default target if the template has not defined one.
309                         unless (@targets) {
310                                 push @targets, catfile(CONFIGURE_DIRECTORY, basename $_);
311                         }
312
313                         # Write the templated files to disk.
314                         for my $target (@targets) {
315
316                                 # Create the directory if it doesn't already exist.
317                                 my $directory = dirname $target;
318                                 unless (-e $directory) {
319                                         print_format "Creating <|GREEN $directory|> ...\n";
320                                         create_directory $directory, 0750 or print_error "unable to create $directory: $!";
321                                 };
322
323                                 # Write the template file.
324                                 print_format "Writing <|GREEN $target|> ...\n";
325                                 open(my $fh, '>', $target) or print_error "unable to write $target: $!";
326                                 foreach (@lines) {
327                                         say $fh $_;
328                                 }
329                                 close $fh;
330
331                                 # Set file permissions.
332                                 if (defined $mode) {
333                                         chmod $mode, $target;
334                                 }
335                         }
336                 }
337         }
338 }
339
340 1;