Skip to content

Commit

Permalink
Fix libffi recipe, and build + runtime linker errors when compiling o…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
Aralox authored and AndreMiras committed Mar 20, 2019
1 parent 6c4fcd0 commit f16a665
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pythonforandroid/archs.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_env(self, with_flags_in_cc=True, clang=False):
self.ctx.python_recipe.version[0:3])
)

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

env["CXXFLAGS"] = env["CFLAGS"]

Expand Down
8 changes: 8 additions & 0 deletions pythonforandroid/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):

env['SYSROOT'] = sysroot

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

return env

def set_libs_flags(self, env, arch):
Expand Down
42 changes: 3 additions & 39 deletions pythonforandroid/recipes/libffi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from os.path import exists, join
from multiprocessing import cpu_count
from pythonforandroid.recipe import Recipe
from pythonforandroid.logger import info, shprint
from pythonforandroid.logger import shprint
from pythonforandroid.util import current_directory, ensure_dir
from glob import glob
import sh


Expand Down Expand Up @@ -37,46 +37,10 @@ def build_arch(self, arch):
'--prefix=' + self.get_build_dir(arch.arch),
'--disable-builddir',
'--enable-shared', _env=env)
# '--with-sysroot={}'.format(self.ctx.ndk_platform),
# '--target={}'.format(arch.toolchain_prefix),

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

try:
shprint(sh.make, '-j5', 'libffi.la', _env=env)
except sh.ErrorReturnCode_2:
info("make libffi.la failed as expected")
cc = sh.Command(env['CC'].split()[0])
cflags = env['CC'].split()[1:]
host_build = self.get_build_dir(arch.arch)

arch_flags = ''
if '-march=' in env['CFLAGS']:
arch_flags = '-march={}'.format(env['CFLAGS'].split('-march=')[1])

src_arch = arch.command_prefix.split('-')[0]
if src_arch == 'x86_64':
# libffi has not specific arch files for x86_64...so...using
# the ones from x86 which seems to build fine...
src_arch = 'x86'

cflags.extend(arch_flags.split())
cflags.extend(['-shared', '-fPIC', '-DPIC'])
cflags.extend(glob(join(host_build, 'src/.libs/*.o')))
cflags.extend(glob(join(host_build, 'src', src_arch, '.libs/*.o')))

ldflags = env['LDFLAGS'].split()
cflags.extend(ldflags)
cflags.extend(['-Wl,-soname', '-Wl,libffi.so', '-o',
'.libs/libffi.so'])

with current_directory(host_build):
shprint(cc, *cflags, _env=env)

ensure_dir(self.ctx.get_libs_dir(arch.arch))
shprint(sh.cp,
join(host_build, '.libs', 'libffi.so'),
Expand Down
4 changes: 4 additions & 0 deletions pythonforandroid/recipes/python3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sh
from pythonforandroid.python import GuestPythonRecipe
from pythonforandroid.recipe import Recipe

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

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

if sh.which('lld') is not None:
patches = patches + ["patches/remove-fix-cortex-a8.patch"]

depends = ['hostpython3', 'sqlite3', 'openssl', 'libffi']
conflicts = ['python3crystax', 'python2', 'python2legacy']

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

0 comments on commit f16a665

Please sign in to comment.