3 # $(srcdir)/build-aux/subdirmk-setup SUBDIR...
11 our $root = [ '.', [ ] ];
12 # each node is [ 'relative subdir name', \@children ]
15 foreach my $subdir (@ARGV) {
16 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
18 foreach my $d (@path) {
19 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
22 push @{ $node->[1] }, $c;
29 sub write_makefile ($$) {
30 my ($subdir,$depth) = @_;
31 start_output_file("$subdir/Makefile");
32 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
36 $(MAKE) -C $cd $subdir/$@
40 sub filter_subdir_mk ($) {
42 my $in = "$srcdir/$subdir/Subdir.mk.in";
43 open I, '<' $in or die "open $in: $!\n";
44 my $caps_re = qr{[A-Z][0-9_A-Z]*(?!\w)};
45 my $lc_e = qr{[a-z][-+,0-9_a-z]*(?!\w)};
53 s{^$esc}{} or die "$_ ?";
57 elsif (m{^(?=$caps_re)}) { o "${for_var}_" }
58 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
59 elsif (s{^_}{}) { o "${for_var}_" }
60 elsif (s{^/}{}) { o $dir_prefix }
61 elsif (s{^=_}{}) { o $for_var }
62 elsif (s{^=/}{}) { o $dir_name }
63 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
64 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
67 if (s{^[ \t]+($caps_re)}{}) {
80 } elsif (s{^[_/]}{}) {
85 sub process_subtree ($$) {
86 # => list of descendants (in form SUBDIR/)
87 # recursive, children first
89 my $dprefix = join '', map { "${_}/" } @$path;
90 my $vprefix = join '', map { "${_}_" } @$path;
91 my $subdir = @$path ? (join '/', @$path) : '.';
92 write_makefile($subdir, scalar @$path);
94 my %targets = qw(all 1);
96 foreach my $child (@{ $node->[1] }) {
97 my @childpath = (@$path, $child->[0]);
98 push @child_subdirs, join '/', @childpath;
99 $targets{$_}++ foreach
100 process_subtree($child, [ ]);
102 start_output_file("$subdir/Subdir.mk.tmp");
106 $targets{$_}++ foreach
107 write_subdir($child);
109 my @targets = sort keys %targets;
110 foreach my $target (@targets) {
111 $vsuffix = $target eq 'all' ? '' : "_$target";
113 ${dprefix}${target}: \$(${vprefix}TARGETS${vsuffix})
115 if (@child_subdirs) {
116 print O "${dprefix}${target}:";
117 foreach my $child_subdir (@child_subdirs) {
118 print O " $child_subdir/$target";
125 foreach my $descendant (@descendants) {
126 foreach my $target (@$targets) {
131 sub process_subdir ($$) {
133 my $depth = $subdir eq '.' ? 0 : scalar split m{/+}, $subdir;
134 write_makefile($subdir,$depth);
141 process_subtree($root, [ ]);