configdata.pm.in 14 KB

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