3cfbe1
#! /bin/bash
3cfbe1
# Copyright (C) 2010 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
3cfbe1
#
3cfbe1
# This program is free software; you can redistribute it and/or modify
3cfbe1
# it under the terms of the GNU General Public License as published by
3cfbe1
# the Free Software Foundation; version 3 of the License.
3cfbe1
#
3cfbe1
# This program is distributed in the hope that it will be useful,
3cfbe1
# but WITHOUT ANY WARRANTY; without even the implied warranty of
3cfbe1
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3cfbe1
# GNU General Public License for more details.
3cfbe1
#
3cfbe1
# You should have received a copy of the GNU General Public License
3cfbe1
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
3cfbe1
3cfbe1
3cfbe1
## Usage: dso-fixup <build-root> <libdir> <pattern> <libs>*
3cfbe1
3cfbe1
BR=$1
3cfbe1
LIBDIR=$2
3cfbe1
PATTERN=$3
3cfbe1
shift 3
3cfbe1
3cfbe1
for i; do
3cfbe1
    echo -n $(basename "$i")' '
3cfbe1
    readelf -d $i | sed "\\!(NEEDED).*${PATTERN}!"'s/[^:]\+: \[\(.*\)\]/\1/p;d' | xargs echo -n
3cfbe1
    echo " ##"
3cfbe1
done | ./depsort | while read lib deps; do
3cfbe1
      link=$BR$LIBDIR/${lib%.so.*}.so
3cfbe1
      test -L "$link" || {
3cfbe1
	echo "bad file '$link'" >&2
3cfbe1
	exit 1
3cfbe1
      }
3cfbe1
3cfbe1
      set -- $deps
3cfbe1
      test $# -ne 0 || continue
3cfbe1
3cfbe1
      rm -f $link
3cfbe1
3cfbe1
3cfbe1
      {
3cfbe1
	echo '/* GNU ld script */'
3cfbe1
	echo -n "INPUT($LIBDIR/$lib"
3cfbe1
	d=
3cfbe1
	if test "$#" -gt 0; then
3cfbe1
	    echo -n " AS_NEEDED("
3cfbe1
	    for i; do
3cfbe1
		echo -n "$d$LIBDIR/$i"
3cfbe1
		d=' '
3cfbe1
	    done
3cfbe1
	    echo -n ")"
3cfbe1
	fi
3cfbe1
	echo ")"
3cfbe1
      } > "$link"
3cfbe1
3cfbe1
      chmod 0644 "$link"
3cfbe1
done