forked from QBDI/QBDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
141 lines (119 loc) · 4.81 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
#!/usr/bin/env python3
# This file is part of pyQBDI (python binding for QBDI).
#
# Copyright 2017 - 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import sys
import platform
import subprocess
import shutil
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion
def detect_QBDI_platform():
current_os = None
arch = None
if hasattr(sys.implementation, "_multiarch"):
if '-' in sys.implementation._multiarch:
base_arch, base_os = sys.implementation._multiarch.split('-')[:2]
else:
base_arch = platform.machine()
base_os = sys.implementation._multiarch
else:
base_arch = platform.machine()
base_os = platform.system()
base_arch = base_arch.lower()
base_os = base_os.lower()
if base_os == 'darwin':
current_os = 'osx'
elif base_os == 'windows':
current_os = 'windows'
elif base_os == 'linux':
current_os = 'linux'
if base_arch in ['amd64', 'amd', 'x64', 'x86_64', 'x86', 'i386', 'i686']:
# intel arch
if sys.maxsize > 2**32:
arch = "X86_64"
else:
arch = "X86"
elif base_arch in ['aarch64', 'arm64', 'aarch64_be', 'armv8b', 'armv8l']:
assert sys.maxsize > 2**32
arch = "AARCH64"
elif base_arch == "arm" or \
platform.machine().startswith('armv4') or \
platform.machine().startswith('armv5') or \
platform.machine().startswith('armv6') or \
platform.machine().startswith('armv7'):
assert sys.maxsize < 2**32
arch = "ARM"
if current_os and arch:
return (current_os, arch)
raise RuntimeError("Cannot determine the QBDI platform : system={}, machine={}, is64bits={}".format(
base_arch, base_os, sys.maxsize > 2**32))
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
@staticmethod
def has_ninja():
return bool(shutil.which('ninja'))
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
detected_platform, detected_arch = detect_QBDI_platform()
cmake_args = ['-DPYQBDI_OUTPUT_DIRECTORY=' + extdir,
'-DPython3_EXECUTABLE=' + sys.executable,
'-DCMAKE_BUILD_TYPE=Release',
'-DQBDI_PLATFORM=' + detected_platform,
'-DQBDI_ARCH=' + detected_arch,
'-DQBDI_BENCHMARK=OFF',
'-DQBDI_INSTALL=OFF',
'-DQBDI_INCLUDE_DOCS=OFF',
'-DQBDI_INCLUDE_PACKAGE=OFF',
'-DQBDI_SHARED_LIBRARY=OFF',
'-DQBDI_TEST=OFF',
'-DQBDI_TOOLS_FRIDAQBDI=OFF',
'-DQBDI_TOOLS_PYQBDI=ON',
]
build_args = ['--config', 'Release', '--']
if platform.system() == "Windows":
cmake_args += ["-G", "Ninja"]
else:
cmake_args += ['-DQBDI_TOOLS_QBDIPRELOAD=ON']
if self.has_ninja():
cmake_args += ["-G", "Ninja"]
else:
build_args.append('-j4')
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
self.distribution.get_version())
for var in ["Python3_ROOT_DIR"]:
if var in env:
cmake_args.append("-D{}={}".format(var, env[var]))
elif var.upper() in env:
cmake_args.append("-D{}={}".format(var, env[var.upper()]))
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
if not os.path.isdir(extdir):
raise RuntimeError("Compile Error : No library available.")
setup(
ext_modules=[CMakeExtension('pyqbdi')],
cmdclass={
"build_ext": CMakeBuild,
},
)