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 while (@ARGV && $ARGV[0] =~ m/^-/) {
28 die "$0: unknown option \`$_'\n";
33 s{/+$}{} foreach @subdirs;
35 our $root = [ '.', [ ], 1 ];
36 # each node is [ 'relative subdir name', \@children, $mentioned ]
39 foreach my $subdir (@subdirs) {
40 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
42 foreach my $d (@path) {
43 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
46 push @{ $node->[1] }, $c;
54 sub target_varname ($$) {
55 my ($var_prefix, $target) = @_;
56 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
60 our $buffering_output;
63 our @output_makefiles;
65 sub close_any_output_file() {
66 return unless defined $writing_output;
67 O->error and die "error writing $writing_output.tmp: $! (?)\n";
68 close O or die "error closing $writing_output.tmp: $!\n";
69 $writing_output = undef;
73 die unless defined $writing_output;
74 print O @_ or die "error writing $writing_output.tmp: $!\n";
78 if (defined $buffering_output) {
79 $buffering_output .= $_ foreach @_;
87 sub od { # maybe $-doubled
99 sub start_output_file ($) {
100 close_any_output_file();
101 ($writing_output) = @_;
102 die "$writing_output ?" if $output_files{$writing_output}++;
103 my $tmp = "$writing_output.tmp";
104 open O, ">", $tmp or die "create $tmp: $!\n";
105 oraw "# autogenerated - do not edit\n";
108 sub install_output_files () {
109 close_any_output_file();
110 foreach my $f (sort keys %output_files) {
111 rename "$f.tmp", $f or die "install new $f: $!\n";
115 sub write_makefile ($$) {
116 my ($dir_prefix,$depth) = @_;
117 #print STDERR "write_makefile @_\n";
118 start_output_file("${dir_prefix}Makefile");
119 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
120 my $suppress_templates=
121 '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
122 ' MAKEFILE_TEMPLATES=)';
125 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
127 subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
128 Makefile run-main.mk:
129 \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
135 our ($dir_prefix, $dir_suffix, $dir_name,
136 $var_prefix, $var_prefix_name);
140 join '', map { "$_/" } @$path;
143 sub set_dir_vars ($) {
145 $dir_prefix = dir_prefix($path);
146 $dir_suffix = join '', map { "/$_" } @$path;
147 $dir_name = join '/', @$path ? @$path : '.';
148 $var_prefix_name = join '_', @$path ? @$path : qw(TOP);
149 $var_prefix = "${var_prefix_name}_";
152 sub process_input_mk ($$$$);
153 sub process_input_mk ($$$$) {
154 my ($targets, $f, $esclitr, $enoent_ok) = @_;
156 my $caps_re = qr{[A-Z]};
157 my $lc_re = qr{[a-z]};
166 my $input = new IO::File $f, '<';
168 die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
174 '^' => "\$(top_srcdir)${dir_suffix}",
175 '~' => "\$(top_srcdir)",
180 $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
185 my $push_nest = sub {
186 my ($nk, $nndbl) = @_;
187 unshift @nest, [ $nk, $ddbl ];
192 die unless $nest[0][0] eq $nk;
193 $ddbl = (shift @nest)[1];
197 if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
201 } elsif (s#^\s*$esc\:endm\s+$##) {
202 $pop_nest->('Macro');
205 } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) {
206 $buffering_output='';
207 } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
208 die "unknown directive $1";
209 } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
211 od target_varname($var_prefix, $t);
212 $targets->{$t} //= [ ];
215 die if $ddbl && defined $buffering_output;
216 unless (s{^(.*?)$esc}{}) { od $_; last; }
218 if (s{^\\$esc}{}) { od "$$esclitr" }
219 elsif (s{^\\\$}{}) { oud '$' }
220 elsif (s{^\\\s+$}{}) { }
221 elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" }
222 elsif (m{^(?=$caps_re)}) { od $var_prefix }
223 elsif (s{^\$([A-Za-z]\w+)}{}) { od "\$(${var_prefix}$1)" }
224 elsif (s{^([~^]?)(?=$lc_re)}{}) { od $pfxmap{$1} }
225 elsif (s{^_}{}) { od $var_prefix }
226 elsif (s{^=}{}) { od $var_prefix_name }
227 elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} }
228 elsif (s{^\.}{}) { od $dir_name }
229 elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} }
230 elsif (s{^\$\-}{}) { $ddbl=undef; }
231 elsif (s{^\$\+}{}) { $ddbl=1; }
232 elsif (s{^\$\(}{}) { die unless $ddbl; oud "\$("; }
233 elsif (s{^\$(\d+)}{}) { die unless $ddbl; oud "\$($1)"; }
234 elsif (s{^([~^]?)(?=[ \t])}{}) {
235 my $prefix = $pfxmap{$1} // die;
237 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
238 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
243 } elsif (s{^![ \t]+}{}) {
247 die "bad escape $$esclitr$_ ";
250 if (defined $buffering_output) {
251 $_=$buffering_output;
252 $buffering_output=undef;
253 if (m#^(-?)include\s+(\S+)\s+$#) {
254 my $subf = "$srcdir/$2";
255 process_input_mk($targets, $subf, $esclitr, $1);
257 } elsif (m#^macro\s+(\S+)\s+$#) {
259 $push_nest->('Macro', 1);
261 die "internal error buffering directive $_ ";
265 $input->error and die "read $f: $!\n";
266 close $input or die "close $f: $!\n";
269 sub filter_subdir_mk ($) {
273 #print STDERR "filter @_\n";
278 my ($f, $enoentok) = @_;
279 process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
281 $pi->("Prefix.sd.mk", 1);
282 $pi->("${dir_prefix}Subdir.sd.mk", 0);
283 $pi->("Suffix.sd.mk", 1);
286 sub process_subtree ($$);
287 sub process_subtree ($$) {
288 # => list of targets (in form SUBDIR/)
289 # recursive, children first
290 my ($node, $path) = @_;
293 #print STDERR Dumper(\@_);
295 my $dir_prefix = dir_prefix($path);
296 # ^ this is the only var which we need before we come back from
299 push @output_makefiles, "${dir_prefix}Subdir.mk";
300 write_makefile($dir_prefix, scalar @$path);
302 my %targets = (all => []);
303 foreach my $child (@{ $node->[1] }) {
304 my @childpath = (@$path, $child->[0]);
305 my $child_subdir = join '/', @childpath;
306 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
307 push @{ $targets{$_} }, $child_subdir foreach
308 process_subtree($child, \@childpath);
312 start_output_file("${dir_prefix}Subdir.mk.tmp");
315 filter_subdir_mk(\%targets);
317 my $sdmk = "${dir_prefix}Subdir.sd.mk";
319 die "$sdmk unexpectedly exists (${dir_prefix} not mentioned)";
320 } elsif ($!==ENOENT) {
322 die "stat $sdmk: $!";
328 my @targets = sort keys %targets;
329 foreach my $target (@targets) {
330 my $target_varname = target_varname($var_prefix, $target);
331 print O "${dir_prefix}${target}:: \$($target_varname)";
332 foreach my $child_subdir (@{ $targets{$target} }) {
333 print O " $child_subdir/$target";
339 print O " ${dir_prefix}${_}" foreach @targets;
346 sub process_final ($) {
349 push @output_makefiles, "Final.mk";
350 start_output_file("Final.mk.tmp");
353 process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
354 delete $ntargets{$_} foreach @$otargets;
355 my @ntargets = sort keys %ntargets;
356 die "late new targets @ntargets" if @ntargets;
360 my @targets = process_subtree($root, [ ]);
361 process_final(\@targets);
362 start_output_file("main.mk.tmp");
363 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
366 oraw "SUBDIRMK_MAKEFILES :=\n";
367 oraw "MAKEFILE_TEMPLATES :=\n";
368 foreach my $mf (@output_makefiles) {
369 oraw "SUBDIRMK_MAKEFILES += $mf\n";
371 foreach my $input (sort keys %input_files) {
372 oraw "MAKEFILE_TEMPLATES += $input\n";
374 oraw "include \$(SUBDIRMK_MAKEFILES)\n";
379 install_output_files();