chiark / gitweb /
README: Fix a mistake in the file reference table
[subdirmk.git] / generate
index 14e18c0899a58c2a729d4e06bc398c4adbe1ba24..9275b25770b818e8346a6c11b0ba532c9e2e4de7 100755 (executable)
--- a/generate
+++ b/generate
@@ -97,14 +97,18 @@ sub write_makefile ($$) {
     #print STDERR "write_makefile @_\n";
     start_output_file("${dir_prefix}Makefile");
     my $cd = $depth ? join('/', ('..',) x $depth) : '.';
+    my $suppress_templates=
+       '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'.
+       ' MAKEFILE_TEMPLATES=)';
     o <<END;
 default: all
-%:     FORCE-ALWAYS-RUN
+\$(filter-out all,\$(MAKECMDGOALS)) all: run-main.mk
        \@:
-Makefile FORCE-ALWAYS-RUN:
-       \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(or \$(MAKECMDGOALS),all))
+subdirmk_targets:=\$(or \$(MAKECMDGOALS),all)
+Makefile run-main.mk:
+       \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(subdirmk_targets))$suppress_templates
 .SUFFIXES:
-.PHONY:        FORCE-ALWAYS-RUN
+.PHONY:        run-main.mk
 END
 }
 
@@ -131,6 +135,12 @@ sub process_input_mk ($$$$$$$$) {
     }
     $input_files{$f}++;
 
+    my %pfxmap = (
+                 ''  => $dir_prefix,
+                 '^' => "\$(top_srcdir)${dir_suffix}/",
+                 '~' => "\$(top_srcdir)/",
+                );
+
     while (<$input>) {
        if (s#^\s*$esc\:##) {
            $buffering_output='';
@@ -147,19 +157,13 @@ sub process_input_mk ($$$$$$$$) {
                $targets->{$t} //= [ ];
            }
            elsif (m{^(?=$caps_re)}) { o $var_prefix }
-           elsif (m{^(?=$lc_re)}) { o $dir_prefix }
+           elsif (s{^([~^]?)(?=$lc_re)}{}) { o $pfxmap{$1} }
            elsif (s{^_}{}) { o $var_prefix }
-           elsif (s{^/}{}) { o $dir_prefix }
            elsif (s{^=_}{}) { o $var_prefix }
+           elsif (s{^([~^]?)/}{}) { o $pfxmap{$1} }
            elsif (s{^=/}{}) { o $dir_name }
-           elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
-           elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
-           elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
-               my $prefix =
-                   !$1       ? $dir_prefix                     :
-                   $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
-                   $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
-                   die;
+           elsif (s{^([~^]?)(?=[ \t])}{}) {
+               my $prefix = $pfxmap{$1} // die;
                my $after='';
                if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
                s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
@@ -254,6 +258,11 @@ sub process_subtree ($$) {
        }
        print O "\n";
     }
+    if (@targets) {
+       print O ".PHONY:";
+       print O " ${dir_prefix}${_}" foreach @targets;
+       print O "\n";
+    }
 
     return @targets;
 }
@@ -264,14 +273,16 @@ sub process_tree() {
     foreach my $v (qw(top_srcdir abs_top_srcdir)) {
        o "$v=\@$v@\n";
     }
-    o "MAKEFILES += Subdir.mk\n";
+    o "SUBDIRMK_MAKEFILES :=\n";
+    o "MAKEFILE_TEMPLATES :=\n";
+    o "SUBDIRMK_MAKEFILES += Subdir.mk\n";
     foreach my $subdir (@subdirs) {
-       o "MAKEFILES += $subdir/Subdir.mk\n";
+       o "SUBDIRMK_MAKEFILES += $subdir/Subdir.mk\n";
     }
     foreach my $input (sort keys %input_files) {
        o "MAKEFILE_TEMPLATES += $input\n";
     }
-    o "include \$(MAKEFILES)";
+    o "include \$(SUBDIRMK_MAKEFILES)";
 }
 
 build_tree();