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");
57 sub close_any_output_file() {
58 return unless defined $writing_output;
59 O->error and die "error writing $writing_output.tmp: $! (?)\n";
60 close O or die "error closing $writing_output.tmp: $!\n";
61 $writing_output = undef;
65 die unless defined $writing_output;
66 print O @_ or die "error writing $writing_output.tmp: $!\n";
69 sub start_output_file ($) {
70 close_any_output_file();
71 ($writing_output) = @_;
72 die "$writing_output ?" if $output_files{$writing_output}++;
73 my $tmp = "$writing_output.tmp";
74 open O, ">", $tmp or die "create $tmp: $!\n";
75 o "# autogenerated - do not edit\n";
78 sub install_output_files () {
79 close_any_output_file();
80 foreach my $f (sort keys %output_files) {
81 rename "$f.tmp", $f or die "install new $f: $!\n";
85 sub write_makefile ($$) {
86 my ($dir_prefix,$depth) = @_;
87 #print STDERR "write_makefile @_\n";
88 start_output_file("${dir_prefix}Makefile");
89 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
94 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
95 Makefile FORCE-ALWAYS-RUN:
101 sub process_input_mk ($$$$$$$);
102 sub process_input_mk ($$$$$$$) {
103 my ($dir_prefix, $dir_suffix, $dir_name,
104 $var_prefix, $targets,
107 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
108 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
117 open I, '<', $f or die "open $f: $!\n";
120 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
122 if ($2) { o $$esclitr; next; }
123 s{^$esc}{} or die "$_ ?";
124 if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
125 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
127 o target_varname($var_prefix, $t);
128 $targets->{$t} //= [ ];
130 elsif (m{^(?=$caps_re)}) { o $var_prefix }
131 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
132 elsif (s{^_}{}) { o $var_prefix }
133 elsif (s{^/}{}) { o $dir_prefix }
134 elsif (s{^=_}{}) { o $var_prefix }
135 elsif (s{^=/}{}) { o $dir_name }
136 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
137 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
138 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
141 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
142 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
145 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
146 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
149 } elsif (s{^![ \t]+}{}) {
152 } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
156 die "bad escape $$esclitr$_ ";
160 I->error and die "read $f: $!\n";
163 sub filter_subdir_mk ($$$$$) {
164 my ($dir_prefix, $dir_suffix, $dir_name,
165 $var_prefix, $targets) = @_;
168 #print STDERR "filter @_\n";
171 for my $f ("${srcdir}/${dir_prefix}Subdir.mk.in",
172 "${srcdir}/Perdir.mk.in") {
173 process_input_mk($dir_prefix, $dir_suffix, $dir_name,
174 $var_prefix, $targets,
179 sub process_subtree ($$);
180 sub process_subtree ($$) {
181 # => list of descendants (in form SUBDIR/)
182 # recursive, children first
183 my ($node, $path) = @_;
186 #print STDERR Dumper(\@_);
188 my $dir_prefix = join '', map { "$_/" } @$path;
189 my $dir_suffix = join '', map { "/$_" } @$path;
190 my $dir_name = join '/', @$path ? @$path : '.';
191 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
193 write_makefile($dir_prefix, scalar @$path);
195 my %targets = (all => []);
196 foreach my $child (@{ $node->[1] }) {
197 my @childpath = (@$path, $child->[0]);
198 my $child_subdir = join '/', @childpath;
199 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
200 push @{ $targets{$_} }, $child_subdir foreach
201 process_subtree($child, \@childpath);
203 start_output_file("${dir_prefix}Subdir.mk.tmp");
205 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
206 $var_prefix, \%targets);
210 my @targets = sort keys %targets;
211 foreach my $target (@targets) {
212 my $target_varname = target_varname($var_prefix, $target);
213 print O "${dir_prefix}${target}: \$($target_varname)";
214 foreach my $child_subdir (@{ $targets{$target} }) {
215 print O " $child_subdir/$target";
224 process_subtree($root, [ ]);
225 start_output_file("main.mk.tmp");
226 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
229 o "MAKEFILES += Subdir.mk\n";
230 foreach my $subdir (@subdirs) {
231 o "MAKEFILES += $subdir/Subdir.mk\n";
233 o "include \$(MAKEFILES)";
238 install_output_files();