3 # $(srcdir)/build-aux/subdirmk-setup SUBDIR...
14 die unless $ARGV[0] eq '--srcdir';
17 ($srcdir, @subdirs) = @ARGV;
19 our $root = [ '.', [ ] ];
20 # each node is [ 'relative subdir name', \@children ]
23 foreach my $subdir (@subdirs) {
24 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
26 foreach my $d (@path) {
27 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
30 push @{ $node->[1] }, $c;
37 sub target_varname ($$) {
38 my ($var_prefix, $target) = @_;
39 return $vprefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
42 sub write_makefile ($$) {
43 my ($dir_prefix,$depth) = @_;
44 start_output_file("${dir_prefix}Makefile");
45 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
49 \$(MAKE) -C $cd ${dir_prefix}$@
53 sub filter_subdir_mk ($$$$$) {
54 my ($dir_prefix, $dir_suffix, $dir_name,
55 $var_prefix, $targets) = @_;
57 my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
58 open I, '<' $in or die "open $in: $!\n";
59 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
60 my $lc_e = qr{[a-z][-+,0-9_a-z]*(?=\W)};
66 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
68 if ($2) { o $esclit; next; }
69 s{^$esc}{} or die "$_ ?";
70 if (s{^$esc}{}) { o "$esclit$esclit" }
71 elsif (m{^TARGETS(?:_[0-9a-zA-Z_]+)?(?=\W)}{}) {
73 o target_varname($varname_prefix, $t);
76 elsif (m{^(?=$caps_re)}) { o "${var_prefix}_" }
77 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
78 elsif (s{^_}{}) { o "${var_prefix}_" }
79 elsif (s{^/}{}) { o $dir_prefix }
80 elsif (s{^=_}{}) { o $var_prefix }
81 elsif (s{^=/}{}) { o $dir_name }
82 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
83 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
84 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t]){}}{}) {
87 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
88 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
91 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
92 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
95 } elsif (s{^![ \t]+}{}) {
98 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
103 die "bad escape $esclit$_ ";
109 sub process_subtree ($$) {
110 # => list of descendants (in form SUBDIR/)
111 # recursive, children first
114 my $dir_prefix = join '', map { "$_/" } @$path;
115 my $dir_suffix = join '', map { "/$_" } @$path;
116 my $dir_name = join '/', @$path ? @$path : '.';
117 my $var_prefix = map { "${_}_" } @$path ? @$path : qw(TOP);
119 write_makefile($subdir, scalar @$path);
121 my %targets = qw(all 1);
123 foreach my $child (@{ $node->[1] }) {
124 my @childpath = (@$path, $child->[0]);
125 push @child_subdirs, join '/', @childpath;
126 $targets{$_}++ foreach
127 process_subtree($child, [ ]);
129 start_output_file("$subdir/Subdir.mk.tmp");
131 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
132 $var_prefix, \%targets);
134 my @targets = sort keys %targets;
135 foreach my $target (@targets) {
136 my $target_varname = target_varname($var_prefix, target);
138 ${dprefix}${target}: \$($target_varname)
140 if (@child_subdirs) {
141 print O "${dprefix}${target}:";
142 foreach my $child_subdir (@child_subdirs) {
143 print O " $child_subdir/$target";
153 process_subtree($root, [ ]);
154 start_output_file("subdirs.mk");
155 o "include Subdir.mk\n";
156 foreach my $subdir (@subdirs) {
157 o "include $subdir/Subdir.mk";
163 install_output_files();