mkinstallvars.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #! /usr/bin/env perl
  2. # Copyright 2021-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # All variables are supposed to come from Makefile, in environment variable
  9. # form, or passed as variable assignments on the command line.
  10. # The result is a Perl module creating the package OpenSSL::safe::installdata.
  11. use 5.10.0;
  12. use strict;
  13. use warnings;
  14. use Carp;
  15. use File::Spec;
  16. #use List::Util qw(pairs);
  17. sub _pairs (@);
  18. # These are expected to be set up as absolute directories
  19. my @absolutes = qw(PREFIX libdir);
  20. # These may be absolute directories, and if not, they are expected to be set up
  21. # as subdirectories to PREFIX or LIBDIR. The order of the pairs is important,
  22. # since the LIBDIR subdirectories depend on the calculation of LIBDIR from
  23. # PREFIX.
  24. my @subdirs = _pairs (PREFIX => [ qw(BINDIR LIBDIR INCLUDEDIR APPLINKDIR) ],
  25. LIBDIR => [ qw(ENGINESDIR MODULESDIR PKGCONFIGDIR
  26. CMAKECONFIGDIR) ]);
  27. # For completeness, other expected variables
  28. my @others = qw(VERSION LDLIBS);
  29. my %all = ( );
  30. foreach (@absolutes) { $all{$_} = 1 }
  31. foreach (@subdirs) { foreach (@{$_->[1]}) { $all{$_} = 1 } }
  32. foreach (@others) { $all{$_} = 1 }
  33. print STDERR "DEBUG: all keys: ", join(", ", sort keys %all), "\n";
  34. my %keys = ();
  35. my %values = ();
  36. foreach (@ARGV) {
  37. (my $k, my $v) = m|^([^=]*)=(.*)$|;
  38. $keys{$k} = 1;
  39. push @{$values{$k}}, $v;
  40. }
  41. # warn if there are missing values, and also if there are unexpected values
  42. foreach my $k (sort keys %all) {
  43. warn "No value given for $k\n" unless $keys{$k};
  44. }
  45. foreach my $k (sort keys %keys) {
  46. warn "Unknown variable $k\n" unless $all{$k};
  47. }
  48. # This shouldn't be needed, but just in case we get relative paths that
  49. # should be absolute, make sure they actually are.
  50. foreach my $k (@absolutes) {
  51. my $v = $values{$k} || [ '.' ];
  52. die "Can't have more than one $k\n" if scalar @$v > 1;
  53. print STDERR "DEBUG: $k = $v->[0] => ";
  54. $v = [ map { File::Spec->rel2abs($_) } @$v ];
  55. $values{$k} = $v;
  56. print STDERR "$k = $v->[0]\n";
  57. }
  58. # Absolute paths for the subdir variables are computed. This provides
  59. # the usual form of values for names that have become norm, known as GNU
  60. # installation paths.
  61. # For the benefit of those that need it, the subdirectories are preserved
  62. # as they are, using the same variable names, suffixed with '_REL_{var}',
  63. # if they are indeed subdirectories. The '{var}' part of the name tells
  64. # which other variable value they are relative to.
  65. foreach my $pair (@subdirs) {
  66. my ($var, $subdir_vars) = @$pair;
  67. foreach my $k (@$subdir_vars) {
  68. my $kr = "${k}_REL_${var}";
  69. my $v2 = $values{$k} || [ '.' ];
  70. $values{$k} = []; # We're rebuilding it
  71. print STDERR "DEBUG: $k = ",
  72. (scalar @$v2 > 1 ? "[ " . join(", ", @$v2) . " ]" : $v2->[0]),
  73. " => ";
  74. foreach my $v (@$v2) {
  75. if (File::Spec->file_name_is_absolute($v)) {
  76. push @{$values{$k}}, $v;
  77. push @{$values{$kr}},
  78. File::Spec->abs2rel($v, $values{$var}->[0]);
  79. } else {
  80. push @{$values{$kr}}, $v;
  81. push @{$values{$k}},
  82. File::Spec->rel2abs($v, $values{$var}->[0]);
  83. }
  84. }
  85. print STDERR join(", ",
  86. map {
  87. my $v = $values{$_};
  88. "$_ = " . (scalar @$v > 1
  89. ? "[ " . join(", ", @$v) . " ]"
  90. : $v->[0]);
  91. } ($k, $kr)),
  92. "\n";
  93. }
  94. }
  95. print <<_____;
  96. package OpenSSL::safe::installdata;
  97. use strict;
  98. use warnings;
  99. use Exporter;
  100. our \@ISA = qw(Exporter);
  101. our \@EXPORT = qw(
  102. _____
  103. foreach my $k (@absolutes) {
  104. print " \@$k\n";
  105. }
  106. foreach my $pair (@subdirs) {
  107. my ($var, $subdir_vars) = @$pair;
  108. foreach my $k (@$subdir_vars) {
  109. my $k2 = "${k}_REL_${var}";
  110. print " \@$k \@$k2\n";
  111. }
  112. }
  113. print <<_____;
  114. \$VERSION \@LDLIBS
  115. );
  116. _____
  117. foreach my $k (@absolutes) {
  118. print "our \@$k" . ' ' x (27 - length($k)) . "= ( '",
  119. join("', '", @{$values{$k}}),
  120. "' );\n";
  121. }
  122. foreach my $pair (@subdirs) {
  123. my ($var, $subdir_vars) = @$pair;
  124. foreach my $k (@$subdir_vars) {
  125. my $k2 = "${k}_REL_${var}";
  126. print "our \@$k" . ' ' x (27 - length($k)) . "= ( '",
  127. join("', '", @{$values{$k}}),
  128. "' );\n";
  129. print "our \@$k2" . ' ' x (27 - length($k2)) . "= ( '",
  130. join("', '", @{$values{$k2}}),
  131. "' );\n";
  132. }
  133. }
  134. print <<_____;
  135. our \$VERSION = '$values{VERSION}->[0]';
  136. our \@LDLIBS =
  137. # Unix and Windows use space separation, VMS uses comma separation
  138. \$^O eq 'VMS'
  139. ? split(/ *, */, '$values{LDLIBS}->[0]')
  140. : split(/ +/, '$values{LDLIBS}->[0]');
  141. 1;
  142. _____
  143. ######## Helpers
  144. # _pairs LIST
  145. #
  146. # This operates on an even-sized list, and returns a list of "ARRAY"
  147. # references, each containing two items from the given LIST.
  148. #
  149. # It is a quick cheap reimplementation of List::Util::pairs(), a function
  150. # we cannot use, because it only appeared in perl v5.19.3, and we claim to
  151. # support perl versions all the way back to v5.10.
  152. sub _pairs (@) {
  153. croak "Odd number of arguments" if @_ & 1;
  154. my @pairlist = ();
  155. while (@_) {
  156. my $x = [ shift, shift ];
  157. push @pairlist, $x;
  158. }
  159. return @pairlist;
  160. }