Skip to content

Commit

Permalink
Merge @inclement's python 3 recipe
Browse files Browse the repository at this point in the history
References: kivy#1412
  • Loading branch information
opacam committed Oct 18, 2018
1 parent 381636a commit 9efead4
Show file tree
Hide file tree
Showing 18 changed files with 605 additions and 305 deletions.
3 changes: 2 additions & 1 deletion pythonforandroid/archs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_env(self, with_flags_in_cc=True):
env['CFLAGS'] = ' '.join([
'-DANDROID', '-mandroid', '-fomit-frame-pointer'
' -D__ANDROID_API__={}'.format(self.ctx._android_api),
])
])
env['LDFLAGS'] = ' '

sysroot = join(self.ctx._ndk_dir, 'sysroot')
Expand Down Expand Up @@ -133,6 +133,7 @@ def get_env(self, with_flags_in_cc=True):
env['PATH'] = environ['PATH']

env['ARCH'] = self.arch
env['NDK_API'] = str(self.ctx.ndk_api)

if self.ctx.python_recipe and self.ctx.python_recipe.from_crystax:
env['CRYSTAX_PYTHON_VERSION'] = self.ctx.python_recipe.version
Expand Down
12 changes: 9 additions & 3 deletions pythonforandroid/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,15 @@ def strip_libraries(self, arch):
warning('Can\'t find strip in PATH...')
return
strip = sh.Command(strip)
filens = shprint(sh.find, join(self.dist_dir, 'private'),
join(self.dist_dir, 'libs'),
'-iname', '*.so', _env=env).stdout.decode('utf-8')

if self.ctx.python_recipe.name == 'python2':
filens = shprint(sh.find, join(self.dist_dir, 'private'),
join(self.dist_dir, 'libs'),
'-iname', '*.so', _env=env).stdout.decode('utf-8')
else:
filens = shprint(sh.find, join(self.dist_dir, '_python_bundle', '_python_bundle', 'modules'),
join(self.dist_dir, 'libs'),
'-iname', '*.so', _env=env).stdout.decode('utf-8')
logger.info('Stripping libraries in private dir')
for filen in filens.split('\n'):
try:
Expand Down
75 changes: 65 additions & 10 deletions pythonforandroid/bootstraps/sdl2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pythonforandroid.util import ensure_dir
from os.path import join, exists, curdir, abspath
from os import walk
import os
import glob
import sh

Expand All @@ -11,9 +12,9 @@


class SDL2GradleBootstrap(Bootstrap):
name = 'sdl2_gradle'
name = 'sdl2'

recipe_depends = ['sdl2', ('python2', 'python3crystax')]
recipe_depends = ['sdl2', ('python2', 'python3', 'python3crystax')]

def run_distribute(self):
info_main("# Creating Android project ({})".format(self.name))
Expand All @@ -23,44 +24,49 @@ def run_distribute(self):
from_crystax = self.ctx.python_recipe.from_crystax
crystax_python_dir = join("crystax_python", "crystax_python")

python_bundle_dir = join('_python_bundle', '_python_bundle')

if len(self.ctx.archs) > 1:
raise ValueError("SDL2/gradle support only one arch")

info("Copying SDL2/gradle build for {}".format(arch))
shprint(sh.rm, "-rf", self.dist_dir)
shprint(sh.cp, "-r", self.build_dir, self.dist_dir)

# either the build use environemnt variable (ANDROID_HOME)
# either the build use environment variable (ANDROID_HOME)
# or the local.properties if exists
with current_directory(self.dist_dir):
with open('local.properties', 'w') as fileh:
fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir))

# TODO: Move the packaged python building to the python recipes
with current_directory(self.dist_dir):
info("Copying Python distribution")

if not exists("private") and not from_crystax:
if 'python2' in self.ctx.recipe_build_order:
ensure_dir("private")
if not exists("crystax_python") and from_crystax:
elif not exists("crystax_python") and from_crystax:
ensure_dir(crystax_python_dir)
elif 'python3' in self.ctx.recipe_build_order:
ensure_dir(python_bundle_dir)

