chiark / gitweb /
ea9cb5b8b36230fd2b9e3d536416b8a44708a5bf
[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 o {
61     die unless defined $writing_output;
62     print O @_ or die "error writing $writing_output.tmp: $!\n";
63 }
64
65 sub write_makefile ($$) {
66     my ($dir_prefix,$depth) = @_;
67     start_output_file("${dir_prefix}Makefile");
68     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
69     o sprintf <<'END', $cd, $dir_prefix;
70 default: all
71 %%:
72         $(MAKE) -C %s %s$@
73 END
74 }
75
76 sub filter_subdir_mk ($$$$$) {
77     my ($dir_prefix, $dir_suffix, $dir_name,
78         $var_prefix, $targets) = @_;
79
80     my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
81     open I, '<', $in or die "open $in: $!\n";
82     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
83     my $lc_e = qr{[a-z][-+,0-9_a-z]*(?=\W)};
84     my $esclit = '&';
85     my $esc = '\\&';
86
87     while (<I>) {
88         for (;;) {
89             unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
90             o $1;
91             if ($2) { o $esclit; next; }
92             s{^$esc}{} or die "$_ ?";
93             if (s{^$esc}{}) { o "$esclit$esclit" }
94             elsif (m{^TARGETS(?:_[0-9a-zA-Z_]+)?(?=\W)}{}) {
95                 my $t = $2 // 'all';
96                 o target_varname($varname_prefix, $t);
97                 $targets->{$t}=1;
98             }
99             elsif (m{^(?=$caps_re)}) { o "${var_prefix}_" }
100             elsif (m{^(?=$lc_re)}) { o $dir_prefix }
101             elsif (s{^_}{}) { o "${var_prefix}_" }
102             elsif (s{^/}{}) { o $dir_prefix }
103             elsif (s{^=_}{}) { o $var_prefix }
104             elsif (s{^=/}{}) { o $dir_name }
105             elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
106             elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
107             elsif (s{^(?:[ \t]+([~^]))?(?=[ \t]){}}{}) {
108                 my $prefix =
109                     !$1       ? $dir_prefix                     :
110                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
111                     $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
112                     die;
113                 my $after='';
114                 if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
115                 s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
116                 o $_;
117                 $_ = $after;
118             } elsif (s{^![ \t]+}{}) {
119                 o $_;
120                 $_ = '';
121             } elsif (s{^!(\pPosixWord+|\pPosixPunct+)[ \t]*}{}) {
122                 $esclit = $1;
123                 $esc = $esclit;
124                 $esc =~ s/\W/\\$&/g;
125             } else {
126                 die "bad escape $esclit$_ ";
127             }
128         }
129     }
130 }
131
132 sub process_subtree ($$) {
133     # => list of descendants (in form SUBDIR/)
134     # recursive, children first
135     my ($node, $path);
136
137     my $dir_prefix = join '', map { "$_/" } @$path;
138     my $dir_suffix = join '', map { "/$_" } @$path;
139     my $dir_name = join '/', @$path ? @$path : '.';
140     my $var_prefix = map { "${_}_" } @$path ? @$path : qw(TOP);
141
142     write_makefile($subdir, scalar @$path);
143
144     my %targets = qw(all 1);
145     my @child_subdirs;
146     foreach my $child (@{ $node->[1] }) {
147         my @childpath = (@$path, $child->[0]);
148         push @child_subdirs, join '/', @childpath;
149         $targets{$_}++ foreach
150             process_subtree($child, [  ]);
151     }
152     start_output_file("$subdir/Subdir.mk.tmp");
153
154     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
155                      $var_prefix, \%targets);
156
157     my @targets = sort keys %targets;
158     foreach my $target (@targets) {
159         my $target_varname = target_varname($var_prefix, target);
160         print O <<END;
161 ${dprefix}${target}: \$($target_varname)
162 END
163         if (@child_subdirs) {
164             print O "${dprefix}${target}:";
165             foreach my $child_subdir (@child_subdirs) {
166                 print O " $child_subdir/$target";
167             }
168             print O "\n";
169         }
170     }
171     
172     return @targets;
173 }
174
175 sub process_tree() {
176     process_subtree($root, [ ]);
177     start_output_file("subdirs.mk");
178     o "include Subdir.mk\n";
179     foreach my $subdir (@subdirs) {
180         o "include $subdir/Subdir.mk";
181     }
182 }
183
184 build_tree();
185 process_tree();
186 install_output_files();