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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
# !/usr/bin/env python3
# Copyright 2014-2020 Brockmann Consult GmbH
#
# 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 glob
import os
import os.path
import platform
import shutil
import subprocess
import sys
import unittest
from distutils import log
from distutils.cmd import Command
from distutils.util import get_platform
from setuptools import setup
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.test import test
from setuptools.extension import Extension
import jpyutil
__author__ = jpyutil.__author__
__copyright__ = jpyutil.__copyright__
__license__ = jpyutil.__license__
__version__ = jpyutil.__version__
base_dir = os.path.dirname(os.path.relpath(__file__))
src_main_c_dir = os.path.join(base_dir, 'src', 'main', 'c')
src_test_py_dir = os.path.join(base_dir, 'src', 'test', 'python')
is_ci = os.environ.get('CI') == 'true'
sources = [
os.path.join(src_main_c_dir, 'jpy_module.c'),
os.path.join(src_main_c_dir, 'jpy_diag.c'),
os.path.join(src_main_c_dir, 'jpy_verboseexcept.c'),
os.path.join(src_main_c_dir, 'jpy_conv.c'),
os.path.join(src_main_c_dir, 'jpy_compat.c'),
os.path.join(src_main_c_dir, 'jpy_jtype.c'),
os.path.join(src_main_c_dir, 'jpy_jarray.c'),
os.path.join(src_main_c_dir, 'jpy_jobj.c'),
os.path.join(src_main_c_dir, 'jpy_jmethod.c'),
os.path.join(src_main_c_dir, 'jpy_jfield.c'),
os.path.join(src_main_c_dir, 'jni/org_jpy_PyLib.c'),
]
headers = [
os.path.join(src_main_c_dir, 'jpy_module.h'),
os.path.join(src_main_c_dir, 'jpy_diag.h'),
os.path.join(src_main_c_dir, 'jpy_conv.h'),
os.path.join(src_main_c_dir, 'jpy_compat.h'),
os.path.join(src_main_c_dir, 'jpy_jtype.h'),
os.path.join(src_main_c_dir, 'jpy_jarray.h'),
os.path.join(src_main_c_dir, 'jpy_jobj.h'),
os.path.join(src_main_c_dir, 'jpy_jmethod.h'),
os.path.join(src_main_c_dir, 'jpy_jfield.h'),
os.path.join(src_main_c_dir, 'jni/org_jpy_PyLib.h'),
]
# Python unit tests that just use Java runtime classes (rt.jar)
python_java_rt_tests = [
os.path.join(src_test_py_dir, 'jpy_rt_test.py'),
os.path.join(src_test_py_dir, 'jpy_mt_test.py'),
os.path.join(src_test_py_dir, 'jpy_diag_test.py'),
# os.path.join(src_test_py_dir, 'jpy_perf_test.py'),
]
# Python unit tests that require target/test-classes or target/classes
# available on the classpath
python_java_jpy_tests = [
os.path.join(src_test_py_dir, 'jpy_array_test.py'),
os.path.join(src_test_py_dir, 'jpy_field_test.py'),
os.path.join(src_test_py_dir, 'jpy_retval_test.py'),
os.path.join(src_test_py_dir, 'jpy_exception_test.py'),
os.path.join(src_test_py_dir, 'jpy_overload_test.py'),
os.path.join(src_test_py_dir, 'jpy_typeconv_test.py'),
os.path.join(src_test_py_dir, 'jpy_typeconv_test_pyobj.py'),
os.path.join(src_test_py_dir, 'jpy_typeres_test.py'),
os.path.join(src_test_py_dir, 'jpy_modretparam_test.py'),
os.path.join(src_test_py_dir, 'jpy_translation_test.py'),
os.path.join(src_test_py_dir, 'jpy_gettype_test.py'),
os.path.join(src_test_py_dir, 'jpy_reentrant_test.py'),
os.path.join(src_test_py_dir, 'jpy_java_embeddable_test.py'),
os.path.join(src_test_py_dir, 'jpy_obj_test.py'),
os.path.join(src_test_py_dir, 'jpy_eval_exec_test.py'),
os.path.join(src_test_py_dir, 'jpy_mt_eval_exec_test.py'),
]
# e.g. jdk_home_dir = '/home/marta/jdk1.7.0_15'
jdk_home_dir = jpyutil.find_jdk_home_dir()
if jdk_home_dir is None:
log.error('Error: environment variable "JAVA_HOME" must be set to a JDK (>= v1.7) installation directory')
exit(1)
log.info('Building a %s-bit library for a %s system with JDK at %s' % (
'64' if jpyutil.PYTHON_64BIT else '32', platform.system(), jdk_home_dir))
jvm_dll_file = jpyutil.find_jvm_dll_file(jdk_home_dir)
if not jvm_dll_file:
log.error('Error: Cannot find any JVM shared library')
exit(1)
lib_dir = os.path.join(base_dir, 'lib')
jpy_jar_file = os.path.join(lib_dir, 'jpy.jar')
jvm_dll_dir = os.path.dirname(jvm_dll_file)
include_dirs = [src_main_c_dir, os.path.join(jdk_home_dir, 'include')]
library_dirs = [jvm_dll_dir]
libraries = [jpyutil.JVM_LIB_NAME]
define_macros = []
extra_link_args = []
extra_compile_args = []
if platform.system() == 'Windows':
define_macros += [('WIN32', '1')]
include_dirs += [os.path.join(jdk_home_dir, 'include', 'win32')]
library_dirs += [os.path.join(jdk_home_dir, 'lib')]
elif platform.system() == 'Linux':
include_dirs += [os.path.join(jdk_home_dir, 'include', 'linux')]
libraries += ['dl']
extra_link_args += ['-Xlinker', '-rpath', jvm_dll_dir]
elif platform.system() == 'Darwin':
include_dirs += [os.path.join(jdk_home_dir, 'include', 'darwin')]
if is_ci:
# This has the effect of removing the non-portable LC_LOAD_DYLIB and LC_RPATH directives for libjvm.dylib from the .so files.
# See https://github.com/jpy-consortium/jpy/issues/79 for details.
libraries = None
library_dirs = None
else:
# Remove local build workaround for non-portable macOS wheels
# TODO: https://github.com/jpy-consortium/jpy/issues/80
library_dirs += [os.path.join(sys.exec_prefix, 'lib')]
extra_link_args += ['-Xlinker', '-rpath', jvm_dll_dir]
# ----------- Functions -------------
def _build_dir():
# this is hacky, but use distutils logic to get build dir. see: distutils.command.build
plat = '.%s-%d.%d' % (get_platform(), sys.version_info.major, sys.version_info.minor)
log.info('Platform specifier: "%s"' % plat)
path = os.path.join('build', 'lib' + plat)
log.info('Build directory path: "%s"' % path)
return path
def package_maven():
""" Run maven package lifecycle """
if not os.getenv('JAVA_HOME'):
# make sure Maven uses the same JDK which we have used to compile
# and link the C-code
os.environ['JAVA_HOME'] = jdk_home_dir
mvn_goal = 'package'
log.info("Executing Maven goal '" + mvn_goal + "'")
code = subprocess.call(['mvn', 'clean', mvn_goal, '-DskipTests', '-B'],
shell=platform.system() == 'Windows')
if code:
exit(code)
# Copy JAR results to lib/*.jar
if not os.path.exists(lib_dir):
os.mkdir(lib_dir)
target_dir = os.path.join(base_dir, 'target')
jar_files = glob.glob(os.path.join(target_dir, '*.jar'))
jar_files = [f for f in jar_files
if not (f.endswith('-sources.jar')
or f.endswith('-javadoc.jar'))]
if not jar_files:
log.error('Maven did not generate any JAR artifacts')
exit(1)
for jar_file in jar_files:
build_dir = _build_dir()
if os.path.exists(build_dir):
log.info('Build directory "%s" exists.' % build_dir)
else:
log.info('Creating missing build directory "%s".' % build_dir)
os.makedirs(build_dir)
log.info("Copying " + jar_file + " -> " + build_dir + "")
shutil.copy(jar_file, build_dir)
def _read(filename):
""" Helper function for reading in project files """
with open(filename, encoding='UTF-8') as file:
return file.read()
def test_python_java_rt():
""" Run Python test cases against Java runtime classes. """
sub_env = {'PYTHONPATH': _build_dir()}
import os
sub_env.update(dict(os.environ))
log.info('Executing Python unit tests (against Java runtime classes)...')
return jpyutil._execute_python_scripts(python_java_rt_tests, env=sub_env)
def test_python_java_classes():
""" Run Python tests against JPY test classes """
sub_env = {'PYTHONPATH': _build_dir()}
import os
sub_env.update(dict(os.environ))
log.info('Executing Python unit tests (against JPY test classes)...')
return jpyutil._execute_python_scripts(python_java_jpy_tests, env=sub_env)
def test_maven():
jpy_config = os.path.join(_build_dir(), 'jpyconfig.properties')
mvn_args = '-DargLine=-Xmx512m -Djpy.config=' + jpy_config + ' -Djpy.debug=true'
log.info("Executing Maven goal 'test' with arg line " + repr(mvn_args))
code = subprocess.call(['mvn', 'test', mvn_args], shell=platform.system() == 'Windows')
return code == 0
def _write_jpy_config(target_dir=None, install_dir=None):
"""
Write out a well-formed jpyconfig.properties file for easier Java
integration in a given location.
"""
if is_ci:
# We don't want to publish the properties for the CI build system.
return None
if not target_dir:
target_dir = _build_dir()
args = [sys.executable,
os.path.join(target_dir, 'jpyutil.py'),
'--jvm_dll', jvm_dll_file,
'--java_home', jdk_home_dir,
'--log_level', 'DEBUG',
'--req_java',
'--req_py']
if install_dir:
args.append('--install_dir')
args.append(install_dir)
log.info('Writing jpy configuration to %s using install_dir %s' % (target_dir, install_dir))
return subprocess.call(args)
def _copy_jpyutil():
src = os.path.relpath(jpyutil.__file__)
dest = _build_dir()
log.info('Copying %s to %s' % (src, dest))
shutil.copy(src, dest)
def _build_jpy():
# package_maven()
_copy_jpyutil()
_write_jpy_config()
def test_suite():
suite = unittest.TestSuite()
def test_python_with_java_runtime(self):
assert 0 == test_python_java_rt()
def test_python_with_java_classes(self):
assert 0 == test_python_java_classes()
def test_java(self):
assert test_maven()
suite.addTest(test_python_with_java_runtime)
suite.addTest(test_python_with_java_classes)
# comment out because the asynchronous nature of the PyObject GC in Java makes stopPython/startPython flakey.
# suite.addTest(test_java)
return suite
class MavenBuildCommand(Command):
""" Custom JPY Maven builder command """
description = 'run Maven to generate JPY jar'
user_options = [] # do not remove, needs to be stubbed out!
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
self.announce('Building JPY')
_build_jpy()
class JpyBuildBeforeTest(test):
""" Customization of SetupTools Install command for JPY """
def run(self):
self.run_command('build')
self.run_command('maven')
test.run(self)
class JpyInstallLib(install_lib):
""" Custom install_lib command for getting install_dir """
def run(self):
_write_jpy_config(install_dir=self.install_dir)
install_lib.run(self)
class JpyInstall(install):
""" Custom install command to trigger Maven steps """
def run(self):
self.run_command('build')
self.run_command('maven')
install.run(self)
setup(name='jpy',
description='Bi-directional Python-Java bridge',
long_description=_read('README.md') + '\n\n' + _read('CHANGES.md'),
version=__version__,
long_description_content_type='text/markdown',
platforms='Windows, Linux, Darwin',
author=__author__,
author_email='norman.fomferra@brockmann-consult.de',
maintainer='Brockmann Consult GmbH',
maintainer_email='norman.fomferra@brockmann-consult.de',
license=__license__,
url='https://github.com/jpy-consortium/jpy',
download_url='https://pypi.python.org/pypi/jpy/' + __version__,
py_modules=['jpyutil'],
ext_modules=[Extension('jpy',
sources=sources,
depends=headers,
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
define_macros=define_macros),
Extension('jdl',
sources=[os.path.join(src_main_c_dir, 'jni/org_jpy_DL.c')],
depends=[os.path.join(src_main_c_dir, 'jni/org_jpy_DL.h')],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
define_macros=define_macros),
],
test_suite='setup.test_suite',
cmdclass={
'maven': MavenBuildCommand,
'test': JpyBuildBeforeTest,
'install': JpyInstall,
'install_lib': JpyInstallLib
},
python_requires='>=3.9',
classifiers=['Development Status :: 5 - Production/Stable',
# Indicate who your project is intended for
'Intended Audience :: Developers',
# Pick your license as you wish (should match "license" above)
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Programming Language :: Python :: 3.14',
])
|