chiark / gitweb /
fix out of tree builds
[subdirmk.git] / subdirmk / generate
1 #!/usr/bin/perl -w
2 #
3 # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
4 #
5 # generates in each subdirectory
6 #   Subdir.mk.tmp
7 #   Makefile
8 # and in toplevel
9 #   main.mk.tmp
10 # from in each subdirectory
11 #   Subdir.mk.in
12 # and at toplevel
13 #   Perdir.mk.in
14
15 use strict;
16 use POSIX;
17
18 print "$0 @ARGV\n" or die $!;
19
20 our $srcdir='.';
21
22 while (@ARGV && $ARGV[0] =~ m/^-/) {
23     $_ = shift @ARGV;
24     last if $_ eq '--';
25     if (s/^--srcdir=//) {
26         $srcdir=$';
27     } else {
28         die "$0: unknown option \`$_'\n";
29     }
30 }
31 our @subdirs = @ARGV;
32
33 s{/+$}{} foreach @subdirs;
34
35 our $root = [ '.', [ ] ];
36 # each node is [ 'relative subdir name', \@children ]
37
38 sub build_tree () {
39     foreach my $subdir (@subdirs) {
40         my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
41         my $node = $root;
42         foreach my $d (@path) {
43             my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
44             if (!$c) {
45                 $c = [ $d, [ ] ];
46                 push @{ $node->[1] }, $c;
47             }
48             $node = $c;
49         }
50     }
51 }
52
53 sub target_varname ($$) {
54     my ($var_prefix, $target) = @_;
55     return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
56 }
57
58 our $writing_output;
59 our %output_files;
60
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;
66 }
67
68 sub o {
69     die unless defined $writing_output;
70     print O @_ or die "error writing $writing_output.tmp: $!\n";
71 }
72
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";
80 }
81
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";
86     }
87 }
88
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) : '.';
94     o <<END;
95 default: all
96         \@: \$@
97 %:      FORCE-ALWAYS-RUN
98         \$(MAKE) -C $cd -f main.mk ${dir_prefix}\$@
99 Makefile FORCE-ALWAYS-RUN:
100         \@: \$@
101 .SUFFIXES:
102 END
103 }
104
105 sub filter_subdir_mk ($$$$$) {
106     my ($dir_prefix, $dir_suffix, $dir_name,
107         $var_prefix, $targets) = @_;
108
109     #use Data::Dumper;
110     #print STDERR "filter @_\n";
111
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)};
115     my $esclit = '&';
116     my $esc = '\\&';
117
118     for my $f ($in, "${srcdir}/Perdir.mk.in") {
119       open I, '<', $f or die "open $f: $!\n";
120       while (<I>) {
121         for (;;) {
122             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
123             o $1;
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)}{}) {
128                 my $t = $1 // 'all';
129                 o target_varname($var_prefix, $t);
130                 $targets->{$t} //= [ ];
131             }
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])}{}) {
141                 my $prefix =
142                     !$1       ? $dir_prefix                     :
143                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
144                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
145                     die;
146                 my $after='';
147                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
148                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
149                 o $_;
150                 $_ = $after;
151             } elsif (s{^![ \t]+}{}) {
152                 o $_;
153                 $_ = '';
154             } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
155                 $esclit = $1;
156                 $esc = $esclit;
157                 $esc =~ s/\W/\\$&/g;
158             } else {
159                 die "bad escape $esclit$_ ";
160             }
161         }
162       }
163       I->error and die "read $f: $!\n";
164     }
165 }
166
167 sub process_subtree ($$);
168 sub process_subtree ($$) {
169     # => list of descendants (in form SUBDIR/)
170     # recursive, children first
171     my ($node, $path) = @_;
172
173     #use Data::Dumper;
174     #print STDERR Dumper(\@_);
175
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);
180
181     write_makefile($dir_prefix, scalar @$path);
182
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);
190     }
191     start_output_file("${dir_prefix}Subdir.mk.tmp");
192
193     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
194                      $var_prefix, \%targets);
195
196     o "\n";
197
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";
204         }
205         print O "\n";
206     }
207     
208     return @targets;
209 }
210
211 sub process_tree() {
212     process_subtree($root, [ ]);
213     start_output_file("main.mk.tmp");
214     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
215         o "$v=\@$v@\n";
216     }
217     o "MAKEFILES += Subdir.mk\n";
218     foreach my $subdir (@subdirs) {
219         o "MAKEFILES += $subdir/Subdir.mk\n";
220     }
221     o "include \$(MAKEFILES)";
222 }
223
224 build_tree();
225 process_tree();
226 install_output_files();