d/shuffle-binaries: Fix binary shuffling script for cross-building
authorElliott Mitchell <ehem+debian@m5p.com>
Fri, 17 Jul 2020 06:37:42 +0000 (23:37 -0700)
committerHans van Kranenburg <hans@knorrie.org>
Sun, 28 Feb 2021 18:53:12 +0000 (19:53 +0100)
`ldd` doesn't work with cross-builds, so use `file` to detect scripts
and `strings | grep` for identifying linked libraries.  Even though
debhelper depends on file, make the requirement explicit.

Heavily simplify script inner loop.  While the core of that inner loop
was often executed only once, it is best to shrink inner loops.

Signed-off-by: Elliott Mitchell <ehem+debian@m5p.com>
Acked-by: Hans van Kranenburg <hans@knorrie.org>
debian/control
debian/shuffle-binaries

index 942bf6711c34a77ff88c44ad2211ea51d0cf8c19..1f88f1a40f7d15c2fca2341f4488159736175d4f 100644 (file)
@@ -8,6 +8,7 @@ Build-Depends:
    debhelper (>= 10),
    dh-exec,
    dpkg-dev (>= 1.16.0~),
+   file,
    rdfind,
    lsb-release,
    flex, bison,
index 8a823da10b1915d48818dbcf0a2dfc8593f1256b..3d951ed721c6a6f42389078ca28b91a640484aef 100755 (executable)
@@ -9,11 +9,9 @@ version="$1"; shift
 # currently-running version of Xen.  The actual binaries go
 # in xen-utils-$version:usr/lib/xen-$version/bin
 #
-# We use ldd to see what libraries the binary is linked against.
-# We ignore errors from ldd because we are running it on scripts
-# and things too and it is hard to distinguish these errors.
-#
-# We then match against the libraries listed for inclusion here:
+# We first have to figure out what libraries the binary is linked
+# against.  We then match against the libraries listed for inclusion
+# here:
 list=debian/libxenmiscV.install.vsn-in
 
 t=debian/tmp
@@ -23,28 +21,28 @@ cd=/usr/lib/xen-common/bin
 mkdir -p "$t/$vd"
 
 for binary in `find $t/usr/{bin,sbin} -type f`; do
-       reason=''
-       { ldd "$binary" ||: ; } | { fgrep '=>' ||: ; } \
-       | (
-               while read lib dummy; do
+       # filter for executables (ignore scripts)
+       file "$binary" | grep -q -eELF.\\+version.\\+interpreter || continue
+
+       reason=$(
+               strings "$binary" | grep -e^lib.\\+\\.so\\.\[.0-9\]\\+\$ | \
+               while read lib; do
                        lib=${lib%.so.*}
-                       if grep -F "usr/lib/*/$lib.so.*" $list >/dev/null; then
-                               reason+=" $lib"
+                       if grep -q -F "usr/lib/*/$lib.so.*" $list; then
+                               printf " %s" "$lib"
                        fi
                done
+       )
 
-               if [ "x$reason" = x ]; then
-                       exit 0
-               fi
+       # if no reason, then skip
+       [ -n "$reason" ] || continue
 
-               echo "shuffling $binary $reason"
+       echo "shuffling $binary$reason"
 
-               leaf=${binary##*}
-               mv -v $binary $t/$vd/$leaf
-               ln -vs $cd/xen-utils-wrapper $binary
+       mv -v "$binary" "$t/$vd/"
+       ln -vs "$cd/xen-utils-wrapper" "$binary"
 
-               touch debian/shuffle-binaries.stamp
-       )
+       touch "$0.stamp"
 done
 
 if [ ! -e "$0.stamp" ]; then