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 filter_subdir_mk ($$$$$) {
102 my ($dir_prefix, $dir_suffix, $dir_name,
103 $var_prefix, $targets) = @_;
106 #print STDERR "filter @_\n";
108 my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
109 my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
110 my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
114 for my $f ($in, "${srcdir}/Perdir.mk.in") {
115 open I, '<', $f or die "open $f: $!\n";
118 unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
120 if ($2) { o $esclit; next; }
121 s{^$esc}{} or die "$_ ?";
122 if (s{^$esc}{}) { o "$esclit$esclit" }
123 elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
125 o target_varname($var_prefix, $t);
126 $targets->{$t} //= [ ];
128 elsif (m{^(?=$caps_re)}) { o $var_prefix }
129 elsif (m{^(?=$lc_re)}) { o $dir_prefix }
130 elsif (s{^_}{}) { o $var_prefix }
131 elsif (s{^/}{}) { o $dir_prefix }
132 elsif (s{^=_}{}) { o $var_prefix }
133 elsif (s{^=/}{}) { o $dir_name }
134 elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
135 elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
136 elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
139 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
140 $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
143 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
144 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
147 } elsif (s{^![ \t]+}{}) {
150 } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
155 die "bad escape $esclit$_ ";
159 I->error and die "read $f: $!\n";
163 sub process_subtree ($$);
164 sub process_subtree ($$) {
165 # => list of descendants (in form SUBDIR/)
166 # recursive, children first
167 my ($node, $path) = @_;
170 #print STDERR Dumper(\@_);
172 my $dir_prefix = join '', map { "$_/" } @$path;
173 my $dir_suffix = join '', map { "/$_" } @$path;
174 my $dir_name = join '/', @$path ? @$path : '.';
175 my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
177 write_makefile($dir_prefix, scalar @$path);
179 my %targets = (all => []);
180 foreach my $child (@{ $node->[1] }) {
181 my @childpath = (@$path, $child->[0]);
182 my $child_subdir = join '/', @childpath;
183 mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
184 push @{ $targets{$_} }, $child_subdir foreach
185 process_subtree($child, \@childpath);
187 start_output_file("${dir_prefix}Subdir.mk.tmp");
189 filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
190 $var_prefix, \%targets);
194 my @targets = sort keys %targets;
195 foreach my $target (@targets) {
196 my $target_varname = target_varname($var_prefix, $target);
197 print O "${dir_prefix}${target}: \$($target_varname)";
198 foreach my $child_subdir (@{ $targets{$target} }) {
199 print O " $child_subdir/$target";
208 process_subtree($root, [ ]);
209 start_output_file("main.mk.tmp");
210 foreach my $v (qw(top_srcdir abs_top_srcdir)) {
213 o "MAKEFILES += Subdir.mk\n";
214 foreach my $subdir (@subdirs) {
215 o "MAKEFILES += $subdir/Subdir.mk\n";
217 o "include \$(MAKEFILES)";
222 install_output_files();