X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?p=subdirmk.git;a=blobdiff_plain;f=generate;h=03aabb056787e4e98a1d6d6f6167ec32b24620ec;hp=49038dfbe0ca5a57ca69221eb9f38f2f5b1fc6ef;hb=8534ffc15c44bfd2f7a72ee745afd284255d17c7;hpb=8ff6b1c417910f9a7f69ae98d120f2c798a0c18f diff --git a/generate b/generate index 49038df..03aabb0 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 @@ -152,6 +152,8 @@ Makefile run-main.mk: END } +our %varref; + our ($dir_prefix, $dir_suffix, $dir_name, $var_prefix, $var_prefix_name); @@ -176,6 +178,11 @@ sub err ($) { die "subdirmk: ${err_file}:$.: $m\n"; } +sub wrn ($) { + my ($m) = @_; + print STDERR "subdirmk: warning: ${err_file}:$.: $m\n"; +} + sub ddbl_only ($) { my ($e) = @_; return if $ddbl; @@ -216,32 +223,40 @@ sub process_input_mk ($$$$) { local $ddbl; 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; - # ^ 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]; }; + # Our detection of variable settings does not have to be completely + # accurate, since it is only going to be used for advice to the user. + my $note_varref = sub { + my ($vn,$amp) = @_; + $varref{$vn}{$amp}{"$f:$."} = 1; + }; + while (<$input>) { if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) { $$esclitr = $1; $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); @@ -250,17 +265,19 @@ 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; 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" } @@ -282,7 +299,8 @@ sub process_input_mk ($$$$) { elsif (s{^\$\{}{}) { 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 "internal error ($1?)"; my $after=''; @@ -296,7 +314,8 @@ sub process_input_mk ($$$$) { od $_; $_ = ''; } else { - err "bad &-escape \`$$esclitr$_'"; + m{^.{0,5}}; + err "bad &-escape \`$$esclitr$&'"; } } if (defined $buffering_output) { @@ -308,13 +327,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"; } @@ -331,9 +351,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 ($$); @@ -349,7 +369,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 => []); @@ -362,12 +382,12 @@ 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 "subdirmk: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)"; @@ -382,16 +402,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; @@ -429,6 +449,20 @@ sub process_tree() { oraw "include \$(SUBDIRMK_MAKEFILES)\n"; } +sub print_varref_warnings () { + foreach my $vn (sort keys %varref) { + my $vv = $varref{$vn}; + next unless $vv->{''} && $vv->{1}; + print STDERR "subdirmk: warning: saw both $vn and &$vn\n"; + foreach my $amp ('', 1) { + printf STDERR " saw %s%s at %s\n", + ($amp ? '&' : ''), $vn, $_ + foreach sort keys %{ $vv->{$amp} }; + } + } +} + build_tree(); process_tree(); +print_varref_warnings(); install_output_files();