Skip to content

Commit dab65a2

Browse files
committed
[dist] Implement GuestPythonRecipe.should_build
To do so, we also create new class properties, so we can do a little refactor:   - `link_version`: the python link version, eg: `3.7m`   - `lib_name`: the python library name, eg: `libpython3.7m.so` Note: Those new properties are created because, I needed those values to create `should_build`
1 parent 06e27e2 commit dab65a2

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

pythonforandroid/python.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ def __init__(self, *args, **kwargs):
103103
self._ctx = None
104104
super(GuestPythonRecipe, self).__init__(*args, **kwargs)
105105

106+
@property
107+
def link_version(self):
108+
py_version = self.major_minor_version_string
109+
if py_version[0] == '3':
110+
py_version += 'm'
111+
return py_version
112+
113+
@property
114+
def lib_name(self):
115+
return 'libpython{version}.so'.format(version=self.link_version)
116+
117+
def should_build(self, arch):
118+
if not exists(self.link_root(arch.arch)):
119+
return True
120+
if exists(join(self.link_root(arch.arch), self.lib_name)):
121+
return False
122+
return True
123+
106124
def get_recipe_env(self, arch=None, with_flags_in_cc=True):
107125
env = environ.copy()
108126

@@ -244,12 +262,9 @@ def build_arch(self, arch):
244262
_env=env)
245263

246264
if not exists('python'):
247-
py_version = self.major_minor_version_string
248-
if self.major_minor_version_string[0] == '3':
249-
py_version += 'm'
250265
shprint(sh.make, 'all', '-j', str(cpu_count()),
251-
'INSTSONAME=libpython{version}.so'.format(
252-
version=py_version), _env=env)
266+
'INSTSONAME={lib_name}'.format(lib_name=self.lib_name),
267+
_env=env)
253268

254269
# TODO: Look into passing the path to pyconfig.h in a
255270
# better way, although this is probably acceptable
@@ -335,12 +350,7 @@ def create_python_bundle(self, dirn, arch):
335350
copy2(filen, join(dirn, 'site-packages', filen))
336351

337352
# copy the python .so files into place
338-
python_build_dir = join(self.get_build_dir(arch.arch),
339-
'android-build')
340-
python_lib_name = 'libpython' + self.major_minor_version_string
341-
if self.major_minor_version_string[0] == '3':
342-
python_lib_name += 'm'
343-
shprint(sh.cp, join(python_build_dir, python_lib_name + '.so'),
353+
shprint(sh.cp, join(self.link_root(arch.arch), self.lib_name),
344354
join(self.ctx.dist_dir, self.ctx.dist_name, 'libs', arch.arch))
345355

346356
info('Renaming .so files to reflect cross-compile')

pythonforandroid/recipe.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,8 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True, with_python=False):
690690
env['PYTHON_INCLUDE_ROOT'] = self.ctx.python_recipe.include_root(arch.arch)
691691
env['PYTHON_LINK_ROOT'] = self.ctx.python_recipe.link_root(arch.arch)
692692
env['EXTRA_LDLIBS'] = ' -lpython{}'.format(
693-
self.ctx.python_recipe.major_minor_version_string)
694-
if 'python3' in self.ctx.python_recipe.name:
695-
env['EXTRA_LDLIBS'] += 'm'
693+
self.ctx.python_recipe.link_version
694+
)
696695
return env
697696

698697

@@ -833,16 +832,13 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
833832
env['LANG'] = "en_GB.UTF-8"
834833

835834
if not self.call_hostpython_via_targetpython:
836-
python_name = self.ctx.python_recipe.name
837835
env['CFLAGS'] += ' -I{}'.format(
838836
self.ctx.python_recipe.include_root(arch.arch)
839837
)
840838
env['LDFLAGS'] += ' -L{} -lpython{}'.format(
841839
self.ctx.python_recipe.link_root(arch.arch),
842-
self.ctx.python_recipe.major_minor_version_string,
840+
self.ctx.python_recipe.link_version,
843841
)
844-
if python_name == 'python3':
845-
env['LDFLAGS'] += 'm'
846842

847843
hppath = []
848844
hppath.append(join(dirname(self.hostpython_location), 'Lib'))

pythonforandroid/recipes/boost/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ def get_recipe_env(self, arch):
9191
env['PYTHON_ROOT'] = self.ctx.python_recipe.link_root(arch.arch)
9292
env['PYTHON_INCLUDE'] = self.ctx.python_recipe.include_root(arch.arch)
9393
env['PYTHON_MAJOR_MINOR'] = self.ctx.python_recipe.version[:3]
94-
env['PYTHON_LINK_VERSION'] = self.ctx.python_recipe.major_minor_version_string
95-
if 'python3' in self.ctx.python_recipe.name:
96-
env['PYTHON_LINK_VERSION'] += 'm'
94+
env['PYTHON_LINK_VERSION'] = self.ctx.python_recipe.link_version
9795

9896
env['ARCH'] = self.select_build_arch(arch)
9997
env['CROSSHOST'] = env['ARCH'] + '-linux-androideabi'

pythonforandroid/recipes/cffi/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def get_recipe_env(self, arch=None):
4444
env['BUILDLIB_PATH'],
4545
])
4646
env['LDFLAGS'] += ' -L{}'.format(self.ctx.python_recipe.link_root(arch.arch))
47-
env['LDFLAGS'] += ' -lpython{}'.format(self.ctx.python_recipe.major_minor_version_string)
48-
if 'python3' in self.ctx.python_recipe.name:
49-
env['LDFLAGS'] += 'm'
47+
env['LDFLAGS'] += ' -lpython{}'.format(
48+
self.ctx.python_recipe.link_version
49+
)
5050
return env
5151

5252

pythonforandroid/recipes/numpy/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ def get_recipe_env(self, arch):
3838
self.ctx.ndk_platform
3939
)
4040

41-
py_ver = self.ctx.python_recipe.major_minor_version_string
41+
py_ver = self.ctx.python_recipe.link_version
4242
py_inc_dir = self.ctx.python_recipe.include_root(arch.arch)
4343
py_lib_dir = self.ctx.python_recipe.link_root(arch.arch)
4444
flags += ' -I{}'.format(py_inc_dir)
4545
flags += ' -L{} -lpython{}'.format(py_lib_dir, py_ver)
46-
if 'python3' in self.ctx.python_recipe.name:
47-
flags += 'm'
4846

4947
if flags not in env['CC']:
5048
env['CC'] += flags

pythonforandroid/recipes/opencv/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ def build_arch(self, arch):
5353
python_include_root = self.ctx.python_recipe.include_root(arch.arch)
5454
python_site_packages = self.ctx.get_site_packages_dir()
5555
python_link_root = self.ctx.python_recipe.link_root(arch.arch)
56-
python_link_version = self.ctx.python_recipe.major_minor_version_string
57-
if 'python3' in self.ctx.python_recipe.name:
58-
python_link_version += 'm'
59-
python_library = join(python_link_root,
60-
'libpython{}.so'.format(python_link_version))
56+
python_link_version = self.ctx.python_recipe.link_version
57+
python_library = join(
58+
python_link_root, self.ctx.python_recipe.lib_name
59+
)
6160
python_include_numpy = join(python_site_packages,
6261
'numpy', 'core', 'include')
6362

0 commit comments

Comments
 (0)