3 # $(srcdir)/build-aux/subdirmk-setup SUBDIR...
14 die unless $ARGV[0] eq '--srcdir';
17 ($srcdir, @subdirs) = @ARGV;
19 s{/+$}{} foreach @subdirs;
21 our $root = [ '.', [ ] ];
22 # each node is [ 'relative subdir name', \@children ]
25 foreach my $subdir (@subdirs) {
26 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
28 foreach my $d (@path) {
29 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
32 push @{ $node->[1] }, $c;
39 sub target_varname ($$) {
40 my ($var_prefix, $target) = @_;
41 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
47 sub close_any_output_file() {
48 return unless defined $writing_output;
49 O->error and die "error writing $writing_output.tmp: $! (?)\n";
50 close O or die "error closing $writing_output.tmp: $!\n";
51 $writing_output = undef;
55 die unless defined $writing_output;
56 print O @_ or die "error writing $writing_output.tmp: $!\n";
59 sub start_output_file ($) {
60 close_any_output_file();
61 ($writing_output) = @_;
62 die "$writing_output ?" if $output_files{$writing_output}++;
63 my $tmp = "$writing_output.tmp";
64 open O, ">", $tmp or die "create $tmp: $!\n";
65 o "# autogenerated - do not edit\n";
68 sub install_output_files () {
69 close_any_output_file();
70 foreach my $f (sort keys %output_files) {
71 rename "$f.tmp", $f or die "install new $f: $!\n";
75 sub write_makefile ($$) {
76 my ($dir_prefix,$depth) = @_;
77 #print STDERR "write_makefile @_\n";
78 start_output_file("${dir_prefix}Makefile");
79 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
83 \$(MAKE) -C $cd -f subdirs.mk ${dir_prefix}\$@
84 Makefile FORCE-ALWAYS-RUN:
90 sub filter_subdir_mk ($$$$$) {
91 my ($dir_prefix, $dir_suffix, $dir_name,
92 $var_prefix, $targets) = @_;
95 #print STDERR "filter @_\n";
97 my $in = "${dir_prefix}Subdir.mk.in";
98 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
99 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
103 for my $f ($in, "Perdir.mk.in") {
104 open I, '<', $f or die "open $f: $!\n";
107 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
109 if ($2) { o $esclit; next; }
110 s{^$esc}{} or die "$_ ?";
111 if (s{^$esc}{}) { o "$esclit$esclit" }
112 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
114 o target_varname($var_prefix, $t);
117 elsif (m{^(?=$caps_re)}) { o $var_prefix }
118 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
119 elsif (s{^_}{}) { o $var_prefix }
120 elsif (s{^/}{}) { o $dir_prefix }
121 elsif (s{^=_}{}) { o $var_prefix }
122 elsif (s{^=/}{}) { o $dir_name }
123 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
124 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
125 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
128 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
129 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
132 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
133 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
136 } elsif (s{^![ \t]+}{}) {
139 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
144 die "bad escape $esclit$_ ";
148 I->error and die "read $f: $!\n";
152 sub process_subtree ($$);
153 sub process_subtree ($$) {
154 # => list of descendants (in form SUBDIR/)
155 # recursive, children first
156 my ($node, $path) = @_;
159 #print STDERR Dumper(\@_);
161 my $dir_prefix = join '', map { "$_/" } @$path;
162 my $dir_suffix = join '', map { "/$_" } @$path;
163 my $dir_name = join '/', @$path ? @$path : '.';
164 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
166 write_makefile($dir_prefix, scalar @$path);
168 my %targets = qw(all 1);
170 foreach my $child (@{ $node->[1] }) {
171 my @childpath = (@$path, $child->[0]);
172 push @child_subdirs, join '/', @childpath;
173 $targets{$_}++ foreach
174 process_subtree($child, \@childpath);
176 start_output_file("${dir_prefix}Subdir.mk.tmp");
178 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
179 $var_prefix, \%targets);
183 my @targets = sort keys %targets;
184 foreach my $target (@targets) {
185 my $target_varname = target_varname($var_prefix, $target);
186 print O "${dir_prefix}${target}: \$($target_varname)";
187 foreach my $child_subdir (@child_subdirs) {
188 print O " $child_subdir/$target";
197 process_subtree($root, [ ]);
198 start_output_file("subdirs.mk.tmp");
199 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
202 o "MAKEFILES += Subdir.mk\n";
203 foreach my $subdir (@subdirs) {
204 o "MAKEFILES += $subdir/Subdir.mk\n";
206 o "include \$(MAKEFILES)";
211 install_output_files();