From bf495752ae758e72defd41d61df456f73bd256a1 Mon Sep 17 00:00:00 2001 From: Ansh Dadwal Date: Sat, 12 Apr 2025 18:25:56 +0530 Subject: [PATCH] `kivy` and `pyjnius`: switch to `PyProjectRecipe` --- pythonforandroid/recipes/kivy/__init__.py | 43 ++++++------------- .../recipes/kivy/use_cython.patch | 11 +++++ pythonforandroid/recipes/pyjnius/__init__.py | 19 ++++++-- .../recipes/pyjnius/use_cython.patch | 13 ++++++ 4 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 pythonforandroid/recipes/kivy/use_cython.patch create mode 100644 pythonforandroid/recipes/pyjnius/use_cython.patch diff --git a/pythonforandroid/recipes/kivy/__init__.py b/pythonforandroid/recipes/kivy/__init__.py index a5030abf95..f80024155f 100644 --- a/pythonforandroid/recipes/kivy/__init__.py +++ b/pythonforandroid/recipes/kivy/__init__.py @@ -1,10 +1,9 @@ -import glob -from os.path import basename, exists, join +from os.path import join import sys import packaging.version import sh -from pythonforandroid.recipe import CythonRecipe +from pythonforandroid.recipe import PyProjectRecipe from pythonforandroid.toolchain import current_directory, shprint @@ -21,7 +20,7 @@ def is_kivy_affected_by_deadlock_issue(recipe=None, arch=None): ) < packaging.version.Version("2.2.0.dev0") -class KivyRecipe(CythonRecipe): +class KivyRecipe(PyProjectRecipe): version = '2.3.1' url = 'https://github.com/kivy/kivy/archive/{version}.zip' name = 'kivy' @@ -33,34 +32,20 @@ class KivyRecipe(CythonRecipe): # sdl-gl-swapwindow-nogil.patch is needed to avoid a deadlock. # See: https://github.com/kivy/kivy/pull/8025 # WARNING: Remove this patch when a new Kivy version is released. - patches = [("sdl-gl-swapwindow-nogil.patch", is_kivy_affected_by_deadlock_issue)] + patches = [("sdl-gl-swapwindow-nogil.patch", is_kivy_affected_by_deadlock_issue), "use_cython.patch"] - def cythonize_build(self, env, build_dir='.'): - super().cythonize_build(env, build_dir=build_dir) + def get_recipe_env(self, arch, **kwargs): + env = super().get_recipe_env(arch, **kwargs) - if not exists(join(build_dir, 'kivy', 'include')): - return + # Taken from CythonRecipe + env['LDFLAGS'] = env['LDFLAGS'] + ' -L{} '.format( + self.ctx.get_libs_dir(arch.arch) + + ' -L{} '.format(self.ctx.libs_dir) + + ' -L{}'.format(join(self.ctx.bootstrap.build_dir, 'obj', 'local', + arch.arch))) + env['LDSHARED'] = env['CC'] + ' -shared' + env['LIBLINK'] = 'NOTNONE' - # If kivy is new enough to use the include dir, copy it - # manually to the right location as we bypass this stage of - # the build - with current_directory(build_dir): - build_libs_dirs = glob.glob(join('build', 'lib.*')) - - for dirn in build_libs_dirs: - shprint(sh.cp, '-r', join('kivy', 'include'), - join(dirn, 'kivy')) - - def cythonize_file(self, env, build_dir, filename): - # We can ignore a few files that aren't important to the - # android build, and may not work on Android anyway - do_not_cythonize = ['window_x11.pyx', 'camera_avfoundation.pyx', 'img_imageio.pyx', 'egl_angle_metal.pyx'] - if basename(filename) in do_not_cythonize: - return - super().cythonize_file(env, build_dir, filename) - - def get_recipe_env(self, arch): - env = super().get_recipe_env(arch) # NDKPLATFORM is our switch for detecting Android platform, so can't be None env['NDKPLATFORM'] = "NOTNONE" if 'sdl2' in self.ctx.recipe_build_order: diff --git a/pythonforandroid/recipes/kivy/use_cython.patch b/pythonforandroid/recipes/kivy/use_cython.patch new file mode 100644 index 0000000000..2a0d2074ba --- /dev/null +++ b/pythonforandroid/recipes/kivy/use_cython.patch @@ -0,0 +1,11 @@ +--- kivy-master/setup.py 2025-02-25 03:08:18.000000000 +0530 ++++ kivy-master.mod/setup.py 2025-03-01 13:10:24.227808612 +0530 +@@ -249,7 +249,7 @@ + # This determines whether Cython specific functionality may be used. + can_use_cython = True + +-if platform in ('ios', 'android'): ++if platform in ('ios'): + # NEVER use or declare cython on these platforms + print('Not using cython on %s' % platform) + can_use_cython = False diff --git a/pythonforandroid/recipes/pyjnius/__init__.py b/pythonforandroid/recipes/pyjnius/__init__.py index 00369df216..cd80b46342 100644 --- a/pythonforandroid/recipes/pyjnius/__init__.py +++ b/pythonforandroid/recipes/pyjnius/__init__.py @@ -1,11 +1,11 @@ -from pythonforandroid.recipe import CythonRecipe +from pythonforandroid.recipe import PyProjectRecipe from pythonforandroid.toolchain import shprint, current_directory, info from pythonforandroid.patching import will_build import sh from os.path import join -class PyjniusRecipe(CythonRecipe): +class PyjniusRecipe(PyProjectRecipe): version = '1.6.1' url = 'https://github.com/kivy/pyjnius/archive/{version}.zip' name = 'pyjnius' @@ -13,12 +13,23 @@ class PyjniusRecipe(CythonRecipe): site_packages_name = 'jnius' patches = [ + "use_cython.patch", ('genericndkbuild_jnienv_getter.patch', will_build('genericndkbuild')), ('sdl3_jnienv_getter.patch', will_build('sdl3')), ] - def get_recipe_env(self, arch): - env = super().get_recipe_env(arch) + def get_recipe_env(self, arch, **kwargs): + env = super().get_recipe_env(arch, **kwargs) + + # Taken from CythonRecipe + env['LDFLAGS'] = env['LDFLAGS'] + ' -L{} '.format( + self.ctx.get_libs_dir(arch.arch) + + ' -L{} '.format(self.ctx.libs_dir) + + ' -L{}'.format(join(self.ctx.bootstrap.build_dir, 'obj', 'local', + arch.arch))) + env['LDSHARED'] = env['CC'] + ' -shared' + env['LIBLINK'] = 'NOTNONE' + # NDKPLATFORM is our switch for detecting Android platform, so can't be None env['NDKPLATFORM'] = "NOTNONE" return env diff --git a/pythonforandroid/recipes/pyjnius/use_cython.patch b/pythonforandroid/recipes/pyjnius/use_cython.patch new file mode 100644 index 0000000000..59265e99a7 --- /dev/null +++ b/pythonforandroid/recipes/pyjnius/use_cython.patch @@ -0,0 +1,13 @@ +--- pyjnius-1.6.1/setup.py 2023-11-05 21:07:43.000000000 +0530 ++++ pyjnius-1.6.1.mod/setup.py 2025-03-01 14:47:11.964847337 +0530 +@@ -59,10 +59,6 @@ + if NDKPLATFORM is not None and getenv('LIBLINK'): + PLATFORM = 'android' + +-# detect platform +-if PLATFORM == 'android': +- PYX_FILES = [fn[:-3] + 'c' for fn in PYX_FILES] +- + JAVA=get_java_setup(PLATFORM) + + assert JAVA.is_jdk(), "You need a JDK, we only found a JRE. Try setting JAVA_HOME"