#!/bin/sh
# Standalone reproduction for automake bug#7328 / Debian #704225:
# "make -j install" ignores dependencies between libtool libraries
# installed by different install-*LTLIBRARIES targets of one Makefile.
#
# Usage: repro.sh [AUTOMAKE_BINDIR]
#   AUTOMAKE_BINDIR holds the automake/aclocal to test (default: from PATH).
# Exit status: 0 if the install ordering is correct, 1 if the bug shows.

set -u
test $# -ge 1 && PATH=$1:$PATH && export PATH
ACLOCAL_PATH=${ACLOCAL_PATH-/usr/share/aclocal}; export ACLOCAL_PATH

work=$(mktemp -d) || exit 2
trap 'rm -rf "$work"' EXIT
cd "$work" || exit 2
echo "automake: $(command -v automake) ($(automake --version | sed 1q))"

cat >configure.ac <<'END'
AC_INIT([repro],[1])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
LT_INIT
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
END

# libbase.la goes to $(pkglibdir) (install-exec); the module ext.la goes
# to a custom directory (install-data) and links against libbase.la, so
# libtool must relink it against the *installed* libbase at install time.
# liba/libb/libc are listed first only to widen the race window.
cat >Makefile.am <<'END'
myextdir = $(libdir)/repro-ext
pkglib_LTLIBRARIES = liba.la libb.la libc.la libbase.la
myext_LTLIBRARIES = ext.la
ext_la_LDFLAGS = -module
ext_la_LIBADD = libbase.la
bin_PROGRAMS = prog
prog_LDADD = $(builddir)/libbase.la
END
for x in a b c; do echo "int f$x (void) { return 1; }" >lib$x.c; done
echo 'int base (void) { return 42; }' >libbase.c
echo 'int base (void); int ext (void) { return base (); }' >ext.c
echo 'int base (void); int main (void) { return base () != 42; }' >prog.c

{ libtoolize -q && aclocal && automake -a && autoconf &&
  ./configure -q --prefix=/opt/repro && make -s; } >build.log 2>&1 ||
  { cat build.log; exit 2; }

status=0

echo
echo "== 1. Ordering rules emitted into Makefile.in"
if grep '^install-[A-Za-z_]*\(LTLIBRARIES\|PROGRAMS\): install-' Makefile.in
then :; else echo "(none)"; fi
for rule in 'install-myextLTLIBRARIES: install-pkglibLTLIBRARIES' \
            'install-binPROGRAMS: install-pkglibLTLIBRARIES'; do
  if grep -q "^$rule\$" Makefile.in; then echo "ok:   $rule"
  else echo "MISSING: $rule"; status=1; fi
done

echo
echo "== 2. Install only the dependent module into an empty DESTDIR"
echo "   (deterministic: its dependency must be installed first)"
if make install-myextLTLIBRARIES DESTDIR="$work/d1" >d1.log 2>&1; then
  echo "ok:   install-myextLTLIBRARIES succeeded"
else
  echo "FAIL: install-myextLTLIBRARIES failed:"
  grep -E 'cannot find|error' d1.log | sed 's/^/      /'
  status=1
fi

echo
echo "== 3. make -j8 install, 10 times: is ext.la relinked before"
echo "   libbase.la has been installed?"
bad=0
for i in 1 2 3 4 5 6 7 8 9 10; do
  rm -rf "$work/dj"
  make -j8 install DESTDIR="$work/dj" >j.log 2>&1 || { bad=$((bad+1)); continue; }
  inst=$(grep -n 'libbase\.lai' j.log | sed 1q | cut -d: -f1)
  relink=$(grep -n "relinking 'ext\.la'" j.log | sed 1q | cut -d: -f1)
  test -n "$inst" && test -n "$relink" && test "$relink" -lt "$inst" &&
    bad=$((bad+1))
done
if test $bad -eq 0; then echo "ok:   correct order in 10/10 runs"
else echo "FAIL: wrong order (or failure) in $bad/10 runs"; status=1; fi

echo
test $status -eq 0 && echo "RESULT: install order correct" ||
  echo "RESULT: bug present"
exit $status
