]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/configure.pm
Add a link to the packaging advice to the configure help.
[user/henk/code/inspircd.git] / make / configure.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2013-2020 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 Exporter              qw(import);
34 use File::Basename        qw(basename dirname);
35 use File::Spec::Functions qw(abs2rel catdir catfile);
36
37 use make::common;
38 use make::console;
39
40 use constant CONFIGURE_ROOT          => dirname dirname __FILE__;
41 use constant CONFIGURE_DIRECTORY     => catdir(CONFIGURE_ROOT, '.configure');
42 use constant CONFIGURE_CACHE_FILE    => catfile(CONFIGURE_DIRECTORY, 'cache.cfg');
43 use constant CONFIGURE_CACHE_VERSION => '1';
44 use constant CONFIGURE_ERROR_PIPE    => $ENV{INSPIRCD_VERBOSE} ? '' : '1>/dev/null 2>/dev/null';
45
46 our @EXPORT = qw(CONFIGURE_CACHE_FILE
47                  CONFIGURE_CACHE_VERSION
48                  cmd_clean
49                  cmd_help
50                  cmd_update
51                  run_test
52                  test_file
53                  test_header
54                  write_configure_cache
55                  get_compiler_info
56                  find_compiler
57                  parse_templates);
58
59 sub __get_socketengines {
60         my @socketengines;
61         foreach (<${\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 <<EOH;
110 Usage: $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 PATH OPTIONS
117
118   --system                      Automatically set up the installation paths
119                                 for system-wide installation.
120   --prefix=[dir]                The root install directory. If this is set then
121                                 all subdirectories will be adjusted accordingly.
122                                 [${\CONFIGURE_ROOT}/run]
123   --binary-dir=[dir]            The location where the main server binary is
124                                 stored.
125                                 [${\CONFIGURE_ROOT}/run/bin]
126   --config-dir=[dir]            The location where the configuration files and
127                                 SSL certificates are stored.
128                                 [${\CONFIGURE_ROOT}/run/conf]
129   --data-dir=[dir]              The location where the data files, such as the
130                                 pid file, are stored.
131                                 [${\CONFIGURE_ROOT}/run/data]
132   --example-dir=[dir]           The location where the example configuration files
133                                 and SQL schemas are stored.
134                                 [${\CONFIGURE_ROOT}/run/conf/examples]
135   --log-dir=[dir]               The location where the log files are stored.
136                                 [${\CONFIGURE_ROOT}/run/logs]
137   --manual-dir=[dir]            The location where the manual files are stored.
138                                 [${\CONFIGURE_ROOT}/run/manuals]
139   --module-dir=[dir]            The location where the loadable modules are
140                                 stored.
141                                 [${\CONFIGURE_ROOT}/run/modules]
142   --script-dir=[dir]            The location where the scripts, such as the
143                                 init scripts, are stored.
144                                 [${\CONFIGURE_ROOT}/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-auto-extras         Disables automatically enabling extra modules
158                                 for which the dependencies are available.
159   --disable-interactive         Disables the interactive configuration wizard.
160   --distribution-label=[text]   Sets a distribution specific version label in
161                                 the build configuration.
162   --gid=[id|name]               Sets the group to run InspIRCd as.
163   --help                        Show this message and exit.
164   --socketengine=[name]         Sets the socket engine to be used. Possible
165                                 values are $SELIST.
166   --uid=[id|name]               Sets the user to run InspIRCd as.
167   --update                      Updates the build environment with the settings
168                                 from the cache.
169
170
171 FLAGS
172
173   CXX=[name]                    Sets the C++ compiler to use when building the
174                                 server. If not specified then the build system
175                                 will search for c++, g++, clang++ or icpc.
176
177 If you have any problems with configuring InspIRCd then visit our IRC channel
178 at irc.inspircd.org #InspIRCd for support.
179
180 Packagers: see https://docs.inspircd.org/packaging/ for packaging advice.
181 EOH
182         exit 0;
183 }
184
185 sub cmd_update {
186         print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
187         say 'Updating...';
188         my %config = read_config_file(CONFIGURE_CACHE_FILE);
189         my %compiler = get_compiler_info($config{CXX});
190         my %version = get_version $config{DISTRIBUTION};
191         parse_templates(\%config, \%compiler, \%version);
192         say 'Update complete!';
193         exit 0;
194 }
195
196 sub run_test($$;$) {
197         my ($what, $result, $adjective) = @_;
198         $adjective //= 'available';
199         print_format "Checking whether <|GREEN $what|> is $adjective ... ";
200         print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
201         return $result;
202 }
203
204 sub test_file($$;$) {
205         my ($compiler, $file, $args) = @_;
206         my $status = 0;
207         $args //= '';
208         $status ||= system "$compiler -o ${\CONFIGURE_ROOT}/__test_$file ${\CONFIGURE_ROOT}/make/test/$file $args ${\CONFIGURE_ERROR_PIPE}";
209         $status ||= system "${\CONFIGURE_ROOT}/__test_$file ${\CONFIGURE_ERROR_PIPE}";
210         unlink "${\CONFIGURE_ROOT}/__test_$file";
211         return !$status;
212 }
213
214 sub test_header($$;$) {
215         my ($compiler, $header, $args) = @_;
216         $args //= '';
217         open(my $fh, "| $compiler -E - $args ${\CONFIGURE_ERROR_PIPE}") or return 0;
218         print $fh "#include <$header>";
219         close $fh;
220         return !$?;
221 }
222
223 sub write_configure_cache(%) {
224         unless (-e CONFIGURE_DIRECTORY) {
225                 print_format "Creating <|GREEN ${\abs2rel CONFIGURE_DIRECTORY, CONFIGURE_ROOT}|> ...\n";
226                 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
227         }
228
229         print_format "Writing <|GREEN ${\abs2rel CONFIGURE_CACHE_FILE, CONFIGURE_ROOT}|> ...\n";
230         my %config = @_;
231         write_config_file CONFIGURE_CACHE_FILE, %config;
232 }
233
234 sub get_compiler_info($) {
235         my $binary = shift;
236         my %info = (NAME => 'Unknown', VERSION => '0.0');
237         return %info if system "$binary -o __compiler_info ${\CONFIGURE_ROOT}/make/test/compiler_info.cpp ${\CONFIGURE_ERROR_PIPE}";
238         open(my $fh, '-|', './__compiler_info 2>/dev/null');
239         while (my $line = <$fh>) {
240                 $info{$1} = $2 if $line =~ /^([A-Z]+)\s(.+)$/;
241         }
242         close $fh;
243         unlink './__compiler_info';
244         return %info;
245 }
246
247 sub find_compiler {
248         my @compilers = qw(c++ g++ clang++ icpc);
249         foreach my $compiler (shift // @compilers) {
250                 return $compiler if __test_compiler $compiler;
251                 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
252         }
253 }
254
255 sub parse_templates($$$) {
256
257         # These are actually hash references
258         my ($config, $compiler, $version) = @_;
259
260         # Collect settings to be used when generating files
261         my %settings = __get_template_settings($config, $compiler, $version);
262
263         # Iterate through files in make/template.
264         foreach my $template (<${\CONFIGURE_ROOT}/make/template/*>) {
265                 print_format "Parsing <|GREEN ${\abs2rel $template, CONFIGURE_ROOT}|> ...\n";
266                 open(my $fh, $template) or print_error "unable to read $template: $!";
267                 my (@lines, $mode, @platforms, @targets);
268
269                 # First pass: parse template variables and directives.
270                 while (my $line = <$fh>) {
271                         chomp $line;
272
273                         # Does this line match a variable?
274                         while ($line =~ /(@(\w+?)(?:\|(\w*))?@)/) {
275                                 my ($variable, $name, $default) = ($1, $2, $3);
276                                 if (defined $settings{$name}) {
277                                         $line =~ s/\Q$variable\E/$settings{$name}/;
278                                 } elsif (defined $default) {
279                                         $line =~ s/\Q$variable\E/$default/;
280                                 } else {
281                                         print_warning "unknown template variable '$name' in $template!";
282                                         last;
283                                 }
284                         }
285
286                         # Does this line match a directive?
287                         if ($line =~ /^(\s*)%(\w+)\s+(.+)$/) {
288                                 if ($2 eq 'define') {
289                                         if ($settings{$3}) {
290                                                 push @lines, "#$1define $3";
291                                         } else {
292                                                 push @lines, "#$1undef $3";
293                                         }
294                                 } elsif ($2 eq 'mode') {
295                                         $mode = oct $3;
296                                 } elsif ($2 eq 'platform') {
297                                         push @platforms, $3;
298                                 } elsif ($2 eq 'target') {
299                                         push @targets, catfile CONFIGURE_ROOT, $3;
300                                 } else {
301                                         print_warning "unknown template command '$2' in $template!";
302                                         push @lines, $line;
303                                 }
304                                 next;
305                         }
306                         push @lines, $line;
307                 }
308                 close $fh;
309
310                 # Only proceed if this file should be templated on this platform.
311                 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
312
313                         # Add a default target if the template has not defined one.
314                         unless (@targets) {
315                                 push @targets, catfile(CONFIGURE_DIRECTORY, basename $template);
316                         }
317
318                         # Write the templated files to disk.
319                         for my $target (@targets) {
320
321                                 # Create the directory if it doesn't already exist.
322                                 my $directory = dirname $target;
323                                 unless (-e $directory) {
324                                         print_format "Creating <|GREEN ${\abs2rel $directory, CONFIGURE_ROOT}|> ...\n";
325                                         create_directory $directory, 0750 or print_error "unable to create $directory: $!";
326                                 };
327
328                                 # Write the template file.
329                                 print_format "Writing <|GREEN ${\abs2rel $target, CONFIGURE_ROOT}|> ...\n";
330                                 open(my $fh, '>', $target) or print_error "unable to write $target: $!";
331                                 foreach (@lines) {
332                                         say $fh $_;
333                                 }
334                                 close $fh;
335
336                                 # Set file permissions.
337                                 if (defined $mode) {
338                                         chmod $mode, $target;
339                                 }
340                         }
341                 }
342         }
343 }
344
345 1;