chiark / gitweb /
generate: Introduce oraw
[subdirmk.git] / generate
1 #!/usr/bin/perl -w
2 #
3 # subdirmk - &-filter (makefile generation program)
4 #  Copyright 2019 Ian Jackson
5 # SPDX-License-Identifier: LGPL-2.0-or-later
6 #
7 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
8 #
9 # generates in each subdirectory
10 #     Subdir.mk.tmp
11 #     Makefile
12 # and in toplevel
13 #     main.mk.tmp
14
15 use strict;
16 use POSIX;
17
18 print "$0 @ARGV\n" or die $!;
19
20 our $srcdir='.';
21
22 while (@ARGV && $ARGV[0] =~ m/^-/) {
23     $_ = shift @ARGV;
24     last if $_ eq '--';
25     if (s/^--srcdir=//) {
26         $srcdir=$';
27     } else {
28         die "$0: unknown option \`$_'\n";
29     }
30 }
31 our @subdirs = @ARGV;
32
33 s{/+$}{} foreach @subdirs;
34
35 our $root = [ '.', [ ], 1 ];
36 # each node is [ 'relative subdir name', \@children, $mentioned ]
37
38 sub build_tree () {
39     foreach my $subdir (@subdirs) {
40         my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
41         my $node = $root;
42         foreach my $d (@path) {
43             my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
44             if (!$c) {
45                 $c = [ $d, [ ] ];
46                 push @{ $node->[1] }, $c;
47             }
48             $node = $c;
49         }
50         $node->[2] = 1;
51     }
52 }
53
54 sub target_varname ($$) {
55     my ($var_prefix, $target) = @_;
56     return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
57 }
58
59 our $writing_output;
60 our $buffering_output;
61 our %output_files;
62 our %input_files;
63 our @output_makefiles;
64
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;
70 }
71
72 sub oraw {
73     die unless defined $writing_output;
74     print O @_ or die "error writing $writing_output.tmp: $!\n";
75 }
76
77 sub od { # maybe $-doubled
78     if (defined $buffering_output) {
79         $buffering_output .= $_ foreach @_;
80         return;
81     }
82     oraw @_;
83 }
84
85 sub start_output_file ($) {
86     close_any_output_file();
87     ($writing_output) = @_;
88     die "$writing_output ?" if $output_files{$writing_output}++;
89     my $tmp = "$writing_output.tmp";
90     open O, ">", $tmp or die "create $tmp: $!\n";
91     oraw "# autogenerated - do not edit\n";
92 }
93
94 sub install_output_files () {
95     close_any_output_file();
96     foreach my $f (sort keys %output_files) {
97         rename "$f.tmp", $f or die "install new $f: $!\n";
98     }
99 }
100
101 sub write_makefile ($$) {
102     my ($dir_prefix,$depth) = @_;
103     #print STDERR "write_makefile @_\n";
104     start_output_file("${dir_prefix}Makefile");
105     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
106     my $suppress_templates=
107         '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
108         ' MAKEFILE_TEMPLATES=)';
109     oraw <<END;
110 default: all
111 \$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
112         \@:
113 subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
114 Makefile run-main.mk:
115         \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
116 .SUFFIXES:
117 .PHONY: run-main.mk
118 END
119 }
120
121 our ($dir_prefix, $dir_suffix, $dir_name,
122      $var_prefix, $var_prefix_name);
123
124 sub dir_prefix ($) {
125     my ($path) = @_;
126     join '', map { "$_/" } @$path;
127 }
128
129 sub set_dir_vars ($) {
130     my ($path) = @_;
131     $dir_prefix = dir_prefix($path);
132     $dir_suffix = join '', map { "/$_" } @$path;
133     $dir_name = join '/', @$path ? @$path : '.';
134     $var_prefix_name = join '_', @$path ? @$path : qw(TOP);
135     $var_prefix = "${var_prefix_name}_";
136 }
137
138 sub process_input_mk ($$$$);
139 sub process_input_mk ($$$$) {
140     my ($targets, $f, $esclitr, $enoent_ok) = @_;
141
142     my $caps_re = qr{[A-Z]};
143     my $lc_re = qr{[a-z]};
144
145     my $esc;
146     my $set_esc = sub {
147         $esc = $$esclitr;
148         $esc =~ s/\W/\\$&/g;
149     };
150     $set_esc->();
151
152     my $input = new IO::File $f, '<';
153     if (!$input) {
154         die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
155         return;
156     }
157     $input_files{$f}++;
158
159     my %srcdirmap = (
160                   '^' => "\$(top_srcdir)${dir_suffix}",
161                   '~' => "\$(top_srcdir)",
162                     );
163     my %pfxmap = (
164                   ''  => $dir_prefix,
165                  );
166     $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap;
167
168     while (<$input>) {
169         if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
170             $$esclitr = $1;
171             $set_esc->();
172             next;
173         } elsif (s#^\s*$esc\:(?=(-?)include)##) {
174             $buffering_output='';
175         } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) {
176             die "unknown directive $1";
177         } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
178             my $t = $1 // 'all';
179             od target_varname($var_prefix, $t);
180             $targets->{$t} //= [ ];
181         }
182         for (;;) {
183             unless (s{^(.*?)$esc}{}) { od $_; last; }
184             od $1;
185             if (s{^\\$esc}{}) { od "$$esclitr" }
186             elsif (s{^\\\$}{}) { od '$' }
187             elsif (s{^\\\s+$}{}) { }
188             elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" }
189             elsif (m{^(?=$caps_re)}) { od $var_prefix }
190             elsif (s{^\$([A-Za-z]\w+)}{}) { od "\$(${var_prefix}$1)" }
191             elsif (s{^([~^]?)(?=$lc_re)}{}) { od $pfxmap{$1} }
192             elsif (s{^_}{}) { od $var_prefix }
193             elsif (s{^=}{}) { od $var_prefix_name }
194             elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} }
195             elsif (s{^\.}{}) { od $dir_name }
196             elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} }
197             elsif (s{^([~^]?)(?=[ \t])}{}) {
198                 my $prefix = $pfxmap{$1} // die;
199                 my $after='';
200                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
201                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
202                 od $_;
203                 $_ = $after;
204             } elsif (s{^\#}{}) {
205                 $_ = '';
206             } elsif (s{^![ \t]+}{}) {
207                 od $_;
208                 $_ = '';
209             } else {
210                 die "bad escape $$esclitr$_ ";
211             }
212         }
213         if (defined $buffering_output) {
214             $_=$buffering_output;
215             $buffering_output=undef;
216             if (m#^(-?)include\s+(\S+)\s+$#) {
217                 my $subf = "$srcdir/$2";
218                 process_input_mk($targets, $subf, $esclitr, $1);
219                 od "\n";
220             } else {
221                 die "internal error buffering directive $_ ";
222             }
223         }
224     }
225     $input->error and die "read $f: $!\n";
226     close $input or die "close $f: $!\n";
227 }
228
229 sub filter_subdir_mk ($) {
230     my ($targets) = @_;
231
232     #use Data::Dumper;
233     #print STDERR "filter @_\n";
234
235     my $esclit = '&';
236
237     my $pi = sub {
238         my ($f, $enoentok) = @_;
239         process_input_mk($targets, "${srcdir}/$f", \$esclit, $enoentok);
240     };
241     $pi->("Prefix.sd.mk",              1);
242     $pi->("${dir_prefix}Subdir.sd.mk", 0);
243     $pi->("Suffix.sd.mk",              1);
244 }
245
246 sub process_subtree ($$);
247 sub process_subtree ($$) {
248     # => list of targets (in form SUBDIR/)
249     # recursive, children first
250     my ($node, $path) = @_;
251
252     #use Data::Dumper;
253     #print STDERR Dumper(\@_);
254
255     my $dir_prefix = dir_prefix($path);
256     # ^ this is the only var which we need before we come back from
257     #   the recursion.
258
259     push @output_makefiles, "${dir_prefix}Subdir.mk";
260     write_makefile($dir_prefix, scalar @$path);
261
262     my %targets = (all => []);
263     foreach my $child (@{ $node->[1] }) {
264         my @childpath = (@$path, $child->[0]);
265         my $child_subdir = join '/', @childpath;
266         mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
267         push @{ $targets{$_} }, $child_subdir foreach
268             process_subtree($child, \@childpath);
269     }
270
271     set_dir_vars($path);
272     start_output_file("${dir_prefix}Subdir.mk.tmp");
273
274     if ($node->[2]) {
275         filter_subdir_mk(\%targets);
276     } else {
277         my $sdmk = "${dir_prefix}Subdir.sd.mk";
278         if (stat $sdmk) {
279             die "$sdmk unexpectedly exists (${dir_prefix} not mentioned)";
280         } elsif ($!==ENOENT) {
281         } else {
282             die "stat $sdmk: $!";
283         }
284     }
285
286     oraw "\n";
287
288     my @targets = sort keys %targets;
289     foreach my $target (@targets) {
290         my $target_varname = target_varname($var_prefix, $target);
291         print O "${dir_prefix}${target}:: \$($target_varname)";
292         foreach my $child_subdir (@{ $targets{$target} }) {
293             print O " $child_subdir/$target";
294         }
295         print O "\n";
296     }
297     if (@targets) {
298         print O ".PHONY:";
299         print O " ${dir_prefix}${_}" foreach @targets;
300         print O "\n";
301     }
302
303     return @targets;
304 }
305
306 sub process_final ($) {
307     my ($otargets) = @_;
308     set_dir_vars([]);
309     push @output_makefiles, "Final.mk";
310     start_output_file("Final.mk.tmp");
311     my %ntargets;
312     my $esclit='&';
313     process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1);
314     delete $ntargets{$_} foreach @$otargets;
315     my @ntargets = sort keys %ntargets;
316     die "late new targets @ntargets" if @ntargets;
317 }
318
319 sub process_tree() {
320     my @targets = process_subtree($root, [ ]);
321     process_final(\@targets);
322     start_output_file("main.mk.tmp");
323     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
324         oraw "$v=\@$v@\n";
325     }
326     oraw "SUBDIRMK_MAKEFILES :=\n";
327     oraw "MAKEFILE_TEMPLATES :=\n";
328     foreach my $mf (@output_makefiles) {
329         oraw "SUBDIRMK_MAKEFILES += $mf\n";
330     }
331     foreach my $input (sort keys %input_files) {
332         oraw "MAKEFILE_TEMPLATES += $input\n";
333     }
334     oraw "include \$(SUBDIRMK_MAKEFILES)\n";
335 }
336
337 build_tree();
338 process_tree();
339 install_output_files();