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