From 12642c25558b1e1d0315ac4035b576c8149fb1b3 Mon Sep 17 00:00:00 2001 From: Andrew Dutcher Date: Sun, 24 Sep 2017 07:33:01 -0700 Subject: [PATCH] Cleanups/fixes for the library issue conglomerate (#897) * Python: Disable distribution of static library on linux and macos; add environment variable LIBUNICORN_PATH to let user specify location of native library; prevent build of native library if this option is enabled; closes #869 * Python: Update README.TXT to describe how to manage the building and usage of the native library --- bindings/python/README.TXT | 5 +++++ bindings/python/setup.py | 20 +++++++++++++------- bindings/python/unicorn/unicorn.py | 5 ++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bindings/python/README.TXT b/bindings/python/README.TXT index 8cc6e889..ab5747a4 100644 --- a/bindings/python/README.TXT +++ b/bindings/python/README.TXT @@ -8,6 +8,11 @@ from source. This will build the core C library, package it with the python bindings, and install it to your system. + If you want to prevent the build of the native library during the python installation, + set the environment variable LIBUNICORN_PATH. You may also set this to a directory + containing libunicorn.so if you wish to use a verison of the native library other than + the globally installed one. + 2. Installing on Windows: diff --git a/bindings/python/setup.py b/bindings/python/setup.py index a11b0697..349a4ef9 100755 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -65,13 +65,13 @@ else: if SYSTEM == 'darwin': LIBRARY_FILE = "libunicorn.dylib" - STATIC_LIBRARY_FILE = 'libunicorn.a' + STATIC_LIBRARY_FILE = None elif SYSTEM in ('win32', 'cygwin'): LIBRARY_FILE = "unicorn.dll" STATIC_LIBRARY_FILE = "unicorn.lib" else: LIBRARY_FILE = "libunicorn.so" - STATIC_LIBRARY_FILE = 'libunicorn.a' + STATIC_LIBRARY_FILE = None def clean_bins(): shutil.rmtree(LIBS_DIR, ignore_errors=True) @@ -153,9 +153,11 @@ def build_libraries(): # check if a prebuilt library exists # if so, use it instead of building if os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE)) \ - and os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE)): + and (STATIC_LIBRARY_FILE is None \ + or os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE))): shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE), LIBS_DIR) - shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE), LIBS_DIR) + if STATIC_LIBRARY_FILE is not None: + shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE), LIBS_DIR) return # otherwise, build!! @@ -181,7 +183,8 @@ def build_libraries(): shutil.copy(LIBRARY_FILE, LIBS_DIR) try: # static library may fail to build on windows if user doesn't have visual studio installed. this is fine. - shutil.copy(STATIC_LIBRARY_FILE, LIBS_DIR) + if STATIC_LIBRARY_FILE is not None: + shutil.copy(STATIC_LIBRARY_FILE, LIBS_DIR) except: print('Warning: Could not build static library file! This build is not appropriate for a binary distribution') # enforce this @@ -198,8 +201,11 @@ class custom_sdist(sdist): class custom_build(build): def run(self): - log.info("Building C extensions") - build_libraries() + if 'LIBUNICORN_PATH' in os.environ: + log.info("Skipping building C extensions since LIBUNICORN_PATH is set") + else: + log.info("Building C extensions") + build_libraries() return build.run(self) class custom_bdist_egg(bdist_egg): diff --git a/bindings/python/unicorn/unicorn.py b/bindings/python/unicorn/unicorn.py index a394138c..43e51777 100644 --- a/bindings/python/unicorn/unicorn.py +++ b/bindings/python/unicorn/unicorn.py @@ -71,13 +71,15 @@ def _load_lib(path): _uc = None # Loading attempts, in order +# - user-provided environment variable # - pkg_resources can get us the path to the local libraries # - we can get the path to the local libraries by parsing our filename # - global load # - python's lib directory # - last-gasp attempt at some hardcoded paths on darwin and linux -_path_list = [pkg_resources.resource_filename(__name__, 'lib'), +_path_list = [os.getenv('LIBUNICORN_PATH', None), + pkg_resources.resource_filename(__name__, 'lib'), os.path.join(os.path.split(__file__)[0], 'lib'), '', distutils.sysconfig.get_python_lib(), @@ -88,6 +90,7 @@ _path_list = [pkg_resources.resource_filename(__name__, 'lib'), #print("-" * 80) for _path in _path_list: + if _path is None: continue _uc = _load_lib(_path) if _uc is not None: break else: