Subject: bug#23599: [PATCH 1/2 v2] texi: pass automatic -I to dvi & pdf generation

From: Mike Frysinger <[email-masked]>
Date: Thu, 27 Jan 2022 03:12:50 -0500

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

When generating info/html pages, automake adds -I flags to source
dirs that contain the texi files, but it doesn't do this for dvi or
pdf formats.  Instead, automake has been relying on texi2dvi to use
makeinfo for expanding macros, and it hasn't done that by default in
a long time.

Since adding --expand to the texi2dvi call is undesirable (due to bad
and unpredictable BEHAVIOR), pass those automatic -I flags directly
to TEXI2DVI & TEXI2PDF so they work regardless of --expand behavior.

We have to keep the MAKEINFO= setting around as texi2dvi might itself
fall back to it if the version of tex is old or broken.

* bin/automake.in: Add comment about $makeinfoflags usage.
* doc/automake.texi: Mention automatic -I subdir flags.
* lib/am/texibuild.am: Pass %MAKEINFOFLAGS% to TEXI2DVI & TEXI2PDF.
* t/txinfo-subdir-pr343.sh: Check for -I subdir usage.
---
 bin/automake.in          |  4 ++++
 doc/automake.texi        |  5 +++++
 lib/am/texibuild.am      | 16 ++++++++++------
 t/txinfo-subdir-pr343.sh |  5 +++++
 4 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/bin/automake.in b/bin/automake.in
index 58b589495e39..6d55884023cd 100644
--- a/bin/automake.in
+++ b/bin/automake.in
@@ -3135,6 +3135,10 @@ sub output_texinfo_build_rules
   # but just remember that some logic deeper in Automake will not
   # output the same rule twice); while the later need to be output for
   # each Texinfo source.
+  #
+  # NB: The makeinfoflags is currently passed to makeinfo and texi2dvi, so
+  # make sure that it only utilizes compatible flags.  Best to stick to -I.
+  # Changing this requires updating lib/am/texibuild.am.
   my $generic;
   my $makeinfoflags;
   my $sdir = dirname $source;
diff --git a/doc/automake.texi b/doc/automake.texi
index 67827cf8698a..c94b9f528c11 100644
--- a/doc/automake.texi
+++ b/doc/automake.texi
@@ -8012,6 +8012,11 @@ the generated @file{.info} files are by default placed in @code{srcdir}
 rather than in the @code{builddir}.  This can be changed with the
 @option{info-in-builddir} option.
 
+If the Texinfo sources are in a subdirectory relative to the Makefile, then
+@code{-I} flags for the subdirectory, both in the source directory and in the
+build directory, will automatically be added.  There is no need to specify
+these in @samp{$(MAKEINFO)}, @samp{$(MAKEINFOFLAGS)}, etc...
+
 @trindex dvi
 @trindex html
 @trindex pdf
diff --git a/lib/am/texibuild.am b/lib/am/texibuild.am
index e3d63087a4f9..c9c4f92d82cb 100644
--- a/lib/am/texibuild.am
+++ b/lib/am/texibuild.am
@@ -63,7 +63,9 @@ INFO_DEPS += %DEST_INFO_PREFIX%%DEST_SUFFIX%
 ?!GENERIC?%DEST_PREFIX%.dvi: %SOURCE% %DEPS% %DIRSTAMP%
 	%AM_V_TEXI2DVI%TEXINPUTS="$(am__TEXINFO_TEX_DIR)$(PATH_SEPARATOR)$$TEXINPUTS" \
 ## Must set MAKEINFO like this so that version.texi will be found even
-## if it is in srcdir (-I $(srcdir) is set in %MAKEINFOFLAGS%).
+## if it is in srcdir (-I $(srcdir) is set in %MAKEINFOFLAGS%), and in case
+## texi2dvi automatically falls back to using makeinfo for expanding (-E).
+## If texi2dvi doesn't fall back, we also pass %MAKEINFOFLAGS% directly below.
 	MAKEINFO='$(MAKEINFO) $(AM_MAKEINFOFLAGS) $(MAKEINFOFLAGS) %MAKEINFOFLAGS%' \
 ## texi2dvi doesn't silence everything with -q, redirect to /dev/null instead.
 ## We still want -q (%TEXIQUIET%) because it turns on batch mode.
