8000 Fix building ssl for python3 · ArchieMeng/python-for-android@a2fb5ec · GitHub
[go: up one dir, main page]

Skip to content

Commit a2fb5ec

Browse files
author
agilewalker
committed
Fix building ssl for python3
1 parent 8829312 commit a2fb5ec

File tree

3 files changed

+318
-48
lines changed

3 files changed

+318
-48
lines changed

pythonforandroid/recipes/openssl/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def build_arch(self, arch):
6060
break
6161
shprint(sh.make, 'clean', _env=env)
6262

63-
self.install_libs(arch, 'libssl' + self.version + '.so',
64-
'libcrypto' + self.version + '.so')
63+
self.install_libs(arch, 'libssl.a', 'libssl' + self.version + '.so',
64+
'libcrypto.a', 'libcrypto' + self.version + '.so')
6565

6666
recipe = OpenSSLRecipe()
Lines changed: 227 additions & 46 deletions
1E0A
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,257 @@
1-
21
from pythonforandroid.recipe import TargetPythonRecipe
32
from pythonforandroid.toolchain import shprint, current_directory, ArchARM
43
from pythonforandroid.logger import info, error
54
from pythonforandroid.util import ensure_dir, temp_directory
65
from os.path import exists, join
6+
import os
77
import glob
88
import sh
9+
from sh import Command
10+
11+
# This is the content of opensslconf.h taken from
12+
# ndkdir/build/tools/build-target-openssl.sh
13+
OPENSSLCONF = """#if defined(__ARM_ARCH_5TE__)
14+
#include "opensslconf_armeabi.h"
15+
#elif defined(__ARM_ARCH_7A__) && !defined(__ARM_PCS_VFP)
16+
#include "opensslconf_armeabi_v7a.h"
17+
#elif defined(__ARM_ARCH_7A__) && defined(__ARM_PCS_VFP)
18+
#include "opensslconf_armeabi_v7a_hard.h"
19+
#elif defined(__aarch64__)
20+
#include "opensslconf_arm64_v8a.h"
21+
#elif defined(__i386__)
22+
#include "opensslconf_x86.h"
23+
#elif defined(__x86_64__)
24+
#include "opensslconf_x86_64.h"
25+
#elif defined(__mips__) && !defined(__mips64)
26+
#include "opensslconf_mips.h"
27+
#elif defined(__mips__) && defined(__mips64)
28+
#include "opensslconf_mips64.h"
29+
#else
30+
#error "Unsupported ABI"
31+
#endif
32+
"""
33+
LATEST_FULL_VERSION = {
34+
'3.5': '3.5.1',
35+
'3.6': '3.6.4'
36+
}
37+
38+
def realpath(fname):
39+
"""
40+
Own implementation of os.realpath which may be broken in some python versions
41+
Returns: the absolute path o
42+
43+
"""
44+
45+
if not os.path.islink(fname):
46+
return os.path.abspath(fname)
947

10-
prebuilt_download_locations = {
11-
'3.6': ('https://github.com/inclement/crystax_python_builds/'
12-
'releases/download/0.1/crystax_python_3.6_armeabi_armeabi-v7a.tar.gz')}
48+
abs_path = os.path.abspath(fname).split(os.sep)[:-1]
49+
rel_path = os.readlink(fname)
50+
51+
if os.path.abspath(rel_path) == rel_path:
52+
return rel_path
53+
54+
rel_path = rel_path.split(os.sep)
55+
for folder in rel_path:
56+
if folder == '..':
57+
abs_path.pop()
58+
else:
59+
abs_path.append(folder)
60+
return os.sep.join(abs_path)
1361

1462
class Python3Recipe(TargetPythonRecipe):
1563
version = '3.5'
1664
url = ''
1765
name = 'python3crystax'
1866

19-
depends = ['hostpython3crystax']
67+
depends = ['hostpython3crystax']
2068
conflicts = ['python2', 'python3']
2169

2270
from_crystax = True
2371

72+
def download_if_necessary(self):
73+
if 'openssl' in self.ctx.recipe_build_order or self.version == '3.6':
74+
full_version = LATEST_FULL_VERSION[self.version]
75+
Python3Recipe.url = 'https://www.python.org/ftp/python/{0}.{1}.{2}/Python-{0}.{1}.{2}.tgz'.format(*full_version.split('.'))
76+
super(Python3Recipe, self).download_if_necessary()
77+
2478
def get_dir_name(self):
2579
name = super(Python3Recipe, self).get_dir_name()
2680
name += '-version{}'.format(self.version)
2781
return name
2882

