configdata.pm.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. use lib '{- sourcedir('Configurations') -}';
  113. use lib '{- $config{builddir} -}';
  114. use platform;
  115. _____
  116. my @autowarntext = (
  117. 'WARNING: do not edit!',
  118. "Generated by configdata.pm from "
  119. .join(", ", @{$config{build_file_templates}})
  120. );
  121. print 'Creating ',$target{build_file},"\n";
  122. open BUILDFILE, ">$target{build_file}.new"
  123. or die "Trying to create $target{build_file}.new: $!";
  124. foreach (@{$config{build_file_templates}}) {
  125. my $tmpl = OpenSSL::Template->new(TYPE => 'FILE',
  126. SOURCE => $_);
  127. $tmpl->fill_in(FILENAME => $_,
  128. OUTPUT => \*BUILDFILE,
  129. HASH => { config => \%config,
  130. target => \%target,
  131. disabled => \%disabled,
  132. withargs => \%withargs,
  133. unified_info => \%unified_info,
  134. autowarntext => \@autowarntext },
  135. PREPEND => $prepend,
  136. # To ensure that global variables and functions
  137. # defined in one template stick around for the
  138. # next, making them combinable
  139. PACKAGE => 'OpenSSL::safe')
  140. or die $Text::Template::ERROR;
  141. }
  142. close BUILDFILE;
  143. rename("$target{build_file}.new", $target{build_file})
  144. or die "Trying to rename $target{build_file}.new to $target{build_file}: $!";
  145. exit(0);
  146. }
  147. my $dump = undef;
  148. my $cmdline = undef;
  149. my $options = undef;
  150. my $target = undef;
  151. my $envvars = undef;
  152. my $makevars = undef;
  153. my $buildparams = undef;
  154. my $reconf = undef;
  155. my $verbose = undef;
  156. my $help = undef;
  157. my $man = undef;
  158. GetOptions('dump|d' => \$dump,
  159. 'command-line|c' => \$cmdline,
  160. 'options|o' => \$options,
  161. 'target|t' => \$target,
  162. 'environment|e' => \$envvars,
  163. 'make-variables|m' => \$makevars,
  164. 'build-parameters|b' => \$buildparams,
  165. 'reconfigure|reconf|r' => \$reconf,
  166. 'verbose|v' => \$verbose,
  167. 'help' => \$help,
  168. 'man' => \$man)
  169. or die "Errors in command line arguments\n";
  170. if (scalar @ARGV > 0) {
  171. print STDERR <<"_____";
  172. Unrecognised arguments.
  173. For more information, do '$0 --help'
  174. _____
  175. exit(2);
  176. }
  177. if ($help) {
  178. pod2usage(-exitval => 0,
  179. -verbose => 1);
  180. }
  181. if ($man) {
  182. pod2usage(-exitval => 0,
  183. -verbose => 2);
  184. }
  185. if ($dump || $cmdline) {
  186. print "\nCommand line (with current working directory = $here):\n\n";
  187. print ' ',join(' ',
  188. $config{PERL},
  189. catfile($config{sourcedir}, 'Configure'),
  190. @{$config{perlargv}}), "\n";
  191. print "\nPerl information:\n\n";
  192. print ' ',$config{perl_cmd},"\n";
  193. print ' ',$config{perl_version},' for ',$config{perl_archname},"\n";
  194. }
  195. if ($dump || $options) {
  196. my $longest = 0;
  197. my $longest2 = 0;
  198. foreach my $what (@disablables) {
  199. $longest = length($what) if $longest < length($what);
  200. $longest2 = length($disabled{$what})
  201. if $disabled{$what} && $longest2 < length($disabled{$what});
  202. }
  203. print "\nEnabled features:\n\n";
  204. foreach my $what (@disablables) {
  205. print " $what\n" unless $disabled{$what};
  206. }
  207. print "\nDisabled features:\n\n";
  208. foreach my $what (@disablables) {
  209. if ($disabled{$what}) {
  210. print " $what", ' ' x ($longest - length($what) + 1),
  211. "[$disabled{$what}]", ' ' x ($longest2 - length($disabled{$what}) + 1);
  212. print $disabled_info{$what}->{macro}
  213. if $disabled_info{$what}->{macro};
  214. print ' (skip ',
  215. join(', ', @{$disabled_info{$what}->{skipped}}),
  216. ')'
  217. if $disabled_info{$what}->{skipped};
  218. print "\n";
  219. }
  220. }
  221. }
  222. if ($dump || $target) {
  223. print "\nConfig target attributes:\n\n";
  224. foreach (sort keys %target) {
  225. next if $_ =~ m|^_| || $_ eq 'template';
  226. my $quotify = sub {
  227. map {
  228. if (defined $_) {
  229. (my $x = $_) =~ s|([\\\$\@"])|\\$1|g; "\"$x\""
  230. } else {
  231. "undef";
  232. }
  233. } @_;
  234. };
  235. print ' ', $_, ' => ';
  236. if (ref($target{$_}) eq "ARRAY") {
  237. print '[ ', join(', ', $quotify->(@{$target{$_}})), " ],\n";
  238. } else {
  239. print $quotify->($target{$_}), ",\n"
  240. }
  241. }
  242. }
  243. if ($dump || $envvars) {
  244. print "\nRecorded environment:\n\n";
  245. foreach (sort keys %{$config{perlenv}}) {
  246. print ' ',$_,' = ',($config{perlenv}->{$_} || ''),"\n";
  247. }
  248. }
  249. if ($dump || $makevars) {
  250. print "\nMakevars:\n\n";
  251. foreach my $var (@makevars) {
  252. my $prefix = '';
  253. $prefix = $config{CROSS_COMPILE}
  254. if grep { $var eq $_ } @user_crossable;
  255. $prefix //= '';
  256. print ' ',$var,' ' x (16 - length $var),'= ',
  257. (ref $config{$var} eq 'ARRAY'
  258. ? join(' ', @{$config{$var}})
  259. : $prefix.$config{$var}),
  260. "\n"
  261. if defined $config{$var};
  262. }
  263. my @buildfile = ($config{builddir}, $config{build_file});
  264. unshift @buildfile, $here
  265. unless file_name_is_absolute($config{builddir});
  266. my $buildfile = canonpath(catdir(@buildfile));
  267. print <<"_____";
  268. NOTE: These variables only represent the configuration view. The build file
  269. template may have processed these variables further, please have a look at the
  270. build file for more exact data:
  271. $buildfile
  272. _____
  273. }
  274. if ($dump || $buildparams) {
  275. my @buildfile = ($config{builddir}, $config{build_file});
  276. unshift @buildfile, $here
  277. unless file_name_is_absolute($config{builddir});
  278. print "\nbuild file:\n\n";
  279. print " ", canonpath(catfile(@buildfile)),"\n";
  280. print "\nbuild file templates:\n\n";
  281. foreach (@{$config{build_file_templates}}) {
  282. my @tmpl = ($_);
  283. unshift @tmpl, $here
  284. unless file_name_is_absolute($config{sourcedir});
  285. print ' ',canonpath(catfile(@tmpl)),"\n";
  286. }
  287. }
  288. if ($reconf) {
  289. if ($verbose) {
  290. print 'Reconfiguring with: ', join(' ',@{$config{perlargv}}), "\n";
  291. foreach (sort keys %{$config{perlenv}}) {
  292. print ' ',$_,' = ',($config{perlenv}->{$_} || ""),"\n";
  293. }
  294. }
  295. chdir $here;
  296. exec $^X,catfile($config{sourcedir}, 'Configure'),'reconf';
  297. }
  298. }
  299. 1;
  300. __END__
  301. =head1 NAME
  302. configdata.pm - configuration data for OpenSSL builds
  303. =head1 SYNOPSIS
  304. Interactive:
  305. perl configdata.pm [options]
  306. As data bank module:
  307. use configdata;
  308. =head1 DESCRIPTION
  309. This module can be used in two modes, interactively and as a module containing
  310. all the data recorded by OpenSSL's Configure script.
  311. When used interactively, simply run it as any perl script.
  312. If run with no arguments, it will rebuild the build file (Makefile or
  313. corresponding).
  314. With at least one option, it will instead get the information you ask for, or
  315. re-run the configuration process.
  316. See L</OPTIONS> below for more information.
  317. When loaded as a module, you get a few databanks with useful information to
  318. perform build related tasks. The databanks are:
  319. %config Configured things.
  320. %target The OpenSSL config target with all inheritances
  321. resolved.
  322. %disabled The features that are disabled.
  323. @disablables The list of features that can be disabled.
  324. %withargs All data given through --with-THING options.
  325. %unified_info All information that was computed from the build.info
  326. files.
  327. =head1 OPTIONS
  328. =over 4
  329. =item B<--help>
  330. Print a brief help message and exit.
  331. =item B<--man>
  332. Print the manual page and exit.
  333. =item B<--dump> | B<-d>
  334. Print all relevant configuration data. This is equivalent to B<--command-line>
  335. B<--options> B<--target> B<--environment> B<--make-variables>
  336. B<--build-parameters>.
  337. =item B<--command-line> | B<-c>
  338. Print the current configuration command line.
  339. =item B<--options> | B<-o>
  340. Print the features, both enabled and disabled, and display defined macro and
  341. skipped directories where applicable.
  342. =item B<--target> | B<-t>
  343. Print the config attributes for this config target.
  344. =item B<--environment> | B<-e>
  345. Print the environment variables and their values at the time of configuration.
  346. =item B<--make-variables> | B<-m>
  347. Print the main make variables generated in the current configuration
  348. =item B<--build-parameters> | B<-b>
  349. Print the build parameters, i.e. build file and build file templates.
  350. =item B<--reconfigure> | B<--reconf> | B<-r>
  351. Re-run the configuration process.
  352. =item B<--verbose> | B<-v>
  353. Verbose output.
  354. =back
  355. =cut
  356. EOF