From 9b6436b38337f8e88d6adddb617a21ada5bec58c Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Wed, 17 Oct 2018 22:53:59 +0100 Subject: [PATCH] Did initial implementation of an ndk-api build target option --- pythonforandroid/archs.py | 1 + .../bootstraps/sdl2/build/jni/Application.mk | 2 +- .../bootstraps/sdl2/build/jni/src/Android.mk | 6 +++--- pythonforandroid/build.py | 10 ++++++++-- pythonforandroid/recipe.py | 11 +++++++++-- pythonforandroid/recipes/python3/__init__.py | 8 ++++++++ pythonforandroid/recipes/sdl2/__init__.py | 9 +++++++++ pythonforandroid/toolchain.py | 14 +++++++++++--- 8 files changed, 50 insertions(+), 11 deletions(-) diff --git a/pythonforandroid/archs.py b/pythonforandroid/archs.py index b6baeb393d..3c8cdc3071 100644 --- a/pythonforandroid/archs.py +++ b/pythonforandroid/archs.py @@ -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 diff --git a/pythonforandroid/bootstraps/sdl2/build/jni/Application.mk b/pythonforandroid/bootstraps/sdl2/build/jni/Application.mk index c99aaa4061..15598537ca 100644 --- a/pythonforandroid/bootstraps/sdl2/build/jni/Application.mk +++ b/pythonforandroid/bootstraps/sdl2/build/jni/Application.mk @@ -5,4 +5,4 @@ # APP_ABI := armeabi armeabi-v7a x86 APP_ABI := $(ARCH) -APP_PLATFORM := android-21 +APP_PLATFORM := $(NDK_API) diff --git a/pythonforandroid/bootstraps/sdl2/build/jni/src/Android.mk b/pythonforandroid/bootstraps/sdl2/build/jni/src/Android.mk index d8408f6019..be2bb9b27a 100644 --- a/pythonforandroid/bootstraps/sdl2/build/jni/src/Android.mk +++ b/pythonforandroid/bootstraps/sdl2/build/jni/src/Android.mk @@ -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) -I$(LOCAL_PATH)/../../../../other_builds/python3/$(ARCH)/python3/Include -I$(LOCAL_PATH)/../../../../other_builds/python3/$(ARCH)/python3/android-build +LOCAL_CFLAGS += -I$(PYTHON_INCLUDE_ROOT) $(EXTRA_CFLAGS) LOCAL_SHARED_LIBRARIES := SDL2 python_shared -LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog $(EXTRA_LDLIBS) -lpython3.7m +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) -L$(LOCAL_PATH)/../../../../other_builds/python3/$(ARCH)/python3/android-build +LOCAL_LDFLAGS += -L$(PYTHON_LINK_ROOT) $(APPLICATION_ADDITIONAL_LDFLAGS) include $(BUILD_SHARED_LIBRARY) diff --git a/pythonforandroid/build.py b/pythonforandroid/build.py index 3fc86217fa..451d7ad684 100644 --- a/pythonforandroid/build.py +++ b/pythonforandroid/build.py @@ -158,8 +158,12 @@ def ndk_dir(self): def ndk_dir(self, value): self._ndk_dir = value - def prepare_build_environment(self, user_sdk_dir, user_ndk_dir, - user_android_api, user_ndk_ver): + def prepare_build_environment(self, + user_sdk_dir, + user_ndk_dir, + user_android_api, + user_ndk_ver, + user_ndk_api): '''Checks that build dependencies exist and sets internal variables for the Android SDK etc. @@ -328,6 +332,8 @@ def prepare_build_environment(self, user_sdk_dir, user_ndk_dir, 'set it with `--ndk-version=...`.') self.ndk_ver = ndk_ver + self.ndk_api = user_ndk_api + info('Using {} NDK {}'.format(self.ndk.capitalize(), self.ndk_ver)) virtualenv = None diff --git a/pythonforandroid/recipe.py b/pythonforandroid/recipe.py index 80194fad38..d047e2268c 100644 --- a/pythonforandroid/recipe.py +++ b/pythonforandroid/recipe.py @@ -277,7 +277,8 @@ def get_build_container_dir(self, arch): alternative or optional dependencies are being built. ''' dir_name = self.get_dir_name() - return join(self.ctx.build_dir, 'other_builds', dir_name, arch) + return join(self.ctx.build_dir, 'other_builds', + dir_name, '{}__ndk_target_{}'.format(arch, self.ctx.ndk_api)) def get_dir_name(self): choices = self.check_recipe_choices() @@ -1081,7 +1082,6 @@ def get_recipe_env(self, arch, with_flags_in_cc=True): return env - class TargetPythonRecipe(Recipe): '''Class for target python recipes. Sets ctx.python_recipe to point to itself, so as to know later what kind of Python was built or used.''' @@ -1102,6 +1102,13 @@ def prebuild_arch(self, arch): exit(1) self.ctx.python_recipe = self + def include_root(self, arch): + '''The root directory from which to include headers.''' + raise NotImplementedError('Not implemented in TargetPythonRecipe') + + def link_root(self): + raise NotImplementedError('Not implemented in TargetPythonRecipe') + # @property # def ctx(self): # return self._ctx diff --git a/pythonforandroid/recipes/python3/__init__.py b/pythonforandroid/recipes/python3/__init__.py index 8898cd95f8..ad21a52d47 100644 --- a/pythonforandroid/recipes/python3/__init__.py +++ b/pythonforandroid/recipes/python3/__init__.py @@ -103,5 +103,13 @@ def build_arch(self, arch): # TODO: Look into passing the path to pyconfig.h in a # better way, although this is probably acceptable sh.cp('pyconfig.h', join(recipe_build_dir, 'Include')) + + def include_root(self, arch_name): + return join(self.get_build_dir(arch_name), + 'Include') + + def link_root(self, arch_name): + return join(self.get_build_dir(arch_name), + 'android-build') recipe = Python3Recipe() diff --git a/pythonforandroid/recipes/sdl2/__init__.py b/pythonforandroid/recipes/sdl2/__init__.py index 59b91dac6d..97e21e0b82 100644 --- a/pythonforandroid/recipes/sdl2/__init__.py +++ b/pythonforandroid/recipes/sdl2/__init__.py @@ -17,11 +17,20 @@ class LibSDL2Recipe(BootstrapNDKRecipe): def get_recipe_env(self, arch=None): env = super(LibSDL2Recipe, self).get_recipe_env(arch) + py2 = self.get_recipe('python2', arch.ctx) env['PYTHON2_NAME'] = py2.get_dir_name() + py3 = self.get_recipe('python3', arch.ctx) + + env['PYTHON_INCLUDE_ROOT'] = self.ctx.python_recipe.include_root(arch.arch) + env['PYTHON_LINK_ROOT'] = self.ctx.python_recipe.link_root(arch.arch) + if 'python2' in self.ctx.recipe_build_order: env['EXTRA_LDLIBS'] = ' -lpython2.7' + if 'python3' in self.ctx.recipe_build_order: + env['EXTRA_LDLIBS'] = ' -lpython3.7m' # TODO: don't hardcode the python version + env['APP_ALLOW_MISSING_DEPS'] = 'true' return env diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index ff8ab85a99..be07d6cee9 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -139,7 +139,8 @@ def wrapper_func(self, args): ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, - user_ndk_ver=self.ndk_version) + user_ndk_ver=self.ndk_version, + user_ndk_api=self.ndk_api) dist = self._dist if dist.needs_build: info_notify('No dist exists that meets your requirements, ' @@ -260,6 +261,10 @@ def __init__(self): '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) + generic_parser.add_argument( + '--ndk-api', type=int, default=21, + help=('The Android API level to compile against. This should be your ' + '*minimal supported* API, not normally the same as your --android-api.')) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', @@ -500,6 +505,7 @@ def add_parser(subparsers, *args, **kwargs): self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version + self.ndk_api = args.ndk_api self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool @@ -903,7 +909,8 @@ def sdk_tools(self, args): ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, - user_ndk_ver=self.ndk_version) + user_ndk_ver=self.ndk_version, + user_ndk_api=self.ndk_api) android = sh.Command(join(ctx.sdk_dir, 'tools', args.tool)) output = android( *args.unknown_args, _iter=True, _out_bufsize=1, _err_to_out=True) @@ -930,7 +937,8 @@ def _adb(self, commands): ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, - user_ndk_ver=self.ndk_version) + user_ndk_ver=self.ndk_version, + user_ndk_api=self.ndk_api) if platform in ('win32', 'cygwin'): adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb.exe')) else: