"""tools/hip/dll_closure.py - which DLLs a DLL needs, read from the PE import tables (no pefile, no objdump). python tools/hip/dll_closure.py DIR amdhip64_7.dll amd_comgr.dll # prints the closure inside DIR Why: the Windows HIP engine puts amdhip64_7.dll and amd_comgr.dll next to strata.exe so that Windows binds THEM and not an AMD driver's copy in System32 (#468, #461). But amdhip64_7.dll itself imports rocm_kpack.dll, which imports the MSVC runtime; with only the two DLLs beside the exe the full-path load fails (error 126, ERROR_MOD_NOT_FOUND), Windows falls back to System32's copy, and the first big prompt dies with hipErrorInvalidDeviceFunction (#461, maraa081). So what goes beside the exe is the DLLs' whole dependency closure, computed here from the files actually shipped rather than from a list of names that goes stale with the next ROCm. A name is followed only when the DLL exists in `search_dir` (the bundle's rocm/bin). Everything else - kernel32, user32, setupapi, the api-ms-win-* forwarders - is a Windows system DLL and is left alone. """ from __future__ import annotations import struct import sys from pathlib import Path def pe_imports(path) -> list[str]: """The DLL names in a PE file's import table and delay-import table, in file order. [] for a file that is not a PE image (or is truncated): the caller treats it as a leaf.""" try: data = Path(path).read_bytes() except OSError: return [] try: if data[:2] != b"MZ": return [] pe = struct.unpack_from(" list[str]: """`roots` (file names inside `search_dir`) plus every DLL in `search_dir` they import, followed recursively. The names are returned as spelled on disk, roots first, the rest in discovery order. A root that is not in the directory is skipped; an import that is not in the directory (a Windows system DLL) is not followed.""" d = Path(search_dir) on_disk = {p.name.lower(): p.name for p in d.iterdir() if p.is_file() and p.suffix.lower() == ".dll"} \ if d.is_dir() else {} out: list[str] = [] todo = [r for r in roots] seen: set[str] = set() while todo: name = todo.pop(0) low = name.lower() if low in seen or low not in on_disk: continue seen.add(low) real = on_disk[low] out.append(real) for dep in imports(d / real): if dep.lower() not in seen: todo.append(dep) return out def main() -> int: if len(sys.argv) < 3: print(__doc__) return 2 for n in dll_closure(sys.argv[1], sys.argv[2:]): print(n) return 0 if __name__ == "__main__": raise SystemExit(main())