Yesterday I wrote:
> gnulib-tool with option --makefile-name will be changed to append the
> '-Wno-error' and related options also to AM_CFLAGS.

Done through the attached two patches. (The lists.gnu.org reference needs to
be updated a posteriori.)

> This will break coreutils and some other packages, which don't initialize
> the necessary variables, mentioned in [2].

In fact, only those packages are affected that use --makefile-name and
--with-tests together. These are:
  - libunistring
  - gettext
  - coreutils
  - diffutils
  - idutils
  - sed
  - m4
  - grep

I'll follow up with each of them.
>From 813f4991a29b51d13b56a5fe7b64e149e1d58f3c Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Wed, 3 Sep 2025 23:43:45 +0200
Subject: [PATCH 1/2] gnulib-tool: Improve C++ awareness of 2021-06-10 /
 2024-03-06 commit.

* gnulib-tool.sh (func_emit_lib_Makefile_am): If the file list contains
some C++ files, emit also an initialization for
${libname}_${libext}_CXXFLAGS.
(func_emit_tests_Makefile_am): If the file list contains no C++ files,
don't emit an initialization for CXXFLAGS.
* pygnulib/GLEmiter.py (GLEmiter.lib_Makefile_am): If the file list
contains some C++ files, emit also an initialization for
${libname}_${libext}_CXXFLAGS.
(GLEmiter.tests_Makefile_am): If the file list contains no C++ files,
don't emit an initialization for CXXFLAGS.
---
 ChangeLog            | 14 ++++++++++++++
 gnulib-tool.sh       | 27 ++++++++++++++++++++++-----
 pygnulib/GLEmiter.py | 27 ++++++++++++++++++++++-----
 3 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0e489bd482..7d7c44aea5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2025-09-03  Bruno Haible  <[email protected]>
+
+	gnulib-tool: Improve C++ awareness of 2021-06-10 / 2024-03-06 commit.
+	* gnulib-tool.sh (func_emit_lib_Makefile_am): If the file list contains
+	some C++ files, emit also an initialization for
+	${libname}_${libext}_CXXFLAGS.
+	(func_emit_tests_Makefile_am): If the file list contains no C++ files,
+	don't emit an initialization for CXXFLAGS.
+	* pygnulib/GLEmiter.py (GLEmiter.lib_Makefile_am): If the file list
+	contains some C++ files, emit also an initialization for
+	${libname}_${libext}_CXXFLAGS.
+	(GLEmiter.tests_Makefile_am): If the file list contains no C++ files,
+	don't emit an initialization for CXXFLAGS.
+
 2025-09-02  Collin Funk  <[email protected]>
 
 	crypto/sha3-buffer: Add support for OpenSSL.
diff --git a/gnulib-tool.sh b/gnulib-tool.sh
index 96e2f39b4a..e3a9b92de6 100755
--- a/gnulib-tool.sh
+++ b/gnulib-tool.sh
@@ -3840,6 +3840,7 @@ func_emit_lib_Makefile_am ()
   fi
   echo
   uses_subdirs=
