| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# The MIT License (MIT) |
| 4 |
# |
| 5 |
# Copyright (c) 2015 Martin Preisler |
| 6 |
# |
| 7 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
# of this software and associated documentation files (the "Software"), to deal |
| 9 |
# in the Software without restriction, including without limitation the rights |
| 10 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
# copies of the Software, and to permit persons to whom the Software is |
| 12 |
# furnished to do so, subject to the following conditions: |
| 13 |
# |
| 14 |
# The above copyright notice and this permission notice shall be included in all |
| 15 |
# copies or substantial portions of the Software. |
| 16 |
# |
| 17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
# SOFTWARE. |
| 24 |
|
| 25 |
|
| 26 |
import subprocess |
| 27 |
import os.path |
| 28 |
import argparse |
| 29 |
import shutil |
| 30 |
|
| 31 |
# The mingw path matches where Fedora 21 installs mingw32; this is the default |
| 32 |
# fallback if no other search path is specified in $MINGW_BUNDLEDLLS_SEARCH_PATH |
| 33 |
DEFAULT_PATH_PREFIXES = [ |
| 34 |
"", "/usr/bin", "/usr/i686-w64-mingw32/sys-root/mingw/bin", "/mingw64/bin", |
| 35 |
] |
| 36 |
|
| 37 |
env_path_prefixes = os.environ.get('MINGW_BUNDLEDLLS_SEARCH_PATH', None) |
| 38 |
if env_path_prefixes is not None: |
| 39 |
path_prefixes = [path for path in env_path_prefixes.split(os.pathsep) if path] |
| 40 |
else: |
| 41 |
path_prefixes = DEFAULT_PATH_PREFIXES |
| 42 |
|
| 43 |
# This blacklist may need extending |
| 44 |
blacklist = [ |
| 45 |
"advapi32.dll", "kernel32.dll", "msvcrt.dll", "ole32.dll", "user32.dll", |
| 46 |
"ws2_32.dll", "comdlg32.dll", "gdi32.dll", "imm32.dll", "oleaut32.dll", |
| 47 |
"shell32.dll", "winmm.dll", "winspool.drv", "wldap32.dll", |
| 48 |
"ntdll.dll", "d3d9.dll", "mpr.dll", "crypt32.dll", "dnsapi.dll", |
| 49 |
"shlwapi.dll", "version.dll", "iphlpapi.dll", "msimg32.dll", "setupapi.dll", |
| 50 |
] |
| 51 |
|
| 52 |
|
| 53 |
def find_full_path(filename, path_prefixes): |
| 54 |
path = None |
| 55 |
|
| 56 |
for path_prefix in path_prefixes: |
| 57 |
path_candidate = os.path.join(path_prefix, filename) |
| 58 |
|
| 59 |
if os.path.exists(path_candidate): |
| 60 |
path = path_candidate |
| 61 |
break |
| 62 |
|
| 63 |
if path is None: |
| 64 |
raise RuntimeError( |
| 65 |
"Can't find " + filename + ". If it is an inbuilt Windows DLL, " |
| 66 |
"please add it to the blacklist variable in the script and send " |
| 67 |
"a pull request!" |
| 68 |
) |
| 69 |
|
| 70 |
return path |
| 71 |
|
| 72 |
|
| 73 |
def gather_deps(path, path_prefixes, seen): |
| 74 |
ret = [path] |
| 75 |
output = subprocess.check_output(["objdump", "-p", path]).decode( |
| 76 |
"utf-8", "replace").split("\n") |
| 77 |
for line in output: |
| 78 |
if not line.startswith("\tDLL Name: "): |
| 79 |
continue |
| 80 |
|
| 81 |
dep = line.split("DLL Name: ")[1].strip() |
| 82 |
ldep = dep.lower() |
| 83 |
|
| 84 |
if ldep in blacklist: |
| 85 |
continue |
| 86 |
|
| 87 |
if ldep in seen: |
| 88 |
continue |
| 89 |
|
| 90 |
dep_path = find_full_path(dep, path_prefixes) |
| 91 |
seen.extend([ldep]) |
| 92 |
subdeps = gather_deps(dep_path, path_prefixes, seen) |
| 93 |
ret.extend(subdeps) |
| 94 |
|
| 95 |
return ret |
| 96 |
|
| 97 |
|
| 98 |
def main(): |
| 99 |
parser = argparse.ArgumentParser() |
| 100 |
parser.add_argument( |
| 101 |
"exe_file", |
| 102 |
help="EXE or DLL file that you need to bundle dependencies for" |
| 103 |
) |
| 104 |
parser.add_argument( |
| 105 |
"--copy", |
| 106 |
action="store_true", |
| 107 |
help="In addition to printing out the dependencies, also copy them next to the exe_file" |
| 108 |
) |
| 109 |
parser.add_argument( |
| 110 |
"--upx", |
| 111 |
action="store_true", |
| 112 |
help="Only valid if --copy is provided. Run UPX on all the DLLs and EXE." |
| 113 |
) |
| 114 |
args = parser.parse_args() |
| 115 |
|
| 116 |
if args.upx and not args.copy: |
| 117 |
raise RuntimeError("Can't run UPX if --copy hasn't been provided.") |
| 118 |
|
| 119 |
all_deps = set(gather_deps(args.exe_file, path_prefixes, [])) |
| 120 |
all_deps.remove(args.exe_file) |
| 121 |
|
| 122 |
print("\n".join(all_deps)) |
| 123 |
|
| 124 |
if args.copy: |
| 125 |
print("Copying enabled, will now copy all dependencies next to the exe_file.\n") |
| 126 |
|
| 127 |
parent_dir = os.path.dirname(os.path.abspath(args.exe_file)) |
| 128 |
|
| 129 |
for dep in all_deps: |
| 130 |
target = os.path.join(parent_dir, os.path.basename(dep)) |
| 131 |
|
| 132 |
try: |
| 133 |
print("Copying '%s' to '%s'" % (dep, target)) |
| 134 |
shutil.copy(dep, parent_dir) |
| 135 |
|
| 136 |
except shutil.SameFileError: |
| 137 |
print("Dependency '%s' was already in target directory, " |
| 138 |
"skipping..." % (dep)) |
| 139 |
|
| 140 |
if args.upx: |
| 141 |
subprocess.call(["upx", target]) |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == "__main__": |
| 145 |
main() |