chiark / gitweb /
generate: Nested scope: Change `Eval' to `eval'
[subdirmk.git] / generate
index 5273e1543d2567a79096d3103a619fd3111cee72..747a79b8441a740dedeb641886849111c5077c27 100755 (executable)
--- a/generate
+++ b/generate
@@ -19,13 +19,32 @@ print "$0 @ARGV\n" or die $!;
 
 our $srcdir='.';
 
+# error handling methods:
+#
+# Error in input file, while $err_file and $. set, eg in most of
+# process_input_mk:
+#         err "message";
+#
+# Other input or usage errors:
+#         die "subdirmk: $file:$lno: problem\n";
+#         die "subdirmk: some problem not locatable in that way\n";
+#
+# Usage error:
+#         die "subdirmk $0: explanation of problem\n";
+#
+# System call error (not ENOENT) accessing input/output files:
+#         die "description of problem eg maybe erbing noun: $!\n";
+#
+# Bug detedcted in `generate':
+#         die "internal error (some information)?"; # or similar
+
 while (@ARGV && $ARGV[0] =~ m/^-/) {
     $_ = shift @ARGV;
     last if $_ eq '--';
     if (s/^--srcdir=//) {
        $srcdir=$';
     } else {
-       die "$0: unknown option \`$_'\n";
+       die "subdirmk $0: unknown option \`$_'\n";
     }
 }
 our @subdirs = @ARGV;
@@ -69,22 +88,41 @@ sub close_any_output_file() {
     $writing_output = undef;
 }
 
