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) : '.';
84 \$(MAKE) -C $cd -f subdirs.mk ${dir_prefix}\$@
85 Makefile FORCE-ALWAYS-RUN:
91 sub filter_subdir_mk ($$$$$) {
92 my ($dir_prefix, $dir_suffix, $dir_name,
93 $var_prefix, $targets) = @_;
96 #print STDERR "filter @_\n";
98 my $in = "${dir_prefix}Subdir.mk.in";
99 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
100 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
104 for my $f ($in, "Perdir.mk.in") {
105 open I, '<', $f or die "open $f: $!\n";
108 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
110 if ($2) { o $esclit; next; }
111 s{^$esc}{} or die "$_ ?";
112 if (s{^$esc}{}) { o "$esclit$esclit" }
113 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
115 o target_varname($var_prefix, $t);
118 elsif (m{^(?=$caps_re)}) { o $var_prefix }
119 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
120 elsif (s{^_}{}) { o $var_prefix }
121 elsif (s{^/}{}) { o $dir_prefix }
122 elsif (s{^=_}{}) { o $var_prefix }
123 elsif (s{^=/}{}) { o $dir_name }
124 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
125 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
126 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
129 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
130 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
133 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
134 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
137 } elsif (s{^![ \t]+}{}) {
140 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
145 die "bad escape $esclit$_ ";
149 I->error and die "read $f: $!\n";
153 sub process_subtree ($$);
154 sub process_subtree ($$) {
155 # => list of descendants (in form SUBDIR/)
156 # recursive, children first
157 my ($node, $path) = @_;
160 #print STDERR Dumper(\@_);
162 my $dir_prefix = join '', map { "$_/" } @$path;
163 my $dir_suffix = join '', map { "/$_" } @$path;
164 my $dir_name = join '/', @$path ? @$path : '.';
165 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
167 write_makefile($dir_prefix, scalar @$path);
169 my %targets = qw(all 1);
171 foreach my $child (@{ $node->[1] }) {
172 my @childpath = (@$path, $child->[0]);
173 push @child_subdirs, join '/', @childpath;
174 $targets{$_}++ foreach
175 process_subtree($child, \@childpath);
177 start_output_file("${dir_prefix}Subdir.mk.tmp");
179 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
180 $var_prefix, \%targets);
184 my @targets = sort keys %targets;
185 foreach my $target (@targets) {
186 my $target_varname = target_varname($var_prefix, $target);
187 print O "${dir_prefix}${target}: \$($target_varname)";
188 foreach my $child_subdir (@child_subdirs) {
189 print O " $child_subdir/$target";
198 process_subtree($root, [ ]);
199 start_output_file("subdirs.mk.tmp");
200 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
203 o "MAKEFILES += Subdir.mk\n";
204 foreach my $subdir (@subdirs) {
205 o "MAKEFILES += $subdir/Subdir.mk\n";
207 o "include \$(MAKEFILES)";
212 install_output_files();