forked from cea-sec/miasm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
344 lines (318 loc) · 11.7 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#! /usr/bin/env python2
from __future__ import print_function
from distutils.core import setup, Extension
from distutils.util import get_platform
from distutils.sysconfig import get_python_lib, get_config_vars
from distutils.dist import DistributionMetadata
from distutils.command.install_data import install_data
from tempfile import TemporaryFile
import fnmatch
import io
import os
import platform
from shutil import copy2, copyfile, rmtree
import sys
import tempfile
import atexit
is_win = platform.system() == "Windows"
is_mac = platform.system() == "Darwin"
is_64bit = platform.architecture()[0] == "64bit"
if is_win:
import winreg
def set_extension_compile_args(extension):
rel_lib_path = extension.name.replace('.', '/')
abs_lib_path = os.path.join(get_python_lib(), rel_lib_path)
lib_name = abs_lib_path + '.so'
extension.extra_link_args = [ '-Wl,-install_name,' + lib_name]
class smart_install_data(install_data):
"""Replacement for distutils.command.install_data to handle
configuration files location.
"""
def run(self):
# install files to /etc when target was /usr(/local)/etc
self.data_files = [
(path, files) for path, files in self.data_files
if path # skip README.md or any file with an empty path
]
return install_data.run(self)
def win_get_llvm_reg():
REG_PATH = "SOFTWARE\\LLVM\\LLVM"
try:
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, REG_PATH, 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
except FileNotFoundError:
pass
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, REG_PATH, 0, winreg.KEY_READ)
def win_find_clang_path():
try:
with win_get_llvm_reg() as rkey:
return winreg.QueryValueEx(rkey, None)[0]
except FileNotFoundError:
return None
def win_use_clang():
# Recent (>= 8 ?) LLVM versions does not ship anymore a cl.exe binary in
# the msbuild-bin directory. Thus, we need to
# * copy-paste bin/clang-cl.exe into a temporary directory
# * rename it to cl.exe
# * add that path first in %Path%
# * clean this mess on exit
# We could use the build directory created by distutils for this, but it
# seems non trivial to gather
# (https://stackoverflow.com/questions/12896367/reliable-way-to-get-the-build-directory-from-within-setup-py).
clang_path = win_find_clang_path()
if clang_path is None:
return False
tmpdir = tempfile.mkdtemp(prefix="llvm")
copyfile(os.path.join(clang_path, "bin", "clang-cl.exe"), os.path.join(tmpdir, "cl.exe"))
os.environ['Path'] = "%s;%s" % (tmpdir, os.environ["Path"])
atexit.register(lambda dir_: rmtree(dir_), tmpdir)
return True
win_force_clang = False
if is_win and is_64bit:
# We do not change to clang if under 32 bits, because even with Clang we
# don't have uint128_t with the 32 bits ABI.
win_force_clang = win_use_clang()
if not win_force_clang:
print("Warning: couldn't find a Clang/LLVM installation. Some runtime functions needed by the jitter won't be compiled.")
def buil_all():
packages=[
"miasm",
"miasm/arch",
"miasm/arch/x86",
"miasm/arch/arm",
"miasm/arch/aarch64",
"miasm/arch/msp430",
"miasm/arch/mep",
"miasm/arch/sh4",
"miasm/arch/mips32",
"miasm/arch/ppc",
"miasm/core",
"miasm/expression",
"miasm/ir",
"miasm/ir/translators",
"miasm/analysis",
"miasm/os_dep",
"miasm/os_dep/linux",
"miasm/loader",
"miasm/jitter",
"miasm/jitter/arch",
"miasm/jitter/loader",
]
ext_modules_all = [
Extension(
"miasm.jitter.VmMngr",
[
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/bn.c",
]
),
Extension(
"miasm.jitter.arch.JitCore_x86",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/op_semantics.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_x86.c"
]
),
Extension(
"miasm.jitter.arch.JitCore_arm",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/op_semantics.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_arm.c"
]
),
Extension(
"miasm.jitter.arch.JitCore_aarch64",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/op_semantics.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_aarch64.c"
]
),
Extension(
"miasm.jitter.arch.JitCore_msp430",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/op_semantics.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_msp430.c"
]
),
Extension(
"miasm.jitter.arch.JitCore_mep",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_mep.c"
]
),
Extension(
"miasm.jitter.arch.JitCore_mips32",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/op_semantics.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_mips32.c"
]
),
Extension(
"miasm.jitter.arch.JitCore_ppc32",
[
"miasm/jitter/JitCore.c",
"miasm/jitter/vm_mngr.c",
"miasm/jitter/vm_mngr_py.c",
"miasm/jitter/op_semantics.c",
"miasm/jitter/bn.c",
"miasm/jitter/arch/JitCore_ppc32.c"
],
depends=[
"miasm/jitter/arch/JitCore_ppc32.h",
"miasm/jitter/arch/JitCore_ppc32_regs.h",
"miasm/jitter/bn.h",
]
),
Extension("miasm.jitter.Jitllvm",
["miasm/jitter/Jitllvm.c",
"miasm/jitter/bn.c",
"miasm/runtime/udivmodti4.c",
"miasm/runtime/divti3.c",
"miasm/runtime/udivti3.c"
]),
Extension("miasm.jitter.Jitgcc",
["miasm/jitter/Jitgcc.c",
"miasm/jitter/bn.c",
]),
]
if is_win:
# Force setuptools to use whatever msvc version installed
os.environ['MSSdk'] = '1'
os.environ['DISTUTILS_USE_SDK'] = '1'
if win_force_clang:
march = "-m64" if is_64bit else "-m32"
for extension in ext_modules_all:
extension.extra_compile_args = [march]
elif is_mac:
for extension in ext_modules_all:
set_extension_compile_args(extension)
cfg_vars = get_config_vars()
cfg_vars['LDSHARED'] = cfg_vars['LDSHARED'].replace('-bundle', '-dynamiclib')
print("building")
build_ok = False
for name, ext_modules in [("all", ext_modules_all),
]:
print("build with", repr(name))
try:
s = setup(
name = "miasm",
version = __import__("miasm").VERSION,
packages = packages,
data_files=[('', ["README.md"])],
package_data = {
"miasm": [
"jitter/*.h",
"jitter/arch/*.h",
"VERSION"
]
},
cmdclass={"install_data": smart_install_data},
ext_modules = ext_modules,
# Metadata
author = "Fabrice Desclaux",
author_email = "[email protected]",
description = "Machine code manipulation library",
license = "GPLv2",
long_description=long_description,
long_description_content_type=long_description_content_type,
keywords = [
"reverse engineering",
"disassembler",
"emulator",
"symbolic execution",
"intermediate representation",
"assembler",
],
classifiers=[
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.6",
],
url = "http://miasm.re",
)
except SystemExit as e:
print(repr(e))
continue
build_ok = True
break
if not build_ok:
raise ValueError("Unable to build Miasm!")
print("build", name)
# we copy libraries from build dir to current miasm directory
build_base = "build"
if "build" in s.command_options:
if "build_base" in s.command_options["build"]:
build_base = s.command_options["build"]["build_base"]
print(build_base)
if is_win:
libs = []
for root, _, files in os.walk(build_base):
for filename in files:
if not filename.endswith(".lib"):
continue
f_path = os.path.join(root, filename)
libs.append(f_path)
lib_dirname = None
for dirname in os.listdir(build_base):
if not dirname.startswith("lib"):
continue
lib_dirname = dirname
break
jitters = []
for lib in libs:
filename = os.path.basename(lib)
dst = os.path.join(build_base, lib_dirname, "miasm", "jitter")
# Windows built libraries may have a name like VmMngr.cp38-win_amd64.lib
if not any([fnmatch.fnmatch(filename, pattern) for pattern in ["VmMngr.*lib", "Jitgcc.*lib", "Jitllvm.*lib"]]):
dst = os.path.join(dst, "arch")
dst = os.path.join(dst, filename)
if not os.path.isfile(dst):
print("Copying", lib, "to", dst)
copy2(lib, dst)
with io.open(os.path.join(os.path.abspath(os.path.dirname('__file__')),
'README.md'), encoding='utf-8') as fdesc:
long_description = fdesc.read()
long_description_content_type = 'text/markdown'
# Monkey patching (distutils does not handle Description-Content-Type
# from long_description_content_type parameter in setup()).
_write_pkg_file_orig = DistributionMetadata.write_pkg_file
def _write_pkg_file(self, file):
with TemporaryFile(mode="w+") as tmpfd:
_write_pkg_file_orig(self, tmpfd)
tmpfd.seek(0)
for line in tmpfd:
if line.startswith('Metadata-Version: '):
file.write('Metadata-Version: 2.1\n')
elif line.startswith('Description: '):
file.write('Description-Content-Type: %s; charset=UTF-8\n' %
long_description_content_type)
file.write(line)
else:
file.write(line)
DistributionMetadata.write_pkg_file = _write_pkg_file
buil_all()