forked from angr/angr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
121 lines (94 loc) · 3.11 KB
/
setup.py
File metadata and controls
121 lines (94 loc) · 3.11 KB
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
# pylint: disable=missing-class-docstring
from __future__ import annotations
import glob
import importlib
import importlib.resources
import os
import shutil
import subprocess
import sys
from distutils.command.build import build as st_build
from setuptools import Command, setup
from setuptools.command.develop import develop as st_develop
from setuptools.errors import LibError
# Import setuptools_rust to ensure an error is raised if not installed
try:
_ = importlib.import_module("setuptools_rust")
except ImportError as err:
raise Exception("angr requires setuptools-rust to build") from err
if sys.platform == "darwin":
library_file = "unicornlib.dylib"
elif sys.platform in ("win32", "cygwin"):
library_file = "unicornlib.dll"
else:
library_file = "unicornlib.so"
def build_unicornlib():
try:
importlib.import_module("pyvex")
except ImportError as e:
raise LibError("You must install pyvex before building angr") from e
env = os.environ.copy()
env_data = (
("PYVEX_INCLUDE_PATH", "pyvex", "include"),
("PYVEX_LIB_PATH", "pyvex", "lib"),
("PYVEX_LIB_FILE", "pyvex", "lib\\pyvex.lib"),
)
for var, pkg, fnm in env_data:
base = importlib.resources.files(pkg)
for child in fnm.split("\\"):
base = base.joinpath(child)
env[var] = str(base)
if sys.platform == "win32":
cmd = ["nmake", "/f", "Makefile-win"]
elif shutil.which("gmake") is not None:
cmd = ["gmake"]
else:
cmd = ["make"]
try:
subprocess.run(cmd, cwd="native/unicornlib", env=env, check=True)
except FileNotFoundError as err:
raise LibError("Couldn't find " + cmd[0] + " in PATH") from err
except subprocess.CalledProcessError as err:
raise LibError("Error while building unicornlib: " + str(err)) from err
shutil.rmtree("angr/lib", ignore_errors=True)
os.mkdir("angr/lib")
shutil.copy(os.path.join("native/unicornlib", library_file), "angr")
def clean_unicornlib():
oglob = glob.glob("native/*.o")
oglob += glob.glob("native/*.obj")
oglob += glob.glob("native/*.so")
oglob += glob.glob("native/*.dll")
oglob += glob.glob("native/*.dylib")
for fname in oglob:
os.unlink(fname)
class build(st_build):
def run(self, *args):
self.execute(build_unicornlib, (), msg="Building unicornlib")
super().run(*args)
class clean(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
self.execute(clean, (), msg="Cleaning unicornlib")
class develop(st_develop):
def run(self):
self.run_command("build")
super().run()
cmdclass = {
"build": build,
"clean_unicornlib": clean,
"develop": develop,
}
try:
from setuptools.command.editable_wheel import editable_wheel as st_editable_wheel
class editable_wheel(st_editable_wheel):
def run(self):
self.run_command("build")
super().run()
cmdclass["editable_wheel"] = editable_wheel
except ModuleNotFoundError:
pass
setup(cmdclass=cmdclass)