X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?p=subdirmk.git;a=blobdiff_plain;f=generate;h=f5c416db226dd22b1c2b7e434c09f3516fc1b2b2;hp=52a0ec8262522076de57b00cbe6013dd04a8d59d;hb=e36c3e76d81cd2dc6d157234bc60b6c9bd156e0d;hpb=8c3e6dc4a16406496aa6991f8dc2fb77a96359ea diff --git a/generate b/generate index 52a0ec8..f5c416d 100755 --- 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 @@ -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; @@ -70,7 +89,7 @@ sub close_any_output_file() { } sub oraw { - die unless defined $writing_output; + die 'internal error' unless defined $writing_output; print O @_ or die "error writing $writing_output.tmp: $!\n"; } @@ -99,7 +118,8 @@ sub od { # maybe $-doubled 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"; oraw "# autogenerated - do not edit\n"; @@ -149,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) = @_; @@ -165,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)", @@ -180,16 +215,18 @@ sub process_input_mk ($$$$) { $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap; local $ddbl; - my @nest; + my @nest = (['']); + my $evalcall_brackets; 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; + err "unexpectedly closed $nk in middle of $nest[0][0] ($nest[0][2])" + unless $nest[0][0] eq $nk; $ddbl = (shift @nest)[1]; }; @@ -199,31 +236,34 @@ 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_]*)#) { - die "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); $targets->{$t} //= [ ]; } for (;;) { - die if $ddbl && defined $buffering_output; - unless (@nest && $nest[0][0] eq 'Eval' + 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++; + od $2; + $evalcall_brackets++; next; } elsif ($2 eq '}') { - next if --$ddbl; - $pop_nest->('Eval'); - od '}}'; + od $2; + next if --$evalcall_brackets; + $pop_nest->('eval'); + od '}'; next; } if (s{^\\$esc}{}) { od "$$esclitr" } @@ -240,14 +280,15 @@ sub process_input_mk ($$$$) { elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} } elsif (s{^\$\-}{}) { $ddbl=undef; } elsif (s{^\$\+}{}) { $ddbl=1; } - elsif (s{^\$\(}{}) { die unless $ddbl; oud "\$("; } - elsif (s{^\$(\d+)}{}) { die unless $ddbl; oud "\$($1)"; } + elsif (s{^\$\(}{}) { ddbl_only($&); oud "\$("; } + elsif (s{^\$(\d+)}{}) { ddbl_only($&); oud "\$($1)"; } elsif (s{^\$\{}{}) { - die if $ddbl; + err 'macro invocation cannot be re-$-doubled' if $ddbl; od '${eval ${call '; - $push_nest->('Eval',1); + $evalcall_brackets = 1; + $push_nest->('eval',1, '&${...}'); } elsif (s{^([~^]?)(?=[ \t])}{}) { - my $prefix = $pfxmap{$1} // die; + my $prefix = $pfxmap{$1} // die "internal error ($1?)"; my $after=''; if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); } s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g; @@ -259,7 +300,8 @@ sub process_input_mk ($$$$) { od $_; $_ = ''; } else { - die "bad escape $$esclitr$_ "; + m{^.{0,5}}; + err "bad &-escape \`$$esclitr$&'"; } } if (defined $buffering_output) { @@ -271,12 +313,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 buffering directive $_ "; + err "bad directive argument syntax"; } } } + 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"; } @@ -293,9 +337,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 ($$); @@ -311,30 +355,31 @@ 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 => []); 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); } 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 "$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"; } } @@ -343,16 +388,16 @@ sub process_subtree ($$) { my @targets = sort keys %targets; foreach my $target (@targets) { my $target_varname = target_varname($var_prefix, $target); - print O "${dir_prefix}${target}:: \$($target_varname)"; + oraw "${dir_prefix}${target}:: \$($target_varname)"; foreach my $child_subdir (@{ $targets{$target} }) { - print O " $child_subdir/$target"; + oraw " $child_subdir/$target"; } - print O "\n"; + oraw "\n"; } if (@targets) { - print O ".PHONY:"; - print O " ${dir_prefix}${_}" foreach @targets; - print O "\n"; + oraw ".PHONY:"; + oraw " ${dir_prefix}${_}" foreach @targets; + oraw "\n"; } return @targets; @@ -368,7 +413,8 @@ sub process_final ($) { process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1); delete $ntargets{$_} foreach @$otargets; my @ntargets = sort keys %ntargets; - die "late new targets @ntargets" if @ntargets; + die "subdirmk: Final.sd.mk may not introduce new top-level targets". + " (@ntargets)\n" if @ntargets; } sub process_tree() {