Skip to content

Commit 56c19f0

Browse files
committed
Tested with Buildozer
1 parent 38682dc commit 56c19f0

File tree

5 files changed

+56
-47
lines changed

5 files changed

+56
-47
lines changed

pythonforandroid/build.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,6 @@ def run_pymodules_install(ctx, arch, modules, project_dir=None,
706706
"source venv/bin/activate && pip install -U pip"
707707
), _env=copy.copy(base_env))
708708

709-
# Install Cython in case modules need it to build:
710-
info('Install Cython in case one of the modules needs it to build')
711-
shprint(sh.bash, '-c', (
712-
"venv/bin/pip install Cython"
713-
), _env=copy.copy(base_env))
714-
715709
# Get environment variables for build (with CC/compiler set):
716710
standard_recipe = CythonRecipe()
717711
standard_recipe.ctx = ctx

pythonforandroid/logger.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def shprint(command, *args, **kwargs):
150150
command_string = command_path[-1]
151151
string = ' '.join(['{}->{} running'.format(Out_Fore.LIGHTBLACK_EX,
152152
Out_Style.RESET_ALL),
153-
command_string] + list(args))
153+
str(command)] + list(args))
154154

155155
# If logging is not in DEBUG mode, trim the command if necessary
156156
if logger.level > logging.DEBUG:
@@ -159,6 +159,13 @@ def shprint(command, *args, **kwargs):
159159
else:
160160
logger.debug('{}{}'.format(string, Err_Style.RESET_ALL))
161161

162+
if "_env" in kwargs:
163+
env = kwargs["_env"]
164+
env_str = "\n".join("{}='{}'".format(n, v) for n, v in env.items())
165+
logger.debug(
166+
'{}ENV:\n{}{}'.format(Err_Fore.YELLOW, env_str, Err_Fore.RESET)
167+
)
168+
162169
need_closing_newline = False
163170
try:
164171
msg_hdr = ' working: '

pythonforandroid/recipe.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from os.path import basename, dirname, exists, isdir, isfile, join, realpath, split
1+
from os.path import basename, dirname, exists, isdir, isfile, join, realpath, relpath, split
22
import glob
33
import hashlib
44
import json
@@ -973,6 +973,8 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
973973
# as it occasionally matters how Python e.g. reads files
974974
env['LANG'] = "en_GB.UTF-8"
975975

976+
env["PYTHONDONTWRITEBYTECODE"] = "1"
977+
976978
# Binaries made by packages installed by pip
977979
self.patch_shebangs(self._host_recipe.local_bin, self.real_hostpython_location)
978980
env["PATH"] = self._host_recipe.local_bin + ":" + self._host_recipe.site_bin + ":" + env["PATH"]
@@ -993,11 +995,17 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
993995

994996
def should_build(self, arch):
995997
name = self.folder_name
996-
if self.ctx.has_package(name, arch):
997-
info('Python package already exists in site-packages')
998-
return False
999-
info('{} apparently isn\'t already in site-packages'.format(name))
1000-
return True
998+
host_package_dir = join(self._host_recipe.site_dir, name)
999+
if self.install_in_hostpython and not exists(host_package_dir):
1000+
info('{} apparently isn\'t already in hostpython site-packages ({})'.format(name, host_package_dir))
1001+
return True
1002+
1003+
if self.install_in_targetpython and not self.ctx.has_package(name, arch):
1004+
info('{} apparently isn\'t already in site-packages'.format(name))
1005+
return True
1006+
1007+
info('Python package already exists in target site-packages')
1008+
return False
10011009

10021010
def build_arch(self, arch):
10031011
'''Install the Python module by calling setup.py install with
@@ -1009,7 +1017,6 @@ def build_arch(self, arch):
10091017
def install_python_package(self, arch, name=None, env=None, is_dir=True):
10101018
'''Automate the installation of a Python package (or a cython
10111019
package where the cython components are pre-built).'''
1012-
# arch = self.filtered_archs[0] # old kivy-ios way
10131020
if name is None:
10141021
name = self.name
10151022
if env is None:
@@ -1022,7 +1029,8 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
10221029
with current_directory(self.get_build_dir(arch.arch)):
10231030

10241031
if isfile("setup.py"):
1025-
shprint(hostpython, 'setup.py', 'install', '-O2',
1032+
if self.install_in_targetpython:
1033+
shprint(hostpython, 'setup.py', 'install', '-O2',
10261034
'--root={}'.format(self.ctx.get_python_install_dir(arch.arch)),
10271035
'--install-lib=.',
10281036
_env=hpenv, *self.setup_extra_args)
@@ -1035,6 +1043,7 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
10351043

10361044
def get_hostrecipe_env(self, arch=None):
10371045
env = environ.copy()
1046+
env["PYTHONDONTWRITEBYTECODE"] = "1"
10381047
_python_path = self._host_recipe.get_path_to_python()
10391048
libdir = glob.glob(join(_python_path, "build", "lib*"))
10401049
env['PYTHONPATH'] = self._host_recipe.site_dir + ":" + join(
@@ -1049,8 +1058,7 @@ def install_hostpython_package(self, arch):
10491058
env = self.get_hostrecipe_env(arch)
10501059
real_hostpython = sh.Command(self.real_hostpython_location)
10511060
shprint(real_hostpython, 'setup.py', 'install', '-O2',
1052-
'--root={}'.format(dirname(self.real_hostpython_location)),
1053-
'--install-lib=Lib/site-packages',
1061+
'--install-lib={}'.format(relpath(self._host_recipe.site_dir, self._host_recipe.site_root)),
10541062
'--root={}'.format(self._host_recipe.site_root),
10551063
_env=env, *self.setup_extra_args)
10561064

@@ -1066,6 +1074,8 @@ def install_hostpython_prerequisites(self, packages=None, force_upgrade=True):
10661074
if len(packages) == 0:
10671075
return
10681076

1077+
debug('Installing hostpython prerequisites: {} ({})'.format(packages, self._host_recipe.site_dir))
1078+
10691079
pip_options = [
10701080
"install",
10711081
*packages,

pythonforandroid/recipes/hostpython3/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class HostPython3Recipe(Recipe):
3737
'''
3838

