Skip to content

Commit 1584f40

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 e21cd8b commit 1584f40

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
@@ -106,6 +106,46 @@ class Recipe(with_metaclass(RecipeMeta)):
106106

107107
archs = ['armeabi'] # Not currently implemented properly
108108

109+
need_stl_shared = False
110+
'''Some libraries or python packages may need to be linked with android's
111+
stl. We can automatically do this for any recipe if we set this property to
112+
`True`'''
113+
114+
stl_lib_name = 'c++_shared'
115+
'''
116+
The default STL shared lib to use: `c++_shared`.
117+
118+
.. note:: Android NDK version > 17 only supports 'c++_shared', because
119+
starting from NDK r18 the `gnustl_shared` lib has been deprecated.
120+
'''
121+
122+
stl_lib_source = '{ctx.ndk_dir}/sources/cxx-stl/llvm-libc++'
123+
'''
124+
The source directory of the selected stl lib, defined in property
125+
`stl_lib_name`
126+
'''
127+
128+
@property
129+
def stl_include_dir(self):
130+
return join(self.stl_lib_source.format(ctx=self.ctx), 'include')
131+
132+
def get_stl_lib_dir(self, arch):
133+
return join(
134+
self.stl_lib_source.format(ctx=self.ctx), 'libs', arch.arch
135+
)
136+
137+
def get_stl_library(self, arch):
138+
return join(
139+
self.get_stl_lib_dir(arch),
140+
'lib{name}.so'.format(name=self.stl_lib_name),
141+
)
142+
143+
def install_stl_lib(self, arch):
144+
if not self.ctx.has_lib(
145+
arch.arch, 'lib{name}.so'.format(name=self.stl_lib_name)
146+
):
147+
self.install_libs(arch, self.get_stl_library(arch))
148+
109149
@property
110150
def version(self):
111151
key = 'VERSION_' + self.name
@@ -435,7 +475,22 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True, clang=False):
435475
"""
436476
if arch is None:
437477
arch = self.filtered_archs[0]
438-
return arch.get_env(with_flags_in_cc=with_flags_in_cc, clang=clang)
478+
env = arch.get_env(with_flags_in_cc=with_flags_in_cc, clang=clang)
479+
480+
if self.need_stl_shared:
481+
env['CPPFLAGS'] = env.get('CPPFLAGS', '')
482+
env['CPPFLAGS'] += ' -I{}'.format(self.stl_include_dir)
483+
484+
env['CXXFLAGS'] = env['CFLAGS'] + ' -frtti -fexceptions'
485+
486+
if with_flags_in_cc:
487+
env['CXX'] += ' -frtti -fexceptions'
488+
489+
env['LDFLAGS'] += ' -L{}'.format(self.get_stl_lib_dir(arch))
490+
env['LIBS'] = env.get('LIBS', '') + " -l{}".format(
491+
self.stl_lib_name
492+
)
493+
return env
439494

440495
def prebuild_arch(self, arch):
441496
'''Run any pre-build tasks for the Recipe. By default, this checks if
@@ -501,6 +556,9 @@ def postbuild_arch(self, arch):
501556
if hasattr(self, postbuild):
502557
getattr(self, postbuild)()
503558

559+
if self.need_stl_shared:
560+
self.install_stl_lib(arch)
561+
504562
def prepare_build_dir(self, arch):
505563
'''Copies the recipe data into a build dir for the given arch. By
506564
default, this unpacks a downloaded recipe. You should override

0 commit comments

Comments
 (0)