Hi Collin,

> It also breaks --create-testdir though:
> 
>     $ gnulib-tool --create-testdir --dir testdir1 crypto/sha3
>     [...]
>     gllib/Makefile.am: installing 'build-aux/depcomp'
>     gltests/Makefile.am:46: error: AM_CFLAGS must be set with '=' before 
> using '+='
> 
> I pushed the attatched patch to fix both gnulib-tool.sh and
> gnulib-tool.py.

Oops. Thanks for the stop-gap fix. 

Thinking more about it:
  - We can simplify the two emitted lines into a single line, as shown below:

diff -r -u ../testdir1/gltests/Makefile.am ../testdir2/gltests/Makefile.am
--- ../testdir1/gltests/Makefile.am     2025-09-04 22:19:25.794121004 +0200
+++ ../testdir2/gltests/Makefile.am     2025-09-04 22:41:37.305994956 +0200
@@ -43,8 +43,7 @@
 DISTCLEANFILES =
 MAINTAINERCLEANFILES =
 
-AM_CFLAGS =
-AM_CFLAGS += @GL_CFLAG_ALLOW_WARNINGS@
+AM_CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@
 
 CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@ @CFLAGS@
 

  - There is also the case of using --with-tests without --makefile-name.

Committing the attached patch. And this time, I've updated the gnulib-tool-tests
as well.

Bruno

>From 4ef864ec873506aacf717d0cb558d950831dc399 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Thu, 4 Sep 2025 22:52:44 +0200
Subject: [PATCH] gnulib-tool: Improve changes from yesterday and today.

* gnulib-tool.sh (func_emit_tests_Makefile_am): Consider also the case
that --with-tests is specified and --makefile-name /
--tests-makefile-name are not specified. Simplify an initialization and
augmentation to an initialization.
* pygnulib/GLEmiter.py (GLEmiter.tests_Makefile_am): Likewise.
---
 ChangeLog            |  9 +++++++++
 gnulib-tool.sh       | 19 +++++++++----------
 pygnulib/GLEmiter.py | 25 +++++++++++++------------
 3 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3752bb9198..5052d65d21 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2025-09-04  Bruno Haible  <[email protected]>
+
+	gnulib-tool: Improve changes from yesterday and today.
+	* gnulib-tool.sh (func_emit_tests_Makefile_am): Consider also the case
+	that --with-tests is specified and --makefile-name /
+	--tests-makefile-name are not specified. Simplify an initialization and
+	augmentation to an initialization.
+	* pygnulib/GLEmiter.py (GLEmiter.tests_Makefile_am): Likewise.
+
 2025-09-03  Collin Funk  <[email protected]>
 
 	crypto/sha3-buffer: Don't abort on OOM.
diff --git a/gnulib-tool.sh b/gnulib-tool.sh
index 566525742a..15a642279f 100755
--- a/gnulib-tool.sh
+++ b/gnulib-tool.sh
@@ -4454,23 +4454,22 @@ func_emit_tests_Makefile_am ()
   #   CFLAGS, they have asked for errors, they will get errors. But they have
   #   no right to complain about these errors, because Gnulib does not support
   #   '-Werror'.
+  if ! $for_test && { test -n "$tests_makefile_name" || test -n "$makefile_name"; }; then
+    # This function produces a makefile that is meant to be 'include'd.
+    am_set_or_augment='+='
+  else
+    # This function produces a makefile 'Makefile.am' that is standalone.
+    am_set_or_augment='='
+  fi
   cflags_for_gnulib_code=
   if ! $for_test; then
     # Enable or disable warnings as suitable for the Gnulib coding style.
     cflags_for_gnulib_code=" \$(GL_CFLAG_GNULIB_WARNINGS)"
-  else
-    # Make sure AM_CFLAGS is set or Automake will error when we append to it
-    # using '+='.
-    echo 'AM_CFLAGS ='
-    # Likewise for AM_CXXFLAGS.
-    if test -n "$uses_cxx"; then
-      echo 'AM_CXXFLAGS ='
-    fi
   fi
   # The primary place to add these options is AM_CFLAGS.
