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 |
|
---|
9 | # All variables are supposed to come from Makefile, in environment variable
|
---|
10 | # form, or passed as variable assignments on the command line.
|
---|
11 | # The result is a Perl module creating the package OpenSSL::safe::installdata.
|
---|
12 |
|
---|
13 | use File::Spec;
|
---|
14 | use List::Util qw(pairs);
|
---|
15 |
|
---|
16 | # These are expected to be set up as absolute directories
|
---|
17 | my @absolutes = qw(PREFIX libdir);
|
---|
18 | # These may be absolute directories, and if not, they are expected to be set up
|
---|
19 | # as subdirectories to PREFIX or LIBDIR. The order of the pairs is important,
|
---|
20 | # since the LIBDIR subdirectories depend on the calculation of LIBDIR from
|
---|
21 | # PREFIX.
|
---|
22 | my @subdirs = pairs (PREFIX => [ qw(BINDIR LIBDIR INCLUDEDIR APPLINKDIR) ],
|
---|
23 | LIBDIR => [ qw(ENGINESDIR MODULESDIR PKGCONFIGDIR
|
---|
24 | CMAKECONFIGDIR) ]);
|
---|
25 | # For completeness, other expected variables
|
---|
26 | my @others = qw(VERSION LDLIBS);
|
---|
27 |
|
---|
28 | my %all = ( );
|
---|
29 | foreach (@absolutes) { $all{$_} = 1 }
|
---|
30 | foreach (@subdirs) { foreach (@{$_->[1]}) { $all{$_} = 1 } }
|
---|
31 | foreach (@others) { $all{$_} = 1 }
|
---|
32 | print STDERR "DEBUG: all keys: ", join(", ", sort keys %all), "\n";
|
---|
33 |
|
---|
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 |
|
---|
42 | # warn if there are missing values, and also if there are unexpected values
|
---|
43 | foreach my $k (sort keys %all) {
|
---|
44 | warn "No value given for $k\n" unless $keys{$k};
|
---|
45 | }
|
---|
46 | foreach my $k (sort keys %keys) {
|
---|
47 | warn "Unknown variable $k\n" unless $all{$k};
|
---|
48 | }
|
---|
49 |
|
---|
50 | # This shouldn't be needed, but just in case we get relative paths that
|
---|
51 | # should be absolute, make sure they actually are.
|
---|
52 | foreach my $k (@absolutes) {
|
---|
53 | my $v = $values{$k} || [ '.' ];
|
---|
54 | die "Can't have more than one $k\n" if scalar @$v > 1;
|
---|
55 | print STDERR "DEBUG: $k = $v->[0] => ";
|
---|
56 | $v = [ map { File::Spec->rel2abs($_) } @$v ];
|
---|
57 | $values{$k} = $v;
|
---|
58 | print STDERR "$k = $v->[0]\n";
|
---|
59 | }
|
---|
60 |
|
---|
61 | # Absolute paths for the subdir variables are computed. This provides
|
---|
62 | # the usual form of values for names that have become norm, known as GNU
|
---|
63 | # installation paths.
|
---|
64 | # For the benefit of those that need it, the subdirectories are preserved
|
---|
65 | # as they are, using the same variable names, suffixed with '_REL_{var}',
|
---|
66 | # if they are indeed subdirectories. The '{var}' part of the name tells
|
---|
67 | # which other variable value they are relative to.
|
---|
68 | foreach my $pair (@subdirs) {
|
---|
69 | my ($var, $subdir_vars) = @$pair;
|
---|
70 | foreach my $k (@$subdir_vars) {
|
---|
71 | my $kr = "${k}_REL_${var}";
|
---|
72 | my $v2 = $values{$k} || [ '.' ];
|
---|
73 | $values{$k} = []; # We're rebuilding it
|
---|
74 | print STDERR "DEBUG: $k = ",
|
---|
75 | (scalar @$v2 > 1 ? "[ " . join(", ", @$v2) . " ]" : $v2->[0]),
|
---|
76 | " => ";
|
---|
77 | foreach my $v (@$v2) {
|
---|
78 | if (File::Spec->file_name_is_absolute($v)) {
|
---|
79 | push @{$values{$k}}, $v;
|
---|
80 | push @{$values{$kr}},
|
---|
81 | File::Spec->abs2rel($v, $values{$var}->[0]);
|
---|
82 | } else {
|
---|
83 | push @{$values{$kr}}, $v;
|
---|
84 | push @{$values{$k}},
|
---|
85 | File::Spec->rel2abs($v, $values{$var}->[0]);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | print STDERR join(", ",
|
---|
89 | map {
|
---|
90 | my $v = $values{$_};
|
---|
91 | "$_ = " . (scalar @$v > 1
|
---|
92 | ? "[ " . join(", ", @$v) . " ]"
|
---|
93 | : $v->[0]);
|
---|
94 | } ($k, $kr)),
|
---|
95 | "\n";
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | print <<_____;
|
---|
100 | package OpenSSL::safe::installdata;
|
---|
101 |
|
---|
102 | use strict;
|
---|
103 | use warnings;
|
---|
104 | use Exporter;
|
---|
105 | our \@ISA = qw(Exporter);
|
---|
106 | our \@EXPORT = qw(
|
---|
107 | _____
|
---|
108 |
|
---|
109 | foreach my $k (@absolutes) {
|
---|
110 | print " \@$k\n";
|
---|
111 | }
|
---|
112 | foreach my $pair (@subdirs) {
|
---|
113 | my ($var, $subdir_vars) = @$pair;
|
---|
114 | foreach my $k (@$subdir_vars) {
|
---|
115 | my $k2 = "${k}_REL_${var}";
|
---|
116 | print " \@$k \@$k2\n";
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | print <<_____;
|
---|
121 | \$VERSION \@LDLIBS
|
---|
122 | );
|
---|
123 |
|
---|
124 | _____
|
---|
125 |
|
---|
126 | foreach my $k (@absolutes) {
|
---|
127 | print "our \@$k" . ' ' x (27 - length($k)) . "= ( '",
|
---|
128 | join("', '", @{$values{$k}}),
|
---|
129 | "' );\n";
|
---|
130 | }
|
---|
131 | foreach my $pair (@subdirs) {
|
---|
132 | my ($var, $subdir_vars) = @$pair;
|
---|
133 | foreach my $k (@$subdir_vars) {
|
---|
134 | my $k2 = "${k}_REL_${var}";
|
---|
135 | print "our \@$k" . ' ' x (27 - length($k)) . "= ( '",
|
---|
136 | join("', '", @{$values{$k}}),
|
---|
137 | "' );\n";
|
---|
138 | print "our \@$k2" . ' ' x (27 - length($k2)) . "= ( '",
|
---|
139 | join("', '", @{$values{$k2}}),
|
---|
140 | "' );\n";
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | print <<_____;
|
---|
145 | our \$VERSION = '$values{VERSION}->[0]';
|
---|
146 | our \@LDLIBS =
|
---|
147 | # Unix and Windows use space separation, VMS uses comma separation
|
---|
148 | \$^O eq 'VMS'
|
---|
149 | ? split(/ *, */, '$values{LDLIBS}->[0]')
|
---|
150 | : split(/ +/, '$values{LDLIBS}->[0]');
|
---|
151 |
|
---|
152 | 1;
|
---|
153 | _____
|
---|