chiark / gitweb /
7b8727b1988c254968889387fb5c402a8f7965b6
[subdirmk.git] / subdirmk / generate
1 #!/usr/bin/perl -w
2 #
3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
4 #
5 # generates in each subdirectory        from in each subdirectory
6 #     Subdir.mk.tmp                         Subdir.mk.in
7 #     Makefile
8 # and in toplevel                       and in toplevel
9 #     main.mk.tmp                           Perdir.mk.in
10
11 use strict;
12 use POSIX;
13
14 print "$0 @ARGV\n" or die $!;
15
16 our $srcdir='.';
17
18 while (@ARGV && $ARGV[0] =~ m/^-/) {
19     $_ = shift @ARGV;
20     last if $_ eq '--';
21     if (s/^--srcdir=//) {
22         $srcdir=$';
23     } else {
24         die "$0: unknown option \`$_'\n";
25     }
26 }
27 our @subdirs = @ARGV;
28
29 s{/+$}{} foreach @subdirs;
30
31 our $root = [ '.', [ ] ];
32 # each node is [ 'relative subdir name', \@children ]
33
34 sub build_tree () {
35     foreach my $subdir (@subdirs) {
36         my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
37         my $node = $root;
38         foreach my $d (@path) {
39             my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
40             if (!$c) {
41                 $c = [ $d, [ ] ];
42                 push @{ $node->[1] }, $c;
43             }
44             $node = $c;
45         }
46     }
47 }
48
49 sub target_varname ($$) {
50     my ($var_prefix, $target) = @_;
51     return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
52 }
53
54 our $writing_output;
55 our $buffering_output;
56 our %output_files;
57
58 sub close_any_output_file() {
59     return unless defined $writing_output;
60     O->error and die "error writing $writing_output.tmp: $! (?)\n";
61     close O or die "error closing $writing_output.tmp: $!\n";
62     $writing_output = undef;
63 }
64
65 sub o {
66     if (defined $buffering_output) {
67         $buffering_output .= $_ foreach @_;
68         return;
69     }
70     die unless defined $writing_output;
71     print O @_ or die "error writing $writing_output.tmp: $!\n";
72 }
73
74 sub start_output_file ($) {
75     close_any_output_file();
76     ($writing_output) = @_;
77     die "$writing_output ?" if $output_files{$writing_output}++;
78     my $tmp = "$writing_output.tmp";
79     open O, ">", $tmp or die "create $tmp: $!\n";
80     o "# autogenerated - do not edit\n";
81 }
82
83 sub install_output_files () {
84     close_any_output_file();
85     foreach my $f (sort keys %output_files) {
86         rename "$f.tmp", $f or die "install new $f: $!\n";
87     }
88 }
89
90 sub write_makefile ($$) {
91     my ($dir_prefix,$depth) = @_;
92     #print STDERR "write_makefile @_\n";
93     start_output_file("${dir_prefix}Makefile");
94     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
95     o <<END;
96 default: all
97         \@: \$@
98 %:      FORCE-ALWAYS-RUN
99         \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
100 Makefile FORCE-ALWAYS-RUN:
101         \@: \$@
102 .SUFFIXES:
103 END
104 }
105
106 sub process_input_mk ($$$$$$$$);
107 sub process_input_mk ($$$$$$$$) {
108     my ($dir_prefix, $dir_suffix, $dir_name,
109         $var_prefix, $targets,
110         $f, $esclitr, $enoent_ok) = @_;
111
112     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
113     my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
114
115     my $esc;
116     my $set_esc = sub {
117         $esc = $$esclitr;
118         $esc =~ s/\W/\\$&/g;
119     };
120     $set_esc->();
121
122     my $input = new IO::File $f, '<';
123     if (!$input) {
124         die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
125         return;
126     }
127     while (<$input>) {
128         for (;;) {
129             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
130             o $1;
131             if ($2) { o $$esclitr; next; }
132             s{^$esc}{} or die "$_ ?";
133             if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
134             elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
135                 my $t = $1 // 'all';
136                 o target_varname($var_prefix, $t);
137                 $targets->{$t} //= [ ];
138             }
139             elsif (m{^(?=$caps_re)}) { o $var_prefix }
140             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
141             elsif (s{^_}{}) { o $var_prefix }
142             elsif (s{^/}{}) { o $dir_prefix }
143             elsif (s{^=_}{}) { o $var_prefix }
144             elsif (s{^=/}{}) { o $dir_name }
145             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
146             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
147             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
148                 my $prefix =
149                     !$1       ? $dir_prefix                     :
150                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
151                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
152                     die;
153                 my $after='';
154                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
155                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
156                 o $_;
157                 $_ = $after;
158             } elsif (s{^![ \t]+}{}) {
159                 o $_;
160                 $_ = '';
161             } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
162                 $$esclitr = $1;
163                 $set_esc->();
164             } else {
165                 die "bad escape $$esclitr$_ ";
166             }
167         }
168     }
169     $input->error and die "read $f: $!\n";
170     close $input or die "close $f: $!\n";
171 }
172
173 sub filter_subdir_mk ($$$$$) {
174     my ($dir_prefix, $dir_suffix, $dir_name,
175         $var_prefix, $targets) = @_;
176
177     #use Data::Dumper;
178     #print STDERR "filter @_\n";
179
180     my $esclit = '&';
181     for my $f ("${srcdir}/${dir_prefix}Subdir.mk.in",
182                "${srcdir}/Perdir.mk.in") {
183         process_input_mk($dir_prefix, $dir_suffix, $dir_name,
184                          $var_prefix, $targets,
185                          $f, \$esclit, 0);
186     }
187 }
188
189 sub process_subtree ($$);
190 sub process_subtree ($$) {
191     # => list of descendants (in form SUBDIR/)
192     # recursive, children first
193     my ($node, $path) = @_;
194
195     #use Data::Dumper;
196     #print STDERR Dumper(\@_);
197
198     my $dir_prefix = join '', map { "$_/" } @$path;
199     my $dir_suffix = join '', map { "/$_" } @$path;
200     my $dir_name = join '/', @$path ? @$path : '.';
201     my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
202
203     write_makefile($dir_prefix, scalar @$path);
204
205     my %targets = (all => []);
206     foreach my $child (@{ $node->[1] }) {
207         my @childpath = (@$path, $child->[0]);
208         my $child_subdir = join '/', @childpath;
209         mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
210         push @{ $targets{$_} }, $child_subdir foreach
211             process_subtree($child, \@childpath);
212     }
213     start_output_file("${dir_prefix}Subdir.mk.tmp");
214
215     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
216                      $var_prefix, \%targets);
217
218     o "\n";
219
220     my @targets = sort keys %targets;
221     foreach my $target (@targets) {
222         my $target_varname = target_varname($var_prefix, $target);
223         print O "${dir_prefix}${target}: \$($target_varname)";
224         foreach my $child_subdir (@{ $targets{$target} }) {
225             print O " $child_subdir/$target";
226         }
227         print O "\n";
228     }
229     
230     return @targets;
231 }
232
233 sub process_tree() {
234     process_subtree($root, [ ]);
235     start_output_file("main.mk.tmp");
236     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
237         o "$v=\@$v@\n";
238     }
239     o "MAKEFILES += Subdir.mk\n";
240     foreach my $subdir (@subdirs) {
241         o "MAKEFILES += $subdir/Subdir.mk\n";
242     }
243     o "include \$(MAKEFILES)";
244 }
245
246 build_tree();
247 process_tree();
248 install_output_files();