Skip to content

Commit bf214b3

Browse files
opacamAndreMiras
authored andcommitted
[WIP][CORE UPDATE - PART X] Protobuf_cpp fixes and updated version (kivy#1551)
* Update protobuf_cpp recipe and grant python3 compatibility The removed flags are already set in base class, so no need to set in here. * Move libraries from LDFLAGS to LIBS for protobuf_cpp recipe Because this is how you are supposed to do it, you must use LDFLAGS for linker flags and LDLIBS (or the equivalent LOADLIBES) for the libraries
1 parent ddb46f6 commit bf214b3

File tree

2 files changed

+109
-28
lines changed

2 files changed

+109
-28
lines changed

pythonforandroid/recipes/protobuf_cpp/__init__.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pythonforandroid.recipe import PythonRecipe
22
from pythonforandroid.logger import shprint, info_notify
33
from pythonforandroid.util import current_directory, shutil
4-
from os.path import exists, join, dirname
4+
from os.path import exists, join
55
import sh
66
from multiprocessing import cpu_count
77
from pythonforandroid.toolchain import info
@@ -11,7 +11,7 @@
1111

1212
class ProtobufCppRecipe(PythonRecipe):
1313
name = 'protobuf_cpp'
14-
version = '3.5.1'
14+
version = '3.6.1'
1515
url = 'https://github.com/google/protobuf/releases/download/v{version}/protobuf-python-{version}.tar.gz'
1616
call_hostpython_via_targetpython = False
1717
depends = ['cffi', 'setuptools']
@@ -20,6 +20,12 @@ class ProtobufCppRecipe(PythonRecipe):
2020

2121
def prebuild_arch(self, arch):
2222
super(ProtobufCppRecipe, self).prebuild_arch(arch)
23+
24+
patch_mark = join(self.get_build_dir(arch.arch), '.protobuf-patched')
25+
if self.ctx.python_recipe.name == 'python3' and not exists(patch_mark):
26+
self.apply_patch('fix-python3-compatibility.patch', arch.arch)
27+
shprint(sh.touch, patch_mark)
28+
2329
# During building, host needs to transpile .proto files to .py
2430
# ideally with the same version as protobuf runtime, or with an older one.
2531
# Because protoc is compiled for target (i.e. Android), we need an other binary
@@ -100,34 +106,18 @@ def install_python_package(self, arch):
100106
with current_directory(join(self.get_build_dir(arch.arch), 'python')):
101107
hostpython = sh.Command(self.hostpython_location)
102108

103-
if self.ctx.python_recipe.from_crystax:
104-
hpenv = env.copy()
105-
shprint(hostpython, 'setup.py', 'install', '-O2',
106-
'--root={}'.format(self.ctx.get_python_install_dir()),
107-
'--install-lib=.',
108-
'--cpp_implementation',
109-
_env=hpenv, *self.setup_extra_args)
110-
else:
111-
hppath = join(dirname(self.hostpython_location), 'Lib',
112-
'site-packages')
113-
hpenv = env.copy()
114-
if 'PYTHONPATH' in hpenv:
115-
hpenv['PYTHONPATH'] = ':'.join([hppath] +
116-
hpenv['PYTHONPATH'].split(':'))
117-
else:
118-
hpenv['PYTHONPATH'] = hppath
119-
shprint(hostpython, 'setup.py', 'install', '-O2',
120-
'--root={}'.format(self.ctx.get_python_install_dir()),
121-
'--install-lib=lib/python2.7/site-packages',
122-
'--cpp_implementation',
123-
_env=hpenv, *self.setup_extra_args)
109+
hpenv = env.copy()
110+
shprint(hostpython, 'setup.py', 'install', '-O2',
111+
'--root={}'.format(self.ctx.get_python_install_dir()),
112+
'--install-lib=.',
113+
'--cpp_implementation',
114+
_env=hpenv, *self.setup_extra_args)
124115

125116
def get_recipe_env(self, arch):
126117
env = super(ProtobufCppRecipe, self).get_recipe_env(arch)
127118
if self.protoc_dir is not None:
128119
# we need protoc with binary for host platform
129120
env['PROTOC'] = join(self.protoc_dir, 'bin', 'protoc')
130-
env['PYTHON_ROOT'] = self.ctx.get_python_install_dir()
131121
env['TARGET_OS'] = 'OS_ANDROID_CROSSCOMPILE'
132122
env['CFLAGS'] += (
133123
' -I' + self.ctx.ndk_dir + '/platforms/android-' +
@@ -136,17 +126,17 @@ def get_recipe_env(self, arch):
136126
' -I' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' +
137127
self.ctx.toolchain_version + '/include' +
138128
' -I' + self.ctx.ndk_dir + '/sources/cxx-stl/gnu-libstdc++/' +
139-
self.ctx.toolchain_version + '/libs/' + arch.arch + '/include' +
140-
' -I' + env['PYTHON_ROOT'] + '/include/python2.7')
129+
self.ctx.toolchain_version + '/libs/' + arch.arch + '/include')
130+
env['CFLAGS'] += ' -std=gnu++11'
141131
env['CXXFLAGS'] = env['CFLAGS']
142132
env['CXXFLAGS'] += ' -frtti'
143133
env['CXXFLAGS'] += ' -fexceptions'
144134
env['LDFLAGS'] += (
145135
' -L' + self.ctx.ndk_dir +
146136
'/sources/cxx-stl/gnu-libstdc++/' + self.ctx.toolchain_version +
147-
'/libs/' + arch.arch + ' -lgnustl_shared -lpython2.7 -landroid -llog')
137+
'/libs/' + arch.arch)
138+
env['LIBS'] = env.get('LIBS', '') + ' -lgnustl_shared -landroid -llog'
148139

