chiark / gitweb /
Big incompatible change: Rename `Subdir' to `Dir'
[subdirmk.git] / generate
index 2a65d991ceb466d193002138abfab1f573d0f4b2..a6a6cda90c6b1e032dabf49797086c0280789a41 100755 (executable)
--- a/generate
+++ b/generate
@@ -7,7 +7,7 @@
 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
 #
 # generates in each subdirectory
-#     Subdir.mk.tmp
+#     Dir.mk.tmp
 #     Makefile
 # and in toplevel
 #     main.mk.tmp
@@ -26,8 +26,11 @@ our $srcdir='.';
 #         err "message";
 #
 # Other input or usage errors:
-#         die "$0: $file:$lno: problem\n";
-#         die "$0: some problem not locatable in that way\n";
+#         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";
@@ -41,7 +44,7 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
     if (s/^--srcdir=//) {
        $srcdir=$';
     } else {
-       die "$0: unknown option \`$_'\n";
+       die "subdirmk $0: unknown option \`$_'\n";
     }
 }
 our @subdirs = @ARGV;
@@ -170,7 +173,7 @@ our $err_file;
 
 sub err ($) {
     my ($m) = @_;
-    die "$0: ${err_file}:$.: $m\n";
+    die "subdirmk: ${err_file}:$.: $m\n";
 }
 
 sub ddbl_only ($) {
@@ -215,14 +218,14 @@ sub process_input_mk ($$$$) {
     my @nest = (['']);
 
     my $push_nest = sub {
-       my ($nk, $nndbl) = @_;
-       unshift @nest, [ $nk, $ddbl ];
+       my ($nk, $nndbl, $what) = @_;
+       unshift @nest, [ $nk, $ddbl, $what, $. ];
        $ddbl = $nndbl;
     };
     my $pop_nest = sub {
        my ($nk) = @_;
-       die unless $nest[0][0] eq $nk;
-       # ^ xxx need better message
+       err "unexpectedly closed $nk in middle of $nest[0][0] ($nest[0][2])"
+           unless $nest[0][0] eq $nk;
        $ddbl = (shift @nest)[1];
     };
 
@@ -232,13 +235,13 @@ sub process_input_mk ($$$$) {
            $set_esc->();
            next;
        } elsif (s#^\s*$esc\:endm\s+$##) {
-           $pop_nest->('Macro');
+           $pop_nest->('macro');
            od "endef\n";
            next;
        } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) {
            $buffering_output='';
        } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
-           err "unknown directive &:$1";
+           err "unknown directive &:$1 or bad argumnt syntax";
        } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
            my $t = $1 // 'all';
            od target_varname($var_prefix, $t);
@@ -247,7 +250,7 @@ sub process_input_mk ($$$$) {
        for (;;) {
            err 'cannot $-double &-processed RHS of directive'
                if $ddbl && defined $buffering_output;
-           unless ($nest[0][0] eq 'Eval'
+           unless ($nest[0][0] eq 'eval'
                    ? s{^(.*?)($esc|[{}])}{}
                    : s{^(.*?)($esc)}{}) { od $_; last; }
            od $1;
@@ -256,7 +259,7 @@ sub process_input_mk ($$$$) {
                next;
            } elsif ($2 eq '}') {
                next if --$ddbl;
-               $pop_nest->('Eval');
+               $pop_nest->('eval');
                od '}}';
                next;
            }
@@ -279,7 +282,7 @@ sub process_input_mk ($$$$) {
            elsif (s{^\$\{}{}) {
                err 'macro invocation cannot be re-$-doubled' if $ddbl;
                od '${eval ${call ';
-               $push_nest->('Eval',1);
+               $push_nest->('eval',1, '&${...}');
            } elsif (s{^([~^]?)(?=[ \t])}{}) {
                my $prefix = $pfxmap{$1} // die "internal error ($1?)";
                my $after='';
@@ -293,7 +296,8 @@ sub process_input_mk ($$$$) {
                od $_;
                $_ = '';
            } else {
-               err "bad &-escape \`$$esclitr$_'";
+               m{^.{0,5}};
+               err "bad &-escape \`$$esclitr$&'";
            }
        }
        if (defined $buffering_output) {
@@ -305,13 +309,14 @@ sub process_input_mk ($$$$) {
                od "\n";
            } elsif (m#^macro\s+(\S+)\s+$#) {
                od "define $1\n";
-               $push_nest->('Macro', 1);
+               $push_nest->('macro', 1, '&:macro');
            } else {
-               die "internal error ($_?)";
+               err "bad directive argument syntax";
            }
        }
     }
-    die "unclosed $nest[0][0]" if $nest[0][0];
+    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";
 }
@@ -328,9 +333,9 @@ sub filter_subdir_mk ($) {
        my ($f, $enoentok) = @_;
        process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
     };
-    $pi->("Prefix.sd.mk",              1);
-    $pi->("${dir_prefix}Subdir.sd.mk", 0);
-    $pi->("Suffix.sd.mk",              1);
+    $pi->("Prefix.sd.mk",           1);
+    $pi->("${dir_prefix}Dir.sd.mk", 0);
+    $pi->("Suffix.sd.mk",           1);
 }
 
 sub process_subtree ($$);
@@ -346,7 +351,7 @@ sub process_subtree ($$) {
     # ^ this is the only var which we need before we come back from
     #   the recursion.
 
-    push @output_makefiles, "${dir_prefix}Subdir.mk";
+    push @output_makefiles, "${dir_prefix}Dir.mk";
     write_makefile($dir_prefix, scalar @$path);
 
     my %targets = (all => []);
@@ -359,15 +364,15 @@ sub process_subtree ($$) {
     }
 
     set_dir_vars($path);
-    start_output_file("${dir_prefix}Subdir.mk.tmp");
+    start_output_file("${dir_prefix}Dir.mk.tmp");
 
     if ($node->[2]) {
        filter_subdir_mk(\%targets);
     } else {
-       my $sdmk = "${dir_prefix}Subdir.sd.mk";
+       my $sdmk = "${dir_prefix}Dir.sd.mk";
        if (stat $sdmk) {
            die
- "$0: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)";
+ "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: $!\n";
@@ -404,7 +409,7 @@ sub process_final ($) {
     process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
     delete $ntargets{$_} foreach @$otargets;
     my @ntargets = sort keys %ntargets;
-    die "$0: Final.sd.mk may not introduce new top-level targets".
+    die "subdirmk: Final.sd.mk may not introduce new top-level targets".
        " (@ntargets)\n" if @ntargets;
 }