summaryrefslogtreecommitdiff
path: root/nsis/find_dlls.sh
blob: 6e02688e811259081370d633386668500423cc5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env sh

###############################################################################
# Script to determine dll dependencies of a Windows executable file.          #
# This should help automate the process of locating dlls to include as part   #
# of an installer.                                                            #
#                                                                             #
# This script is licensed under the GPLv3+                                    #
# Written by Louie Shprung <lshprung@tutanota.com>                            #
###############################################################################

help() {
	echo "Usage: $0 [OPTION]... BINARY..."
	echo "Determine dlls required by BINARY (an .exe or .dll file)"
	echo
	echo "Options:"
	echo "  -h         display this help message and exit"
	echo "  -q         hide warnings"
	echo "  -s PATH    set a path to search for dlls on. Default is \$PATH"
}

get_dll_path() {
	(
	IFS=':'
	for path in $SEARCHPATH; do
		if [ -e "$path/$1" ]; then
			echo "$path/$1"
			return 0
		fi
	done
	echo "Warning: Could not locate '$1' on system"
	return 1
	)
}

print_dlls() {
	while [ -n "$1" ]; do
		DLL_NAMES=$(objdump -x "$1" | grep "DLL Name: " | sed 's/^[^D]*DLL Name: //')
		for file in $DLL_NAMES; do
			if DLL_PATH="$(get_dll_path "$file")"; then
				echo "$DLL_PATH"
				print_dlls "$DLL_PATH"
			elif [ "$WARNINGS" -eq 1 ]; then
				echo "$DLL_PATH"
			fi
		done
		shift
	done
}

SEARCHPATH="$PATH"
WARNINGS=1

# Check args
while getopts "hqs:" flag; do
	case "$flag" in
		h)
			help
			exit
			;;
		q)
			WARNINGS=0
			;;
		s)
			SEARCHPATH="${OPTARG}"
			;;
		*)
			break
	esac
done
shift $((OPTIND-1))

if [ -z "$1" ]; then
	help
	exit 1
fi

if [ ! -e "$1" ]; then
	>&2 echo "Error: '$1' does not exist - skipping"
fi

print_dlls "$@"