summaryrefslogtreecommitdiffstats
path: root/helper
diff options
context:
space:
mode:
Diffstat (limited to 'helper')
-rw-r--r--helper/binutil.inc122
1 files changed, 122 insertions, 0 deletions
diff --git a/helper/binutil.inc b/helper/binutil.inc
new file mode 100644
index 00000000..b97ab0f8
--- /dev/null
+++ b/helper/binutil.inc
@@ -0,0 +1,122 @@
+#!/bin/bash
+#
+# Common functions to copy binaries and their dependancies.
+#
+############################################################
+#
+# Usage: get_dynamic_dependencies <binary_list>
+# (the list must be seperated by spaces)
+#
+# Ouput:
+# Will simply echo list of required libraries
+
+# List of libraries to exclude from
+BLACKLIST="ld-linux linux-gate linux-vdso libc.so"
+LOCALSEARCH=0
+LOCALSEARCHDIR=""
+
+get_dynamic_dependencies() {
+
+ if [ "x$1" == "x-l" ]; then
+ LOCALSEARCH=1
+ pdebug "Local search activated."
+ shift
+ [ ! -d $1 ] && perror "Directory given does not exist, exiting."
+ LOCALSEARCHDIR=$1
+ shift
+ pdebug "Looking in: $LOCALSEARCHDIR"
+ fi
+
+ while [ $# != 0 ]; do
+ local BINARY=$1
+ shift
+
+ pdebug "Processing ${BINARY} ..."
+
+ local LDD_OUT="ldd_output"
+ if ldd $BINARY > $LDD_OUT; then
+ # Case 1: dynamic
+ for LIB in $(cat $LDD_OUT | grep -v $(echo $BLACKLIST|sed 's/ /\\|/g') | awk '{print $1 $2 $3}'); do
+ # split the entry into an array, ex:
+ # libm.so.6 => /lib/libm.so.6 would be split into:
+ # liblink[0] liblink[1]
+ local liblink=(${LIB//=>/ })
+ pdebug " Searching for ${liblink[0]}..."
+ lib_search
+ done
+ else
+ # Case 2: not a dynamic
+ pdebug "$BINARY not a dynamic, skipping."
+ continue
+ fi
+ rm $LDD_OUT
+ done
+}
+
+lib_search(){
+
+ # if activated, start by searching the lib locally
+ if [ "x$LOCALSEARCH" == "x1" ]; then
+ cd $LOCALSEARCHDIR
+ local LOCAL_MATCHES=$(find . -name "${liblink[0]}") # | awk -F '.' '{print $1}')".so\*)
+ cd - &>/dev/null
+ if [ "x${LOCAL_MATCHES}" != "x" ]; then
+ for llib in ${LOCAL_MATCHES}; do
+ pdebug "\tFound locally, copying ${LOCALSEARCHDIR}/${llib}"
+ get_link_chain "${LOCALSEARCHDIR}"/"${llib}"
+
+ done
+ # found the libs, we are done
+ return
+ fi
+ # mark local search as done
+ LOCALSEARCH=0
+ fi
+
+ # search the lib on the system since it was not found earlier
+ if [ ! -z ${liblink[1]} ] && [ "x${liblink[1]}" != "xnot" ]; then
+ pdebug "\tNot found locally but in system, copying ${liblink[1]}"
+ # get chain of symlink for that lib
+ get_link_chain ${liblink[1]}
+ else
+ pwarning "Lib '${liblink[0]}' from required dir '$ENTRY' neither found in build directory nor on this system."
+ pwarning "If this lib is not supplied by another module, this module will probably fail in your final system"
+ fi
+}
+############################################################
+#
+# Usage: get_link_chain <link>
+#
+# <link> must be in absolute form
+#
+# Output:
+# Lists the symlink chain until a hardlink is found.
+get_link_chain() {
+
+ # the first parameter must be absolute
+ [[ "$1" == /* ]] || perror "get_link_chain() requires absolute paths."
+
+ # canonalize
+ local LINK=$(canonicalize $1)
+
+ # write the first link in the chain
+ # we strip the LOCALSEARCHDIR if it was found locally
+ [ "x$LOCALSEARCH" == "x1" ] && echo "./${LINK#$LOCALSEARCHDIR}" || echo ${LINK}
+
+ # now we check for symlinks
+ local TRY=0
+ while [ -L $LINK ] && [ $TRY -lt 10 ]; do
+ let TRY=TRY+1
+
+ # save the directory prefix
+ CURRENTDIR=$(dirname ${LINK})
+ # first follow the link
+ LINK=$(readlink $LINK)
+
+ pdebug "Processing $LINK"
+ # $LINK can be absolute or relative, check cases
+ [[ "$LINK" == /* ]] || LINK=$(canonicalize "$CURRENTDIR"/"${LINK}")
+ [ "x$LOCALSEARCH" == "x1" ] && echo "./${LINK#$LOCALSEARCHDIR}" || echo ${LINK}
+ done
+
+}