Refactor file writing - introduce template driven file writing
authorRichard Levitte <levitte@openssl.org>
Mon, 18 May 2015 20:35:23 +0000 (22:35 +0200)
committerRichard Levitte <richard@levitte.org>
Fri, 22 Jan 2016 22:12:22 +0000 (23:12 +0100)
apps/CA.pl and tools/c_rehash are built from template files.  So far,
this was done by Configure, which created its own problems as it
forced everyone to reconfigure just because one of the template files
had changed.
Instead, have those files created as part of the normal build in apps/
and in tools/.

Furthermore, this prepares for a future where Configure may produce
entirely other build files than Makefile, and the latter can't be
guaranteed to be the holder of all information for other scripts.
Instead, configdata.pm (described below) becomes the center of
configuration information.

This introduces a few new things:

%config         a hash table to hold all kinds of configuration data
                that can be used by any other script.
configdata.pm   a perl module that Configure writes.  It currently
                holds the hash tables %config and %target.
util/dofile.pl  a script that takes a template on STDIN and outputs
                the result after applying configuration data on it.
                It's supposed to be called like this:

                        perl -I$(TOP) -Mconfigdata < template > result

                or

                        perl -I$(TOP) -Mconfigdata templ1 templ2 ... > result

                Note: util/dofile.pl requires Text::Template.

As part of this changed, remove a number of variables that are really
just copies of entries in %target, and use %target directly.  The
exceptions are $target{cflags} and $target{lflags}, they do get copied
to $cflags and $lflags.  The reason for this is that those variable
potentially go through a lot of changes and would rather deserve a
place in %config.  That, however, is for another commit.

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
.gitignore
Configure
apps/CA.pl.in
apps/Makefile.in
tools/Makefile.in
tools/c_rehash.in
util/dofile.pl [new file with mode: 0644]

index 281905b63fdf608fe6b68bc089ae1aedd698f55d..0a3f054a1e927628a9afa8d438474c3117a04de5 100644 (file)
@@ -21,6 +21,7 @@
 /makefile.*
 /out.*
 /tmp.*
+/configdata.pm
 
 **/Makefile
 
