sit/check_linking.sh

129 lines
3 KiB
Bash
Raw Normal View History

#!/bin/bash
#
# Library dependency check script inspired by revdep-rebuild of Gentoo Linux
#
# Christoph Niethammer <niethammer@hlrs.de> (C) 2011
#
declare -r APP_NAME="${0##*/}"
declare -r VERSION="0.1"
declare -r FILES_FILE=1_files.rr
declare -r BROKEN_FILE=2_broken.rr
declare VERBOSE=0
declare findMask=
declare MODULE_NAMES=
declare SEARCH_DIRS=
# extract path information from module file
find_paths() {
local pathspec=$1
local PATHS=$( module display $MODULE_NAME 2>&1 | awk "/.*path[[:space:]]+$pathspec[[:space:]]+/{ sub(/.*path[[:space:]]+$pathspec[[:space:]]+/,\"\"); print }" )
PATHS=$(echo $PATHS | sed -e 's/:/ /g')
for path in $PATHS; do
SEARCH_DIRS+="$path "
done
}
function print_usage {
binary=`basename $0`
cat <<EOF
${APP_NAME}: (${VERSION})
Copyright (C) 2011 Christoph Niethammer <niethammer@hlrs.de>
Usage: $APP_NAME MODULE
-h, --help Print this usage
Checks linking consistency of given modules.
EOF
}
##############################################################################
# MAIN
##############################################################################
if [ $# -lt 1 ]; then
print_usage
exit 0
fi
for arg in $@; do
case $arg in
--help|-h)
print_usage
exit 0
;;
--verbose|-v)
VERBOSE=1
;;
*)
MODULE_NAMES+="${IFS}${arg}"
;;
esac
done
for MODULE_NAME in $MODULE_NAMES; do
find_paths PATH
find_paths LD_LIBRARY_PATH
done
echo "SEARCH_DIRS=$SEARCH_DIRS"
find ${SEARCH_DIRS[@]} $findMask -type f \( -perm -u+x -o -perm -g+x -o -perm -o+x -o \
-name '*.so' -o -name '*.so.*' -o -name '*.la' \) -print 2> /dev/null |
sort -u > "$FILES_FILE"
files=($(<"$FILES_FILE"))
for target_file in "${files[@]}"; do
if [ $VERBOSE = 1 ] ; then
echo -n "$target_file ..."
fi
if ! file $target_file | grep ELF > /dev/null ; then
if [ $VERBOSE = 1 ] ; then
echo " skipped"
fi
continue
fi
if [[ $target_file != *.la ]]; then
ldd_output=$(ldd "$target_file" 2> /dev/null)
MISSING_LIBS=$(
expr='s/[[:space:]]*\([^[:space:]]*\) => not found/\1/p'
sed -n "$expr" <<< "$ldd_output"
)
REQUIRED_LIBS=$(
expr='s/^[[:space:]]*NEEDED[[:space:]]*\([^[:space:]]*\).*/\1/p';
objdump -x "$target_file" | grep NEEDED | sed "$expr" | sort -u
)
MISSING_LIBS=$(grep -F "$REQUIRED_LIBS" <<< "$MISSING_LIBS")
if [[ $MISSING_LIBS ]]; then
echo "obj $target_file" >> "$BROKEN_FILE"
if [ $VERBOSE = 1 ] ; then
echo " broken (requires $MISSING_LIBS)"
else
echo " broken $target_file (requires $MISSING_LIBS)"
fi
else
if [ $VERBOSE = 1 ] ; then
echo " ok"
fi
fi
else
if [ $VERBOSE = 1 ] ; then
echo " skipped la file."
fi
fi
done