manjaro-tools/bin/find-libdeps.in

88 lines
2.5 KiB
Text
Raw Normal View History

2014-10-04 00:46:40 +02:00
#!/bin/bash
2014-11-18 19:20:53 +01:00
#
2014-10-04 15:41:22 +02:00
# 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; version 2 of the License.
#
# 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.
2014-10-08 13:14:28 +02:00
version=@version@
2015-06-09 00:01:31 +02:00
LIBDIR='@libdir@'
[[ -r ${LIBDIR}/util-msg.sh ]] && source ${LIBDIR}/util-msg.sh
2017-03-27 18:07:26 +02:00
import ${LIBDIR}/util-pkg.sh
2014-10-04 00:46:40 +02:00
set -e
shopt -s extglob
IGNORE_INTERNAL=0
if [[ $1 = "--ignore-internal" ]]; then
2016-09-15 23:58:18 +02:00
IGNORE_INTERNAL=1
shift
2014-10-04 00:46:40 +02:00
fi
script_mode=${0##*/find-lib}
case $script_mode in
2016-09-15 23:58:18 +02:00
deps|provides) true;;
*) die "Unknown mode %s" "$script_mode" ;;
2014-10-04 00:46:40 +02:00
esac
if [[ -z $1 ]]; then
2016-09-15 23:58:18 +02:00
echo "${0##*/} [options] <package file|extracted package dir>"
echo "Options:"
echo " --ignore-internal ignore internal libraries"
exit 1
2014-10-04 00:46:40 +02:00
fi
if [[ -d $1 ]]; then
2016-09-15 23:58:18 +02:00
pushd $1 >/dev/null
2014-10-04 00:46:40 +02:00
else
2016-09-15 23:58:18 +02:00
WORKDIR=$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX")
2014-10-04 00:46:40 +02:00
2016-09-15 23:58:18 +02:00
case ${script_mode} in
deps) bsdtar -C "$WORKDIR" -xf "$1";;
provides) bsdtar -C "$WORKDIR" -xf "$1" --include="*.so*";;
esac
2014-10-04 00:46:40 +02:00
2016-09-15 23:58:18 +02:00
pushd "$WORKDIR" >/dev/null
2014-10-04 00:46:40 +02:00
fi
case $script_mode in
2016-09-15 23:58:18 +02:00
deps) find_args=(-perm -u+x);;
provides) find_args=(-name *.so*);;
2014-10-04 00:46:40 +02:00
esac
2014-12-27 22:49:23 +01:00
find $PWD -type f "${find_args[@]}" | while read filename; do
2016-09-15 23:58:18 +02:00
if [[ $script_mode = "provides" ]]; then
# ignore if we don't have a shared object
if ! LC_ALL=C readelf -h "$filename" 2>/dev/null | grep -q '.*Type:.*DYN (Shared object file).*'; then
continue
fi
fi
# get architecture of the file; if soarch is empty it's not an ELF binary
soarch=$(LC_ALL=C readelf -h "$filename" 2>/dev/null | sed -n 's/.*Class.*ELF\(32\|64\)/\1/p')
[[ -n $soarch ]] || continue
if [[ $script_mode = "provides" ]]; then
# get the string binaries link to: libfoo.so.1.2 -> libfoo.so.1
sofile=$(LC_ALL=C readelf -d "$filename" 2>/dev/null | sed -n 's/.*Library soname: \[\(.*\)\].*/\1/p')
[[ -z $sofile ]] && sofile="${filename##*/}"
process_sofile "${sofile}" "${soarch}"
elif [[ $script_mode = "deps" ]]; then
# process all libraries needed by the binary
for sofile in $(LC_ALL=C readelf -d "$filename" 2>/dev/null | sed -nr 's/.*Shared library: \[(.*)\].*/\1/p'); do
process_sofile "${sofile}" "${soarch}"
done
fi
2014-10-04 00:46:40 +02:00
done
popd >/dev/null