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