chiark / gitweb /
c1866f6bcb29ae5f48858a6c3353e691972046dd
[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 $root = [ '.', [ ] ];
12 # each node is [ 'relative subdir name', \@children ]
13
14 sub build_tree () {
15     foreach my $subdir (@ARGV) {
16         my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
17         my $node = $root;
18         foreach my $d (@path) {
19             my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
20             if (!$c) {
21                 $c = [ $d, [ ] ];
22                 push @{ $node->[1] }, $c;
23             }
24             $node = $c;
25         }
26     }
27 }
28
29
30
31 sub write_makefile ($$) {
32     my ($subdir,$depth) = @_;
33     start_output_file("Makefile");
34     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
35     print O <<END;
36 %:
37         $(MAKE) -C $cd $subdir/$@
38 END
39 }
40
41 sub process_subdir ($$) {
42     my ($subdir) = @_;
43     my $depth = $subdir eq '.' ? 0 : scalar split m{/+}, $subdir;
44     write_makefile($subdir,$depth);
45     filter_subdir_mk();
46 }
47
48 build_tree();