2
0

configdata.pm.in 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 splitting 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::Compare qw(compare_text);
  70. use File::Copy;
  71. use Pod::Usage;
  72. use lib '{- sourcedir('util', 'perl') -}';
  73. use OpenSSL::fallback '{- sourcefile('external', 'perl', 'MODULES.txt') -}';
  74. my $here = dirname($0);
  75. if (scalar @ARGV == 0) {
  76. # With no arguments, re-create the build file
  77. # We do that in two steps, where the first step emits perl
  78. # snippets.
  79. my $buildfile = $config{build_file};
  80. my $buildfile_template = "$buildfile.in";
  81. my @autowarntext = (
  82. 'WARNING: do not edit!',
  83. "Generated by configdata.pm from "
  84. .join(", ", @{$config{build_file_templates}}),
  85. "via $buildfile_template"
  86. );
  87. my %gendata = (
  88. config => \%config,
  89. target => \%target,
  90. disabled => \%disabled,
  91. withargs => \%withargs,
  92. unified_info => \%unified_info,
  93. autowarntext => \@autowarntext,
  94. );
  95. use lib '.';
  96. use lib '{- sourcedir('Configurations') -}';
  97. use gentemplate;
  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. print 'Created ',$buildfile_template,"\n";
  107. use OpenSSL::Template;
  108. my $prepend = <<'_____';
  109. use File::Spec::Functions;
  110. use lib '{- sourcedir('util', 'perl') -}';
  111. use lib '{- sourcedir('Configurations') -}';
  112. use lib '{- $config{builddir} -}';
  113. use platform;
  114. _____
  115. my $tmpl;
  116. open BUILDFILE, ">$buildfile.new"
  117. or die "Trying to create $buildfile.new: $!";
  118. $tmpl = OpenSSL::Template->new(TYPE => 'FILE',
  119. SOURCE => $buildfile_template);
  120. $tmpl->fill_in(FILENAME => $_,
  121. OUTPUT => \*BUILDFILE,
  122. HASH => \%gendata,
  123. PREPEND => $prepend,
  124. # To ensure that global variables and functions
  125. # defined in one template stick around for the
  126. # next, making them combinable
  127. PACKAGE => 'OpenSSL::safe')
  128. or die $Text::Template::ERROR;
  129. close BUILDFILE;
  130. rename("$buildfile.new", $buildfile)
  131. or die "Trying to rename $buildfile.new to $buildfile: $!";
  132. print 'Created ',$buildfile,"\n";
  133. my $configuration_h =
  134. catfile('include', 'openssl', 'configuration.h');
  135. my $configuration_h_in =
  136. catfile($config{sourcedir}, 'include', 'openssl', 'configuration.h.in');
  137. open CONFIGURATION_H, ">${configuration_h}.new"
  138. or die "Trying to create ${configuration_h}.new: $!";
  139. $tmpl = OpenSSL::Template->new(TYPE => 'FILE',
  140. SOURCE => $configuration_h_in);
  141. $tmpl->fill_in(FILENAME => $_,
  142. OUTPUT => \*CONFIGURATION_H,
  143. HASH => \%gendata,
  144. PREPEND => $prepend,
  145. # To ensure that global variables and functions
  146. # defined in one template stick around for the
  147. # next, making them combinable
  148. PACKAGE => 'OpenSSL::safe')
  149. or die $Text::Template::ERROR;
  150. close CONFIGURATION_H;
  151. # When using stat() on Windows, we can get it to perform better by
  152. # avoid some data. This doesn't affect the mtime field, so we're not
  153. # losing anything...
  154. ${^WIN32_SLOPPY_STAT} = 1;
  155. my $update_configuration_h = 0;
  156. if (-f $configuration_h) {
  157. my $configuration_h_mtime = (stat($configuration_h))[9];
  158. my $configuration_h_in_mtime = (stat($configuration_h_in))[9];
  159. # If configuration.h.in was updated after the last configuration.h,
  160. # or if configuration.h.new differs configuration.h, we update
  161. # configuration.h
  162. if ($configuration_h_mtime < $configuration_h_in_mtime
  163. || compare_text("${configuration_h}.new", $configuration_h) != 0) {
  164. $update_configuration_h = 1;
  165. } else {
  166. # If nothing has changed, let's just drop the new one and
  167. # pretend like nothing happened
  168. unlink "${configuration_h}.new"
  169. }
  170. } else {
  171. $update_configuration_h = 1;
  172. }
  173. if ($update_configuration_h) {
  174. rename("${configuration_h}.new", $configuration_h)
  175. or die "Trying to rename ${configuration_h}.new to $configuration_h: $!";
  176. print 'Created ',$configuration_h,"\n";
  177. }
  178. exit(0);
  179. }
  180. my $dump = undef;
  181. my $cmdline = undef;
  182. my $options = undef;
  183. my $target = undef;
  184. my $envvars = undef;
  185. my $makevars = undef;
  186. my $buildparams = undef;
  187. my $reconf = undef;
  188. my $verbose = undef;
  189. my $query = undef;
  190. my $help = undef;
  191. my $man = undef;
  192. GetOptions('dump|d' => \$dump,
  193. 'command-line|c' => \$cmdline,
  194. 'options|o' => \$options,
  195. 'target|t' => \$target,
  196. 'environment|e' => \$envvars,
  197. 'make-variables|m' => \$makevars,
  198. 'build-parameters|b' => \$buildparams,
  199. 'reconfigure|reconf|r' => \$reconf,
  200. 'verbose|v' => \$verbose,
  201. 'query|q=s' => \$query,
  202. 'help' => \$help,
  203. 'man' => \$man)
  204. or die "Errors in command line arguments\n";
  205. # We allow extra arguments with --query. That allows constructs like
  206. # this:
  207. # ./configdata.pm --query 'get_sources(@ARGV)' file1 file2 file3
  208. if (!$query && scalar @ARGV > 0) {
  209. print STDERR <<"_____";
  210. Unrecognised arguments.
  211. For more information, do '$0 --help'
  212. _____
  213. exit(2);
  214. }
  215. if ($help) {
  216. pod2usage(-exitval => 0,
  217. -verbose => 1);
  218. }
  219. if ($man) {
  220. pod2usage(-exitval => 0,
  221. -verbose => 2);
  222. }
  223. if ($dump || $cmdline) {
  224. print "\nCommand line (with current working directory = $here):\n\n";
  225. print ' ',join(' ',
  226. $config{PERL},
  227. catfile($config{sourcedir}, 'Configure'),
  228. @{$config{perlargv}}), "\n";
  229. print "\nPerl information:\n\n";
  230. print ' ',$config{perl_cmd},"\n";
  231. print ' ',$config{perl_version},' for ',$config{perl_archname},"\n";
  232. }
  233. if ($dump || $options) {
  234. my $longest = 0;
  235. my $longest2 = 0;
  236. foreach my $what (@disablables) {
  237. $longest = length($what) if $longest < length($what);
  238. $longest2 = length($disabled{$what})
  239. if $disabled{$what} && $longest2 < length($disabled{$what});
  240. }
  241. print "\nEnabled features:\n\n";
  242. foreach my $what (@disablables) {
  243. print " $what\n" unless $disabled{$what};
  244. }
  245. print "\nDisabled features:\n\n";
  246. foreach my $what (@disablables) {
  247. if ($disabled{$what}) {
  248. print " $what", ' ' x ($longest - length($what) + 1),
  249. "[$disabled{$what}]", ' ' x ($longest2 - length($disabled{$what}) + 1);
  250. print $disabled_info{$what}->{macro}
  251. if $disabled_info{$what}->{macro};
  252. print ' (skip ',
  253. join(', ', @{$disabled_info{$what}->{skipped}}),
  254. ')'
  255. if $disabled_info{$what}->{skipped};
  256. print "\n";
  257. }
  258. }
  259. }
  260. if ($dump || $target) {
  261. print "\nConfig target attributes:\n\n";
  262. foreach (sort keys %target) {
  263. next if $_ =~ m|^_| || $_ eq 'template';
  264. my $quotify = sub {
  265. map {
  266. if (defined $_) {
  267. (my $x = $_) =~ s|([\\\$\@"])|\\$1|g; "\"$x\""
  268. } else {
  269. "undef";
  270. }
  271. } @_;
  272. };
  273. print ' ', $_, ' => ';
  274. if (ref($target{$_}) eq "ARRAY") {
  275. print '[ ', join(', ', $quotify->(@{$target{$_}})), " ],\n";
  276. } else {
  277. print $quotify->($target{$_}), ",\n"
  278. }
  279. }
  280. }
  281. if ($dump || $envvars) {
  282. print "\nRecorded environment:\n\n";
  283. foreach (sort keys %{$config{perlenv}}) {
  284. print ' ',$_,' = ',($config{perlenv}->{$_} || ''),"\n";
  285. }
  286. }
  287. if ($dump || $makevars) {
  288. print "\nMakevars:\n\n";
  289. foreach my $var (@makevars) {
  290. my $prefix = '';
  291. $prefix = $config{CROSS_COMPILE}
  292. if grep { $var eq $_ } @user_crossable;
  293. $prefix //= '';
  294. print ' ',$var,' ' x (16 - length $var),'= ',
  295. (ref $config{$var} eq 'ARRAY'
  296. ? join(' ', @{$config{$var}})
  297. : $prefix.$config{$var}),
  298. "\n"
  299. if defined $config{$var};
  300. }
  301. my @buildfile = ($config{builddir}, $config{build_file});
  302. unshift @buildfile, $here
  303. unless file_name_is_absolute($config{builddir});
  304. my $buildfile = canonpath(catdir(@buildfile));
  305. print <<"_____";
  306. NOTE: These variables only represent the configuration view. The build file
  307. template may have processed these variables further, please have a look at the
  308. build file for more exact data:
  309. $buildfile
  310. _____
  311. }
  312. if ($dump || $buildparams) {
  313. my @buildfile = ($config{builddir}, $config{build_file});
  314. unshift @buildfile, $here
  315. unless file_name_is_absolute($config{builddir});
  316. print "\nbuild file:\n\n";
  317. print " ", canonpath(catfile(@buildfile)),"\n";
  318. print "\nbuild file templates:\n\n";
  319. foreach (@{$config{build_file_templates}}) {
  320. my @tmpl = ($_);
  321. unshift @tmpl, $here
  322. unless file_name_is_absolute($config{sourcedir});
  323. print ' ',canonpath(catfile(@tmpl)),"\n";
  324. }
  325. }
  326. if ($reconf) {
  327. if ($verbose) {
  328. print 'Reconfiguring with: ', join(' ',@{$config{perlargv}}), "\n";
  329. foreach (sort keys %{$config{perlenv}}) {
  330. print ' ',$_,' = ',($config{perlenv}->{$_} || ""),"\n";
  331. }
  332. }
  333. chdir $here;
  334. exec $^X,catfile($config{sourcedir}, 'Configure'),'reconf';
  335. }
  336. if ($query) {
  337. use OpenSSL::Config::Query;
  338. my $confquery = OpenSSL::Config::Query->new(info => \%unified_info,
  339. config => \%config);
  340. my $result = eval "\$confquery->$query";
  341. # We may need a result class with a printing function at some point.
  342. # Until then, we assume that we get a scalar, or a list or a hash table
  343. # with scalar values and simply print them in some orderly fashion.
  344. if (ref $result eq 'ARRAY') {
  345. print "$_\n" foreach @$result;
  346. } elsif (ref $result eq 'HASH') {
  347. print "$_ : \\\n ", join(" \\\n ", @{$result->{$_}}), "\n"
  348. foreach sort keys %$result;
  349. } elsif (ref $result eq 'SCALAR') {
  350. print "$$result\n";
  351. }
  352. }
  353. }
  354. 1;
  355. __END__
  356. =head1 NAME
  357. configdata.pm - configuration data for OpenSSL builds
  358. =head1 SYNOPSIS
  359. Interactive:
  360. perl configdata.pm [options]
  361. As data bank module:
  362. use configdata;
  363. =head1 DESCRIPTION
  364. This module can be used in two modes, interactively and as a module containing
  365. all the data recorded by OpenSSL's Configure script.
  366. When used interactively, simply run it as any perl script.
  367. If run with no arguments, it will rebuild the build file (Makefile or
  368. corresponding).
  369. With at least one option, it will instead get the information you ask for, or
  370. re-run the configuration process.
  371. See L</OPTIONS> below for more information.
  372. When loaded as a module, you get a few databanks with useful information to
  373. perform build related tasks. The databanks are:
  374. %config Configured things.
  375. %target The OpenSSL config target with all inheritances
  376. resolved.
  377. %disabled The features that are disabled.
  378. @disablables The list of features that can be disabled.
  379. %withargs All data given through --with-THING options.
  380. %unified_info All information that was computed from the build.info
  381. files.
  382. =head1 OPTIONS
  383. =over 4
  384. =item B<--help>
  385. Print a brief help message and exit.
  386. =item B<--man>
  387. Print the manual page and exit.
  388. =item B<--dump> | B<-d>
  389. Print all relevant configuration data. This is equivalent to B<--command-line>
  390. B<--options> B<--target> B<--environment> B<--make-variables>
  391. B<--build-parameters>.
  392. =item B<--command-line> | B<-c>
  393. Print the current configuration command line.
  394. =item B<--options> | B<-o>
  395. Print the features, both enabled and disabled, and display defined macro and
  396. skipped directories where applicable.
  397. =item B<--target> | B<-t>
  398. Print the config attributes for this config target.
  399. =item B<--environment> | B<-e>
  400. Print the environment variables and their values at the time of configuration.
  401. =item B<--make-variables> | B<-m>
  402. Print the main make variables generated in the current configuration
  403. =item B<--build-parameters> | B<-b>
  404. Print the build parameters, i.e. build file and build file templates.
  405. =item B<--reconfigure> | B<--reconf> | B<-r>
  406. Re-run the configuration process.
  407. =item B<--verbose> | B<-v>
  408. Verbose output.
  409. =back
  410. =cut
  411. EOF