From 10c4aa807d54d730a3ea0dc5a4db89ec8d5018fb Mon Sep 17 00:00:00 2001 From: Audrey Dutcher Date: Tue, 20 Feb 2018 05:11:19 -0800 Subject: [PATCH] Add option for inhibiting the core build while installing the python components. Add option for loading the core from a custom path. (#1089) --- bindings/python/README.txt | 10 +++++++++- bindings/python/capstone/__init__.py | 5 ++++- bindings/python/setup.py | 7 +++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bindings/python/README.txt b/bindings/python/README.txt index 82fe1482..69e36bbb 100644 --- a/bindings/python/README.txt +++ b/bindings/python/README.txt @@ -8,10 +8,18 @@ compile C code. On Linux, this is usually easy, but on Windows, this involves installing Visual Studio and using the "Developer Command Prompt" to perform the installation. See BUILDING.txt for more information. +By default, attempting to install the python bindings will trigger a build of +the capstone native core. If this is undesirable for whatever reason, for +instance, you already have a globally installed copy of libcapstone, you may +inhibit the build by setting the environment variable LIBCAPSTONE_PATH. The +exact value is not checked, just setting it will inhibit the build. During +execution, this variable may be set to the path of a directory containing a +specific version of libcapstone you would like to use. + If you don't want to build your own copy of Capstone, you can use a precompiled binary distribution from PyPI. Saying `pip install capstone` should automatically obtain an appropriate copy for your system. If it does not, please -open an issue at https://github.com/aquynh/capstone and tag @rhelmot - they +open an issue at https://github.com/aquynh/capstone and tag @rhelmot - she will fix this, probably! -------------------------------------------------------------------------------- diff --git a/bindings/python/capstone/__init__.py b/bindings/python/capstone/__init__.py index 3c9be870..bfd2e024 100644 --- a/bindings/python/capstone/__init__.py +++ b/bindings/python/capstone/__init__.py @@ -230,19 +230,22 @@ def _load_lib(path): _cs = 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('LIBCAPSTONE_PATH', None), + pkg_resources.resource_filename(__name__, 'lib'), join(split(__file__)[0], 'lib'), '', distutils.sysconfig.get_python_lib(), "/usr/local/lib/" if sys.platform == 'darwin' else '/usr/lib64'] for _path in _path_list: + if _path is None: continue _cs = _load_lib(_path) if _cs is not None: break else: diff --git a/bindings/python/setup.py b/bindings/python/setup.py index a7f2472c..0798e7a5 100755 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -156,8 +156,11 @@ class custom_sdist(sdist): class custom_build(build): def run(self): - log.info('Building C extensions') - build_libraries() + if 'LIBCAPSTONE_PATH' in os.environ: + log.info('Skipping building C extensions since LIBCAPSTONE_PATH is set') + else: + log.info('Building C extensions') + build_libraries() return build.run(self)