Skip to content

Instantly share code, notes, and snippets.

@mustafaabdullahk
Last active October 11, 2022 13:20
Show Gist options
  • Save mustafaabdullahk/6cf3488f407fec4677290316ce09fdb9 to your computer and use it in GitHub Desktop.
Save mustafaabdullahk/6cf3488f407fec4677290316ce09fdb9 to your computer and use it in GitHub Desktop.
recursively calculate shared library dependencies and secondary libs then generate.inspired -> cawka/android-shared-lib-dependencies.sh
# Color defines
RED='\033[0;31m'
NC='\033[0m' # No Color
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Set correct path to readelf binary in Yocto toolchain
READELF=/opt/poky/4.0.4/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux-musl/aarch64-poky-linux-musl-readelf
SYSROOT=/opt/poky/4.0.4/sysroots/cortexa72-poky-linux-musl/usr/lib/
# List libraries that exist in /system/lib
STANDARD_LIBS=(c dl m stdc++ log z)
declare -A DEPS
echo $SYSROOT
has() {
local p=$1
echo p
shift
local x
for x in "$@"; do
[[ "${x}" == "${p}" ]] && return 0
done
return 1
}
LIBS=()
findSecondaryDependencies() {
local lib=$1
# if [ ! -f "lib$lib.so.*" ]; then
# return
# fi
local i
for i in `$READELF -d $lib | grep NEEDED | sed -e "s/.*\[//g" | sed -e "s/\].*$//g" | xargs`; do
if has $i ${STANDARD_LIBS[*]}; then
continue
fi
findSecondaryDependencies $i
if has $i ${LIBS[*]}; then
continue
fi
LIBS+=($i)
done
if has $lib ${LIBS[*]}; then
continue
fi
if [ ! -f "lib$lib.so.*" ]; then
return
fi
LIBS+=($lib)
}
findDependencies() {
local lib=$1
local i
for i in `$READELF -d $lib | grep NEEDED | sed -e "s/.*\[//g" | sed -e "s/\].*$//g" | xargs`; do
if has $i ${STANDARD_LIBS[*]}; then
continue
fi
findSecondaryDependencies $i
if has $i ${LIBS[*]}; then
continue
fi
echo "----------------- $i"
LIBS+=($i)
done
}
findDependencies $1
for lib in ${LIBS[*]}; do
echo $SYSROOT$lib
findSecondaryDependencies $SYSROOT$lib
done
# x $1
echo $1
for lib in ${LIBS[*]}; do
echo -e "[$1] dependent -> ${RED}(\"$lib\")${NC}"
done
mkdir -p dependencies
pushd dependencies
for lib in ${LIBS[*]}; do
echo -e "copied lib: (${Green}(\"$lib\")${NC})"
find /opt/poky/ -iname "$lib" -exec cp "{}" . \;
done
popd
echo -e "(${Cyan} copied all deps ${NC})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment