Skip to content

Instantly share code, notes, and snippets.

@asmwarrior
Forked from h0tw1r3/mingw-copy-deps.sh
Created March 3, 2022 13:15
Show Gist options
  • Save asmwarrior/93f3c6f41b6049ce889e3fde39b892e3 to your computer and use it in GitHub Desktop.
Save asmwarrior/93f3c6f41b6049ce889e3fde39b892e3 to your computer and use it in GitHub Desktop.
Recursively copy windows binary (dll/exe) dependencies from a sysroot (mingw toolchain) to the binaries directory.
#!/usr/bin/env bash
#
# Copyright: © 2015 Jeffrey Clark <https://github.com/h0tw1r3/>
# License: GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
#
set -o errtrace
error() {
echo "ERROR in $0 : line $1 exit code $2"
exit $2
}
trap 'error ${LINENO} ${?}' ERR
if [[ -z $1 || ! -d $1 || ! -f $2 ]]
then
echo "Usage: $(basename $0) SYSROOT BINARY"
echo "Search SYSROOT for BINARY dependencies and copy to dirname(BINARY)"
exit 1
fi
SYSROOT=$1
COPYTO=$(dirname $2)
if [[ ! -w ${COPYTO} ]]
then
echo "Error: ${COPYTO} is not writable"
exit 1
fi
function copydeps
{
depfilename=$(basename "${1}")
strings "${1}" | awk '{temp=tolower($0)} temp ~ /^([^\ ])*\.dll$/ && !/^'${depfilename,,}'$/ { print $0 }' | while read dll
do
destname=${COPYTO}/$(basename "${dll}")
[ -f "${destname}" ] && continue
find "${SYSROOT}" -name "${dll}" | while read f
do
cp -v "${f}" "${destname}" && copydeps "${f}" && break
done
done
}
copydeps "${2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment