commit 51e9f66a0bc2db7c527da4e8dac4962d284d6b4e
Author: Dmitry Goncharov <dgoncharov@users.sf.net>
Date:   Fri Nov 25 19:02:58 2022 -0500

    [SV 63417] Fix global .NOTINTERMEDIATE.
    
    Fix .NOTINTERMEDIATE without prerequisites to really cause all targets to be
    treated as not intermediate.
    
    * filedef.h: Declare extern no_intermediates.
    * file.c: Remove static from no_intermediates.
    (remove_intermediates): Check no_intermediates.
    * implicit.c (pattern_search): For a file found by implicit search set
    file->notintermediate if the pattern has notintermediate set or
    no_intermediates is set.
    * remake.c (update_file_1): Avoid setting file->secondary for a pre-existing
    file if file->notintermediate is set or no_intermediates is set. The check for
    no_intermediates here is redundant, but won't hurt. Let it be in case make code
    changes such that its presence matters.
    * ../tests/scripts/targets/NOTINTERMEDIATE: Fix a test.

diff --git a/src/file.c b/src/file.c
index 226af61f..4f6109ab 100644
--- a/src/file.c
+++ b/src/file.c
@@ -63,8 +63,7 @@ static struct hash_table files;
 /* Whether or not .SECONDARY with no prerequisites was given.  */
 static int all_secondary = 0;
 
-/* Whether or not .NOTINTERMEDIATE with no prerequisites was given.  */
-static int no_intermediates = 0;
+int no_intermediates = 0;
 
 /* Access the hash table of all file records.
    lookup_file  given a name, return the struct file * for that name,
@@ -368,7 +367,7 @@ remove_intermediates (int sig)
   int doneany = 0;
 
   /* If there's no way we will ever remove anything anyway, punt early.  */
-  if (question_flag || touch_flag || all_secondary)
+  if (question_flag || touch_flag || all_secondary || no_intermediates)
     return;
 
   if (sig && just_print_flag)
@@ -731,7 +730,7 @@ snap_file (const void *item, void *arg)
   /* If .NOTINTERMEDIATE is set with no deps, mark all targets as
      notintermediate, unless the target is a prereq of .INTERMEDIATE.  */
   if (no_intermediates && !f->intermediate && !f->secondary)
-      f->notintermediate = 1;
+    f->notintermediate = 1;
 
   /* If .EXTRA_PREREQS is set, add them as ignored by automatic variables.  */
   if (f->variables)
diff --git a/src/filedef.h b/src/filedef.h
index ae2f26dd..ab748fca 100644
--- a/src/filedef.h
+++ b/src/filedef.h
@@ -227,3 +227,6 @@ FILE_TIMESTAMP f_mtime (struct file *file, int search);
 
 /* Have we snapped deps yet?  */
 extern int snapped_deps;
+
+/* Whether or not .NOTINTERMEDIATE with no prerequisites was given.  */
+extern int no_intermediates;
diff --git a/src/implicit.c b/src/implicit.c
index a3bd318c..0d7197db 100644
--- a/src/implicit.c
+++ b/src/implicit.c
@@ -1008,7 +1008,7 @@ pattern_search (struct file *file, int archive,
           f->also_make = imf->also_make;
           f->is_target = 1;
           f->is_explicit |= imf->is_explicit || pat->is_explicit;
-          f->notintermediate |= imf->notintermediate;
+          f->notintermediate |= imf->notintermediate || no_intermediates;
           f->intermediate |= !f->is_explicit && !f->notintermediate;
           f->tried_implicit = 1;
 
@@ -1090,7 +1090,7 @@ pattern_search (struct file *file, int archive,
       {
         if (f->precious)
           file->precious = 1;
-        if (f->notintermediate)
+        if (f->notintermediate || no_intermediates)
           file->notintermediate = 1;
       }
   }
@@ -1123,7 +1123,7 @@ pattern_search (struct file *file, int archive,
             {
               if (f->precious)
                 new->file->precious = 1;
-              if (f->notintermediate)
+              if (f->notintermediate || no_intermediates)
                 new->file->notintermediate = 1;
             }
 
diff --git a/src/remake.c b/src/remake.c
index 4ce3d2a3..7ba606cb 100644
--- a/src/remake.c
+++ b/src/remake.c
@@ -871,7 +871,8 @@ update_file_1 (struct file *file, unsigned int depth)
 
       /* Since make has not created this file, make should not remove it,
          even if the file is intermediate. */
-      file->secondary = 1;
+      if (!file->notintermediate && no_intermediates == 0)
+        file->secondary = 1;
 
       notice_finished_file (file);
 
