chiark / gitweb /
$enoent_ok and GEN filehandle (nfc)
[subdirmk.git] / subdirmk / generate
index 110f8f1a6e8fcba53ccefe3ef7838a805d8d0c55..7b8727b1988c254968889387fb5c402a8f7965b6 100755 (executable)
@@ -52,6 +52,7 @@ sub target_varname ($$) {
 }
 
 our $writing_output;
+our $buffering_output;
 our %output_files;
 
 sub close_any_output_file() {
@@ -62,6 +63,10 @@ sub close_any_output_file() {
 }
 
 sub o {
+    if (defined $buffering_output) {
+       $buffering_output .= $_ foreach @_;
+       return;
+    }
     die unless defined $writing_output;
     print O @_ or die "error writing $writing_output.tmp: $!\n";
 }
@@ -98,11 +103,11 @@ Makefile FORCE-ALWAYS-RUN:
 END
 }
 
-sub process_input_mk ($$$$$$$);
-sub process_input_mk ($$$$$$$) {
+sub process_input_mk ($$$$$$$$);
+sub process_input_mk ($$$$$$$$) {
     my ($dir_prefix, $dir_suffix, $dir_name,
        $var_prefix, $targets,
-       $f, $esclitr) = @_;
+       $f, $esclitr, $enoent_ok) = @_;
 
     my $caps_re = qr{[A-Z][0-9_A-Z]*(?=\W)};
     my $lc_re = qr{[a-z][-+,0-9_a-z]*(?=\W)};
@@ -114,8 +119,12 @@ sub process_input_mk ($$$$$$$) {
     };
     $set_esc->();
 
-    open I, '<', $f or die "open $f: $!\n";
-    while (<I>) {
+    my $input = new IO::File $f, '<';
+    if (!$input) {
+       die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
+       return;
+    }
+    while (<$input>) {
        for (;;) {
            unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
            o $1;
@@ -157,7 +166,8 @@ sub process_input_mk ($$$$$$$) {
            }
        }
     }
-    I->error and die "read $f: $!\n";
+    $input->error and die "read $f: $!\n";
+    close $input or die "close $f: $!\n";
 }
 
 sub filter_subdir_mk ($$$$$) {
@@ -172,7 +182,7 @@ sub filter_subdir_mk ($$$$$) {
               "${srcdir}/Perdir.mk.in") {
        process_input_mk($dir_prefix, $dir_suffix, $dir_name,
                         $var_prefix, $targets,
-                        $f, \$esclit);
+                        $f, \$esclit, 0);
     }
 }