This patch seems to be the proper fix for this diff from Emacs
merge-gnulib:

diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index 6df7f6d9b00..49ca3229728 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -3296,7 +3296,9 @@ ifneq (,$(GL_COND_OBJ_STDIO_WRITE_CONDITION))
 libgnu_a_SOURCES += stdio-write.c
 endif
 
-EXTRA_DIST += stdio.in.h
+EXTRA_DIST += stdio-read.c stdio.in.h
+
+EXTRA_libgnu_a_SOURCES += stdio-read.c
 
 endif
 ## end   gnulib module stdio

It looks like Dmitry kindly marked it with a TODO, but I didn't know
enough about gnulib-tool until now to understand the issue.

This patch seems correct to me and allows me to do this for the first
time:

$ env GNULIB_TOOL_IMPL=sh ./admin/merge-gnulib
$ git add .
$ rm -rf *
$ git restore .
$ env GNULIB_TOOL_IMPL=py ./admin/merge-gnulib
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   lib/gnulib.mk.in
        modified:   lib/mini-gmp.c
        modified:   lib/stdlib.in.h
        modified:   m4/stdlib_h.m4

I would appreciate this patch being double checked though. The
corresponding lines in gnulib-tool.sh are 2589-2633.

Not entirely related, but I don't think I knew of the
{ compound-list ; } shell execution as used to set
'already_mentioned_files' until looking at gnulib-tool.sh. Lots of
clever stuff in that file for me to learn.

Collin
From 1e0cca9cb270840ed6fc3b0c8810589bab5833be Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 20 Mar 2024 11:57:19 -0700
Subject: [PATCH] gnulib-tool.py: Fix unconditional Automake snippets for
 non-tests.

* pygnulib/GLModuleSystem.py
(GLModule.getAutomakeSnippet_Unconditional): Fix the file lookups used
to determine the EXTRA_DIST and EXTRA_lib_SOURCES Automake variables.
Update comment to match gnulib-tool.sh.
---
 ChangeLog                  |  8 ++++++++
 pygnulib/GLModuleSystem.py | 36 ++++++++++++++++++++----------------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 09ab4ac158..16c8ab130b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-03-20  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Fix unconditional Automake snippets for non-tests.
+	* pygnulib/GLModuleSystem.py
+	(GLModule.getAutomakeSnippet_Unconditional): Fix the file lookups used
+	to determine the EXTRA_DIST and EXTRA_lib_SOURCES Automake variables.
+	Update comment to match gnulib-tool.sh.
+
 2024-03-20  Bruno Haible  <br...@clisp.org>
 
 	gnulib-tool.sh: Undocument the --[no-]cache-modules options.
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index a96a2988b5..9dfc3dd5bb 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -652,30 +652,34 @@ class GLModule(object):
                     result += 'EXTRA_DIST += %s' % ' '.join(extra_files)
                     result += constants.NL * 2
             else:  # if not tests module
-                # TODO: unconditional automake snippet for nontests modules
                 snippet = self.getAutomakeSnippet_Conditional()
                 snippet = constants.combine_lines(snippet)
-                pattern = re.compile('^lib_SOURCES[\t ]*\\+=[\t ]*(.*)$', re.M)
-                mentioned_files = pattern.findall(snippet)
-                if mentioned_files != list():
-                    mentioned_files = mentioned_files[-1].split(' ')
-                    mentioned_files = [ f.strip()
-                                        for f in mentioned_files ]
-                    mentioned_files = [ f
-                                        for f in mentioned_files
-                                        if f != '' ]
-                    mentioned_files = sorted(set(mentioned_files))
+                pattern = re.compile(r'^lib_SOURCES[\t ]*\+=[\t ]*(.*)$', re.MULTILINE)
+                mentioned_files = set(pattern.findall(snippet))
+                if mentioned_files:
+                    # Get all the file names from 'lib_SOURCES += ...'.
+                    mentioned_files = { filename
+                                        for line in mentioned_files
+                                        for filename in line.split() }
                 all_files = self.getFiles()
                 lib_files = filter_filelist(constants.NL, all_files,
                                             'lib/', '', 'lib/', '').split(constants.NL)
-                extra_files = [ f
-                                for f in lib_files
-                                if f not in mentioned_files ]
-                extra_files = sorted(set(extra_files))
+                # Remove mentioned_files from lib_files.
+                extra_files = sorted(set(lib_files).difference(mentioned_files))
                 if extra_files != [''] and extra_files:
                     result += 'EXTRA_DIST += %s' % ' '.join(extra_files)
                     result += '\n\n'
-                # Synthesize also an EXTRA_lib_SOURCES augmentation
+                # Synthesize also an EXTRA_lib_SOURCES augmentation.
+                # This is necessary so that automake can generate the right list of
+                # dependency rules.
+                # A possible approach would be to use autom4te --trace of the redefined
+                # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
+                # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
+                # inside autoconf's built-in macros are not missed).
+                # But it's simpler and more robust to do it here, based on the file list.
+                # If some .c file exists and is not used with AC_LIBOBJ - for example,
+                # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
+                # automake will generate a useless dependency; this is harmless.
                 if str(self) != 'relocatable-prog-wrapper' and str(self) != 'pt_chown':
                     extra_files = filter_filelist(constants.NL, extra_files,
                                                   '', '.c', '', '').split(constants.NL)
-- 
2.44.0

Reply via email to