chiark / gitweb /
wip
[subdirmk.git] / build-aux / subdirmk-setup
index 59430c7cc987977d9f866ae5f0b491903782f4de..603dab6f6cdbeebd9ed3270ebf99de34d1300f97 100755 (executable)
@@ -16,6 +16,8 @@ die unless @ARGV>=2;
 shift @ARGV;
 ($srcdir, @subdirs) = @ARGV;
 
+s{/+$}{} foreach @subdirs;
+
 our $root = [ '.', [ ] ];
 # each node is [ 'relative subdir name', \@children ]
 
@@ -36,17 +38,52 @@ sub build_tree () {
 
 sub target_varname ($$) {
     my ($var_prefix, $target) = @_;
-    return $vprefix.'TARGETS'.($target eq 'all' ? '' : "_$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 o {
+    die unless defined $writing_output;
+    print O @_ or die "error writing $writing_output.tmp: $!\n";
+}
+
+sub start_output_file ($) {
+    close_any_output_file();
+    ($writing_output) = @_;
+    die "$writing_output ?" if $output_files{$writing_output}++;
+    my $tmp = "$writing_output.tmp";
+    open O, ">", $tmp or die "create $tmp: $!\n";
+    o "# autogenerated - do not edit\n";
+}
+
+sub install_output_files () {
+    close_any_output_file();
+    foreach my $f (sort keys %output_files) {
+       rename "$f.tmp", $f or die "install new $f: $!\n";
+    }
 }
 
 sub write_makefile ($$) {
     my ($dir_prefix,$depth) = @_;
+    #print STDERR "write_makefile @_\n";
     start_output_file("${dir_prefix}Makefile");
     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
     o <<END;
 default: all
-%:
-       \$(MAKE) -C $cd ${dir_prefix}$@
+%:     FORCE-ALWAYS-RUN
+       \$(MAKE) -C $cd -f subdirs.mk ${dir_prefix}\$@
+Makefile FORCE-ALWAYS-RUN:
+       : \$@
+.SUFFIXES:
 END
 }
 
@@ -54,34 +91,38 @@ sub filter_subdir_mk ($$$$$) {
     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";
+    #use Data::Dumper;
+    #print STDERR "filter @_\n";
+
+    my $in = "${dir_prefix}Subdir.mk.in";
     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
-    my $lc_e = qr{[a-z][-+,0-9_a-z]*(?=\W)};
+    my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
     my $esclit = '&';
     my $esc = '\\&';
 
-    while (<I>) {
+    for my $f ($in, "Perdir.mk.in") {
+      open I, '<', $f or die "open $f: $!\n";
+      while (<I>) {
        for (;;) {
            unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
            o $1;
            if ($2) { o $esclit; next; }
            s{^$esc}{} or die "$_ ?";
            if (s{^$esc}{}) { o "$esclit$esclit" }
-           elsif (m{^TARGETS(?:_[0-9a-zA-Z_]+)?(?=\W)}{}) {
-               my $t = $2 // 'all';
-               o target_varname($varname_prefix, $t);
+           elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
+               my $t = $1 // 'all';
+               o target_varname($var_prefix, $t);
                $targets->{$t}=1;
            }
-           elsif (m{^(?=$caps_re)}) { o "${var_prefix}_" }
+           elsif (m{^(?=$caps_re)}) { o $var_prefix }
            elsif (m{^(?=$lc_re)}) { o $dir_prefix }
-           elsif (s{^_}{}) { o "${var_prefix}_" }
+           elsif (s{^_}{}) { o $var_prefix }
            elsif (s{^/}{}) { o $dir_prefix }
            elsif (s{^=_}{}) { o $var_prefix }
            elsif (s{^=/}{}) { o $dir_name }
            elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
            elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
-           elsif (s{^(?:[ \t]+([~^]))?(?=[ \t]){}}{}) {
+           elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
                my $prefix =
                    !$1       ? $dir_prefix                     :
                    $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
@@ -103,20 +144,26 @@ sub filter_subdir_mk ($$$$$) {
                die "bad escape $esclit$_ ";
            }
        }
+      }
+      I->error and die "read $f: $!\n";
     }
 }
 
+sub process_subtree ($$);
 sub process_subtree ($$) {
     # => list of descendants (in form SUBDIR/)
     # recursive, children first
-    my ($node, $path);
+    my ($node, $path) = @_;
+
+    #use Data::Dumper;
+    #print STDERR Dumper(\@_);
 
     my $dir_prefix = join '', map { "$_/" } @$path;
     my $dir_suffix = join '', map { "/$_" } @$path;
     my $dir_name = join '/', @$path ? @$path : '.';
-    my $var_prefix = map { "${_}_" } @$path ? @$path : qw(TOP);
+    my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
 
-    write_makefile($subdir, scalar @$path);
+    write_makefile($dir_prefix, scalar @$path);
 
     my %targets = qw(all 1);
     my @child_subdirs;
@@ -124,26 +171,23 @@ sub process_subtree ($$) {
        my @childpath = (@$path, $child->[0]);
        push @child_subdirs, join '/', @childpath;
        $targets{$_}++ foreach
-           process_subtree($child, [  ]);
+           process_subtree($child, \@childpath);
     }
-    start_output_file("$subdir/Subdir.mk.tmp");
+    start_output_file("${dir_prefix}Subdir.mk.tmp");
 
     filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
                     $var_prefix, \%targets);
 
+    o "\n";
+
     my @targets = sort keys %targets;
     foreach my $target (@targets) {
-       my $target_varname = target_varname($var_prefix, target);
-       print O <<END;
-${dprefix}${target}: \$($target_varname)
-END
-       if (@child_subdirs) {
-           print O "${dprefix}${target}:";
-           foreach my $child_subdir (@child_subdirs) {
-               print O " $child_subdir/$target";
-           }
-           print O "\n";
+       my $target_varname = target_varname($var_prefix, $target);
+       print O "${dir_prefix}${target}: \$($target_varname)";
+       foreach my $child_subdir (@child_subdirs) {
+           print O " $child_subdir/$target";
        }
+       print O "\n";
     }
     
     return @targets;
@@ -151,11 +195,15 @@ END
 
 sub process_tree() {
     process_subtree($root, [ ]);
-    start_output_file("subdirs.mk");
-    o "include Subdir.mk\n";
+    start_output_file("subdirs.mk.tmp");
+    foreach my $v (qw(top_srcdir abs_top_srcdir)) {
+       o "$v=\@$v@\n";
+    }
+    o "MAKEFILES += Subdir.mk\n";
     foreach my $subdir (@subdirs) {
-       o "include $subdir/Subdir.mk";
+       o "MAKEFILES += $subdir/Subdir.mk\n";
     }
+    o "include \$(MAKEFILES)";
 }
 
 build_tree();