forked from kivy/pyjnius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
275 lines (226 loc) · 7.79 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
'''
Setup.py for creating a binary distribution.
'''
from __future__ import print_function
try:
from setuptools import setup, Extension
except ImportError:
from distutils.core import setup, Extension
try:
import subprocess32 as subprocess
except ImportError:
import subprocess
import os
from os import environ
from os.path import dirname, join, exists, realpath
import sys
from platform import machine
from setup_sdist import SETUP_KWARGS
PY2 = sys.version_info < (3, 0, 0)
def getenv(key):
'''Get value from environment and decode it.'''
val = environ.get(key)
if val is not None:
if not PY2:
try:
return val.decode()
except AttributeError:
return val
return val
FILES = [
'jni.pxi',
'jnius_compat.pxi',
'jnius_conversion.pxi',
'jnius_export_class.pxi',
'jnius_export_func.pxi',
'jnius_jvm_android.pxi',
'jnius_jvm_desktop.pxi',
'jnius_jvm_dlopen.pxi',
'jnius_localref.pxi',
'jnius.pyx',
'jnius_utils.pxi',
]
LIBRARIES = []
LIBRARY_DIRS = []
LIB_LOCATION = None
EXTRA_LINK_ARGS = []
INCLUDE_DIRS = []
INSTALL_REQUIRES = ['six>=1.7.0']
# detect Python for android
PLATFORM = sys.platform
NDKPLATFORM = getenv('NDKPLATFORM')
if NDKPLATFORM is not None and getenv('LIBLINK'):
PLATFORM = 'android'
# detect cython
try:
from Cython.Distutils import build_ext
INSTALL_REQUIRES.append('cython')
except ImportError:
# pylint: disable=ungrouped-imports
try:
from setuptools.command.build_ext import build_ext
except ImportError:
from distutils.command.build_ext import build_ext
if PLATFORM != 'android':
print('\n\nYou need Cython to compile Pyjnius.\n\n')
raise
# On Android we expect to see 'c' files lying about.
# and we go ahead with the 'desktop' file? Odd.
FILES = [fn[:-3] + 'c' for fn in FILES if fn.endswith('pyx')]
def find_javac(possible_homes):
'''Find javac in all possible locations.'''
name = "javac.exe" if sys.platform == "win32" else "javac"
for home in possible_homes:
for javac in [join(home, name), join(home, 'bin', name)]:
if exists(javac):
return javac
return name # Fall back to "hope it's on the path"
def compile_native_invocation_handler(*possible_homes):
'''Find javac and compile NativeInvocationHandler.java.'''
javac = find_javac(possible_homes)
subprocess.check_call([
javac, '-target', '1.6', '-source', '1.6',
join('jnius', 'src', 'org', 'jnius', 'NativeInvocationHandler.java')
])
if PLATFORM == 'android':
# for android, we use SDL...
LIBRARIES = ['sdl', 'log']
LIBRARY_DIRS = ['libs/' + getenv('ARCH')]
elif PLATFORM == 'darwin':
FRAMEWORK = subprocess.Popen(
'/usr/libexec/java_home',
stdout=subprocess.PIPE, shell=True).communicate()[0]
if not PY2:
FRAMEWORK = FRAMEWORK.decode('utf-8')
FRAMEWORK = FRAMEWORK.strip()
if not FRAMEWORK:
raise Exception('You must install Java on your Mac OS X distro')
if '1.6' in FRAMEWORK:
LIB_LOCATION = '../Libraries/libjvm.dylib'
INCLUDE_DIRS = [join(
FRAMEWORK, (
'System/Library/Frameworks/'
'JavaVM.framework/Versions/Current/Headers'
)
)]
else:
LIB_LOCATION = 'jre/lib/server/libjvm.dylib'
# We want to favor Java installation declaring JAVA_HOME
if getenv('JAVA_HOME'):
FRAMEWORK = getenv('JAVA_HOME')
FULL_LIB_LOCATION = join(FRAMEWORK, LIB_LOCATION)
if not exists(FULL_LIB_LOCATION):
# In that case, the Java version is very likely >=9.
# So we need to modify the `libjvm.so` path.
LIB_LOCATION = 'lib/server/libjvm.dylib'
INCLUDE_DIRS = [
'{0}/include'.format(FRAMEWORK),
'{0}/include/darwin'.format(FRAMEWORK)
]
print('JAVA_HOME: {0}\n'.format(FRAMEWORK))
compile_native_invocation_handler(FRAMEWORK)
else:
# Note: if on Windows, set ONLY JAVA_HOME
# not on android or osx, we need to search the JDK_HOME
JDK_HOME = getenv('JDK_HOME')
if not JDK_HOME:
if PLATFORM == 'win32':
TMP_JDK_HOME = getenv('JAVA_HOME')
print(TMP_JDK_HOME)
if not TMP_JDK_HOME:
raise Exception('Unable to find JAVA_HOME')
# Remove /bin if it's appended to JAVA_HOME
if TMP_JDK_HOME[-3:] == 'bin':
TMP_JDK_HOME = TMP_JDK_HOME[:-4]
# Check whether it's JDK
if exists(join(TMP_JDK_HOME, 'bin', 'javac.exe')):
JDK_HOME = TMP_JDK_HOME
else:
JDK_HOME = realpath(
subprocess.check_output(
['which', 'javac']
).decode('utf-8').strip()
).replace('bin/javac', '')
if not JDK_HOME or not exists(JDK_HOME):
raise Exception('Unable to determine JDK_HOME')
JRE_HOME = None
if exists(join(JDK_HOME, 'jre')):
JRE_HOME = join(JDK_HOME, 'jre')
if PLATFORM != 'win32' and not JRE_HOME:
JRE_HOME = realpath(
subprocess.check_output(
['which', 'java']
).decode('utf-8').strip()
).replace('bin/java', '')
# This dictionary converts values from platform.machine()
# to a "cpu" string. It is needed to set the correct lib path,
# found in the JRE_HOME, e.g.: <JRE_HOME>/lib/<cpu>/.
MACHINE2CPU = {
"i686": "i386",
"x86_64": "amd64",
"armv7l": "arm"
}
if machine() in MACHINE2CPU.keys():
CPU = MACHINE2CPU[machine()]
else:
print(
"WARNING: Not able to assign machine()"
" = %s to a cpu value!" % machine()
)
print(" Using cpu = 'i386' instead!")
CPU = 'i386'
if PLATFORM == 'win32':
INCL_DIR = join(JDK_HOME, 'include', 'win32')
LIBRARIES = ['jvm']
else:
INCL_DIR = join(JDK_HOME, 'include', 'linux')
LIB_LOCATION = 'jre/lib/{}/server/libjvm.so'.format(CPU)
if isinstance(JRE_HOME, bytes):
JAVA_HOME = dirname(JRE_HOME.decode())
else:
JAVA_HOME = dirname(JRE_HOME)
FULL_LIB_LOCATION = join(JAVA_HOME, LIB_LOCATION)
if not exists(FULL_LIB_LOCATION):
# In that case, the Java version is very likely >=9.
# So we need to modify the `libjvm.so` path.
LIB_LOCATION = 'lib/server/libjvm.so'
INCLUDE_DIRS = [
join(JDK_HOME, 'include'),
INCL_DIR
]
if PLATFORM == 'win32':
if isinstance(JRE_HOME, bytes):
JRE_HOME = JRE_HOME.decode('utf-8')
LIBRARY_DIRS = [
join(JDK_HOME, 'lib'),
join(JDK_HOME, 'bin', 'server')
]
print('JDK_HOME: {0}\n'.format(JDK_HOME))
print('JRE_HOME: {0}\n'.format(JRE_HOME))
compile_native_invocation_handler(JDK_HOME, JRE_HOME)
# generate the config.pxi
with open(join(dirname(__file__), 'jnius', 'config.pxi'), 'w') as fd:
fd.write('DEF JNIUS_PLATFORM = {0!r}\n\n'.format(PLATFORM))
if not PY2:
fd.write('DEF JNIUS_PYTHON3 = True\n\n')
else:
fd.write('DEF JNIUS_PYTHON3 = False\n\n')
if LIB_LOCATION is not None:
fd.write('DEF JNIUS_LIB_SUFFIX = {0!r}\n\n'.format(LIB_LOCATION))
# pop setup.py from included files in the installed package
SETUP_KWARGS['py_modules'].remove('setup')
# create the extension
setup(
cmdclass={'build_ext': build_ext},
install_requires=INSTALL_REQUIRES,
ext_modules=[
Extension(
'jnius', [join('jnius', x) for x in FILES],
libraries=LIBRARIES,
library_dirs=LIBRARY_DIRS,
include_dirs=INCLUDE_DIRS,
extra_link_args=EXTRA_LINK_ARGS
)
],
**SETUP_KWARGS
)