3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
5 # generates in each subdirectory from in each subdirectory
6 # Subdir.mk.tmp Subdir.mk.in
8 # and in toplevel and in toplevel
9 # main.mk.tmp Perdir.mk.in
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;
58 sub close_any_output_file() {
59 return unless defined $writing_output;
60 O->error and die "error writing $writing_output.tmp: $! (?)\n";
61 close O or die "error closing $writing_output.tmp: $!\n";
62 $writing_output = undef;
66 if (defined $buffering_output) {
67 $buffering_output .= $_ foreach @_;
70 die unless defined $writing_output;
71 print O @_ or die "error writing $writing_output.tmp: $!\n";
74 sub start_output_file ($) {
75 close_any_output_file();
76 ($writing_output) = @_;
77 die "$writing_output ?" if $output_files{$writing_output}++;
78 my $tmp = "$writing_output.tmp";
79 open O, ">", $tmp or die "create $tmp: $!\n";
80 o "# autogenerated - do not edit\n";
83 sub install_output_files () {
84 close_any_output_file();
85 foreach my $f (sort keys %output_files) {
86 rename "$f.tmp", $f or die "install new $f: $!\n";
90 sub write_makefile ($$) {
91 my ($dir_prefix,$depth) = @_;
92 #print STDERR "write_makefile @_\n";
93 start_output_file("${dir_prefix}Makefile");
94 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
99 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
100 Makefile FORCE-ALWAYS-RUN:
106 sub process_input_mk ($$$$$$$$);
107 sub process_input_mk ($$$$$$$$) {
108 my ($dir_prefix, $dir_suffix, $dir_name,
109 $var_prefix, $targets,
110 $f, $esclitr, $enoent_ok) = @_;
112 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
113 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
122 my $input = new IO::File $f, '<';
124 die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
128 if (s#^\s*$esc\:##) {
129 $buffering_output='';
132 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
134 if ($2) { o $$esclitr; next; }
135 s{^$esc}{} or die "$_ ?";
136 if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
137 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
139 o target_varname($var_prefix, $t);
140 $targets->{$t} //= [ ];
142 elsif (m{^(?=$caps_re)}) { o $var_prefix }
143 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
144 elsif (s{^_}{}) { o $var_prefix }
145 elsif (s{^/}{}) { o $dir_prefix }
146 elsif (s{^=_}{}) { o $var_prefix }
147 elsif (s{^=/}{}) { o $dir_name }
148 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
149 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
150 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
153 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
154 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
157 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
158 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
161 } elsif (s{^![ \t]+}{}) {
164 } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
168 die "bad escape $$esclitr$_ ";
171 if (defined $buffering_output) {
172 $_=$buffering_output;
173 $buffering_output=undef;
174 if (m#^(-?)include\s+(\S+)\s+$#) {
175 my $subf = "$srcdir/$2";
176 o "MAKEFILE_TEMPLATES += $subf\n";
177 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
178 $var_prefix, $targets,
179 $subf, $esclitr, $subf);
182 die "unknown directive $_ ";
186 $input->error and die "read $f: $!\n";
187 close $input or die "close $f: $!\n";
190 sub filter_subdir_mk ($$$$$) {
191 my ($dir_prefix, $dir_suffix, $dir_name,
192 $var_prefix, $targets) = @_;
195 #print STDERR "filter @_\n";
198 for my $f ("${srcdir}/${dir_prefix}Subdir.mk.in",
199 "${srcdir}/Perdir.mk.in") {
200 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
201 $var_prefix, $targets,
206 sub process_subtree ($$);
207 sub process_subtree ($$) {
208 # => list of descendants (in form SUBDIR/)
209 # recursive, children first
210 my ($node, $path) = @_;
213 #print STDERR Dumper(\@_);
215 my $dir_prefix = join '', map { "$_/" } @$path;
216 my $dir_suffix = join '', map { "/$_" } @$path;
217 my $dir_name = join '/', @$path ? @$path : '.';
218 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
220 write_makefile($dir_prefix, scalar @$path);
222 my %targets = (all => []);
223 foreach my $child (@{ $node->[1] }) {
224 my @childpath = (@$path, $child->[0]);
225 my $child_subdir = join '/', @childpath;
226 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
227 push @{ $targets{$_} }, $child_subdir foreach
228 process_subtree($child, \@childpath);
230 start_output_file("${dir_prefix}Subdir.mk.tmp");
232 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
233 $var_prefix, \%targets);
237 my @targets = sort keys %targets;
238 foreach my $target (@targets) {
239 my $target_varname = target_varname($var_prefix, $target);
240 print O "${dir_prefix}${target}: \$($target_varname)";
241 foreach my $child_subdir (@{ $targets{$target} }) {
242 print O " $child_subdir/$target";
251 process_subtree($root, [ ]);
252 start_output_file("main.mk.tmp");
253 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
256 o "MAKEFILES += Subdir.mk\n";
257 foreach my $subdir (@subdirs) {
258 o "MAKEFILES += $subdir/Subdir.mk\n";
260 o "include \$(MAKEFILES)";
265 install_output_files();