3939
version = '3.11.13'
40-
_p_version = Version(version)
4140

4241
url = 'https://github.com/python/cpython/archive/refs/tags/v{version}.tar.gz'
4342
'''The default url to download our host python recipe. This url will
@@ -104,14 +103,13 @@ def site_bin(self):
104103

105104
@property
106105
def local_bin(self):
107-
dir = None
108106
dir = "usr/local/bin/"
109107
return join(self.site_root, dir)
110108

111109
@property
112110
def site_dir(self):
113-
dir = None
114-
dir = f"usr/local/lib/python{self._p_version.major}.{self._p_version.minor}/site-packages/"
111+
p_version = Version(self.version)
112+
dir = f"usr/local/lib/python{p_version.major}.{p_version.minor}/site-packages/"
115113
return join(self.site_root, dir)
116114

117115
def build_arch(self, arch):

pythonforandroid/recipes/python3/__init__.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class Python3Recipe(TargetPythonRecipe):
5656
'''
5757

5858
version = '3.11.13'
59-
_p_version = Version(version)
6059
url = 'https://github.com/python/cpython/archive/refs/tags/v{version}.tar.gz'
6160
name = 'python3'
6261

@@ -65,24 +64,6 @@ class Python3Recipe(TargetPythonRecipe):
6564
'patches/reproducible-buildinfo.diff',
6665
]
6766

68-
if _p_version.major == 3 and _p_version.minor == 7:
69-
patches += [
70-
'patches/py3.7.1_fix-ctypes-util-find-library.patch',
71-
'patches/py3.7.1_fix-zlib-version.patch',
72-
]
73-
74-
if 8 <= _p_version.minor <= 10:
75-
patches.append('patches/py3.8.1.patch')
76-
77-
if _p_version.minor >= 11:
78-
patches.append('patches/cpython-311-ctypes-find-library.patch')
79-
80-
if shutil.which('lld') is not None:
81-
if _p_version.minor == 7:
82-
patches.append("patches/py3.7.1_fix_cortex_a8.patch")
83-
elif _p_version.minor >= 8:
84-
patches.append("patches/py3.8.1_fix_cortex_a8.patch")
85-
8667
depends = ['hostpython3', 'sqlite3', 'openssl', 'libffi']
8768
# those optional depends allow us to build python compression modules:
8869
# - _bz2.so
@@ -113,11 +94,6 @@ class Python3Recipe(TargetPythonRecipe):
11394
'ac_cv_header_bzlib_h=no',
11495
]
11596

116-
if _p_version.minor >= 11:
117-
configure_args.extend([
118-
'--with-build-python={python_host_bin}',
119-
])
120-
12197
'''The configure arguments needed to build the python recipe. Those are
12298
used in method :meth:`build_arch` (if not overwritten like python3's
12399
recipe does).
@@ -215,6 +191,30 @@ def should_build(self, arch):
215191
return not isfile(join(self.link_root(arch.arch), self._libpython))
216192

217193
def prebuild_arch(self, arch):
194+
p_version = Version(self.version)
195+
if p_version.major == 3 and p_version.minor == 7:
196+
self.patches += [
197+
'patches/py3.7.1_fix-ctypes-util-find-library.patch',
198+
'patches/py3.7.1_fix-zlib-version.patch',
199+
]
200+
201+
if 8 <= p_version.minor <= 10:
202+
self.patches.append('patches/py3.8.1.patch')
203+
204+
if p_version.minor >= 11:
205+
self.patches.append('patches/cpython-311-ctypes-find-library.patch')
206+
207+
if shutil.which('lld') is not None:
208+
if p_version.minor == 7:
209+
self.patches.append("patches/py3.7.1_fix_cortex_a8.patch")
210+
elif p_version.minor >= 8:
211+
self.patches.append("patches/py3.8.1_fix_cortex_a8.patch")
212+
213+
if p_version.minor >= 11:
214+
self.configure_args.extend([
215+
'--with-build-python={python_host_bin}',
216+
])
217+
218218
super().prebuild_arch(arch)
219219
self.ctx.python_recipe = self
220220

@@ -312,7 +312,7 @@ def add_flags(include_flags, link_dirs, link_libs):
312312
env['ZLIB_VERSION'] = line.replace('#define ZLIB_VERSION ', '')
313313
add_flags(' -I' + zlib_includes, ' -L' + zlib_lib_path, ' -lz')
314314

315-
if self._p_version.minor >= 13 and self.disable_gil:
315+
if Version(self.version).minor >= 13 and self.disable_gil:
316316
self.configure_args.append("--disable-gil")
317317

318318
return env
@@ -398,7 +398,7 @@ def create_python_bundle(self, dirn, arch):
398398
'build',
399399
'lib.{}{}-{}-{}'.format(
400400
# android is now supported platform
401-
"android" if self._p_version.minor >= 13 else "linux",
401+
"android" if Version(self.version).minor >= 13 else "linux",
402402
'2' if self.version[0] == '2' else '',
403403
arch.command_prefix.split('-')[0],
404404
self.major_minor_version_string

0 commit comments

Comments
 (0)