Skip to content

Commit 5647b35

Browse files
committed
[recipe-stl] Rework of protobuf_cpp recipe
In here we do: - inherit from CppCompiledComponentsPythonRecipe Because depends on android's STL library - make use of the base class methods for library recipes - Split build_arch into proper methods - Shorten some long lines (to be PEP8 friendly) - make generated library shared - remove recipe from CI/constants
1 parent 9801865 commit 5647b35

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

ci/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class TargetPython(Enum):
6868
'icu',
6969
# requires `libpq-dev` system dependency e.g. for `pg_config` binary
7070
'psycopg2',
71-
'protobuf_cpp',
7271
# most likely some setup in the Docker container, because it works in host
7372
'pyjnius', 'pyopenal',
7473
# SyntaxError: invalid syntax (Python2)

pythonforandroid/recipes/protobuf_cpp/__init__.py

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from pythonforandroid.recipe import PythonRecipe
1+
from pythonforandroid.recipe import CppCompiledComponentsPythonRecipe
22
from pythonforandroid.logger import shprint, info_notify
3-
from pythonforandroid.util import current_directory, shutil
3+
from pythonforandroid.util import current_directory
44
from os.path import exists, join
55
import sh
66
from multiprocessing import cpu_count
@@ -9,13 +9,19 @@
99
import os
1010

1111

12-
class ProtobufCppRecipe(PythonRecipe):
12+
class ProtobufCppRecipe(CppCompiledComponentsPythonRecipe):
13+
"""This is a two-in-one recipe:
14+
- build labraru `libprotobuf.so`
15+
- build and install python binding for protobuf_cpp
16+
"""
1317
name = 'protobuf_cpp'
1418
version = '3.6.1'
1519
url = 'https://github.com/google/protobuf/releases/download/v{version}/protobuf-python-{version}.tar.gz'
1620
call_hostpython_via_targetpython = False
1721
depends = ['cffi', 'setuptools']
1822
site_packages_name = 'google/protobuf/pyext'
23+
setup_extra_args = ['--cpp_implementation']
24+
built_libraries = {'libprotobuf.so': 'src/.libs'}
1925
protoc_dir = None
2026

2127
def prebuild_arch(self, arch):
@@ -65,42 +71,37 @@ def prebuild_arch(self, arch):
6571
def build_arch(self, arch):
6672
env = self.get_recipe_env(arch)
6773

68-
# Build libproto.a
74+
# Build libproto.so
6975
with current_directory(self.get_build_dir(arch.arch)):
70-
env['HOSTARCH'] = 'arm-eabi'
71-
env['BUILDARCH'] = shprint(sh.gcc, '-dumpmachine').stdout.decode('utf-8').split('\n')[0]
76+
build_arch = (
77+
shprint(sh.gcc, '-dumpmachine')
78+
.stdout.decode('utf-8')
79+
.split('\n')[0]
80+
)
7281

7382
if not exists('configure'):
7483
shprint(sh.Command('./autogen.sh'), _env=env)
7584

7685
shprint(sh.Command('./configure'),
77-
'--host={}'.format(env['HOSTARCH']),
86+
'--build={}'.format(build_arch),
87+
'--host={}'.format(arch.command_prefix),
88+
'--target={}'.format(arch.command_prefix),
89+
'--disable-static',
7890
'--enable-shared',
7991
_env=env)
8092

8193
with current_directory(join(self.get_build_dir(arch.arch), 'src')):
8294
shprint(sh.make, 'libprotobuf.la', '-j'+str(cpu_count()), _env=env)
83-
shprint(sh.cp, '.libs/libprotobuf.a', join(self.ctx.get_libs_dir(arch.arch), 'libprotobuf.a'))
84-
85-
# Copy stl library
86-
shutil.copyfile(
87-
self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' + self.ctx.toolchain_version + '/libs/' + arch.arch + '/libgnustl_shared.so',
88-
join(self.ctx.get_libs_dir(arch.arch), 'libgnustl_shared.so'))
8995

96+
def build_compiled_components(self, arch):
9097
# Build python bindings and _message.so
98+
env = self.get_recipe_env(arch)
9199
with current_directory(join(self.get_build_dir(arch.arch), 'python')):
92100
hostpython = sh.Command(self.hostpython_location)
93101
shprint(hostpython,
94102
'setup.py',
95103
'build_ext',
96-
'--cpp_implementation', _env=env)
97-
98-
# Install python bindings
99-
self.install_python_package(arch)
100-
101-
# Create __init__.py which is missing (cf. https://github.com/protocolbuffers/protobuf/issues/1296
102-
# and https://stackoverflow.com/questions/13862562/google-protocol-buffers-not-found-when-trying-to-freeze-python-app)
103-
open(join(self.ctx.get_site_packages_dir(), 'google', '__init__.py'), 'a').close()
104+
_env=env, *self.setup_extra_args)
104105

105106
def install_python_package(self, arch):
106107
env = self.get_recipe_env(arch)
@@ -114,32 +115,25 @@ def install_python_package(self, arch):
114115
shprint(hostpython, 'setup.py', 'install', '-O2',
115116
'--root={}'.format(self.ctx.get_python_install_dir()),
116117
'--install-lib=.',
117-
'--cpp_implementation',
118118
_env=hpenv, *self.setup_extra_args)
119119

120+
# Create __init__.py which is missing, see also:
121+
# - https://github.com/protocolbuffers/protobuf/issues/1296
122+
# - https://stackoverflow.com/questions/13862562/
123+
# google-protocol-buffers-not-found-when-trying-to-freeze-python-app
124+
open(
125+
join(self.ctx.get_site_packages_dir(), 'google', '__init__.py'),
126+
'a',
127+
).close()
128+
120129
def get_recipe_env(self, arch):
121130
env = super(ProtobufCppRecipe, self).get_recipe_env(arch)
122131
if self.protoc_dir is not None:
123132
# we need protoc with binary for host platform
124133
env['PROTOC'] = join(self.protoc_dir, 'bin', 'protoc')
125134
env['TARGET_OS'] = 'OS_ANDROID_CROSSCOMPILE'
126-
env['CFLAGS'] += (
127-
' -I' + self.ctx.ndk_dir + '/platforms/android-' +
128-
str(self.ctx.android_api) +
129-
'/arch-' + arch.arch.replace('eabi', '') + '/usr/include' +
130-
' -I' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' +
131-
self.ctx.toolchain_version + '/include' +
132-
' -I' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' +
133-
self.ctx.toolchain_version + '/libs/' + arch.arch + '/include')
134-
env['CFLAGS'] += ' -std=gnu++11'
135-
env['CXXFLAGS'] = env['CFLAGS']
136-
env['CXXFLAGS'] += ' -frtti'
137-
env['CXXFLAGS'] += ' -fexceptions'
138-
env['LDFLAGS'] += (
139-
' -lgnustl_shared -landroid -llog' +
140-
' -L' + self.ctx.ndk_dir +
141-
'/sources/cxx-stl/gnu-libstdc++/' + self.ctx.toolchain_version +
142-
'/libs/' + arch.arch)
135+
env['CXXFLAGS'] += ' -std=c++11'
136+
env['LDFLAGS'] += ' -lm -landroid -llog'
143137
return env
144138

145139

0 commit comments

Comments
 (0)