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