chiark / gitweb /
f7216219700d2e5fe82416ba19ecb4a1430ebc49
[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 $root = [ '.', [ ] ];
12 # each node is [ 'relative subdir name', \@children ]
13
14 sub build_tree () {
15     foreach my $subdir (@ARGV) {
16         my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
17         my $node = $root;
18         foreach my $d (@path) {
19             my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
20             if (!$c) {
21                 $c = [ $d, [ ] ];
22                 push @{ $node->[1] }, $c;
23             }
24             $node = $c;
25         }
26     }
27 }
28
29 sub write_makefile ($$) {
30     my ($dir_prefix,$depth) = @_;
31     start_output_file("${dir_prefix}Makefile");
32     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
33     o <<END;
34 default: all
35 %:
36         $(MAKE) -C $cd ${dir_prefix}$@
37 END
38 }
39
40 sub filter_subdir_mk ($$$$$) {
41     my ($dir_prefix, $dir_suffix, $dir_name, $var_prefix,
42         $targets) = @_;
43
44     my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
45     open I, '<' $in or die "open $in: $!\n";
46     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
47     my $lc_e = qr{[a-z][-+,0-9_a-z]*(?=\W)};
48     my $esclit = '&';
49     my $esc = '\\&';
50
51     while (<I>) {
52         for (;;) {
53             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
54             o $1;
55             if ($2) { o $esclit; next; }
56             s{^$esc}{} or die "$_ ?";
57             if (s{^$esc}{}) { o "$esclit$esclit" }
58             elsif (m{^TARGETS(?:_[0-9a-zA-Z_]+)?(?=\W)}{}) {
59                 my $t = $2 // 'all';
60                 o target_varname($varname_prefix, $t);
61                 $targets->{$t}=1;
62             }
63             elsif (m{^(?=$caps_re)}) { o "${var_prefix}_" }
64             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
65             elsif (s{^_}{}) { o "${var_prefix}_" }
66             elsif (s{^/}{}) { o $dir_prefix }
67             elsif (s{^=_}{}) { o $var_prefix }
68             elsif (s{^=/}{}) { o $dir_name }
69             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
70             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
71             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t]){}}{}) {
72                 my $prefix =
73                     !$1       ? $dir_prefix                     :
74                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
75                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
76                     die;
77                 my $after='';
78                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
79                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
80                 o $_;
81                 $_ = $after;
82             } elsif (s{^![ \t]+}{}) {
83                 o $_;
84                 $_ = '';
85             } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
86                 $esclit = $1;
87                 $esc = $esclit;
88                 $esc =~ s/\W/\\$&/g;
89             } else {
90                 die "bad escape $esclit$_ ";
91             }
92         }
93     }
94 }
95
96 sub target_varname ($$) {
97     my ($var_prefix, $target) = @_;
98     return $vprefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
99 }
100
101 sub process_subtree ($$) {
102     # => list of descendants (in form SUBDIR/)
103     # recursive, children first
104     my ($node, $path);
105
106     my $dir_prefix = join '', map { "$_/" } @$path;
107     my $dir_suffix = join '', map { "/$_" } @$path;
108     my $dir_name = join '/', @$path ? @$path : '.';
109     my $var_prefix = map { "${_}_" } @$path ? @$path : qw(TOP);
110
111     write_makefile($subdir, scalar @$path);
112
113     my %targets = qw(all 1);
114     my @child_subdirs;
115     foreach my $child (@{ $node->[1] }) {
116         my @childpath = (@$path, $child->[0]);
117         push @child_subdirs, join '/', @childpath;
118         $targets{$_}++ foreach
119             process_subtree($child, [  ]);
120     }
121     start_output_file("$subdir/Subdir.mk.tmp");
122
123     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
124                      $var_prefix, \%targets);
125
126     my @targets = sort keys %targets;
127     foreach my $target (@targets) {
128         my $target_varname = target_varname($var_prefix, target);
129         print O <<END;
130 ${dprefix}${target}: \$($target_varname)
131 END
132         if (@child_subdirs) {
133             print O "${dprefix}${target}:";
134             foreach my $child_subdir (@child_subdirs) {
135                 print O " $child_subdir/$target";
136             }
137             print O "\n";
138         }
139     }
140     
141
142     foreach my $descendant (@descendants) {
143         foreach my $target (@$targets) {
144     print O <<END;
145 END
146
147
148 sub process_subdir ($$) {
149     my ($subdir) = @_;
150     my $depth = $subdir eq '.' ? 0 : scalar split m{/+}, $subdir;
151     write_makefile($subdir,$depth);
152     filter_subdir_mk();
153 }
154
155
156     
157 build_tree();
158 process_subtree($root, [ ]);