83+
def copy_include_dir(self, source, target):
84+
ensure_dir(target)
85+
for fname in os.listdir(source):
86+
sh.ln('-sf', realpath(join(source, fname)), join(target, fname))
87+
88+
def _patch_dev_defaults(self, fp, target_ver):
89+
for line in fp:
90+
if 'OPENSSL_VERSIONS=' in line:
91+
versions = line.split('"')[1].split(' ')
92+
if versions[0] == target_ver:
93+
raise ValueError('Patch not needed')
94+
95+
if target_ver in versions:
96+
versions.remove(target_ver)
97+
98+
versions.insert(0, target_ver)
99+
100+
yield 'OPENSSL_VERSIONS="{}"\n'.format(' '.join(versions))
101+
else:
102+
yield line
103+
104+
def patch_dev_defaults(self, ssl_recipe):
105+
def_fname = join(self.ctx.ndk_dir, 'build', 'tools', 'dev-defaults.sh')
106+
try:
107+
with open(def_fname, 'r') as fp:
108+
s = ''.join(self._patch_dev_defaults(fp,
109+
str(ssl_recipe.version)))
110+
with open(def_fname, 'w') as fp:
111+
fp.write(s)
112+
113+
except ValueError:
114+
pass
115+
116+
def check_for_sslso(self, ssl_recipe, arch):
117+
# type: (Recipe, str)
118+
dynlib_dir = join(self.ctx.ndk_dir, 'sources', 'python', self.version,
119+
'libs', arch.arch, 'modules')
120+
121+
if os.path.exists(join(dynlib_dir, '_ssl.so')):
122+
return 10, 'Shared object exists in ndk'
123+
124+
# find out why _ssl.so is missing
125+
126+
source_dir = join 10000 (self.ctx.ndk_dir, 'sources', 'openssl', ssl_recipe.version)
127+
if not os.path.exists(source_dir):
128+
return 0, 'Openssl version not present'
129+
130+
# these two path checks are lifted straight from:
131+
# crystax-ndk/build/tools/build-target-python.sh
132+
if not os.path.exists(join(source_dir, 'Android.mk')):
133+
return 1.1, 'Android.mk is missing in openssl source'
134+
135+
include_dir = join(source_dir, 'include','openssl')
136+
if not os.path.exists(join(include_dir, 'opensslconf.h')):
137+
return 1.2, 'Openssl include dir missing'
138+
139+
under_scored_arch = arch.arch.replace('-', '_')
140+
if not os.path.lexists(join(include_dir,
141+
'opensslconf_{}.h'.format(under_scored_arch))):
142+
return 1.3, 'Opensslconf arch header missing from include'
143+
144+
145+
146+
# lastly a check to see whether shared objects for the correct arch
147+
# is present in the ndk
148+
if not os.path.exists(join(source_dir, 'libs', arch.arch)):
149+
return 2, 'Openssl libs for this arch is missing in ndk'
150+
151+
return 5, 'Ready to recompile python'
152+
153+
def find_Android_mk(self):
154+
openssl_dir = join(self.ctx.ndk_dir, 'sources', 'openssl')
155+
for version in os.listdir(openssl_dir):
156+
mk_path = join(openssl_dir, version, 'Android.mk')
157+
if os.path.exists(mk_path):
158+
return mk_path
159+
160+
def prebuild_arch(self, arch):
161+
super(Python3Recipe, self).prebuild_arch(arch)
162+
if self.version == '3.6':
163+
Python3Recipe.patches = ['patch_python3.6.patch']
164+
build_dir = self.get_build_dir(arch.arch)
165+
shprint(sh.ln, '-sf',
166+
realpath(join(build_dir, 'Lib/site-packages/README.txt')),
167+
join(build_dir, 'Lib/site-packages/README'))
168+
python_build_files = ['android.mk', 'config.c', 'interpreter.c']
169+
n 10000 dk_build_tools_python_dir = join(self.ctx.ndk_dir, 'build', 'tools', 'build-target-python')
170+
for python_build_file in python_build_files:
171+
shprint(sh.cp, join(ndk_build_tools_python_dir, python_build_file+'.3.5'),
172+
join(ndk_build_tools_python_dir, python_build_file+'.3.6'))
173+
ndk_sources_python_dir = join(self.ctx.ndk_dir, 'sources', 'python')
174+
if not os.path.exists(join(ndk_sources_python_dir, '3.6')):
175+
os.mkdir(join(ndk_sources_python_dir, '3.6'))
176+
sh.sed('s#3.5#3.6#',
177+
join(ndk_sources_python_dir, '3.5/Android.mk'),
178+
_out=join(ndk_sources_python_dir, '3.6/Android.mk'))
179+
29180
def build_arch(self, arch):
30-
# We don't have to actually build anything as CrystaX comes
31-
# with the necessary modules. They are included by modifying
32-
# the Android.mk in the jni folder.
33-
34-
# If the Python version to be used is not prebuilt with the CrystaX
35-
# NDK, we do have to download it.
36-
37-
crystax_python_dir = join(self.ctx.ndk_dir, 'sources', 'python')
38-
if not exists(join(crystax_python_dir, self.version)):
39-
info(('The NDK does not have a prebuilt Python {}, trying '
40-
'to obtain one.').format(self.version))
41-
42-
if self.version not in prebuilt_download_locations:
43-
error(('No prebuilt version for Python {} could be found, '
44-
'the built cannot continue.'))
45-
exit(1)
46-
47-
with temp_directory() as td:
48-
self.download_file(prebuilt_download_locations[self.version],
49-
join(td, 'downloaded_python'))
50-
shprint(sh.tar, 'xf', join(td, 'downloaded_python'),
51-
'--directory', crystax_python_dir)
52-
53-
if not exists(join(crystax_python_dir, self.version)):
54-
error(('Something went wrong, the directory at {} should '
55-
'have been created but does not exist.').format(
56-
join(crystax_python_dir, self.version)))
57-
58-
if not exists(join(
59-
crystax_python_dir, self.version, 'libs', arch.arch)):
60-
error(('The prebuilt Python for version {} does not contain '
61-
'binaries for your chosen architecture "{}".').format(
62-
self.version, arch.arch))
63-
exit(1)
64-
65-
# TODO: We should have an option to build a new Python. This
66-
# would also allow linking to openssl and sqlite from CrystaX.
181+
# If openssl is needed we may have to recompile cPython to get the
182+
# ssl.py module working properly
183+
if self.from_crystax and 'openssl' in self.ctx.recipe_build_order:
184+
info('Openssl and crystax-python combination may require '
185+
'recompilation of python...')
186+
ssl_recipe = self.get_recipe('openssl', self.ctx)
187+
stage, msg = self.check_for_sslso(ssl_recipe, arch)
188+
stage = 0 if stage < 5 else stage
189+
info(msg)
190+
openssl_build_dir = ssl_recipe.get_build_dir(arch.arch)
191+
openssl_ndk_dir = join(self.ctx.ndk_dir, 'sources', 'openssl',
192+
ssl_recipe.version)
193+
194+
if stage < 2:
195+
info('Copying openssl headers and Android.mk to ndk')
196+
ensure_dir(openssl_ndk_dir)
197+
if stage < 1.2:
198+
# copy include folder and Android.mk to ndk
199+
mk_path = self.find_Android_mk()
200+
if mk_path is None:
201+
raise IOError('Android.mk file could not be found in '
202+
'any versions in ndk->sources->openssl')
203+
shprint(sh.cp, mk_path, openssl_ndk_dir)
204+
205+
include_dir = join(openssl_build_dir, 'include')
206+
if stage < 1.3:
207+
ndk_include_dir = join(openssl_ndk_dir, 'include', 'openssl')
208+
self.copy_include_dir(join(include_dir, 'openssl'), ndk_include_dir)
209+
210+
target_conf = join(openssl_ndk_dir, 'include', 'openssl',
211+
'opensslconf.h')
212+
shprint(sh.rm, '-f', target_conf)
213+
# overwrite opensslconf.h
214+
with open(target_conf, 'w') as fp:
215+
fp.write(OPENSSLCONF)
216+
217+
if stage < 1.4:
218+
# move current conf to arch specific conf in ndk
219+
under_scored_arch = arch.arch.replace('-', '_')
220+
shprint(sh.ln, '-sf',
221+
realpath(join(include_dir, 'openssl', 'opensslconf.h')),
222+
join(openssl_ndk_dir, 'include', 'openssl',
223+
'opensslconf_{}.h'.format(under_scored_arch))
224+
)
67225

