-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsetup.py
171 lines (125 loc) · 5.27 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
import os
import re
import sys
import platform
import subprocess
import glob
import setuptools
import pathlib
from pkg_resources import Distribution, get_distribution
from setuptools import setup, Extension
from setuptools import Extension
from setuptools.command.build_ext import build_ext, copy_file
from distutils.version import LooseVersion
from distutils import log
MIN_SETUPTOOLS_VERSION = "31.0.0"
assert (LooseVersion(setuptools.__version__) >= LooseVersion(MIN_SETUPTOOLS_VERSION)), "pyJadx requires a setuptools version '{}' or higher (pip install setuptools --upgrade)".format(MIN_SETUPTOOLS_VERSION)
CURRENT_DIR = pathlib.Path(__file__).parent
class PyJadxDistribution(setuptools.Distribution):
global_options = setuptools.Distribution.global_options + [
('static-jvm', None, 'Link against JVM statically'),
]
def __init__(self, attrs=None):
self.static_jvm = False
super().__init__(attrs)
class Module(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(os.path.join(CURRENT_DIR))
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
for ext in self.extensions:
self.build_extension(ext)
self.copy_extensions_to_source()
def build_extension(self, ext):
jobs = self.parallel if self.parallel else 1
fullname = self.get_ext_fullname(ext.name)
filename = self.get_ext_filename(fullname)
source_dir = ext.sourcedir
build_temp = self.build_temp
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
static_jvm = "ON" if self.distribution.static_jvm else "OFF"
cmake_library_output_directory = os.path.abspath(os.path.dirname(build_temp))
cmake_args = [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}'.format(cmake_library_output_directory),
'-DPYTHON_EXECUTABLE={}'.format(sys.executable),
'-DJVM_STATIC_LINK={}'.format(static_jvm),
]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), cmake_library_output_directory)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
else:
cmake_args += ['-A', 'x86']
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
env = os.environ.copy()
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)
log.info("Using {} jobs".format(jobs))
subprocess.check_call(['make', '-j', str(jobs), 'pyjadx'], cwd=self.build_temp)
pyjadx_dst = os.path.join(self.build_lib, self.get_ext_filename(self.get_ext_fullname(ext.name)))
libsuffix = pyjadx_dst.split(".")[-1]
pyjadx_path = os.path.join(cmake_library_output_directory, "pyjadx.{}".format(libsuffix))
if not os.path.exists(self.build_lib):
os.makedirs(self.build_lib)
copy_file(
pyjadx_path, pyjadx_dst, verbose=self.verbose,
dry_run=self.dry_run)
# From setuptools-git-version
command = 'git describe --tags --long --dirty'
is_tagged_cmd = 'git tag --list --points-at=HEAD'
fmt = '{tag}.dev2'
fmt_tagged = '{tag}'
def format_version(version, fmt=fmt):
parts = version.split('-')
assert len(parts) in (3, 4)
dirty = len(parts) == 4
tag, count, sha = parts[:3]
if count == '0' and not dirty:
return tag
return fmt.format(tag=tag, gitsha=sha.lstrip('g'))
def get_git_version(is_tagged):
git_version = subprocess.check_output(command.split()).decode('utf-8').strip()
if is_tagged:
return format_version(version=git_version, fmt=fmt_tagged)
return format_version(version=git_version, fmt=fmt)
def check_if_tagged():
output = subprocess.check_output(is_tagged_cmd.split()).decode('utf-8').strip()
return output != ""
def get_pkg_info_version(pkg_info_file):
dist_info = Distribution.from_filename(CURRENT_DIR / "pyjadx.egg-info")
pkg = get_distribution('pyjadx')
return pkg.version
def get_version():
version = "0.0.0"
pkg_info = CURRENT_DIR / "pyjadx.egg-info" / "PKG-INFO"
git_dir = CURRENT_DIR / ".git"
if git_dir.is_dir():
is_tagged = False
try:
is_tagged = check_if_tagged()
except:
is_tagged = False
try:
return get_git_version(is_tagged)
except:
pass
if pkg_info.is_file():
return get_pkg_info_version(pkg_info)
version = get_version()
setup(
distclass=PyJadxDistribution,
ext_modules=[Module('pyjadx')],
cmdclass=dict(build_ext=CMakeBuild),
version=version
)