149-
env['LDSHARED'] = env['CC'] + ' -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions'
150140
return env
151141

152142

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
From 539bc017a62f91bdf7c547b58948cb5a2f59d918 Mon Sep 17 00:00:00 2001
2+
From: Ben Webb <ben@salilab.org>
3+
Date: Thu, 12 Jul 2018 10:58:10 -0700
4+
Subject: [PATCH] Add Python 3.7 compatibility (#4862)
5+
6+
Compilation of Python wrappers fails with Python 3.7 because
7+
the Python folks changed their C API such that
8+
PyUnicode_AsUTF8AndSize() now returns a const char* rather
9+
than a char*. Add a patch to work around. Relates #4086.
10+
---
11+
python/google/protobuf/pyext/descriptor.cc | 2 +-
12+
python/google/protobuf/pyext/descriptor_containers.cc | 2 +-
13+
python/google/protobuf/pyext/descriptor_pool.cc | 2 +-
14+
python/google/protobuf/pyext/extension_dict.cc | 2 +-
15+
python/google/protobuf/pyext/message.cc | 4 ++--
16+
5 files changed, 6 insertions(+), 6 deletions(-)
17+
18+
diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc
19+
index 8af0cb1289..19a1c38a62 100644
20+
--- a/python/google/protobuf/pyext/descriptor.cc
21+
+++ b/python/google/protobuf/pyext/descriptor.cc
22+
@@ -56,7 +56,7 @@
23+
#endif
24+
#define PyString_AsStringAndSize(ob, charpp, sizep) \
25+
(PyUnicode_Check(ob)? \
26+
- ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
27+
+ ((*(charpp) = const_cast<char*>(PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL? -1: 0): \
28+
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
29+
#endif
30+
31+
diff --git a/python/google/protobuf/pyext/descriptor_containers.cc b/python/google/protobuf/pyext/descriptor_containers.cc
32+
index bc007f7efa..0153664f50 100644
33+
--- a/python/google/protobuf/pyext/descriptor_containers.cc
34+
+++ b/python/google/protobuf/pyext/descriptor_containers.cc
35+
@@ -66,7 +66,7 @@
36+
#endif
37+
#define PyString_AsStringAndSize(ob, charpp, sizep) \
38+
(PyUnicode_Check(ob)? \
39+
- ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
40+
+ ((*(charpp) = const_cast<char*>(PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL? -1: 0): \
41+
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
42+
#endif
43+
44+
diff --git a/python/google/protobuf/pyext/descriptor_pool.cc b/python/google/protobuf/pyext/descriptor_pool.cc
45+
index 95882aeb35..962accc6e9 100644
46+
--- a/python/google/protobuf/pyext/descriptor_pool.cc
47+
+++ b/python/google/protobuf/pyext/descriptor_pool.cc
48+
@@ -48,7 +48,7 @@
49+
#endif
50+
#define PyString_AsStringAndSize(ob, charpp, sizep) \
51+
(PyUnicode_Check(ob)? \
52+
- ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
53+
+ ((*(charpp) = const_cast<char*>(PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL? -1: 0): \
54+
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
55+
#endif
56+
57+
diff --git a/python/google/protobuf/pyext/extension_dict.cc b/python/google/protobuf/pyext/extension_dict.cc
58+
index 018b5c2c49..174c5470c2 100644
59+
--- a/python/google/protobuf/pyext/extension_dict.cc
60+
+++ b/python/google/protobuf/pyext/extension_dict.cc
61+
@@ -53,7 +53,7 @@
62+
#endif
63+
#define PyString_AsStringAndSize(ob, charpp, sizep) \
64+
(PyUnicode_Check(ob)? \
65+
- ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
66+
+ ((*(charpp) = const_cast<char*>(PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL? -1: 0): \
67+
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
68+
#endif
69+
70+
diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc
71+
index 5893533adf..31094b7e10 100644
72+
--- a/python/google/protobuf/pyext/message.cc
73+
+++ b/python/google/protobuf/pyext/message.cc
74+
@@ -79,7 +79,7 @@
75+
(PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob))
76+
#define PyString_AsStringAndSize(ob, charpp, sizep) \
77+
(PyUnicode_Check(ob)? \
78+
- ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
79+
+ ((*(charpp) = const_cast<char*>(PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL? -1: 0): \
80+
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
81+
#endif
82+
#endif
83+
@@ -1529,7 +1529,7 @@ PyObject* HasField(CMessage* self, PyObject* arg) {
84+
return NULL;
85+
}
86+
#else
87+
- field_name = PyUnicode_AsUTF8AndSize(arg, &size);
88+
+ field_name = const_cast<char*>(PyUnicode_AsUTF8AndSize(arg, &size));
89+
if (!field_name) {
90+
return NULL;
91+
}

0 commit comments

Comments
 (0)