hostpython = sh.Command(self.ctx.hostpython)
if not from_crystax:
if self.ctx.python_recipe.name == 'python2':
try:
shprint(hostpython, '-OO', '-m', 'compileall',
python_install_dir,
_tail=10, _filterout="^Listing")
except sh.ErrorReturnCode:
pass
if not exists('python-install'):
if 'python2' in self.ctx.recipe_build_order and not exists('python-install'):
shprint(
sh.cp, '-a', python_install_dir, './python-install')

self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
self.distribute_javaclasses(self.ctx.javaclass_dir,
dest_dir=join("src", "main", "java"))

if not from_crystax:
if self.ctx.python_recipe.name == 'python2':
info("Filling private directory")
if not exists(join("private", "lib")):
info("private/lib does not exist, making")
Expand Down Expand Up @@ -100,7 +106,56 @@ def run_distribute(self):
shprint(sh.rm, '-f', filename)
shprint(sh.rm, '-rf', 'config/python.o')

else: # Python *is* loaded from crystax
elif self.ctx.python_recipe.name == 'python3':
ndk_dir = self.ctx.ndk_dir
py_recipe = self.ctx.python_recipe

## Build the python bundle:

# Bundle compiled python modules to a folder
modules_dir = join(python_bundle_dir, 'modules')
ensure_dir(modules_dir)

modules_build_dir = join(
self.ctx.python_recipe.get_build_dir(arch.arch),
'android-build',
'build',
'lib.linux-arm-3.7')
module_filens = (glob.glob(join(modules_build_dir, '*.so')) +
glob.glob(join(modules_build_dir, '*.py')))
for filen in module_filens:
shprint(sh.cp, filen, modules_dir)

# zip up the standard library
stdlib_zip = join(self.dist_dir, python_bundle_dir, 'stdlib.zip')
with current_directory(
join(self.ctx.python_recipe.get_build_dir(arch.arch),
'Lib')):
shprint(sh.zip, '-r', stdlib_zip, *os.listdir())

# copy the site-packages into place
shprint(sh.cp, '-r', self.ctx.get_python_install_dir(),
join(python_bundle_dir, 'site-packages'))

# copy the python .so files into place
python_build_dir = join(py_recipe.get_build_dir(arch.arch),
'android-build')
shprint(sh.cp, join(python_build_dir, 'libpython3.7m.so'),
'libs/{}'.format(arch.arch))
shprint(sh.cp, join(python_build_dir, 'libpython3.7m.so.1.0'),
'libs/{}'.format(arch.arch))

info('Renaming .so files to reflect cross-compile')
site_packages_dir = join(python_bundle_dir, 'site-packages')
py_so_files = shprint(sh.find, site_packages_dir, '-iname', '*.so')
filens = py_so_files.stdout.decode('utf-8').split('\n')[:-1]
for filen in filens:
parts = filen.split('.')
if len(parts) <= 2:
continue
shprint(sh.mv, filen, parts[0] + '.so')

