pyjnius/setup.py

209 lines
6.5 KiB
Python
Raw Normal View History

from __future__ import print_function
try:
from setuptools import setup, Extension
except ImportError:
from distutils.core import setup, Extension
2012-08-14 01:42:43 +00:00
from os import environ
from os.path import dirname, join, exists
2012-08-14 01:42:43 +00:00
import sys
from platform import machine
2017-03-23 17:56:02 +00:00
PY3 = sys.version_info >= (3, 0, 0)
2012-08-14 01:42:43 +00:00
2015-11-03 13:57:04 +00:00
def getenv(key):
val = environ.get(key)
if val is not None:
if PY3:
try:
return val.decode()
except AttributeError:
return val
return val
2015-11-03 13:57:04 +00:00
2017-03-23 17:56:02 +00:00
files = [
'jni.pxi',
'jnius_conversion.pxi',
'jnius_export_class.pxi',
'jnius_export_func.pxi',
'jnius_jvm_android.pxi',
'jnius_jvm_desktop.pxi',
2015-05-01 16:16:58 +00:00
'jnius_jvm_dlopen.pxi',
'jnius_localref.pxi',
'jnius.pyx',
'jnius_utils.pxi',
]
2012-08-14 01:42:43 +00:00
libraries = []
library_dirs = []
lib_location = None
2012-08-14 01:42:43 +00:00
extra_link_args = []
include_dirs = []
2016-02-11 19:53:24 +00:00
install_requires = ['six>=1.7.0']
2012-08-14 01:42:43 +00:00
# detect Python for android
platform = sys.platform
2015-11-03 13:57:04 +00:00
ndkplatform = getenv('NDKPLATFORM')
if ndkplatform is not None and getenv('LIBLINK'):
2012-08-14 01:42:43 +00:00
platform = 'android'
# detect cython
try:
from Cython.Distutils import build_ext
install_requires.append('cython')
2012-08-14 01:42:43 +00:00
except ImportError:
try:
from setuptools.command.build_ext import build_ext
except ImportError:
from distutils.command.build_ext import build_ext
if platform != 'android':
2014-08-19 12:35:20 +00:00
print('\n\nYou need Cython to compile Pyjnius.\n\n')
raise
2015-05-01 16:16:58 +00:00
# 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')]
2012-08-14 01:42:43 +00:00
if platform == 'android':
# for android, we use SDL...
libraries = ['sdl', 'log']
2015-11-03 13:57:04 +00:00
library_dirs = ['libs/' + getenv('ARCH')]
elif platform == 'darwin':
import subprocess
2017-03-23 17:56:02 +00:00
framework = subprocess.Popen(
'/usr/libexec/java_home',
stdout=subprocess.PIPE, shell=True).communicate()[0]
if PY3:
2016-02-07 14:09:40 +00:00
framework = framework.decode()
framework = framework.strip()
2016-02-07 14:09:40 +00:00
print('java_home: {0}\n'.format(framework))
if not framework:
raise Exception('You must install Java on your Mac OS X distro')
2015-05-01 01:29:55 +00:00
if '1.6' in framework:
2015-05-01 16:16:58 +00:00
lib_location = '../Libraries/libjvm.dylib'
2015-05-01 01:29:55 +00:00
include_dirs = [join(framework, 'System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers')]
else:
2015-05-01 16:16:58 +00:00
lib_location = 'jre/lib/server/libjvm.dylib'
2017-03-23 17:56:02 +00:00
include_dirs = [
'{0}/include'.format(framework),
'{0}/include/darwin'.format(framework)
]
2012-08-14 01:42:43 +00:00
else:
import subprocess
# otherwise, we need to search the JDK_HOME
2015-11-03 13:57:04 +00:00
jdk_home = getenv('JDK_HOME')
2012-08-14 01:42:43 +00:00
if not jdk_home:
if platform == 'win32':
2015-11-03 13:57:04 +00:00
env_var = getenv('JAVA_HOME')
if env_var and 'jdk' in env_var:
jdk_home = env_var
# Remove /bin if it's appended to JAVA_HOME
if jdk_home[-3:] == 'bin':
jdk_home = jdk_home[:-4]
else:
2017-03-23 17:56:02 +00:00
jdk_home = subprocess.Popen(
'readlink -f `which javac` | sed "s:bin/javac::"',
shell=True, stdout=subprocess.PIPE).communicate()[0].strip()
if jdk_home is not None and PY3:
jdk_home = jdk_home.decode()
if not jdk_home or not exists(jdk_home):
2012-08-14 01:42:43 +00:00
raise Exception('Unable to determine JDK_HOME')
jre_home = None
if exists(join(jdk_home, 'jre')):
jre_home = join(jdk_home, 'jre')
2012-08-14 01:42:43 +00:00
if not jre_home:
2017-03-23 17:56:02 +00:00
jre_home = subprocess.Popen(
'readlink -f `which java` | sed "s:bin/java::"',
shell=True, stdout=subprocess.PIPE).communicate()[0].strip()
2012-08-14 01:42:43 +00:00
if not jre_home:
raise Exception('Unable to determine JRE_HOME')
2017-03-23 17:38:55 +00:00
# This dictionary converts values from platform.machine() to a "cpu" string.
2017-03-23 17:56:02 +00:00
# It is needed to set the correct lib path, found in the jre_home, eg.
# <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'
2017-03-23 17:38:55 +00:00
2013-11-26 04:44:46 +00:00
if platform == 'win32':
incl_dir = join(jdk_home, 'include', 'win32')
2015-05-01 16:16:58 +00:00
libraries = ['jvm']
2013-11-26 04:44:46 +00:00
else:
incl_dir = join(jdk_home, 'include', 'linux')
lib_location = 'jre/lib/{}/server/libjvm.so'.format(cpu)
2013-11-26 04:44:46 +00:00
2012-08-14 01:42:43 +00:00
include_dirs = [
2017-03-23 17:56:02 +00:00
join(jdk_home, 'include'),
incl_dir
]
2015-05-01 16:16:58 +00:00
if platform == 'win32':
library_dirs = [
2017-03-23 17:56:02 +00:00
join(jdk_home, 'lib'),
join(jre_home, 'bin', 'server')
]
2012-08-14 01:42:43 +00:00
# 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 PY3:
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))
2012-08-14 01:42:43 +00:00
with open(join('jnius', '__init__.py')) as fd:
versionline = [x for x in fd.readlines() if x.startswith('__version__')]
version = versionline[0].split("'")[-2]
2012-08-14 01:42:43 +00:00
# create the extension
2017-03-23 17:56:02 +00:00
setup(
name='jnius',
version=version,
cmdclass={'build_ext': build_ext},
packages=['jnius'],
py_modules=['jnius_config'],
url='https://pyjnius.readthedocs.io',
author='Kivy Team and other contributors',
author_email='kivy-dev@googlegroups.com',
license='MIT',
description='Python library to access Java classes',
install_requires=install_requires,
ext_package='jnius',
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
)
],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
2015-03-02 10:30:23 +00:00
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Operating System :: MacOS :: OS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
2017-03-23 17:38:55 +00:00
'Programming Language :: Python :: 3.6',
2017-03-23 17:56:02 +00:00
'Topic :: Software Development :: Libraries :: Application Frameworks'
]
)