chiark / gitweb /
110f8f1a6e8fcba53ccefe3ef7838a805d8d0c55
[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 %output_files;
56
57 sub close_any_output_file() {
58     return unless defined $writing_output;
59     O->error and die "error writing $writing_output.tmp: $! (?)\n";
60     close O or die "error closing $writing_output.tmp: $!\n";
61     $writing_output = undef;
62 }
63
64 sub o {
65     die unless defined $writing_output;
66     print O @_ or die "error writing $writing_output.tmp: $!\n";
67 }
68
69 sub start_output_file ($) {
70     close_any_output_file();
71     ($writing_output) = @_;
72     die "$writing_output ?" if $output_files{$writing_output}++;
73     my $tmp = "$writing_output.tmp";
74     open O, ">", $tmp or die "create $tmp: $!\n";
75     o "# autogenerated - do not edit\n";
76 }
77
78 sub install_output_files () {
79     close_any_output_file();
80     foreach my $f (sort keys %output_files) {
81         rename "$f.tmp", $f or die "install new $f: $!\n";
82     }
83 }
84
85 sub write_makefile ($$) {
86     my ($dir_prefix,$depth) = @_;
87     #print STDERR "write_makefile @_\n";
88     start_output_file("${dir_prefix}Makefile");
89     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
90     o <<END;
91 default: all
92         \@: \$@
93 %:      FORCE-ALWAYS-RUN
94         \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
95 Makefile FORCE-ALWAYS-RUN:
96         \@: \$@
97 .SUFFIXES:
98 END
99 }
100
101 sub process_input_mk ($$$$$$$);
102 sub process_input_mk ($$$$$$$) {
103     my ($dir_prefix, $dir_suffix, $dir_name,
104         $var_prefix, $targets,
105         $f, $esclitr) = @_;
106
107     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
108     my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
109
110     my $esc;
111     my $set_esc = sub {
112         $esc = $$esclitr;
113         $esc =~ s/\W/\\$&/g;
114     };
115     $set_esc->();
116
117     open I, '<', $f or die "open $f: $!\n";
118     while (<I>) {
119         for (;;) {
120             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
121             o $1;
122             if ($2) { o $$esclitr; next; }
123             s{^$esc}{} or die "$_ ?";
124             if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
125             elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
126                 my $t = $1 // 'all';
127                 o target_varname($var_prefix, $t);
128                 $targets->{$t} //= [ ];
129             }
130             elsif (m{^(?=$caps_re)}) { o $var_prefix }
131             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
132             elsif (s{^_}{}) { o $var_prefix }
133             elsif (s{^/}{}) { o $dir_prefix }
134             elsif (s{^=_}{}) { o $var_prefix }
135             elsif (s{^=/}{}) { o $dir_name }
136             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
137             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
138             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
139                 my $prefix =
140                     !$1       ? $dir_prefix                     :
141                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
142                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
143                     die;
144                 my $after='';
145                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
146                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
147                 o $_;
148                 $_ = $after;
149             } elsif (s{^![ \t]+}{}) {
150                 o $_;
151                 $_ = '';
152             } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
153                 $$esclitr = $1;
154                 $set_esc->();
155             } else {
156                 die "bad escape $$esclitr$_ ";
157             }
158         }
159     }
160     I->error and die "read $f: $!\n";
161 }
162
163 sub filter_subdir_mk ($$$$$) {
164     my ($dir_prefix, $dir_suffix, $dir_name,
165         $var_prefix, $targets) = @_;
166
167     #use Data::Dumper;
168     #print STDERR "filter @_\n";
169
170     my $esclit = '&';
171     for my $f ("${srcdir}/${dir_prefix}Subdir.mk.in",
172                "${srcdir}/Perdir.mk.in") {
173         process_input_mk($dir_prefix, $dir_suffix, $dir_name,
174                          $var_prefix, $targets,
175                          $f, \$esclit);
176     }
177 }
178
179 sub process_subtree ($$);
180 sub process_subtree ($$) {
181     # => list of descendants (in form SUBDIR/)
182     # recursive, children first
183     my ($node, $path) = @_;
184
185     #use Data::Dumper;
186     #print STDERR Dumper(\@_);
187
188     my $dir_prefix = join '', map { "$_/" } @$path;
189     my $dir_suffix = join '', map { "/$_" } @$path;
190     my $dir_name = join '/', @$path ? @$path : '.';
191     my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
192
193     write_makefile($dir_prefix, scalar @$path);
194
195     my %targets = (all => []);
196     foreach my $child (@{ $node->[1] }) {
197         my @childpath = (@$path, $child->[0]);
198         my $child_subdir = join '/', @childpath;
199         mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
200         push @{ $targets{$_} }, $child_subdir foreach
201             process_subtree($child, \@childpath);
202     }
203     start_output_file("${dir_prefix}Subdir.mk.tmp");
204
205     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
206                      $var_prefix, \%targets);
207
208     o "\n";
209
210     my @targets = sort keys %targets;
211     foreach my $target (@targets) {
212         my $target_varname = target_varname($var_prefix, $target);
213         print O "${dir_prefix}${target}: \$($target_varname)";
214         foreach my $child_subdir (@{ $targets{$target} }) {
215             print O " $child_subdir/$target";
216         }
217         print O "\n";
218     }
219     
220     return @targets;
221 }
222
223 sub process_tree() {
224     process_subtree($root, [ ]);
225     start_output_file("main.mk.tmp");
226     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
227         o "$v=\@$v@\n";
228     }
229     o "MAKEFILES += Subdir.mk\n";
230     foreach my $subdir (@subdirs) {
231         o "MAKEFILES += $subdir/Subdir.mk\n";
232     }
233     o "include \$(MAKEFILES)";
234 }
235
236 build_tree();
237 process_tree();
238 install_output_files();