elif self.ctx.python_recipe.from_crystax: # Python *is* loaded from crystax
ndk_dir = self.ctx.ndk_dir
py_recipe = self.ctx.python_recipe
python_dir = join(ndk_dir, 'sources', 'python',
Expand Down Expand Up @@ -129,7 +184,7 @@ def run_distribute(self):
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')

self.strip_libraries(arch)
self.fry_eggs(site_packages_dir)
# self.fry_eggs(site_packages_dir) # TODO uncomment this and make it work with python3
super(SDL2GradleBootstrap, self).run_distribute()


Expand Down
26 changes: 14 additions & 12 deletions pythonforandroid/bootstraps/sdl2/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def make_python_zip():

if not exists('private'):
print('No compiled python is present to zip, skipping.')
print('this should only be the case if you are using the CrystaX python')
print('this should only be the case if you are using the CrystaX python or python3')
return

global python_files
Expand All @@ -137,10 +137,10 @@ def select(fn):
fn = realpath(fn)
assert(fn.startswith(d))
fn = fn[len(d):]
if (fn.startswith('/site-packages/')
or fn.startswith('/config/')
or fn.startswith('/lib-dynload/')
or fn.startswith('/libpymodules.so')):
if (fn.startswith('/site-packages/') or
fn.startswith('/config/') or
fn.startswith('/lib-dynload/') or
fn.startswith('/libpymodules.so')):
return False
return fn

Expand Down Expand Up @@ -243,6 +243,8 @@ def make_package(args):
tar_dirs.append('private')
if exists('crystax_python'):
tar_dirs.append('crystax_python')
if exists('_python_bundle'):
tar_dirs.append('_python_bundle')

if args.private:
make_tar('src/main/assets/private.mp3', tar_dirs, args.ignore_path)
Expand Down Expand Up @@ -378,7 +380,7 @@ def make_package(args):
url_scheme=url_scheme,
private_version=str(time.time()))

# gradle build templates
## gradle build templates
render(
'build.tmpl.gradle',
'build.gradle',
Expand All @@ -388,7 +390,7 @@ def make_package(args):
android_api=android_api,
build_tools_version=build_tools_version)

# ant build templates
## ant build templates
render(
'build.tmpl.xml',
'build.xml',
Expand All @@ -409,7 +411,7 @@ def make_package(args):

def parse_args(args=None):
global BLACKLIST_PATTERNS, WHITELIST_PATTERNS, PYTHON
default_android_api = 12
default_android_api = 21
import argparse
ap = argparse.ArgumentParser(description='''\
Package a Python application for Android.
Expand All @@ -418,10 +420,10 @@ def parse_args(args=None):
tools directory of the Android SDK.
''')

# `required=True` for launcher, crashes in make_package
# if not mentioned (and the check is there anyway)
ap.add_argument('--private', dest='private',
help='the dir of user files')
# , required=True) for launcher, crashes in make_package
# if not mentioned (and the check is there anyway)
ap.add_argument('--package', dest='package',
help=('The name of the java package the project will be'
' packaged under.'),
Expand Down Expand Up @@ -486,8 +488,8 @@ def parse_args(args=None):
ap.add_argument('--depend', dest='depends', action='append',
help=('Add a external dependency '
'(eg: com.android.support:appcompat-v7:19.0.1)'))
# The --sdk option has been removed, it is ignored in favour of
# --android-api handled by toolchain.py
## The --sdk option has been removed, it is ignored in favour of
## --android-api handled by toolchain.py
ap.add_argument('--sdk', dest='sdk_version', default=-1,
type=int, help=('Deprecated argument, does nothing'))
ap.add_argument('--minsdk', dest='min_sdk_version',
Expand Down
1 change: 1 addition & 0 deletions pythonforandroid/bootstraps/sdl2/build/jni/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

# APP_ABI := armeabi armeabi-v7a x86
APP_ABI := $(ARCH)
APP_PLATFORM := $(NDK_API)
4 changes: 2 additions & 2 deletions pythonforandroid/bootstraps/sdl2/build/jni/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
start.c

LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/include/python2.7 $(EXTRA_CFLAGS)
LOCAL_CFLAGS += -I$(PYTHON_INCLUDE_ROOT) $(EXTRA_CFLAGS)

LOCAL_SHARED_LIBRARIES := SDL2 python_shared

LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog $(EXTRA_LDLIBS)

LOCAL_LDFLAGS += -L$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/lib $(APPLICATION_ADDITIONAL_LDFLAGS)
LOCAL_LDFLAGS += -L$(PYTHON_LINK_ROOT) $(APPLICATION_ADDITIONAL_LDFLAGS)

include $(BUILD_SHARED_LIBRARY)

Expand Down
Loading

0 comments on commit 9efead4

Please sign in to comment.