Writing a recipe for bzip2

15 views
Skip to first unread message

Нестор Янчук

unread,
Jul 15, 2019, 10:05:56 AM7/15/19
to Kivy users support
Hello. I am trying to write a recipe to compile python with bzip support.
Currently, i have this as a recipe for bzip:

from pythonforandroid.toolchain import Recipe, current_directory, shprint
from os.path import join, realpath
import sh

from pythonforandroid.recipe import CompiledComponentsPythonRecipe


class Bz2Recipe(CompiledComponentsPythonRecipe):
    version = '1.0.6'
    md5sum = '00B516F4704D4A7CB50A1D97E6E8E15B'.lower()

    # call_hostpython_via_targetpython = True

    install_in_hostpython = True
    depends = []

    def should_build(self, arch):
        return True

    def build_arch(self, arch):
        with current_directory(self.get_build_dir(arch.arch)):
            env = arch.get_env()

            mkfile = open('Makefile-libbz2_so', 'r').read()
            with open('Makefile', 'w') as fp:
                mkfile = mkfile.replace(
                    "$(CC) -shared -Wl,-soname -Wl,libbz2.so.1.0 -o libbz2.so.{v} $(OBJS)".format(v=self.version),
                    "$(CC) -fPIC -fpic -shared -Wl,-soname -Wl,libbz2.so -o libbz2.so $(OBJS)",
                ).replace(
                    "PREFIX=/usr/local",
                    "PREFIX=" + realpath('.')
                )

                fp.write(mkfile)

            shprint(sh.make, _env=env)

            self.install_libs(arch, 'libbz2.so')

    def include_flags(self, arch):
        bz2_includes = join(self.get_build_dir(arch.arch), 'include')

        return (' -I' + bz2_includes)

    def link_dirs_flags(self, arch):

        return ' -L' + self.get_build_dir(arch.arch) + ' -L' + self.get_build_dir(arch.arch) + '/lib'

    def link_libs_flags(self):
        return ' -lbz2'

    def get_recipe_env(self, arch):
        env = super(Bz2Recipe, self).get_recipe_env(arch)
        env['NDK_PROJECT_PATH'] = self.get_build_dir(arch.arch)
        return env


recipe = Bz2Recipe()



And I have modified recipe for python3, so now it looks like this:

import sh
from pythonforandroid.python import GuestPythonRecipe
from pythonforandroid.recipe import Recipe
from pythonforandroid.logger import info


class Python3Recipe(GuestPythonRecipe):
    '''
    The python3's recipe.

    .. note:: This recipe can be built only against API 21+. Also, in order to
        build certain python modules, we need to add some extra recipes to our
        build requirements:

            - ctypes: you must add the recipe for ``libffi``.

    .. versionchanged:: 0.6.0
        Refactored into class
        :class:`~pythonforandroid.python.GuestPythonRecipe`
    '''

    version = '3.7.1'
    name = 'python3'

    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', 'bz2']
    conflicts = ['python3crystax', 'python2']

    configure_args = (
        '--host={android_host}',
        '--build={android_build}',
        '--enable-shared',
        '--enable-ipv6',
        'ac_cv_file__dev_ptmx=yes',
        'ac_cv_file__dev_ptc=no',
        '--without-ensurepip',
        'ac_cv_little_endian_double=yes',
        '--prefix={prefix}',
        '--exec-prefix={exec_prefix}')

    def should_build(self, arch):
        return True

    def set_libs_flags(self, env, arch):
        env = super(Python3Recipe, self).set_libs_flags(env, arch)

        def add_flags(include_flags, link_dirs, link_libs):
            env['CPPFLAGS'] = env.get('CPPFLAGS', '') + include_flags
            env['LDFLAGS'] = env.get('LDFLAGS', '') + link_dirs
            env['LIBS'] = env.get('LIBS', '') + link_libs

        if 'openssl' in self.ctx.recipe_build_order:
            recipe = Recipe.get_recipe('openssl', self.ctx)
            self.configure_args += \
                ('--with-openssl=' + recipe.get_build_dir(arch.arch),)

        if 'bz2' in self.ctx.recipe_build_order:
            info('Activating flags for bz2')
            recipe = Recipe.get_recipe('bz2', self.ctx)

            add_flags(recipe.include_flags(arch),
                      recipe.link_dirs_flags(arch),
                      recipe.link_libs_flags()
                      )

        return env


recipe = Python3Recipe()

But still i`m getting this error on my android device:

ModuleNotFoundError: No module named '_bz2'

For now I'm stuck and don't know what to do, because I do not have much experience with c/c++ and compiling stuff. Can you help me finish this?
Reply all
Reply to author
Forward
0 new messages