Update source files for pre-3.0 deprecation
[openssl.git] / util / dofile.pl
1 #! /usr/bin/env perl
2 # Copyright 2016-2018 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 # We expect to get a lot of information from configdata, so check that
24 # it was part of our commandline.
25 die "You must run this script with -Mconfigdata\n"
26     if !exists($config{target});
27
28 # Check options ######################################################
29
30 # -o ORIGINATOR
31 #               declares ORIGINATOR as the originating script.
32 # -i .ext       Like Perl's edit-in-place -i flag
33 my %opts = ();
34 getopt('oi', \%opts);
35
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;
45
46 # Template setup #####################################################
47
48 my @template_settings =
49     @ARGV
50     ? map { { TYPE => 'FILE', SOURCE => $_, FILENAME => $_ } } @ARGV
51     : ( { TYPE => 'FILEHANDLE', SOURCE => \*STDIN, FILENAME => '<stdin>' } );
52
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
62 # Engage! ############################################################
63
64 my $prepend = <<"_____";
65 use File::Spec::Functions;
66 _____
67 $prepend .= <<"_____" if defined $target{perl_platform};
68 use lib "$FindBin::Bin/../Configurations";
69 use lib '$config{builddir}';
70 use platform;
71 _____
72
73 foreach (@template_settings) {
74     my $template = OpenSSL::Template->new(%$_);
75     die "Couldn't create template: $Text::Template::ERROR"
76         if !defined($template);
77
78     my $result = $template->fill_in(%$_,
79                        HASH => { config => \%config,
80                                  target => \%target,
81                                  disabled => \%disabled,
82                                  withargs => \%withargs,
83                                  unified_info => \%unified_info,
84                                  autowarntext => \@autowarntext },
85                        BROKEN => \&errorcallback,
86                        PREPEND => $prepend,
87                        # To ensure that global variables and functions
88                        # defined in one template stick around for the
89                        # next, making them combinable
90                        PACKAGE => 'OpenSSL::safe');
91     exit 1 if $failed;
92
93     if (defined($opts{i})) {
94         my $in = $_->{FILENAME};
95         my $out = $in;
96         $out =~ s/$opts{i}$//;
97         die "Cannot replace file in-place $in"
98             if $in eq $out;
99         open OFH, ">$out"
100             or die "Can't open $out, $!";
101         print OFH $result;
102         close OFH;
103     } else {
104         print $result;
105     }
106 }