1 | #! /usr/bin/env perl
|
---|
2 | # Copyright 2016-2020 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 | # Reads one or more template files and runs it through Text::Template
|
---|
10 | #
|
---|
11 | # It is assumed that this scripts is called with -Mconfigdata, a module
|
---|
12 | # that holds configuration data in %config
|
---|
13 |
|
---|
14 | use strict;
|
---|
15 | use warnings;
|
---|
16 |
|
---|
17 | use FindBin;
|
---|
18 | use lib "$FindBin::Bin/perl";
|
---|
19 | use OpenSSL::fallback "$FindBin::Bin/../external/perl/MODULES.txt";
|
---|
20 | use Getopt::Std;
|
---|
21 | use OpenSSL::Template;
|
---|
22 |
|
---|
23 | use File::Basename; # VBOX addition for basename
|
---|
24 |
|
---|
25 | # We expect to get a lot of information from configdata, so check that
|
---|
26 | # it was part of our commandline.
|
---|
27 | die "You must run this script with -Mconfigdata\n"
|
---|
28 | if !exists($config{target});
|
---|
29 |
|
---|
30 | # Check options ######################################################
|
---|
31 |
|
---|
32 | # -o ORIGINATOR
|
---|
33 | # declares ORIGINATOR as the originating script.
|
---|
34 | # -i .ext Like Perl's edit-in-place -i flag
|
---|
35 | my %opts = ();
|
---|
36 | getopt('oi', \%opts);
|
---|
37 |
|
---|
38 | my @autowarntext = (
|
---|
39 | "WARNING: do not edit!",
|
---|
40 | "Generated"
|
---|
41 | . (defined($opts{o}) ? " by $opts{o}" : "")
|
---|
42 | . (scalar(@ARGV) > 0 ? " from " .join(", ", basename(@ARGV)) : "") # VBOX modified: added basename()
|
---|
43 | );
|
---|
44 |
|
---|
45 | if (defined($opts{s})) {
|
---|
46 | local $/ = undef;
|
---|
47 | open VARS, $opts{s} or die "Couldn't open $opts{s}, $!";
|
---|
48 | my $contents = <VARS>;
|
---|
49 | close VARS;
|
---|
50 | eval $contents;
|
---|
51 | die $@ if $@;
|
---|
52 | }
|
---|
53 | die "Must have input files"
|
---|
54 | if defined($opts{i}) and scalar(@ARGV) == 0;
|
---|
55 |
|
---|
56 | # Template setup #####################################################
|
---|
57 |
|
---|
58 | my @template_settings =
|
---|
59 | @ARGV
|
---|
60 | ? map { { TYPE => 'FILE', SOURCE => $_, FILENAME => $_ } } @ARGV
|
---|
61 | : ( { TYPE => 'FILEHANDLE', SOURCE => \*STDIN, FILENAME => '<stdin>' } );
|
---|
62 |
|
---|
63 | # Error callback; print message, set status, return "stop processing"
|
---|
64 | my $failed = 0;
|
---|
65 | sub errorcallback {
|
---|
66 | my %args = @_;
|
---|
67 | print STDERR $args{error};
|
---|
68 | $failed++;
|
---|
69 | return undef;
|
---|
70 | }
|
---|
71 |
|
---|
72 | # Engage! ############################################################
|
---|
73 |
|
---|
74 | my $prepend = <<"_____";
|
---|
75 | use File::Spec::Functions;
|
---|
76 | use lib '$FindBin::Bin/../Configurations';
|
---|
77 | use lib '$config{builddir}';
|
---|
78 | use platform;
|
---|
79 | _____
|
---|
80 |
|
---|
81 | foreach (@template_settings) {
|
---|
82 | my $template = OpenSSL::Template->new(%$_);
|
---|
83 | die "Couldn't create template: $Text::Template::ERROR"
|
---|
84 | if !defined($template);
|
---|
85 |
|
---|
86 | my $result = $template->fill_in(%$_,
|
---|
87 | HASH => { config => \%config,
|
---|
88 | target => \%target,
|
---|
89 | disabled => \%disabled,
|
---|
90 | withargs => \%withargs,
|
---|
91 | unified_info => \%unified_info,
|
---|
92 | autowarntext => \@autowarntext },
|
---|
93 | BROKEN => \&errorcallback,
|
---|
94 | PREPEND => $prepend,
|
---|
95 | # To ensure that global variables and functions
|
---|
96 | # defined in one template stick around for the
|
---|
97 | # next, making them combinable
|
---|
98 | PACKAGE => 'OpenSSL::safe');
|
---|
99 | exit 1 if $failed;
|
---|
100 |
|
---|
101 | if (defined($opts{i})) {
|
---|
102 | my $in = $_->{FILENAME};
|
---|
103 | my $out = $in;
|
---|
104 | $out =~ s/$opts{i}$//;
|
---|
105 | die "Cannot replace file in-place $in"
|
---|
106 | if $in eq $out;
|
---|
107 | open OFH, ">$out"
|
---|
108 | or die "Can't open $out, $!";
|
---|
109 | print OFH $result;
|
---|
110 | close OFH;
|
---|
111 | } else {
|
---|
112 | print $result;
|
---|
113 | }
|
---|
114 | }
|
---|