pacman/scripts/libmakepkg/autodep/library_depends.sh.in
Balló György 351cefb16b Merge branch 'bgyorgy-master-patch-59422' into 'master'
libmakepkg/autodep: add support for optional autodeps

See merge request pacman/pacman!58
2025-08-02 10:39:32 +02:00

108 lines
3.1 KiB
Bash

#!/bin/bash
#
# library_depends.sh - Automatically add library requirements to depends
#
# Copyright (c) 2021-2025 Pacman Development Team <pacman-dev@lists.archlinux.org>
#
# 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 of the License, 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 <http://www.gnu.org/licenses/>.
#
[[ -n "$LIBMAKEPKG_AUTODEP_LIBRARY_DEPENDS_SH" ]] && return
LIBMAKEPKG_AUTODEP_LIBRARY_DEPENDS_SH=1
MAKEPKG_LIBRARY=${MAKEPKG_LIBRARY:-'@libmakepkgdir@'}
autodep_functions+=('library_depends')
library_depends() {
if check_option "autodeps" "y"; then
local dep filename libdeps libdir libpath prefix sofile isoptdep
declare -A libdeps
declare -A optlibdeps
while IFS= read -rd '' filename; do
for sofile in $(LC_ALL=C readelf -d $filename 2>/dev/null | sed -nr 's/.*Shared library: \[(.*)\].*/\1/p'); do
# get the full path of the library
libpath=$(ldd $filename | sed -nr "s/.$sofile => (.*) \(.*\)/\1/p")
# if ldd can not find the library, it is likely part of the package and not in filesystem
if [[ -z $libpath ]]; then
continue
fi
# skip if the library is part of the package
if [[ -e "$pkgdir/$libpath" ]]; then
continue
fi
# find the prefix for the dependency
libpath=${libpath#/}
libpath=${libpath%/*}
unset prefix
for libdir in ${LIB_DIRS[@]}; do
if [[ ${libdir#*:} == ${libpath} ]]; then
prefix=${libdir%%:*}
fi
done
if [[ -z ${prefix} ]]; then
continue
fi
# only add library dependency if it exists - this helps bootstrapping dependencies
if [[ $(run_pacman -T "$prefix:$sofile") ]]; then
continue
fi
# if the binary marked as optional, add its dependencies to optdepends
unset isoptdep
for optbinary in "${optbinaries[@]}"
do
IFS=':'; local optbinarydata=($optbinary); unset IFS;
if [[ "${optbinarydata[0]}" == "${filename#$pkgdir/}" ]] ; then
if [[ -z ${optbinarydata[1]} ]]; then
optlibdeps["$prefix:$sofile"]+=""
else
optlibdeps["$prefix:$sofile"]+="${optbinarydata[1]# }; "
fi
isoptdep=1
fi
done
if [[ -n ${isoptdep} ]]; then
continue
fi
libdeps["$prefix:$sofile"]=1
done
done < <(find "$pkgdir" -type f -perm -u+x -print0)
# remove optlibdep if already in libdep
for libdep in ${!libdeps[@]}; do
unset optlibdeps["$libdep"]
done
depends+=(${!libdeps[@]})
for optlibdep in "${!optlibdeps[@]}"; do
if [[ -z ${optlibdeps[${optlibdep}]} ]]; then
optdepends+=("$optlibdep")
else
optdepends+=("$optlibdep: ${optlibdeps[${optlibdep}]%; }")
fi
done
fi
}