1#!/usr/bin/env python3
2
3# Copied from https://github.com/xantares/mingw-ldd/blob/master/mingw-ldd.py
4# Modified to point to right prefix location on Fedora.
5
6# WTFPL - Do What the Fuck You Want to Public License
7import pefile
8import os
9import sys
10
11
12def get_dependency(filename):
13    deps = []
14    pe = pefile.PE(filename)
15    for imp in pe.DIRECTORY_ENTRY_IMPORT:
16        deps.append(imp.dll.decode())
17    return deps
18
19
20def dep_tree(root, prefix=None):
21    if not prefix:
22        arch = get_arch(root)
23        #print('Arch =', arch)
24        prefix = '/usr/'+arch+'-w64-mingw32/sys-root/mingw/bin'
25        #print('Using default prefix', prefix)
26    dep_dlls = dict()
27
28    def dep_tree_impl(root, prefix):
29        for dll in get_dependency(root):
30            if dll in dep_dlls:
31                continue
32            full_path = os.path.join(prefix, dll)
33            if os.path.exists(full_path):
34                dep_dlls[dll] = full_path
35                dep_tree_impl(full_path, prefix=prefix)
36            else:
37                dep_dlls[dll] = 'not found'
38
39    dep_tree_impl(root, prefix)
40    return (dep_dlls)
41
42
43def get_arch(filename):
44    type2arch= {pefile.OPTIONAL_HEADER_MAGIC_PE: 'i686',
45                pefile.OPTIONAL_HEADER_MAGIC_PE_PLUS: 'x86_64'}
46    pe = pefile.PE(filename)
47    try:
48        return type2arch[pe.PE_TYPE]
49    except KeyError:
50        sys.stderr.write('Error: unknown architecture')
51        sys.exit(1)
52
53if __name__ == '__main__':
54    filename = sys.argv[1]
55    for dll, full_path in dep_tree(filename).items():
56        print(' ' * 7, dll, '=>', full_path)
57
58