3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
5 # generates in each subdirectory
10 # from in each subdirectory
18 print "$0 @ARGV\n" or die $!;
22 while (@ARGV && $ARGV[0] =~ m/^-/) {
28 die "$0: unknown option \`$_'\n";
33 s{/+$}{} foreach @subdirs;
35 our $root = [ '.', [ ] ];
36 # each node is [ 'relative subdir name', \@children ]
39 foreach my $subdir (@subdirs) {
40 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
42 foreach my $d (@path) {
43 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
46 push @{ $node->[1] }, $c;
53 sub target_varname ($$) {
54 my ($var_prefix, $target) = @_;
55 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
61 sub close_any_output_file() {
62 return unless defined $writing_output;
63 O->error and die "error writing $writing_output.tmp: $! (?)\n";
64 close O or die "error closing $writing_output.tmp: $!\n";
65 $writing_output = undef;
69 die unless defined $writing_output;
70 print O @_ or die "error writing $writing_output.tmp: $!\n";
73 sub start_output_file ($) {
74 close_any_output_file();
75 ($writing_output) = @_;
76 die "$writing_output ?" if $output_files{$writing_output}++;
77 my $tmp = "$writing_output.tmp";
78 open O, ">", $tmp or die "create $tmp: $!\n";
79 o "# autogenerated - do not edit\n";
82 sub install_output_files () {
83 close_any_output_file();
84 foreach my $f (sort keys %output_files) {
85 rename "$f.tmp", $f or die "install new $f: $!\n";
89 sub write_makefile ($$) {
90 my ($dir_prefix,$depth) = @_;
91 #print STDERR "write_makefile @_\n";
92 start_output_file("${dir_prefix}Makefile");
93 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
98 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
99 Makefile FORCE-ALWAYS-RUN:
105 sub filter_subdir_mk ($$$$$) {
106 my ($dir_prefix, $dir_suffix, $dir_name,
107 $var_prefix, $targets) = @_;
110 #print STDERR "filter @_\n";
112 my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
113 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
114 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
118 for my $f ($in, "${srcdir}/Perdir.mk.in") {
119 open I, '<', $f or die "open $f: $!\n";
122 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
124 if ($2) { o $esclit; next; }
125 s{^$esc}{} or die "$_ ?";
126 if (s{^$esc}{}) { o "$esclit$esclit" }
127 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
129 o target_varname($var_prefix, $t);
130 $targets->{$t} //= [ ];
132 elsif (m{^(?=$caps_re)}) { o $var_prefix }
133 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
134 elsif (s{^_}{}) { o $var_prefix }
135 elsif (s{^/}{}) { o $dir_prefix }
136 elsif (s{^=_}{}) { o $var_prefix }
137 elsif (s{^=/}{}) { o $dir_name }
138 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
139 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
140 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
143 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
144 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
147 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
148 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
151 } elsif (s{^![ \t]+}{}) {
154 } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
159 die "bad escape $esclit$_ ";
163 I->error and die "read $f: $!\n";
167 sub process_subtree ($$);
168 sub process_subtree ($$) {
169 # => list of descendants (in form SUBDIR/)
170 # recursive, children first
171 my ($node, $path) = @_;
174 #print STDERR Dumper(\@_);
176 my $dir_prefix = join '', map { "$_/" } @$path;
177 my $dir_suffix = join '', map { "/$_" } @$path;
178 my $dir_name = join '/', @$path ? @$path : '.';
179 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
181 write_makefile($dir_prefix, scalar @$path);
183 my %targets = (all => []);
184 foreach my $child (@{ $node->[1] }) {
185 my @childpath = (@$path, $child->[0]);
186 my $child_subdir = join '/', @childpath;
187 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
188 push @{ $targets{$_} }, $child_subdir foreach
189 process_subtree($child, \@childpath);
191 start_output_file("${dir_prefix}Subdir.mk.tmp");
193 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
194 $var_prefix, \%targets);
198 my @targets = sort keys %targets;
199 foreach my $target (@targets) {
200 my $target_varname = target_varname($var_prefix, $target);
201 print O "${dir_prefix}${target}: \$($target_varname)";
202 foreach my $child_subdir (@{ $targets{$target} }) {
203 print O " $child_subdir/$target";
212 process_subtree($root, [ ]);
213 start_output_file("main.mk.tmp");
214 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
217 o "MAKEFILES += Subdir.mk\n";
218 foreach my $subdir (@subdirs) {
219 o "MAKEFILES += $subdir/Subdir.mk\n";
221 o "include \$(MAKEFILES)";
226 install_output_files();