Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to link spacy & thinc's compiled libraries with C++ STL #1

Open
F1uctus opened this issue May 22, 2022 · 1 comment
Open

Unable to link spacy & thinc's compiled libraries with C++ STL #1

F1uctus opened this issue May 22, 2022 · 1 comment

Comments

@F1uctus
Copy link
Owner

F1uctus commented May 22, 2022

libc++_shared.so is not added to the NEEDED section of spacy & thinc's .so libraries.

$ readelf -dW cblas.so

Dynamic section at offset 0x221f0 contains 24 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libpython3.10.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
 0x000000000000001e (FLAGS)              BIND_NOW
 0x000000006ffffffb (FLAGS_1)            Flags: NOW
 0x0000000000000007 (RELA)               0x2918
 0x0000000000000008 (RELASZ)             11664 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffff9 (RELACOUNT)          429
 0x0000000000000017 (JMPREL)             0x56a8
 0x0000000000000002 (PLTRELSZ)           3456 (bytes)
 0x0000000000000003 (PLTGOT)             0x244a8
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000006 (SYMTAB)             0x308
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000005 (STRTAB)             0x184c
 0x000000000000000a (STRSZ)              4295 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x1800
 0x000000000000001a (FINI_ARRAY)         0x241e0
 0x000000000000001c (FINI_ARRAYSZ)       16 (bytes)
 0x000000006ffffff0 (VERSYM)             0x1628
 0x000000006ffffffe (VERNEED)            0x17c0
 0x000000006fffffff (VERNEEDNUM)         2
 0x0000000000000000 (NULL)               0x0

Because of this, I'm facing a runtime issue while loading spaCy.

File "/data/user/0/com.example/assets/python/scripts/test.py", line 4, in <module>
from spacy import Language
File "/p4a/build/python-installs/unnamed_dist_1/arm64-v8a/spacy/__init__.py", line 11, in <module>
File "/p4a/build/python-installs/unnamed_dist_1/arm64-v8a/thinc/api.py", line 2, in <module>
File "/p4a/build/python-installs/unnamed_dist_1/arm64-v8a/thinc/initializers.py", line 4, in <module>
File "/p4a/build/python-installs/unnamed_dist_1/arm64-v8a/thinc/backends/__init__.py", line 7, in <module>
File "/p4a/build/python-installs/unnamed_dist_1/arm64-v8a/thinc/backends/ops.py", line 15, in <module>
ImportError: dlopen failed: cannot locate symbol "_ZNKSt6__ndk119__shared_weak_count13__get_deleterERKSt9type_info" referenced by "/data/data/com.example/assets/python/site-packages/thinc/backends/cblas.so"...

At first I've tried to just extend these recipes from the CppCompiledComponentsPythonRecipe, since I expected it to reference the STL correctly.

https://github.com/kivy/python-for-android/blob/f7f8cea636714dc4843b8818c553510f99084b6a/pythonforandroid/recipe.py#L1042-L1045

class CppCompiledComponentsPythonRecipe(CompiledComponentsPythonRecipe):
    """ Extensions that require the cxx-stl """
    call_hostpython_via_targetpython = False
    need_stl_shared = True

But then thinc compilation failed with Something's broken. UCHAR_MAX should be defined in limits.h. and some other errors related to unexpected platform char size, etc. I bet it is occurred because of conflicting headers from both C stdlib and C++ STL.

So, then I've tried to override the CppCompiledComponentsPythonRecipe behaviour by removing the includes from compiler flags, but leaving linker flags as-is. Now it compiles fine, but libc++_shared.so is still not included in .so dependencies.

Right now I can fix that simply by patching all .so files in spacy & thinc folders under site-packages using

$ find . -type f -name '*.so' -exec patchelf --add-needed libc++_shared.so {} \;

And everything runs just fine.
But I need to figure out how to properly link libc++_shared.so in the corresponding p4a recipes:

class ThincRecipe(CompiledComponentsPythonRecipe):
version = "master"
url = (
"https://github.com/explosion/thinc/archive/90631684f8e4448fb5894cc8ab748a68939f2654.tar.gz"
if version == "master"
else "https://pypi.python.org/packages/source/t/thinc/thinc-{version}.tar.gz"
)
site_packages_name = "thinc"
depends = [
"setuptools",
"cython",
# Explosion-provided dependencies
"murmurhash",
"cymem",
"preshed",
"blis",
"srsly",
"wasabi",
"catalogue",
"ml_datasets",
# Third-party dependencies
"pydantic",
"numpy",
]
call_hostpython_via_targetpython = False
install_in_hostpython = True
def get_recipe_env(self, arch=None, with_flags_in_cc=True):
env = super().get_recipe_env(arch, with_flags_in_cc)
env["CXXFLAGS"] = env["CFLAGS"] + " -frtti -fexceptions"
if with_flags_in_cc:
env["CXX"] += " -frtti -fexceptions"
env["LDFLAGS"] += " -L{}".format(self.get_stl_lib_dir(arch))
env["LIBS"] = env.get("LIBS", "") + " -l{}".format(self.stl_lib_name)
return env
def postbuild_arch(self, arch):
super().postbuild_arch(arch)
self.install_stl_lib(arch)

class SpacyRecipe(CompiledComponentsPythonRecipe):
version = "3.3.0"
url = "https://pypi.python.org/packages/source/s/spacy/spacy-{version}.tar.gz"
site_packages_name = "spacy"
depends = [
"setuptools",
"cython",
# Explosion-provided dependencies
"spacy-legacy",
"spacy-loggers",
"cymem",
"preshed",
"thinc",
"blis",
"ml_datasets",
"murmurhash",
"wasabi",
"srsly",
"catalogue",
"typer",
"pathy",
# Third party dependencies
"numpy",
"requests",
"tqdm",
"pydantic",
"jinja2",
"langcodes",
# Official Python utilities
"packaging",
# Required for Russian language
"pymorphy2",
"pymorphy2_dicts_ru",
"DAWG-Python",
"appdirs",
"pyparsing",
]
call_hostpython_via_targetpython = False
def get_recipe_env(self, arch=None, with_flags_in_cc=True):
env = super().get_recipe_env(arch, with_flags_in_cc)
env["CXXFLAGS"] = env["CFLAGS"] + " -frtti -fexceptions"
if with_flags_in_cc:
env["CXX"] += " -frtti -fexceptions"
env["LDFLAGS"] += " -L{}".format(self.get_stl_lib_dir(arch))
env["LIBS"] = env.get("LIBS", "") + " -l{}".format(self.stl_lib_name)
return env
def postbuild_arch(self, arch):
super().postbuild_arch(arch)
self.install_stl_lib(arch)

@F1uctus
Copy link
Owner Author

F1uctus commented May 22, 2022

Any other suggestions on improving the quality of recipes are also appreciated in the separate issues! I hope to create a PR with these recipes in the main P4A repository in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant