3 # subdirmk - &-filter (makefile generation program)
4 # Copyright 2019 Ian Jackson
5 # SPDX-License-Identifier: LGPL-2.0-or-later
6 # There is NO WARRANTY.
8 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
10 # generates in each subdirectory
19 print "$0 @ARGV\n" or die $!;
23 # error handling methods:
25 # Error in input file, while $err_file and $. set, eg in most of
29 # Other input or usage errors:
30 # die "subdirmk: $file:$lno: problem\n";
31 # die "subdirmk: some problem not locatable in that way\n";
34 # die "subdirmk $0: explanation of problem\n";
36 # System call error (not ENOENT) accessing input/output files:
37 # die "description of problem eg maybe erbing noun: $!\n";
39 # Bug detedcted in `generate':
40 # die "internal error (some information)?"; # or similar
42 while (@ARGV && $ARGV[0] =~ m/^-/) {
48 die "subdirmk $0: unknown option \`$_'\n";
53 s{/+$}{} foreach @subdirs;
55 our $root = [ '.', [ ], 1 ];
56 # each node is [ 'relative subdir name', \@children, $mentioned ]
59 foreach my $subdir (@subdirs) {
60 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
62 foreach my $d (@path) {
63 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
66 push @{ $node->[1] }, $c;
74 sub target_varname ($$) {
75 my ($var_prefix, $target) = @_;
76 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
80 our $buffering_output;
83 our @output_makefiles;
85 sub close_any_output_file() {
86 return unless defined $writing_output;
87 O->error and die "error writing $writing_output.tmp: $! (?)\n";
88 close O or die "error closing $writing_output.tmp: $!\n";
89 $writing_output = undef;
93 die 'internal error' unless defined $writing_output;
94 print O @_ or die "error writing $writing_output.tmp: $!\n";
98 if (defined $buffering_output) {
99 $buffering_output .= $_ foreach @_;
107 sub od { # maybe $-doubled
119 sub start_output_file ($) {
120 close_any_output_file();
121 ($writing_output) = @_;
122 die "internal error ($writing_output?)"
123 if $output_files{$writing_output}++;
124 my $tmp = "$writing_output.tmp";
125 open O, ">", $tmp or die "create $tmp: $!\n";
126 oraw "# autogenerated - do not edit\n";
129 sub install_output_files () {
130 close_any_output_file();
131 foreach my $f (sort keys %output_files) {
132 rename "$f.tmp", $f or die "install new $f: $!\n";
136 sub write_makefile ($$) {
137 my ($dir_prefix,$depth) = @_;
138 #print STDERR "write_makefile @_\n";
139 start_output_file("${dir_prefix}Makefile");
140 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
141 my $suppress_templates=
142 '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
143 ' MAKEFILE_TEMPLATES=)';
146 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
148 subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
149 Makefile run-main.mk:
150 \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
159 our ($dir_prefix, $dir_suffix, $dir_name,
160 $var_prefix, $var_prefix_name);
164 join '', map { "$_/" } @$path;
167 sub set_dir_vars ($) {
169 $dir_prefix = dir_prefix($path);
170 $dir_suffix = join '', map { "/$_" } @$path;
171 $dir_name = join '/', @$path ? @$path : '.';
172 $var_prefix_name = join '_', @$path ? @$path : qw(TOP);
173 $var_prefix = "${var_prefix_name}_";
178 our @warn_ena_dfl = map { $_ => 1 } qw(
184 our %warn_ena = @warn_ena_dfl;
191 die defined $err_file
192 ? "subdirmk: ${err_file}:$.: $m\n"
198 return 0 unless $warn_ena{$wk} // warn "internal error $wk ?";
200 print STDERR "subdirmk: warning ($wk): $m\n";
207 return 0 if $warn_dedupe{$err_file,$.,$wk,$m}++;
208 wrncore($wk, "${err_file}:$.: $m");
214 err "escape &$e is valid only during \$-doubling";
217 sub process_input_mk ($$$$);
218 sub process_input_mk ($$$$) {
219 my ($targets, $f, $esclitr, $enoent_ok) = @_;
221 my $caps_re = qr{[A-Z]};
222 my $lc_re = qr{[a-z]};
231 my $input = new IO::File $f, '<';
233 err "open $f: $!" unless $!==ENOENT && $enoent_ok;
241 '^' => "\${top_srcdir}${dir_suffix}",
242 '~' => "\${top_srcdir}",
247 $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
251 my $evalcall_brackets;
253 my $push_nest = sub {
254 my ($nk, $nndbl, $what) = @_;
255 unshift @nest, [ $nk, $ddbl, $what, $. ];
260 err "unexpectedly closed $nk in middle of $nest[0][0] ($nest[0][2])"
261 unless $nest[0][0] eq $nk;
262 $ddbl = (shift @nest)[1];
265 # Our detection of variable settings does not have to be completely
266 # accurate, since it is only going to be used for advice to the user.
267 my $note_varref = sub {
269 my $exp = !!$varref_exp{$vn}{$amp};
270 $varref{$vn}{$exp}{$amp}{"$f:$."} = 1;
274 if (m#^\s*($esc)?(\w+)\s*(?:=|\+=|\?=|:=)# ||
275 m#^\s*(?:$esc\:macro|define)\s+($esc)?(\S+)\s#) {
276 $note_varref->($2,!!$1);
278 if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
282 } elsif (s#^\s*$esc\:endm\s+$##) {
283 $pop_nest->('macro');
286 } elsif (s#^\s*$esc\:warn\s+(\S.*)$##) {
287 foreach my $wk (split /\s+/, $1) {
288 my $yes = $wk !~ s{^!}{};
289 if (defined $warn_ena{$wk}) {
290 $warn_ena{$wk} = $yes;
293 wrn 'unknown-warning',
294 "unknown warning $wk requested";
296 $warn_unk{$wk} //= "$f:$.";
300 } elsif (s#^\s*$esc\:local\+global\s+(\S.*)$##) {
301 foreach my $vn (split /\s+/, $1) {
302 my $pos = !($vn =~ s{^!}{});
303 my $amp = $vn =~ s{^$esc}{};
304 $varref_exp{$vn}{!!$amp} = $pos;
307 } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) {
308 $buffering_output='';
309 } elsif (m#^\s*$esc\:([a-z][-+0-9a-z_]*)#) {
310 err "unknown directive &:$1 or bad argumnt syntax";
311 } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
313 my $vn = target_varname($var_prefix, $t);
314 $note_varref->($vn,1);
316 $targets->{$t} //= [ ];
319 err 'cannot $-double &-processed RHS of directive'
320 if $ddbl && defined $buffering_output;
321 unless ($nest[0][0] eq 'eval'
322 ? s{^(.*?)($esc|\$|[{}])}{}
323 : s{^(.*?)($esc|\$)}{}) { od $_; last; }
327 $evalcall_brackets++;
329 } elsif ($2 eq '}') {
331 next if --$evalcall_brackets;
335 } elsif ($2 eq '$') {
337 if (s{^\$}{}) { od $&; }
338 elsif (m{^[a-zA-Z]\w}) {
339 wrn 'single-char-var',
340 'possibly confusing unbracketed single-char $-expansion';
343 wrn 'broken-var-ref',
344 'broken $&... expansion; you probably meant &$';
346 elsif (m{^\(($esc)?([^()\$]+)\)} ||
347 m{^\{($esc)?([^{}\$]+)\}}) {
348 $note_varref->($2,!!$1);
352 if (s{^\\$esc}{}) { od "$$esclitr" }
353 elsif (s{^\\\$}{}) { oud '$' }
354 elsif (s{^\\\s+$}{}) { }
355 elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" }
356 elsif (m{^(?=$caps_re)}) { od $var_prefix }
357 elsif (s{^\$([A-Za-z]\w+)}{}) {
358 $note_varref->($1,1);
359 od "\${${var_prefix}$1}";
361 elsif (s{^([~^]?)(?=$lc_re)}{}) { od $pfxmap{$1} }
362 elsif (s{^_}{}) { od $var_prefix }
363 elsif (s{^=}{}) { od $var_prefix_name }
364 elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} }
365 elsif (s{^\.}{}) { od $dir_name }
366 elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} }
367 elsif (s{^\$\-}{}) { $ddbl=undef; }
368 elsif (s{^\$\+}{}) { $ddbl=1; }
370 ddbl_only($&); oud "\${";
371 $note_varref->($2,!!$1) if m{^($esc)?([^()\$]+\))};
373 elsif (s{^\$(\d+)}{}) { ddbl_only($&); oud "\${$1}"; }
375 err 'macro invocation cannot be re-$-doubled' if $ddbl;
377 $evalcall_brackets = 1;
378 $push_nest->('eval',1, '&${...}');
379 $note_varref->($2,!!$1) if m{^\s*($esc)?([^,{}\$]+)};
380 } elsif (s{^([~^]?)(?=[ \t])}{}) {
381 my $prefix = $pfxmap{$1} // die "internal error ($1?)";
383 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
384 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
389 } elsif (s{^![ \t]+}{}) {
394 err "bad &-escape \`$$esclitr$&'";
397 if (defined $buffering_output) {
398 $_=$buffering_output;
399 $buffering_output=undef;
400 if (m#^(-?)include\s+(\S+)\s+$#) {
401 my $subf = "$srcdir/$2";
402 process_input_mk($targets, $subf, $esclitr, $1);
404 } elsif (m#^macro\s+(\S+)\s+$#) {
406 $push_nest->('macro', 1, '&:macro');
408 err "bad directive argument syntax";
412 die "subdirmk: $f:$nest[0][3]: unclosed $nest[0][0] ($nest[0][2])\n"
414 $input->error and die "read $f: $!\n";
415 close $input or die "close $f: $!\n";
418 sub filter_subdir_mk ($) {
422 #print STDERR "filter @_\n";
427 my ($f, $enoentok) = @_;
428 process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
430 $pi->("Prefix.sd.mk", 1);
431 $pi->("${dir_prefix}Dir.sd.mk", 0);
432 $pi->("Suffix.sd.mk", 1);
435 sub process_subtree ($$);
436 sub process_subtree ($$) {
437 # => list of targets (in form SUBDIR/)
438 # recursive, children first
439 my ($node, $path) = @_;
442 #print STDERR Dumper(\@_);
446 my $dir_prefix = dir_prefix($path);
447 # ^ this is the only var which we need before we come back from
450 push @output_makefiles, "${dir_prefix}Dir.mk";
451 write_makefile($dir_prefix, scalar @$path);
453 my %targets = (all => []);
454 foreach my $child (@{ $node->[1] }) {
455 my @childpath = (@$path, $child->[0]);
456 my $child_subdir = join '/', @childpath;
457 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!\n";
458 local %warn_ena = @warn_ena_dfl;
459 push @{ $targets{$_} }, $child_subdir foreach
460 process_subtree($child, \@childpath);
464 start_output_file("${dir_prefix}Dir.mk.tmp");
467 filter_subdir_mk(\%targets);
469 my $sdmk = "${dir_prefix}Dir.sd.mk";
472 "subdirmk: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)";
473 } elsif ($!==ENOENT) {
475 die "stat $sdmk: $!\n";
481 my @targets = sort keys %targets;
482 foreach my $target (@targets) {
483 my $target_varname = target_varname($var_prefix, $target);
484 oraw "${dir_prefix}${target}:: \$($target_varname)";
485 foreach my $child_subdir (@{ $targets{$target} }) {
486 oraw " $child_subdir/$target";
492 oraw " ${dir_prefix}${_}" foreach @targets;
499 sub process_final ($) {
502 push @output_makefiles, "Final.mk";
503 start_output_file("Final.mk.tmp");
506 process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
507 delete $ntargets{$_} foreach @$otargets;
508 my @ntargets = sort keys %ntargets;
509 die "subdirmk: Final.sd.mk may not introduce new top-level targets".
510 " (@ntargets)\n" if @ntargets;
514 my @targets = process_subtree($root, [ ]);
515 process_final(\@targets);
516 start_output_file("main.mk.tmp");
517 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
520 oraw "SUBDIRMK_MAKEFILES :=\n";
521 oraw "MAKEFILE_TEMPLATES :=\n";
522 foreach my $mf (@output_makefiles) {
523 oraw "SUBDIRMK_MAKEFILES += $mf\n";
525 foreach my $input (sort keys %input_files) {
526 oraw "MAKEFILE_TEMPLATES += $input\n";
528 oraw "include \$(SUBDIRMK_MAKEFILES)\n";
531 sub flmap ($) { local ($_) = @_; s{:(\d+)$}{ sprintf ":%10d", $1 }e; $_; }
533 sub print_varref_warnings () {
534 foreach my $vn (sort keys %varref) {
535 my $vv = $varref{$vn};
536 next unless $vv->{''}{''} && $vv->{''}{1};
537 wrncore 'local+global', "saw both $vn and &$vn" or return;
538 foreach my $exp ('', 1) {
539 foreach my $amp ('', 1) {
542 ? " expectedly saw %s%s at %s\n"
543 : " saw %s%s at %s\n"),
544 ($amp ? '&' : ''), $vn, $_
546 sort { flmap($a) cmp flmap($b) }
547 keys %{ $vv->{$exp}{$amp} };
553 sub print_warning_warnings () {
554 return unless $warned;
555 foreach my $wk (sort keys %warn_unk) {
556 wrncore 'unknown-warning',
557 "$warn_unk{$wk}: attempt to suppress unknown warning(s) \`$wk'";
563 print_varref_warnings();
564 print_warning_warnings();
565 install_output_files();