#!@SH@ # # opkhelper # src/oh-shlibdeps.sh # Find shared library dependencies. # # Copyright (C) 2019 Patrick "P. J." McDermott # # 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 . . '@pkgdatadir@/load.sm' LIBRARY_PATH='@library_path@' READELF_NEEDED_SED='s/^ 0x[0-9a-f]* (NEEDED) *Shared library: \[\(.*\)\]$/\1/p' find_lib() { local lib="${1}" shift 1 local pkg= local dir= # Check just-built packages. for pkg in *.data/; do IFS=':' for dir in ${LIBRARY_PATH}; do unset IFS if [ -r "${pkg}/${dir}/${lib}" ]; then printf '%s,\n' "${pkg%.data/}" return 0 fi done unset IFS done # Check installed packages. IFS=':' for dir in ${LIBRARY_PATH}; do unset IFS if [ -r "${dir}/${lib}" ]; then lib="${dir}/${lib}" pkg="$('@OPKG@' search "${lib}" | sed 's/ - .*$//')" if [ -n "${pkg}" ]; then printf '%s,\n' "${pkg}" return 0 else oh_warn "$(oh_get_msg 'shlib_no_pkg')" "${lib}" return 0 fi fi done unset IFS oh_warn "$(oh_get_msg 'shlib_no_lib')" "${lib}" return 0 } find_needed() { local elf="${1}" shift 1 while IFS='' read -r lib; do find_lib "${lib}" done <<-EOF $('@READELF@' -d "${elf}" | sed -n "${READELF_NEEDED_SED}") EOF return 0 } find_elves() { local d="${1}" shift 1 local file= while IFS='' read -r file; do if file "${file}" | grep -E 'ELF.*(executable|shared)' \ >/dev/null 2>&1; then find_needed "${file}" fi done <<-EOF $(find "${d}" -type f -a ! -path '*/debug/*' \ -a \( -name '*.so*' -o -perm '-a+x' \)) EOF return 0 } main() { local d= oh_init for d in *.data/; do exec 3>"${d%.data/}.substvars" printf 'Shlib-Depends:' >&3 printf ' %s' $(find_elves "${d}" | sort -u) >&3 exec 3>&- done return 0 } main "${@}"