226+
if stage < 3:
227+
info('Copying openssl libs to ndk')
228+
arch_ndk_lib = join(openssl_ndk_dir, 'libs', arch.arch)
229+
ensure_dir(arch_ndk_lib)
230+
shprint(sh.ln, '-sf',
231+
realpath(join(openssl_build_dir, 'libcrypto{}.so'.format(ssl_recipe.version))),
232+
join(openssl_build_dir, 'libcrypto.so'))
233+
shprint(sh.ln, '-sf',
234+
realpath(join(openssl_build_dir, 'libssl{}.so'.format(ssl_recipe.version))),
235+
join(openssl_build_dir, 'libssl.so'))
236+
libs = ['libcrypto.a', 'libcrypto.so', 'libssl.a', 'libssl.so']
237+
cmd = [join(openssl_build_dir, lib) for lib in libs] + [arch_ndk_lib]
238+
shprint(sh.cp, '-f', *cmd)
239+
240+
if stage < 10:
241+
info('Recompiling python-crystax')
242+
self.patch_dev_defaults(ssl_recipe)
243+
build_script = join(self.ctx.ndk_dir, 'build', 'tools',
244+
'build-target-python.sh')
245+
246+
shprint(Command(build_script),
247+
'--ndk-dir={}'.format(self.ctx.ndk_dir),
248+
'--abis={}'.format(arch.arch),
249+
'-j5', '--verbose',
250+
self.get_build_dir(arch.arch))
251+
252+
info('Extracting CrystaX python3 from NDK package')
68253
dirn = self.ctx.get_python_install_dir()
69254
ensure_dir(dirn)
70-
71-
# Instead of using a locally built hostpython, we use the
72-
# user's Python for now. They must have the right version
73-
# available. Using e.g. pyenv makes this easy.
74255
self.ctx.hostpython = 'python{}'.format(self.version)
75256

76257
recipe = Python3Recipe()

0 commit comments

Comments
 (0)
0