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