index 0a6d812a37c6235934ad0a8317005147f181278d..45103ff063c4b11e267c9e8c951b6fb192ed00f3 100755 (executable)
--- a/Configure
+++ b/Configure
@@ -150,6 +150,7 @@ my $apitable = {
 
 my $base_target = "BASE";   # The template that all other inherit from
 our %table = ();
+our %config = ();
 
 # Forward declarations ###############################################
 
@@ -172,9 +173,11 @@ foreach (sort glob($pattern) ) {
     &read_config($_);
 }
 
-my $prefix="";
+
+$config{perl};
+$config{prefix}="";
+$config{openssldir}="";
 my $libdir="";
-my $openssldir="";
 my $exe_ext="";
 my $install_prefix= "$ENV{'INSTALL_PREFIX'}";
 my $cross_compile_prefix="";
@@ -192,7 +195,6 @@ my @skip=();
 my $Makefile="Makefile";
 my $processor="";
 my $default_ranlib;
-my $perl;
 my $fips=0;
 
 # Known TLS and DTLS protocols
@@ -539,7 +541,7 @@ foreach (@argvcopy)
                {
                if (/^--prefix=(.*)$/)
                        {
-                       $prefix=$1;
+                       $config{prefix}=$1;
                        }
                elsif (/^--api=(.*)$/)
                        {
@@ -551,7 +553,7 @@ foreach (@argvcopy)
                        }
                elsif (/^--openssldir=(.*)$/)
                        {
-                       $openssldir=$1;
+                       $config{openssldir}=$1;
                        }
                elsif (/^--install.prefix=(.*)$/)
                        {
@@ -750,7 +752,7 @@ if ($d) {
        $target = $t;
     }
 }
-
+$config{target} = $target;
 delete $table{$base_target}->{template}; # or the next test will fail.
 my %target = ( %{$table{$base_target}}, resolve_config($target) );
 
@@ -760,21 +762,20 @@ $exe_ext=".exe" if ($target eq "Cygwin" || $target eq "DJGPP" || $target =~ /^mi
 $exe_ext=".nlm" if ($target =~ /netware/);
 $exe_ext=".pm"  if ($target =~ /vos/);
 
-$default_ranlib= &which("ranlib") or $default_ranlib="true";
-$perl=$ENV{'PERL'} or $perl=&which("perl5") or $perl=&which("perl")
-  or $perl="perl";
-my $make = $ENV{'MAKE'} || "make";
+$default_ranlib        = which("ranlib") || "true";
+$config{perl}  = $ENV{'PERL'} || which("perl5") || which("perl") || "perl";
+my $make       = $ENV{'MAKE'} || "make";
 
 $cross_compile_prefix=$ENV{'CROSS_COMPILE'} if $cross_compile_prefix eq "";
 
-$prefix = "/usr/local" if !$prefix;
-$openssldir = "ssl" if !$openssldir;
-$openssldir = catdir($prefix, $openssldir)
-    unless file_name_is_absolute($openssldir);
+$config{prefix} = "/usr/local" if !$config{prefix};
+$config{openssldir} = "ssl" if !$config{openssldir};
+$config{openssldir} = catdir($config{prefix}, $config{openssldir})
+    unless file_name_is_absolute($config{openssldir});
 
 
 # Allow environment CC to override compiler...
-my $cc = $ENV{CC} || $target{cc};
+$target{cc} = $ENV{CC} || $target{cc};
 
 # For cflags and lflags, add the debug_ or release_ attributes
 # Do it in such a way that no spurious space is appended (hence the grep).
@@ -785,49 +786,19 @@ my $lflags = join(" ",
                  grep { $_ } ($target{lflags},
                               $target{$build_prefix."lflags"}));
 
-my $unistd = $target{unistd};
-my $thread_cflag = $target{thread_cflag};
-my $sys_id = $target{sys_id};
-my $bn_ops = $target{bn_ops};
-my $cpuid_obj = $target{cpuid_obj};
-my $bn_obj = $target{bn_obj};
-my $ec_obj = $target{ec_obj};
-my $des_obj = $target{des_obj};
-my $aes_obj = $target{aes_obj};
-my $bf_obj = $target{bf_obj};
-my $md5_obj = $target{md5_obj};
-my $sha1_obj = $target{sha1_obj};
-my $cast_obj = $target{cast_obj};
-my $rc4_obj = $target{rc4_obj};
-my $rmd160_obj = $target{rmd160_obj};
-my $rc5_obj = $target{rc5_obj};
-my $wp_obj = $target{wp_obj};
-my $cmll_obj = $target{cmll_obj};
-my $modes_obj = $target{modes_obj};
-my $engines_obj = $target{engines_obj};
-my $chacha_obj = $target{chacha_obj};
-my $poly1305_obj = $target{poly1305_obj};
-my $perlasm_scheme = $target{perlasm_scheme};
-my $dso_scheme = $target{dso_scheme};
-my $shared_target = $target{shared_target};
-my $shared_cflag = $target{shared_cflag};
-my $shared_ldflag = $target{shared_ldflag};
-my $shared_extension = $target{shared_extension};
-my $ranlib = $ENV{'RANLIB'} || $target{ranlib};
-my $ar = $ENV{'AR'} || "ar";
-my $arflags = $target{arflags};
-my $multilib = $target{multilib};
-my @build_scheme =
-    ref($target{build_scheme}) eq "ARRAY"
-    ? @{$target{build_scheme}} : ( $target{build_scheme} );
-
-# if $prefix/lib$multilib is not an existing directory, then
+$target{ranlib} = $ENV{'RANLIB'} || $target{ranlib} || $default_ranlib;
+$target{ar} = $ENV{'AR'} || "ar";
+# Make sure build_scheme is consistent.
+$target{build_scheme} = [ $target{build_scheme} ]
+    if ref($target{build_scheme}) ne "ARRAY";
+
+# if $config{prefix}/lib$target{multilib} is not an existing directory, then
 # assume that it's not searched by linker automatically, in
-# which case adding $multilib suffix causes more grief than
+# which case adding $target{multilib} suffix causes more grief than
 # we're ready to tolerate, so don't...
-$multilib="" if !-d "$prefix/lib$multilib";
+$target{multilib}="" if !-d "$config{prefix}/lib$target{multilib}";
 
-$libdir="lib$multilib" if $libdir eq "";
+$libdir="lib$target{multilib}" if $libdir eq "";
 
 $cflags = "$cflags$exp_cflags";
 
@@ -836,10 +807,10 @@ my ($prelflags,$postlflags)=split('%',$lflags);
 if (defined($postlflags))      { $lflags=$postlflags;  }
 else                           { $lflags=$prelflags; undef $prelflags; }
 
-if ($target =~ /^mingw/ && `$cc --target-help 2>&1` !~ m/\-mno\-cygwin/m)
+if ($target =~ /^mingw/ && `$target{cc} --target-help 2>&1` !~ m/\-mno\-cygwin/m)
        {
        $cflags =~ s/\-mno\-cygwin\s*//;
-       $shared_ldflag =~ s/\-mno\-cygwin\s*//;
+       $target{shared_ldflag} =~ s/\-mno\-cygwin\s*//;
        }
 
 if ($target =~ /linux.*\-mips/ && !$no_asm && $flags !~ /\-m(ips|arch=)/) {
@@ -861,32 +832,32 @@ else                      { $no_user_cflags=1;       }
 # by a define "DSO_<name>" ... we translate the "dso_scheme" config
 # string entry into using the following logic;
 my $dso_cflags;
-if (!$no_dso && $dso_scheme ne "")
+if (!$no_dso && $target{dso_scheme} ne "")
        {
-       $dso_scheme =~ tr/[a-z]/[A-Z]/;
-       if ($dso_scheme eq "DLFCN")
+       $target{dso_scheme} =~ tr/[a-z]/[A-Z]/;
+       if ($target{dso_scheme} eq "DLFCN")
                {
                $dso_cflags = "-DDSO_DLFCN -DHAVE_DLFCN_H";
                }
-       elsif ($dso_scheme eq "DLFCN_NO_H")
+       elsif ($target{dso_scheme} eq "DLFCN_NO_H")
                {
                $dso_cflags = "-DDSO_DLFCN";
                }
        else
                {
-               $dso_cflags = "-DDSO_$dso_scheme";
+               $dso_cflags = "-DDSO_$target{dso_scheme}";
                }
        $cflags = "$dso_cflags $cflags";
        }
 
 my $thread_cflags;
 my $thread_defines;
-if ($thread_cflag ne "(unknown)" && !$no_threads)
+if ($target{thread_cflag} ne "(unknown)" && !$no_threads)
        {
        # If we know how to do it, support threads by default.
        $threads = 1;
        }
-if ($thread_cflag eq "(unknown)" && $threads)
+if ($target{thread_cflag} eq "(unknown)" && $threads)
        {
        # If the user asked for "threads", [s]he is also expected to
        # provide any system-dependent compiler options that are
@@ -902,10 +873,10 @@ if ($thread_cflag eq "(unknown)" && $threads)
        }
 else
        {
-       $thread_cflags="-DOPENSSL_THREADS $thread_cflag $cflags";
+       $thread_cflags="-DOPENSSL_THREADS $target{thread_cflag} $cflags";
        $thread_defines .= "#define OPENSSL_THREADS\n";
 #      my $def;
-#      foreach $def (split ' ',$thread_cflag)
+#      foreach $def (split ' ',$target{thread_cflag})
 #              {
 #              if ($def =~ s/^-D// && $def !~ /^_/)
 #                      {
@@ -955,20 +926,20 @@ if (defined($disabled{"deprecated"})) {
 
 # You will find shlib_mark1 and shlib_mark2 explained in Makefile.in
 my $shared_mark = "";
-if ($shared_target eq "")
+if ($target{shared_target} eq "")
        {
        $no_shared_warn = 1 if !$no_shared && !$fips;
        $no_shared = 1;
        }
 if (!$no_shared)
        {
-       if ($shared_cflag ne "")
+       if ($target{shared_cflag} ne "")
                {
-               $cflags = "$shared_cflag -DOPENSSL_PIC $cflags";
+               $cflags = "$target{shared_cflag} -DOPENSSL_PIC $cflags";
                }
        }
 
-if ($build_scheme[0] ne "mk1mf")
+if ($target{build_scheme}->[0] ne "mk1mf")
        {
        # add {no-}static-engine to options to allow mkdef.pl to work without extra arguments
        if ($no_shared)
@@ -989,7 +960,7 @@ if ($build_scheme[0] ne "mk1mf")
 if ($target =~ /\-icc$/)       # Intel C compiler
        {
        my $iccver=0;
-       if (open(FD,"$cc -V 2>&1 |"))
+       if (open(FD,"$target{cc} -V 2>&1 |"))
                {
                while(<FD>) { $iccver=$1 if (/Version ([0-9]+)\./); }
                close(FD);
@@ -1028,80 +999,80 @@ if ($target =~ /\-icc$/) # Intel C compiler
 # linker only when --prefix is not /usr.
 if ($target =~ /^BSD\-/)
        {
-       $shared_ldflag.=" -Wl,-rpath,\$\$(LIBRPATH)" if ($prefix !~ m|^/usr[/]*$|);
+       $target{shared_ldflag}.=" -Wl,-rpath,\$\$(LIBRPATH)" if ($config{prefix} !~ m|^/usr[/]*$|);
        }
 
-if ($sys_id ne "")
+if ($target{sys_id} ne "")
        {
-       #$cflags="-DOPENSSL_SYS_$sys_id $cflags";
-       $openssl_sys_defines="#define OPENSSL_SYS_$sys_id\n";
+       #$cflags="-DOPENSSL_SYS_$target{sys_id} $cflags";
+       $openssl_sys_defines="#define OPENSSL_SYS_$target{sys_id}\n";
        }
 
-if ($ranlib eq "")
+if ($target{ranlib} eq "")
        {
-       $ranlib = $default_ranlib;
+       $target{ranlib} = $default_ranlib;
        }
 
 if (!$no_asm) {
-    $cpuid_obj=$table{BASE}->{cpuid_obj} if ($processor eq "386");
-    $cpuid_obj.=" uplink.o uplink-x86.o" if ($cflags =~ /\-DOPENSSL_USE_APPLINK/);
+    $target{cpuid_obj}=$table{BASE}->{cpuid_obj} if ($processor eq "386");
+    $target{cpuid_obj}.=" uplink.o uplink-x86.o" if ($cflags =~ /\-DOPENSSL_USE_APPLINK/);
 
-    $bn_obj =~ s/\w+-gf2m.o// if (defined($disabled{ec2m}));
+    $target{bn_obj} =~ s/\w+-gf2m.o// if (defined($disabled{ec2m}));
 
     # bn-586 is the only one implementing bn_*_part_words
-    $cflags.=" -DOPENSSL_BN_ASM_PART_WORDS" if ($bn_obj =~ /bn-586/);
-    $cflags.=" -DOPENSSL_IA32_SSE2" if (!$no_sse2 && $bn_obj =~ /86/);
+    $cflags.=" -DOPENSSL_BN_ASM_PART_WORDS" if ($target{bn_obj} =~ /bn-586/);
+    $cflags.=" -DOPENSSL_IA32_SSE2" if (!$no_sse2 && $target{bn_obj} =~ /86/);
 
-    $cflags.=" -DOPENSSL_BN_ASM_MONT" if ($bn_obj =~ /-mont/);
-    $cflags.=" -DOPENSSL_BN_ASM_MONT5" if ($bn_obj =~ /-mont5/);
-    $cflags.=" -DOPENSSL_BN_ASM_GF2m" if ($bn_obj =~ /-gf2m/);
+    $cflags.=" -DOPENSSL_BN_ASM_MONT" if ($target{bn_obj} =~ /-mont/);
+    $cflags.=" -DOPENSSL_BN_ASM_MONT5" if ($target{bn_obj} =~ /-mont5/);
+    $cflags.=" -DOPENSSL_BN_ASM_GF2m" if ($target{bn_obj} =~ /-gf2m/);
 
     if ($fips) {
        $openssl_other_defines.="#define OPENSSL_FIPS\n";
     }
 
-    if ($sha1_obj =~ /\.o$/) {
-       $cflags.=" -DSHA1_ASM"   if ($sha1_obj =~ /sx86/ || $sha1_obj =~ /sha1/);
-       $cflags.=" -DSHA256_ASM" if ($sha1_obj =~ /sha256/);
-       $cflags.=" -DSHA512_ASM" if ($sha1_obj =~ /sha512/);
-       if ($sha1_obj =~ /sse2/) {
+    if ($target{sha1_obj} =~ /\.o$/) {
+       $cflags.=" -DSHA1_ASM"   if ($target{sha1_obj} =~ /sx86/ || $target{sha1_obj} =~ /sha1/);
+       $cflags.=" -DSHA256_ASM" if ($target{sha1_obj} =~ /sha256/);
+       $cflags.=" -DSHA512_ASM" if ($target{sha1_obj} =~ /sha512/);
+       if ($target{sha1_obj} =~ /sse2/) {
            if ($no_sse2) {
-               $sha1_obj =~ s/\S*sse2\S+//;
+               $target{sha1_obj} =~ s/\S*sse2\S+//;
            } elsif ($cflags !~ /OPENSSL_IA32_SSE2/) {
                $cflags.=" -DOPENSSL_IA32_SSE2";
            }
        }
     }
-    if ($md5_obj =~ /\.o$/) {
+    if ($target{md5_obj} =~ /\.o$/) {
        $cflags.=" -DMD5_ASM";
     }
-    $cast_obj=$table{BASE}->{cast_obj} if (!$no_shared); # CAST assembler is not PIC
-    if ($rmd160_obj =~ /\.o$/) {
+    $target{cast_obj}=$table{BASE}->{cast_obj} if (!$no_shared); # CAST assembler is not PIC
+    if ($target{rmd160_obj} =~ /\.o$/) {
        $cflags.=" -DRMD160_ASM";
     }
-    if ($aes_obj =~ /\.o$/) {
-       $cflags.=" -DAES_ASM" if ($aes_obj =~ m/\baes\-/);;
+    if ($target{aes_obj} =~ /\.o$/) {
+       $cflags.=" -DAES_ASM" if ($target{aes_obj} =~ m/\baes\-/);;
        # aes-ctr.o is not a real file, only indication that assembler
        # module implements AES_ctr32_encrypt...
-       $cflags.=" -DAES_CTR_ASM" if ($aes_obj =~ s/\s*aes\-ctr\.o//);
+       $cflags.=" -DAES_CTR_ASM" if ($target{aes_obj} =~ s/\s*aes\-ctr\.o//);
        # aes-xts.o indicates presence of AES_xts_[en|de]crypt...
-       $cflags.=" -DAES_XTS_ASM" if ($aes_obj =~ s/\s*aes\-xts\.o//);
-       $aes_obj =~ s/\s*(vpaes|aesni)\-x86\.o//g if ($no_sse2);
-       $cflags.=" -DVPAES_ASM" if ($aes_obj =~ m/vpaes/);
-       $cflags.=" -DBSAES_ASM" if ($aes_obj =~ m/bsaes/);
+       $cflags.=" -DAES_XTS_ASM" if ($target{aes_obj} =~ s/\s*aes\-xts\.o//);
+       $target{aes_obj} =~ s/\s*(vpaes|aesni)\-x86\.o//g if ($no_sse2);
+       $cflags.=" -DVPAES_ASM" if ($target{aes_obj} =~ m/vpaes/);
+       $cflags.=" -DBSAES_ASM" if ($target{aes_obj} =~ m/bsaes/);
     }
-    if ($wp_obj =~ /mmx/ && $processor eq "386") {
-       $wp_obj=$table{BASE}->{wp_obj};
+    if ($target{wp_obj} =~ /mmx/ && $processor eq "386") {
+       $target{wp_obj}=$table{BASE}->{wp_obj};
     } elsif (!$disabled{"whirlpool"}) {
        $cflags.=" -DWHIRLPOOL_ASM";
     }
-    if ($modes_obj =~ /ghash\-/) {
+    if ($target{modes_obj} =~ /ghash\-/) {
        $cflags.=" -DGHASH_ASM";
     }
-    if ($ec_obj =~ /ecp_nistz256/) {
+    if ($target{ec_obj} =~ /ecp_nistz256/) {
        $cflags.=" -DECP_NISTZ256_ASM";
     }
-    if ($poly1305_obj =~ /\.o$/) {
+    if ($target{poly1305_obj} =~ /\.o$/) {
        $cflags.=" -DPOLY1305_ASM";
     }
 }
@@ -1148,8 +1119,8 @@ if (defined($api)) {
     $cflags .= " $apiflag";
 }
 
-my $ecc = $cc;
-$ecc = "clang" if `$cc --version 2>&1` =~ /clang/;
+my $ecc = $target{cc};
+$ecc = "clang" if `$target{cc} --version 2>&1` =~ /clang/;
 
 if ($strict_warnings)
        {
@@ -1172,15 +1143,60 @@ if ($strict_warnings)
                        {
                        $cflags .= " $wopt" unless ($cflags =~ /(^|\s)$wopt(\s|$)/)
                        }
-                if ($target =~ /^BSD-/)
-                       {
-                        $lflags .= " -lexecinfo";
-                        }
-                }
+               if ($target =~ /^BSD-/)
+                       {
+                       $lflags .= " -lexecinfo";
+                       }
+               }
        }
 
-open(IN,"<Makefile.in") || die "unable to read Makefile.in$!\n";
-unlink("$Makefile.new") || die "unable to remove old $Makefile.new:$!\n" if -e "$Makefile.new";
+# Write down our configuration where it fits #########################
+
+open(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
+print OUT <<"EOF";
+package configdata;
+
+use strict;
+use warnings;
+
+use Exporter;
+#use vars qw(\@ISA \@EXPORT);
+our \@ISA = qw(Exporter);
+our \@EXPORT = qw(\%config \%target);
+
+EOF
+print OUT "our %config = (\n";
+foreach (sort keys %config) {
+    if (ref($config{$_}) eq "ARRAY") {
+       print OUT "  ", $_, " => [ ", join(", ",
+                                          map { quotify("perl", $_) }
+                                          @{$config{$_}}), " ],\n";
+    } else {
+       print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
+    }
+}
+print OUT <<"EOF";
+);
+
+EOF
+print OUT "our %target = (\n";
+foreach (sort keys %target) {
+    if (ref($target{$_}) eq "ARRAY") {
+       print OUT "  ", $_, " => [ ", join(", ",
+                                          map { quotify("perl", $_) }
+                                          @{$target{$_}}), " ],\n";
+    } else {
+       print OUT "  ", $_, " => ", quotify("perl", $target{$_}), ",\n"
+    }
+}
+print OUT <<"EOF";
+);
+
+1;
+EOF
+close(OUT);
+
+open(IN,"<Makefile.in") || die "unable to read Makefile.in: $!\n";
 open(OUT,">$Makefile.new") || die "unable to create $Makefile.new:$!\n";
 print OUT "### Generated automatically from Makefile.in by Configure.\n\n";
 my $sdirs=0;
@@ -1206,10 +1222,10 @@ while (<IN>)
        s/^SHLIB_VERSION_HISTORY=.*/SHLIB_VERSION_HISTORY=$shlib_version_history/;
        s/^SHLIB_MAJOR=.*/SHLIB_MAJOR=$shlib_major/;
        s/^SHLIB_MINOR=.*/SHLIB_MINOR=$shlib_minor/;
-       s/^SHLIB_EXT=.*/SHLIB_EXT=$shared_extension/;
-       s/^INSTALLTOP=.*$/INSTALLTOP=$prefix/;
-       s/^MULTILIB=.*$/MULTILIB=$multilib/;
-       s/^OPENSSLDIR=.*$/OPENSSLDIR=$openssldir/;
+       s/^SHLIB_EXT=.*/SHLIB_EXT=$target{shared_extension}/;
+       s/^INSTALLTOP=.*$/INSTALLTOP=$config{prefix}/;
+       s/^MULTILIB=.*$/MULTILIB=$target{multilib}/;
+       s/^OPENSSLDIR=.*$/OPENSSLDIR=$config{openssldir}/;
        s/^LIBDIR=.*$/LIBDIR=$libdir/;
        s/^INSTALL_PREFIX=.*$/INSTALL_PREFIX=$install_prefix/;
        s/^PLATFORM=.*$/PLATFORM=$target/;
@@ -1218,45 +1234,45 @@ while (<IN>)
        s/^CONFIGURE_ARGS=.*$/CONFIGURE_ARGS=$argvstring/;
        if ($cross_compile_prefix)
                {
-               s/^CC=.*$/CROSS_COMPILE= $cross_compile_prefix\nCC= \$\(CROSS_COMPILE\)$cc/;
+               s/^CC=.*$/CROSS_COMPILE= $cross_compile_prefix\nCC= \$\(CROSS_COMPILE\)$target{cc}/;
                s/^AR=\s*/AR= \$\(CROSS_COMPILE\)/;
                s/^NM=\s*/NM= \$\(CROSS_COMPILE\)/;
                s/^RANLIB=\s*/RANLIB= \$\(CROSS_COMPILE\)/;
-               s/^MAKEDEPPROG=.*$/MAKEDEPPROG= \$\(CROSS_COMPILE\)$cc/ if $cc eq "gcc";
+               s/^MAKEDEPPROG=.*$/MAKEDEPPROG= \$\(CROSS_COMPILE\)$target{cc}/ if $target{cc} eq "gcc";
                }
        else    {
-               s/^CC=.*$/CC= $cc/;
-               s/^AR=\s*ar/AR= $ar/;
-               s/^RANLIB=.*/RANLIB= $ranlib/;
-               s/^MAKEDEPPROG=.*$/MAKEDEPPROG= $cc/ if $ecc eq "gcc" || $ecc eq "clang";
+               s/^CC=.*$/CC= $target{cc}/;
+               s/^AR=\s*ar/AR= $target{ar}/;
+               s/^RANLIB=.*/RANLIB= $target{ranlib}/;
+               s/^MAKEDEPPROG=.*$/MAKEDEPPROG= $target{cc}/ if $ecc eq "gcc" || $ecc eq "clang";
                }
        s/^CFLAG=.*$/CFLAG= $cflags/;
        s/^DEPFLAG=.*$/DEPFLAG=$depflags/;
        s/^PEX_LIBS=.*$/PEX_LIBS= $prelflags/;
        s/^EX_LIBS=.*$/EX_LIBS= $lflags/;
        s/^EXE_EXT=.*$/EXE_EXT= $exe_ext/;
-       s/^CPUID_OBJ=.*$/CPUID_OBJ= $cpuid_obj/;
-       s/^BN_ASM=.*$/BN_ASM= $bn_obj/;
-       s/^EC_ASM=.*$/EC_ASM= $ec_obj/;
-       s/^DES_ENC=.*$/DES_ENC= $des_obj/;
-       s/^AES_ENC=.*$/AES_ENC= $aes_obj/;
-       s/^BF_ENC=.*$/BF_ENC= $bf_obj/;
-       s/^CAST_ENC=.*$/CAST_ENC= $cast_obj/;
-       s/^RC4_ENC=.*$/RC4_ENC= $rc4_obj/;
-       s/^RC5_ENC=.*$/RC5_ENC= $rc5_obj/;
-       s/^MD5_ASM_OBJ=.*$/MD5_ASM_OBJ= $md5_obj/;
-       s/^SHA1_ASM_OBJ=.*$/SHA1_ASM_OBJ= $sha1_obj/;
-       s/^RMD160_ASM_OBJ=.*$/RMD160_ASM_OBJ= $rmd160_obj/;
-       s/^WP_ASM_OBJ=.*$/WP_ASM_OBJ= $wp_obj/;
-       s/^CMLL_ENC=.*$/CMLL_ENC= $cmll_obj/;
-       s/^MODES_ASM_OBJ.=*$/MODES_ASM_OBJ= $modes_obj/;
-       s/^CHACHA_ENC=.*$/CHACHA_ENC= $chacha_obj/;
-       s/^POLY1305_ASM_OBJ=.*$/POLY1305_ASM_OBJ= $poly1305_obj/;
-       s/^ENGINES_ASM_OBJ.=*$/ENGINES_ASM_OBJ= $engines_obj/;
-       s/^PERLASM_SCHEME=.*$/PERLASM_SCHEME= $perlasm_scheme/;
+       s/^CPUID_OBJ=.*$/CPUID_OBJ= $target{cpuid_obj}/;
+       s/^BN_ASM=.*$/BN_ASM= $target{bn_obj}/;
+       s/^EC_ASM=.*$/EC_ASM= $target{ec_obj}/;
+       s/^DES_ENC=.*$/DES_ENC= $target{des_obj}/;
+       s/^AES_ENC=.*$/AES_ENC= $target{aes_obj}/;
+       s/^BF_ENC=.*$/BF_ENC= $target{bf_obj}/;
+       s/^CAST_ENC=.*$/CAST_ENC= $target{cast_obj}/;
+       s/^RC4_ENC=.*$/RC4_ENC= $target{rc4_obj}/;
+       s/^RC5_ENC=.*$/RC5_ENC= $target{rc5_obj}/;
+       s/^MD5_ASM_OBJ=.*$/MD5_ASM_OBJ= $target{md5_obj}/;
+       s/^SHA1_ASM_OBJ=.*$/SHA1_ASM_OBJ= $target{sha1_obj}/;
+       s/^RMD160_ASM_OBJ=.*$/RMD160_ASM_OBJ= $target{rmd160_obj}/;
+       s/^WP_ASM_OBJ=.*$/WP_ASM_OBJ= $target{wp_obj}/;
+       s/^CMLL_ENC=.*$/CMLL_ENC= $target{cmll_obj}/;
+       s/^MODES_ASM_OBJ.=*$/MODES_ASM_OBJ= $target{modes_obj}/;
+       s/^ENGINES_ASM_OBJ.=*$/ENGINES_ASM_OBJ= $target{engines_obj}/;
+       s/^CHACHA_ENC=.*$/CHACHA_ENC= $target{chacha_obj}/;
+       s/^POLY1305_ASM_OBJ=.*$/POLY1305_ASM_OBJ= $target{poly1305_obj}/;
+       s/^PERLASM_SCHEME=.*$/PERLASM_SCHEME= $target{perlasm_scheme}/;
        s/^PROCESSOR=.*/PROCESSOR= $processor/;
-       s/^ARFLAGS=.*/ARFLAGS= $arflags/;
-       s/^PERL=.*/PERL= $perl/;
+       s/^ARFLAGS=.*/ARFLAGS= $target{arflags}/;
+       s/^PERL=.*/PERL= $config{perl}/;
        s/^LIBZLIB=.*/LIBZLIB=$withargs{"zlib-lib"}/;
        s/^ZLIB_INCLUDE=.*/ZLIB_INCLUDE=$withargs{"zlib-include"}/;
        s/^FIPSLIBDIR=.*/FIPSLIBDIR=$fipslibdir/;
@@ -1264,60 +1280,59 @@ while (<IN>)
        s/^SHARED_FIPS=.*/SHARED_FIPS=/;
        s/^SHLIBDIRS=.*/SHLIBDIRS= crypto ssl/;
        s/^BASEADDR=.*/BASEADDR=$baseaddr/;
-       s/^SHLIB_TARGET=.*/SHLIB_TARGET=$shared_target/;
+       s/^SHLIB_TARGET=.*/SHLIB_TARGET=$target{shared_target}/;
        s/^SHLIB_MARK=.*/SHLIB_MARK=$shared_mark/;
        s/^SHARED_LIBS=.*/SHARED_LIBS=\$(SHARED_CRYPTO) \$(SHARED_SSL)/ if (!$no_shared);
-       if ($shared_extension ne "" && $shared_extension =~ /^\.s([ol])\.[^\.]*$/)
+       if ($target{shared_extension} ne "" && $target{shared_extension} =~ /^\.s([ol])\.[^\.]*$/)
                {
                my $sotmp = $1;
                s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.s$sotmp/;
                }
-       elsif ($shared_extension ne "" && $shared_extension =~ /^\.[^\.]*\.dylib$/)
+       elsif ($target{shared_extension} ne "" && $target{shared_extension} =~ /^\.[^\.]*\.dylib$/)
                {
                s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.dylib/;
                }
-       elsif ($shared_extension ne "" && $shared_extension =~ /^\.s([ol])\.[^\.]*\.[^\.]*$/)
+       elsif ($target{shared_extension} ne "" && $target{shared_extension} =~ /^\.s([ol])\.[^\.]*\.[^\.]*$/)
                {
                my $sotmp = $1;
                s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.s$sotmp.\$(SHLIB_MAJOR) .s$sotmp/;
                }
-       elsif ($shared_extension ne "" && $shared_extension =~ /^\.[^\.]*\.[^\.]*\.dylib$/)
+       elsif ($target{shared_extension} ne "" && $target{shared_extension} =~ /^\.[^\.]*\.[^\.]*\.dylib$/)
                {
                s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.\$(SHLIB_MAJOR).dylib .dylib/;
                }
-       s/^SHARED_LDFLAGS=.*/SHARED_LDFLAGS=$shared_ldflag/;
+       s/^SHARED_LDFLAGS=.*/SHARED_LDFLAGS=$target{shared_ldflag}/;
        print OUT $_."\n";
        }
 close(IN);
 close(OUT);
-rename($Makefile,"$Makefile.orig") || die "unable to rename $Makefile\n" if -e $Makefile;
 rename("$Makefile.new",$Makefile) || die "unable to rename $Makefile.new\n";
 
-print "IsMK1MF       =", ($build_scheme[0] eq "mk1mf" ? "yes" : "no"), "\n";
-print "CC            =$cc\n";
+print "IsMK1MF       =", ($target{build_scheme}->[0] eq "mk1mf" ? "yes" : "no"), "\n";
+print "CC            =$target{cc}\n";
 print "CFLAG         =$cflags\n";
 print "EX_LIBS       =$lflags\n";
-print "CPUID_OBJ     =$cpuid_obj\n";
-print "BN_ASM        =$bn_obj\n";
-print "EC_ASM        =$ec_obj\n";
-print "DES_ENC       =$des_obj\n";
-print "AES_ENC       =$aes_obj\n";
-print "BF_ENC        =$bf_obj\n";
-print "CAST_ENC      =$cast_obj\n";
-print "RC4_ENC       =$rc4_obj\n";
-print "RC5_ENC       =$rc5_obj\n";
-print "MD5_OBJ_ASM   =$md5_obj\n";
-print "SHA1_OBJ_ASM  =$sha1_obj\n";
-print "RMD160_OBJ_ASM=$rmd160_obj\n";
-print "CMLL_ENC      =$cmll_obj\n";
-print "MODES_OBJ     =$modes_obj\n";
-print "ENGINES_OBJ   =$engines_obj\n";
-print "CHACHA_ENC    =$chacha_obj\n";
-print "POLY1305_OBJ  =$poly1305_obj\n";
+print "CPUID_OBJ     =$target{cpuid_obj}\n";
+print "BN_ASM        =$target{bn_obj}\n";
+print "EC_ASM        =$target{ec_obj}\n";
+print "DES_ENC       =$target{des_obj}\n";
+print "AES_ENC       =$target{aes_obj}\n";
+print "BF_ENC        =$target{bf_obj}\n";
+print "CAST_ENC      =$target{cast_obj}\n";
+print "RC4_ENC       =$target{rc4_obj}\n";
+print "RC5_ENC       =$target{rc5_obj}\n";
+print "MD5_OBJ_ASM   =$target{md5_obj}\n";
+print "SHA1_OBJ_ASM  =$target{sha1_obj}\n";
+print "RMD160_OBJ_ASM=$target{rmd160_obj}\n";
+print "CMLL_ENC      =$target{cmll_obj}\n";
+print "MODES_OBJ     =$target{modes_obj}\n";
+print "ENGINES_OBJ   =$target{engines_obj}\n";
+print "CHACHA_ENC    =$target{chacha_obj}\n";
+print "POLY1305_OBJ  =$target{poly1305_obj}\n";
 print "PROCESSOR     =$processor\n";
-print "RANLIB        =$ranlib\n";
-print "ARFLAGS       =$arflags\n";
-print "PERL          =$perl\n";
+print "RANLIB        =$target{ranlib}\n";
+print "ARFLAGS       =$target{arflags}\n";
+print "PERL          =$config{perl}\n";
 
 my $des_ptr=0;
 my $des_risc1=0;
@@ -1338,7 +1353,7 @@ my $export_var_as_fn=0;
 
 my $des_int;
 
-foreach (sort split(/\s+/,$bn_ops))
+foreach (sort split(/\s+/,$target{bn_ops}))
        {
        $des_ptr=1 if /DES_PTR/;
        $des_risc1=1 if /DES_RISC1/;
@@ -1368,7 +1383,6 @@ foreach (sort split(/\s+/,$bn_ops))
        }
 
 open(IN,'<crypto/opensslconf.h.in') || die "unable to read crypto/opensslconf.h.in:$!\n";
-unlink("include/openssl/opensslconf.h.new") || die "unable to remove old include/openssl/opensslconf.h.new:$!\n" if -e "include/openssl/opensslconf.h.new";
 open(OUT,'>include/openssl/opensslconf.h.new') || die "unable to create include/openssl/opensslconf.h.new:$!\n";
 print OUT "/* opensslconf.h */\n";
 print OUT "/* WARNING: Generated automatically from opensslconf.h.in by Configure. */\n\n";
@@ -1409,19 +1423,19 @@ print OUT "#ifdef OPENSSL_ALGORITHM_DEFINES\n";
 print OUT $openssl_algorithm_defines_trans;
 print OUT "#endif\n\n";
 
-print OUT "#define OPENSSL_CPUID_OBJ\n\n" if ($cpuid_obj ne "mem_clr.o");
+print OUT "#define OPENSSL_CPUID_OBJ\n\n" if ($target{cpuid_obj} ne "mem_clr.o");
 
 while (<IN>)
        {
        if      (/^#define\s+OPENSSLDIR/)
                {
-               my $foo = $openssldir;
+               my $foo = $config{openssldir};
                $foo =~ s/\\/\\\\/g;
                print OUT "#define OPENSSLDIR \"$foo\"\n";
                }
        elsif   (/^#define\s+ENGINESDIR/)
                {
-               my $foo = "$prefix/$libdir/engines";
+               my $foo = "$config{prefix}/$libdir/engines";
                $foo =~ s/\\/\\\\/g;
                print OUT "#define ENGINESDIR \"$foo\"\n";
                }
@@ -1432,7 +1446,7 @@ while (<IN>)
                        ($export_var_as_fn)?"define":"undef"; }
        elsif   (/^#define\s+OPENSSL_UNISTD/)
                {
-               print OUT "#define OPENSSL_UNISTD $unistd\n";
+               print OUT "#define OPENSSL_UNISTD $target{unistd}\n";
                }
        elsif   (/^#((define)|(undef))\s+SIXTY_FOUR_BIT_LONG/)
                { printf OUT "#%s SIXTY_FOUR_BIT_LONG\n",($b64l)?"define":"undef"; }
@@ -1490,7 +1504,6 @@ print OUT "#ifdef  __cplusplus\n";
 print OUT "}\n";
 print OUT "#endif\n";
 close(OUT);
-rename("include/openssl/opensslconf.h","include/openssl/opensslconf.h.bak") || die "unable to rename include/openssl/opensslconf.h\n" if -e "include/openssl/opensslconf.h";
 rename("include/openssl/opensslconf.h.new","include/openssl/opensslconf.h") || die "unable to rename include/openssl/opensslconf.h.new\n";
 
 
@@ -1541,18 +1554,11 @@ find(sub {
 
 my %builders = (
     unixmake => sub {
-       my $perlguess = $perl =~ m@^/@ ? $perl : '/usr/local/bin/perl';
-       my $make_command = "$make PERL=\'$perlguess\'";
+       my $make_command = "$make PERL=\'$config{perl}\'";
        my $make_targets = "";
        $make_targets .= " depend" if $depflags ne $default_depflags && $make_depend;
        (system $make_command.$make_targets) == 0 or die "make $make_targets failed"
            if $make_targets ne "";
-       &dofile("tools/c_rehash",$perlguess,
-               '^#!/'           => '#!%s',
-               '^my \$dir;$'    => 'my $dir = "' . $openssldir . '";',
-               '^my \$prefix;$' => 'my $prefix = "' . $prefix . '";');
-       &dofile("apps/CA.pl",$perlguess,
-               '^#!/'           => '#!%s');
        if ($depflags ne $default_depflags && !$make_depend) {
             $warn_make_depend++;
         }
@@ -1571,7 +1577,7 @@ EOF
        close(OUT);
 
        # create the ms/version32.rc file if needed
-       if (! grep /^netware/, @build_scheme) {
+       if (! grep /^netware/, @{$target{build_scheme}}) {
            my ($v1, $v2, $v3, $v4);
            if ($version_num =~ /^0x([0-9a-f]{1})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{1})L$/i) {
                $v1=hex $1;
@@ -1634,7 +1640,7 @@ EOF
     },
     );
 
-my ($builder, @builder_opts) = @build_scheme;
+my ($builder, @builder_opts) = @{$target{build_scheme}};
 $builders{$builder}->(@builder_opts);
 
 print <<"EOF";
@@ -1968,23 +1974,109 @@ sub which
                }
        }
 
-sub dofile
+sub print_table_entry
        {
-       my $f; my $p; my %m; my @a; my $k; my $ff;
-       ($f,$p,%m)=@_;
-
-       open(IN,"<$f.in") || open(IN,"<$f") || die "unable to open $f:$!\n";
-       @a=<IN>;
-       close(IN);
-       foreach $k (keys %m)
-               {
-               grep(/$k/ && ($_=sprintf($m{$k}."\n",$p)),@a);
+       my $target = shift;
+       my %target = resolve_config($target);
+       my $type = shift;
+
+       # Don't print the templates
+       return if $target{template};
+
+       if ($type eq "TABLE") {
+           print <<"EOF"
+
+*** $target
+\$cc           = $target{cc}
+\$cflags       = $target{cflags}
+\$debug_cflags   = $target{debug_cflags}
+\$release_cflags = $target{release_cflags}
+\$unistd       = $target{unistd}
+\$thread_cflag = $target{thread_cflag}
+\$sys_id       = $target{sys_id}
+\$lflags       = $target{lflags}
+\$debug_lflags   = $target{debug_lflags}
+\$release_lflags = $target{release_lflags}
+\$bn_ops       = $target{bn_ops}
+\$cpuid_obj    = $target{cpuid_obj}
+\$bn_obj       = $target{bn_obj}
+\$ec_obj       = $target{ec_obj}
+\$des_obj      = $target{des_obj}
+\$aes_obj      = $target{aes_obj}
+\$bf_obj       = $target{bf_obj}
+\$md5_obj      = $target{md5_obj}
+\$sha1_obj     = $target{sha1_obj}
+\$cast_obj     = $target{cast_obj}
+\$rc4_obj      = $target{rc4_obj}
+\$rmd160_obj   = $target{rmd160_obj}
+\$rc5_obj      = $target{rc5_obj}
+\$wp_obj       = $target{wp_obj}
+\$cmll_obj     = $target{cmll_obj}
+\$modes_obj    = $target{modes_obj}
+\$engines_obj  = $target{engines_obj}
+\$chacha_obj   = $target{chacha_obj}
+\$poly1305_obj = $target{poly1305_obj}
+\$perlasm_scheme = $target{perlasm_scheme}
+\$dso_scheme   = $target{dso_scheme}
+\$shared_target= $target{shared_target}
+\$shared_cflag = $target{shared_cflag}
+\$shared_ldflag = $target{shared_ldflag}
+\$shared_extension = $target{shared_extension}
+\$ranlib       = $target{ranlib}
+\$arflags      = $target{arflags}
+\$multilib     = $target{multilib}
+EOF
+       } elsif ($type eq "HASH") {
+           my @sequence = (
+               "cc",
+               "cflags",
+               "debug_cflags",
+               "release_cflags",
+               "unistd",
+               "thread_cflag",
+               "sys_id",
+               "lflags",
+               "debug_lflags",
+               "release_lflags",
+               "bn_ops",
+               "cpuid_obj",
+               "bn_obj",
+               "ec_obj",
+               "des_obj",
+               "aes_obj",
+               "bf_obj",
+               "md5_obj",
+               "sha1_obj",
+               "cast_obj",
+               "rc4_obj",
+               "rmd160_obj",
+               "rc5_obj",
+               "wp_obj",
+               "cmll_obj",
+               "modes_obj",
+               "engines_obj",
+               "chacha_obj",
+               "poly1305_obj",
+               "perlasm_scheme",
+               "dso_scheme",
+               "shared_target",
+               "shared_cflag",
+               "shared_ldflag",
+               "shared_extension",
+               "ranlib",
+               "arflags",
+               "multilib",
+               );
+           my $largest =
+               length((sort { length($a) <=> length($b) } @sequence)[-1]);
+           print "    '$target' => {\n";
+           foreach (@sequence) {
+               if ($target{$_}) {
+                   print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
                }
-       open(OUT,">$f.new") || die "unable to open $f.new:$!\n";
-       print OUT @a;
-       close(OUT);
-       rename($f,"$f.bak") || die "unable to rename $f\n" if -e $f;
-       rename("$f.new",$f) || die "unable to rename $f.new\n";
+           }
+           print "    },\n";
+       }
        }
 
 sub quotify {
index 23e116a5f588873fb3b7a3112778d3fee676f255..b54df5efa95ebb0c6ca982e872461ab576d3e7ad 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!{- $config{perl} -}
 #
 # Wrapper around the ca to make it easier to use
 # Edit CA.pl.in not CA.pl!
index c7153eeb9800755379e17b0cce88b0292bea6345..c765bd44603844535cab023e8b0ad710b5ca1ddd 100644 (file)
@@ -137,4 +137,9 @@ progs.h: progs.pl Makefile
        $(PERL) progs.pl $(COMMANDS) >progs.h
        $(RM) openssl.o
 
+CA.pl: CA.pl.in
+       $(PERL) -I$(TOP) -Mconfigdata $(TOP)/util/dofile.pl < CA.pl.in > CA.pl.new
+       mv CA.pl.new CA.pl
+
+
 # DO NOT DELETE THIS LINE -- make depend depends on it.
index d6c224a022839c51bc172a06d3950a2d26bb9488..b52b45f7d3b8c62c752a81425a53cf8ddd81bf56 100644 (file)
@@ -15,7 +15,9 @@ GENERAL=Makefile
 APPS= c_rehash
 MISC_APPS= c_hash c_info c_issuer c_name
 
-all:
+all: apps
+
+apps: $(APPS)
 
 install:
        @[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
@@ -56,4 +58,8 @@ clean:
 
 errors:
 
+c_rehash: c_rehash.in
+       $(PERL) -I$(TOP) -Mconfigdata $(TOP)/util/dofile.pl < c_rehash.in > c_rehash.new
+       mv c_rehash.new c_rehash
+
 # DO NOT DELETE THIS LINE -- make depend depends on it.
index 834de780ca247d6d5eaeccd993ffe4de2dad068d..912618c2300e10422f369bb24e6286b64496e0c3 100644 (file)
@@ -1,10 +1,10 @@
-#!/usr/local/bin/perl
+#!{- $config{perl} -}
 
 # Perl c_rehash script, scan all files in a directory
 # and add symbolic links to their hash values.
 
-my $dir;
-my $prefix;
+my $dir = {- quotify1($config{openssldir}) -};
+my $prefix = {- quotify1($config{prefix}) -};
 
 my $errorcount = 0;
 my $openssl = $ENV{OPENSSL} || "openssl";
diff --git a/util/dofile.pl b/util/dofile.pl
new file mode 100644 (file)
index 0000000..4bf90ba
--- /dev/null
@@ -0,0 +1,99 @@
+#! /usr/bin/perl
+#
+# Reads one or more template files and runs it through Text::Template
+#
+# It is assumed that this scripts is called with -Mconfigdata, a module
+# that holds configuration data in %config
+
+use strict;
+use warnings;
+use Text::Template;
+
+# We actually expect to get the following hash tables from configdata:
+#
+#    %config
+#    %target
+#    %withargs
+#
+# We just do a minimal test to see that we got what we expected.
+# $config{target} must exist as an absolute minimum.
+die "You must run this script with -Mconfigdata\n" if !exists($config{target});
+
+# Helper functions for the templates #################################
+
+# It might be practical to quotify some strings and have them protected
+# from possible harm.  These functions primarly quote things that might
+# be interpreted wrongly by a perl eval.
+
+# quotify1 STRING
+# This adds quotes (") around the given string, and escapes any $, @, \,
+# " and ' by prepending a \ to them.
+sub quotify1 {
+    my $s = shift @_;
+    $s =~ s/([\$\@\\"'])/\\$1/g;
+    '"'.$s.'"';
+}
+
+# quotify_l LIST
+# For each defined element in LIST (i.e. elements that aren't undef), have
+# it quotified with 'quotofy1'
+sub quotify_l {
+    map {
+        if (!defined($_)) {
+            ();
+        } else {
+            quotify1($_);
+        }
+    } @_;
+}
+
+# Error reporter #####################################################
+
+# The error reporter uses %lines to figure out exactly which file the
+# error happened and at what line.  Not that the line number may be
+# the start of a perl snippet rather than the exact line where it
+# happened.  Nothing we can do about that here.
+
+my %lines = ();
+sub broken {
+    my %args = @_;
+    my $filename = "<STDIN>";
+    my $deducelines = 0;
+    foreach (sort keys %lines) {
+        $filename = $lines{$_};
+        last if ($_ > $args{lineno});
+        $deducelines += $_;
+    }
+    print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
+    undef;
+}
+
+# Template reading ###################################################
+
+# Read in all the templates into $text, while keeping track of each
+# file and its size in lines, to try to help report errors with the
+# correct file name and line number.
+
+my $prev_linecount = 0;
+my $text =
+    @ARGV
+    ? join("", map { my $x = my $y = Text::Template::_load_text($_);
+                     my $linecount = $x =~ tr/\n//;
+                     $prev_linecount = ($linecount += $prev_linecount);
+                     $lines{$linecount} = $_;
+                     $x } @ARGV)
+    : join("", <STDIN>);
+
+# Engage! ############################################################
+
+# Load the full template (combination of files) into Text::Template
+# and fill it up with our data.  Output goes directly to STDOUT
+
+my $template = Text::Template->new(TYPE => 'STRING', SOURCE => $text );
+$template->fill_in(OUTPUT => \*STDOUT,
+                   HASH => { config => \%config,
+                             target => \%target,
+                             quotify1 => \&quotify1,
+                             quotify_l => \&quotify_l },
+                   DELIMITERS => [ "{-", "-}" ],
+                   BROKEN => \&broken);