configdata.pm.in 14 KB

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