chiark / gitweb /
WIP
[subdirmk.git] / build-aux / subdirmk-setup
old mode 100644 (file)
new mode 100755 (executable)
index f721621..ea9cb5b
@@ -8,11 +8,19 @@
 
 use strict;
 
+our $srcdir;
+our @subdirs = @ARGV;
+
+die unless $ARGV[0] eq '--srcdir';
+die unless @ARGV>=2;
+shift @ARGV;
+($srcdir, @subdirs) = @ARGV;
+
 our $root = [ '.', [ ] ];
 # each node is [ 'relative subdir name', \@children ]
 
 sub build_tree () {
-    foreach my $subdir (@ARGV) {
+    foreach my $subdir (@subdirs) {
        my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
        my $node = $root;
        foreach my $d (@path) {
@@ -26,23 +34,51 @@ sub build_tree () {
     }
 }
 
+sub target_varname ($$) {
+    my ($var_prefix, $target) = @_;
+    return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
+}
+
+our $writing_output;
+our %output_files;
+
+sub close_any_output_file() {
+    return unless defined $writing_output;
+    O->error and die "error writing $writing_output.tmp: $! (?)\n";
+    close O or die "error closing $writing_output.tmp: $!\n";
+    $writing_output = undef;
+}
+
+sub start_output_file ($) {
+    close_any_output_file();
+    ($writing_output) = @_;
+    die if $output_files{$writing_output}++;
+    my $tmp = "$writing_output.tmp";
+    open O, ">", $tmp or die "create $tmp: $!\n";
+}
+
+sub o {
+    die unless defined $writing_output;
+    print O @_ or die "error writing $writing_output.tmp: $!\n";
+}
+
 sub write_makefile ($$) {
     my ($dir_prefix,$depth) = @_;
     start_output_file("${dir_prefix}Makefile");
     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
-    o <<END;
+    o sprintf <<'END', $cd, $dir_prefix;
 default: all
-%:
-       $(MAKE) -C $cd ${dir_prefix}$@
+%%:
+       $(MAKE) -C %s %s$@
 END
 }
 
 sub filter_subdir_mk ($$$$$) {
-    my ($dir_prefix, $dir_suffix, $dir_name, $var_prefix,
-       $targets) = @_;
+    my ($dir_prefix, $dir_suffix, $dir_name,
+       $var_prefix, $targets) = @_;
 
     my $in = "${srcdir}/${dir_prefix}Subdir.mk.in";
-    open I, '<' $in or die "open $in: $!\n";
+    open I, '<', $in or die "open $in: $!\n";
     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
     my $lc_e = qr{[a-z][-+,0-9_a-z]*(?=\W)};
     my $esclit = '&';
@@ -93,11 +129,6 @@ sub filter_subdir_mk ($$$$$) {
     }
 }
 
-sub target_varname ($$) {
-    my ($var_prefix, $target) = @_;
-    return $vprefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
-}
-
 sub process_subtree ($$) {
     # => list of descendants (in form SUBDIR/)
     # recursive, children first
@@ -138,21 +169,18 @@ END
        }
     }
     
-
-    foreach my $descendant (@descendants) {
-       foreach my $target (@$targets) {
-    print O <<END;
-END
-
-
-sub process_subdir ($$) {
-    my ($subdir) = @_;
-    my $depth = $subdir eq '.' ? 0 : scalar split m{/+}, $subdir;
-    write_makefile($subdir,$depth);
-    filter_subdir_mk();
+    return @targets;
 }
 
+sub process_tree() {
+    process_subtree($root, [ ]);
+    start_output_file("subdirs.mk");
+    o "include Subdir.mk\n";
+    foreach my $subdir (@subdirs) {
+       o "include $subdir/Subdir.mk";
+    }
+}
 
-    
 build_tree();
-process_subtree($root, [ ]);
+process_tree();
+install_output_files();