]>
Commit | Line | Data |
---|---|---|
e0a65194 | 1 | #! /usr/bin/env perl |
b0edda11 | 2 | # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
291e94df | 3 | # |
9059ab42 | 4 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
e0a65194 RS |
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 | ||
291e94df RL |
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; | |
632b3092 | 16 | |
1935a586 RL |
17 | use FindBin; |
18 | use lib "$FindBin::Bin/perl"; | |
486f1491 RL |
19 | use OpenSSL::fallback "$FindBin::Bin/../external/perl/MODULES.txt"; |
20 | use Getopt::Std; | |
21 | use OpenSSL::Template; | |
9ab6fc59 | 22 | |
9fcb9702 RS |
23 | # We expect to get a lot of information from configdata, so check that |
24 | # it was part of our commandline. | |
486f1491 RL |
25 | die "You must run this script with -Mconfigdata\n" |
26 | if !exists($config{target}); | |
291e94df | 27 | |
9ab6fc59 RL |
28 | # Check options ###################################################### |
29 | ||
9ab6fc59 RL |
30 | # -o ORIGINATOR |
31 | # declares ORIGINATOR as the originating script. | |
9fcb9702 RS |
32 | # -i .ext Like Perl's edit-in-place -i flag |
33 | my %opts = (); | |
34 | getopt('oi', \%opts); | |
9ab6fc59 | 35 | |
9fcb9702 RS |
36 | my @autowarntext = ( |
37 | "WARNING: do not edit!", | |
38 | "Generated" | |
39 | . (defined($opts{o}) ? " by $opts{o}" : "") | |
40 | . (scalar(@ARGV) > 0 ? " from " .join(", ", @ARGV) : "") | |
41 | ); | |
42 | ||
43 | die "Must have input files" | |
44 | if defined($opts{i}) and scalar(@ARGV) == 0; | |
9ab6fc59 | 45 | |
486f1491 | 46 | # Template setup ##################################################### |
291e94df | 47 | |
486f1491 | 48 | my @template_settings = |
291e94df | 49 | @ARGV |
486f1491 RL |
50 | ? map { { TYPE => 'FILE', SOURCE => $_, FILENAME => $_ } } @ARGV |
51 | : ( { TYPE => 'FILEHANDLE', SOURCE => \*STDIN, FILENAME => '<stdin>' } ); | |
291e94df | 52 | |
9fcb9702 RS |
53 | # Error callback; print message, set status, return "stop processing" |
54 | my $failed = 0; | |
55 | sub errorcallback { | |
56 | my %args = @_; | |
57 | print STDERR $args{error}; | |
58 | $failed++; | |
59 | return undef; | |
60 | } | |
61 | ||
291e94df RL |
62 | # Engage! ############################################################ |
63 | ||
486f1491 | 64 | my $prepend = <<"_____"; |
d7e4932e | 65 | use File::Spec::Functions; |
486f1491 | 66 | use lib "$FindBin::Bin/../Configurations"; |
d7e4932e RL |
67 | use lib '$config{builddir}'; |
68 | use platform; | |
486f1491 RL |
69 | _____ |
70 | ||
71 | foreach (@template_settings) { | |
72 | my $template = OpenSSL::Template->new(%$_); | |
9fcb9702 RS |
73 | die "Couldn't create template: $Text::Template::ERROR" |
74 | if !defined($template); | |
75 | ||
76 | my $result = $template->fill_in(%$_, | |
486f1491 RL |
77 | HASH => { config => \%config, |
78 | target => \%target, | |
79 | disabled => \%disabled, | |
80 | withargs => \%withargs, | |
81 | unified_info => \%unified_info, | |
82 | autowarntext => \@autowarntext }, | |
9fcb9702 | 83 | BROKEN => \&errorcallback, |
486f1491 RL |
84 | PREPEND => $prepend, |
85 | # To ensure that global variables and functions | |
86 | # defined in one template stick around for the | |
87 | # next, making them combinable | |
88 | PACKAGE => 'OpenSSL::safe'); | |
9fcb9702 RS |
89 | exit 1 if $failed; |
90 | ||
91 | if (defined($opts{i})) { | |
92 | my $in = $_->{FILENAME}; | |
93 | my $out = $in; | |
94 | $out =~ s/$opts{i}$//; | |
95 | die "Cannot replace file in-place $in" | |
96 | if $in eq $out; | |
97 | open OFH, ">$out" | |
98 | or die "Can't open $out, $!"; | |
99 | print OFH $result; | |
100 | close OFH; | |
101 | } else { | |
102 | print $result; | |
103 | } | |
deb02194 | 104 | } |