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 from in each subdirectory
10 # Subdir.mk.tmp Subdir.sd.mk
11 # Makefile and included files
12 # and in toplevel and in toplevel
13 # main.mk.tmp Perdir.sd.mk
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 = [ '.', [ ] ];
36 # each node is [ 'relative subdir name', \@children ]
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;
53 sub target_varname ($$) {
54 my ($var_prefix, $target) = @_;
55 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
59 our $buffering_output;
63 sub close_any_output_file() {
64 return unless defined $writing_output;
65 O->error and die "error writing $writing_output.tmp: $! (?)\n";
66 close O or die "error closing $writing_output.tmp: $!\n";
67 $writing_output = undef;
71 if (defined $buffering_output) {
72 $buffering_output .= $_ foreach @_;
75 die unless defined $writing_output;
76 print O @_ or die "error writing $writing_output.tmp: $!\n";
79 sub start_output_file ($) {
80 close_any_output_file();
81 ($writing_output) = @_;
82 die "$writing_output ?" if $output_files{$writing_output}++;
83 my $tmp = "$writing_output.tmp";
84 open O, ">", $tmp or die "create $tmp: $!\n";
85 o "# autogenerated - do not edit\n";
88 sub install_output_files () {
89 close_any_output_file();
90 foreach my $f (sort keys %output_files) {
91 rename "$f.tmp", $f or die "install new $f: $!\n";
95 sub write_makefile ($$) {
96 my ($dir_prefix,$depth) = @_;
97 #print STDERR "write_makefile @_\n";
98 start_output_file("${dir_prefix}Makefile");
99 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
104 Makefile FORCE-ALWAYS-RUN:
105 \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(MAKECMDGOALS))
110 sub process_input_mk ($$$$$$$$);
111 sub process_input_mk ($$$$$$$$) {
112 my ($dir_prefix, $dir_suffix, $dir_name,
113 $var_prefix, $targets,
114 $f, $esclitr, $enoent_ok) = @_;
116 my $caps_re = qr{[A-Z]};
117 my $lc_re = qr{[a-z]};
126 my $input = new IO::File $f, '<';
128 die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
134 if (s#^\s*$esc\:##) {
135 $buffering_output='';
138 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
140 if ($2) { o $$esclitr; next; }
141 s{^$esc}{} or die "$_ ?";
142 if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
143 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
145 o target_varname($var_prefix, $t);
146 $targets->{$t} //= [ ];
148 elsif (m{^(?=$caps_re)}) { o $var_prefix }
149 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
150 elsif (s{^_}{}) { o $var_prefix }
151 elsif (s{^/}{}) { o $dir_prefix }
152 elsif (s{^=_}{}) { o $var_prefix }
153 elsif (s{^=/}{}) { o $dir_name }
154 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
155 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
156 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
159 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
160 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
163 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
164 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
167 } elsif (s{^![ \t]+}{}) {
170 } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
174 die "bad escape $$esclitr$_ ";
177 if (defined $buffering_output) {
178 $_=$buffering_output;
179 $buffering_output=undef;
180 if (m#^(-?)include\s+(\S+)\s+$#) {
181 my $subf = "$srcdir/$2";
182 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
183 $var_prefix, $targets,
184 $subf, $esclitr, $1);
187 die "unknown directive $_ ";
191 $input->error and die "read $f: $!\n";
192 close $input or die "close $f: $!\n";
195 sub filter_subdir_mk ($$$$$) {
196 my ($dir_prefix, $dir_suffix, $dir_name,
197 $var_prefix, $targets) = @_;
200 #print STDERR "filter @_\n";
205 my ($f, $enoentok) = @_;
206 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
207 $var_prefix, $targets,
208 "${srcdir}/$f", \$esclit, $enoentok);
210 $pi->("${dir_prefix}Subdir.sd.mk", 0);
211 $pi->("Perdir.sd.mk", 1);
214 sub process_subtree ($$);
215 sub process_subtree ($$) {
216 # => list of descendants (in form SUBDIR/)
217 # recursive, children first
218 my ($node, $path) = @_;
221 #print STDERR Dumper(\@_);
223 my $dir_prefix = join '', map { "$_/" } @$path;
224 my $dir_suffix = join '', map { "/$_" } @$path;
225 my $dir_name = join '/', @$path ? @$path : '.';
226 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
228 write_makefile($dir_prefix, scalar @$path);
230 my %targets = (all => []);
231 foreach my $child (@{ $node->[1] }) {
232 my @childpath = (@$path, $child->[0]);
233 my $child_subdir = join '/', @childpath;
234 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
235 push @{ $targets{$_} }, $child_subdir foreach
236 process_subtree($child, \@childpath);
238 start_output_file("${dir_prefix}Subdir.mk.tmp");
240 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
241 $var_prefix, \%targets);
245 my @targets = sort keys %targets;
246 foreach my $target (@targets) {
247 my $target_varname = target_varname($var_prefix, $target);
248 print O "${dir_prefix}${target}:: \$($target_varname)";
249 foreach my $child_subdir (@{ $targets{$target} }) {
250 print O " $child_subdir/$target";
259 process_subtree($root, [ ]);
260 start_output_file("main.mk.tmp");
261 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
264 o "MAKEFILES += Subdir.mk\n";
265 foreach my $subdir (@subdirs) {
266 o "MAKEFILES += $subdir/Subdir.mk\n";
268 foreach my $input (sort keys %input_files) {
269 o "MAKEFILE_TEMPLATES += $input\n";
271 o "include \$(MAKEFILES)";
276 install_output_files();