1
- from pythonforandroid .recipe import PythonRecipe
1
+ from pythonforandroid .recipe import CppCompiledComponentsPythonRecipe
2
2
from pythonforandroid .logger import shprint , info_notify
3
- from pythonforandroid .util import current_directory , shutil
3
+ from pythonforandroid .util import current_directory
4
4
from os .path import exists , join
5
5
import sh
6
6
from multiprocessing import cpu_count
9
9
import os
10
10
11
11
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
+ """
13
17
name = 'protobuf_cpp'
14
18
version = '3.6.1'
15
19
url = 'https://github.com/google/protobuf/releases/download/v{version}/protobuf-python-{version}.tar.gz'
16
20
call_hostpython_via_targetpython = False
17
21
depends = ['cffi' , 'setuptools' ]
18
22
site_packages_name = 'google/protobuf/pyext'
23
+ setup_extra_args = ['--cpp_implementation' ]
24
+ built_libraries = {'libprotobuf.so' : 'src/.libs' }
19
25
protoc_dir = None
20
26
21
27
def prebuild_arch (self , arch ):
@@ -65,42 +71,37 @@ def prebuild_arch(self, arch):
65
71
def build_arch (self , arch ):
66
72
env = self .get_recipe_env (arch )
67
73
68
- # Build libproto.a
74
+ # Build libproto.so
69
75
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
+ )
72
81
73
82
if not exists ('configure' ):
74
83
shprint (sh .Command ('./autogen.sh' ), _env = env )
75
84
76
85
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' ,
78
90
'--enable-shared' ,
79
91
_env = env )
80
92
81
93
with current_directory (join (self .get_build_dir (arch .arch ), 'src' )):
82
94
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' ))
89
95
96
+ def build_compiled_components (self , arch ):
90
97
# Build python bindings and _message.so
98
+ env = self .get_recipe_env (arch )
91
99
with current_directory (join (self .get_build_dir (arch .arch ), 'python' )):
92
100
hostpython = sh .Command (self .hostpython_location )
93
101
shprint (hostpython ,
94
102
'setup.py' ,
95
103
'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 )
104
105
105
106
def install_python_package (self , arch ):
106
107
env = self .get_recipe_env (arch )
@@ -114,32 +115,25 @@ def install_python_package(self, arch):
114
115
shprint (hostpython , 'setup.py' , 'install' , '-O2' ,
115
116
'--root={}' .format (self .ctx .get_python_install_dir ()),
116
117
'--install-lib=.' ,
117
- '--cpp_implementation' ,
118
118
_env = hpenv , * self .setup_extra_args )
119
119
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
+
120
129
def get_recipe_env (self , arch ):
121
130
env = super (ProtobufCppRecipe , self ).get_recipe_env (arch )
122
131
if self .protoc_dir is not None :
123
132
# we need protoc with binary for host platform
124
133
env ['PROTOC' ] = join (self .protoc_dir , 'bin' , 'protoc' )
125
134
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'
143
137
return env
144
138
145
139
0 commit comments