@@ -71,10 +73,10 @@ INFO_DEPS += %DEST_INFO_PREFIX%%DEST_SUFFIX%
 ## by-products are left in there, instead of cluttering the current
 ## directory (see automake bug#11146).  Use a different build-dir for
 ## each file (and distinct from that of the corresponding PDF file) to
-## avoid hitting a Texinfop bug that could cause low-probability racy
+## avoid hitting a Texinfo bug that could cause low-probability racy
 ## failure when doing parallel builds; see:
 ## https://lists.gnu.org/archive/html/automake-patches/2012-06/msg00073.html
-	$(TEXI2DVI) %TEXIQUIET% --build-dir=$(@:.dvi=.t2d) -o $@ %TEXIDEVNULL% \
+	$(TEXI2DVI) %MAKEINFOFLAGS% %TEXIQUIET% --build-dir=$(@:.dvi=.t2d) -o $@ %TEXIDEVNULL% \
 ?GENERIC?	%SOURCE%
 ?!GENERIC?	`test -f '%SOURCE%' || echo '$(srcdir)/'`%SOURCE%
 
@@ -82,7 +84,9 @@ INFO_DEPS += %DEST_INFO_PREFIX%%DEST_SUFFIX%
 ?!GENERIC?%DEST_PREFIX%.pdf: %SOURCE% %DEPS% %DIRSTAMP%
 	%AM_V_TEXI2PDF%TEXINPUTS="$(am__TEXINFO_TEX_DIR)$(PATH_SEPARATOR)$$TEXINPUTS" \
 ## Must set MAKEINFO like this so that version.texi will be found even
-## if it is in srcdir (-I $(srcdir) is set in %MAKEINFOFLAGS%).
+## if it is in srcdir (-I $(srcdir) is set in %MAKEINFOFLAGS%), and in case
+## texi2dvi automatically falls back to using makeinfo for expanding (-E).
+## If texi2dvi doesn't fall back, we also pass %MAKEINFOFLAGS% directly below.
 	MAKEINFO='$(MAKEINFO) $(AM_MAKEINFOFLAGS) $(MAKEINFOFLAGS) %MAKEINFOFLAGS%' \
 ## texi2pdf doesn't silence everything with -q, redirect to /dev/null instead.
 ## We still want -q (%TEXIQUIET%) because it turns on batch mode.
@@ -90,10 +94,10 @@ INFO_DEPS += %DEST_INFO_PREFIX%%DEST_SUFFIX%
 ## by-products are left in there, instead of cluttering the current
 ## directory (see automake bug#11146).  Use a different build-dir for
 ## each file (and distinct from that of the corresponding DVI file) to
-## avoid hitting a Texinfop bug that could cause low-probability racy
+## avoid hitting a Texinfo bug that could cause low-probability racy
 ## failure when doing parallel builds; see:
 ## https://lists.gnu.org/archive/html/automake-patches/2012-06/msg00073.html
-	$(TEXI2PDF) %TEXIQUIET% --build-dir=$(@:.pdf=.t2p) -o $@ %TEXIDEVNULL% \
+	$(TEXI2PDF) %MAKEINFOFLAGS% %TEXIQUIET% --build-dir=$(@:.pdf=.t2p) -o $@ %TEXIDEVNULL% \
 ?GENERIC?	%SOURCE%
 ?!GENERIC?	`test -f '%SOURCE%' || echo '$(srcdir)/'`%SOURCE%
 
diff --git a/t/txinfo-subdir-pr343.sh b/t/txinfo-subdir-pr343.sh
index 625a4faa7f89..085d53e2ba29 100644
--- a/t/txinfo-subdir-pr343.sh
+++ b/t/txinfo-subdir-pr343.sh
@@ -57,4 +57,9 @@ $MAKE distcheck
 test -f ../subdir/main.info
 test ! -e subdir/main.info
 
+# Make sure automatic -I flags were added for the subdir.
+grep '\$(MAKEINFO).* -I subdir ' ../Makefile.in
+grep '\$(TEXI2DVI).* -I subdir ' ../Makefile.in
+grep '\$(TEXI2PDF).* -I subdir ' ../Makefile.in
+
 :
-- 
2.34.1