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