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