chiark / gitweb /
f353702fbb78a5277591bec73e505fbcf499afee
[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) = @_;
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     open I, '<', $f or die "open $f: $!\n";
123     while (<I>) {
124         for (;;) {
125             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
126             o $1;
127             if ($2) { o $$esclitr; next; }
128             s{^$esc}{} or die "$_ ?";
129             if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
130             elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
131                 my $t = $1 // 'all';
132                 o target_varname($var_prefix, $t);
133                 $targets->{$t} //= [ ];
134             }
135             elsif (m{^(?=$caps_re)}) { o $var_prefix }
136             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
137             elsif (s{^_}{}) { o $var_prefix }
138             elsif (s{^/}{}) { o $dir_prefix }
139             elsif (s{^=_}{}) { o $var_prefix }
140             elsif (s{^=/}{}) { o $dir_name }
141             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
142             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
143             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
144                 my $prefix =
145                     !$1       ? $dir_prefix                     :
146                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
147                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
148                     die;
149                 my $after='';
150                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
151                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
152                 o $_;
153                 $_ = $after;
154             } elsif (s{^![ \t]+}{}) {
155                 o $_;
156                 $_ = '';
157             } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
158                 $$esclitr = $1;
159                 $set_esc->();
160             } else {
161                 die "bad escape $$esclitr$_ ";
162             }
163         }
164     }
165     I->error and die "read $f: $!\n";
166 }
167
168 sub filter_subdir_mk ($$$$$) {
169     my ($dir_prefix, $dir_suffix, $dir_name,
170         $var_prefix, $targets) = @_;
171
172     #use Data::Dumper;
173     #print STDERR "filter @_\n";
174
175     my $esclit = '&';
176     for my $f ("${srcdir}/${dir_prefix}Subdir.mk.in",
177                "${srcdir}/Perdir.mk.in") {
178         process_input_mk($dir_prefix, $dir_suffix, $dir_name,
179                          $var_prefix, $targets,
180                          $f, \$esclit);
181     }
182 }
183
184 sub process_subtree ($$);
185 sub process_subtree ($$) {
186     # => list of descendants (in form SUBDIR/)
187     # recursive, children first
188     my ($node, $path) = @_;
189
190     #use Data::Dumper;
191     #print STDERR Dumper(\@_);
192
193     my $dir_prefix = join '', map { "$_/" } @$path;
194     my $dir_suffix = join '', map { "/$_" } @$path;
195     my $dir_name = join '/', @$path ? @$path : '.';
196     my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
197
198     write_makefile($dir_prefix, scalar @$path);
199
200     my %targets = (all => []);
201     foreach my $child (@{ $node->[1] }) {
202         my @childpath = (@$path, $child->[0]);
203         my $child_subdir = join '/', @childpath;
204         mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
205         push @{ $targets{$_} }, $child_subdir foreach
206             process_subtree($child, \@childpath);
207     }
208     start_output_file("${dir_prefix}Subdir.mk.tmp");
209
210     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
211                      $var_prefix, \%targets);
212
213     o "\n";
214
215     my @targets = sort keys %targets;
216     foreach my $target (@targets) {
217         my $target_varname = target_varname($var_prefix, $target);
218         print O "${dir_prefix}${target}: \$($target_varname)";
219         foreach my $child_subdir (@{ $targets{$target} }) {
220             print O " $child_subdir/$target";
221         }
222         print O "\n";
223     }
224     
225     return @targets;
226 }
227
228 sub process_tree() {
229     process_subtree($root, [ ]);
230     start_output_file("main.mk.tmp");
231     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
232         o "$v=\@$v@\n";
233     }
234     o "MAKEFILES += Subdir.mk\n";
235     foreach my $subdir (@subdirs) {
236         o "MAKEFILES += $subdir/Subdir.mk\n";
237     }
238     o "include \$(MAKEFILES)";
239 }
240
241 build_tree();
242 process_tree();
243 install_output_files();