forked from fjh658/keystone-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·195 lines (160 loc) · 6.19 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
#!/usr/bin/env python
# Python binding for Keystone engine. Nguyen Anh Quynh <[email protected]>
import glob
import os
import platform
import shutil
import stat
import sys
from distutils import log
from distutils import dir_util
from distutils.command.build_clib import build_clib
from distutils.command.sdist import sdist
from distutils.core import setup
from distutils.sysconfig import get_python_lib
# prebuilt libraries for Windows - for sdist
PATH_LIB64 = "prebuilt/win64/keystone.dll"
PATH_LIB32 = "prebuilt/win32/keystone.dll"
# package name can be 'keystone-engine' or 'keystone-engine-windows'
PKG_NAME = 'keystone-engine'
if os.path.exists(PATH_LIB64) and os.path.exists(PATH_LIB32):
PKG_NAME = 'keystone-engine-windows'
VERSION = '0.9.1-3'
SYSTEM = sys.platform
# virtualenv breaks import, but get_python_lib() will work.
SITE_PACKAGES = os.path.join(get_python_lib(), "keystone")
if "--user" in sys.argv:
try:
from site import getusersitepackages
SITE_PACKAGES = os.path.join(getusersitepackages(), "keystone")
except ImportError:
pass
SETUP_DATA_FILES = []
# adapted from commit e504b81 of Nguyen Tan Cong
# Reference: https://docs.python.org/2/library/platform.html#cross-platform
is_64bits = sys.maxsize > 2**32
def copy_sources():
"""Copy the C sources into the source directory.
This rearranges the source files under the python distribution
directory.
"""
src = []
try:
dir_util.remove_tree("src/")
except (IOError, OSError):
pass
dir_util.copy_tree("../../llvm", "src/llvm/")
dir_util.copy_tree("../../include", "src/include/")
src.extend(glob.glob("../../*.h"))
src.extend(glob.glob("../../*.cpp"))
src.extend(glob.glob("../../*.inc"))
src.extend(glob.glob("../../*.def"))
src.extend(glob.glob("../../CMakeLists.txt"))
src.extend(glob.glob("../../CMakeUninstall.in"))
src.extend(glob.glob("../../*.txt"))
src.extend(glob.glob("../../*.TXT"))
src.extend(glob.glob("../../COPYING"))
src.extend(glob.glob("../../LICENSE*"))
src.extend(glob.glob("../../EXCEPTIONS-CLIENT"))
src.extend(glob.glob("../../README.md"))
src.extend(glob.glob("../../RELEASE_NOTES"))
src.extend(glob.glob("../../ChangeLog"))
src.extend(glob.glob("../../SPONSORS.TXT"))
src.extend(glob.glob("../../*.cmake"))
src.extend(glob.glob("../../*.sh"))
src.extend(glob.glob("../../*.bat"))
for filename in src:
outpath = os.path.join("./src/", os.path.basename(filename))
log.info("%s -> %s" % (filename, outpath))
shutil.copy(filename, outpath)
class custom_sdist(sdist):
"""Reshuffle files for distribution."""
def run(self):
# if prebuilt libraries are existent, then do not copy source
if os.path.exists(PATH_LIB64) and os.path.exists(PATH_LIB32):
return sdist.run(self)
copy_sources()
return sdist.run(self)
class custom_build_clib(build_clib):
"""Customized build_clib command."""
def run(self):
log.info('running custom_build_clib')
build_clib.run(self)
def finalize_options(self):
# We want build-clib to default to build-lib as defined by the "build"
# command. This is so the compiled library will be put in the right
# place along side the python code.
self.set_undefined_options('build',
('build_lib', 'build_clib'),
('build_temp', 'build_temp'),
('compiler', 'compiler'),
('debug', 'debug'),
('force', 'force'))
build_clib.finalize_options(self)
def build_libraries(self, libraries):
if SYSTEM in ("win32", "cygwin"):
# if Windows prebuilt library is available, then include it
if is_64bits and os.path.exists(PATH_LIB64):
SETUP_DATA_FILES.append(PATH_LIB64)
return
elif os.path.exists(PATH_LIB32):
SETUP_DATA_FILES.append(PATH_LIB32)
return
# build library from source if src/ is existent
if not os.path.exists('src'):
return
try:
for (lib_name, build_info) in libraries:
log.info("building '%s' library", lib_name)
# cd src/build
os.chdir("src")
os.mkdir("build")
os.chdir("build")
# platform description refers at https://docs.python.org/2/library/sys.html#sys.platform
if SYSTEM == "cygwin":
os.chmod("make.sh", stat.S_IREAD|stat.S_IEXEC)
if is_64bits:
os.system("KEYSTONE_BUILD_CORE_ONLY=yes ./make.sh cygwin-mingw64")
else:
os.system("KEYSTONE_BUILD_CORE_ONLY=yes ./make.sh cygwin-mingw32")
SETUP_DATA_FILES.append("src/build/keystone.dll")
else: # Unix
os.chmod("../make-share.sh", stat.S_IREAD|stat.S_IEXEC)
os.system("../make-share.sh lib_only")
if SYSTEM == "darwin":
SETUP_DATA_FILES.append("src/build/llvm/lib/libkeystone.dylib")
else: # Non-OSX
SETUP_DATA_FILES.append("src/build/llvm/lib/libkeystone.so")
# back to root dir
os.chdir("../..")
except:
pass
def dummy_src():
return []
setup(
provides=['keystone'],
packages=['keystone'],
name=PKG_NAME,
version=VERSION,
author='Nguyen Anh Quynh',
author_email='[email protected]',
description='Keystone assembler engine',
url='http://www.keystone-engine.org',
classifiers=[
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
],
requires=['ctypes'],
cmdclass=dict(
build_clib=custom_build_clib,
sdist=custom_sdist,
),
libraries=[(
'keystone', dict(
package='keystone',
sources=dummy_src()
),
)],
data_files=[(SITE_PACKAGES, SETUP_DATA_FILES)],
)