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 "$0: $file:$lno: problem\n";
30 # die "$0: some problem not locatable in that way\n";
32 # System call error (not ENOENT) accessing input/output files:
33 # die "description of problem eg maybe erbing noun: $!\n";
35 # Bug detedcted in `generate':
36 # die "internal error (some information)?"; # or similar
38 while (@ARGV && $ARGV[0] =~ m/^-/) {
44 die "$0: unknown option \`$_'\n";
49 s{/+$}{} foreach @subdirs;
51 our $root = [ '.', [ ], 1 ];
52 # each node is [ 'relative subdir name', \@children, $mentioned ]
55 foreach my $subdir (@subdirs) {
56 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
58 foreach my $d (@path) {
59 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
62 push @{ $node->[1] }, $c;
70 sub target_varname ($$) {
71 my ($var_prefix, $target) = @_;
72 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
76 our $buffering_output;
79 our @output_makefiles;
81 sub close_any_output_file() {
82 return unless defined $writing_output;
83 O->error and die "error writing $writing_output.tmp: $! (?)\n";
84 close O or die "error closing $writing_output.tmp: $!\n";
85 $writing_output = undef;
89 die 'internal error' unless defined $writing_output;
90 print O @_ or die "error writing $writing_output.tmp: $!\n";
94 if (defined $buffering_output) {
95 $buffering_output .= $_ foreach @_;
103 sub od { # maybe $-doubled
115 sub start_output_file ($) {
116 close_any_output_file();
117 ($writing_output) = @_;
118 die "internal error ($writing_output?)"
119 if $output_files{$writing_output}++;
120 my $tmp = "$writing_output.tmp";
121 open O, ">", $tmp or die "create $tmp: $!\n";
122 oraw "# autogenerated - do not edit\n";
125 sub install_output_files () {
126 close_any_output_file();
127 foreach my $f (sort keys %output_files) {
128 rename "$f.tmp", $f or die "install new $f: $!\n";
132 sub write_makefile ($$) {
133 my ($dir_prefix,$depth) = @_;
134 #print STDERR "write_makefile @_\n";
135 start_output_file("${dir_prefix}Makefile");
136 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
137 my $suppress_templates=
138 '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
139 ' MAKEFILE_TEMPLATES=)';
142 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
144 subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
145 Makefile run-main.mk:
146 \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
152 our ($dir_prefix, $dir_suffix, $dir_name,
153 $var_prefix, $var_prefix_name);
157 join '', map { "$_/" } @$path;
160 sub set_dir_vars ($) {
162 $dir_prefix = dir_prefix($path);
163 $dir_suffix = join '', map { "/$_" } @$path;
164 $dir_name = join '/', @$path ? @$path : '.';
165 $var_prefix_name = join '_', @$path ? @$path : qw(TOP);
166 $var_prefix = "${var_prefix_name}_";
173 die "$0: ${err_file}:$.: $m\n";
179 err "escape &$e is valid only during \$-doubling";
182 sub process_input_mk ($$$$);
183 sub process_input_mk ($$$$) {
184 my ($targets, $f, $esclitr, $enoent_ok) = @_;
186 my $caps_re = qr{[A-Z]};
187 my $lc_re = qr{[a-z]};
196 my $input = new IO::File $f, '<';
198 err "open $f: $!" unless $!==ENOENT && $enoent_ok;
206 '^' => "\$(top_srcdir)${dir_suffix}",
207 '~' => "\$(top_srcdir)",
212 $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
217 my $push_nest = sub {
218 my ($nk, $nndbl) = @_;
219 unshift @nest, [ $nk, $ddbl ];
224 die unless $nest[0][0] eq $nk;
225 # ^ xxx need better message
226 $ddbl = (shift @nest)[1];
230 if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
234 } elsif (s#^\s*$esc\:endm\s+$##) {
235 $pop_nest->('Macro');
238 } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) {
239 $buffering_output='';
240 } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
241 err "unknown directive &:$1";
242 } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
244 od target_varname($var_prefix, $t);
245 $targets->{$t} //= [ ];
248 err 'cannot $-double &-processed RHS of directive'
249 if $ddbl && defined $buffering_output;
250 unless ($nest[0][0] eq 'Eval'
251 ? s{^(.*?)($esc|[{}])}{}
252 : s{^(.*?)($esc)}{}) { od $_; last; }
257 } elsif ($2 eq '}') {
263 if (s{^\\$esc}{}) { od "$$esclitr" }
264 elsif (s{^\\\$}{}) { oud '$' }
265 elsif (s{^\\\s+$}{}) { }
266 elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" }
267 elsif (m{^(?=$caps_re)}) { od $var_prefix }
268 elsif (s{^\$([A-Za-z]\w+)}{}) { od "\$(${var_prefix}$1)" }
269 elsif (s{^([~^]?)(?=$lc_re)}{}) { od $pfxmap{$1} }
270 elsif (s{^_}{}) { od $var_prefix }
271 elsif (s{^=}{}) { od $var_prefix_name }
272 elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} }
273 elsif (s{^\.}{}) { od $dir_name }
274 elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} }
275 elsif (s{^\$\-}{}) { $ddbl=undef; }
276 elsif (s{^\$\+}{}) { $ddbl=1; }
277 elsif (s{^\$\(}{}) { ddbl_only($&); oud "\$("; }
278 elsif (s{^\$(\d+)}{}) { ddbl_only($&); oud "\$($1)"; }
280 err 'macro invocation cannot be re-$-doubled' if $ddbl;
282 $push_nest->('Eval',1);
283 } elsif (s{^([~^]?)(?=[ \t])}{}) {
284 my $prefix = $pfxmap{$1} // die "internal error ($1?)";
286 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
287 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
292 } elsif (s{^![ \t]+}{}) {
296 err "bad &-escape \`$$esclitr$_'";
299 if (defined $buffering_output) {
300 $_=$buffering_output;
301 $buffering_output=undef;
302 if (m#^(-?)include\s+(\S+)\s+$#) {
303 my $subf = "$srcdir/$2";
304 process_input_mk($targets, $subf, $esclitr, $1);
306 } elsif (m#^macro\s+(\S+)\s+$#) {
308 $push_nest->('Macro', 1);
310 die "internal error ($_?)";
314 die "unclosed $nest[0][0]" if $nest[0][0];
315 $input->error and die "read $f: $!\n";
316 close $input or die "close $f: $!\n";
319 sub filter_subdir_mk ($) {
323 #print STDERR "filter @_\n";
328 my ($f, $enoentok) = @_;
329 process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
331 $pi->("Prefix.sd.mk", 1);
332 $pi->("${dir_prefix}Subdir.sd.mk", 0);
333 $pi->("Suffix.sd.mk", 1);
336 sub process_subtree ($$);
337 sub process_subtree ($$) {
338 # => list of targets (in form SUBDIR/)
339 # recursive, children first
340 my ($node, $path) = @_;
343 #print STDERR Dumper(\@_);
345 my $dir_prefix = dir_prefix($path);
346 # ^ this is the only var which we need before we come back from
349 push @output_makefiles, "${dir_prefix}Subdir.mk";
350 write_makefile($dir_prefix, scalar @$path);
352 my %targets = (all => []);
353 foreach my $child (@{ $node->[1] }) {
354 my @childpath = (@$path, $child->[0]);
355 my $child_subdir = join '/', @childpath;
356 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!\n";
357 push @{ $targets{$_} }, $child_subdir foreach
358 process_subtree($child, \@childpath);
362 start_output_file("${dir_prefix}Subdir.mk.tmp");
365 filter_subdir_mk(\%targets);
367 my $sdmk = "${dir_prefix}Subdir.sd.mk";
370 "$0: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)";
371 } elsif ($!==ENOENT) {
373 die "stat $sdmk: $!\n";
379 my @targets = sort keys %targets;
380 foreach my $target (@targets) {
381 my $target_varname = target_varname($var_prefix, $target);
382 print O "${dir_prefix}${target}:: \$($target_varname)";
383 foreach my $child_subdir (@{ $targets{$target} }) {
384 print O " $child_subdir/$target";
390 print O " ${dir_prefix}${_}" foreach @targets;
397 sub process_final ($) {
400 push @output_makefiles, "Final.mk";
401 start_output_file("Final.mk.tmp");
404 process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
405 delete $ntargets{$_} foreach @$otargets;
406 my @ntargets = sort keys %ntargets;
407 die "$0: Final.sd.mk may not introduce new top-level targets".
408 " (@ntargets)\n" if @ntargets;
412 my @targets = process_subtree($root, [ ]);
413 process_final(\@targets);
414 start_output_file("main.mk.tmp");
415 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
418 oraw "SUBDIRMK_MAKEFILES :=\n";
419 oraw "MAKEFILE_TEMPLATES :=\n";
420 foreach my $mf (@output_makefiles) {
421 oraw "SUBDIRMK_MAKEFILES += $mf\n";
423 foreach my $input (sort keys %input_files) {
424 oraw "MAKEFILE_TEMPLATES += $input\n";
426 oraw "include \$(SUBDIRMK_MAKEFILES)\n";
431 install_output_files();