3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] SUBDIR...
5 # generates in each subdirectory
10 # from in each subdirectory
20 die unless $ARGV[0] eq '--srcdir';
23 ($srcdir, @subdirs) = @ARGV;
25 s{/+$}{} foreach @subdirs;
27 our $root = [ '.', [ ] ];
28 # each node is [ 'relative subdir name', \@children ]
31 foreach my $subdir (@subdirs) {
32 my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
34 foreach my $d (@path) {
35 my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
38 push @{ $node->[1] }, $c;
45 sub target_varname ($$) {
46 my ($var_prefix, $target) = @_;
47 return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
53 sub close_any_output_file() {
54 return unless defined $writing_output;
55 O->error and die "error writing $writing_output.tmp: $! (?)\n";
56 close O or die "error closing $writing_output.tmp: $!\n";
57 $writing_output = undef;
61 die unless defined $writing_output;
62 print O @_ or die "error writing $writing_output.tmp: $!\n";
65 sub start_output_file ($) {
66 close_any_output_file();
67 ($writing_output) = @_;
68 die "$writing_output ?" if $output_files{$writing_output}++;
69 my $tmp = "$writing_output.tmp";
70 open O, ">", $tmp or die "create $tmp: $!\n";
71 o "# autogenerated - do not edit\n";
74 sub install_output_files () {
75 close_any_output_file();
76 foreach my $f (sort keys %output_files) {
77 rename "$f.tmp", $f or die "install new $f: $!\n";
81 sub write_makefile ($$) {
82 my ($dir_prefix,$depth) = @_;
83 #print STDERR "write_makefile @_\n";
84 start_output_file("${dir_prefix}Makefile");
85 my $cd = $depth ? join('/', ('..',) x $depth) : '.';
90 \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
91 Makefile FORCE-ALWAYS-RUN:
97 sub filter_subdir_mk ($$$$$) {
98 my ($dir_prefix, $dir_suffix, $dir_name,
99 $var_prefix, $targets) = @_;
102 #print STDERR "filter @_\n";
104 my $in = "${dir_prefix}Subdir.mk.in";
105 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
106 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
110 for my $f ($in, "Perdir.mk.in") {
111 open I, '<', $f or die "open $f: $!\n";
114 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
116 if ($2) { o $esclit; next; }
117 s{^$esc}{} or die "$_ ?";
118 if (s{^$esc}{}) { o "$esclit$esclit" }
119 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
121 o target_varname($var_prefix, $t);
122 $targets->{$t} //= [ ];
124 elsif (m{^(?=$caps_re)}) { o $var_prefix }
125 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
126 elsif (s{^_}{}) { o $var_prefix }
127 elsif (s{^/}{}) { o $dir_prefix }
128 elsif (s{^=_}{}) { o $var_prefix }
129 elsif (s{^=/}{}) { o $dir_name }
130 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
131 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
132 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
135 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
136 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
139 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
140 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
143 } elsif (s{^![ \t]+}{}) {
146 } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
151 die "bad escape $esclit$_ ";
155 I->error and die "read $f: $!\n";
159 sub process_subtree ($$);
160 sub process_subtree ($$) {
161 # => list of descendants (in form SUBDIR/)
162 # recursive, children first
163 my ($node, $path) = @_;
166 #print STDERR Dumper(\@_);
168 my $dir_prefix = join '', map { "$_/" } @$path;
169 my $dir_suffix = join '', map { "/$_" } @$path;
170 my $dir_name = join '/', @$path ? @$path : '.';
171 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
173 write_makefile($dir_prefix, scalar @$path);
175 my %targets = (all => []);
176 foreach my $child (@{ $node->[1] }) {
177 my @childpath = (@$path, $child->[0]);
178 my $child_subdir = join '/', @childpath;
179 push @{ $targets{$_} }, $child_subdir foreach
180 process_subtree($child, \@childpath);
182 start_output_file("${dir_prefix}Subdir.mk.tmp");
184 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
185 $var_prefix, \%targets);
189 my @targets = sort keys %targets;
190 foreach my $target (@targets) {
191 my $target_varname = target_varname($var_prefix, $target);
192 print O "${dir_prefix}${target}: \$($target_varname)";
193 foreach my $child_subdir (@{ $targets{$target} }) {
194 print O " $child_subdir/$target";
203 process_subtree($root, [ ]);
204 start_output_file("main.mk.tmp");
205 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
208 o "MAKEFILES += Subdir.mk\n";
209 foreach my $subdir (@subdirs) {
210 o "MAKEFILES += $subdir/Subdir.mk\n";
212 o "include \$(MAKEFILES)";
217 install_output_files();