Here is a patch that implements the '%reldir%' replacements when
generating GNU Make output instead of Automake. It seems fairly
straightforward unless I missed something.

Side note, I would like to start making the code _slightly_ more
readable as more features get implemented. Reducing code duplication
and such. I was tempted to just do three nested re.sub calls but in
each function but I felt the patch would have looked a bit silly. :)

In this patch I've defined some "constant" (not really because Python)
regular expressions and and a private helper function. I've prefixed
these with an underscore assuming that no one has a strong dislike for
this. It is a common convention meaning an interface is only meant for
internal use [1]. It also allows definitions to not be imported when
using 'from .GLEmiter import *', for example.

[1] https://peps.python.org/pep-0008/#descriptive-naming-styles

Collin
From 9f290777ec05cb5e81d77d54b69cd68b68fbcf49 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Fri, 8 Mar 2024 01:43:26 -0800
Subject: [PATCH] gnulib-tool.py: Follow gnulib-tool changes, part 47.

Follow gnulib-tool changes
2021-12-15  Bruno Haible  <br...@clisp.org>
Fix creation of build directories with non-recursive-gnulib-prefix-hack.
2021-12-18  Bruno Haible  <br...@clisp.org>
Fix support for --gnu-make in tests (regression 2021-12-15).

* pygnulib/GLEmiter.py (_CONVERT_TO_GNU_MAKE): New variable which is a
list of tuples which are used with re.sub.
(_convert_to_gnu_make): New function which converts an input snippet
from Automake to GNU Make syntax using the regular expressions stored in
_CONVERT_TO_GNU_MAKE.
(GLEmiter.lib_Makefile_am): Use _convert_to_gnu_make. Replace '%reldir%'
tokens.
(GLEmiter.tests_Makefile_am): Likewise.
---
 ChangeLog            |  17 +++++++
 gnulib-tool.py.TODO  | 113 -------------------------------------------
 pygnulib/GLEmiter.py |  27 ++++++++---
 3 files changed, 38 insertions(+), 119 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f85da3af66..de0cd2ad63 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2024-03-08  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Follow gnulib-tool changes, part 47.
+	Follow gnulib-tool changes
+	2021-12-15  Bruno Haible  <br...@clisp.org>
+	Fix creation of build directories with non-recursive-gnulib-prefix-hack.
+	2021-12-18  Bruno Haible  <br...@clisp.org>
+	Fix support for --gnu-make in tests (regression 2021-12-15).
+	* pygnulib/GLEmiter.py (_CONVERT_TO_GNU_MAKE): New variable which is a
+	list of tuples which are used with re.sub.
+	(_convert_to_gnu_make): New function which converts an input snippet
+	from Automake to GNU Make syntax using the regular expressions stored in
+	_CONVERT_TO_GNU_MAKE.
+	(GLEmiter.lib_Makefile_am): Use _convert_to_gnu_make. Replace '%reldir%'
+	tokens.
+	(GLEmiter.tests_Makefile_am): Likewise.
+
 2024-03-07  Collin Funk  <collin.fu...@gmail.com>
 
 	gnulib-tool.py: Fix errors when executing --create-megatestdir.
diff --git a/gnulib-tool.py.TODO b/gnulib-tool.py.TODO
index a2414115aa..8918d58a88 100644
--- a/gnulib-tool.py.TODO
+++ b/gnulib-tool.py.TODO
@@ -344,119 +344,6 @@ Date:   Wed Dec 15 19:18:31 2021 +0100
 
 --------------------------------------------------------------------------------
 
