Skip to content

Instantly share code, notes, and snippets.

@Nislaco
Forked from h0tw1r3/mingw-copy-deps.sh
Last active September 6, 2020 16:15
Show Gist options
  • Save Nislaco/f4d54a6aece08361f79a1b4308435012 to your computer and use it in GitHub Desktop.
Save Nislaco/f4d54a6aece08361f79a1b4308435012 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}" | busybox 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}"
@Nislaco
Copy link
Author

Nislaco commented Sep 6, 2020

Switched to busy box for Awk due to errors after gawk 5.0 update. This is due to the following error:

awk: cmd. line:1: warning: regexp escape sequence `\ ' is not a known regexp operator

Using busybox is easier than trying to downgrade gawk. This is likely not needed as the error did not stop the dll's from being copied, but it does repress the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment