Skip to content

Commit

Permalink
Python: make setup work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
rhelmot committed Oct 25, 2016
1 parent f585a84 commit ced66e3
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 231 deletions.
4 changes: 3 additions & 1 deletion bindings/python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
MANIFEST
dist/
src/
src/
capstone/lib
capstone/include
3 changes: 1 addition & 2 deletions bindings/python/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
recursive-include capstone *
recursive-include prebuilt *
recursive-include src *
include LICENSE.TXT
include README
70 changes: 33 additions & 37 deletions bindings/python/Makefile
Original file line number Diff line number Diff line change
@@ -1,62 +1,58 @@
ifndef BUILDDIR
OBJDIR = build
else
OBJDIR = $(abspath $(BUILDDIR))/obj/bindings/python
endif
PYX_BUILDDIR=build_pyx

.PHONY: gen_const install install3 install_cython clean

gen_const:
cd .. && python const_generator.py python

install:
rm -rf $(OBJDIR) src/
rm -rf src/
rm -rf prebuilt/win64/capstone.dll
rm -rf prebuilt/win32/capstone.dll
if test -n "${DESTDIR}"; then \
python setup.py build -b $(OBJDIR) install --root="${DESTDIR}"; \
python setup.py build install --root="${DESTDIR}"; \
else \
python setup.py build -b $(OBJDIR) install; \
python setup.py build install; \
fi

install3:
rm -rf $(OBJDIR) src/
rm -rf src/
rm -rf prebuilt/win64/capstone.dll
rm -rf prebuilt/win32/capstone.dll
if test -n "${DESTDIR}"; then \
python3 setup.py build -b $(OBJDIR) install --root="${DESTDIR}"; \
python3 setup.py build install --root="${DESTDIR}"; \
else \
python3 setup.py build -b $(OBJDIR) install; \
python3 setup.py build install; \
fi

