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