Skip to content

Commit c9d2633

Browse files
committed
[recipe-stl] Add android's STL lib support to Recipe
To allow us to refactor some common operations that we use in our recipes that depends on android's STL library. Note: This commit will allow us to begin the migration to `c++_shared`. This is a must when we move to android's NDK r19+, because as for android NDK >= 18 is the only one supported STL library.
1 parent 9801865 commit c9d2633

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

pythonforandroid/recipe.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,46 @@ class Recipe(with_metaclass(RecipeMeta)):
125125
path: `'.', None or ''`
126126
"""
127127

128+
need_stl_shared = False
129+
'''Some libraries or python packages may need to be linked with android's
130+
stl. We can automatically do this for any recipe if we set this property to
131+
`True`'''
132+
133+
stl_lib_name = 'c++_shared'
134+
'''
135+
The default STL shared lib to use: `c++_shared`.
136+
137+
.. note:: Android NDK version > 17 only supports 'c++_shared', because
138+
starting from NDK r18 the `gnustl_shared` lib has been deprecated.
139+
'''
140+
141+
stl_lib_source = '{ctx.ndk_dir}/sources/cxx-stl/llvm-libc++'
142+
'''
143+
The source directory of the selected stl lib, defined in property
144+
`stl_lib_name`
145+
'''
146+
147+
@property
148+
def stl_include_dir(self):
149+
return join(self.stl_lib_source.format(ctx=self.ctx), 'include')
150+
151+
def get_stl_lib_dir(self, arch):
152+
return join(
153+
self.stl_lib_source.format(ctx=self.ctx), 'libs', arch.arch
154+
)
155+
156+
def get_stl_library(self, arch):
157+
return join(
158+
self.get_stl_lib_dir(arch),
159+
'lib{name}.so'.format(name=self.stl_lib_name),
160+
)
161+
162+
def install_stl_lib(self, arch):
163+
if not self.ctx.has_lib(
164+
arch.arch, 'lib{name}.so'.format(name=self.stl_lib_name)
165+
):
166+
self.install_libs(arch, self.get_stl_library(arch))
167+
128168
@property
129169
def version(self):
130170
key = 'VERSION_' + self.name
@@ -454,7 +494,22 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
454494
"""
455495
if arch is None:
456496
arch = self.filtered_archs[0]
457-
return arch.get_env(with_flags_in_cc=with_flags_in_cc)
497+
env = arch.get_env(with_flags_in_cc=with_flags_in_cc)
498+
499+
if self.need_stl_shared:
500+
env['CPPFLAGS'] = env.get('CPPFLAGS', '')
501+
env['CPPFLAGS'] += ' -I{}'.format(self.stl_include_dir)
502+
503+
env['CXXFLAGS'] = env['CFLAGS'] + ' -frtti -fexceptions'
504+
505+
if with_flags_in_cc:
506+
env['CXX'] += ' -frtti -fexceptions'
507+
508+
env['LDFLAGS'] += ' -L{}'.format(self.get_stl_lib_dir(arch))
509+
env['LIBS'] = env.get('LIBS', '') + " -l{}".format(
510+
self.stl_lib_name
511+
)
512+
return env
458513

459514
def prebuild_arch(self, arch):
460515
'''Run any pre-build tasks for the Recipe. By default, this checks if
@@ -538,6 +593,9 @@ def postbuild_arch(self, arch):
538593
if hasattr(self, postbuild):
539594
getattr(self, postbuild)()
540595

596+
if self.need_stl_shared:
597+
self.install_stl_lib(arch)
598+
541599
def prepare_build_dir(self, arch):
542600
'''Copies the recipe data into a build dir for the given arch. By
543601
default, this unpacks a downloaded recipe. You should override

0 commit comments

Comments
 (0)