Subject: Re: bug#9546: parallel-tests: auxiliary script `test-driver' is not distributed when TESTS is defined in a subdir Makefile (regression in branch test-protocols)

From: Stefano Lattarini <[email-masked]>
Date: Sat, 1 Oct 2011 22:25:22 +0200

On Sunday 18 September 2011, Stefano Lattarini wrote:
> On Sunday 18 September 2011, Stefano Lattarini wrote:
> > Severity: serious
> > 
> > The bug is demonstrated by the attached test script.
> > I hope I'll be able to take a look into it soonish.
> > 
> The test script has a couple of bugs, which I've now fixed.  Attached
> is the patch I've pushed to expose the bug.
> 
OK, I've a patch that fixes the bug (see attachment).

But wait to rejoice, since the patch also introduces new failures when
"threaded automake" is in use (AUTOMAKE_JOBS > 1).  The reason is that
automake needs to serialize file installions spawned by `--add-missing'
(and if I understand correctly, this isn't for cosmetic reasons, but is
needed to avoid possible race conditions and botched output files).
Currently the code that installs required files is intertwined with the
code that declares the DIST_COMMON variables; so, upon deserialization,
the definition of DIST_COMMON gets emitted in the wrong Makefile.in
(ouch!).

I hope I'll have time and patience to look into this soonish; anyway,
suggestions on how to minimally fix the new problem would be very
welcome (the best & long-term fix would probably entail a quite heavy
refactoring of automake, which, altough defintely desirable, would
require a lot of time and testing ... so not going to happen soon,
sigh).

Regards,
  Stefano
From 640dc492560940aa7487f6879ff359c649e579d5 Mon Sep 17 00:00:00 2001
Message-Id: <640dc4925[email-masked]>
From: Stefano Lattarini <[email-masked]>
Date: Fri, 30 Sep 2011 12:12:43 +0200
Subject: [PATCH] dist: auxiliary files can be distributed from subdir Makefiles

---
 ChangeLog                             |   35 ++++++++
 Makefile.in                           |    7 +-
 automake.in                           |   11 +++
 doc/Makefile.in                       |    3 +-
 lib/Automake/Makefile.in              |    2 +-
 lib/Automake/tests/Makefile.in        |    3 +-
 lib/am/Makefile.in                    |    2 +-
 lib/am/distdir.am                     |    1 +
 m4/Makefile.in                        |    3 +-
 tests/Makefile.am                     |    5 +-
 tests/Makefile.in                     |   10 ++-
 tests/dist-auxdir-many-subdirs.test   |  142 +++++++++++++++++++++++++++++++++
 tests/dist-readonly.test              |   60 ++++++++++++++
 tests/dist-repeated.test              |  102 +++++++++++++++++++++++
 tests/distcom-subdir.test             |   72 +++++++++++++++++
 tests/java-compile-run-nested.test    |    2 +-
 tests/test-driver-is-distributed.test |   44 +++++++++-
 17 files changed, 487 insertions(+), 17 deletions(-)
 create mode 100755 tests/dist-auxdir-many-subdirs.test
 create mode 100755 tests/dist-readonly.test
 create mode 100755 tests/dist-repeated.test
 create mode 100755 tests/distcom-subdir.test

diff --git a/ChangeLog b/ChangeLog
index 7a78db6..a30326c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,38 @@
+2011-09-30  Stefano Lattarini  <[email-masked]>
+
+	dist: auxiliary files can be distributed from subdir Makefiles
+	With this change, we make it possible for subdir Makefile.am
+	files to distribute files in the config auxdir; while this means
+	that some files might be copied multiple times, it simplify some
+	logic in the automake script, and fix at least one important bug.
+	In fact, before this change, the auxiliary script `test-driver'
+	was not being distributed as expected when TESTS was defined only
+	in a subdir Makefile (which is a pretty common setups indeed).
+	Now this does not happen anymore: so this change fixes automake
+	bug#9546.
+	*** FIXME *** however, this changes breaks "threaded automake";
+	*** FIXME *** this is due to the fact that queue_required_conf_file
+	*** FIXME *** relies on the internals of require_file_internal and
+	*** FIXME *** maybe_push_required_file, in that it exploits the
+	*** FIXME *** fact that only the contents of the last handled
+	*** FIXME *** output file may be impacted (which in turn is dealt
+	*** FIXME *** with by the master thread) -- but this fact is not
+	*** FIXME *** true anymore after the present change ...
+	* automake.in (maybe_push_required_file): Add ad-hoc handling for
+	the case where the directory part of the required file is different
+	from the subdir where the current Makefile.am resides, but is equal
+	to the "config-aux directory" ($config_auxdir).  This is needed to
+	allow a construct in a non-top-level Makefile.am to require a file
+	in the config-aux directory.
+	* tests/test-driver-is-distributed.test: Extend and adjust.  This
+	test now passes.
+	* tests/java-compile-run-nested.test: Likewise.
+	* tests/distcom-subdir.test: New test.
+	* tests/dist-readonly.test: Likewise.
+	* tests/dist-repeated.test: Likewise.
+	* tests/dist-auxdir-many-subdirs.test: Likewise.
+	* tests/Makefile.am (TESTS, XFAIL_TESTS): Update.
+
 2011-09-28  Stefano Lattarini  <[email-masked]>
 
 	tap/awk: improve comments about Korn shell signal handling issues
