2 # InspIRCd -- Internet Relay Chat Daemon
4 # Copyright (C) 2012-2017 Peter Powell <petpow@saberuk.com>
5 # Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 # Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
7 # Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 # Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
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.
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
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/>.
28 package make::configure;
32 use warnings FATAL => qw(all);
35 use Exporter qw(import);
36 use File::Basename qw(basename dirname);
37 use File::Spec::Functions qw(catdir catfile);
42 use constant CONFIGURE_ROOT => dirname dirname __FILE__;
43 use constant CONFIGURE_DIRECTORY => catdir(CONFIGURE_ROOT, '.configure');
44 use constant CONFIGURE_CACHE_FILE => catfile(CONFIGURE_DIRECTORY, 'cache.cfg');
45 use constant CONFIGURE_CACHE_VERSION => '1';
46 use constant CONFIGURE_ERROR_PIPE => $ENV{INSPIRCD_VERBOSE} ? '' : '1>/dev/null 2>/dev/null';
48 our @EXPORT = qw(CONFIGURE_CACHE_FILE
49 CONFIGURE_CACHE_VERSION
61 sub __get_socketengines {
63 foreach (<src/socketengines/socketengine_*.cpp>) {
64 s/src\/socketengines\/socketengine_(\w+)\.cpp/$1/;
65 push @socketengines, $1;
67 return @socketengines;
70 # TODO: when buildtool is done this can be mostly removed with
71 # the remainder being merged into parse_templates.
72 sub __get_template_settings($$$) {
74 # These are actually hash references
75 my ($config, $compiler, $version) = @_;
77 # Start off by populating with the config
78 my %settings = %$config;
80 # Compiler information
81 while (my ($key, $value) = each %{$compiler}) {
82 $settings{'COMPILER_' . $key} = $value;
86 while (my ($key, $value) = each %{$version}) {
87 $settings{'VERSION_' . $key} = $value;
90 # Miscellaneous information
91 $settings{CONFIGURE_DIRECTORY} = CONFIGURE_DIRECTORY;
92 $settings{CONFIGURE_CACHE_FILE} = CONFIGURE_CACHE_FILE;
93 $settings{SYSTEM_NAME} = lc $^O;
98 sub __test_compiler($) {
100 return 0 unless run_test("`$compiler`", !system "$compiler -v ${\CONFIGURE_ERROR_PIPE}");
101 return 0 unless run_test("`$compiler`", test_file($compiler, 'compiler.cpp', '-fno-rtti'), 'compatible');
106 unlink CONFIGURE_CACHE_FILE;
111 my $SELIST = join ', ', __get_socketengines();
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.
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.
126 --binary-dir=[dir] The location where the main server binary is
129 --config-dir=[dir] The location where the configuration files and
130 SSL certificates are stored.
132 --data-dir=[dir] The location where the data files, such as the
133 pid file, are stored.
135 --example-dir=[dir] The location where the example configuration files
136 and SQL schemas are stored.
137 [$PWD/run/conf/examples]
138 --log-dir=[dir] The location where the log files are stored.
140 --manual-dir=[dir] The location where the manual files are stored.
142 --module-dir=[dir] The location where the loadable modules are
145 --script-dir=[dir] The location where the scripts, such as the
146 init scripts, are stored.
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
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
169 --uid=[id|name] Sets the user to run InspIRCd as.
170 --update Updates the build environment with the settings
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.
180 If you have any problems with configuring InspIRCd then visit our IRC channel
181 at irc.inspircd.org #InspIRCd for support.
188 print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE;
190 my %config = read_config_file(CONFIGURE_CACHE_FILE);
191 my %compiler = get_compiler_info($config{CXX});
192 my %version = get_version $config{DISTRIBUTION};
193 parse_templates(\%config, \%compiler, \%version);
194 say 'Update complete!';
199 my ($what, $result, $adjective) = @_;
200 $adjective //= 'available';
201 print_format "Checking whether <|GREEN $what|> is $adjective ... ";
202 print_format $result ? "<|GREEN yes|>\n" : "<|RED no|>\n";
206 sub test_file($$;$) {
207 my ($compiler, $file, $args) = @_;
210 $status ||= system "$compiler -o __test_$file ${\CONFIGURE_ROOT}/make/test/$file $args ${\CONFIGURE_ERROR_PIPE}";
211 $status ||= system "./__test_$file ${\CONFIGURE_ERROR_PIPE}";
212 unlink "./__test_$file";
216 sub test_header($$;$) {
217 my ($compiler, $header, $args) = @_;
219 open(my $fh, "| $compiler -E - $args ${\CONFIGURE_ERROR_PIPE}") or return 0;
220 print $fh "#include <$header>";
225 sub write_configure_cache(%) {
226 unless (-e CONFIGURE_DIRECTORY) {
227 print_format "Creating <|GREEN ${\CONFIGURE_DIRECTORY}|> ...\n";
228 create_directory CONFIGURE_DIRECTORY, 0750 or print_error "unable to create ${\CONFIGURE_DIRECTORY}: $!";
231 print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n";
233 write_config_file CONFIGURE_CACHE_FILE, %config;
236 sub get_compiler_info($) {
238 my %info = (NAME => 'Unknown', VERSION => '0.0');
239 return %info if system "$binary -o __compiler_info ${\CONFIGURE_ROOT}/make/test/compiler_info.cpp ${\CONFIGURE_ERROR_PIPE}";
240 open(my $fh, '-|', './__compiler_info 2>/dev/null');
241 while (my $line = <$fh>) {
242 $info{$1} = $2 if $line =~ /^([A-Z]+)\s(.+)$/;
245 unlink './__compiler_info';
250 my @compilers = qw(c++ g++ clang++ icpc);
251 foreach my $compiler (shift // @compilers) {
252 return $compiler if __test_compiler $compiler;
253 return "xcrun $compiler" if $^O eq 'darwin' && __test_compiler "xcrun $compiler";
257 sub parse_templates($$$) {
259 # These are actually hash references
260 my ($config, $compiler, $version) = @_;
262 # Collect settings to be used when generating files
263 my %settings = __get_template_settings($config, $compiler, $version);
265 # Iterate through files in make/template.
266 foreach (<make/template/*>) {
267 print_format "Parsing <|GREEN $_|> ...\n";
268 open(my $fh, $_) or print_error "unable to read $_: $!";
269 my (@lines, $mode, @platforms, @targets);
271 # First pass: parse template variables and directives.
272 while (my $line = <$fh>) {
275 # Does this line match a variable?
276 while ($line =~ /(@(\w+?)@)/) {
277 my ($variable, $name) = ($1, $2);
278 if (defined $settings{$name}) {
279 $line =~ s/\Q$variable\E/$settings{$name}/;
281 print_warning "unknown template variable '$name' in $_!";
286 # Does this line match a directive?
287 if ($line =~ /^(\s*)%(\w+)\s+(.+)$/) {
288 if ($2 eq 'define') {
290 push @lines, "#$1define $3";
292 push @lines, "#$1undef $3";
294 } elsif ($2 eq 'mode') {
296 } elsif ($2 eq 'platform') {
298 } elsif ($2 eq 'target') {
301 print_warning "unknown template command '$2' in $_!";
310 # Only proceed if this file should be templated on this platform.
311 if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
313 # Add a default target if the template has not defined one.
315 push @targets, catfile(CONFIGURE_DIRECTORY, basename $_);
318 # Write the templated files to disk.
319 for my $target (@targets) {
321 # Create the directory if it doesn't already exist.
322 my $directory = dirname $target;
323 unless (-e $directory) {
324 print_format "Creating <|GREEN $directory|> ...\n";
325 create_directory $directory, 0750 or print_error "unable to create $directory: $!";
328 # Write the template file.
329 print_format "Writing <|GREEN $target|> ...\n";
330 open(my $fh, '>', $target) or print_error "unable to write $target: $!";
336 # Set file permissions.
338 chmod $mode, $target;