Subject: bug#32868: [PATCH v2] m4: rework silent-rules macros to avoid double expansion

From: Mike Frysinger <[email-masked]>
Date: Sun, 27 Feb 2022 21:01:35 -0500

Fixes automake bug https://bugs.gnu.org/32868.

The AM_SILENT_RULES macro defines all the silent-rules related setup.
It's also called by users to change the default verbosity level.  This
leads to a quirk where automake calls it, expands the full context,
and then users call it, and it's fully expanded again.

Instead, let's rename AM_SILENT_RULES to _AM_SILENT_RULES and move the
initialization logic to late in the configure stage.  This allows the
user-centric AM_SILENT_RULES call to expand into a single line to set
the default verbosity.

* m4/init.m4: Switch to _AM_SILENT_RULES.
* m4/silent.m4: Rename AM_SILENT_RULES to _AM_SILENT_RULES.  Delay
evaluation of AM_SILENT_RULES to the end.  Define new AM_SILENT_RULES to
set default rules verbosity.
* t/silent-defaults.sh: New tests.
* t/list-of-tests.mk: Add t/silent-defaults.sh.
---
v2
- fix default handling when AM_SILENT_RULES is called before AM_INIT_AUTOMAKE.

 m4/init.m4           |   2 +-
 m4/silent.m4         |  49 ++++++++-----
 t/list-of-tests.mk   |   1 +
 t/silent-defaults.sh | 164 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 196 insertions(+), 20 deletions(-)
 create mode 100644 t/silent-defaults.sh

diff --git a/m4/init.m4 b/m4/init.m4
index f3abf66c6e96..f3c7845bca70 100644
--- a/m4/init.m4
+++ b/m4/init.m4
@@ -134,7 +134,7 @@ if test -z "$CSCOPE"; then
 fi
 AC_SUBST([CSCOPE])
 
-AC_REQUIRE([AM_SILENT_RULES])dnl
+AC_REQUIRE([_AM_SILENT_RULES])dnl
 dnl The testsuite driver may need to know about EXEEXT, so add the
 dnl 'am__EXEEXT' conditional if _AM_COMPILER_EXEEXT was seen.  This
 dnl macro is hooked onto _AC_COMPILER_EXEEXT early, see below.
diff --git a/m4/silent.m4 b/m4/silent.m4
index b1502ed389b6..d19c56e8ac3b 100644
--- a/m4/silent.m4
+++ b/m4/silent.m4
@@ -5,12 +5,12 @@
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
 
