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