3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
5 # generates in each subdirectory from in each subdirectory
6 # Subdir.mk.tmp Subdir.sd.mk
8 # and in toplevel and in toplevel
9 # main.mk.tmp Perdir.sd.mk
14 print "$0 @ARGV\n" or die $!;
18 while (@ARGV && $ARGV[0] =~ m/^-/) {
24 die "$0: unknown option \`$_'\n";
29 s{/+$}{} foreach @subdirs;
31 our $root = [ '.', [ ] ];
32 # each node is [ 'relative subdir name', \@children ]
35 foreach my $subdir (@subdirs) {
36 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
38 foreach my $d (@path) {
39 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
42 push @{ $node->[1] }, $c;
49 sub target_varname ($$) {
50 my ($var_prefix, $target) = @_;
51 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
55 our $buffering_output;
59 sub close_any_output_file() {
60 return unless defined $writing_output;
61 O->error and die "error writing $writing_output.tmp: $! (?)\n";
62 close O or die "error closing $writing_output.tmp: $!\n";
63 $writing_output = undef;
67 if (defined $buffering_output) {
68 $buffering_output .= $_ foreach @_;
71 die unless defined $writing_output;
72 print O @_ or die "error writing $writing_output.tmp: $!\n";
75 sub start_output_file ($) {
76 close_any_output_file();
77 ($writing_output) = @_;
78 die "$writing_output ?" if $output_files{$writing_output}++;
79 my $tmp = "$writing_output.tmp";
80 open O, ">", $tmp or die "create $tmp: $!\n";
81 o "# autogenerated - do not edit\n";
84 sub install_output_files () {
85 close_any_output_file();
86 foreach my $f (sort keys %output_files) {
87 rename "$f.tmp", $f or die "install new $f: $!\n";
91 sub write_makefile ($$) {
92 my ($dir_prefix,$depth) = @_;
93 #print STDERR "write_makefile @_\n";
94 start_output_file("${dir_prefix}Makefile");
95 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
100 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
101 Makefile FORCE-ALWAYS-RUN:
107 sub process_input_mk ($$$$$$$$);
108 sub process_input_mk ($$$$$$$$) {
109 my ($dir_prefix, $dir_suffix, $dir_name,
110 $var_prefix, $targets,
111 $f, $esclitr, $enoent_ok) = @_;
113 my $caps_re = qr{[A-Z]};
114 my $lc_re = qr{[a-z]};
123 my $input = new IO::File $f, '<';
125 die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
131 if (s#^\s*$esc\:##) {
132 $buffering_output='';
135 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
137 if ($2) { o $$esclitr; next; }
138 s{^$esc}{} or die "$_ ?";
139 if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
140 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
142 o target_varname($var_prefix, $t);
143 $targets->{$t} //= [ ];
145 elsif (m{^(?=$caps_re)}) { o $var_prefix }
146 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
147 elsif (s{^_}{}) { o $var_prefix }
148 elsif (s{^/}{}) { o $dir_prefix }
149 elsif (s{^=_}{}) { o $var_prefix }
150 elsif (s{^=/}{}) { o $dir_name }
151 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
152 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
153 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
156 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
157 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
160 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
161 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
164 } elsif (s{^![ \t]+}{}) {
167 } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
171 die "bad escape $$esclitr$_ ";
174 if (defined $buffering_output) {
175 $_=$buffering_output;
176 $buffering_output=undef;
177 if (m#^(-?)include\s+(\S+)\s+$#) {
178 my $subf = "$srcdir/$2";
179 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
180 $var_prefix, $targets,
181 $subf, $esclitr, $1);
184 die "unknown directive $_ ";
188 $input->error and die "read $f: $!\n";
189 close $input or die "close $f: $!\n";
192 sub filter_subdir_mk ($$$$$) {
193 my ($dir_prefix, $dir_suffix, $dir_name,
194 $var_prefix, $targets) = @_;
197 #print STDERR "filter @_\n";
202 my ($f, $enoentok) = @_;
203 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
204 $var_prefix, $targets,
205 "${srcdir}/$f", \$esclit, $enoentok);
207 $pi->("${dir_prefix}Subdir.sd.mk", 0);
208 $pi->("Perdir.sd.mk", 1);
211 sub process_subtree ($$);
212 sub process_subtree ($$) {
213 # => list of descendants (in form SUBDIR/)
214 # recursive, children first
215 my ($node, $path) = @_;
218 #print STDERR Dumper(\@_);
220 my $dir_prefix = join '', map { "$_/" } @$path;
221 my $dir_suffix = join '', map { "/$_" } @$path;
222 my $dir_name = join '/', @$path ? @$path : '.';
223 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
225 write_makefile($dir_prefix, scalar @$path);
227 my %targets = (all => []);
228 foreach my $child (@{ $node->[1] }) {
229 my @childpath = (@$path, $child->[0]);
230 my $child_subdir = join '/', @childpath;
231 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
232 push @{ $targets{$_} }, $child_subdir foreach
233 process_subtree($child, \@childpath);
235 start_output_file("${dir_prefix}Subdir.mk.tmp");
237 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
238 $var_prefix, \%targets);
242 my @targets = sort keys %targets;
243 foreach my $target (@targets) {
244 my $target_varname = target_varname($var_prefix, $target);
245 print O "${dir_prefix}${target}:: \$($target_varname)";
246 foreach my $child_subdir (@{ $targets{$target} }) {
247 print O " $child_subdir/$target";
256 process_subtree($root, [ ]);
257 start_output_file("main.mk.tmp");
258 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
261 o "MAKEFILES += Subdir.mk\n";
262 foreach my $subdir (@subdirs) {
263 o "MAKEFILES += $subdir/Subdir.mk\n";
265 foreach my $input (sort keys %input_files) {
266 o "MAKEFILE_TEMPLATES += $input\n";
268 o "include \$(MAKEFILES)";
273 install_output_files();