forked from angr/pyvex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
213 lines (183 loc) · 7.13 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# pylint: disable=no-name-in-module,import-error
import os
import subprocess
import sys
import shutil
import glob
import tarfile
import multiprocessing
import time
IS_PYTHON2 = sys.version_info < (3, 0)
if IS_PYTHON2:
from urllib2 import urlopen
else:
from urllib.request import urlopen
import platform
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
LIB_DIR = os.path.join(PROJECT_DIR, 'pyvex', 'lib')
INCLUDE_DIR = os.path.join(PROJECT_DIR, 'pyvex', 'include')
try:
from setuptools import setup
from setuptools import find_packages
packages = find_packages()
except ImportError:
from distutils.core import setup
packages = []
for root, _, filenames in os.walk(PROJECT_DIR):
if "__init__.py" in filenames:
packages.append(root)
from distutils.util import get_platform
from distutils.errors import LibError
from distutils.command.build import build as _build
from distutils.command.sdist import sdist as _sdist
if sys.platform in ('win32', 'cygwin'):
LIBRARY_FILE = 'pyvex.dll'
STATIC_LIBRARY_FILE = 'pyvex.lib'
elif sys.platform == 'darwin':
LIBRARY_FILE = "libpyvex.dylib"
STATIC_LIBRARY_FILE = 'libpyvex.a'
else:
LIBRARY_FILE = "libpyvex.so"
STATIC_LIBRARY_FILE = 'libpyvex.a'
VEX_LIB_NAME = "vex" # can also be vex-amd64-linux
VEX_PATH = os.path.abspath(os.path.join(PROJECT_DIR, '..', 'vex'))
if not os.path.exists(VEX_PATH):
VEX_PATH = os.path.join(PROJECT_DIR, 'vex')
if not os.path.exists(VEX_PATH):
VEX_PATH = os.path.join(PROJECT_DIR, 'vex-master')
if not os.path.exists(VEX_PATH):
sys.__stderr__.write('###########################################################################\n')
sys.__stderr__.write('WARNING: downloading vex sources directly from github.\n')
sys.__stderr__.write('If this strikes you as a bad idea, please abort and clone the angr/vex repo\n')
sys.__stderr__.write('into the same folder containing the pyvex repo.\n')
sys.__stderr__.write('###########################################################################\n')
sys.__stderr__.flush()
time.sleep(10)
VEX_URL = 'https://github.com/angr/vex/archive/master.tar.gz'
with open('vex-master.tar.gz', 'wb') as v:
v.write(urlopen(VEX_URL).read())
with tarfile.open('vex-master.tar.gz') as tar:
tar.extractall()
def _build_vex():
e = os.environ.copy()
e['MULTIARCH'] = '1'
e['DEBUG'] = '1'
cmd1 = ['nmake', '/f', 'Makefile-msvc', 'all']
cmd2 = ['make', '-f', 'Makefile-gcc', '-j', str(multiprocessing.cpu_count()), 'all']
cmd3 = ['gmake', '-f', 'Makefile-gcc', '-j', str(multiprocessing.cpu_count()), 'all']
for cmd in (cmd1, cmd2, cmd3):
try:
if subprocess.call(cmd, cwd=VEX_PATH, env=e) == 0:
break
except OSError:
continue
else:
raise LibError("Unable to build libVEX.")
def _build_pyvex():
e = os.environ.copy()
e['VEX_LIB_PATH'] = VEX_PATH
e['VEX_INCLUDE_PATH'] = os.path.join(VEX_PATH, 'pub')
e['VEX_LIB_FILE'] = os.path.join(VEX_PATH, 'libvex.lib')
cmd1 = ['nmake', '/f', 'Makefile-msvc']
cmd2 = ['make', '-j', str(multiprocessing.cpu_count())]
cmd3 = ['gmake', '-j', str(multiprocessing.cpu_count())]
for cmd in (cmd1, cmd2, cmd3):
try:
if subprocess.call(cmd, cwd='pyvex_c', env=e) == 0:
break
except OSError as err:
continue
else:
raise LibError("Unable to build libpyvex.")
def _shuffle_files():
shutil.rmtree(LIB_DIR, ignore_errors=True)
shutil.rmtree(INCLUDE_DIR, ignore_errors=True)
os.mkdir(LIB_DIR)
os.mkdir(INCLUDE_DIR)
pyvex_c_dir = os.path.join(PROJECT_DIR, 'pyvex_c')
shutil.copy(os.path.join(pyvex_c_dir, LIBRARY_FILE), LIB_DIR)
shutil.copy(os.path.join(pyvex_c_dir, STATIC_LIBRARY_FILE), LIB_DIR)
shutil.copy(os.path.join(pyvex_c_dir, 'pyvex.h'), INCLUDE_DIR)
for f in glob.glob(os.path.join(VEX_PATH, 'pub', '*')):
shutil.copy(f, INCLUDE_DIR)
def _clean_bins():
shutil.rmtree(LIB_DIR, ignore_errors=True)
shutil.rmtree(INCLUDE_DIR, ignore_errors=True)
def _copy_sources():
local_vex_path = os.path.join(PROJECT_DIR, 'vex')
assert local_vex_path != VEX_PATH
shutil.rmtree(local_vex_path, ignore_errors=True)
os.mkdir(local_vex_path)
vex_src = ['LICENSE.GPL', 'LICENSE.README', 'Makefile-gcc', 'Makefile-msvc', 'common.mk', 'pub/*.h', 'priv/*.c', 'priv/*.h', 'auxprogs/*.c']
for spec in vex_src:
dest_dir = os.path.join(local_vex_path, os.path.dirname(spec))
if not os.path.isdir(dest_dir):
os.mkdir(dest_dir)
for srcfile in glob.glob(os.path.join(VEX_PATH, spec)):
shutil.copy(srcfile, dest_dir)
def _build_ffi():
import make_ffi
try:
make_ffi.doit(os.path.join(VEX_PATH, 'pub'))
except Exception as e:
print(repr(e))
raise
class build(_build):
def run(self):
self.execute(_build_vex, (), msg="Building libVEX")
self.execute(_build_pyvex, (), msg="Building libpyvex")
self.execute(_shuffle_files, (), msg="Copying libraries and headers")
self.execute(_build_ffi, (), msg="Creating CFFI defs file")
_build.run(self)
class sdist(_sdist):
def run(self):
self.execute(_clean_bins, (), msg="Removing binaries")
self.execute(_copy_sources, (), msg="Copying VEX sources")
_sdist.run(self)
cmdclass = { 'build': build, 'sdist': sdist }
try:
from setuptools.command.develop import develop as _develop
from setuptools.command.bdist_egg import bdist_egg as _bdist_egg
class develop(_develop):
def run(self):
self.execute(_build_vex, (), msg="Building libVEX")
self.execute(_build_pyvex, (), msg="Building libpyvex")
self.execute(_shuffle_files, (), msg="Copying libraries and headers")
self.execute(_build_ffi, (), msg="Creating CFFI defs file")
_develop.run(self)
cmdclass['develop'] = develop
class bdist_egg(_bdist_egg):
def run(self):
self.run_command('build')
_bdist_egg.run(self)
cmdclass['bdist_egg'] = bdist_egg
except ImportError:
print("Proper 'develop' support unavailable.")
if 'bdist_wheel' in sys.argv and '--plat-name' not in sys.argv:
sys.argv.append('--plat-name')
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
sys.argv.append('manylinux1_' + platform.machine())
else:
# https://www.python.org/dev/peps/pep-0425/
sys.argv.append(name.replace('.', '_').replace('-', '_'))
setup(
name="pyvex", version='7.8.7.1', description="A Python interface to libVEX and VEX IR",
url='https://github.com/angr/pyvex',
packages=packages,
cmdclass=cmdclass,
install_requires=[
'pycparser',
'cffi>=1.0.3',
'archinfo>=7.8.7.1',
'bitstring',
'future',
],
setup_requires=[ 'pycparser', 'cffi>=1.0.3' ],
include_package_data=True,
package_data={
'pyvex': ['lib/*', 'include/*']
}
)