-# AM_SILENT_RULES([DEFAULT])
-# --------------------------
-# Enable less verbose build rules; with the default set to DEFAULT
-# ("yes" being less verbose, "no" or empty being verbose).
-AC_DEFUN([AM_SILENT_RULES],
-[AC_ARG_ENABLE([silent-rules], [dnl
+# _AM_SILENT_RULES
+# ----------------
+# Enable less verbose build rules support.
+AC_DEFUN([_AM_SILENT_RULES],
+[AM_DEFAULT_VERBOSITY=1
+AC_ARG_ENABLE([silent-rules], [dnl
 AS_HELP_STRING(
   [--enable-silent-rules],
   [less verbose build output (undo: "make V=1")])
@@ -18,11 +18,6 @@ AS_HELP_STRING(
   [--disable-silent-rules],
   [verbose build output (undo: "make V=0")])dnl
 ])
-case $enable_silent_rules in @%:@ (((
-  yes) AM_DEFAULT_VERBOSITY=0;;
-   no) AM_DEFAULT_VERBOSITY=1;;
-    *) AM_DEFAULT_VERBOSITY=m4_if([$1], [yes], [0], [1]);;
-esac
 dnl
 dnl A few 'make' implementations (e.g., NonStop OS and NextStep)
 dnl do not support nested variable expansions.
@@ -41,14 +36,6 @@ am__doit:
 else
   am_cv_make_support_nested_variables=no
 fi])
-if test $am_cv_make_support_nested_variables = yes; then
-  dnl Using '$V' instead of '$(V)' breaks IRIX make.
-  AM_V='$(V)'
-  AM_DEFAULT_V='$(AM_DEFAULT_VERBOSITY)'
-else
-  AM_V=$AM_DEFAULT_VERBOSITY
-  AM_DEFAULT_V=$AM_DEFAULT_VERBOSITY
-fi
 AC_SUBST([AM_V])dnl
 AM_SUBST_NOTMAKE([AM_V])dnl
 AC_SUBST([AM_DEFAULT_V])dnl
@@ -57,4 +44,28 @@ AC_SUBST([AM_DEFAULT_VERBOSITY])dnl
 AM_BACKSLASH='\'
 AC_SUBST([AM_BACKSLASH])dnl
 _AM_SUBST_NOTMAKE([AM_BACKSLASH])dnl
+dnl Delay evaluation of AM_DEFAULT_VERBOSITY to the end to allow multiple calls
+dnl to AM_SILENT_RULES to change the default value.
+AC_CONFIG_COMMANDS_PRE([dnl
+case $enable_silent_rules in @%:@ (((
+  yes) AM_DEFAULT_VERBOSITY=0;;
+   no) AM_DEFAULT_VERBOSITY=1;;
+esac
+if test $am_cv_make_support_nested_variables = yes; then
+  dnl Using '$V' instead of '$(V)' breaks IRIX make.
+  AM_V='$(V)'
+  AM_DEFAULT_V='$(AM_DEFAULT_VERBOSITY)'
+else
+  AM_V=$AM_DEFAULT_VERBOSITY
+  AM_DEFAULT_V=$AM_DEFAULT_VERBOSITY
+fi
+])dnl
 ])
+
+# AM_SILENT_RULES([DEFAULT])
+# --------------------------
+# Set the default verbosity level to DEFAULT ("yes" being less verbose, "no" or
+# empty being verbose).
+AC_DEFUN([AM_SILENT_RULES],
+[AC_REQUIRE([_AM_SILENT_RULES])
+AM_DEFAULT_VERBOSITY=m4_if([$1], [yes], [0], [1])])
diff --git a/t/list-of-tests.mk b/t/list-of-tests.mk
index d959b68db47b..dbfd8d6d1145 100644
--- a/t/list-of-tests.mk
+++ b/t/list-of-tests.mk
@@ -1015,6 +1015,7 @@ t/sanity.sh \
 t/seenc.sh \
 t/silent-c.sh \
 t/silent-cxx.sh \
+t/silent-defaults.sh \
 t/silent-lt.sh \
 t/silent-f77.sh \
 t/silent-f90.sh \
diff --git a/t/silent-defaults.sh b/t/silent-defaults.sh
new file mode 100644
index 000000000000..c4cdee548911
--- /dev/null
+++ b/t/silent-defaults.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+# Copyright (C) 2022 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Check verbose mode defaults and behavior.
+
+. test-init.sh
+
+: > Makefile.am
+
+# Default behavior is currently verbose.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_INIT_AUTOMAKE
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$ACLOCAL
+$AUTOMAKE
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+# User doesn't pick a silent mode default before AM_INIT_AUTOMAKE.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_SILENT_RULES
+AM_INIT_AUTOMAKE
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+# User disables silent mode default before AM_INIT_AUTOMAKE.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_SILENT_RULES([no])
+AM_INIT_AUTOMAKE
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+# User enables silent mode default before AM_INIT_AUTOMAKE.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_SILENT_RULES([yes])
+AM_INIT_AUTOMAKE
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+# User doesn't pick a silent mode default after AM_INIT_AUTOMAKE.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_INIT_AUTOMAKE
+AM_SILENT_RULES
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+# User disables silent mode default after AM_INIT_AUTOMAKE.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_INIT_AUTOMAKE
+AM_SILENT_RULES([no])
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+# User enables silent mode default after AM_INIT_AUTOMAKE.
+cat <<EOF >configure.ac
+AC_INIT([silent-defaults], [1.0])
+AM_INIT_AUTOMAKE
+AM_SILENT_RULES([yes])
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+$AUTOCONF
+
+./configure -C
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --enable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 0' Makefile
+
+./configure -C --disable-silent-rules
+grep '^AM_DEFAULT_VERBOSITY = 1' Makefile
+
+:
-- 
2.34.1