summaryrefslogtreecommitdiff
path: root/find-libdeps.in
blob: 88ccad055c7dfc3e9aab4e983815351d30c347df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# License: Unspecified

. "$(librelib messages)"

set -e
shopt -s extglob

IGNORE_INTERNAL=0

if [[ $1 = "--ignore-internal" ]]; then
	IGNORE_INTERNAL=1
	shift
fi

script_mode=${0##*/find-lib}

case $script_mode in
	deps|provides) true;;
	*) die "Unknown mode %s" "$script_mode" ;;
esac

usage() {
	print "Usage: find-lib(deps|provides) [options] <package file|extracted package dir>"
	print "Find library dependencies or provides of a package."
	echo
	prose 'Prints a list of library dependencies in the format:'
	echo
	print '    <soname>=<soversion>-<soarch>'
	echo
	prose 'Where <soversion> is the shared library version, or
	      <soname> repeated if there is no version attached; and
	      <soarch> is the architecture of the library (either `32`
	      or `64`, based on the ELF Class).'
	echo
	print "Options:"
	flag "--ignore-internal" "Ignore internal libraries; libraries
	                          without a version attached"
	flag "-h"                "Show this message"
}
if [[ -z $1 ]]; then
	usage >&2
	exit 1
fi
if [[ $1 = '-h' ]]; then
	usage
	exit 0
fi

if [[ -d $1 ]]; then
	pushd "$1" >/dev/null
else
	setup_workdir

	case ${script_mode} in
		deps) bsdtar -C "$WORKDIR" -xf "$1";;
		provides) bsdtar -C "$WORKDIR" -xf "$1" --include="*.so*";;
	esac

	pushd "$WORKDIR" >/dev/null
fi

process_sofile() {
	# extract the library name: libfoo.so
	soname="${sofile%.so?(+(.+([0-9])))}".so
	# extract the major version: 1
	soversion="${sofile##*\.so\.}"
	if [[ "$soversion" = "$sofile" ]] && ((IGNORE_INTERNAL)); then
		continue
	fi
	if ! in_array "${soname}=${soversion}-${soarch}" "${soobjects[@]}"; then
		# libfoo.so=1-64
		echo "${soname}=${soversion}-${soarch}"
		soobjects+=("${soname}=${soversion}-${soarch}")
	fi
}

case $script_mode in
	deps) find_args=(-perm -u+x);;
  provides) find_args=(-name '*.so*');;
esac

find . -type f "${find_args[@]}" | while read -r filename; do
	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
	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
		done
	fi
done

popd >/dev/null