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 target_varname ($$) {
30 my ($var_prefix, $target) = @_;
31 return $vprefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
34 sub write_makefile ($$) {
35 my ($dir_prefix,$depth) = @_;
36 start_output_file("${dir_prefix}Makefile");
37 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
41 $(MAKE) -C $cd ${dir_prefix}$@
45 sub filter_subdir_mk ($$$$$) {
46 my ($dir_prefix, $dir_suffix, $dir_name,
47 $var_prefix, $targets) = @_;
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)};
58 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
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)}{}) {
65 o target_varname($varname_prefix, $t);
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]){}}{}) {
79 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
80 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
83 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
84 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
87 } elsif (s{^![ \t]+}{}) {
90 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
95 die "bad escape $esclit$_ ";
101 sub process_subtree ($$) {
102 # => list of descendants (in form SUBDIR/)
103 # recursive, children first
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);
111 write_makefile($subdir, scalar @$path);
113 my %targets = qw(all 1);
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, [ ]);
121 start_output_file("$subdir/Subdir.mk.tmp");
123 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
124 $var_prefix, \%targets);
126 my @targets = sort keys %targets;
127 foreach my $target (@targets) {
128 my $target_varname = target_varname($var_prefix, target);
130 ${dprefix}${target}: \$($target_varname)
132 if (@child_subdirs) {
133 print O "${dprefix}${target}:";
134 foreach my $child_subdir (@child_subdirs) {
135 print O " $child_subdir/$target";
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";
155 install_output_files();