Skip to content

Commit f16a665

Browse files
AraloxAndreMiras
authored andcommitted
Fix libffi recipe, and build + runtime linker errors when compiling on WSL (#1744)
* Fix libffi linker errors when compiling on WSL Optional use of lld (LLVM's linker), and include a python37 bugfix. https://clang.llvm.org/docs/Toolchain.html https://lld.llvm.org/ https://bz-attachments.freebsd.org/attachment.cgi?id=200526 * Remove lld unsupported fix-cortex-a8 from LDFLAGS android/ndk#313 android/ndk#766 https://developer.android.com/ndk/guides/standalone_toolchain * Fix libffi recipe, runtime libffi linker errors. Libtool didnt like the space after --sysroot, replaced with = * CRAs - Check for lld before using it. Changed hardcoded cpu count to cpu_count(). Added comment to python patch. Made remove-fix-cortex-a8.patch conditional on lld. Moved LDFLAGS change to get_recipe_env() so its not libffi specific within python.py.
1 parent 6c4fcd0 commit f16a665

File tree

5 files changed

+30
-40
lines changed

5 files changed

+30
-40
lines changed

pythonforandroid/archs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_env(self, with_flags_in_cc=True, clang=False):
8686
self.ctx.python_recipe.version[0:3])
8787
)
8888

89-
env['LDFLAGS'] += '--sysroot {} '.format(self.ctx.ndk_platform)
89+
env['LDFLAGS'] += '--sysroot={} '.format(self.ctx.ndk_platform)
9090

9191
env["CXXFLAGS"] = env["CFLAGS"]
9292

pythonforandroid/python.py

+8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
170170

171171
env['SYSROOT'] = sysroot
172172

173+
if sh.which('lld') is not None:
174+
# Note: The -L. is to fix a bug in python 3.7.
175+
# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234409
176+
env["LDFLAGS"] += ' -L. -fuse-ld=lld'
177+
else:
178+
logger.warning('lld not found, linking without it. ' +
179+
'Consider installing lld if linker errors occur.')
180+
173181
return env
174182

175183
def set_libs_flags(self, env, arch):

pythonforandroid/recipes/libffi/__init__.py

+3-39
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from os.path import exists, join
2+
from multiprocessing import cpu_count
23
from pythonforandroid.recipe import Recipe
3-
from pythonforandroid.logger import info, shprint
4+
from pythonforandroid.logger import shprint
45
from pythonforandroid.util import current_directory, ensure_dir
5-
from glob import glob
66
import sh
77

88

@@ -37,46 +37,10 @@ def build_arch(self, arch):
3737
'--prefix=' + self.get_build_dir(arch.arch),
3838
'--disable-builddir',
3939
'--enable-shared', _env=env)
40-
# '--with-sysroot={}'.format(self.ctx.ndk_platform),
41-
# '--target={}'.format(arch.toolchain_prefix),
4240

43-
# ndk 15 introduces unified headers required --sysroot and
44-
# -isysroot for libraries and headers. libtool's head explodes
45-
# trying to weave them into it's own magic. The result is a link
46-
# failure trying to link libc. We call make to compile the bits
47-
# and manually link...
41+
shprint(sh.make, '-j', str(cpu_count()), 'libffi.la', _env=env)
4842

49-
try:
50-
shprint(sh.make, '-j5', 'libffi.la', _env=env)
51-
except sh.ErrorReturnCode_2:
52-
info("make libffi.la failed as expected")
53-
cc = sh.Command(env['CC'].split()[0])
54-
cflags = env['CC'].split()[1:]
5543
host_build = self.get_build_dir(arch.arch)
56-
57-
arch_flags = ''
58-
if '-march=' in env['CFLAGS']:
59-
arch_flags = '-march={}'.format(env['CFLAGS'].split('-march=')[1])
60-
61-
src_arch = arch.command_prefix.split('-')[0]
62-
if src_arch == 'x86_64':
63-
# libffi has not specific arch files for x86_64...so...using
64-
# the ones from x86 which seems to build fine...
65-
src_arch = 'x86'
66-
67-
cflags.extend(arch_flags.split())
68-
cflags.extend(['-shared', '-fPIC', '-DPIC'])
69-
cflags.extend(glob(join(host_build, 'src/.libs/*.o')))
70-
cflags.extend(glob(join(host_build, 'src', src_arch, '.libs/*.o')))
71-
72-
ldflags = env['LDFLAGS'].split()
73-
cflags.extend(ldflags)
74-
cflags.extend(['-Wl,-soname', '-Wl,libffi.so', '-o',
75-
'.libs/libffi.so'])
76-
77-
with current_directory(host_build):
78-
shprint(cc, *cflags, _env=env)
79-
8044
ensure_dir(self.ctx.get_libs_dir(arch.arch))
8145
shprint(sh.cp,
8246
join(host_build, '.libs', 'libffi.so'),

pythonforandroid/recipes/python3/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sh
12
from pythonforandroid.python import GuestPythonRecipe
23
from pythonforandroid.recipe import Recipe
34

@@ -23,6 +24,9 @@ class Python3Recipe(GuestPythonRecipe):
2324

2425
patches = ["patches/fix-ctypes-util-find-library.patch"]
2526

27+
if sh.which('lld') is not None:
28+
patches = patches + ["patches/remove-fix-cortex-a8.patch"]
29+
2630
depends = ['hostpython3', 'sqlite3', 'openssl', 'libffi']
2731
conflicts = ['python3crystax', 'python2', 'python2legacy']
2832

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
This patch removes --fix-cortex-a8 from the linker flags in order to support linking
2+
with lld, as lld does not support this flag (https://github.com/android-ndk/ndk/issues/766).
3+
diff --git a/configure b/configure
4+
--- a/configure
5+
+++ b/configure
6+
@@ -5671,7 +5671,7 @@ $as_echo_n "checking for the Android arm ABI... " >&6; }
7+
$as_echo "$_arm_arch" >&6; }
8+
if test "$_arm_arch" = 7; then
9+
BASECFLAGS="${BASECFLAGS} -mfloat-abi=softfp -mfpu=vfpv3-d16"
10+
- LDFLAGS="${LDFLAGS} -march=armv7-a -Wl,--fix-cortex-a8"
11+
+ LDFLAGS="${LDFLAGS} -march=armv7-a"
12+
fi
13+
else
14+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: not Android" >&5

0 commit comments

Comments
 (0)