dofile.pl 3.4 KB

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