# NOTE: Newer cython can be installed by: sudo pip install --upgrade cython
install_cython:
rm -rf $(OBJDIR) src/ dist/
rm -rf src/ dist/
rm -rf prebuilt/win64/capstone.dll
rm -rf prebuilt/win32/capstone.dll
mkdir -p $(OBJDIR)/pyx
cp setup_cython.py $(OBJDIR)
cp pyx/ccapstone* $(OBJDIR)/pyx/
cp capstone/__init__.py $(OBJDIR)/pyx/__init__.py
cp capstone/arm.py $(OBJDIR)/pyx/arm.pyx
cp capstone/arm_const.py $(OBJDIR)/pyx/arm_const.pyx
cp capstone/arm64.py $(OBJDIR)/pyx/arm64.pyx
cp capstone/arm64_const.py $(OBJDIR)/pyx/arm64_const.pyx
cp capstone/mips.py $(OBJDIR)/pyx/mips.pyx
cp capstone/mips_const.py $(OBJDIR)/pyx/mips_const.pyx
cp capstone/ppc.py $(OBJDIR)/pyx/ppc.pyx
cp capstone/ppc_const.py $(OBJDIR)/pyx/ppc_const.pyx
cp capstone/sparc.py $(OBJDIR)/pyx/sparc.pyx
cp capstone/sparc_const.py $(OBJDIR)/pyx/sparc_const.pyx
cp capstone/systemz.py $(OBJDIR)/pyx/systemz.pyx
cp capstone/sysz_const.py $(OBJDIR)/pyx/sysz_const.pyx
cp capstone/x86.py $(OBJDIR)/pyx/x86.pyx
cp capstone/x86_const.py $(OBJDIR)/pyx/x86_const.pyx
cp capstone/xcore.py $(OBJDIR)/pyx/xcore.pyx
cp capstone/xcore_const.py $(OBJDIR)/pyx/xcore_const.pyx
cd $(OBJDIR) && python setup_cython.py build -b ./tmp install --home=$(OBJDIR)
mv $(OBJDIR)/build/lib/python/capstone/* capstone
cd $(OBJDIR) && python setup_cython.py build -b ./tmp install
mkdir -p $(PYX_BUILDDIR)/pyx
cp setup_cython.py $(PYX_BUILDDIR)
cp pyx/ccapstone* $(PYX_BUILDDIR)/pyx/
cp capstone/__init__.py $(PYX_BUILDDIR)/pyx/__init__.py
cp capstone/arm.py $(PYX_BUILDDIR)/pyx/arm.pyx
cp capstone/arm_const.py $(PYX_BUILDDIR)/pyx/arm_const.pyx
cp capstone/arm64.py $(PYX_BUILDDIR)/pyx/arm64.pyx
cp capstone/arm64_const.py $(PYX_BUILDDIR)/pyx/arm64_const.pyx
cp capstone/mips.py $(PYX_BUILDDIR)/pyx/mips.pyx
cp capstone/mips_const.py $(PYX_BUILDDIR)/pyx/mips_const.pyx
cp capstone/ppc.py $(PYX_BUILDDIR)/pyx/ppc.pyx
cp capstone/ppc_const.py $(PYX_BUILDDIR)/pyx/ppc_const.pyx
cp capstone/sparc.py $(PYX_BUILDDIR)/pyx/sparc.pyx
cp capstone/sparc_const.py $(PYX_BUILDDIR)/pyx/sparc_const.pyx
cp capstone/systemz.py $(PYX_BUILDDIR)/pyx/systemz.pyx
cp capstone/sysz_const.py $(PYX_BUILDDIR)/pyx/sysz_const.pyx
cp capstone/x86.py $(PYX_BUILDDIR)/pyx/x86.pyx
cp capstone/x86_const.py $(PYX_BUILDDIR)/pyx/x86_const.pyx
cp capstone/xcore.py $(PYX_BUILDDIR)/pyx/xcore.pyx
cp capstone/xcore_const.py $(PYX_BUILDDIR)/pyx/xcore_const.pyx
cd $(PYX_BUILDDIR) && python setup_cython.py build -b ./tmp install --home=$(PYX_BUILDDIR)
mv $(PYX_BUILDDIR)/build/lib/python/capstone/* capstone
cd $(PYX_BUILDDIR) && python setup_cython.py build -b ./tmp install

# build & upload PyPi package with source code of the core
sdist:
Expand Down Expand Up @@ -93,8 +89,8 @@ sdist3_win:
python3 setup.py sdist register upload

clean:
rm -rf $(OBJDIR) src/ dist/ README
rm -f capstone/*.so
rm -rf $(PYX_BUILDDIR) build/ src/ dist/ *.egg-info README
rm -rf capstone/lib capstone/include
rm -rf prebuilt/win64/capstone.dll
rm -rf prebuilt/win32/capstone.dll

Expand Down
87 changes: 39 additions & 48 deletions bindings/python/capstone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,66 +195,57 @@
CS_SUPPORT_X86_REDUCE = CS_ARCH_ALL+2


import ctypes, ctypes.util, sys
import ctypes, ctypes.util
from os.path import split, join, dirname
import distutils.sysconfig

import pkg_resources

import inspect
if not hasattr(sys.modules[__name__], '__file__'):
__file__ = inspect.getfile(inspect.currentframe())

_lib_path = split(__file__)[0]
if sys.platform == 'darwin':
_lib = "libcapstone.dylib"
elif sys.platform in ('win32', 'cygwin'):
_lib = "capstone.dll"
else:
_lib = "libcapstone.so"

_all_libs = ['capstone.dll', 'libcapstone.so.3', 'libcapstone.so', 'libcapstone.dylib']
_found = False

for _lib in _all_libs:
def _load_lib(path):
lib_file = join(path, _lib)
try:
_lib_file = join(_lib_path, _lib)
# print "Trying to load:", _lib_file
_cs = ctypes.cdll.LoadLibrary(_lib_file)
_found = True
break
return ctypes.cdll.LoadLibrary(lib_file)
except OSError:
pass
if _found == False:
# try loading from default paths
for _lib in _all_libs:
try:
_cs = ctypes.cdll.LoadLibrary(_lib)
_found = True
break
except OSError:
pass

if _found == False:
# last try: loading from python lib directory
_lib_path = distutils.sysconfig.get_python_lib()
for _lib in _all_libs:
try:
_lib_file = join(_lib_path, 'capstone', _lib)
# print "Trying to load:", _lib_file
_cs = ctypes.cdll.LoadLibrary(_lib_file)
_found = True
break
except OSError:
pass

# Attempt Darwin specific load (10.11 specific),
# since LD_LIBRARY_PATH is not guaranteed to exist
if (_found == False) and (system() == 'Darwin'):
_lib_path = '/usr/local/lib/'
for _lib in _all_libs:
try:
_lib_file = join(_lib_path, _lib)
# print "Trying to load:", _lib_file
_cs = ctypes.cdll.LoadLibrary(_lib_file)
_found = True
break
except OSError:
pass

if _found == False:
# if we're on linux, try again with .so.3 extension
if lib_file.endswith('.so'):
try:
return ctypes.cdll.LoadLibrary(lib_file)
except OSError:
return None
return None

_cs = None

# Loading attempts, in order
# - 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'),
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:
_cs = _load_lib(_path)
if _cs is not None: break
else:
raise ImportError("ERROR: fail to load the dynamic library.")


Expand Down
Loading

0 comments on commit ced66e3

Please sign in to comment.