Skip to content

Instantly share code, notes, and snippets.

@TimothyGu
Last active October 31, 2018 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TimothyGu/62d8732d914e5db412a5 to your computer and use it in GitHub Desktop.
Save TimothyGu/62d8732d914e5db412a5 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Copyright (c) 2014 Tiancheng "Timothy" Gu
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
OBJDUMP=objdump
tmp=`mktemp -d`
help() {
cat <<EOF >&2
Usage: $0 -c -d DEST -s SRC [-f FILE|-F DIR]
or: $0 -p [-f FILE|-F DIR]
or: $0 -c -p -d DEST -s SRC [-f FILE|-F DIR]
Copy executable FILE(s) and their DLL dependencies for SRC directory to a
DEST directory, and/or print the recursive dependencies.
Operating modes:
-c, --copy
Operating options:
-d, --destdir Destination directory
-f, --infile The input executable file or DLL.
-F, --infiles, --indir The input directory of executable files and/or DLLs.
-s, --srcdir The directory with DLLs that can be copied.
Other options:
-h, --help Display this message and exit
EOF
}
die() {
echo $1 >&2
rm -rf "$tmp"
help
exit 1
}
while [ $# -gt 1 ]; do
key="$1"
shift
case $key in
-f|--infile)
infile="$1"
shift
;;
-F|--indir|--infiles)
indir="$1"
shift
;;
-s|--srcdir)
srcdir="$1"
shift
;;
-d|--destdir)
destdir="$1"
shift
;;
-h|--help)
help
exit 0
;;
*)
echo "unknown option $key ignored" >&2
;;
esac
done
if [ "$indir" ] && [ "$infile" ]; then
die '--indir and --infile are mutually exclusive.'
elif ! [ "$indir" ] && ! [ "$infile" ]; then
die 'Neither --indir nor --infile is specified.'
fi
if ! [ "$destdir" ]; then
die '--destdir is not specified.'
fi
if ! [ "$srcdir" ]; then
die '--srcdir is not specified.'
fi
if [ "$indir" ]; then
filelist=`find $indir -iregex '.*\(dll\|exe\)' | tr '\n' ' '`
else
filelist="$infile"
fi
if [ -n "$(ls -A $destdir 2>/dev/null)" ]; then
echo 'Warning: --destdir already exists and contains files.' >&2
fi
mkdir -p "$destdir"
append_deps() {
local bn="`basename $1`"
if [ -e "$tmp/$bn" ]; then
return 0
fi
if [ $# -eq 2 ]; then
path="$1"
else
path="${srcdir}/$1"
fi
echo "Processing $1" >&2
if ! [ -e "$path" ]; then
touch "$tmp/$bn"
return 0
fi
$OBJDUMP -p "$path" | grep 'DLL Name:' | cut -f3 -d' ' > "$tmp/$bn"
for dll in `cat "$tmp/$bn" | tr '\n' ' '`; do
append_deps "$dll"
done
alldeps=$(printf "$alldeps\n%s" "$(cat $tmp/$bn)" |
sort | uniq)
}
for f in $filelist; do
append_deps "$f" rel
done
for dll in `echo $alldeps | tr '\n' ' '`; do
if [ -e "${srcdir}/${dll}" ]; then
cp "${srcdir}/${dll}" "$destdir"
else
echo "Warning: \"$dll\" not found." >&2
fi
done
rm -rf "$tmp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment