aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/win-dll.py
diff options
context:
space:
mode:
authorTristan Gingold <tgingold@free.fr>2023-01-22 09:42:09 +0100
committerTristan Gingold <tgingold@free.fr>2023-01-22 09:42:09 +0100
commit55dfa111c5eeedc0ffc59282992169ad7bf571af (patch)
tree00d38a48165f8f8ceae5642dbf920f6d99335068 /scripts/win-dll.py
parenteaf79618a20465ef015cca2810f6f139bd9b0cf0 (diff)
downloadghdl-55dfa111c5eeedc0ffc59282992169ad7bf571af.tar.gz
ghdl-55dfa111c5eeedc0ffc59282992169ad7bf571af.tar.bz2
ghdl-55dfa111c5eeedc0ffc59282992169ad7bf571af.zip
Add scripts/win-dll.py and adjust Makefile.in
Diffstat (limited to 'scripts/win-dll.py')
-rw-r--r--scripts/win-dll.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/scripts/win-dll.py b/scripts/win-dll.py
new file mode 100644
index 000000000..e77e7ed12
--- /dev/null
+++ b/scripts/win-dll.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+# Get non-system DLL dependencies
+import pefile
+import os
+import os.path
+import sys
+
+def is_system(filename):
+ """Return true if the file is a system file. Very ad-hoc."""
+ return filename.startswith('/c/Windows')
+
+def get_imports(filename):
+ """Return list of dll imports"""
+ if is_system(filename):
+ return []
+ with pefile.PE(filename) as pe:
+ try:
+ imports = pe.DIRECTORY_ENTRY_IMPORT
+ except AttributeError:
+ imports = []
+ return [e.dll.decode() for e in imports]
+
+def search_dll(name, libraries_path):
+ """Search :param name: in :param libraries_path:"""
+ for path in libraries_path:
+ filename = os.path.join(path, name)
+ if os.path.isfile(filename):
+ return filename
+ return None
+
+def get_dependencies(name, libraries_path, cache):
+ """Return the non-system dll dependencies of :param name:"""
+ deps = get_imports(name)
+ res = []
+ for lib in deps:
+ if lib in cache:
+ continue
+ # Search on the path
+ filename = search_dll(lib, libraries_path)
+ # Always add in the cache
+ cache[lib] = filename
+ if filename is not None:
+ if not is_system(filename):
+ res.append(filename)
+ res.extend(get_dependencies(filename, libraries_path, cache))
+ return res
+
+if __name__ == '__main__':
+ if len(sys.argv) != 2:
+ print('usage: {} ldd-file'.format(sys.argv[0]))
+ sys.exit(1)
+
+ libraries_path = os.environ['PATH'].split(os.pathsep)
+ filename = sys.argv[1]
+ cache = {}
+ res = get_dependencies(filename, libraries_path, cache)
+ for f in res:
+ print(f)