-  echo "AM_CFLAGS += @GL_CFLAG_ALLOW_WARNINGS@${cflags_for_gnulib_code}"
+  echo "AM_CFLAGS ${am_set_or_augment} @GL_CFLAG_ALLOW_WARNINGS@${cflags_for_gnulib_code}"
   if test -n "$uses_cxx"; then
-    echo "AM_CXXFLAGS += @GL_CXXFLAG_ALLOW_WARNINGS@"
+    echo "AM_CXXFLAGS ${am_set_or_augment} @GL_CXXFLAG_ALLOW_WARNINGS@"
   fi
   echo
   # The secondary place to add these options is CFLAGS. This is less reliable,
diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py
index c055786a03..8461bfde54 100644
--- a/pygnulib/GLEmiter.py
+++ b/pygnulib/GLEmiter.py
@@ -1000,9 +1000,9 @@ def lib_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable: G
     def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable: GLModuleTable,
                           makefiletable: GLMakefileTable, witness_macro: str, for_test: bool) -> str:
         '''Emit the contents of the tests Makefile. Returns it as a string.
-        GLConfig: localpath, modules, libname, auxdir, makefile_name, libtool,
-        sourcebase, m4base, testsbase, macro_prefix, witness_c_macro,
-        single_configure, libtests.
+        GLConfig: localpath, modules, libname, auxdir, makefile_name,
+        tests_makefile_name, libtool, sourcebase, m4base, testsbase,
+        macro_prefix, witness_c_macro, single_configure, libtests.
 
         destfile is a filename relative to destdir of Makefile being generated.
         witness_macro is a string which represents witness_c_macro with the suffix.
@@ -1034,6 +1034,8 @@ def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable:
         m4base = self.config['m4base']
         testsbase = self.config['testsbase']
         gnu_make = self.config['gnu_make']
+        makefile_name = self.config['makefile_name']
+        tests_makefile_name = self.config['tests_makefile_name']
         libtool = self.config['libtool']
         macro_prefix = self.config['macro_prefix']
         conddeps = self.config['conddeps']
@@ -1238,21 +1240,20 @@ def tests_Makefile_am(self, destfile: str, modules: list[GLModule], moduletable:
         #   CFLAGS, they have asked for errors, they will get errors. But they have
         #   no right to complain about these errors, because Gnulib does not support
         #   '-Werror'.
+        if (not for_test) and (tests_makefile_name or makefile_name):
+            # This function produces a makefile that is meant to be 'include'd.
+            am_set_or_augment = '+='
+        else:
+            # This function produces a makefile 'Makefile.am' that is standalone.
+            am_set_or_augment = '='
         cflags_for_gnulib_code = ''
         if not for_test:
             # Enable or disable warnings as suitable for the Gnulib coding style.
             cflags_for_gnulib_code = ' $(GL_CFLAG_GNULIB_WARNINGS)'
-        else:
-            # Make sure AM_CFLAGS is set or Automake will error when we append to it
-            # using '+='.
-            emit += 'AM_CFLAGS =\n'
-            # Likewise for AM_CXXFLAGS.
-            if uses_cxx:
-                emit += 'AM_CXXFLAGS =\n'
         # The primary place to add these options is AM_CFLAGS.
-        emit += 'AM_CFLAGS += @GL_CFLAG_ALLOW_WARNINGS@%s\n' % (cflags_for_gnulib_code)
+        emit += 'AM_CFLAGS %s @GL_CFLAG_ALLOW_WARNINGS@%s\n' % (am_set_or_augment, cflags_for_gnulib_code)
         if uses_cxx:
-            emit += 'AM_CXXFLAGS += @GL_CXXFLAG_ALLOW_WARNINGS@\n'
+            emit += 'AM_CXXFLAGS %s @GL_CXXFLAG_ALLOW_WARNINGS@\n' % (am_set_or_augment)
         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
-- 
2.50.1

Reply via email to