Skip to content

Commit 31cad00

Browse files
authored
✨ Compression libraries - episode I: libbz2 (kivy#2095)
* ✨ Add `libbz2` recipe (libbz2.so) * ✅ Add tests for `libbz2` recipe
1 parent 21d4d35 commit 31cad00

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sh
2+
3+
from multiprocessing import cpu_count
4+
5+
from pythonforandroid.archs import Arch
6+
from pythonforandroid.logger import shprint
7+
from pythonforandroid.recipe import Recipe
8+
from pythonforandroid.util import current_directory
9+
10+
11+
class LibBz2Recipe(Recipe):
12+
13+
version = "1.0.8"
14+
url = "https://sourceware.org/pub/bzip2/bzip2-{version}.tar.gz"
15+
built_libraries = {"libbz2.so": ""}
16+
patches = ["lib_android.patch"]
17+
18+
def build_arch(self, arch: Arch) -> None:
19+
env = self.get_recipe_env(arch)
20+
with current_directory(self.get_build_dir(arch.arch)):
21+
shprint(
22+
sh.make,
23+
"-j",
24+
str(cpu_count()),
25+
f'CC={env["CC"]}',
26+
f'AR={env["AR"]}',
27+
f'RANLIB={env["RANLIB"]}',
28+
"-f",
29+
"Makefile-libbz2_so",
30+
_env=env,
31+
)
32+
33+
def get_library_includes(self, arch: Arch) -> str:
34+
"""
35+
Returns a string with the appropriate `-I<lib directory>` to link
36+
with the bz2 lib. This string is usually added to the environment
37+
variable `CPPFLAGS`.
38+
"""
39+
return " -I" + self.get_build_dir(arch.arch)
40+
41+
def get_library_ldflags(self, arch: Arch) -> str:
42+
"""
43+
Returns a string with the appropriate `-L<lib directory>` to link
44+
with the bz2 lib. This string is usually added to the environment
45+
variable `LDFLAGS`.
46+
"""
47+
return " -L" + self.get_build_dir(arch.arch)
48+
49+
@staticmethod
50+
def get_library_libs_flag() -> str:
51+
"""
52+
Returns a string with the appropriate `-l<lib>` flags to link with
53+
the bz2 lib. This string is usually added to the environment
54+
variable `LIBS`.
55+
"""
56+
return " -lbz2"
57+
58+
59+
recipe = LibBz2Recipe()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Set default compiler to `clang` and disable versioned shared library
2+
--- bzip2-1.0.8/Makefile-libbz2_so.orig 2019-07-13 19:50:05.000000000 +0200
3+
+++ bzip2-1.0.8/Makefile-libbz2_so 2020-03-13 20:10:32.336990786 +0100
4+
@@ -22,7 +22,7 @@
5+
6+
7+
SHELL=/bin/sh
8+
-CC=gcc
9+
+CC=clang
10+
BIGFILES=-D_FILE_OFFSET_BITS=64
11+
CFLAGS=-fpic -fPIC -Wall -Winline -O2 -g $(BIGFILES)
12+
13+
@@ -35,13 +35,11 @@ OBJS= blocksort.o \
14+
bzlib.o
15+
16+
all: $(OBJS)
17+
- $(CC) -shared -Wl,-soname -Wl,libbz2.so.1.0 -o libbz2.so.1.0.8 $(OBJS)
18+
- $(CC) $(CFLAGS) -o bzip2-shared bzip2.c libbz2.so.1.0.8
19+
- rm -f libbz2.so.1.0
20+
- ln -s libbz2.so.1.0.8 libbz2.so.1.0
21+
+ $(CC) -shared -Wl,-soname=libbz2.so -o libbz2.so $(OBJS)
22+
+ $(CC) $(CFLAGS) -o bzip2-shared bzip2.c libbz2.so
23+
24+
clean:
25+
- rm -f $(OBJS) bzip2.o libbz2.so.1.0.8 libbz2.so.1.0 bzip2-shared
26+
+ rm -f $(OBJS) bzip2.o libbz2.so bzip2-shared
27+
28+
blocksort.o: blocksort.c
29+
$(CC) $(CFLAGS) -c blocksort.c

tests/recipes/test_libbz2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
3+
from tests.recipes.recipe_lib_test import BaseTestForMakeRecipe
4+
5+
6+
class TestLibBz2Recipe(BaseTestForMakeRecipe, unittest.TestCase):
7+
"""TestCase for recipe :mod:`~pythonforandroid.recipes.libbz2`."""
8+
recipe_name = "libbz2"
9+
sh_command_calls = []
10+
11+
def test_get_library_includes(self):
12+
"""
13+
Test :meth:`~pythonforandroid.recipes.libbz2.get_library_includes`.
14+
"""
15+
self.assertEqual(
16+
self.recipe.get_library_includes(self.arch),
17+
f" -I{self.recipe.get_build_dir(self.arch.arch)}",
18+
)
19+
20+
def test_get_library_ldflags(self):
21+
"""
22+
Test :meth:`~pythonforandroid.recipes.libbz2.get_library_ldflags`.
23+
"""
24+
self.assertEqual(
25+
self.recipe.get_library_ldflags(self.arch),
26+
f" -L{self.recipe.get_build_dir(self.arch.arch)}",
27+
)
28+
29+
def test_link_libs_flags(self):
30+
"""
31+
Test :meth:`~pythonforandroid.recipes.libbz2.get_library_ldflags`.
32+
"""
33+
self.assertEqual(self.recipe.get_library_libs_flag(), " -lbz2")

0 commit comments

Comments
 (0)