forked from kivy/pyjnius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
122 lines (99 loc) · 3.13 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
'''
Setup.py for creating a binary distribution.
'''
from os import environ
from os.path import dirname, join
import subprocess
import sys
from setup_sdist import SETUP_KWARGS
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
# XXX hack to be able to import jnius.env withough having build
# jnius.jnius yet, better solution welcome
syspath = sys.path[:]
sys.path.insert(0, 'jnius')
from env import get_java_setup
sys.path = syspath
def getenv(key):
'''Get value from environment and decode it.'''
val = environ.get(key)
if val is not None:
try:
return val.decode()
except AttributeError:
return val
return val
PYX_FILES = [
'jnius.pyx',
]
PXI_FILES = [
'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_nativetypes3.pxi',
'jnius_proxy.pxi',
'jnius.pyx',
'jnius_utils.pxi'
]
EXTRA_LINK_ARGS = []
# detect Python for android
PLATFORM = sys.platform
NDKPLATFORM = getenv('NDKPLATFORM')
if NDKPLATFORM is not None and getenv('LIBLINK'):
PLATFORM = 'android'
# detect platform
if PLATFORM == 'android':
PYX_FILES = [fn[:-3] + 'c' for fn in PYX_FILES]
JAVA=get_java_setup(PLATFORM)
assert JAVA.is_jdk(), "You need a JDK, we only found a JRE. Try setting JAVA_HOME"
def compile_native_invocation_handler(java):
'''Find javac and compile NativeInvocationHandler.java.'''
javac = java.get_javac()
source_level = '8'
try:
subprocess.check_call([
javac, '-target', source_level, '-source', source_level,
join('jnius', 'src', 'org', 'jnius', 'NativeInvocationHandler.java')
])
except FileNotFoundError:
subprocess.check_call([
javac.replace('"', ''), '-target', source_level, '-source', source_level,
join('jnius', 'src', 'org', 'jnius', 'NativeInvocationHandler.java')
])
compile_native_invocation_handler(JAVA)
# generate the config.pxi
with open(join(dirname(__file__), 'jnius', 'config.pxi'), 'w') as fd:
if PLATFORM == 'android':
cython3 = environ.get('ANDROID_PYJNIUS_CYTHON_3', '0') == '1'
else:
import Cython
cython3 = Cython.__version__.startswith('3.')
fd.write('DEF JNIUS_PLATFORM = {0!r}\n\n'.format(PLATFORM))
# record the Cython version, to address #669
fd.write(f'DEF JNIUS_CYTHON_3 = {cython3}')
# pop setup.py from included files in the installed package
SETUP_KWARGS['py_modules'].remove('setup')
ext_modules = [
Extension(
'jnius',
[join('jnius', x) for x in PYX_FILES],
depends=[join('jnius', x) for x in PXI_FILES],
libraries=JAVA.get_libraries(),
library_dirs=JAVA.get_library_dirs(),
include_dirs=JAVA.get_include_dirs(),
extra_link_args=EXTRA_LINK_ARGS,
)
]
for ext_mod in ext_modules:
ext_mod.cython_directives = {'language_level': 3}
# create the extension
setup(
cmdclass={'build_ext': build_ext},
ext_modules=ext_modules,
**SETUP_KWARGS
)