chiark / gitweb /
603dab6f6cdbeebd9ed3270ebf99de34d1300f97
[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 %:      FORCE-ALWAYS-RUN
83         \$(MAKE) -C $cd -f subdirs.mk ${dir_prefix}\$@
84 Makefile FORCE-ALWAYS-RUN:
85         : \$@
86 .SUFFIXES:
87 END
88 }
89
90 sub filter_subdir_mk ($$$$$) {
91     my ($dir_prefix, $dir_suffix, $dir_name,
92         $var_prefix, $targets) = @_;
93
94     #use Data::Dumper;
95     #print STDERR "filter @_\n";
96
97     my $in = "${dir_prefix}Subdir.mk.in";
98     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
99     my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
100     my $esclit = '&';
101     my $esc = '\\&';
102
103     for my $f ($in, "Perdir.mk.in") {
104       open I, '<', $f or die "open $f: $!\n";
105       while (<I>) {
106         for (;;) {
107             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
108             o $1;
109             if ($2) { o $esclit; next; }
110             s{^$esc}{} or die "$_ ?";
111             if (s{^$esc}{}) { o "$esclit$esclit" }
112             elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
113                 my $t = $1 // 'all';
114                 o target_varname($var_prefix, $t);
115                 $targets->{$t}=1;
116             }
117             elsif (m{^(?=$caps_re)}) { o $var_prefix }
118             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
119             elsif (s{^_}{}) { o $var_prefix }
120             elsif (s{^/}{}) { o $dir_prefix }
121             elsif (s{^=_}{}) { o $var_prefix }
122             elsif (s{^=/}{}) { o $dir_name }
123             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
124             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
125             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
126                 my $prefix =
127                     !$1       ? $dir_prefix                     :
128                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
129                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
130                     die;
131                 my $after='';
132                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
133                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
134                 o $_;
135                 $_ = $after;
136             } elsif (s{^![ \t]+}{}) {
137                 o $_;
138                 $_ = '';
139             } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
140                 $esclit = $1;
141                 $esc = $esclit;
142                 $esc =~ s/\W/\\$&/g;
143             } else {
144                 die "bad escape $esclit$_ ";
145             }
146         }
147       }
148       I->error and die "read $f: $!\n";
149     }
150 }
151
152 sub process_subtree ($$);
153 sub process_subtree ($$) {
154     # => list of descendants (in form SUBDIR/)
155     # recursive, children first
156     my ($node, $path) = @_;
157
158     #use Data::Dumper;
159     #print STDERR Dumper(\@_);
160
161     my $dir_prefix = join '', map { "$_/" } @$path;
162     my $dir_suffix = join '', map { "/$_" } @$path;
163     my $dir_name = join '/', @$path ? @$path : '.';
164     my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
165
166     write_makefile($dir_prefix, scalar @$path);
167
168     my %targets = qw(all 1);
169     my @child_subdirs;
170     foreach my $child (@{ $node->[1] }) {
171         my @childpath = (@$path, $child->[0]);
172         push @child_subdirs, join '/', @childpath;
173         $targets{$_}++ foreach
174             process_subtree($child, \@childpath);
175     }
176     start_output_file("${dir_prefix}Subdir.mk.tmp");
177
178     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
179                      $var_prefix, \%targets);
180
181     o "\n";
182
183     my @targets = sort keys %targets;
184     foreach my $target (@targets) {
185         my $target_varname = target_varname($var_prefix, $target);
186         print O "${dir_prefix}${target}: \$($target_varname)";
187         foreach my $child_subdir (@child_subdirs) {
188             print O " $child_subdir/$target";
189         }
190         print O "\n";
191     }
192     
193     return @targets;
194 }
195
196 sub process_tree() {
197     process_subtree($root, [ ]);
198     start_output_file("subdirs.mk.tmp");
199     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
200         o "$v=\@$v@\n";
201     }
202     o "MAKEFILES += Subdir.mk\n";
203     foreach my $subdir (@subdirs) {
204         o "MAKEFILES += $subdir/Subdir.mk\n";
205     }
206     o "include \$(MAKEFILES)";
207 }
208
209 build_tree();
210 process_tree();
211 install_output_files();