]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/configure.pm
Add a new runtime directory and move the pid file to it.
[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 BEGIN {
25         require 5.10.0;
26 }
27
28 package make::configure;
29
30 use feature ':5.10';
31 use strict;
32 use warnings FATAL => qw(all);
33
34 use Exporter              qw(import);
35 use File::Basename        qw(basename dirname);
36 use File::Spec::Functions qw(abs2rel 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                  CONFIGURE_DIRECTORY
50                  cmd_clean
51                  cmd_help
52                  cmd_update
53                  run_test
54                  test_file
55                  test_header
56                  module_expand
57                  module_shrink
58                  write_configure_cache
59                  get_compiler_info
60                  find_compiler
61                  parse_templates);
62
63 sub __get_socketengines {
64         my @socketengines;
65         foreach (<${\CONFIGURE_ROOT}/src/socketengines/socketengine_*.cpp>) {
66                 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
67                 push @socketengines, $1;
68         }
69         return @socketengines;
70 }
71
72 # TODO: when buildtool is done this can be mostly removed with
73 #       the remainder being merged into parse_templates.
74 sub __get_template_settings($$$) {
75
76         # These are actually hash references
77         my ($config, $compiler, $version) = @_;
78
79         # Start off by populating with the config
80         my %settings = %$config;
81
82         # Compiler information
83         while (my ($key, $value) = each %{$compiler}) {
84                 $settings{'COMPILER_' . $key} = $value;
85         }
86
87         # Version information
88         while (my ($key, $value) = each %{$version}) {
89                 $settings{'VERSION_' . $key} = $value;
90         }
91
92         # Miscellaneous information
93         $settings{CONFIGURE_DIRECTORY} = CONFIGURE_DIRECTORY;
94         $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
95         $settings{SYSTEM_NAME} = lc $^O;
96
97         return %settings;
98 }
99
100 sub __test_compiler($) {
101         my $compiler = shift;
102         return 0 unless run_test("`$compiler`", !system "$compiler -v ${\CONFIGURE_ERROR_PIPE}");
103         return 0 unless run_test("`$compiler`", test_file($compiler, 'compiler.cpp', '-fno-rtti'), 'compatible');
104         return 1;
105 }
106
107 sub cmd_clean {
108         unlink CONFIGURE_CACHE_FILE;
109 }
110
111 sub cmd_help {
112         my $SELIST = join ', ', __get_socketengines();
113         print <<EOH;
114 Usage: $0 [options]
115
116 When no options are specified, configure runs in interactive mode and you must
117 specify any required values manually. If one or more options are specified,
118 non-interactive configuration is started and any omitted values are defaulted.
119
120 PATH OPTIONS
121
122   --system                      Automatically set up the installation paths
123                                 for system-wide installation.
124   --prefix=[dir]                The root install directory. If this is set then
125                                 all subdirectories will be adjusted accordingly.
126                                 [${\CONFIGURE_ROOT}/run]
127   --binary-dir=[dir]            The location where the main server binary is
128                                 stored.
129                                 [${\CONFIGURE_ROOT}/run/bin]
130   --config-dir=[dir]            The location where the configuration files and
131                                 SSL certificates are stored.
132                                 [${\CONFIGURE_ROOT}/run/conf]
133   --data-dir=[dir]              The location where the data files, such as the
134                                 pid file, are stored.
135                                 [${\CONFIGURE_ROOT}/run/data]
136   --example-dir=[dir]           The location where the example configuration files
137                                 and SQL schemas are stored.
138                                 [${\CONFIGURE_ROOT}/run/conf/examples]
139   --log-dir=[dir]               The location where the log files are stored.
140                                 [${\CONFIGURE_ROOT}/run/logs]
141   --manual-dir=[dir]            The location where the manual files are stored.
142                                 [${\CONFIGURE_ROOT}/run/manuals]
143   --module-dir=[dir]            The location where the loadable modules are
144                                 stored.
145                                 [${\CONFIGURE_ROOT}/run/modules]
146   --script-dir=[dir]            The location where the scripts, such as the
147                                 init scripts, are stored.
148                                 [${\CONFIGURE_ROOT}/run]
149
150 EXTRA MODULE OPTIONS
151
152   --enable-extras=[extras]      Enables a comma separated list of extra modules.
153   --disable-extras=[extras]     Disables a comma separated list of extra modules.
154   --list-extras                 Shows the availability status of all extra
155                                 modules.
156
157 MISC OPTIONS
158
159   --clean                       Remove the configuration cache file and start
160                                 the interactive configuration wizard.
161   --disable-auto-extras         Disables automatically enabling extra modules
162                                 for which the dependencies are available.
163   --disable-interactive         Disables the interactive configuration wizard.
164   --distribution-label=[text]   Sets a distribution specific version label in
165                                 the build configuration.
166   --gid=[id|name]               Sets the group to run InspIRCd as.
167   --help                        Show this message and exit.
168   --socketengine=[name]         Sets the socket engine to be used. Possible
169                                 values are $SELIST.
170   --uid=[id|name]               Sets the user to run InspIRCd as.
171   --update                      Updates the build environment with the settings
172                                 from the cache.
173
174
175 FLAGS
176
177   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
181 If you have any problems with configuring InspIRCd then visit our IRC channel
182 at irc.inspircd.org #InspIRCd or create a support discussion at
183 https://github.com/inspircd/inspircd/discussions.
184
185 Packagers: see https://docs.inspircd.org/packaging/ for packaging advice.
186 EOH
187         exit 0;
188 }
189
190 sub cmd_update {
191         print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
192         say 'Updating...';
193         my %config = read_config_file(CONFIGURE_CACHE_FILE);
194         $config{EXAMPLE_DIR} //= catdir $config{CONFIG_DIR}, 'examples';
195         $config{RUNTIME_DIR} //= $config{DATA_DIR};
196         my %compiler = get_compiler_info($config{CXX});
197         my %version = get_version $config{DISTRIBUTION};
198         parse_templates(\%config, \%compiler, \%version);
199         say 'Update complete!';
200         exit 0;
201 }
202
203 sub run_test($$;$) {
204         my ($what, $result, $adjective) = @_;
205         $adjective //= 'available';
206         print_format "Checking whether <|GREEN $what|> is $adjective ... ";
207         print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
208         return $result;
209 }
210
211 sub test_file($$;$) {
212         my ($compiler, $file, $args) = @_;
213         my $status = 0;
214         $args //= '';
215         $status ||= system "$compiler -o ${\CONFIGURE_ROOT}/__test_$file ${\CONFIGURE_ROOT}/make/test/$file $args ${\CONFIGURE_ERROR_PIPE}";
216         $status ||= system "${\CONFIGURE_ROOT}/__test_$file ${\CONFIGURE_ERROR_PIPE}";
217         unlink "${\CONFIGURE_ROOT}/__test_$file";
218         return !$status;
219 }
220
221 sub test_header($$;$) {
222         my ($compiler, $header, $args) = @_;
223         $args //= '';
224         open(my $fh, "| $compiler -E - $args ${\CONFIGURE_ERROR_PIPE}") or return 0;
225         print $fh "#include <$header>";
226         close $fh;
227         return !$?;
228 }
229
230 sub module_expand($) {
231         my $module = shift;
232         $module = "m_$module" unless $module =~ /^(?:m|core)_/;
233         $module = "$module.cpp" unless $module =~ /\.cpp$/;
234         return $module;
235 }
236
237 sub module_shrink($) {
238         my $module = basename shift;
239         return $module =~ s/(?:^m_|\.cpp$)//gr;
240 }
241
242 sub write_configure_cache(%) {
243         unless (-e CONFIGURE_DIRECTORY) {
244                 print_format "Creating <|GREEN ${\abs2rel CONFIGURE_DIRECTORY, CONFIGURE_ROOT}|> ...\n";
245                 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
246         }
247
248         print_format "Writing <|GREEN ${\abs2rel CONFIGURE_CACHE_FILE, CONFIGURE_ROOT}|> ...\n";
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         foreach 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         foreach my $template (<${\CONFIGURE_ROOT}/make/template/*>) {
284                 print_format "Parsing <|GREEN ${\abs2rel $template, CONFIGURE_ROOT}|> ...\n";
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                                         print_format "Creating <|GREEN ${\abs2rel $directory, CONFIGURE_ROOT}|> ...\n";
344                                         create_directory $directory, 0750 or print_error "unable to create $directory: $!";
345                                 };
346
347                                 # Write the template file.
348                                 print_format "Writing <|GREEN ${\abs2rel $target, CONFIGURE_ROOT}|> ...\n";
349                                 open(my $fh, '>', $target) or print_error "unable to write $target: $!";
350                                 foreach (@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;