scan_dll.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # SPDX-FileCopyrightText: 2019 yuzu Emulator Project
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. import pefile
  4. import sys
  5. import re
  6. import os
  7. import queue
  8. import shutil
  9. # constant definitions
  10. KNOWN_SYS_DLLS = ['WINMM.DLL', 'MSVCRT.DLL', 'VERSION.DLL', 'MPR.DLL',
  11. 'DWMAPI.DLL', 'UXTHEME.DLL', 'DNSAPI.DLL', 'IPHLPAPI.DLL']
  12. # below is for Ubuntu 18.04 with specified PPA enabled, if you are using
  13. # other distro or different repositories, change the following accordingly
  14. DLL_PATH = [
  15. '/usr/x86_64-w64-mingw32/bin/',
  16. '/usr/x86_64-w64-mingw32/lib/',
  17. '/usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/'
  18. ]
  19. missing = []
  20. def parse_imports(file_name):
  21. results = []
  22. pe = pefile.PE(file_name, fast_load=True)
  23. pe.parse_data_directories()
  24. for entry in pe.DIRECTORY_ENTRY_IMPORT:
  25. current = entry.dll.decode()
  26. current_u = current.upper() # b/c Windows is often case insensitive
  27. # here we filter out system dlls
  28. # dll w/ names like *32.dll are likely to be system dlls
  29. if current_u.upper() not in KNOWN_SYS_DLLS and not re.match(string=current_u, pattern=r'.*32\.DLL'):
  30. results.append(current)
  31. return results
  32. def parse_imports_recursive(file_name, path_list=[]):
  33. q = queue.Queue() # create a FIFO queue
  34. # file_name can be a string or a list for the convenience
  35. if isinstance(file_name, str):
  36. q.put(file_name)
  37. elif isinstance(file_name, list):
  38. for i in file_name:
  39. q.put(i)
  40. full_list = []
  41. while q.qsize():
  42. current = q.get_nowait()
  43. print('> %s' % current)
  44. deps = parse_imports(current)
  45. # if this dll does not have any import, ignore it
  46. if not deps:
  47. continue
  48. for dep in deps:
  49. # the dependency already included in the list, skip
  50. if dep in full_list:
  51. continue
  52. # find the requested dll in the provided paths
  53. full_path = find_dll(dep)
  54. if not full_path:
  55. missing.append(dep)
  56. continue
  57. full_list.append(dep)
  58. q.put(full_path)
  59. path_list.append(full_path)
  60. return full_list
  61. def find_dll(name):
  62. for path in DLL_PATH:
  63. for root, _, files in os.walk(path):
  64. for f in files:
  65. if name.lower() == f.lower():
  66. return os.path.join(root, f)
  67. def deploy(name, dst, dry_run=False):
  68. dlls_path = []
  69. parse_imports_recursive(name, dlls_path)
  70. for dll_entry in dlls_path:
  71. if not dry_run:
  72. shutil.copy(dll_entry, dst)
  73. else:
  74. print('[Dry-Run] Copy %s to %s' % (dll_entry, dst))
  75. print('Deploy completed.')
  76. return dlls_path
  77. def main():
  78. if len(sys.argv) < 3:
  79. print('Usage: %s [files to examine ...] [target deploy directory]')
  80. return 1
  81. to_deploy = sys.argv[1:-1]
  82. tgt_dir = sys.argv[-1]
  83. if not os.path.isdir(tgt_dir):
  84. print('%s is not a directory.' % tgt_dir)
  85. return 1
  86. print('Scanning dependencies...')
  87. deploy(to_deploy, tgt_dir)
  88. if missing:
  89. print('Following DLLs are not found: %s' % ('\n'.join(missing)))
  90. return 0
  91. if __name__ == '__main__':
  92. main()