python: fix DLL loading for Windows

This commit is contained in:
Nguyen Anh Quynh 2015-10-19 22:10:05 +08:00
parent b0a1131ef0
commit 298a942dd1

View file

@ -8,7 +8,7 @@ from unicorn_const import *
import ctypes, ctypes.util, sys import ctypes, ctypes.util, sys
from platform import system from platform import system
from os.path import split, join, dirname from os.path import split, join, dirname, exists
import distutils.sysconfig import distutils.sysconfig
@ -17,13 +17,19 @@ if not hasattr(sys.modules[__name__], '__file__'):
__file__ = inspect.getfile(inspect.currentframe()) __file__ = inspect.getfile(inspect.currentframe())
_lib_path = split(__file__)[0] _lib_path = split(__file__)[0]
_all_libs = ['unicorn.dll', 'libunicorn.so', 'libunicorn.dylib'] _all_libs = ('unicorn.dll', 'libunicorn.so', 'libunicorn.dylib')
# Windows DLL in dependency order
_all_windows_dlls = ("libwinpthread-1.dll", "libgcc_s_seh-1.dll", "libgcc_s_dw2-1.dll", "libintl-8.dll", "libglib-2.0-0.dll", "libiconv-2.dll")
_found = False _found = False
for _lib in _all_libs: for _lib in _all_libs:
try: try:
if _lib == 'unicorn.dll':
for dll in _all_windows_dlls: # load all the rest DLLs first
_lib_file = join(_lib_path, dll)
if exists(_lib_file):
ctypes.cdll.LoadLibrary(_lib_file)
_lib_file = join(_lib_path, _lib) _lib_file = join(_lib_path, _lib)
# print "Trying to load:", _lib_file
_uc = ctypes.cdll.LoadLibrary(_lib_file) _uc = ctypes.cdll.LoadLibrary(_lib_file)
_found = True _found = True
break break
@ -45,8 +51,12 @@ if _found == False:
_lib_path = distutils.sysconfig.get_python_lib() _lib_path = distutils.sysconfig.get_python_lib()
for _lib in _all_libs: for _lib in _all_libs:
try: try:
if _lib == 'unicorn.dll':
for dll in _all_windows_dlls: # load all the rest DLLs first
_lib_file = join(_lib_path, 'unicorn', dll)
if exists(_lib_file):
ctypes.cdll.LoadLibrary(_lib_file)
_lib_file = join(_lib_path, 'unicorn', _lib) _lib_file = join(_lib_path, 'unicorn', _lib)
# print "Trying to load:", _lib_file
_uc = ctypes.cdll.LoadLibrary(_lib_file) _uc = ctypes.cdll.LoadLibrary(_lib_file)
_found = True _found = True
break break
@ -55,7 +65,7 @@ if _found == False:
# Attempt Darwin specific load (10.11 specific), # Attempt Darwin specific load (10.11 specific),
# since LD_LIBRARY_PATH is not guaranteed to exist # since LD_LIBRARY_PATH is not guaranteed to exist
if system() == 'Darwin': if (_found == False) and (system() == 'Darwin'):
_lib_path = '/usr/local/lib/' _lib_path = '/usr/local/lib/'
for _lib in _all_libs: for _lib in _all_libs:
try: try: