commit 11ead0e6775965c66501756e481cd802efbde3d8
Author: Dmitry Goncharov <dgoncharov@users.sf.net>
Date:   Sun Dec 11 22:45:21 2022 -0500

    [SV 63537] Fix setting -w in makefiles.
    
    * makeint.h: Declare should_print_dir function. Remove print_directory flag.
    * main.c (main): Remove print_directory flag and all code that sets it.
    (main): Add a comment.
    Define should_print_dir function.
    * output.c (output_dump): Replace print_directory flag with should_print_dir.
    (output_start): Ditto.

diff --git a/src/main.c b/src/main.c
index 181fdc9e..195ccd28 100644
--- a/src/main.c
+++ b/src/main.c
@@ -195,9 +195,6 @@ static const int default_keep_going_flag = 0;
 
 int check_symlink_flag = 0;
 
-/* Nonzero means print directory before starting and when done (-w).  */
-
-int print_directory;
 static int print_directory_flag = -1;
 static const int default_print_directory_flag = -1;
 
@@ -1581,6 +1578,10 @@ main (int argc, char **argv, char **envp)
       define_variable_cname (GNUMAKEFLAGS_NAME, "", o_env, 0);
     }
 
+   /* Honor MAKEFLAGS's origin as command line, because in submake MAKEFLAGS
+    * can carry command line switches.  One consequence of this treatment, it
+    * causes env variable MAKEFLAGS to beat makefile modifications to
+    * MAKEFLAGS.  */
   decode_env_switches (STRING_SIZE_TUPLE (MAKEFLAGS_NAME), o_command);
 
 #if 0
@@ -1682,14 +1683,6 @@ main (int argc, char **argv, char **envp)
   /* Set always_make_flag if -B was given and we've not restarted already.  */
   always_make_flag = always_make_set && (restarts == 0);
 
-  /* If the user didn't specify any print-directory options, compute the
-     default setting: disable under -s / print in sub-makes and under -C.  */
-
-  if (print_directory_flag == -1)
-    print_directory = !silent_flag && (directories != 0 || makelevel > 0);
-  else
-    print_directory = print_directory_flag;
-
   /* If -R was given, set -r too (doesn't make sense otherwise!)  */
   if (no_builtin_variables_flag)
     no_builtin_rules_flag = 1;
@@ -3106,7 +3099,7 @@ handle_non_switch_argument (const char *arg, enum variable_origin origin)
 }
 
 /* Decode switches from ARGC and ARGV.
-   They came from the environment if ENV is nonzero.  */
+   They came from the environment if ORIGIN is o_env.  */
 
 static void
 decode_switches (int argc, const char **argv, enum variable_origin origin)
@@ -3636,6 +3629,19 @@ define_makeflags (int makefile)
 
   return v;
 }
+
+/* Return 1, if the message containing the working directory should be printed.
+ * Otherwise, return 0.  */
+int
+should_print_dir (void)
+{
+    if (print_directory_flag >= 0)
+        return print_directory_flag;
+
+    /* If the user didn't specify any print-directory options, compute the
+       default setting: disable under -s / print in sub-makes and under -C.  */
+    return !silent_flag && (directories != 0 || makelevel > 0);
+}
 
 /* Print version information.  */
 
diff --git a/src/makeint.h b/src/makeint.h
index fa56d3d2..33da2532 100644
--- a/src/makeint.h
+++ b/src/makeint.h
@@ -563,6 +563,7 @@ void decode_env_switches (const char*, size_t line,
                           enum variable_origin origin);
 struct variable;
 struct variable *define_makeflags (int makefile);
+int should_print_dir (void);
 void temp_stdin_unlink (void);
 void die (int) NORETURN;
 void pfatal_with_name (const char *) NORETURN;
@@ -740,7 +741,7 @@ extern unsigned short stopchar_map[];
 extern int just_print_flag, run_silent, ignore_errors_flag, keep_going_flag;
 extern int print_data_base_flag, question_flag, touch_flag, always_make_flag;
 extern int env_overrides, no_builtin_rules_flag, no_builtin_variables_flag;
-extern int print_version_flag, print_directory, check_symlink_flag;
+extern int print_version_flag, check_symlink_flag;
 extern int warn_undefined_variables_flag, posix_pedantic;
 extern int not_parallel, second_expansion, clock_skew_detected;
 extern int rebuilding_makefiles, one_shell, output_sync, verify_flag;
diff --git a/src/output.c b/src/output.c
index 22387dff..9122fee8 100644
--- a/src/output.c
+++ b/src/output.c
@@ -285,7 +285,8 @@ output_dump (struct output *out)
         }
 
       /* Log the working directory for this dump.  */
-      if (print_directory && output_sync != OUTPUT_SYNC_RECURSE)
+
+      if (output_sync != OUTPUT_SYNC_RECURSE && should_print_dir ())
         traced = log_working_directory (1);
 
       if (outfd_not_empty)
@@ -369,7 +370,7 @@ output_start (void)
   /* If we're not syncing this output per-line or per-target, make sure we emit
      the "Entering..." message where appropriate.  */
   if (output_sync == OUTPUT_SYNC_NONE || output_sync == OUTPUT_SYNC_RECURSE)
-    if (! stdio_traced && print_directory)
+    if (! stdio_traced && should_print_dir ())
       stdio_traced = log_working_directory (1);
 }
 