-sub o {
+sub oraw {
+    die 'internal error' unless defined $writing_output;
+    print O @_ or die "error writing $writing_output.tmp: $!\n";
+}
+
+sub oud { # undoubled
     if (defined $buffering_output) {
        $buffering_output .= $_ foreach @_;
        return;
     }
-    die unless defined $writing_output;
-    print O @_ or die "error writing $writing_output.tmp: $!\n";
+    oraw @_;
+}
+
+our $ddbl;
+
+sub od { # maybe $-doubled
+    if (!$ddbl) {
+       oud @_;
+       return;
+    }
+    foreach (@_) {
+       my $e = $_;
+       $e =~ s{\$}{\$\$}g;
+       oud $e;
+    }
 }
 
 sub start_output_file ($) {
     close_any_output_file();
     ($writing_output) = @_;
-    die "$writing_output ?" if $output_files{$writing_output}++;
+    die "internal error ($writing_output?)"
+       if $output_files{$writing_output}++;
     my $tmp = "$writing_output.tmp";
     open O, ">", $tmp or die "create $tmp: $!\n";
-    o "# autogenerated - do not edit\n";
+    oraw "# autogenerated - do not edit\n";
 }
 
 sub install_output_files () {
@@ -102,7 +140,7 @@ sub write_makefile ($$) {
     my $suppress_templates=
        '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
        ' MAKEFILE_TEMPLATES=)';
-    o <<END;
+    oraw <<END;
 default: all
 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
        \@:
@@ -131,6 +169,19 @@ sub set_dir_vars ($) {
     $var_prefix = "${var_prefix_name}_";
 }
 
+our $err_file;
+
+sub err ($) {
+    my ($m) = @_;
+    die "subdirmk: ${err_file}:$.: $m\n";
+}
+
+sub ddbl_only ($) {
+    my ($e) = @_;
+    return if $ddbl;
+    err "escape &$e is valid only during \$-doubling";
+}
+
 sub process_input_mk ($$$$);
 sub process_input_mk ($$$$) {
     my ($targets, $f, $esclitr, $enoent_ok) = @_;
@@ -147,11 +198,13 @@ sub process_input_mk ($$$$) {
 
     my $input = new IO::File $f, '<';
     if (!$input) {
-       die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
+       err "open $f: $!" unless $!==ENOENT && $enoent_ok;
        return;
     }
     $input_files{$f}++;
 
+    local $err_file=$f;
+
     my %srcdirmap = (
                  '^' => "\$(top_srcdir)${dir_suffix}",
                  '~' => "\$(top_srcdir)",
@@ -161,49 +214,90 @@ sub process_input_mk ($$$$) {
                 );
     $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
 
+    local $ddbl;
+    my @nest = (['']);
+
+    my $push_nest = sub {
+       my ($nk, $nndbl, $what) = @_;
+       unshift @nest, [ $nk, $ddbl, $what, $. ];
+       $ddbl = $nndbl;
+    };
+    my $pop_nest = sub {
+       my ($nk) = @_;
+       err "unexpectedly closed $nk in middle of $nest[0][0] ($nest[0][2])"
+           unless $nest[0][0] eq $nk;
+       $ddbl = (shift @nest)[1];
+    };
+
     while (<$input>) {
        if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
            $$esclitr = $1;
            $set_esc->();
            next;
-       } elsif (s#^\s*$esc\:(?=(-?)include)##) {
+       } elsif (s#^\s*$esc\:endm\s+$##) {
+           $pop_nest->('macro');
+           od "endef\n";
+           next;
+       } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) {
            $buffering_output='';
        } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
-           die "unknown directive $1";
+           err "unknown directive &:$1";
        } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
            my $t = $1 // 'all';
-           o target_varname($var_prefix, $t);
+           od target_varname($var_prefix, $t);
            $targets->{$t} //= [ ];
        }
        for (;;) {
-           unless (s{^(.*?)$esc}{}) { o $_; last; }
-           o $1;
-           if (s{^\\$esc}{}) { o "$$esclitr" }
-           elsif (s{^\\\$}{}) { o '$' }
+           err 'cannot $-double &-processed RHS of directive'
+               if $ddbl && defined $buffering_output;
+           unless ($nest[0][0] eq 'eval'
+                   ? s{^(.*?)($esc|[{}])}{}
+                   : s{^(.*?)($esc)}{}) { od $_; last; }
+           od $1;
+           if ($2 eq '{') {
+               $ddbl++;
+               next;
+           } elsif ($2 eq '}') {
+               next if --$ddbl;
+               $pop_nest->('eval');
+               od '}}';
+               next;
+           }
+           if (s{^\\$esc}{}) { od "$$esclitr" }
+           elsif (s{^\\\$}{}) { oud '$' }
            elsif (s{^\\\s+$}{}) { }
-           elsif (s{^$esc}{}) { o "$$esclitr$$esclitr" }
-           elsif (m{^(?=$caps_re)}) { o $var_prefix }
-           elsif (s{^\$([A-Za-z]\w+)}{}) { o "\$(${var_prefix}$1)" }
-           elsif (s{^([~^]?)(?=$lc_re)}{}) { o $pfxmap{$1} }
-           elsif (s{^_}{}) { o $var_prefix }
-           elsif (s{^=}{}) { o $var_prefix_name }
-           elsif (s{^([~^]?)/}{}) { o $pfxmap{$1} }
-           elsif (s{^\.}{}) { o $dir_name }
-           elsif (s{^([~^])\.}{}) { o $srcdirmap{$1} }
-           elsif (s{^([~^]?)(?=[ \t])}{}) {
-               my $prefix = $pfxmap{$1} // die;
+           elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" }
+           elsif (m{^(?=$caps_re)}) { od $var_prefix }
+           elsif (s{^\$([A-Za-z]\w+)}{}) { od "\$(${var_prefix}$1)" }
+           elsif (s{^([~^]?)(?=$lc_re)}{}) { od $pfxmap{$1} }
+           elsif (s{^_}{}) { od $var_prefix }
+           elsif (s{^=}{}) { od $var_prefix_name }
+           elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} }
+           elsif (s{^\.}{}) { od $dir_name }
+           elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} }
+           elsif (s{^\$\-}{}) { $ddbl=undef; }
+           elsif (s{^\$\+}{}) { $ddbl=1; }
+           elsif (s{^\$\(}{}) { ddbl_only($&); oud "\$("; }
+           elsif (s{^\$(\d+)}{}) { ddbl_only($&); oud "\$($1)"; }
+           elsif (s{^\$\{}{}) {
+               err 'macro invocation cannot be re-$-doubled' if $ddbl;
+               od '${eval ${call ';
+               $push_nest->('eval',1, '&${...}');
+           } elsif (s{^([~^]?)(?=[ \t])}{}) {
+               my $prefix = $pfxmap{$1} // die "internal error ($1?)";
                my $after='';
                if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
                s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
-               o $_;
+               od $_;
                $_ = $after;
            } elsif (s{^\#}{}) {
                $_ = '';
            } elsif (s{^![ \t]+}{}) {
-               o $_;
+               od $_;
                $_ = '';
            } else {
-               die "bad escape $$esclitr$_ ";
+               m{^.{0,5}};
+               err "bad &-escape \`$$esclitr$&'";
            }
        }
        if (defined $buffering_output) {
@@ -212,12 +306,17 @@ sub process_input_mk ($$$$) {
            if (m#^(-?)include\s+(\S+)\s+$#) {
                my $subf = "$srcdir/$2";
                process_input_mk($targets, $subf, $esclitr, $1);
-               o "\n";
+               od "\n";
+           } elsif (m#^macro\s+(\S+)\s+$#) {
+               od "define $1\n";
+               $push_nest->('macro', 1, '&:macro');
            } else {
-               die "internal error buffering directive $_ ";
+               die "internal error ($_?)";
            }
        }
     }
+    die "subdirmk: $f:$nest[0][3]: unclosed $nest[0][0] ($nest[0][2])\n"
+       if $nest[0][0];
     $input->error and die "read $f: $!\n";
     close $input or die "close $f: $!\n";
 }
@@ -259,7 +358,7 @@ sub process_subtree ($$) {
     foreach my $child (@{ $node->[1] }) {
        my @childpath = (@$path, $child->[0]);
        my $child_subdir = join '/', @childpath;
-       mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
+       mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!\n";
        push @{ $targets{$_} }, $child_subdir foreach
            process_subtree($child, \@childpath);
     }
@@ -272,14 +371,15 @@ sub process_subtree ($$) {
     } else {
        my $sdmk = "${dir_prefix}Subdir.sd.mk";
        if (stat $sdmk) {
-           die "$sdmk unexpectedly exists (${dir_prefix} not mentioned)";
+           die
+ "subdirmk: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)";
        } elsif ($!==ENOENT) {
        } else {
-           die "stat $sdmk: $!";
+           die "stat $sdmk: $!\n";
        }
     }
 
-    o "\n";
+    oraw "\n";
 
     my @targets = sort keys %targets;
     foreach my $target (@targets) {
@@ -299,21 +399,36 @@ sub process_subtree ($$) {
     return @targets;
 }
 
+sub process_final ($) {
+    my ($otargets) = @_;
+    set_dir_vars([]);
+    push @output_makefiles, "Final.mk";
+    start_output_file("Final.mk.tmp");
+    my %ntargets;
+    my $esclit='&';
+    process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
+    delete $ntargets{$_} foreach @$otargets;
+    my @ntargets = sort keys %ntargets;
+    die "subdirmk: Final.sd.mk may not introduce new top-level targets".
+       " (@ntargets)\n" if @ntargets;
+}
+
 sub process_tree() {
-    process_subtree($root, [ ]);
+    my @targets = process_subtree($root, [ ]);
+    process_final(\@targets);
     start_output_file("main.mk.tmp");
     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
-       o "$v=\@$v@\n";
+       oraw "$v=\@$v@\n";
     }
-    o "SUBDIRMK_MAKEFILES :=\n";
-    o "MAKEFILE_TEMPLATES :=\n";
+    oraw "SUBDIRMK_MAKEFILES :=\n";
+    oraw "MAKEFILE_TEMPLATES :=\n";
     foreach my $mf (@output_makefiles) {
-       o "SUBDIRMK_MAKEFILES += $mf\n";
+       oraw "SUBDIRMK_MAKEFILES += $mf\n";
     }
     foreach my $input (sort keys %input_files) {
-       o "MAKEFILE_TEMPLATES += $input\n";
+       oraw "MAKEFILE_TEMPLATES += $input\n";
     }
-    o "include \$(SUBDIRMK_MAKEFILES)\n";
+    oraw "include \$(SUBDIRMK_MAKEFILES)\n";
 }
 
 build_tree();