>From 61e820715daa8b775566a935302b33c242fd815c Mon Sep 17 00:00:00 2001
From: Florian Pritz <bluewind@xssn.at>
Date: Sat, 15 Aug 2009 12:35:32 +0200
Subject: [PATCH] add support for .so dependencies

Support-by: brain0 <thomas@archlinux.org>
Support-by: GNU\caustic <Christoph.Schied@uni-ulm.de>

Signed-off-by: Florian Pritz <bluewind@xssn.at>
---
 etc/makepkg.conf.in   |    4 ++-
 scripts/makepkg.sh.in |   69 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletions(-)

diff --git a/etc/makepkg.conf.in b/etc/makepkg.conf.in
index 3a3a415..8d91bcc 100644
--- a/etc/makepkg.conf.in
+++ b/etc/makepkg.conf.in
@@ -67,8 +67,10 @@ BUILDENV=(fakeroot !distcc color !ccache)
 #-- emptydirs: Leave empty directories in packages
 #-- zipman:    Compress manual (man and info) pages in MAN_DIRS with gzip
 #-- purge:     Remove files specified by PURGE_TARGETS
+#-- soprovides:add .so files to provides array
+#-- sodepends: add .so files to depends array
 #
-OPTIONS=(strip docs libtool emptydirs zipman purge)
+OPTIONS=(strip docs libtool emptydirs zipman purge soprovides !sodepends)
 
 #-- File integrity checks to use. Valid: md5, sha1, sha256, sha384, sha512
 INTEGRITY_CHECK=(md5)
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 5bbd880..7ad471f 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -874,6 +874,62 @@ tidy_install() {
 	fi
 }
 
+so_in_depends() {
+	for soinfo in $(pacman -Ql | grep -F $1 | tr " " ":"); do
+		sopkg=$(echo $soinfo | cut -d: -f1)
+		sofile=$(echo $soinfo | cut -d: -f2)
+		if in_array ${sopkg} ${depends[@]}; then
+			if [ "$(readelf -h "$sofile" 2> /dev/null | sed -nr 's/.*Class:.*ELF(.*).*/\1/p')" = "$soarch" ]; then
+				echo 0
+				return 0
+			fi
+		fi
+		if in_array ${sopkg} ${optdepends[@]}; then
+			if [ "$(readelf -h "$sofile" 2> /dev/null | sed -nr 's/.*Class:.*ELF(.*).*/\1/p')" = "$soarch" ]; then
+				echo 2
+				return 0
+			fi
+		fi
+		done
+	echo 1
+}
+
+find_sodepends() {
+	find $pkgdir -type f | while read filename
+	do
+		soarch=$(readelf -h "$filename" 2> /dev/null | sed -nr 's/.*Class:.*ELF(.*).*/\1/p')
+		[ -n "$soarch" ] || continue
+		for dep in $(readelf -d "$filename" 2> /dev/null | sed -nr 's/.*Shared library: \[(.*)\].*/\1/p')
+		do
+			if ! in_array "sodep-${soarch}-${dep}" ${sodepends[@]} ${soprovides}; then
+				soret=$(so_in_depends ${dep})
+				if [ "$soret" = "0" ]; then
+					echo "sodep-${soarch}-${dep}"
+				elif [ "$soret" = "2" ]; then
+					# $dep in optdepends
+					msg "$(gettext "Ignoring dependency in \$optdepends: %s")" "$dep"
+					true
+				else
+					warning "$(gettext "Missing dependency providing .so file in \$depends: %s")" "$dep"
+				fi
+				sodepends=(${sodepends[@]} "sodep-${soarch}-${dep}")
+			fi
+		done
+	done
+}
+
+find_soprovides() {
+	find $pkgdir -type f -name \*.so\* | while read filename
+	do
+    if readelf -h "$filename" 2>/dev/null | grep -q '.*Type:.*DYN (Shared object file).*'; then
+      soarch=$(readelf -h "$filename" 2>/dev/null | sed -nr 's/.*Class:.*ELF(.*).*/\1/p')
+      soname=$(readelf -d "$filename" 2>/dev/null | sed -nr 's/.*Library soname: \[(.*)\].*/\1/p')
+      [ -z "$soname" ] && soname="$(basename "$filename")"
+      echo sodep-${soarch}-${soname}
+    fi
+	done
+}
+
 write_pkginfo() {
 	local builddate=$(date -u "+%s")
 	if [[ -n $PACKAGER ]]; then
@@ -903,6 +959,19 @@ write_pkginfo() {
 		echo "force = true" >> .PKGINFO
 	fi
 
+	if [ "$(check_option soprovides)" != "n" ] || [ "$(check_option sodepends)" = "y" ]; then
+		soprovides=$(find_soprovides)
+	fi
+		
+	if [ "$(check_option sodepends)" = "y" ]; then
+		sodepends=$(find_sodepends)
+		depends=("${depends[@]}" ${sodepends})
+	fi
+
+	if [ "$(check_option soprovides)" != "n" ]; then
+		provides=("${provides[@]}" ${soprovides})
+	fi
+
 	local it
 	for it in "${license[@]}"; do
 		echo "license = $it" >>.PKGINFO
-- 
1.6.6.1

