2015-08-21 10:04:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Python binding for Unicorn engine. Nguyen Anh Quynh <aquynh@gmail.com>
|
|
|
|
|
2016-09-20 17:22:38 +03:00
|
|
|
from __future__ import print_function
|
2015-08-21 10:04:50 +03:00
|
|
|
import glob
|
2024-06-12 09:45:23 +03:00
|
|
|
import logging
|
2015-08-21 10:04:50 +03:00
|
|
|
import os
|
2016-11-05 05:56:31 +03:00
|
|
|
import subprocess
|
2015-08-21 10:04:50 +03:00
|
|
|
import shutil
|
|
|
|
import sys
|
2016-11-05 05:56:31 +03:00
|
|
|
import platform
|
2023-01-05 20:10:09 +03:00
|
|
|
import setuptools
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2024-06-12 09:45:23 +03:00
|
|
|
from setuptools import setup
|
|
|
|
from sysconfig import get_platform
|
|
|
|
from setuptools.command.build import build
|
|
|
|
from setuptools.command.sdist import sdist
|
2016-08-15 10:29:38 +03:00
|
|
|
from setuptools.command.bdist_egg import bdist_egg
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2024-06-12 09:45:23 +03:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
SYSTEM = sys.platform
|
|
|
|
|
2016-11-05 05:56:31 +03:00
|
|
|
# sys.maxint is 2**31 - 1 on both 32 and 64 bit mingw
|
|
|
|
IS_64BITS = platform.architecture()[0] == '64bit'
|
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
# are we building from the repository or from a source distribution?
|
2021-10-03 17:14:44 +03:00
|
|
|
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
|
2016-08-15 10:29:38 +03:00
|
|
|
LIBS_DIR = os.path.join(ROOT_DIR, 'unicorn', 'lib')
|
|
|
|
HEADERS_DIR = os.path.join(ROOT_DIR, 'unicorn', 'include')
|
|
|
|
SRC_DIR = os.path.join(ROOT_DIR, 'src')
|
2022-09-25 19:09:41 +03:00
|
|
|
UC_DIR = SRC_DIR if os.path.exists(SRC_DIR) else os.path.join(ROOT_DIR, '../..')
|
2022-01-18 22:15:28 +03:00
|
|
|
BUILD_DIR = os.path.join(UC_DIR, 'build_python')
|
2016-08-15 10:29:38 +03:00
|
|
|
|
2024-09-26 13:44:51 +03:00
|
|
|
VERSION = "2.1.1"
|
2017-01-04 16:50:53 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
if SYSTEM == 'darwin':
|
2022-05-04 14:03:09 +03:00
|
|
|
LIBRARY_FILE = "libunicorn.2.dylib"
|
2022-05-07 16:48:28 +03:00
|
|
|
STATIC_LIBRARY_FILE = "libunicorn.a"
|
2021-10-03 17:14:44 +03:00
|
|
|
elif SYSTEM in ('win32', 'cygwin'):
|
2016-08-15 10:29:38 +03:00
|
|
|
LIBRARY_FILE = "unicorn.dll"
|
2016-12-27 11:30:07 +03:00
|
|
|
STATIC_LIBRARY_FILE = "unicorn.lib"
|
2016-08-15 10:29:38 +03:00
|
|
|
else:
|
2022-05-04 14:03:09 +03:00
|
|
|
LIBRARY_FILE = "libunicorn.so.2"
|
|
|
|
STATIC_LIBRARY_FILE = "libunicorn.a"
|
2016-08-15 10:29:38 +03:00
|
|
|
|
|
|
|
def clean_bins():
|
|
|
|
shutil.rmtree(LIBS_DIR, ignore_errors=True)
|
|
|
|
shutil.rmtree(HEADERS_DIR, ignore_errors=True)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
def copy_sources():
|
|
|
|
"""Copy the C sources into the source directory.
|
|
|
|
This rearranges the source files under the python distribution
|
|
|
|
directory.
|
|
|
|
"""
|
|
|
|
src = []
|
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
shutil.rmtree(SRC_DIR, ignore_errors=True)
|
|
|
|
os.mkdir(SRC_DIR)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
shutil.copytree(os.path.join(ROOT_DIR, '../../qemu'), os.path.join(SRC_DIR, 'qemu/'))
|
2019-10-10 17:00:42 +03:00
|
|
|
shutil.copytree(os.path.join(ROOT_DIR, '../../msvc'), os.path.join(SRC_DIR, 'msvc/'))
|
2016-08-15 10:29:38 +03:00
|
|
|
shutil.copytree(os.path.join(ROOT_DIR, '../../include'), os.path.join(SRC_DIR, 'include/'))
|
|
|
|
# make -> configure -> clean -> clean tests fails unless tests is present
|
|
|
|
shutil.copytree(os.path.join(ROOT_DIR, '../../tests'), os.path.join(SRC_DIR, 'tests/'))
|
2022-09-25 19:46:04 +03:00
|
|
|
shutil.copytree(os.path.join(ROOT_DIR, '../../samples'), os.path.join(SRC_DIR, 'samples/'))
|
|
|
|
shutil.copytree(os.path.join(ROOT_DIR, '../../glib_compat'), os.path.join(SRC_DIR, 'glib_compat/'))
|
|
|
|
|
2017-05-31 06:36:33 +03:00
|
|
|
try:
|
|
|
|
# remove site-specific configuration file
|
|
|
|
# might not exist
|
|
|
|
os.remove(os.path.join(SRC_DIR, 'qemu/config-host.mak'))
|
|
|
|
except OSError:
|
|
|
|
pass
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../*.[ch]")))
|
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../*.mk")))
|
2023-03-31 19:31:02 +03:00
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../cmake/*.cmake")))
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../LICENSE*")))
|
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../README.md")))
|
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../*.TXT")))
|
|
|
|
src.extend(glob.glob(os.path.join(ROOT_DIR, "../../CMakeLists.txt")))
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
for filename in src:
|
2016-08-15 10:29:38 +03:00
|
|
|
outpath = os.path.join(SRC_DIR, os.path.basename(filename))
|
2015-08-21 10:04:50 +03:00
|
|
|
log.info("%s -> %s" % (filename, outpath))
|
|
|
|
shutil.copy(filename, outpath)
|
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
def build_libraries():
|
|
|
|
"""
|
|
|
|
Prepare the unicorn directory for a binary distribution or installation.
|
|
|
|
Builds shared libraries and copies header files.
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
Will use a src/ dir if one exists in the current directory, otherwise assumes it's in the repo
|
|
|
|
"""
|
|
|
|
cwd = os.getcwd()
|
|
|
|
clean_bins()
|
|
|
|
os.mkdir(HEADERS_DIR)
|
|
|
|
os.mkdir(LIBS_DIR)
|
|
|
|
|
|
|
|
# copy public headers
|
2021-10-03 17:14:44 +03:00
|
|
|
shutil.copytree(os.path.join(UC_DIR, 'include', 'unicorn'), os.path.join(HEADERS_DIR, 'unicorn'))
|
2016-08-15 10:29:38 +03:00
|
|
|
|
2016-11-16 17:51:42 +03:00
|
|
|
# check if a prebuilt library exists
|
|
|
|
# if so, use it instead of building
|
2019-09-15 20:58:08 +03:00
|
|
|
if os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE)):
|
2016-12-04 13:18:24 +03:00
|
|
|
shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE), LIBS_DIR)
|
2019-09-15 20:58:08 +03:00
|
|
|
if STATIC_LIBRARY_FILE is not None and os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE)):
|
2017-09-24 17:33:01 +03:00
|
|
|
shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE), LIBS_DIR)
|
2016-11-16 17:51:42 +03:00
|
|
|
return
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
# otherwise, build!!
|
2021-10-03 17:14:44 +03:00
|
|
|
os.chdir(UC_DIR)
|
2016-08-15 10:29:38 +03:00
|
|
|
|
2016-12-27 11:30:07 +03:00
|
|
|
try:
|
2021-10-03 17:14:44 +03:00
|
|
|
subprocess.check_call(['msbuild', '/help'])
|
2016-12-27 11:30:07 +03:00
|
|
|
except:
|
2019-10-10 17:00:42 +03:00
|
|
|
has_msbuild = False
|
|
|
|
else:
|
|
|
|
has_msbuild = True
|
|
|
|
|
2019-11-19 19:34:56 +03:00
|
|
|
if has_msbuild and SYSTEM == 'win32':
|
2021-10-03 17:14:44 +03:00
|
|
|
plat = 'Win32' if platform.architecture()[0] == '32bit' else 'x64'
|
2019-10-10 17:00:42 +03:00
|
|
|
conf = 'Debug' if os.getenv('DEBUG', '') else 'Release'
|
2021-10-04 11:44:30 +03:00
|
|
|
if not os.path.exists(BUILD_DIR):
|
|
|
|
os.mkdir(BUILD_DIR)
|
|
|
|
|
2021-10-05 15:46:04 +03:00
|
|
|
subprocess.check_call(['cmake', '-B', BUILD_DIR, '-G', "Visual Studio 16 2019", "-A", plat, "-DCMAKE_BUILD_TYPE=" + conf])
|
|
|
|
subprocess.check_call(['msbuild', 'unicorn.sln', '-m', '-p:Platform=' + plat, '-p:Configuration=' + conf], cwd=BUILD_DIR)
|
2021-10-04 11:44:30 +03:00
|
|
|
|
|
|
|
obj_dir = os.path.join(BUILD_DIR, conf)
|
2019-10-10 17:00:42 +03:00
|
|
|
shutil.copy(os.path.join(obj_dir, LIBRARY_FILE), LIBS_DIR)
|
2022-05-03 12:11:29 +03:00
|
|
|
shutil.copy(os.path.join(BUILD_DIR, STATIC_LIBRARY_FILE), LIBS_DIR)
|
2019-10-10 17:00:42 +03:00
|
|
|
else:
|
|
|
|
# platform description refs at https://docs.python.org/2/library/sys.html#sys.platform
|
2021-10-03 17:14:44 +03:00
|
|
|
if not os.path.exists(BUILD_DIR):
|
|
|
|
os.mkdir(BUILD_DIR)
|
2021-10-05 15:46:04 +03:00
|
|
|
conf = 'Debug' if os.getenv('DEBUG', '') else 'Release'
|
|
|
|
|
2023-06-28 11:40:17 +03:00
|
|
|
cmake_args = ["cmake", '-B', BUILD_DIR, '-S', UC_DIR, "-DCMAKE_BUILD_TYPE=" + conf]
|
2022-01-18 21:35:43 +03:00
|
|
|
if os.getenv("TRACE", ""):
|
|
|
|
cmake_args += ["-DUNICORN_TRACER=on"]
|
|
|
|
subprocess.check_call(cmake_args)
|
2021-10-03 17:14:44 +03:00
|
|
|
os.chdir(BUILD_DIR)
|
2021-10-11 00:46:22 +03:00
|
|
|
threads = os.getenv("THREADS", "4")
|
2021-11-13 15:34:11 +03:00
|
|
|
subprocess.check_call(["cmake", "--build", ".", "-j" + threads])
|
2021-10-05 15:46:04 +03:00
|
|
|
|
|
|
|
shutil.copy(LIBRARY_FILE, LIBS_DIR)
|
2022-05-04 14:03:09 +03:00
|
|
|
shutil.copy(STATIC_LIBRARY_FILE, LIBS_DIR)
|
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
os.chdir(cwd)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
class custom_sdist(sdist):
|
2015-08-21 10:04:50 +03:00
|
|
|
def run(self):
|
2016-08-15 10:29:38 +03:00
|
|
|
clean_bins()
|
2016-11-05 05:56:31 +03:00
|
|
|
copy_sources()
|
2016-08-15 10:29:38 +03:00
|
|
|
return sdist.run(self)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
class custom_build(build):
|
|
|
|
def run(self):
|
2017-09-24 17:33:01 +03:00
|
|
|
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()
|
2016-08-15 10:29:38 +03:00
|
|
|
return build.run(self)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-08-15 10:29:38 +03:00
|
|
|
class custom_bdist_egg(bdist_egg):
|
|
|
|
def run(self):
|
|
|
|
self.run_command('build')
|
|
|
|
return bdist_egg.run(self)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
def dummy_src():
|
|
|
|
return []
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
cmdclass = {}
|
|
|
|
cmdclass['build'] = custom_build
|
|
|
|
cmdclass['sdist'] = custom_sdist
|
|
|
|
cmdclass['bdist_egg'] = custom_bdist_egg
|
2016-08-15 10:29:38 +03:00
|
|
|
|
2024-09-26 13:13:43 +03:00
|
|
|
if 'bdist_wheel' in sys.argv and '--plat-name' not in sys.argv:
|
|
|
|
idx = sys.argv.index('bdist_wheel') + 1
|
2024-09-26 14:11:53 +03:00
|
|
|
sys.argv.insert(idx, '--plat-name')
|
2024-09-26 13:13:43 +03:00
|
|
|
name = get_platform()
|
|
|
|
if 'linux' in name:
|
|
|
|
# linux_* platform tags are disallowed because the python ecosystem is fubar
|
|
|
|
# linux builds should be built in the centos 5 vm for maximum compatibility
|
|
|
|
# see https://github.com/pypa/manylinux
|
|
|
|
# see also https://github.com/angr/angr-dev/blob/master/bdist.sh
|
2024-09-26 14:11:53 +03:00
|
|
|
sys.argv.insert(idx + 1, 'manylinux1_' + platform.machine())
|
2024-09-26 13:13:43 +03:00
|
|
|
elif 'mingw' in name:
|
|
|
|
if IS_64BITS:
|
|
|
|
sys.argv.insert(idx + 1, 'win_amd64')
|
|
|
|
else:
|
|
|
|
sys.argv.insert(idx + 1, 'win32')
|
|
|
|
else:
|
|
|
|
# https://www.python.org/dev/peps/pep-0425/
|
|
|
|
sys.argv.insert(idx + 1, name.replace('.', '_').replace('-', '_'))
|
2016-11-05 05:56:31 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
try:
|
|
|
|
from setuptools.command.develop import develop
|
|
|
|
class custom_develop(develop):
|
|
|
|
def run(self):
|
|
|
|
log.info("Building C extensions")
|
|
|
|
build_libraries()
|
|
|
|
return develop.run(self)
|
|
|
|
|
|
|
|
cmdclass['develop'] = custom_develop
|
|
|
|
except ImportError:
|
|
|
|
print("Proper 'develop' support unavailable.")
|
|
|
|
|
|
|
|
def join_all(src, files):
|
|
|
|
return tuple(os.path.join(src, f) for f in files)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2020-05-29 06:56:24 +03:00
|
|
|
long_desc = '''
|
|
|
|
Unicorn is a lightweight, multi-platform, multi-architecture CPU emulator framework
|
2021-10-03 17:14:44 +03:00
|
|
|
based on [QEMU](http://qemu.org).
|
2020-05-29 06:56:24 +03:00
|
|
|
|
|
|
|
Unicorn offers some unparalleled features:
|
|
|
|
|
2022-11-01 19:28:40 +03:00
|
|
|
- Multi-architecture: ARM, ARM64 (ARMv8), M68K, MIPS, PowerPC, RISCV, SPARC, S390X, TriCore and X86 (16, 32, 64-bit)
|
2020-05-29 06:56:24 +03:00
|
|
|
- Clean/simple/lightweight/intuitive architecture-neutral API
|
|
|
|
- Implemented in pure C language, with bindings for Crystal, Clojure, Visual Basic, Perl, Rust, Ruby, Python, Java, .NET, Go, Delphi/Free Pascal, Haskell, Pharo, and Lua.
|
|
|
|
- Native support for Windows & *nix (with Mac OSX, Linux, *BSD & Solaris confirmed)
|
|
|
|
- High performance via Just-In-Time compilation
|
|
|
|
- Support for fine-grained instrumentation at various levels
|
|
|
|
- Thread-safety by design
|
|
|
|
- Distributed under free software license GPLv2
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
Further information is available at http://www.unicorn-engine.org
|
2020-05-29 06:56:24 +03:00
|
|
|
'''
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
setup(
|
|
|
|
provides=['unicorn'],
|
2024-04-27 19:11:05 +03:00
|
|
|
packages=setuptools.find_packages(include=["unicorn", "unicorn.*"]),
|
2016-11-05 05:56:31 +03:00
|
|
|
name='unicorn',
|
2015-08-21 10:04:50 +03:00
|
|
|
version=VERSION,
|
|
|
|
author='Nguyen Anh Quynh',
|
|
|
|
author_email='aquynh@gmail.com',
|
|
|
|
description='Unicorn CPU emulator engine',
|
2020-05-29 06:56:24 +03:00
|
|
|
long_description=long_desc,
|
2021-10-04 19:09:44 +03:00
|
|
|
long_description_content_type="text/markdown",
|
2021-10-03 17:14:44 +03:00
|
|
|
url='http://www.unicorn-engine.org',
|
2015-08-21 10:04:50 +03:00
|
|
|
classifiers=[
|
|
|
|
'License :: OSI Approved :: BSD License',
|
|
|
|
'Programming Language :: Python :: 2',
|
|
|
|
'Programming Language :: Python :: 3',
|
|
|
|
],
|
|
|
|
requires=['ctypes'],
|
2021-10-03 17:14:44 +03:00
|
|
|
cmdclass=cmdclass,
|
2021-10-05 16:11:59 +03:00
|
|
|
zip_safe=False,
|
2016-08-15 10:29:38 +03:00
|
|
|
include_package_data=True,
|
2021-10-05 16:11:59 +03:00
|
|
|
is_pure=False,
|
2016-08-15 10:29:38 +03:00
|
|
|
package_data={
|
2024-09-14 22:59:28 +03:00
|
|
|
'unicorn': ['unicorn/py.typed', 'lib/*', 'include/unicorn/*']
|
2016-08-15 10:29:38 +03:00
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
)
|