Subject: bug#12495: AC_CONFIG_HEADERS with

From: Stefano Lattarini <[email-masked]>
Date: Sat, 29 Sep 2012 20:18:11 +0200

tags 12495 + patch
thanks

On 09/28/2012 09:40 AM, Stefano Lattarini wrote:
>
> Now I understand your objections, and I agree that the current Automake
> behaviour is a bug (albeit a minor one).  I'll commit a fix in the next
> days.
>
Here is the patch finally.  I will commit it in a couple of days if there
are no objections.  As usual, comments are welcome.

Thanks,
  Stefano

---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----

>From 21bcf5bf85908ac98ff131467f78d584579870ea Mon Sep 17 00:00:00 2001
Message-Id: <21bcf5bf8[email-masked]>
From: Stefano Lattarini <[email-masked]>
Date: Fri, 28 Sep 2012 21:27:41 +0200
Subject: [PATCH] config headers: don't emit rules for headers not generated
 by autoheader

This change fixed automake bug#12495.

Even if an AC_CONFIG_HEADERS invocation is passed a list of several files
as the first argument, only the first one of those file is considered by
autoheader for automatic generation of the corresponding '.in' template.
This is done on purpose, and is clearly documented in the Autoconf manual,
which (as of the 2.69 version) reads something like this:

    The autoheader program searches for the first invocation of
    AC_CONFIG_HEADERS in configure sources to determine the name of
    the template.  If the first call of AC_CONFIG_HEADERS specifies
    more than one input file name, autoheader uses the first one.

That is, an invocation like:

    AC_CONFIG_HEADERS([config.h config2.h])

should cause autoheader to generate only a 'config.h.in' template,
and not also a 'config2.h.in' one.

Accordingly, automake, when tracing AC_CONFIG_HEADERS, should generate
remake rules only for the template associated to the first input file
name passed to that macro.  In some situations, however, automake failed
to properly limit itself in this way; for example, with an input like:

    AC_CONFIG_HEADERS([config.h sub/foo.h])

in configure.ac, and with the 'sub' directory listed in the SUBDIRS
variable of the top-level Makefile, automake would erroneously generate
in 'sub/Makefile.in' a rule to remake the 'foo.h.in' template by
invoking autoheader.

This issue was likely introduced in commit 'Release-1-8-23-g262bb92'
of 2004-01-05.

* NEWS: Update.
* doc/automake.texi (Optional): Improve wording in the description of
hat rules automake generates in response to an 'AC_CONFIG_HEADERS'
invocation.
* lib/am/remake-hdr.am: Only emit autoheader-invoking remake rules for
the %CONFIG_HIN% template if that corresponds to the first argument of
AC_CONFIG_HEADERS, as explaned above.  Do so using the automake-time
conditional %?FIRST-HDR%, that is properly passed ...
* automake.in (handle_configure): ... from a 'file_contents' invocation
in here.
* t/autohdr-subdir-pr12495.sh: New test.
* t/list-of-tests.mk: Add it.
* THANKS: Update.

Helped-by: Hib Eris <[email-masked]>
Signed-off-by: Stefano Lattarini <[email-masked]>
---
 NEWS                        | 14 +++++++-
 THANKS                      |  1 +
 automake.in                 |  1 +
 doc/automake.texi           |  8 +++--
 lib/am/remake-hdr.am        |  4 +--
 t/autohdr-subdir-pr12495.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++
 t/list-of-tests.mk          |  1 +
 7 files changed, 103 insertions(+), 6 deletions(-)
 create mode 100755 t/autohdr-subdir-pr12495.sh

diff --git a/NEWS b/NEWS
index d67407f..aaa3ad3 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-New in 1.12.4:
+New in 1.12.5:

 * WARNING: Future backward-incompatibilities!

@@ -61,6 +61,18 @@ New in 1.12.4:
     giving more useful warnings than a bare "command not found" from a
     make recipe would.

