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