-commit eafd9cd7bf13be7e99a30188b3675aa4047b9307
-Author: Bruno Haible <br...@clisp.org>
-Date:   Sat Dec 18 14:10:19 2021 +0100
-
-    Fix support for --gnu-make in tests (regression 2021-12-15).
-
-    * gnulib-tool (func_emit_tests_Makefile_am): When producing output for
-    GNU make, eliminate %reldir% tokens.
-
-commit 766ec17a90f67e8cda78394e58a7fffb00f5a4b7
-Author: Bruno Haible <br...@clisp.org>
-Date:   Wed Dec 15 19:18:20 2021 +0100
-
-    Fix creation of build directories with non-recursive-gnulib-prefix-hack.
-
-    This is necessary for executing "make" in a VPATH build of a package
-    that uses prefix-gnulib-mk and a module such as alloca-opt.
-
-    * modules/alloca-opt (Makefile.am): Before generating the .h-t file,
-    ensure that the directory in which we want to create it exists.
-    (configure.ac): Invoke AC_PROG_MKDIR_P.
-    * modules/argz: Likewise.
-    * modules/arpa_inet: Likewise.
-    * modules/assert-h: Likewise.
-    * modules/byteswap: Likewise.
-    * modules/configmake: Likewise.
-    * modules/ctype: Likewise.
-    * modules/dirent: Likewise.
-    * modules/dynarray: Likewise.
-    * modules/errno: Likewise.
-    * modules/execinfo: Likewise.
-    * modules/fcntl-h: Likewise.
-    * modules/float: Likewise.
-    * modules/fnmatch-h: Likewise.
-    * modules/getopt-posix: Likewise.
-    * modules/glob-h: Likewise.
-    * modules/iconv-h: Likewise.
-    * modules/ieee754-h: Likewise.
-    * modules/inttypes-incomplete: Likewise.
-    * modules/langinfo: Likewise.
-    * modules/libgmp: Likewise.
-    * modules/libtextstyle-optional: Likewise.
-    * modules/limits-h: Likewise.
-    * modules/locale: Likewise.
-    * modules/malloc-h: Likewise.
-    * modules/math: Likewise.
-    * modules/monetary: Likewise.
-    * modules/net_if: Likewise.
-    * modules/netdb: Likewise.
-    * modules/netinet_in: Likewise.
-    * modules/openmp-init: Likewise.
-    * modules/poll-h: Likewise.
-    * modules/posix-shell: Likewise.
-    * modules/pthread-h: Likewise.
-    * modules/pty: Likewise.
-    * modules/sched: Likewise.
-    * modules/scratch_buffer: Likewise.
-    * modules/search: Likewise.
-    * modules/selinux-h: Likewise.
-    * modules/signal-h: Likewise.
-    * modules/sigsegv: Likewise.
-    * modules/snippet/link-warning: Likewise.
-    * modules/spawn: Likewise.
-    * modules/stdalign: Likewise.
-    * modules/stdarg: Likewise.
-    * modules/stdbool: Likewise.
-    * modules/stddef: Likewise.
-    * modules/stdint: Likewise.
-    * modules/stdio: Likewise.
-    * modules/stdlib: Likewise.
-    * modules/stdnoreturn: Likewise.
-    * modules/string: Likewise.
-    * modules/strings: Likewise.
-    * modules/sys_file: Likewise.
-    * modules/sys_ioctl: Likewise.
-    * modules/sys_random: Likewise.
-    * modules/sys_resource: Likewise.
-    * modules/sys_select: Likewise.
-    * modules/sys_socket: Likewise.
-    * modules/sys_stat: Likewise.
-    * modules/sys_time: Likewise.
-    * modules/sys_times: Likewise.
-    * modules/sys_types: Likewise.
-    * modules/sys_uio: Likewise.
-    * modules/sys_utsname: Likewise.
-    * modules/sys_wait: Likewise.
-    * modules/sysexits: Likewise.
-    * modules/termios: Likewise.
-    * modules/threads-h: Likewise.
-    * modules/time: Likewise.
-    * modules/uchar: Likewise.
-    * modules/unicase/base: Likewise.
-    * modules/unicase/special-casing: Likewise.
-    * modules/uniconv/base: Likewise.
-    * modules/unictype/base: Likewise.
-    * modules/unigbrk/base: Likewise.
-    * modules/unilbrk/base: Likewise.
-    * modules/uniname/base: Likewise.
-    * modules/uninorm/base: Likewise.
-    * modules/unistd: Likewise.
-    * modules/unistdio/base: Likewise.
-    * modules/unistr/base: Likewise.
-    * modules/unitypes: Likewise.
-    * modules/uniwbrk/base: Likewise.
-    * modules/uniwidth/base: Likewise.
-    * modules/utime-h: Likewise.
-    * modules/wchar: Likewise.
-    * modules/wctype-h: Likewise.
-    * gnulib-tool (func_emit_lib_Makefile_am): When producing output for
-    GNU make, eliminate %reldir% tokens.
-
---------------------------------------------------------------------------------
-
 commit dc08febea1fc0a8c902dfa89d0abc2952873529b
 Author: Bruno Haible <br...@clisp.org>
 Date:   Mon Dec 13 03:03:13 2021 +0100
diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py
index 3545d98018..d4499df258 100644
--- a/pygnulib/GLEmiter.py
+++ b/pygnulib/GLEmiter.py
@@ -47,6 +47,23 @@ relinverse = constants.relinverse
 isfile = os.path.isfile
 normpath = os.path.normpath
 
+# Regular expressions used to convert Automake conditionals to GNU Make syntax.
+# Each tuple is the arguments given to re.sub in the correct order.
+_CONVERT_TO_GNU_MAKE = [
+    (re.compile(r'^if (.*)', re.MULTILINE), r'ifneq (,$(\1_CONDITION))'),
+    (re.compile(r'%reldir%/'), r''),
+    (re.compile(r'%reldir%'), r'.')
+]
+
+
+def _convert_to_gnu_make(snippet: str) -> str:
+    '''Convert a Automake snippet to GNU Make syntax.'''
+    if type(snippet) is not str:
+        raise TypeError(f'snippet must be a str, not {type(snippet).__name__}')
+    for regexp in _CONVERT_TO_GNU_MAKE:
+        snippet = re.sub(regexp[0], regexp[1], snippet)
+    return snippet
+
 
 #===============================================================================
 # Define GLEmiter class
@@ -651,7 +668,6 @@ AC_DEFUN([%V1%_LIBSOURCES], [
         module_indicator_prefix = self.config.getModuleIndicatorPrefix()
         ac_version = self.config['ac_version']
         destfile = os.path.normpath(destfile)
-        convert_to_gnu_make_1 = (re.compile(r'^if (.*)', re.MULTILINE), r'ifneq (,$(\1_CONDITION))')
         emit = ''
 
         # When using GNU make, or when creating an includable Makefile.am snippet,
@@ -733,14 +749,14 @@ AC_DEFUN([%V1%_LIBSOURCES], [
                             else:
                                 allsnippets += 'if %s\n' % name
                     if gnu_make:
-                        allsnippets += re.sub(convert_to_gnu_make_1[0], convert_to_gnu_make_1[1], amsnippet1)
+                        allsnippets += _convert_to_gnu_make(amsnippet1)
                     else:
                         allsnippets += amsnippet1
                     if conddeps:
                         if moduletable.isConditional(module):
                             allsnippets += 'endif\n'
                     if gnu_make:
-                        allsnippets += re.sub(convert_to_gnu_make_1[0], convert_to_gnu_make_1[1], amsnippet2)
+                        allsnippets += _convert_to_gnu_make(amsnippet2)
                     else:
                         allsnippets += amsnippet2
                     if gnu_make:
@@ -954,7 +970,6 @@ AC_DEFUN([%V1%_LIBSOURCES], [
         ac_version = self.config['ac_version']
         libtests = self.config['libtests']
         single_configure = self.config['single_configure']
-        convert_to_gnu_make_1 = (re.compile(r'^if (.*)', re.MULTILINE), r'ifneq (,$(\1_CONDITION))')
         emit = ''
 
         if libtool:
@@ -1034,14 +1049,14 @@ AC_DEFUN([%V1%_LIBSOURCES], [
                             else:
                                 snippet += 'if %s\n' % name
                     if gnu_make:
-                        snippet += re.sub(convert_to_gnu_make_1[0], convert_to_gnu_make_1[1], amsnippet1)
+                        snippet += _convert_to_gnu_make(amsnippet1)
                     else:
                         snippet += amsnippet1
                     if conddeps:
                         if moduletable.isConditional(module):
                             snippet += 'endif\n'
                     if gnu_make:
-                        snippet += re.sub(convert_to_gnu_make_1[0], convert_to_gnu_make_1[1], amsnippet2)
+                        snippet += _convert_to_gnu_make(amsnippet2)
                     else:
                         snippet += amsnippet2
                     if gnu_make:
-- 
2.44.0

Reply via email to