diff --git a/Makefile.in b/Makefile.in
index 6e03d9d..ead365d 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -52,8 +52,11 @@ POST_UNINSTALL = :
 build_triplet = @build@
 subdir = .
 DIST_COMMON = README $(am__configure_deps) $(srcdir)/Makefile.am \
-	$(srcdir)/Makefile.in $(top_srcdir)/configure AUTHORS COPYING \
-	ChangeLog INSTALL NEWS THANKS TODO
+	$(srcdir)/Makefile.in $(top_srcdir)/configure \
+	$(top_srcdir)/lib/config.guess $(top_srcdir)/lib/config.sub \
+	$(top_srcdir)/lib/install-sh $(top_srcdir)/lib/missing \
+	$(top_srcdir)/lib/mkinstalldirs AUTHORS COPYING ChangeLog \
+	INSTALL NEWS THANKS TODO
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
 	$(top_srcdir)/m4/auxdir.m4 $(top_srcdir)/m4/init.m4 \
diff --git a/automake.in b/automake.in
index 91ae825..9efc87b 100644
--- a/automake.in
+++ b/automake.in
@@ -7588,6 +7588,17 @@ sub maybe_push_required_file
       push_dist_common ($file);
       return 1;
     }
+  # This is needed to allow a construct in a non-top-level Makefile.am
+  # to require a file in the build-aux directory (see at least the test
+  # script `test-driver-is-distributed.test').  This is related to the
+  # automake bug#9546.  Note that the use of $config_aux_dir instead
+  # of $am_config_aux_dir here is deliberate and necessary.
+  elsif ($dir eq $config_aux_dir)
+    {
+      # FIXME: this breaks serialization of threaded automake :-(
+      push_dist_common ("$am_config_aux_dir/$file");
+      return 1;
+    }
   elsif ($relative_dir eq '.' && ! &is_make_dir ($dir))
     {
       # If we are doing the topmost directory, and the file is in a
diff --git a/doc/Makefile.in b/doc/Makefile.in
index b0d06f2..ab3e552 100644
--- a/doc/Makefile.in
+++ b/doc/Makefile.in
@@ -52,7 +52,8 @@ subdir = doc
 DIST_COMMON = $(automake_TEXINFOS) $(dist_doc_DATA) $(dist_man1_MANS) \
 	$(dist_noinst_DATA) $(srcdir)/Makefile.am \
 	$(srcdir)/Makefile.in $(srcdir)/stamp-vti \
-	$(srcdir)/version.texi
+	$(srcdir)/version.texi $(top_srcdir)/lib/mdate-sh \
+	$(top_srcdir)/lib/mkinstalldirs $(top_srcdir)/lib/texinfo.tex
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
 	$(top_srcdir)/m4/auxdir.m4 $(top_srcdir)/m4/init.m4 \
diff --git a/lib/Automake/Makefile.in b/lib/Automake/Makefile.in
index 2ea1a18..b83778b 100644
--- a/lib/Automake/Makefile.in
+++ b/lib/Automake/Makefile.in
@@ -51,7 +51,7 @@ POST_UNINSTALL = :
 build_triplet = @build@
 subdir = lib/Automake
 DIST_COMMON = $(dist_perllib_DATA) $(srcdir)/Makefile.am \
-	$(srcdir)/Makefile.in
+	$(srcdir)/Makefile.in $(top_srcdir)/lib/mkinstalldirs
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
 	$(top_srcdir)/m4/auxdir.m4 $(top_srcdir)/m4/init.m4 \
diff --git a/lib/Automake/tests/Makefile.in b/lib/Automake/tests/Makefile.in
index 3e5def5..ead6233 100644
--- a/lib/Automake/tests/Makefile.in
+++ b/lib/Automake/tests/Makefile.in
@@ -48,7 +48,8 @@ PRE_UNINSTALL = :
 POST_UNINSTALL = :
 build_triplet = @build@
 subdir = lib/Automake/tests
-DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
+DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in \
+	$(top_srcdir)/lib/mkinstalldirs $(top_srcdir)/lib/test-driver
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
 	$(top_srcdir)/m4/auxdir.m4 $(top_srcdir)/m4/init.m4 \
diff --git a/lib/am/Makefile.in b/lib/am/Makefile.in
index 53ce53d..050806a 100644
--- a/lib/am/Makefile.in
+++ b/lib/am/Makefile.in
@@ -51,7 +51,7 @@ POST_UNINSTALL = :
 build_triplet = @build@
 subdir = lib/am
 DIST_COMMON = $(dist_am_DATA) $(srcdir)/Makefile.am \
-	$(srcdir)/Makefile.in
+	$(srcdir)/Makefile.in $(top_srcdir)/lib/mkinstalldirs
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
 	$(top_srcdir)/m4/auxdir.m4 $(top_srcdir)/m4/init.m4 \
diff --git a/lib/am/distdir.am b/lib/am/distdir.am
index 29f2d7f..5d62dfe 100644
--- a/lib/am/distdir.am
+++ b/lib/am/distdir.am
@@ -216,6 +216,7 @@ endif %?TOPDIR_P%
 ## Test for file existence because sometimes a file gets included in
 ## DISTFILES twice.  For example this happens when a single source
 ## file is used in building more than one program.
+## See also test `dist-repeated.test'.
 	    test -f "$(distdir)/$$file" \
 	    || cp -p $$d/$$file "$(distdir)/$$file" \
 	    || exit 1; \
diff --git a/m4/Makefile.in b/m4/Makefile.in
index 8bc9b70..66ca138 100644
--- a/m4/Makefile.in
+++ b/m4/Makefile.in
@@ -51,7 +51,8 @@ POST_UNINSTALL = :
 build_triplet = @build@
 subdir = m4
 DIST_COMMON = $(dist_automake_ac_DATA) $(dist_system_ac_DATA) \
-	$(srcdir)/Makefile.am $(srcdir)/Makefile.in
+	$(srcdir)/Makefile.am $(srcdir)/Makefile.in \
+	$(top_srcdir)/lib/mkinstalldirs
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
 	$(top_srcdir)/m4/auxdir.m4 $(top_srcdir)/m4/init.m4 \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 748c6ed..261f33b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -33,7 +33,6 @@ java-nobase.test \
 pr8365-remake-timing.test \
 yacc-dist-nobuild-subdir.test \
 vala-vpath.test \
-test-driver-is-distributed.test \
 txinfo5.test

@@ -401,6 +400,9 @@ dirlist.test \
 dirlist2.test \
 discover.test \
 dist-included-parent-dir.test \
+dist-readonly.test \
+dist-repeated.test \
+dist-auxdir-many-subdirs.test \
 distcleancheck.test \
 distcom2.test \
 distcom3.test \
@@ -408,6 +410,7 @@ distcom4.test \
 distcom5.test \
 distcom6.test \
 distcom7.test \
+distcom-subdir.test \
 distdir.test \
 distlinks.test \
 distlinksbrk.test \
diff --git a/tests/Makefile.in b/tests/Makefile.in
index 90160d2..7cfadc6 100644
--- a/tests/Makefile.in
+++ b/tests/Makefile.in
@@ -51,7 +51,8 @@ build_triplet = @build@
 DIST_COMMON = README $(srcdir)/Makefile.am $(srcdir)/Makefile.in \
 	$(srcdir)/aclocal.in $(srcdir)/automake.in $(srcdir)/defs \
 	$(srcdir)/defs-static.in $(srcdir)/instspc-tests.am \
-	$(srcdir)/parallel-tests.am
+	$(srcdir)/parallel-tests.am $(top_srcdir)/lib/mkinstalldirs \
+	$(top_srcdir)/lib/test-driver
 subdir = tests
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/m4/amversion.m4 \
@@ -296,8 +297,7 @@ EXTRA_DIST = ChangeLog-old gen-parallel-tests instspc-tests.sh \
 XFAIL_TESTS = all.test auxdir2.test cond17.test gcj6.test \
 	override-conditional-2.test java-nobase.test \
 	pr8365-remake-timing.test yacc-dist-nobuild-subdir.test \
-	vala-vpath.test test-driver-is-distributed.test txinfo5.test \
-	$(instspc_xfail_tests)
+	vala-vpath.test txinfo5.test $(instspc_xfail_tests)
 parallel_tests = check-concurrency-bug9245-p.test \
 	check-exported-srcdir-p.test check-fd-redirect-p.test \
 	check-tests-in-builddir-p.test check-p.test check11-p.test \
@@ -665,6 +665,9 @@ dirlist.test \
 dirlist2.test \
 discover.test \
 dist-included-parent-dir.test \
+dist-readonly.test \
+dist-repeated.test \
+dist-auxdir-many-subdirs.test \
 distcleancheck.test \
 distcom2.test \
 distcom3.test \
@@ -672,6 +675,7 @@ distcom4.test \
 distcom5.test \
 distcom6.test \
 distcom7.test \
+distcom-subdir.test \
 distdir.test \
 distlinks.test \
 distlinksbrk.test \
diff --git a/tests/dist-auxdir-many-subdirs.test b/tests/dist-auxdir-many-subdirs.test
new file mode 100755
index 0000000..4f32f64
--- /dev/null
+++ b/tests/dist-auxdir-many-subdirs.test
@@ -0,0 +1,142 @@
+#! /bin/sh
+# Copyright (C) 2011 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/>.
+
+# It should be legitimate for many subdir Makefiles to require the
+# same config-aux files.
+
+am_create_testdir=empty
+parallel_tests=yes
+required=cc
+. ./defs || Exit 1
+
+count=0
+ocwd=`pwd` || fatal_ "cannot get current working directory"
+
+# Usage: do_check [--add-missing] [CONFIG-AUXDIR-PATH=.]
+do_check ()
+{
+  case $1 in
+    -a|--add-missing) add_missing=yes; shift;;
+    *) add_missing=no;;
+  esac
+  auxdir=${1-.}
+
+  count=`expr $count + 1`
+  mkdir T$count.d
+  cd T$count.d
+
+  distdir=$me-$count
+  unindent > configure.in << END
+    AC_INIT([$me], [$count])
+    AC_CONFIG_AUX_DIR([$auxdir])
+    AM_INIT_AUTOMAKE([parallel-tests])
+    AC_PROG_CC
+    # We don't want to require python or emcas in this test, so
+    # the tricks below.
+    AM_PATH_PYTHON([2.2], [], [:])
+    EMACS=no; AM_PATH_LISPDIR
+    AC_CONFIG_FILES([Makefile])
+END
+
+  unindent > Makefile.stub <<'END'
+    ## For depcomp.
+    bin_PROGRAMS = foo
+    foo_SOURCES = foo.c
+    ## For py-compile.
+    python_PYTHON = bar.py
+    ## For elisp-comp.
+    lisp_LISP = baz.el
+    ## For test-driver.
+    TESTS =
+END
+
+  required_files='
+    install-sh
+    missing
+    depcomp
+    py-compile
+    elisp-comp
+    test-driver
+  '
+
+  echo "SUBDIRS =" > Makefile.am
+
+  suffixes='0 1 2 3 4 5 6 7 8 9'
+
+  for x in $suffixes; do
+    mkdir sub$x
+    echo "SUBDIRS += sub$x" >> Makefile.am
+    echo "AC_CONFIG_FILES([sub$x/Makefile])" >> configure.in
+    cp Makefile.stub sub$x/Makefile.am
+    echo 'int main (void) { return 0; }' > sub$x/foo.c
+    touch sub$x/bar.py sub$x/baz.el
+  done
+  echo AC_OUTPUT >> configure.in
+
+  $ACLOCAL
+  $AUTOCONF
+
+  # FIXME: this is not good for installcheck; fix after merging
+  # into testsuite-work.
+  "$top_testsrcdir"/lib/install-sh -d $auxdir
+  if test $add_missing = yes; then
+    $AUTOMAKE -a --copy
+    for f in $required_files; do
+      test -f $auxdir/$f
+      # To ensure that if a auxiliary file is required and distributed
+      # by many Makefiles, the "dist" rule won't try to copy it multiple
+      # times in $(distdir).
+      chmod a-w $auxdir/$f
+    done
+  else
+    for f in $required_files; do
+      # FIXME: this is not good for installcheck; fix after merging
+      # into testsuite-work.
+      cp "$top_testsrcdir"/lib/$f $auxdir/$f
+      # See comments above.
+      chmod a-w $auxdir/$f
+    done
+    $AUTOMAKE
+  fi
+
+  for vpath in : false; do
+    if $vpath; then
+      mkdir build
+      cd build
+      srcdir=..
+    else
+      srcdir=.
+    fi
+    $srcdir/configure
+    $MAKE distdir
+    find $distdir # For debugging.
+    for f in $required_files; do
+      test -f $distdir/$auxdir/$f
+    done
+    cd $srcdir
+  done
+
+  cd "$ocwd" || fatal_ "cannot chdir back to '$ocwd'"
+}
+
+do_check .
+do_check --add-missing .
+do_check build-aux
+do_check --add-missing build-aux
+do_check a/b/c
+do_check --add-missing a/b/c
+
+:
diff --git a/tests/dist-readonly.test b/tests/dist-readonly.test
new file mode 100755
index 0000000..3b558c9
--- /dev/null
+++ b/tests/dist-readonly.test
@@ -0,0 +1,60 @@
+#! /bin/sh
+# Copyright (C) 2011 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/>.
+
+# Test that a readonly files are distributed as such, and not make
+# writable while being copied in the $(distdir).
+
+required=cc
+. ./defs || Exit 1
+
+cat >> configure.in << 'END'
+AC_PROG_CC
+AC_OUTPUT
+END
+
+cat > Makefile.am << 'END'
+bin_PROGRAMS = foo
+foo_SOURCES = foo.c
+EXTRA_DIST = bar.txt
+check-local: test
+.PHONY: test
+test:
+	test -f $(srcdir)/foo.c && test ! -w $(srcdir)/foo.c
+	if (echo x > $(srcdir)/foo.c); then exit 1; else :; fi
+	grep 'main (void)' $(srcdir)/foo.c
+	test -f $(srcdir)/bar.txt && test ! -w $(srcdir)/bar.txt
+	if (echo x > $(srcdir)/bar.txt); then exit 1; else :; fi
+	grep 'To be, or not to be' $(srcdir)/bar.txt
+END
+
+echo 'int main (void) { return 0; }' > foo.c
+echo To be, or not to be ... > bar.txt
+chmod a-w foo.c bar.txt
+
+$ACLOCAL
+$AUTOCONF
+$AUTOMAKE
+
+./configure
+$MAKE distdir
+ls -l $distdir # For debugging.
+test -f foo.c && test ! -w foo.c || Exit 1
+(echo x > foo.c) && Exit 1
+test -f bar.txt && test ! -w bar.txt || Exit 1
+(echo x > bar.txt) && Exit 1
+$MAKE distcheck
+
+:
diff --git a/tests/dist-repeated.test b/tests/dist-repeated.test
new file mode 100755
index 0000000..7dd8bdd
--- /dev/null
+++ b/tests/dist-repeated.test
@@ -0,0 +1,102 @@
+#! /bin/sh
+# Copyright (C) 2011 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/>.
+
+# Test that we can distribute the same file as many times as we want.
+# The distdir target should take care of not copying it more than one
+# time anyway.
+
+. ./defs || Exit 1
+
+echo AC_OUTPUT >> configure.in
+
+cat > Makefile.am <<'END'
+bin_PROGRAMS = foo bar
+foo_SOURCES = foo.c
+bar_SOURCES = foo.c
+python_PYTHON = bar.py
+EXTRA_DIST = foo.c bar.py
+
+.PHONY: sanity-check
+sanity-check:
+	for f in $(DISTFILES); do echo " $$f "; done > dist.txt
+	cat dist.txt
+	test `grep ' foo\.c '  dist.txt | wc -l` -eq 3
+	test `grep ' bar\.py ' dist.txt | wc -l` -eq 2
+
+# So that we don't have to require a C compiler.
+AUTOMAKE_OPTIONS = no-dependencies
+CC = false
+
+# So that we don't have to require a Python interpreter.
+pythondir = ${prefix}/py
+PYTHON = false
+END
+
+ocwd=`pwd` || fatal_ "cannot get current working directory"
+
+# Help to ensure cp won't see the same file twice.
+mkdir bin
+cat > bin/cp <<END
+#!/bin/sh
+PATH='$PATH'; export PATH
+
+case " \$* " in
+  *foo.c\ *)
+    if test -f '$ocwd'/foo-c-copied; then
+      echo "\$0: we tried to copy foo.c twice" >&2
+      exit 1
+    else
+      # For a sanity check later.
+      echo ok > '$ocwd'/cp-wrapper-has-seen-foo-c
+    fi
+    ;;
+esac
+
+case " \$* " in
+  *bar.py\ *)
+    if test -f '$ocwd'/bar-py-copied; then
+      echo "\$0: we tried to copy bar.py twice" >&2
+      exit 1
+    else
+      # For a sanity check later.
+      echo ok > '$ocwd'/cp-wrapper-has-seen-bar-py
+    fi
+    ;;
+esac
+
+exec cp "\$@"
+END
+chmod a+x bin/cp
+PATH=`pwd`/bin$PATH_SEPARATOR$PATH; export PATH;
+
+: > foo.c
+: > bar.py
+: > py-compile
+
+# Help to ensure cp won't try to copy the same file twice.
+chmod a-w foo.c bar.py
+
+$ACLOCAL
+$AUTOCONF
+$AUTOMAKE
+
+./configure
+$MAKE sanity-check || fatal_ "expected invariants not verified"
+$MAKE distdir
+test -f cp-wrapper-has-seen-foo-c && test -f cp-wrapper-has-seen-bar-py \
+  || fatal_ "our cp wrapper hasn't run correctly"
+
+:
diff --git a/tests/distcom-subdir.test b/tests/distcom-subdir.test
new file mode 100755
index 0000000..dca87f7
--- /dev/null
+++ b/tests/distcom-subdir.test
@@ -0,0 +1,72 @@
+#! /bin/sh
+# Copyright (C) 2004, 2009, 2010, 2011 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/>.
+
+# Test to make sure that if an auxfile (here depcomp) is required
+# by a subdir Makefile.am, it is distributed by that Makefile.am.
+
+. ./defs || Exit 1
+
+cat >> configure.in << 'END'
+AC_CONFIG_FILES([subdir/Makefile])
+AC_PROG_CC
+AC_OUTPUT
+END
+
+cat > Makefile.am << 'END'
+SUBDIRS = subdir
+END
+
+rm -f depcomp
+mkdir subdir
+
+: > subdir/Makefile.am
+
+$ACLOCAL
+$AUTOCONF
+$AUTOMAKE
+test ! -f depcomp
+
+cat > subdir/Makefile.am << 'END'
+bin_PROGRAMS = foo
+END
+
+: > subdir/foo.c
+
+$AUTOMAKE -a subdir/Makefile
+test -f depcomp
+
+# FIXME: the logic of this check and other similar ones in other
+# FIXME: `distcom*.test' files should be factored out in a common
+# FIXME: subroutine in `defs'...
+sed -n -e '/^DIST_COMMON =.*\\$/ {
+  :loop
+  p
+  n
+  t clear
+  :clear
+  s/\\$/\\/
+  t loop
+  p
+  n
+}' -e 's/$/ /' -e '/^DIST_COMMON =/p' subdir/Makefile.in > dc.txt
+cat dc.txt
+grep '^DIST_COMMON =.* \$(top_srcdir)/depcomp ' dc.txt
+
+./configure
+$MAKE distdir
+test -f $distdir/depcomp
+
+:
diff --git a/tests/java-compile-run-nested.test b/tests/java-compile-run-nested.test
index ec75c92..2019208 100755
--- a/tests/java-compile-run-nested.test
+++ b/tests/java-compile-run-nested.test
@@ -238,7 +238,7 @@ chmod a+x tests/*.test
 
 $ACLOCAL
 $AUTOCONF
-$AUTOMAKE
+$AUTOMAKE -a
 
 # To have the parallel testsuite more verbose.
 VERBOSE=yes; export VERBOSE
diff --git a/tests/test-driver-is-distributed.test b/tests/test-driver-is-distributed.test
index 09ea45a..e9a3f14 100755
--- a/tests/test-driver-is-distributed.test
+++ b/tests/test-driver-is-distributed.test
@@ -26,19 +26,23 @@ AC_CONFIG_FILES([tests/Makefile])
 AC_OUTPUT
 END
 
+rm -f depcomp # It's unneeded.
+
 mkdir tests
 
 cat > Makefile.am << 'END'
 SUBDIRS = tests
-test0:
-	echo ' ' $(DIST_COMMON) ' ' | grep '[ /]test-driver '
-test1: distdir
+check-local: test-top
+test-top: distdir
 	ls -l $(distdir) $(distdir)/* ;: For debugging.
 	test -f $(distdir)/test-driver
-.PHONY: test0 test1
+.PHONY: test-top
 END
 
 cat > tests/Makefile.am << 'END'
+check-local: test-sub
+test-sub:
+	echo ' ' $(DIST_COMMON) ' ' | grep '[ /]test-driver '
 TESTS = foo.test
 EXTRA_DIST = $(TESTS)
 END
@@ -53,7 +57,37 @@ $ACLOCAL
 $AUTOCONF
 $AUTOMAKE -a
 ./configure
-$MAKE test0 test1
+$MAKE test-top
+cd tests; $MAKE test-sub; cd ..;
+$MAKE distcheck
+
+# Try again, with a `config_auxdir' != `.' this time.
+
+$MAKE distclean
+
+mkdir build-aux
+mv missing install-sh test-driver build-aux
+
+for d in . tests; do
+  sed 's|test-driver|build-aux/test-driver|' $d/Makefile.am > $d/t
+  mv -f $d/t $d/Makefile.am
+  cat $d/Makefile.am # For debugging.
+done
+
+# Extra newline possibly required by OpenBSD 3.9 sed, see the autoconf
+# manual for details.
+sed '/^AC_INIT/i\
+AC_CONFIG_AUX_DIR([build-aux])
+' configure.in > t
+mv -f t configure.in
+cat configure.in # For debugging.
+
+touch aclocal.m4 # To avoid useless remakes.
+$AUTOCONF
+$AUTOMAKE
+./configure
+$MAKE test-top
+cd tests; $MAKE test-sub; cd ..;
 $MAKE distcheck
 
 :
-- 
1.7.2.3