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 if (defined $buffering_output) {
74 $buffering_output .= $_ foreach @_;
77 die unless defined $writing_output;
78 print O @_ or die "error writing $writing_output.tmp: $!\n";
81 sub start_output_file ($) {
82 close_any_output_file();
83 ($writing_output) = @_;
84 die "$writing_output ?" if $output_files{$writing_output}++;
85 my $tmp = "$writing_output.tmp";
86 open O, ">", $tmp or die "create $tmp: $!\n";
87 o "# autogenerated - do not edit\n";
90 sub install_output_files () {
91 close_any_output_file();
92 foreach my $f (sort keys %output_files) {
93 rename "$f.tmp", $f or die "install new $f: $!\n";
97 sub write_makefile ($$) {
98 my ($dir_prefix,$depth) = @_;
99 #print STDERR "write_makefile @_\n";
100 start_output_file("${dir_prefix}Makefile");
101 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
102 my $suppress_templates=
103 '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
104 ' MAKEFILE_TEMPLATES=)';
107 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
109 subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
110 Makefile run-main.mk:
111 \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
117 our ($dir_prefix, $dir_suffix, $dir_name,
118 $var_prefix, $var_prefix_name);
122 join '', map { "$_/" } @$path;
125 sub set_dir_vars ($) {
127 $dir_prefix = dir_prefix($path);
128 $dir_suffix = join '', map { "/$_" } @$path;
129 $dir_name = join '/', @$path ? @$path : '.';
130 $var_prefix_name = join '_', @$path ? @$path : qw(TOP);
131 $var_prefix = "${var_prefix_name}_";
134 sub process_input_mk ($$$$);
135 sub process_input_mk ($$$$) {
136 my ($targets, $f, $esclitr, $enoent_ok) = @_;
138 my $caps_re = qr{[A-Z]};
139 my $lc_re = qr{[a-z]};
148 my $input = new IO::File $f, '<';
150 die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
156 '^' => "\$(top_srcdir)${dir_suffix}",
157 '~' => "\$(top_srcdir)",
162 $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
165 if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
169 } elsif (s#^\s*$esc\:(?=(-?)include)##) {
170 $buffering_output='';
171 } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
172 die "unknown directive $1";
173 } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
175 o target_varname($var_prefix, $t);
176 $targets->{$t} //= [ ];
179 unless (s{^(.*?)$esc}{}) { o $_; last; }
181 if (s{^\\$esc}{}) { o "$$esclitr" }
182 elsif (s{^\\\$}{}) { o '$' }
183 elsif (s{^\\\s+$}{}) { }
184 elsif (s{^$esc}{}) { o "$$esclitr$$esclitr" }
185 elsif (m{^(?=$caps_re)}) { o $var_prefix }
186 elsif (s{^\$([A-Za-z]\w+)}{}) { o "\$(${var_prefix}$1)" }
187 elsif (s{^([~^]?)(?=$lc_re)}{}) { o $pfxmap{$1} }
188 elsif (s{^_}{}) { o $var_prefix }
189 elsif (s{^=}{}) { o $var_prefix_name }
190 elsif (s{^([~^]?)/}{}) { o $pfxmap{$1} }
191 elsif (s{^\.}{}) { o $dir_name }
192 elsif (s{^([~^])\.}{}) { o $srcdirmap{$1} }
193 elsif (s{^([~^]?)(?=[ \t])}{}) {
194 my $prefix = $pfxmap{$1} // die;
196 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
197 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
202 } elsif (s{^![ \t]+}{}) {
206 die "bad escape $$esclitr$_ ";
209 if (defined $buffering_output) {
210 $_=$buffering_output;
211 $buffering_output=undef;
212 if (m#^(-?)include\s+(\S+)\s+$#) {
213 my $subf = "$srcdir/$2";
214 process_input_mk($targets, $subf, $esclitr, $1);
217 die "internal error buffering directive $_ ";
221 $input->error and die "read $f: $!\n";
222 close $input or die "close $f: $!\n";
225 sub filter_subdir_mk ($) {
229 #print STDERR "filter @_\n";
234 my ($f, $enoentok) = @_;
235 process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
237 $pi->("${dir_prefix}Subdir.sd.mk", 0);
238 $pi->("Perdir.sd.mk", 1);
241 sub process_subtree ($$);
242 sub process_subtree ($$) {
243 # => list of targets (in form SUBDIR/)
244 # recursive, children first
245 my ($node, $path) = @_;
248 #print STDERR Dumper(\@_);
250 my $dir_prefix = dir_prefix($path);
251 # ^ this is the only var which we need before we come back from
254 push @output_makefiles, "${dir_prefix}Subdir.mk";
255 write_makefile($dir_prefix, scalar @$path);
257 my %targets = (all => []);
258 foreach my $child (@{ $node->[1] }) {
259 my @childpath = (@$path, $child->[0]);
260 my $child_subdir = join '/', @childpath;
261 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
262 push @{ $targets{$_} }, $child_subdir foreach
263 process_subtree($child, \@childpath);
267 start_output_file("${dir_prefix}Subdir.mk.tmp");
270 filter_subdir_mk(\%targets);
272 my $sdmk = "${dir_prefix}Subdir.sd.mk";
274 die "$sdmk unexpectedly exists (${dir_prefix} not mentioned)";
275 } elsif ($!==ENOENT) {
277 die "stat $sdmk: $!";
283 my @targets = sort keys %targets;
284 foreach my $target (@targets) {
285 my $target_varname = target_varname($var_prefix, $target);
286 print O "${dir_prefix}${target}:: \$($target_varname)";
287 foreach my $child_subdir (@{ $targets{$target} }) {
288 print O " $child_subdir/$target";
294 print O " ${dir_prefix}${_}" foreach @targets;
302 process_subtree($root, [ ]);
303 start_output_file("main.mk.tmp");
304 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
307 o "SUBDIRMK_MAKEFILES :=\n";
308 o "MAKEFILE_TEMPLATES :=\n";
309 foreach my $mf (@output_makefiles) {
310 o "SUBDIRMK_MAKEFILES += $mf\n";
312 foreach my $input (sort keys %input_files) {
313 o "MAKEFILE_TEMPLATES += $input\n";
315 o "include \$(SUBDIRMK_MAKEFILES)\n";
320 install_output_files();