+Bugs fixed in 1.12.5:
+
+* Long-standing bugs:
+
+  - Automake no longer generates spurious remake rules invoking autoheader
+    to regenerate the template corresponding to header files specified after
+    the first one in AC_CONFIG_HEADERS (automake bug#12495).
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+New in 1.12.4:
+
 * Warnings and deprecations:

   - Warnings in the 'obsolete' category are enabled by default both in
diff --git a/THANKS b/THANKS
index ca95db8..88f539c 100644
--- a/THANKS
+++ b/THANKS
@@ -143,6 +143,7 @@ Harald Dunkel                   [email-masked]
 Harlan Stenn                    [email-masked]
 He Li                           [email-masked]
 Henrik Frystyk Nielsen          [email-masked]
+Hib Eris                        [email-masked]
 Ian Lance Taylor                [email-masked]
 Ignacy Gawedzki                 [email-masked]
 Илья Н. Голубев                 [email-masked]
diff --git a/automake.in b/automake.in
index 8c8b127..944b985 100644
--- a/automake.in
+++ b/automake.in
@@ -4225,6 +4225,7 @@ sub handle_configure ($$$@)
 	    file_contents ('remake-hdr',
 			   new Automake::Location,
 			   FILES            => "@files",
+			   'FIRST-HDR'      => ($hdr_index == 1),
 			   CONFIG_H         => $cn_sans_dir,
 			   CONFIG_HIN       => $ins[0],
 			   CONFIG_H_DEPS    => "@ins",
diff --git a/doc/automake.texi b/doc/automake.texi
index 0838822..914c1e8 100644
--- a/doc/automake.texi
+++ b/doc/automake.texi
@@ -2979,9 +2979,11 @@ Automake will require the sources file declared with
 macro.

 @item AC_CONFIG_HEADERS
-Automake will generate rules to rebuild these headers.  Older versions
-of Automake required the use of @code{AM_CONFIG_HEADER}
-(@pxref{Macros}); this is no longer the case.
+Automake will generate rules to rebuild these headers from the
+corresponding templates (usually, the template for a @file{foo.h}
+header being @file{foo.h.in}).  Older versions of Automake required
+the use of @code{AM_CONFIG_HEADER} (@pxref{Macros}); this is no
+longer the case.

 As with @code{AC_CONFIG_FILES} (@pxref{Requirements}), parts of the
 specification using shell variables will be ignored as far as
diff --git a/lib/am/remake-hdr.am b/lib/am/remake-hdr.am
index f61400a..3c7e346 100644
--- a/lib/am/remake-hdr.am
+++ b/lib/am/remake-hdr.am
@@ -30,7 +30,7 @@

 ## Only the first file of AC_CONFIG_HEADERS is assumed to be generated
 ## by autoheader.
-if %?FIRST%
+if %?FIRST-HDR%
 %CONFIG_HIN%: %MAINTAINER-MODE% $(am__configure_deps) %FILES%
 ## Cater to parallel BSD make.
 	($(am__cd) $(top_srcdir) && $(AUTOHEADER))
@@ -71,4 +71,4 @@ if %?FIRST%
 ## by config.status, there is no reason to make things complex for
 ## config.hin.
 	touch $@
-endif %?FIRST%
+endif %?FIRST-HDR%
diff --git a/t/autohdr-subdir-pr12495.sh b/t/autohdr-subdir-pr12495.sh
new file mode 100755
index 0000000..77d2522
--- /dev/null
+++ b/t/autohdr-subdir-pr12495.sh
@@ -0,0 +1,80 @@
+#! /bin/sh
+# Copyright (C) 2012 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Related to automake bug#12495: Automake shouldn't generate useless
+# remake rules for AC_CONFIG_HEADERS arguments after the first one,
+# not even when subdirs are involved.
+
+. ./defs || exit 1
+
+cat >> configure.ac << 'END'
+AC_CONFIG_HEADERS([a.h b.h sub/c.h])
+AC_CONFIG_FILES([sub/Makefile])
+AC_OUTPUT
+END
+
+mkdir sub
+echo SUBDIRS = sub > Makefile.am
+: > sub/Makefile.am
+
+$ACLOCAL
+$AUTOCONF
+$AUTOHEADER
+# Even if an AC_CONFIG_HEADERS invocation is passed several files in
+# the first argument, only the first one is considered by autoheader
+# for automatic generation.  Otherwise, the present test case would
+test -f a.h.in && test ! -f c.h.in && test ! -f sub/c.h.in \
+  || fatal_ "unexpected autoheader behavior with multiple" \
+            "AC_CONFIG_HEADERS arguments"
+# Automake should require the missing headers though.
+AUTOMAKE_fails -Wno-error -Wnone
+grep "^configure\.ac:4:.* required file 'b.h.in' not found" stderr
+grep "^configure\.ac:4:.* required file 'sub/c.h.in' not found" stderr
+: > b.h.in
+: > sub/c.h.in
+$AUTOMAKE
+
+./configure
+
+# Automake should regenerate this.
+grep '^$(srcdir)/a\.h\.in:' Makefile.in
+# But not these.
+grep '[bc]\.h\.in.*:' Makefile.in sub/Makefile.in && exit 1
+
+test -f a.h && test -f b.h && test -f sub/c.h \
+  || fatal_ "unexpected ./configure behavior with multiple" \
+            "AC_CONFIG_HEADERS arguments"
+
+rm -f a.h.in a.h
+$MAKE
+test -f a.h.in
+test -f a.h
+
+ocwd=$(pwd)
+for x in b c; do
+  test $x = b || cd sub
+  rm -f $x.h.in
+  $MAKE $x.h.in 2>stderr && { cat stderr >&2; exit 1; }
+  cat stderr >&2
+  test ! -f $x.h.in
+  if using_gmake; then
+    grep "No rule to make target [\`\"']$x\.h\.in[\`\"']" stderr
+  fi
+  : > $x.h.in
+  cd "$ocwd" || fatal_ "cannot chdir back"
+done
+
+:
diff --git a/t/list-of-tests.mk b/t/list-of-tests.mk
index 6effe77..b3ff6b2 100644
--- a/t/list-of-tests.mk
+++ b/t/list-of-tests.mk
@@ -158,6 +158,7 @@ t/autohdr.sh \
 t/autohdr2.sh \
 t/autohdr3.sh \
 t/autohdr4.sh \
+t/autohdr-subdir-pr12495.sh \
 t/autohdrdry.sh \
 t/automake-cmdline.tap \
 t/auxdir.sh \
-- 
1.7.12.317.g1c54b74