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