3 # subdirmk - &-filter (makefile generation program)
4 # Copyright 2019 Ian Jackson
5 # SPDX-License-Identifier: LGPL-2.0-or-later
7 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
9 # generates in each subdirectory
18 print "$0 @ARGV\n" or die $!;
22 # error handling methods:
24 # Error in input file, while $err_file and $. set, eg in most of
28 # Other input or usage errors:
29 # die "subdirmk: $file:$lno: problem\n";
30 # die "subdirmk: some problem not locatable in that way\n";
33 # die "subdirmk $0: explanation of problem\n";
35 # System call error (not ENOENT) accessing input/output files:
36 # die "description of problem eg maybe erbing noun: $!\n";
38 # Bug detedcted in `generate':
39 # die "internal error (some information)?"; # or similar
41 while (@ARGV && $ARGV[0] =~ m/^-/) {
47 die "subdirmk $0: unknown option \`$_'\n";
52 s{/+$}{} foreach @subdirs;
54 our $root = [ '.', [ ], 1 ];
55 # each node is [ 'relative subdir name', \@children, $mentioned ]
58 foreach my $subdir (@subdirs) {
59 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
61 foreach my $d (@path) {
62 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
65 push @{ $node->[1] }, $c;
73 sub target_varname ($$) {
74 my ($var_prefix, $target) = @_;
75 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
79 our $buffering_output;
82 our @output_makefiles;
84 sub close_any_output_file() {
85 return unless defined $writing_output;
86 O->error and die "error writing $writing_output.tmp: $! (?)\n";
87 close O or die "error closing $writing_output.tmp: $!\n";
88 $writing_output = undef;
92 die 'internal error' unless defined $writing_output;
93 print O @_ or die "error writing $writing_output.tmp: $!\n";
97 if (defined $buffering_output) {
98 $buffering_output .= $_ foreach @_;
106 sub od { # maybe $-doubled
118 sub start_output_file ($) {
119 close_any_output_file();
120 ($writing_output) = @_;
121 die "internal error ($writing_output?)"
122 if $output_files{$writing_output}++;
123 my $tmp = "$writing_output.tmp";
124 open O, ">", $tmp or die "create $tmp: $!\n";
125 oraw "# autogenerated - do not edit\n";
128 sub install_output_files () {
129 close_any_output_file();
130 foreach my $f (sort keys %output_files) {
131 rename "$f.tmp", $f or die "install new $f: $!\n";
135 sub write_makefile ($$) {
136 my ($dir_prefix,$depth) = @_;
137 #print STDERR "write_makefile @_\n";
138 start_output_file("${dir_prefix}Makefile");
139 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
140 my $suppress_templates=
141 '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
142 ' MAKEFILE_TEMPLATES=)';
145 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
147 subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
148 Makefile run-main.mk:
149 \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
155 our ($dir_prefix, $dir_suffix, $dir_name,
156 $var_prefix, $var_prefix_name);
160 join '', map { "$_/" } @$path;
163 sub set_dir_vars ($) {
165 $dir_prefix = dir_prefix($path);
166 $dir_suffix = join '', map { "/$_" } @$path;
167 $dir_name = join '/', @$path ? @$path : '.';
168 $var_prefix_name = join '_', @$path ? @$path : qw(TOP);
169 $var_prefix = "${var_prefix_name}_";
176 die "subdirmk: ${err_file}:$.: $m\n";
182 err "escape &$e is valid only during \$-doubling";
185 sub process_input_mk ($$$$);
186 sub process_input_mk ($$$$) {
187 my ($targets, $f, $esclitr, $enoent_ok) = @_;
189 my $caps_re = qr{[A-Z]};
190 my $lc_re = qr{[a-z]};
199 my $input = new IO::File $f, '<';
201 err "open $f: $!" unless $!==ENOENT && $enoent_ok;
209 '^' => "\$(top_srcdir)${dir_suffix}",
210 '~' => "\$(top_srcdir)",
215 $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
220 my $push_nest = sub {
221 my ($nk, $nndbl, $what) = @_;
222 unshift @nest, [ $nk, $ddbl, $what, $. ];
227 err "unexpectedly closed $nk in middle of $nest[0][0] ($nest[0][2])"
228 unless $nest[0][0] eq $nk;
229 $ddbl = (shift @nest)[1];
233 if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
237 } elsif (s#^\s*$esc\:endm\s+$##) {
238 $pop_nest->('macro');
241 } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) {
242 $buffering_output='';
243 } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
244 err "unknown directive &:$1 or bad argumnt syntax";
245 } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
247 od target_varname($var_prefix, $t);
248 $targets->{$t} //= [ ];
251 err 'cannot $-double &-processed RHS of directive'
252 if $ddbl && defined $buffering_output;
253 unless ($nest[0][0] eq 'eval'
254 ? s{^(.*?)($esc|[{}])}{}
255 : s{^(.*?)($esc)}{}) { od $_; last; }
260 } elsif ($2 eq '}') {
266 if (s{^\\$esc}{}) { od "$$esclitr" }
267 elsif (s{^\\\$}{}) { oud '$' }
268 elsif (s{^\\\s+$}{}) { }
269 elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" }
270 elsif (m{^(?=$caps_re)}) { od $var_prefix }
271 elsif (s{^\$([A-Za-z]\w+)}{}) { od "\$(${var_prefix}$1)" }
272 elsif (s{^([~^]?)(?=$lc_re)}{}) { od $pfxmap{$1} }
273 elsif (s{^_}{}) { od $var_prefix }
274 elsif (s{^=}{}) { od $var_prefix_name }
275 elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} }
276 elsif (s{^\.}{}) { od $dir_name }
277 elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} }
278 elsif (s{^\$\-}{}) { $ddbl=undef; }
279 elsif (s{^\$\+}{}) { $ddbl=1; }
280 elsif (s{^\$\(}{}) { ddbl_only($&); oud "\$("; }
281 elsif (s{^\$(\d+)}{}) { ddbl_only($&); oud "\$($1)"; }
283 err 'macro invocation cannot be re-$-doubled' if $ddbl;
285 $push_nest->('eval',1, '&${...}');
286 } elsif (s{^([~^]?)(?=[ \t])}{}) {
287 my $prefix = $pfxmap{$1} // die "internal error ($1?)";
289 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
290 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
295 } elsif (s{^![ \t]+}{}) {
300 err "bad &-escape \`$$esclitr$&'";
303 if (defined $buffering_output) {
304 $_=$buffering_output;
305 $buffering_output=undef;
306 if (m#^(-?)include\s+(\S+)\s+$#) {
307 my $subf = "$srcdir/$2";
308 process_input_mk($targets, $subf, $esclitr, $1);
310 } elsif (m#^macro\s+(\S+)\s+$#) {
312 $push_nest->('macro', 1, '&:macro');
314 err "bad directive argument syntax";
318 die "subdirmk: $f:$nest[0][3]: unclosed $nest[0][0] ($nest[0][2])\n"
320 $input->error and die "read $f: $!\n";
321 close $input or die "close $f: $!\n";
324 sub filter_subdir_mk ($) {
328 #print STDERR "filter @_\n";
333 my ($f, $enoentok) = @_;
334 process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
336 $pi->("Prefix.sd.mk", 1);
337 $pi->("${dir_prefix}Dir.sd.mk", 0);
338 $pi->("Suffix.sd.mk", 1);
341 sub process_subtree ($$);
342 sub process_subtree ($$) {
343 # => list of targets (in form SUBDIR/)
344 # recursive, children first
345 my ($node, $path) = @_;
348 #print STDERR Dumper(\@_);
350 my $dir_prefix = dir_prefix($path);
351 # ^ this is the only var which we need before we come back from
354 push @output_makefiles, "${dir_prefix}Dir.mk";
355 write_makefile($dir_prefix, scalar @$path);
357 my %targets = (all => []);
358 foreach my $child (@{ $node->[1] }) {
359 my @childpath = (@$path, $child->[0]);
360 my $child_subdir = join '/', @childpath;
361 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!\n";
362 push @{ $targets{$_} }, $child_subdir foreach
363 process_subtree($child, \@childpath);
367 start_output_file("${dir_prefix}Dir.mk.tmp");
370 filter_subdir_mk(\%targets);
372 my $sdmk = "${dir_prefix}Dir.sd.mk";
375 "subdirmk: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)";
376 } elsif ($!==ENOENT) {
378 die "stat $sdmk: $!\n";
384 my @targets = sort keys %targets;
385 foreach my $target (@targets) {
386 my $target_varname = target_varname($var_prefix, $target);
387 oraw "${dir_prefix}${target}:: \$($target_varname)";
388 foreach my $child_subdir (@{ $targets{$target} }) {
389 oraw " $child_subdir/$target";
395 oraw " ${dir_prefix}${_}" foreach @targets;
402 sub process_final ($) {
405 push @output_makefiles, "Final.mk";
406 start_output_file("Final.mk.tmp");
409 process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
410 delete $ntargets{$_} foreach @$otargets;
411 my @ntargets = sort keys %ntargets;
412 die "subdirmk: Final.sd.mk may not introduce new top-level targets".
413 " (@ntargets)\n" if @ntargets;
417 my @targets = process_subtree($root, [ ]);
418 process_final(\@targets);
419 start_output_file("main.mk.tmp");
420 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
423 oraw "SUBDIRMK_MAKEFILES :=\n";
424 oraw "MAKEFILE_TEMPLATES :=\n";
425 foreach my $mf (@output_makefiles) {
426 oraw "SUBDIRMK_MAKEFILES += $mf\n";
428 foreach my $input (sort keys %input_files) {
429 oraw "MAKEFILE_TEMPLATES += $input\n";
431 oraw "include \$(SUBDIRMK_MAKEFILES)\n";
436 install_output_files();