Skip to content

Commit 903207d

Browse files
committed
Enable openssl support for python recipes
This commit grants openssl support for both python versions (2 and 3). The python2 recipe needs some extra work to make it link with python, we need to patch the setup.py file in order to that the python's build system recognises the libraries. This is not the case for the python3 recipe which works like a charm, except for the fact that we have to modify our static class variable ` config_args ` for the python's recipe, i found this to be necessary in my tests, otherwise the openssl libs aren't detected.
1 parent f3be32b commit 903207d

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

pythonforandroid/python.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class GuestPythonRecipe(TargetPythonRecipe):
162162
'''The file extensions from site packages dir that we don't want to be
163163
included in our python bundle.'''
164164

165-
opt_depends = ['sqlite3', 'libffi']
165+
opt_depends = ['sqlite3', 'libffi', 'openssl']
166166
'''The optional libraries which we would like to get our python linked'''
167167

168168
def __init__(self, *args, **kwargs):
@@ -190,6 +190,10 @@ def add_flags(include_flags, link_flags):
190190
' -L' + join(recipe.get_build_dir(arch.arch),
191191
recipe.get_host(arch), '.libs') + ' -lffi')
192192

193+
if 'openssl' in self.ctx.recipe_build_order:
194+
info('Activating flags for openssl')
195+
recipe = Recipe.get_recipe('openssl', self.ctx)
196+
add_flags(recipe.include_flags(arch), recipe.link_flags(arch))
193197
return env
194198

195199
def get_recipe_env(self, arch=None, with_flags_in_cc=True):
@@ -230,12 +234,12 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
230234

231235
ndk_flags = (
232236
'--sysroot={ndk_sysroot} -D__ANDROID_API__={android_api} '
233-
'-isystem {ndk_android_host}').format(
237+
'-isystem {ndk_android_host} -I{ndk_include}').format(
234238
ndk_sysroot=join(self.ctx.ndk_dir, 'sysroot'),
235239
android_api=self.ctx.ndk_api,
236240
ndk_android_host=join(
237-
self.ctx.ndk_dir, 'sysroot', 'usr', 'include',
238-
android_host))
241+
self.ctx.ndk_dir, 'sysroot', 'usr', 'include', android_host),
242+
ndk_include=join(self.ctx.ndk_dir, 'sysroot', 'usr', 'include'))
239243
sysroot = join(self.ctx.ndk_dir, 'platforms',
240244
platform_name, 'arch-arm')
241245
env['CFLAGS'] = env.get('CFLAGS', '') + ' ' + ndk_flags

pythonforandroid/recipes/python2/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from os.path import join, exists
2+
from pythonforandroid.recipe import Recipe
13
from pythonforandroid.python import GuestPythonRecipe
4+
from pythonforandroid.logger import shprint
5+
import sh
26

37

48
class Python2Recipe(GuestPythonRecipe):
@@ -43,5 +47,21 @@ class Python2Recipe(GuestPythonRecipe):
4347
'--prefix={prefix}',
4448
'--exec-prefix={exec_prefix}')
4549

50+
def prebuild_arch(self, arch):
51+
super(Python2Recipe, self).prebuild_arch(arch)
52+
patch_mark = join(self.get_build_dir(arch.arch), '.openssl-patched')
53+
if 'openssl' in self.ctx.recipe_build_order and not exists(patch_mark):
54+
self.apply_patch(join('patches', 'enable-openssl.patch'), arch.arch)
55+
shprint(sh.touch, patch_mark)
56+
57+
def set_libs_flags(self, env, arch):
58+
env = super(Python2Recipe, self).set_libs_flags(env, arch)
59+
if 'openssl' in self.ctx.recipe_build_order:
60+
recipe = Recipe.get_recipe('openssl', self.ctx)
61+
openssl_build = recipe.get_build_dir(arch.arch)
62+
env['OPENSSL_BUILD'] = openssl_build
63+
env['OPENSSL_VERSION'] = recipe.version
64+
return env
65+
4666

4767
recipe = Python2Recipe()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--- Python-2.7.15.orig/setup.py 2018-04-30 00:47:33.000000000 +0200
2+
+++ Python-2.7.15/setup.py 2018-07-05 11:08:57.305125432 +0200
3+
@@ -812,18 +840,15 @@ class PyBuildExt(build_ext):
4+
'/usr/local/ssl/include',
5+
'/usr/contrib/ssl/include/'
6+
]
7+
- ssl_incs = find_file('openssl/ssl.h', inc_dirs,
8+
- search_for_ssl_incs_in
9+
- )
10+
+ ssl_incs = [
11+
+ os.path.join(os.environ["OPENSSL_BUILD"], 'include'),
12+
+ os.path.join(os.environ["OPENSSL_BUILD"], 'include', 'openssl')]
13+
if ssl_incs is not None:
14+
krb5_h = find_file('krb5.h', inc_dirs,
15+
['/usr/kerberos/include'])
16+
if krb5_h:
17+
ssl_incs += krb5_h
18+
- ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
19+
- ['/usr/local/ssl/lib',
20+
- '/usr/contrib/ssl/lib/'
21+
- ] )
22+
+ ssl_libs = [os.environ["OPENSSL_BUILD"]]
23+
24+
if (ssl_incs is not None and
25+
ssl_libs is not None):
26+
@@ -841,8 +866,8 @@ class PyBuildExt(build_ext):
27+
'^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
28+
29+
# look for the openssl version header on the compiler search path.
30+
- opensslv_h = find_file('openssl/opensslv.h', [],
31+
- inc_dirs + search_for_ssl_incs_in)
32+
+ opensslv_h = [os.path.join(os.environ["OPENSSL_BUILD"], 'include'),
33+
+ os.path.join(os.environ["OPENSSL_BUILD"], 'include', 'openssl')]
34+
if opensslv_h:
35+
name = os.path.join(opensslv_h[0], 'openssl/opensslv.h')
36+
if host_platform == 'darwin' and is_macosx_sdk_path(name):
37+
@@ -859,8 +884,7 @@ class PyBuildExt(build_ext):
38+
39+
min_openssl_ver = 0x00907000
40+
have_any_openssl = ssl_incs is not None and ssl_libs is not None
41+
- have_usable_openssl = (have_any_openssl and
42+
- openssl_ver >= min_openssl_ver)
43+
+ have_usable_openssl = (have_any_openssl and True)
44+
45+
if have_any_openssl:
46+
if have_usable_openssl:

pythonforandroid/recipes/python3/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pythonforandroid.python import GuestPythonRecipe
2+
from pythonforandroid.recipe import Recipe
23

34

45
class Python3Recipe(GuestPythonRecipe):
@@ -38,5 +39,13 @@ class Python3Recipe(GuestPythonRecipe):
3839
'--prefix={prefix}',
3940
'--exec-prefix={exec_prefix}')
4041

42+
def set_libs_flags(self, env, arch):
43+
env = super(Python3Recipe, self).set_libs_flags(env, arch)
44+
if 'openssl' in self.ctx.recipe_build_order:
45+
recipe = Recipe.get_recipe('openssl', self.ctx)
46+
self.configure_args += \
47+
('--with-openssl=' + recipe.get_build_dir(arch.arch),)
48+
return env
49+
4150

4251
recipe = Python3Recipe()

0 commit comments

Comments
 (0)