util/dofile.pl, util/perl/OpenSSL/Template.pm: move parts of dofile.pl
[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 actually expect to get the following hash tables from configdata:
24 #
25 #    %config
26 #    %target
27 #    %withargs
28 #    %unified_info
29 #
30 # We just do a minimal test to see that we got what we expected.
31 # $config{target} must exist as an absolute minimum.
32 die "You must run this script with -Mconfigdata\n"
33     if !exists($config{target});
34
35 # Check options ######################################################
36
37 my %opts = ();
38
39 # -o ORIGINATOR
40 #               declares ORIGINATOR as the originating script.
41 getopt('o', \%opts);
42
43 my @autowarntext = ("WARNING: do not edit!",
44                     "Generated"
45                     . (defined($opts{o}) ? " by ".$opts{o} : "")
46                     . (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
47
48 # Template setup #####################################################
49
50 my @template_settings =
51     @ARGV
52     ? map { { TYPE => 'FILE', SOURCE => $_, FILENAME => $_ } } @ARGV
53     : ( { TYPE => 'FILEHANDLE', SOURCE => \*STDIN, FILENAME => '<stdin>' } );
54
55 # Engage! ############################################################
56
57 my $prepend = <<"_____";
58 use File::Spec::Functions;
59 _____
60 $prepend .= <<"_____" if defined $target{perl_platform};
61 use lib "$FindBin::Bin/../Configurations";
62 use lib '$config{builddir}';
63 use platform;
64 _____
65
66 foreach (@template_settings) {
67     my $template = OpenSSL::Template->new(%$_);
68     $template->fill_in(%$_,
69                        OUTPUT => \*STDOUT,
70                        HASH => { config => \%config,
71                                  target => \%target,
72                                  disabled => \%disabled,
73                                  withargs => \%withargs,
74                                  unified_info => \%unified_info,
75                                  autowarntext => \@autowarntext },
76                        PREPEND => $prepend,
77                        # To ensure that global variables and functions
78                        # defined in one template stick around for the
79                        # next, making them combinable
80                        PACKAGE => 'OpenSSL::safe');
81 }