Skip to content

Commit f77973f

Browse files
committed
Added protobuf cpp extension
1 parent bd975de commit f77973f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from pythonforandroid.recipe import PythonRecipe
2+
from pythonforandroid.logger import shprint
3+
from pythonforandroid.util import current_directory
4+
from os.path import exists, join, dirname
5+
import sh
6+
from multiprocessing import cpu_count
7+
8+
9+
from pythonforandroid.toolchain import info
10+
11+
12+
class ProtobufCppRecipe(PythonRecipe):
13+
name = 'protobuf_cpp'
14+
version = '3.1.0'
15+
url = 'https://github.com/google/protobuf/releases/download/v{version}/protobuf-python-{version}.tar.gz'
16+
call_hostpython_via_targetpython = False
17+
depends = ['cffi', 'setuptools']
18+
19+
20+
def build_arch(self, arch):
21+
env = self.get_recipe_env(arch)
22+
23+
# Build libproto.a
24+
with current_directory(self.get_build_dir(arch.arch)):
25+
env['HOSTARCH'] = 'arm-eabi'
26+
env['BUILDARCH'] = shprint(sh.gcc, '-dumpmachine').stdout.decode('utf-8').split('\n')[0]
27+
28+
if not exists('configure'):
29+
shprint(sh.Command('./autogen.sh'), _env=env)
30+
31+
shprint(sh.Command('./configure'),
32+
'--host={}'.format(env['HOSTARCH']),
33+
'--enable-shared',
34+
_env=env)
35+
36+
with current_directory(join(self.get_build_dir(arch.arch), 'src')):
37+
shprint(sh.make, 'libprotobuf.la', '-j'+str(cpu_count()), _env=env)
38+
shprint(sh.cp, '.libs/libprotobuf.a', join(self.ctx.get_libs_dir(arch.arch), 'libprotobuf.a'))
39+
40+
# Build python bindings and _message.so
41+
with current_directory(join(self.get_build_dir(arch.arch), 'python')):
42+
hostpython = sh.Command(self.hostpython_location)
43+
shprint(hostpython,
44+
'setup.py',
45+
'build_ext',
46+
'--cpp_implementation'
47+
, _env=env)
48+
49+
# Install python bindings
50+
self.install_python_package(arch)
51+
52+
53+
def install_python_package(self, arch):
54+
env = self.get_recipe_env(arch)
55+
56+
info('Installing {} into site-packages'.format(self.name))
57+
58+
with current_directory(join(self.get_build_dir(arch.arch), 'python')):
59+
hostpython = sh.Command(self.hostpython_location)
60+
61+
if self.ctx.python_recipe.from_crystax:
62+
hpenv = env.copy()
63+
shprint(hostpython, 'setup.py', 'install', '-O2',
64+
'--root={}'.format(self.ctx.get_python_install_dir()),
65+
'--install-lib=.',
66+
'--cpp_implementation',
67+
_env=hpenv, *self.setup_extra_args)
68+
else:
69+
hppath = join(dirname(self.hostpython_location), 'Lib',
70+
'site-packages')
71+
hpenv = env.copy()
72+
if 'PYTHONPATH' in hpenv:
73+
hpenv['PYTHONPATH'] = ':'.join([hppath] +
74+
hpenv['PYTHONPATH'].split(':'))
75+
else:
76+
hpenv['PYTHONPATH'] = hppath
77+
shprint(hostpython, 'setup.py', 'install', '-O2',
78+
'--root={}'.format(self.ctx.get_python_install_dir()),
79+
'--install-lib=lib/python2.7/site-packages',
80+
'--cpp_implementation',
81+
_env=hpenv, *self.setup_extra_args)
82+
83+
84+
def get_recipe_env(self, arch):
85+
env = super(ProtobufCppRecipe, self).get_recipe_env(arch)
86+
env['PROTOC'] = '/home/fipo/soft/protobuf-3.1.0/src/protoc'
87+
env['PYTHON_ROOT'] = self.ctx.get_python_install_dir()
88+
env['TARGET_OS'] = 'OS_ANDROID_CROSSCOMPILE'
89+
env['CFLAGS'] += ' -I' + self.ctx.ndk_dir + '/platforms/android-' + str(
90+
self.ctx.android_api) + '/arch-' + arch.arch.replace('eabi', '') + '/usr/include' + \
91+
' -I' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' + self.ctx.toolchain_version + '/include' + \
92+
' -I' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' + self.ctx.toolchain_version + '/libs/' + arch.arch + '/include' + \
93+
' -I' + env['PYTHON_ROOT'] + '/include/python2.7'
94+
env['CXXFLAGS'] = env['CFLAGS']
95+
env['CXXFLAGS'] += ' -frtti'
96+
env['CXXFLAGS'] += ' -fexceptions'
97+
env['LDFLAGS'] += ' -L' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' + self.ctx.toolchain_version + '/libs/' + arch.arch + \
98+
' -lgnustl_shared -lpython2.7'
99+
100+
env['LDSHARED'] = env['CC'] + ' -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions'
101+
return env
102+
103+
104+
recipe = ProtobufCppRecipe()

0 commit comments

Comments
 (0)