3 # $(srcdir)/build-aux/subdirmk-setup SUBDIR...
7 # and in each subdirectory
16 die unless $ARGV[0] eq '--srcdir';
19 ($srcdir, @subdirs) = @ARGV;
21 s{/+$}{} foreach @subdirs;
23 our $root = [ '.', [ ] ];
24 # each node is [ 'relative subdir name', \@children ]
27 foreach my $subdir (@subdirs) {
28 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
30 foreach my $d (@path) {
31 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
34 push @{ $node->[1] }, $c;
41 sub target_varname ($$) {
42 my ($var_prefix, $target) = @_;
43 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
49 sub close_any_output_file() {
50 return unless defined $writing_output;
51 O->error and die "error writing $writing_output.tmp: $! (?)\n";
52 close O or die "error closing $writing_output.tmp: $!\n";
53 $writing_output = undef;
57 die unless defined $writing_output;
58 print O @_ or die "error writing $writing_output.tmp: $!\n";
61 sub start_output_file ($) {
62 close_any_output_file();
63 ($writing_output) = @_;
64 die "$writing_output ?" if $output_files{$writing_output}++;
65 my $tmp = "$writing_output.tmp";
66 open O, ">", $tmp or die "create $tmp: $!\n";
67 o "# autogenerated - do not edit\n";
70 sub install_output_files () {
71 close_any_output_file();
72 foreach my $f (sort keys %output_files) {
73 rename "$f.tmp", $f or die "install new $f: $!\n";
77 sub write_makefile ($$) {
78 my ($dir_prefix,$depth) = @_;
79 #print STDERR "write_makefile @_\n";
80 start_output_file("${dir_prefix}Makefile");
81 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
86 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
87 Makefile FORCE-ALWAYS-RUN:
93 sub filter_subdir_mk ($$$$$) {
94 my ($dir_prefix, $dir_suffix, $dir_name,
95 $var_prefix, $targets) = @_;
98 #print STDERR "filter @_\n";
100 my $in = "${dir_prefix}Subdir.mk.in";
101 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
102 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
106 for my $f ($in, "Perdir.mk.in") {
107 open I, '<', $f or die "open $f: $!\n";
110 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
112 if ($2) { o $esclit; next; }
113 s{^$esc}{} or die "$_ ?";
114 if (s{^$esc}{}) { o "$esclit$esclit" }
115 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
117 o target_varname($var_prefix, $t);
118 $targets->{$t} //= [ ];
120 elsif (m{^(?=$caps_re)}) { o $var_prefix }
121 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
122 elsif (s{^_}{}) { o $var_prefix }
123 elsif (s{^/}{}) { o $dir_prefix }
124 elsif (s{^=_}{}) { o $var_prefix }
125 elsif (s{^=/}{}) { o $dir_name }
126 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
127 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
128 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
131 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
132 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
135 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
136 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
139 } elsif (s{^![ \t]+}{}) {
142 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
147 die "bad escape $esclit$_ ";
151 I->error and die "read $f: $!\n";
155 sub process_subtree ($$);
156 sub process_subtree ($$) {
157 # => list of descendants (in form SUBDIR/)
158 # recursive, children first
159 my ($node, $path) = @_;
162 #print STDERR Dumper(\@_);
164 my $dir_prefix = join '', map { "$_/" } @$path;
165 my $dir_suffix = join '', map { "/$_" } @$path;
166 my $dir_name = join '/', @$path ? @$path : '.';
167 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
169 write_makefile($dir_prefix, scalar @$path);
171 my %targets = (all => []);
172 foreach my $child (@{ $node->[1] }) {
173 my @childpath = (@$path, $child->[0]);
174 my $child_subdir = join '/', @childpath;
175 push @{ $targets{$_} }, $child_subdir foreach
176 process_subtree($child, \@childpath);
178 start_output_file("${dir_prefix}Subdir.mk.tmp");
180 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
181 $var_prefix, \%targets);
185 my @targets = sort keys %targets;
186 foreach my $target (@targets) {
187 my $target_varname = target_varname($var_prefix, $target);
188 print O "${dir_prefix}${target}: \$($target_varname)";
189 foreach my $child_subdir (@{ $targets{$target} }) {
190 print O " $child_subdir/$target";
199 process_subtree($root, [ ]);
200 start_output_file("main.mk.tmp");
201 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
204 o "MAKEFILES += Subdir.mk\n";
205 foreach my $subdir (@subdirs) {
206 o "MAKEFILES += $subdir/Subdir.mk\n";
208 o "include \$(MAKEFILES)";
213 install_output_files();