chiark / gitweb /
CONFIGURE_ACS
[subdirmk.git] / subdirmk / generate
1 #!/usr/bin/perl -w
2 #
3 # $(srcdir)/build-aux/subdirmk-setup SUBDIR...
4 #
5 # generates
6 #   main.mk.tmp
7 # and in each subdirectory
8 #   Subdir.mk.tmp
9 #   Makefile
10
11 use strict;
12
13 our $srcdir;
14 our @subdirs = @ARGV;
15
16 die unless $ARGV[0] eq '--srcdir';
17 die unless @ARGV>=2;
18 shift @ARGV;
19 ($srcdir, @subdirs) = @ARGV;
20
21 s{/+$}{} foreach @subdirs;
22
23 our $root = [ '.', [ ] ];
24 # each node is [ 'relative subdir name', \@children ]
25
26 sub build_tree () {
27     foreach my $subdir (@subdirs) {
28         my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
29         my $node = $root;
30         foreach my $d (@path) {
31             my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
32             if (!$c) {
33                 $c = [ $d, [ ] ];
34                 push @{ $node->[1] }, $c;
35             }
36             $node = $c;
37         }
38     }
39 }
40
41 sub target_varname ($$) {
42     my ($var_prefix, $target) = @_;
43     return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
44 }
45
46 our $writing_output;
47 our %output_files;
48
49 sub close_any_output_file() {
50     return unless defined $writing_output;
51     O->error and die "error writing $writing_output.tmp: $! (?)\n";
52     close O or die "error closing $writing_output.tmp: $!\n";
53     $writing_output = undef;
54 }
55
56 sub o {
57     die unless defined $writing_output;
58     print O @_ or die "error writing $writing_output.tmp: $!\n";
59 }
60
61 sub start_output_file ($) {
62     close_any_output_file();
63     ($writing_output) = @_;
64     die "$writing_output ?" if $output_files{$writing_output}++;
65     my $tmp = "$writing_output.tmp";
66     open O, ">", $tmp or die "create $tmp: $!\n";
67     o "# autogenerated - do not edit\n";
68 }
69
70 sub install_output_files () {
71     close_any_output_file();
72     foreach my $f (sort keys %output_files) {
73         rename "$f.tmp", $f or die "install new $f: $!\n";
74     }
75 }
76
77 sub write_makefile ($$) {
78     my ($dir_prefix,$depth) = @_;
79     #print STDERR "write_makefile @_\n";
80     start_output_file("${dir_prefix}Makefile");
81     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
82     o <<END;
83 default: all
84         \@: \$@
85 %:      FORCE-ALWAYS-RUN
86         \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
87 Makefile FORCE-ALWAYS-RUN:
88         \@: \$@
89 .SUFFIXES:
90 END
91 }
92
93 sub filter_subdir_mk ($$$$$) {
94     my ($dir_prefix, $dir_suffix, $dir_name,
95         $var_prefix, $targets) = @_;
96
97     #use Data::Dumper;
98     #print STDERR "filter @_\n";
99
100     my $in = "${dir_prefix}Subdir.mk.in";
101     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
102     my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
103     my $esclit = '&';
104     my $esc = '\\&';
105
106     for my $f ($in, "Perdir.mk.in") {
107       open I, '<', $f or die "open $f: $!\n";
108       while (<I>) {
109         for (;;) {
110             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
111             o $1;
112             if ($2) { o $esclit; next; }
113             s{^$esc}{} or die "$_ ?";
114             if (s{^$esc}{}) { o "$esclit$esclit" }
115             elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
116                 my $t = $1 // 'all';
117                 o target_varname($var_prefix, $t);
118                 $targets->{$t} //= [ ];
119             }
120             elsif (m{^(?=$caps_re)}) { o $var_prefix }
121             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
122             elsif (s{^_}{}) { o $var_prefix }
123             elsif (s{^/}{}) { o $dir_prefix }
124             elsif (s{^=_}{}) { o $var_prefix }
125             elsif (s{^=/}{}) { o $dir_name }
126             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
127             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
128             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
129                 my $prefix =
130                     !$1       ? $dir_prefix                     :
131                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
132                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
133                     die;
134                 my $after='';
135                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
136                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
137                 o $_;
138                 $_ = $after;
139             } elsif (s{^![ \t]+}{}) {
140                 o $_;
141                 $_ = '';
142             } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
143                 $esclit = $1;
144                 $esc = $esclit;
145                 $esc =~ s/\W/\\$&/g;
146             } else {
147                 die "bad escape $esclit$_ ";
148             }
149         }
150       }
151       I->error and die "read $f: $!\n";
152     }
153 }
154
155 sub process_subtree ($$);
156 sub process_subtree ($$) {
157     # => list of descendants (in form SUBDIR/)
158     # recursive, children first
159     my ($node, $path) = @_;
160
161     #use Data::Dumper;
162     #print STDERR Dumper(\@_);
163
164     my $dir_prefix = join '', map { "$_/" } @$path;
165     my $dir_suffix = join '', map { "/$_" } @$path;
166     my $dir_name = join '/', @$path ? @$path : '.';
167     my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
168
169     write_makefile($dir_prefix, scalar @$path);
170
171     my %targets = (all => []);
172     foreach my $child (@{ $node->[1] }) {
173         my @childpath = (@$path, $child->[0]);
174         my $child_subdir = join '/', @childpath;
175         push @{ $targets{$_} }, $child_subdir foreach
176             process_subtree($child, \@childpath);
177     }
178     start_output_file("${dir_prefix}Subdir.mk.tmp");
179
180     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
181                      $var_prefix, \%targets);
182
183     o "\n";
184
185     my @targets = sort keys %targets;
186     foreach my $target (@targets) {
187         my $target_varname = target_varname($var_prefix, $target);
188         print O "${dir_prefix}${target}: \$($target_varname)";
189         foreach my $child_subdir (@{ $targets{$target} }) {
190             print O " $child_subdir/$target";
191         }
192         print O "\n";
193     }
194     
195     return @targets;
196 }
197
198 sub process_tree() {
199     process_subtree($root, [ ]);
200     start_output_file("main.mk.tmp");
201     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
202         o "$v=\@$v@\n";
203     }
204     o "MAKEFILES += Subdir.mk\n";
205     foreach my $subdir (@subdirs) {
206         o "MAKEFILES += $subdir/Subdir.mk\n";
207     }
208     o "include \$(MAKEFILES)";
209 }
210
211 build_tree();
212 process_tree();
213 install_output_files();