Here is a patch for this issue (below). I've rebased it against both 1.19 and 1.20 and run the test suite on it. The patch includes a new test cast, ltorder-dirs.sh, that tickles the bug.
The test is also put together in repro.sh (below) as is a standalone reproduction. Run it as repro.sh [automake-bindir]. Against unpatched 1.19 or 1.20 it reports “bug present”; against either patched it reports “install order correct”. (Note: it does create files, read it before running so you don't accidentally dribble files where you don't want them!) Cheers, --Barak Pearlmutter. CC: [email protected] See: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=7328
Description: Order install rules by actual libtool link dependencies At "make install" time libtool relinks a libtool library (or program) against the *installed* copies of the libtool libraries it links with. When the dependency is installed by a different install-*LTLIBRARIES target of the same Makefile (e.g. pkglib_LTLIBRARIES vs. a custom foo_LTLIBRARIES module directory), automake emitted no ordering between the two targets, so "make -j install" fails at random with "cannot find -lfoo", or silently relinks against a stale installed copy. Automake 1.16.5 papered over the most common case by making every install target depend on install-libLTLIBRARIES, but that does nothing for libraries installed elsewhere, emits bogus target names for nobase_ variables (install-nobasebinPROGRAMS instead of install-nobase_binPROGRAMS), emits useless rules for EXTRA_ and check_ variables, and can create a cycle when a lib_ library links against a library in another directory. . This replaces that hack with handle_install_order, which derives "install-X: install-Y" edges from the _LIBADD/_LDADD variables (normalizing libfoo.la, ./libfoo.la, $(builddir)/libfoo.la and $(top_builddir)/DIR/libfoo.la), then adds the historical "lib_LTLIBRARIES first" edges only where they do not create a cycle. Within a single variable libraries are still installed in the order listed, as before. Author: Barak A. Pearlmutter <[email protected]> Bug: https://debbugs.gnu.org/7328 Bug-Debian: https://bugs.debian.org/704225 Last-Update: 2026-11-12 --- --- a/bin/automake.in +++ b/bin/automake.in @@ -98,6 +98,7 @@ sub handle_java (); sub handle_languages (); sub handle_libraries (); +sub handle_install_order (); sub handle_libtool (); sub handle_ltlibraries (); sub handle_makefiles_serial (); @@ -3035,6 +3036,128 @@ } } +# handle_install_order () +# ----------------------- +# Libtool relinks a library or program at install time against the +# *installed* copies of the libtool libraries it links with. So when +# two install-*LTLIBRARIES / install-*PROGRAMS targets live in the same +# Makefile, the one whose objects link against libraries of the other +# must not run before it, or "make -j install" fails (randomly) with +# "cannot find -lfoo" or links against stale installed copies. +# (automake bug#7328, Debian bug#704225.) +# +# We derive the needed ordering from the _LIBADD and _LDADD variables, +# and keep the historical, coarser rule that everything else installed +# from this Makefile is installed after lib_LTLIBRARIES and +# nobase_lib_LTLIBRARIES, unless that would introduce a cycle. +sub handle_install_order () +{ + # Return the install target for a PROGRAMS or LTLIBRARIES variable, + # or undef if its contents are not installed. + my $install_target = sub { + my ($varname) = @_; + my ($prefix, $primary) = $varname =~ /^(.*)_(PROGRAMS|LTLIBRARIES)$/ + or return undef; + return undef + if $prefix =~ /^(?:nobase_)?(?:dist_|nodist_)?(?:EXTRA|noinst|check)$/; + return "install-$prefix$primary"; + }; + + # Canonical spelling of a reference to a libtool library built by + # this Makefile, so that "libfoo.la", "./libfoo.la", + # "$(builddir)/libfoo.la", and "$(top_builddir)/DIR/libfoo.la" all + # compare equal. + my $normalize = sub { + my ($f) = @_; + $f =~ s{^\$[({](?:abs_)?builddir[)}]/+}{}; + if ($relative_dir eq '.') + { + $f =~ s{^\$[({](?:abs_)?top_builddir[)}]/+}{}; + } + else + { + $f =~ s{^\$[({](?:abs_)?top_builddir[)}]/+\Q$relative_dir\E/+}{}; + } + $f =~ s{^(?:\./+)+}{}; + return $f; + }; + + # Which install target installs each libtool library? + my %lib_target; + foreach my $var (variables 'LTLIBRARIES') + { + my $target = &$install_target ($var->name) or next; + foreach my $lib ($var->value_as_list_recursive) + { + next if $lib =~ /^\@.*\@$/; + $lib_target{&$normalize ($lib)} = $target; + } + } + + # Dependency graph between install targets. + my %deps; + my @targets; + foreach my $var (variables ('PROGRAMS'), variables ('LTLIBRARIES')) + { + my $target = &$install_target ($var->name) or next; + push @targets, $target; + my $addvar = $var->name =~ /_PROGRAMS$/ ? '_LDADD' : '_LIBADD'; + foreach my $obj ($var->value_as_list_recursive) + { + next if $obj =~ /^\@.*\@$/; + $obj =~ s/\$\(EXEEXT\)$//; + my $v = var (canonicalize ($obj) . $addvar) or next; + foreach my $dep ($v->value_as_list_recursive) + { + next unless $dep =~ /\.la$/; + my $dtarget = $lib_target{&$normalize ($dep)} or next; + $deps{$target}{$dtarget} = 1 unless $dtarget eq $target; + } + } + } + + my $reaches; + $reaches = sub { + my ($from, $to, $seen) = @_; + return 1 if $from eq $to; + return 0 if $seen->{$from}++; + foreach my $next (keys %{$deps{$from} || {}}) + { + return 1 if &$reaches ($next, $to, $seen); + } + return 0; + }; + + # Historical behaviour: install the "main" libraries first. + foreach my $libvar ('lib_LTLIBRARIES', 'nobase_lib_LTLIBRARIES') + { + next unless var $libvar; + my $ltarget = &$install_target ($libvar); + foreach my $target (@targets) + { + next if $target eq $ltarget; + next if &$reaches ($ltarget, $target, {}); + $deps{$target}{$ltarget} = 1; + } + } + + foreach my $target (sort keys %deps) + { + foreach my $dtarget (sort keys %{$deps{$target}}) + { + if (&$reaches ($dtarget, $target, {})) + { + # Mutual dependencies between two directories: nothing we + # can do at this level, but don't make it worse. + next; + } + $output_rules .= "$target: $dtarget\n"; + } + } + $output_rules .= "\n" if %deps; + undef $reaches; # Break the closure's reference cycle. +} + # See if any _SOURCES variable were misspelled. sub check_typos () { @@ -8169,29 +8292,8 @@ do_check_merge_target; handle_all ($makefile); - # FIXME: Gross! - my $have_lt1 = !!var("lib_LTLIBRARIES"); - my $have_lt2 = !!var("nobase_lib_LTLIBRARIES"); - for my $var (variables("PROGRAMS"), variables("LTLIBRARIES")) - { - if ($var->name eq "lib_LTLIBRARIES" - || $var->name eq "nobase_lib_LTLIBRARIES" - || substr($var->name, 0, 7) eq "noinst_") - { - next; - } - my $shortname = $var->name; - $shortname =~ tr{_}{}d; - if ($have_lt1) - { - $output_rules .= "install-$shortname: install-libLTLIBRARIES\n\n"; - } - if ($have_lt2) - { - $output_rules .= "install-$shortname: install-nobase_libLTLIBRARIES\n\n"; - } - } - + # Must come after handle_programs and handle_ltlibraries. + handle_install_order; handle_install; handle_clean ($makefile); handle_factored_dependencies; --- a/t/list-of-tests.mk +++ b/t/list-of-tests.mk @@ -703,6 +703,7 @@ t/ltlibobjs.sh \ t/ltlibsrc.sh \ t/ltorder.sh \ +t/ltorder-dirs.sh \ t/m4-inclusion.sh \ t/maintclean-vpath.sh \ t/maintclean.sh \ --- a/t/ltorder-dirs.sh +++ b/t/ltorder-dirs.sh @@ -0,0 +1,90 @@ +#!/bin/sh +# Copyright (C) 2026 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 <https://www.gnu.org/licenses/>. + +# Libtool libraries and programs installed in one directory must be +# installed after the libtool libraries, installed in another directory +# of the same Makefile, that they link against: libtool relinks them +# against the installed copies. Otherwise "make -j install" fails at +# random. See automake bug#7328 and Debian bug#704225. + +required='cc libtoolize' +. test-init.sh + +cat >>configure.ac <<'END' +AC_PROG_CC +AM_PROG_AR +AC_PROG_LIBTOOL +AC_OUTPUT +END + +cat >Makefile.am <<'END' +myextdir = $(libdir)/ext +pkglib_LTLIBRARIES = libbase.la +myext_LTLIBRARIES = ext.la +ext_la_LDFLAGS = -module +ext_la_LIBADD = $(top_builddir)/libbase.la +nobase_bin_PROGRAMS = p +p_LDADD = ./libbase.la +lib_LTLIBRARIES = libmain.la +libmain_la_LIBADD = $(builddir)/libbase.la +END + +echo 'int base () { return 0; }' >libbase.c +echo 'extern int base (); int ext () { return base (); }' >ext.c +echo 'extern int base (); int mainlib () { return base (); }' >libmain.c +echo 'extern int base (); int main () { return base (); }' >p.c + +libtoolize +$ACLOCAL +$AUTOCONF +$AUTOMAKE --add-missing + +# The ordering is derived from _LIBADD and _LDADD. +grep '^install-myextLTLIBRARIES:.* install-pkglibLTLIBRARIES' Makefile.in +grep '^install-nobase_binPROGRAMS:.* install-pkglibLTLIBRARIES' Makefile.in +grep '^install-libLTLIBRARIES:.* install-pkglibLTLIBRARIES' Makefile.in +# The traditional "lib_LTLIBRARIES first" rule must not create a cycle, +# and must use the real names of the install targets. +grep '^install-pkglibLTLIBRARIES:.* install-libLTLIBRARIES' Makefile.in && exit 1 +grep '^install-nobase_binPROGRAMS:.* install-libLTLIBRARIES' Makefile.in +grep '^install-nobasebin' Makefile.in && exit 1 +grep '^install-\(EXTRA\|check\|noinst\)' Makefile.in && exit 1 + +./configure --prefix="$(pwd)/inst" + +# XXX: This is a workaround for a purity mechanism implemented by GNU Guix. +# For more details see +# <https://www.gnu.org/s/guix/manual/html_node/Application-Setup.html#index-ld_002dwrapper>. +GUIX_LD_WRAPPER_ALLOW_IMPURITIES=1 +export GUIX_LD_WRAPPER_ALLOW_IMPURITIES + +$MAKE + +# Installing only the dependent library must install its dependency +# first. This is deterministic, unlike a "make -j install". +run_make -E install-myextLTLIBRARIES +grep 'has not been installed' stderr && exit 1 +test -f inst/lib/ext/ext.la +test -f inst/lib/$me/libbase.la +rm -rf inst + +run_make -E install +grep 'has not been installed' stderr && exit 1 + +$MAKE uninstall +test $(find inst -type f -print | wc -l) -eq 0 + +:
Description: Order install rules by actual libtool link dependencies At "make install" time libtool relinks a libtool library (or program) against the *installed* copies of the libtool libraries it links with. When the dependency is installed by a different install-*LTLIBRARIES target of the same Makefile (e.g. pkglib_LTLIBRARIES vs. a custom foo_LTLIBRARIES module directory), automake emitted no ordering between the two targets, so "make -j install" fails at random with "cannot find -lfoo", or silently relinks against a stale installed copy. Automake 1.16.5 papered over the most common case by making every install target depend on install-libLTLIBRARIES, but that does nothing for libraries installed elsewhere, emits bogus target names for nobase_ variables (install-nobasebinPROGRAMS instead of install-nobase_binPROGRAMS), emits useless rules for EXTRA_ and check_ variables, and can create a cycle when a lib_ library links against a library in another directory. . This replaces that hack with handle_install_order, which derives "install-X: install-Y" edges from the _LIBADD/_LDADD variables (normalizing libfoo.la, ./libfoo.la, $(builddir)/libfoo.la and $(top_builddir)/DIR/libfoo.la), then adds the historical "lib_LTLIBRARIES first" edges only where they do not create a cycle. Within a single variable libraries are still installed in the order listed, as before. Author: Barak A. Pearlmutter <[email protected]> Bug: https://debbugs.gnu.org/7328 Bug-Debian: https://bugs.debian.org/704225 Last-Update: 2026-11-12 --- --- a/bin/automake.in +++ b/bin/automake.in @@ -98,6 +98,7 @@ sub handle_java (); sub handle_languages (); sub handle_libraries (); +sub handle_install_order (); sub handle_libtool (); sub handle_link_dependencies (); sub handle_ltlibraries (); @@ -3075,6 +3076,128 @@ } } +# handle_install_order () +# ----------------------- +# Libtool relinks a library or program at install time against the +# *installed* copies of the libtool libraries it links with. So when +# two install-*LTLIBRARIES / install-*PROGRAMS targets live in the same +# Makefile, the one whose objects link against libraries of the other +# must not run before it, or "make -j install" fails (randomly) with +# "cannot find -lfoo" or links against stale installed copies. +# (automake bug#7328, Debian bug#704225.) +# +# We derive the needed ordering from the _LIBADD and _LDADD variables, +# and keep the historical, coarser rule that everything else installed +# from this Makefile is installed after lib_LTLIBRARIES and +# nobase_lib_LTLIBRARIES, unless that would introduce a cycle. +sub handle_install_order () +{ + # Return the install target for a PROGRAMS or LTLIBRARIES variable, + # or undef if its contents are not installed. + my $install_target = sub { + my ($varname) = @_; + my ($prefix, $primary) = $varname =~ /^(.*)_(PROGRAMS|LTLIBRARIES)$/ + or return undef; + return undef + if $prefix =~ /^(?:nobase_)?(?:dist_|nodist_)?(?:EXTRA|noinst|check)$/; + return "install-$prefix$primary"; + }; + + # Canonical spelling of a reference to a libtool library built by + # this Makefile, so that "libfoo.la", "./libfoo.la", + # "$(builddir)/libfoo.la", and "$(top_builddir)/DIR/libfoo.la" all + # compare equal. + my $normalize = sub { + my ($f) = @_; + $f =~ s{^\$[({](?:abs_)?builddir[)}]/+}{}; + if ($relative_dir eq '.') + { + $f =~ s{^\$[({](?:abs_)?top_builddir[)}]/+}{}; + } + else + { + $f =~ s{^\$[({](?:abs_)?top_builddir[)}]/+\Q$relative_dir\E/+}{}; + } + $f =~ s{^(?:\./+)+}{}; + return $f; + }; + + # Which install target installs each libtool library? + my %lib_target; + foreach my $var (variables 'LTLIBRARIES') + { + my $target = &$install_target ($var->name) or next; + foreach my $lib ($var->value_as_list_recursive) + { + next if $lib =~ /^\@.*\@$/; + $lib_target{&$normalize ($lib)} = $target; + } + } + + # Dependency graph between install targets. + my %deps; + my @targets; + foreach my $var (variables ('PROGRAMS'), variables ('LTLIBRARIES')) + { + my $target = &$install_target ($var->name) or next; + push @targets, $target; + my $addvar = $var->name =~ /_PROGRAMS$/ ? '_LDADD' : '_LIBADD'; + foreach my $obj ($var->value_as_list_recursive) + { + next if $obj =~ /^\@.*\@$/; + $obj =~ s/\$\(EXEEXT\)$//; + my $v = var (canonicalize ($obj) . $addvar) or next; + foreach my $dep ($v->value_as_list_recursive) + { + next unless $dep =~ /\.la$/; + my $dtarget = $lib_target{&$normalize ($dep)} or next; + $deps{$target}{$dtarget} = 1 unless $dtarget eq $target; + } + } + } + + my $reaches; + $reaches = sub { + my ($from, $to, $seen) = @_; + return 1 if $from eq $to; + return 0 if $seen->{$from}++; + foreach my $next (keys %{$deps{$from} || {}}) + { + return 1 if &$reaches ($next, $to, $seen); + } + return 0; + }; + + # Historical behaviour: install the "main" libraries first. + foreach my $libvar ('lib_LTLIBRARIES', 'nobase_lib_LTLIBRARIES') + { + next unless var $libvar; + my $ltarget = &$install_target ($libvar); + foreach my $target (@targets) + { + next if $target eq $ltarget; + next if &$reaches ($ltarget, $target, {}); + $deps{$target}{$ltarget} = 1; + } + } + + foreach my $target (sort keys %deps) + { + foreach my $dtarget (sort keys %{$deps{$target}}) + { + if (&$reaches ($dtarget, $target, {})) + { + # Mutual dependencies between two directories: nothing we + # can do at this level, but don't make it worse. + next; + } + $output_rules .= "$target: $dtarget\n"; + } + } + $output_rules .= "\n" if %deps; + undef $reaches; # Break the closure's reference cycle. +} + # See if any _SOURCES variable were misspelled. sub check_typos () { @@ -8364,29 +8487,8 @@ do_check_merge_target; handle_all ($makefile); - # FIXME: Gross! - my $have_lt1 = !!var("lib_LTLIBRARIES"); - my $have_lt2 = !!var("nobase_lib_LTLIBRARIES"); - for my $var (variables("PROGRAMS"), variables("LTLIBRARIES")) - { - if ($var->name eq "lib_LTLIBRARIES" - || $var->name eq "nobase_lib_LTLIBRARIES" - || substr($var->name, 0, 7) eq "noinst_") - { - next; - } - my $shortname = $var->name; - $shortname =~ tr{_}{}d; - if ($have_lt1) - { - $output_rules .= "install-$shortname: install-libLTLIBRARIES\n\n"; - } - if ($have_lt2) - { - $output_rules .= "install-$shortname: install-nobase_libLTLIBRARIES\n\n"; - } - } - + # Must come after handle_programs and handle_ltlibraries. + handle_install_order; handle_install; handle_clean ($makefile); handle_factored_dependencies; --- a/t/list-of-tests.mk +++ b/t/list-of-tests.mk @@ -712,6 +712,7 @@ t/ltlibobjs.sh \ t/ltlibsrc.sh \ t/ltorder.sh \ +t/ltorder-dirs.sh \ t/m4-inclusion.sh \ t/maintclean-vpath.sh \ t/maintclean.sh \ --- a/t/ltorder-dirs.sh +++ b/t/ltorder-dirs.sh @@ -0,0 +1,90 @@ +#!/bin/sh +# Copyright (C) 2026 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 <https://www.gnu.org/licenses/>. + +# Libtool libraries and programs installed in one directory must be +# installed after the libtool libraries, installed in another directory +# of the same Makefile, that they link against: libtool relinks them +# against the installed copies. Otherwise "make -j install" fails at +# random. See automake bug#7328 and Debian bug#704225. + +required='cc libtoolize' +. test-init.sh + +cat >>configure.ac <<'END' +AC_PROG_CC +AM_PROG_AR +AC_PROG_LIBTOOL +AC_OUTPUT +END + +cat >Makefile.am <<'END' +myextdir = $(libdir)/ext +pkglib_LTLIBRARIES = libbase.la +myext_LTLIBRARIES = ext.la +ext_la_LDFLAGS = -module +ext_la_LIBADD = $(top_builddir)/libbase.la +nobase_bin_PROGRAMS = p +p_LDADD = ./libbase.la +lib_LTLIBRARIES = libmain.la +libmain_la_LIBADD = $(builddir)/libbase.la +END + +echo 'int base () { return 0; }' >libbase.c +echo 'extern int base (); int ext () { return base (); }' >ext.c +echo 'extern int base (); int mainlib () { return base (); }' >libmain.c +echo 'extern int base (); int main () { return base (); }' >p.c + +libtoolize +$ACLOCAL +$AUTOCONF +$AUTOMAKE --add-missing + +# The ordering is derived from _LIBADD and _LDADD. +grep '^install-myextLTLIBRARIES:.* install-pkglibLTLIBRARIES' Makefile.in +grep '^install-nobase_binPROGRAMS:.* install-pkglibLTLIBRARIES' Makefile.in +grep '^install-libLTLIBRARIES:.* install-pkglibLTLIBRARIES' Makefile.in +# The traditional "lib_LTLIBRARIES first" rule must not create a cycle, +# and must use the real names of the install targets. +grep '^install-pkglibLTLIBRARIES:.* install-libLTLIBRARIES' Makefile.in && exit 1 +grep '^install-nobase_binPROGRAMS:.* install-libLTLIBRARIES' Makefile.in +grep '^install-nobasebin' Makefile.in && exit 1 +grep '^install-\(EXTRA\|check\|noinst\)' Makefile.in && exit 1 + +./configure --prefix="$(pwd)/inst" + +# XXX: This is a workaround for a purity mechanism implemented by GNU Guix. +# For more details see +# <https://www.gnu.org/s/guix/manual/html_node/Application-Setup.html#index-ld_002dwrapper>. +GUIX_LD_WRAPPER_ALLOW_IMPURITIES=1 +export GUIX_LD_WRAPPER_ALLOW_IMPURITIES + +$MAKE + +# Installing only the dependent library must install its dependency +# first. This is deterministic, unlike a "make -j install". +run_make -E install-myextLTLIBRARIES +grep 'has not been installed' stderr && exit 1 +test -f inst/lib/ext/ext.la +test -f inst/lib/$me/libbase.la +rm -rf inst + +run_make -E install +grep 'has not been installed' stderr && exit 1 + +$MAKE uninstall +test $(find inst -type f -print | wc -l) -eq 0 + +:
repro.sh
Description: application/shellscript

