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