configdata.pm.in, util/dofile.pl: load 'platform' unconditionally
[oweals/openssl.git] / configdata.pm.in
1 #! {- $config{HASHBANGPERL} -}
2 # -*- mode: perl -*-
3 {-
4  sub out_item {
5      my $ref = shift;
6      # Available options:
7      # indent           => callers indentation (int)
8      # delimiters       => 1 if outer delimiters should be added
9      my %opts = @_;
10
11      my $indent = $opts{indent} // 0;
12      # Indentation of the whole structure, where applicable
13      my $nlindent1 = "\n" . ' ' x $indent;
14      # Indentation of individual items, where applicable
15      my $nlindent2 = "\n" . ' ' x ($indent + 4);
16
17      my $product;      # Finished product, or reference to a function that
18                        # produces a string, given $_
19      # The following are only used when $product is a function reference
20      my $delim_l;      # Left delimiter of structure
21      my $delim_r;      # Right delimiter of structure
22      my $separator;    # Item separator
23      my @items;        # Items to iterate over
24
25      if (ref($ref) eq "ARRAY") {
26          if (scalar @$ref == 0) {
27              $product = $opts{delimiters} ? '[]' : '';
28          } else {
29              $product = sub {
30                  out_item(\$_, delimiters => 1, indent => $indent + 4)
31              };
32              $delim_l = ($opts{delimiters} ? '[' : '').$nlindent2;
33              $delim_r = $nlindent1.($opts{delimiters} ? ']' : '');
34              $separator = ",$nlindent2";
35              @items = @$ref;
36          }
37      } elsif (ref($ref) eq "HASH") {
38          if (scalar keys %$ref == 0) {
39              $product = $opts{delimiters} ? '{}' : '';
40          } else {
41              $product = sub {
42                  quotify1($_) . " => "
43                  . out_item($ref->{$_}, delimiters => 1, indent => $indent + 4)
44              };
45              $delim_l = ($opts{delimiters} ? '{' : '').$nlindent2;
46              $delim_r = $nlindent1.($opts{delimiters} ? '}' : '');
47              $separator = ",$nlindent2";
48              @items = sort keys %$ref;
49          }
50      } elsif (ref($ref) eq "SCALAR") {
51          $product = defined $$ref ? quotify1 $$ref : "undef";
52      } else {
53          $product = defined $ref ? quotify1 $ref : "undef";
54      }
55
56      if (ref($product) eq "CODE") {
57          $delim_l . join($separator, map { &$product } @items) . $delim_r;
58      } else {
59          $product;
60      }
61  }
62
63  # We must make sourcedir() return an absolute path, because configdata.pm
64  # may be loaded as a module from any script in any directory, making
65  # relative paths untrustable.  Because the result is used with 'use lib',
66  # we must ensure that it returns a Unix style path.  Cwd::abs_path does
67  # that (File::Spec::Functions::rel2abs return O/S specific paths)
68  use File::Spec::Functions;
69  use Cwd qw(abs_path);
70  sub sourcedir {
71      return abs_path(catdir($config{sourcedir}, @_));
72  }
73  sub sourcefile {
74      return abs_path(catfile($config{sourcedir}, @_));
75  }
76 -}
77 package configdata;
78
79 use strict;
80 use warnings;
81
82 use Exporter;
83 our @ISA = qw(Exporter);
84 our @EXPORT = qw(
85     %config %target %disabled %withargs %unified_info
86     @disablables @disablables_int
87 );
88
89 our %config = ({- out_item(\%config); -});
90 our %target = ({- out_item(\%target); -});
91 our @disablables = ({- out_item(\@disablables) -});
92 our @disablables_int = ({- out_item(\@disablables_int) -});
93 our %disabled = ({- out_item(\%disabled); -});
94 our %withargs = ({- out_item(\%withargs); -});
95 our %unified_info = ({- out_item(\%unified_info); -});
96
97 # Unexported, only used by OpenSSL::Test::Utils::available_protocols()
98 our %available_protocols = (
99     tls  => [{- out_item(\@tls) -}],
100     dtls => [{- out_item(\@dtls) -}],
101 );
102
103 # The following data is only used when this files is use as a script
104 my @makevars = ({- out_item(\@makevars); -});
105 my %disabled_info = ({- out_item(\%disabled_info); -});
106 my @user_crossable = qw( {- join (' ', @user_crossable) -} );
107
108 # If run directly, we can give some answers, and even reconfigure
109 unless (caller) {
110     use Getopt::Long;
111     use File::Spec::Functions;
112     use File::Basename;
113     use Pod::Usage;
114
115     my $here = dirname($0);
116
117     if (scalar @ARGV == 0) {
118         # With no arguments, re-create the build file
119
120         use lib '{- sourcedir('util', 'perl') -}';
121         use OpenSSL::fallback '{- sourcefile('external', 'perl', 'MODULES.txt') -}';
122         use OpenSSL::Template;
123
124         my $prepend = <<"_____";
125 use File::Spec::Functions;
126 use lib '{- sourcedir('util', 'perl') -}';
127 use lib '{- sourcedir('Configurations') -}';
128 use lib '{- $config{builddir} -}';
129 use platform;
130 _____
131
132         my @autowarntext = (
133             'WARNING: do not edit!',
134             "Generated by configdata.pm from "
135             .join(", ", @{$config{build_file_templates}})
136         );
137
138         print 'Creating ',$target{build_file},"\n";
139         open BUILDFILE, ">$target{build_file}.new"
140             or die "Trying to create $target{build_file}.new: $!";
141         foreach (@{$config{build_file_templates}}) {
142             my $tmpl = OpenSSL::Template->new(TYPE => 'FILE',
143                                               SOURCE => $_);
144             $tmpl->fill_in(FILENAME => $_,
145                            OUTPUT => \*BUILDFILE,
146                            HASH => { config => \%config,
147                                      target => \%target,
148                                      disabled => \%disabled,
149                                      withargs => \%withargs,
150                                      unified_info => \%unified_info,
151                                      autowarntext => \@autowarntext },
152                            PREPEND => $prepend,
153                            # To ensure that global variables and functions
154                            # defined in one template stick around for the
155                            # next, making them combinable
156                            PACKAGE => 'OpenSSL::safe')
157                 or die $Text::Template::ERROR;
158         }
159         close BUILDFILE;
160         rename("$target{build_file}.new", $target{build_file})
161             or die "Trying to rename $target{build_file}.new to $target{build_file}: $!";
162
163         exit(0);
164     }
165
166     my $dump = undef;
167     my $cmdline = undef;
168     my $options = undef;
169     my $target = undef;
170     my $envvars = undef;
171     my $makevars = undef;
172     my $buildparams = undef;
173     my $reconf = undef;
174     my $verbose = undef;
175     my $help = undef;
176     my $man = undef;
177     GetOptions('dump|d'                 => \$dump,
178                'command-line|c'         => \$cmdline,
179                'options|o'              => \$options,
180                'target|t'               => \$target,
181                'environment|e'          => \$envvars,
182                'make-variables|m'       => \$makevars,
183                'build-parameters|b'     => \$buildparams,
184                'reconfigure|reconf|r'   => \$reconf,
185                'verbose|v'              => \$verbose,
186                'help'                   => \$help,
187                'man'                    => \$man)
188         or die "Errors in command line arguments\n";
189
190     if (scalar @ARGV > 0) {
191         print STDERR <<"_____";
192 Unrecognised arguments.
193 For more information, do '$0 --help'
194 _____
195         exit(2);
196     }
197
198     if ($help) {
199         pod2usage(-exitval => 0,
200                   -verbose => 1);
201     }
202     if ($man) {
203         pod2usage(-exitval => 0,
204                   -verbose => 2);
205     }
206     if ($dump || $cmdline) {
207         print "\nCommand line (with current working directory = $here):\n\n";
208         print '    ',join(' ',
209                           $config{PERL},
210                           catfile($config{sourcedir}, 'Configure'),
211                           @{$config{perlargv}}), "\n";
212         print "\nPerl information:\n\n";
213         print '    ',$config{perl_cmd},"\n";
214         print '    ',$config{perl_version},' for ',$config{perl_archname},"\n";
215     }
216     if ($dump || $options) {
217         my $longest = 0;
218         my $longest2 = 0;
219         foreach my $what (@disablables) {
220             $longest = length($what) if $longest < length($what);
221             $longest2 = length($disabled{$what})
222                 if $disabled{$what} && $longest2 < length($disabled{$what});
223         }
224         print "\nEnabled features:\n\n";
225         foreach my $what (@disablables) {
226             print "    $what\n" unless $disabled{$what};
227         }
228         print "\nDisabled features:\n\n";
229         foreach my $what (@disablables) {
230             if ($disabled{$what}) {
231                 print "    $what", ' ' x ($longest - length($what) + 1),
232                     "[$disabled{$what}]", ' ' x ($longest2 - length($disabled{$what}) + 1);
233                 print $disabled_info{$what}->{macro}
234                     if $disabled_info{$what}->{macro};
235                 print ' (skip ',
236                     join(', ', @{$disabled_info{$what}->{skipped}}),
237                     ')'
238                     if $disabled_info{$what}->{skipped};
239                 print "\n";
240             }
241         }
242     }
243     if ($dump || $target) {
244         print "\nConfig target attributes:\n\n";
245         foreach (sort keys %target) {
246             next if $_ =~ m|^_| || $_ eq 'template';
247             my $quotify = sub {
248                 map { (my $x = $_) =~ s|([\\\$\@"])|\\$1|g; "\"$x\""} @_;
249             };
250             print '    ', $_, ' => ';
251             if (ref($target{$_}) eq "ARRAY") {
252                 print '[ ', join(', ', $quotify->(@{$target{$_}})), " ],\n";
253             } else {
254                 print $quotify->($target{$_}), ",\n"
255             }
256         }
257     }
258     if ($dump || $envvars) {
259         print "\nRecorded environment:\n\n";
260         foreach (sort keys %{$config{perlenv}}) {
261             print '    ',$_,' = ',($config{perlenv}->{$_} || ''),"\n";
262         }
263     }
264     if ($dump || $makevars) {
265         print "\nMakevars:\n\n";
266         foreach my $var (@makevars) {
267             my $prefix = '';
268             $prefix = $config{CROSS_COMPILE}
269                 if grep { $var eq $_ } @user_crossable;
270             $prefix //= '';
271             print '    ',$var,' ' x (16 - length $var),'= ',
272                 (ref $config{$var} eq 'ARRAY'
273                  ? join(' ', @{$config{$var}})
274                  : $prefix.$config{$var}),
275                 "\n"
276                 if defined $config{$var};
277         }
278
279         my @buildfile = ($config{builddir}, $config{build_file});
280         unshift @buildfile, $here
281             unless file_name_is_absolute($config{builddir});
282         my $buildfile = canonpath(catdir(@buildfile));
283         print <<"_____";
284
285 NOTE: These variables only represent the configuration view.  The build file
286 template may have processed these variables further, please have a look at the
287 build file for more exact data:
288     $buildfile
289 _____
290     }
291     if ($dump || $buildparams) {
292         my @buildfile = ($config{builddir}, $config{build_file});
293         unshift @buildfile, $here
294             unless file_name_is_absolute($config{builddir});
295         print "\nbuild file:\n\n";
296         print "    ", canonpath(catfile(@buildfile)),"\n";
297
298         print "\nbuild file templates:\n\n";
299         foreach (@{$config{build_file_templates}}) {
300             my @tmpl = ($_);
301             unshift @tmpl, $here
302                 unless file_name_is_absolute($config{sourcedir});
303             print '    ',canonpath(catfile(@tmpl)),"\n";
304         }
305     }
306     if ($reconf) {
307         if ($verbose) {
308             print 'Reconfiguring with: ', join(' ',@{$config{perlargv}}), "\n";
309             foreach (sort keys %{$config{perlenv}}) {
310                 print '    ',$_,' = ',($config{perlenv}->{$_} || ""),"\n";
311             }
312         }
313
314         chdir $here;
315         exec $^X,catfile($config{sourcedir}, 'Configure'),'reconf';
316     }
317 }
318
319 1;
320
321 __END__
322
323 =head1 NAME
324
325 configdata.pm - configuration data for OpenSSL builds
326
327 =head1 SYNOPSIS
328
329 Interactive:
330
331   perl configdata.pm [options]
332
333 As data bank module:
334
335   use configdata;
336
337 =head1 DESCRIPTION
338
339 This module can be used in two modes, interactively and as a module containing
340 all the data recorded by OpenSSL's Configure script.
341
342 When used interactively, simply run it as any perl script.
343 If run with no arguments, it will rebuild the build file (Makefile or
344 corresponding).
345 With at least one option, it will instead get the information you ask for, or
346 re-run the configuration process.
347 See L</OPTIONS> below for more information.
348
349 When loaded as a module, you get a few databanks with useful information to
350 perform build related tasks.  The databanks are:
351
352     %config             Configured things.
353     %target             The OpenSSL config target with all inheritances
354                         resolved.
355     %disabled           The features that are disabled.
356     @disablables        The list of features that can be disabled.
357     %withargs           All data given through --with-THING options.
358     %unified_info       All information that was computed from the build.info
359                         files.
360
361 =head1 OPTIONS
362
363 =over 4
364
365 =item B<--help>
366
367 Print a brief help message and exit.
368
369 =item B<--man>
370
371 Print the manual page and exit.
372
373 =item B<--dump> | B<-d>
374
375 Print all relevant configuration data.  This is equivalent to B<--command-line>
376 B<--options> B<--target> B<--environment> B<--make-variables>
377 B<--build-parameters>.
378
379 =item B<--command-line> | B<-c>
380
381 Print the current configuration command line.
382
383 =item B<--options> | B<-o>
384
385 Print the features, both enabled and disabled, and display defined macro and
386 skipped directories where applicable.
387
388 =item B<--target> | B<-t>
389
390 Print the config attributes for this config target.
391
392 =item B<--environment> | B<-e>
393
394 Print the environment variables and their values at the time of configuration.
395
396 =item B<--make-variables> | B<-m>
397
398 Print the main make variables generated in the current configuration
399
400 =item B<--build-parameters> | B<-b>
401
402 Print the build parameters, i.e. build file and build file templates.
403
404 =item B<--reconfigure> | B<--reconf> | B<-r>
405
406 Re-run the configuration process.
407
408 =item B<--verbose> | B<-v>
409
410 Verbose output.
411
412 =back
413
414 =cut
415
416 EOF