+  uses_cxx=
   {
     for module in $modules; do
       func_verify_nontests_module
@@ -3919,12 +3920,17 @@ func_emit_lib_Makefile_am ()
           echo
         fi
         rm -f "$tmp"/amsnippet1 "$tmp"/amsnippet2
-        # Test whether there are some source files in subdirectories.
+        # Test whether there are some source files in subdirectories, and
+        # test whether there are some C++ source files.
         for f in `func_get_filelist "$module"`; do
           case $f in
             lib/*/*.c)
               uses_subdirs=yes
-              break
+              ;;
+          esac
+          case $f in
+            *.C | *.c++ | *.cc | *.cxx | *.cpp)
+              uses_cxx=yes
               ;;
           esac
         done
@@ -4049,6 +4055,9 @@ func_emit_lib_Makefile_am ()
   #   '-Werror'.
   if ! $for_test; then
     echo "${libname}_${libext}_CFLAGS = \$(AM_CFLAGS) \$(GL_CFLAG_GNULIB_WARNINGS) \$(GL_CFLAG_ALLOW_WARNINGS)"
+    if test -n "$uses_cxx"; then
+      echo "${libname}_${libext}_CXXFLAGS = \$(AM_CXXFLAGS) \$(GL_CXXFLAG_GNULIB_WARNINGS) \$(GL_CXXFLAG_ALLOW_WARNINGS)"
+    fi
   fi
   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
   # automake during its analysis looks for $(LIBOBJS), not for @LIBOBJS@.
@@ -4249,6 +4258,7 @@ func_emit_tests_Makefile_am ()
   func_emit_copyright_notice
   echo
   uses_subdirs=
+  uses_cxx=
   {
     for module in $modules; do
       if $for_test && ! $single_configure; then
@@ -4345,12 +4355,17 @@ func_emit_tests_Makefile_am ()
           } >&$ofd
         fi
         rm -f "$tmp"/amsnippet1 "$tmp"/amsnippet2
-        # Test whether there are some source files in subdirectories.
+        # Test whether there are some source files in subdirectories, and
+        # test whether there are some C++ source files.
         for f in `func_get_filelist "$module"`; do
           case $f in
             lib/*/*.c | tests/*/*.c)
               uses_subdirs=yes
-              break
+              ;;
+          esac
+          case $f in
+            *.C | *.c++ | *.cc | *.cxx | *.cpp)
+              uses_cxx=yes
               ;;
           esac
         done
@@ -4445,7 +4460,9 @@ func_emit_tests_Makefile_am ()
     cflags_for_gnulib_code=" \$(GL_CFLAG_GNULIB_WARNINGS)"
   fi
   echo "CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@${cflags_for_gnulib_code} @CFLAGS@"
-  echo "CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@"
+  if test -n "$uses_cxx"; then
+    echo "CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@"
+  fi
   echo
   echo "AM_CPPFLAGS = \\"
   if $for_test; then
diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py
index efa95244fe..9d02210518 100644
--- a/pygnulib/GLEmiter.py
+++ b/pygnulib/GLEmiter.py
@@ -737,6 +737,7 @@ def lib_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable: G
             emit += '# Reproduce by:\n%s\n' % actioncmd
         emit += '\n'
         uses_subdirs = False
+        uses_cxx = False
 
         # Compute allsnippets variable.
         allsnippets = ''
@@ -807,13 +808,19 @@ def lib_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable: G
                         allsnippets += 'endif\n'
                     allsnippets += '## end   gnulib module %s\n\n' % module.name
 
-                    # Test whether there are some source files in subdirectories.
+                    # Test whether there are some source files in subdirectories, and
+                    # test whether there are some C++ source files.
                     for file in module.getFiles():
                         if (file.startswith('lib/')
                                 and file.endswith('.c')
                                 and file.count('/') > 1):
                             uses_subdirs = True
-                            break
+                        if (file.endswith('.C')
+                                or file.endswith('.c++')
+                                or file.endswith('.cc')
+                                or file.endswith('.cxx')
+                                or file.endswith('.cpp')):
+                            uses_cxx = True
 
         if not makefile_name:
             subdir_options = ''
@@ -928,6 +935,8 @@ def lib_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable: G
         #   '-Werror'.
         if not for_test:
             emit += '%s_%s_CFLAGS = $(AM_CFLAGS) $(GL_CFLAG_GNULIB_WARNINGS) $(GL_CFLAG_ALLOW_WARNINGS)\n' % (libname, libext)
+            if uses_cxx:
+                emit += '%s_%s_CXXFLAGS = $(AM_CXXFLAGS) $(GL_CXXFLAG_GNULIB_WARNINGS) $(GL_CXXFLAG_ALLOW_WARNINGS)\n' % (libname, libext)
         # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
         # automake during its analysis looks for $(LIBOBJS), not for @LIBOBJS@.
         if not for_test:
@@ -1063,6 +1072,7 @@ def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable:
         emit += '%s\n' % self.copyright_notice()
 
         uses_subdirs = False
+        uses_cxx = False
         main_snippets = ''
         longrun_snippets = ''
         for module in modules:
@@ -1143,13 +1153,19 @@ def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable:
                     else:
                         main_snippets += snippet
 
-                    # Test whether there are some source files in subdirectories.
+                    # Test whether there are some source files in subdirectories, and
+                    # test whether there are some C++ source files.
                     for file in module.getFiles():
                         if ((file.startswith('lib/') or file.startswith('tests/'))
                                 and file.endswith('.c')
                                 and file.count('/') > 1):
                             uses_subdirs = True
-                            break
+                        if (file.endswith('.C')
+                                or file.endswith('.c++')
+                                or file.endswith('.cc')
+                                or file.endswith('.cxx')
+                                or file.endswith('.cpp')):
+                            uses_cxx = True
 
         # Generate dependencies here, since it eases the debugging of test failures.
         # If there are source files in subdirectories, prevent collision of the
@@ -1227,7 +1243,8 @@ def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable:
             # Enable or disable warnings as suitable for the Gnulib coding style.
             cflags_for_gnulib_code = ' $(GL_CFLAG_GNULIB_WARNINGS)'
         emit += 'CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@%s @CFLAGS@\n' % (cflags_for_gnulib_code)
-        emit += 'CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@\n'
+        if uses_cxx:
+            emit += 'CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@\n'
         emit += '\n'
 
         emit += 'AM_CPPFLAGS = \\\n'
-- 
2.50.1

From 6fd6098f510255c0b9134088b1526a50fef90edd Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Thu, 4 Sep 2025 00:15:19 +0200
Subject: [PATCH 2/2] gnulib-tool: In tests directories, augment AM_CFLAGS and
 AM_CXXFLAGS.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reported by P??draig Brady in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-09/>.

* gnulib-tool.sh (func_emit_tests_Makefile_am): Append the
warning-protection options to AM_CFLAGS and AM_CXXFLAGS, in addition to
initializing CFLAGS and CXXFLAGS.
* pygnulib/GLEmiter.py (GLEmiter.tests_Makefile_am): Likewise.
* doc/gnulib-tool.texi (Modified build rules): Document the need to
initialize AM_CFLAGS and AM_CXXFLAGS in tests directories.
* NEWS: Mention the new requirement.
---
 ChangeLog            | 13 +++++++++++++
 NEWS                 |  4 ++++
 doc/gnulib-tool.texi | 18 ++++++++++++++++--
 gnulib-tool.sh       | 11 +++++++++++
 pygnulib/GLEmiter.py | 10 ++++++++++
 5 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7d7c44aea5..956c4ab407 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2025-09-03  Bruno Haible  <[email protected]>
+
+	gnulib-tool: In tests directories, augment AM_CFLAGS and AM_CXXFLAGS.
+	Reported by P??draig Brady in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2025-09/msg00018.html>.
+	* gnulib-tool.sh (func_emit_tests_Makefile_am): Append the
+	warning-protection options to AM_CFLAGS and AM_CXXFLAGS, in addition to
+	initializing CFLAGS and CXXFLAGS.
+	* pygnulib/GLEmiter.py (GLEmiter.tests_Makefile_am): Likewise.
+	* doc/gnulib-tool.texi (Modified build rules): Document the need to
+	initialize AM_CFLAGS and AM_CXXFLAGS in tests directories.
+	* NEWS: Mention the new requirement.
+
 2025-09-03  Bruno Haible  <[email protected]>
 
 	gnulib-tool: Improve C++ awareness of 2021-06-10 / 2024-03-06 commit.
diff --git a/NEWS b/NEWS
index 664007395d..de89ddb78c 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,10 @@ Important general notes
 
 Date        Modules         Changes
 
+2025-09-03  gnulib-tool     If you use --makefile-name with --with-tests, you
+                            now need to initialize AM_CFLAGS before the
+                            'include <gnulib-generated-makefile>' statement.
+
 2022-07-24  bootstrap       To specify a reference directory for the 'gnulib'
                             submodule, use the environment variable
                             GNULIB_REFDIR instead of GNULIB_SRCDIR or the
diff --git a/doc/gnulib-tool.texi b/doc/gnulib-tool.texi
index 2ee6fd83de..e9ee83703c 100644
--- a/doc/gnulib-tool.texi
+++ b/doc/gnulib-tool.texi
@@ -747,8 +747,11 @@
 @smallexample
 include Makefile.gnulib
 @end smallexample
-Before this include, you need to initialize this set of @code{Makefile.am}
-variables:
+Before this include, you need to initialize a set of @code{Makefile.am}
+variables.
+
+For the directory into which @code{gnulib-tool} copies the gnulib source files,
+the set of variables to initialize is:
 @itemize @bullet
 @item
 @code{AUTOMAKE_OPTIONS}
@@ -789,6 +792,17 @@
 likely want to initialize some of them with non-empty values, in order
 to achieve the desired customization.
 
+For the directory into which @code{gnulib-tool} copies
+the gnulib unit test files
+(when option @samp{--with-tests} is used),
+the set of variables to initialize is:
+@itemize @bullet
+@item
+@code{AM_CFLAGS}
+@item
+@code{AM_CXXFLAGS} (when this directory contains some C++ code)
+@end itemize
+
 The other approach, the kitchen-sink module, is more advanced.  See
 chapter @ref{Extending Gnulib}.
 
diff --git a/gnulib-tool.sh b/gnulib-tool.sh
index e3a9b92de6..2e9e70036c 100755
--- a/gnulib-tool.sh
+++ b/gnulib-tool.sh
@@ -4459,6 +4459,17 @@ func_emit_tests_Makefile_am ()
     # Enable or disable warnings as suitable for the Gnulib coding style.
     cflags_for_gnulib_code=" \$(GL_CFLAG_GNULIB_WARNINGS)"
   fi
+  # The primary place to add these options is AM_CFLAGS.
+  echo "AM_CFLAGS += @GL_CFLAG_ALLOW_WARNINGS@${cflags_for_gnulib_code}"
+  if test -n "$uses_cxx"; then
+    echo "AM_CXXFLAGS += @GL_CXXFLAG_ALLOW_WARNINGS@"
+  fi
+  echo
+  # The secondary place to add these options is CFLAGS. This is less reliable,
+  # because the user can invoke e.g. "make CFLAGS=-O2"; see
+  # <https://www.gnu.org/software/automake/manual/html_node/User-Variables.html>
+  # But it is a protection against packages which do  AM_CFLAGS += -Werror
+  # after including this generated Makefile (such as GNU grep).
   echo "CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@${cflags_for_gnulib_code} @CFLAGS@"
   if test -n "$uses_cxx"; then
     echo "CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@"
diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py
index 9d02210518..4abbacb710 100644
--- a/pygnulib/GLEmiter.py
+++ b/pygnulib/GLEmiter.py
@@ -1242,6 +1242,16 @@ def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable:
         if not for_test:
             # Enable or disable warnings as suitable for the Gnulib coding style.
             cflags_for_gnulib_code = ' $(GL_CFLAG_GNULIB_WARNINGS)'
+        # The primary place to add these options is AM_CFLAGS.
+        emit += 'AM_CFLAGS += @GL_CFLAG_ALLOW_WARNINGS@%s\n' % (cflags_for_gnulib_code)
+        if uses_cxx:
+            emit += 'AM_CXXFLAGS += @GL_CXXFLAG_ALLOW_WARNINGS@\n'
+        emit += '\n'
+        # The secondary place to add these options is CFLAGS. This is less reliable,
+        # because the user can invoke e.g. "make CFLAGS=-O2"; see
+        # <https://www.gnu.org/software/automake/manual/html_node/User-Variables.html>
+        # But it is a protection against packages which do  AM_CFLAGS += -Werror
+        # after including this generated Makefile (such as GNU grep).
         emit += 'CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@%s @CFLAGS@\n' % (cflags_for_gnulib_code)
         if uses_cxx:
             emit += 'CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@\n'
-- 
2.50.1

Reply via email to