chiark / gitweb /
3d8ef7d8221c7fe52f2f41bfd23457398dfd4a83
[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 target_varname ($$) {
30     my ($var_prefix, $target) = @_;
31     return $vprefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
32 }
33
34 sub write_makefile ($$) {
35     my ($dir_prefix,$depth) = @_;
36     start_output_file("${dir_prefix}Makefile");
37     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
38     o <<END;
39 default: all
40 %:
41         $(MAKE) -C $cd ${dir_prefix}$@
42 END
43 }
44
45 sub filter_subdir_mk ($$$$$) {
46     my ($dir_prefix, $dir_suffix, $dir_name,
47         $var_prefix, $targets) = @_;
48
49     my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
50     open I, '<' $in or die "open $in: $!\n";
51     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
52     my $lc_e = qr{[a-z][-+,0-9_a-z]*(?=\W)};
53     my $esclit = '&';
54     my $esc = '\\&';
55
56     while (<I>) {
57         for (;;) {
58             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
59             o $1;
60             if ($2) { o $esclit; next; }
61             s{^$esc}{} or die "$_ ?";
62             if (s{^$esc}{}) { o "$esclit$esclit" }
63             elsif (m{^TARGETS(?:_[0-9a-zA-Z_]+)?(?=\W)}{}) {
64                 my $t = $2 // 'all';
65                 o target_varname($varname_prefix, $t);
66                 $targets->{$t}=1;
67             }
68             elsif (m{^(?=$caps_re)}) { o "${var_prefix}_" }
69             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
70             elsif (s{^_}{}) { o "${var_prefix}_" }
71             elsif (s{^/}{}) { o $dir_prefix }
72             elsif (s{^=_}{}) { o $var_prefix }
73             elsif (s{^=/}{}) { o $dir_name }
74             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
75             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
76             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t]){}}{}) {
77                 my $prefix =
78                     !$1       ? $dir_prefix                     :
79                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
80                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
81                     die;
82                 my $after='';
83                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
84                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
85                 o $_;
86                 $_ = $after;
87             } elsif (s{^![ \t]+}{}) {
88                 o $_;
89                 $_ = '';
90             } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
91                 $esclit = $1;
92                 $esc = $esclit;
93                 $esc =~ s/\W/\\$&/g;
94             } else {
95                 die "bad escape $esclit$_ ";
96             }
97         }
98     }
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     return @targets;
142 }
143
144 sub process_tree() {
145     process_subtree($root, [ ]);
146     start_output_file("subdirs.mk");
147     o "include Subdir.mk\n";
148     foreach my $subdir (@ARGV) {
149         o "include $subdir/Subdir.mk";
150     }
151 }
152
153 build_tree();
154 process_tree();
155 install_output_files();