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