forked from alibaba/pemja
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
253 lines (211 loc) · 7.45 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
################################################################################
#
# Copyright 2022 Alibaba Group Holding Limited.
#
# 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 io
import os
import sys
import sysconfig
import warnings
from setuptools import setup, Extension
from distutils.command.build_ext import build_ext as old_build_ext
if sys.version_info < (3, 7):
print('Python versions prior to 3.7 are not supported for PemJa.',
file=sys.stderr)
sys.exit(-1)
if sys.version_info >= (3, 11):
fmt = "Pemja may not yet support Python {}.{}."
warnings.warn(
fmt.format(*sys.version_info[:2]),
RuntimeWarning)
del fmt
this_directory = os.path.abspath(os.path.dirname(__file__))
version_file = os.path.join(this_directory, 'src/main/python/pemja/version.py')
try:
exec (open(version_file).read())
except IOError:
print("Failed to load PemJa version file for packaging. {0} not found!".format(version_file),
file=sys.stderr)
sys.exit(-1)
VERSION = __version__ # noqa
with io.open(os.path.join(this_directory, 'README.md'), 'r', encoding='utf-8') as f:
long_description = f.read()
_java_home = None
def get_java_home():
global _java_home
if _java_home is not None:
return _java_home
env_home = os.environ.get('JAVA_HOME')
if env_home:
if os.path.exists(env_home):
_java_home = env_home
return env_home
else:
print('Path {0} indicated by JAVA_HOME does not exist.'.format(env_home),
file=sys.stderr)
sys.exit(-1)
def is_osx():
return 'macosx' in sysconfig.get_platform()
def is_bsd():
return 'bsd' in sysconfig.get_platform()
def is_windows():
return 'win' in sysconfig.get_platform()
def get_python_libs():
libs = []
if not is_bsd():
libs.append('dl')
return libs
def get_files(dir, pattern):
ret = []
for root, dirs, files in os.walk(dir):
for f in files:
if f.endswith(pattern):
ret.append(os.path.join(root, f))
return ret
def is_apple_jdk():
return get_java_home() == '/System/Library/Frameworks/JavaVM.framework'
def get_java_linker_args():
if is_apple_jdk():
return ['-framework JavaVM']
return []
def get_java_libraries():
if is_windows():
return ['jvm']
return []
def get_java_lib_folders():
if not is_osx():
import fnmatch
if is_windows():
jre = os.path.join(get_java_home(), 'lib')
else:
jre = os.path.join(get_java_home(), 'jre', 'lib')
if not os.path.exists(jre):
jre = os.path.join(get_java_home(), 'lib')
folders = []
for root, dirnames, filenames in os.walk(jre):
if is_windows():
for filename in fnmatch.filter(filenames, '*jvm.lib'):
folders.append(os.path.join(
root, os.path.dirname(filename)))
else:
for filename in fnmatch.filter(filenames, '*jvm.so'):
folders.append(os.path.join(
root, os.path.dirname(filename)))
return list(set(folders))
return []
def get_java_include():
inc_name = 'include'
if is_apple_jdk():
inc_name = 'Headers'
inc = os.path.join(get_java_home(), inc_name)
if not os.path.exists(inc):
print("Include folder should be at '{0}' but doesn't exist. "
"Please check you've installed the JDK properly.".format(inc),
file=sys.stderr)
sys.exit(-1)
jni = os.path.join(inc, "jni.h")
if not os.path.exists(jni):
print("jni.h should be in '{0}' but doesn't exist. "
"Please check you've installed the JDK properly.".format(jni),
file=sys.stderr)
sys.exit(-1)
paths = [inc]
# Include platform specific headers if found
include_linux = os.path.join(inc, 'linux')
if os.path.exists(include_linux):
paths.append(include_linux)
include_darwin = os.path.join(inc, 'darwin')
if os.path.exists(include_darwin):
paths.append(include_darwin)
include_win32 = os.path.join(inc, 'win32')
if os.path.exists(include_win32):
paths.append(include_win32)
include_bsd = os.path.join(inc, 'freebsd')
if os.path.exists(include_bsd):
paths.append(include_bsd)
return paths
def get_src_include():
return ['src/main/c/Include']
def _is_using_gcc(obj):
is_gcc = False
if obj.compiler.compiler_type == 'unix':
cc = sysconfig.get_config_var("CC")
if not cc:
cc = ""
is_gcc = "gcc" in cc
return is_gcc
class build_ext(old_build_ext):
def build_extension(self, ext):
if _is_using_gcc(self):
if '-std=c99' not in ext.extra_compile_args:
ext.extra_compile_args.append('-std=c99')
old_build_ext.build_extension(self, ext)
def run(self):
old_build_ext.run(self)
if is_windows():
for lib in self.get_outputs():
dll = lib.replace('.pyd', '.dll')
self.copy_file(lib, dll)
extensions = ([
Extension(
name="pemja_core",
sources=get_files('src/main/c/pemja/core', '.c'),
libraries=get_java_libraries() + get_python_libs(),
library_dirs = get_java_lib_folders(),
extra_link_args=get_java_linker_args(),
include_dirs=get_java_include() + ['src/main/c/pemja/core/include'],
language=3),
Extension(
name="pemja_utils",
sources=get_files('src/main/c/pemja/utils', '.c'),
library_dirs = get_java_lib_folders(),
extra_link_args=get_java_linker_args(),
include_dirs=get_java_include() + ['src/main/c/pemja/utils/include'],
language=3)
])
PACKAGE_DATA = {
'pemja': ['README.txt']
}
PACKAGE_DIR = {
'': 'src/main/python'
}
setup(
name='pemja',
version=VERSION,
packages=["pemja"],
include_package_data=True,
package_dir=PACKAGE_DIR,
package_data=PACKAGE_DATA,
author='Apache Software Foundation',
license='https://www.apache.org/licenses/LICENSE-2.0',
author_email='hxbks2ks@gmail.com',
python_requires='>=3.7',
install_requires=['find-libpython'],
cmdclass={'build_ext': build_ext},
description='PemJa',
long_description=long_description,
long_description_content_type='text/markdown',
zip_safe=False,
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: Implementation :: CPython',
'Operating System :: Unix',
'Operating System :: MacOS', ],
ext_modules=extensions)