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