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 $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
45 sub close_any_output_file() {
46 return unless defined $writing_output;
47 O->error and die "error writing $writing_output.tmp: $! (?)\n";
48 close O or die "error closing $writing_output.tmp: $!\n";
49 $writing_output = undef;
53 die unless defined $writing_output;
54 print O @_ or die "error writing $writing_output.tmp: $!\n";
57 sub start_output_file ($) {
58 close_any_output_file();
59 ($writing_output) = @_;
60 die "$writing_output ?" if $output_files{$writing_output}++;
61 my $tmp = "$writing_output.tmp";
62 open O, ">", $tmp or die "create $tmp: $!\n";
63 o "# autogenerated - do not edit\n";
66 sub install_output_files () {
67 close_any_output_file();
68 foreach my $f (sort keys %output_files) {
69 rename "$f.tmp", $f or die "install new $f: $!\n";
73 sub write_makefile ($$) {
74 my ($dir_prefix,$depth) = @_;
75 #print STDERR "write_makefile @_\n";
76 start_output_file("${dir_prefix}Makefile");
77 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
81 \$(MAKE) -C $cd -f subdirs.mk ${dir_prefix}\$@
85 sub filter_subdir_mk ($$$$$) {
86 my ($dir_prefix, $dir_suffix, $dir_name,
87 $var_prefix, $targets) = @_;
90 #print STDERR "filter @_\n";
92 my $in = "${dir_prefix}Subdir.mk.in";
93 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
94 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
98 for my $f ($in, "Perdir.mk.in") {
99 open I, '<', $f or die "open $f: $!\n";
102 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
104 if ($2) { o $esclit; next; }
105 s{^$esc}{} or die "$_ ?";
106 if (s{^$esc}{}) { o "$esclit$esclit" }
107 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
109 o target_varname($var_prefix, $t);
112 elsif (m{^(?=$caps_re)}) { o $var_prefix }
113 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
114 elsif (s{^_}{}) { o $var_prefix }
115 elsif (s{^/}{}) { o $dir_prefix }
116 elsif (s{^=_}{}) { o $var_prefix }
117 elsif (s{^=/}{}) { o $dir_name }
118 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
119 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
120 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
123 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
124 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
127 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
128 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
131 } elsif (s{^![ \t]+}{}) {
134 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
139 die "bad escape $esclit$_ ";
143 I->error and die "read $f: $!\n";
147 sub process_subtree ($$);
148 sub process_subtree ($$) {
149 # => list of descendants (in form SUBDIR/)
150 # recursive, children first
151 my ($node, $path) = @_;
154 #print STDERR Dumper(\@_);
156 my $dir_prefix = join '', map { "$_/" } @$path;
157 my $dir_suffix = join '', map { "/$_" } @$path;
158 my $dir_name = join '/', @$path ? @$path : '.';
159 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
161 write_makefile($dir_prefix, scalar @$path);
163 my %targets = qw(all 1);
165 foreach my $child (@{ $node->[1] }) {
166 my @childpath = (@$path, $child->[0]);
167 push @child_subdirs, join '/', @childpath;
168 $targets{$_}++ foreach
169 process_subtree($child, \@childpath);
171 start_output_file("${dir_prefix}Subdir.mk.tmp");
173 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
174 $var_prefix, \%targets);
178 my @targets = sort keys %targets;
179 foreach my $target (@targets) {
180 my $target_varname = target_varname($var_prefix, $target);
181 print O "${dir_prefix}${target}: \$($target_varname)";
182 foreach my $child_subdir (@child_subdirs) {
183 print O " $child_subdir/$target";
192 process_subtree($root, [ ]);
193 start_output_file("subdirs.mk.tmp");
194 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
197 o "include Subdir.mk\n";
198 foreach my $subdir (@subdirs) {
199 o "include $subdir/Subdir.mk\n";
205 install_output_files();