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