Skip to content

Commit acb7482

Browse files
committed
android: enable sound, move to PyProjectRecipe
1 parent df00b43 commit acb7482

File tree

12 files changed

+71
-80
lines changed

12 files changed

+71
-80
lines changed

Diff for: pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected static ArrayList<String> getLibraries(File libsDir) {
5656
libsList.add("python3.9");
5757
libsList.add("python3.10");
5858
libsList.add("python3.11");
59+
libsList.add("python3.12");
60+
libsList.add("python3.13");
5961
libsList.add("main");
6062
return libsList;
6163
}
@@ -75,7 +77,7 @@ public static void loadLibraries(File filesDir, File libsDir) {
7577
// load, and it has failed, give a more
7678
// general error
7779
Log.v(TAG, "Library loading error: " + e.getMessage());
78-
if (lib.startsWith("python3.11") && !foundPython) {
80+
if (lib.startsWith("python3.13") && !foundPython) {
7981
throw new RuntimeException("Could not load any libpythonXXX.so");
8082
} else if (lib.startsWith("python")) {
8183
continue;

Diff for: pythonforandroid/recipe.py

+24-30
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,14 @@ def folder_name(self):
946946

947947
def get_recipe_env(self, arch=None, with_flags_in_cc=True):
948948
env = super().get_recipe_env(arch, with_flags_in_cc)
949-
env['PYTHONNOUSERSITE'] = '1'
950949
# Set the LANG, this isn't usually important but is a better default
951950
# as it occasionally matters how Python e.g. reads files
952951
env['LANG'] = "en_GB.UTF-8"
953952
# Binaries made by packages installed by pip
954953
env["PATH"] = self._host_recipe.site_bin + ":" + env["PATH"]
954+
955+
host_env = self.get_hostrecipe_env()
956+
env['PYTHONPATH'] = host_env["PYTHONPATH"]
955957

956958
if not self.call_hostpython_via_targetpython:
957959
env['CFLAGS'] += ' -I{}'.format(
@@ -961,24 +963,11 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
961963
self.ctx.python_recipe.link_root(arch.arch),
962964
self.ctx.python_recipe.link_version,
963965
)
964-
965-
hppath = []
966-
hppath.append(join(dirname(self.hostpython_location), 'Lib'))
967-
hppath.append(join(hppath[0], 'site-packages'))
968-
builddir = join(dirname(self.hostpython_location), 'build')
969-
if exists(builddir):
970-
hppath += [join(builddir, d) for d in listdir(builddir)
971-
if isdir(join(builddir, d))]
972-
if len(hppath) > 0:
973-
if 'PYTHONPATH' in env:
974-
env['PYTHONPATH'] = ':'.join(hppath + [env['PYTHONPATH']])
975-
else:
976-
env['PYTHONPATH'] = ':'.join(hppath)
977966
return env
978967

979968
def should_build(self, arch):
980969
name = self.folder_name
981-
if self.ctx.has_package(name, arch):
970+
if self.ctx.has_package(name, arch) or name in listdir(self._host_recipe.site_dir):
982971
info('Python package already exists in site-packages')
983972
return False
984973
info('{} apparently isn\'t already in site-packages'.format(name))
@@ -1005,22 +994,28 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
1005994
hostpython = sh.Command(self.hostpython_location)
1006995
hpenv = env.copy()
1007996
with current_directory(self.get_build_dir(arch.arch)):
1008-
shprint(hostpython, 'setup.py', 'install', '-O2',
1009-
'--root={}'.format(self.ctx.get_python_install_dir(arch.arch)),
1010-
'--install-lib=.',
1011-
_env=hpenv, *self.setup_extra_args)
997+
if isfile("setup.py"):
998+
shprint(hostpython, 'setup.py', 'install', '-O2',
1012999

1013-
# If asked, also install in the hostpython build dir
1014-
if self.install_in_hostpython:
1015-
self.install_hostpython_package(arch)
1000+
'--root={}'.format(self.ctx.get_python_install_dir(arch.arch)),
1001+
'--install-lib=.',
1002+
_env=hpenv, *self.setup_extra_args)
10161003

1017-
def get_hostrecipe_env(self, arch):
1004+
# If asked, also install in the hostpython build dir
1005+
if self.install_in_hostpython:
1006+
self.install_hostpython_package(arch)
1007+
else:
1008+
warning("`PythonRecipe.install_python_package` called without `setup.py` file!")
1009+
1010+
def get_hostrecipe_env(self):
10181011
env = environ.copy()
1019-
env['PYTHONPATH'] = self._host_recipe.site_dir
1012+
_python_path = self._host_recipe.get_path_to_python()
1013+
env['PYTHONPATH'] = self._host_recipe.site_dir + ":" + join(
1014+
_python_path, "Modules") + ":" + glob.glob(join(_python_path, "build", "lib*"))[0]
10201015
return env
10211016

10221017
def install_hostpython_package(self, arch):
1023-
env = self.get_hostrecipe_env(arch)
1018+
env = self.get_hostrecipe_env()
10241019
real_hostpython = sh.Command(self.real_hostpython_location)
10251020
shprint(real_hostpython, 'setup.py', 'install', '-O2',
10261021
'--root={}'.format(self._host_recipe.site_root),
@@ -1031,7 +1026,7 @@ def python_major_minor_version(self):
10311026
parsed_version = packaging.version.parse(self.ctx.python_recipe.version)
10321027
return f"{parsed_version.major}.{parsed_version.minor}"
10331028

1034-
def install_hostpython_prerequisites(self, packages=None, force_upgrade=True):
1029+
def install_hostpython_prerequisites(self, packages=None, force_upgrade=True, arch=None):
10351030
if not packages:
10361031
packages = self.hostpython_prerequisites
10371032

@@ -1049,7 +1044,7 @@ def install_hostpython_prerequisites(self, packages=None, force_upgrade=True):
10491044
if force_upgrade:
10501045
pip_options.append("--upgrade")
10511046
# Use system's pip
1052-
shprint(sh.Command(), *pip_options)
1047+
shprint(sh.Command(self.real_hostpython_location), "-m", "pip", *pip_options, _env=self.get_hostrecipe_env())
10531048

10541049
def restore_hostpython_prerequisites(self, packages):
10551050
_packages = []
@@ -1088,13 +1083,12 @@ def build_compiled_components(self, arch):
10881083
env['STRIP'], '{}', ';', _env=env)
10891084

10901085
def install_hostpython_package(self, arch):
1091-
env = self.get_hostrecipe_env(arch)
1086+
env = self.get_hostrecipe_env()
10921087
self.rebuild_compiled_components(arch, env)
10931088
super().install_hostpython_package(arch)
10941089

10951090
def rebuild_compiled_components(self, arch, env):
10961091
info('Rebuilding compiled components in {}'.format(self.name))
1097-
10981092
hostpython = sh.Command(self.real_hostpython_location)
10991093
shprint(hostpython, 'setup.py', 'clean', '--all', _env=env)
11001094
shprint(hostpython, 'setup.py', self.build_cmd, '-v', _env=env,
@@ -1128,7 +1122,7 @@ def build_cython_components(self, arch):
11281122

11291123
with current_directory(self.get_build_dir(arch.arch)):
11301124
hostpython = sh.Command(self.ctx.hostpython)
1131-
shprint(hostpython, '-c', 'import sys; print(sys.path)', _env=env)
1125+
shprint(hostpython, '-c', 'import sys; print(sys.path, sys.exec_prefix, sys.prefix)', _env=env)
11321126
debug('cwd is {}'.format(realpath(curdir)))
11331127
info('Trying first build of {} to get cython files: this is '
11341128
'expected to fail'.format(self.name))

Diff for: pythonforandroid/recipes/android/__init__.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
from pythonforandroid.recipe import CythonRecipe, IncludedFilesBehaviour
1+
from pythonforandroid.recipe import PyProjectRecipe, IncludedFilesBehaviour, Recipe
22
from pythonforandroid.util import current_directory
33
from pythonforandroid import logger
44

55
from os.path import join
66

77

8-
class AndroidRecipe(IncludedFilesBehaviour, CythonRecipe):
8+
class AndroidRecipe(IncludedFilesBehaviour, PyProjectRecipe):
99
# name = 'android'
1010
version = None
1111
url = None
1212

1313
src_filename = 'src'
1414

1515
depends = [('sdl2', 'genericndkbuild'), 'pyjnius']
16+
hostpython_prerequisites = ["cython"]
1617

1718
config_env = {}
1819

19-
def get_recipe_env(self, arch):
20-
env = super().get_recipe_env(arch)
20+
def get_recipe_env(self, arch, **kwargs):
21+
env = super().get_recipe_env(arch, **kwargs)
2122
env.update(self.config_env)
2223
return env
2324

@@ -49,6 +50,9 @@ def prebuild_arch(self, arch):
4950
'BOOTSTRAP': bootstrap,
5051
'IS_SDL2': int(is_sdl2),
5152
'PY2': 0,
53+
'ANDROID_LIBS_DIR':join(
54+
Recipe.get_recipe("sdl2", self.ctx).get_build_dir(arch.arch), "../..", "libs", arch.arch
55+
),
5256
'JAVA_NAMESPACE': java_ns,
5357
'JNI_NAMESPACE': jni_ns,
5458
'ACTIVITY_CLASS_NAME': self.ctx.activity_class_name,

Diff for: pythonforandroid/recipes/android/src/setup.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
from distutils.core import setup, Extension
2+
from Cython.Build import cythonize
23
import os
34

4-
library_dirs = ['libs/' + os.environ['ARCH']]
5+
# Define the library directories
6+
library_dirs = [os.environ['ANDROID_LIBS_DIR']]
57
lib_dict = {
68
'sdl2': ['SDL2', 'SDL2_image', 'SDL2_mixer', 'SDL2_ttf']
79
}
810
sdl_libs = lib_dict.get(os.environ['BOOTSTRAP'], ['main'])
911

10-
modules = [Extension('android._android',
11-
['android/_android.c', 'android/_android_jni.c'],
12-
libraries=sdl_libs + ['log'],
13-
library_dirs=library_dirs),
14-
Extension('android._android_billing',
15-
['android/_android_billing.c', 'android/_android_billing_jni.c'],
16-
libraries=['log'],
17-
library_dirs=library_dirs)]
12+
# Define the extensions with Cython
13+
modules = [
14+
Extension('android._android',
15+
['android/_android.pyx', 'android/_android_jni.c'],
16+
libraries=sdl_libs + ['log'],
17+
library_dirs=library_dirs),
18+
Extension('android._android_billing',
19+
['android/_android_billing.pyx', 'android/_android_billing_jni.c'],
20+
libraries=['log'],
21+
library_dirs=library_dirs),
22+
Extension('android._android_sound',
23+
['android/_android_sound.pyx', 'android/_android_sound_jni.c'],
24+
libraries=['log'],
25+
library_dirs=library_dirs)
26+
]
1827

28+
29+
# Use cythonize to build the modules
30+
cythonized_modules = cythonize(modules, compiler_directives={'language_level': "3"})
31+
32+
# Setup the package
1933
setup(name='android',
2034
version='1.0',
2135
packages=['android'],
2236
package_dir={'android': 'android'},
23-
ext_modules=modules
37+
ext_modules=cythonized_modules
2438
)

Diff for: pythonforandroid/recipes/hostpython3/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class HostPython3Recipe(Recipe):
4747
'''The default url to download our host python recipe. This url will
4848
change depending on the python version set in attribute :attr:`version`.'''
4949

50-
patches = ['patches/pyconfig_detection.patch']
51-
5250
@property
5351
def _exe_name(self):
5452
'''

Diff for: pythonforandroid/recipes/hostpython3/patches/pyconfig_detection.patch

-13
This file was deleted.

Diff for: pythonforandroid/recipes/libb2/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from pythonforandroid.recipe import Recipe
22
from pythonforandroid.toolchain import current_directory, shprint
3+
from os.path import join
34
import sh
45

56

67
class Libb2Recipe(Recipe):
78
version = '0.98.1'
89
url = 'https://github.com/BLAKE2/libb2/releases/download/v{version}/libb2-{version}.tar.gz'
9-
built_libraries = {'libb2.so': './src/.libs/'}
10+
built_libraries = {'libb2.so': './src/.libs/', "libomp.so": "./src/.libs"}
1011

1112
def build_arch(self, arch):
1213
with current_directory(self.get_build_dir(arch.arch)):
@@ -17,6 +18,8 @@ def build_arch(self, arch):
1718
configure = sh.Command('./configure')
1819
shprint(configure, *flags, _env=env)
1920
shprint(sh.make, _env=env)
21+
arch_ = {"armeabi-v7a":"arm", "arm64-v8a": "aarch64", "x86_64":"x86_64", "x86":"i386"}[arch.arch]
22+
shprint(sh.cp, join(self.ctx.ndk.llvm_prebuilt_dir, f"lib64/clang/14.0.6/lib/linux/{arch_}/libomp.so") , "./src/.libs")
2023

2124

2225
recipe = Libb2Recipe()

Diff for: pythonforandroid/recipes/pyjnius/__init__.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
from pythonforandroid.recipe import CythonRecipe
1+
from pythonforandroid.recipe import PyProjectRecipe
22
from pythonforandroid.toolchain import shprint, current_directory, info
33
from pythonforandroid.patching import will_build
44
import sh
55
from os.path import join
66

77

8-
class PyjniusRecipe(CythonRecipe):
8+
class PyjniusRecipe(PyProjectRecipe):
99
version = '1.6.1'
1010
url = 'https://github.com/kivy/pyjnius/archive/{version}.zip'
1111
name = 'pyjnius'
12-
depends = [('genericndkbuild', 'sdl2'), 'six']
12+
depends = ['six']
1313
site_packages_name = 'jnius'
14-
1514
patches = [('genericndkbuild_jnienv_getter.patch', will_build('genericndkbuild'))]
1615

17-
def get_recipe_env(self, arch):
18-
env = super().get_recipe_env(arch)
16+
def get_recipe_env(self, arch, **kwargs):
17+
env = super().get_recipe_env(arch, **kwargs)
1918
# NDKPLATFORM is our switch for detecting Android platform, so can't be None
2019
env['NDKPLATFORM'] = "NOTNONE"
2120
return env

Diff for: pythonforandroid/recipes/python3/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ def link_root(self, arch_name):
194194
return join(self.get_build_dir(arch_name), 'android-build')
195195

196196
def should_build(self, arch):
197-
return True
198197
return not Path(self.link_root(arch.arch), self._libpython).is_file()
199198

200199
def prebuild_arch(self, arch):

Diff for: pythonforandroid/recipes/setuptools/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
class SetuptoolsRecipe(PythonRecipe):
55
version = '69.2.0'
6-
url = 'https://pypi.python.org/packages/source/s/setuptools/setuptools-{version}.tar.gz'
6+
url = ''
77
call_hostpython_via_targetpython = False
88
install_in_hostpython = True
9+
hostpython_prerequisites = [f"setuptools=={version}"]
910

1011

1112
recipe = SetuptoolsRecipe()

Diff for: pythonforandroid/recipes/six/__init__.py

-10
This file was deleted.

Diff for: pythonforandroid/toolchain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ def _build_package(self, args, package_type):
10341034
# .../build/bootstrap_builds/sdl2-python3/gradlew
10351035
# if docker on windows, gradle contains CRLF
10361036
output = shprint(
1037-
sh.Command('dos2unix'), gradlew._path.decode('utf8'),
1037+
sh.Command('dos2unix'), gradlew._path,
10381038
_tail=20, _critical=True, _env=env
10391039
)
10401040
if args.build_mode == "debug":

0 commit comments

Comments
 (0)