3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
5 # generates in each subdirectory
10 # from in each subdirectory
17 print "$0 @ARGV\n" or die $!;
21 while (@ARGV && $ARGV[0] =~ m/^-/) {
27 die "$0: unknown option \`$_'\n";
32 s{/+$}{} foreach @subdirs;
34 our $root = [ '.', [ ] ];
35 # each node is [ 'relative subdir name', \@children ]
38 foreach my $subdir (@subdirs) {
39 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
41 foreach my $d (@path) {
42 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
45 push @{ $node->[1] }, $c;
52 sub target_varname ($$) {
53 my ($var_prefix, $target) = @_;
54 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
60 sub close_any_output_file() {
61 return unless defined $writing_output;
62 O->error and die "error writing $writing_output.tmp: $! (?)\n";
63 close O or die "error closing $writing_output.tmp: $!\n";
64 $writing_output = undef;
68 die unless defined $writing_output;
69 print O @_ or die "error writing $writing_output.tmp: $!\n";
72 sub start_output_file ($) {
73 close_any_output_file();
74 ($writing_output) = @_;
75 die "$writing_output ?" if $output_files{$writing_output}++;
76 my $tmp = "$writing_output.tmp";
77 open O, ">", $tmp or die "create $tmp: $!\n";
78 o "# autogenerated - do not edit\n";
81 sub install_output_files () {
82 close_any_output_file();
83 foreach my $f (sort keys %output_files) {
84 rename "$f.tmp", $f or die "install new $f: $!\n";
88 sub write_makefile ($$) {
89 my ($dir_prefix,$depth) = @_;
90 #print STDERR "write_makefile @_\n";
91 start_output_file("${dir_prefix}Makefile");
92 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
97 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
98 Makefile FORCE-ALWAYS-RUN:
104 sub filter_subdir_mk ($$$$$) {
105 my ($dir_prefix, $dir_suffix, $dir_name,
106 $var_prefix, $targets) = @_;
109 #print STDERR "filter @_\n";
111 my $in = "${dir_prefix}Subdir.mk.in";
112 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
113 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
117 for my $f ($in, "Perdir.mk.in") {
118 open I, '<', $f or die "open $f: $!\n";
121 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
123 if ($2) { o $esclit; next; }
124 s{^$esc}{} or die "$_ ?";
125 if (s{^$esc}{}) { o "$esclit$esclit" }
126 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
128 o target_varname($var_prefix, $t);
129 $targets->{$t} //= [ ];
131 elsif (m{^(?=$caps_re)}) { o $var_prefix }
132 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
133 elsif (s{^_}{}) { o $var_prefix }
134 elsif (s{^/}{}) { o $dir_prefix }
135 elsif (s{^=_}{}) { o $var_prefix }
136 elsif (s{^=/}{}) { o $dir_name }
137 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
138 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
139 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
142 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
143 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
146 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
147 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
150 } elsif (s{^![ \t]+}{}) {
153 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
158 die "bad escape $esclit$_ ";
162 I->error and die "read $f: $!\n";
166 sub process_subtree ($$);
167 sub process_subtree ($$) {
168 # => list of descendants (in form SUBDIR/)
169 # recursive, children first
170 my ($node, $path) = @_;
173 #print STDERR Dumper(\@_);
175 my $dir_prefix = join '', map { "$_/" } @$path;
176 my $dir_suffix = join '', map { "/$_" } @$path;
177 my $dir_name = join '/', @$path ? @$path : '.';
178 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
180 write_makefile($dir_prefix, scalar @$path);
182 my %targets = (all => []);
183 foreach my $child (@{ $node->[1] }) {
184 my @childpath = (@$path, $child->[0]);
185 my $child_subdir = join '/', @childpath;
186 push @{ $targets{$_} }, $child_subdir foreach
187 process_subtree($child, \@childpath);
189 start_output_file("${dir_prefix}Subdir.mk.tmp");
191 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
192 $var_prefix, \%targets);
196 my @targets = sort keys %targets;
197 foreach my $target (@targets) {
198 my $target_varname = target_varname($var_prefix, $target);
199 print O "${dir_prefix}${target}: \$($target_varname)";
200 foreach my $child_subdir (@{ $targets{$target} }) {
201 print O " $child_subdir/$target";
210 process_subtree($root, [ ]);
211 start_output_file("main.mk.tmp");
212 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
215 o "MAKEFILES += Subdir.mk\n";
216 foreach my $subdir (@subdirs) {
217 o "MAKEFILES += $subdir/Subdir.mk\n";
219 o "include \$(MAKEFILES)";
224 install_output_files();