Skip to content

Commit c5717ec

Browse files
committed
Add zlib recipe
To solve the build issues of python's zlib module Note: This library has been slightly modified in order to force the soname to include the library version and to be android's compatible (ends with `.so`, which android library mechanism needs to successfully load a shared library). This has been done this way to avoid linkage problems with android's system library `libz`
1 parent 5f05a70 commit c5717ec

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sh
2+
import glob
3+
from os.path import exists, join
4+
from pythonforandroid.toolchain import Recipe, shprint
5+
from pythonforandroid.util import current_directory
6+
from multiprocessing import cpu_count
7+
8+
9+
class ZlibRecipe(Recipe):
10+
version = '1.2.11'
11+
url = 'https://zlib.net/zlib-{version}.tar.gz'
12+
depends = []
13+
patches = ['lib-version.patch']
14+
15+
@property
16+
def version_link(self):
17+
return self.version.replace('.', '')
18+
19+
def should_build(self, arch):
20+
return not exists(
21+
join(self.ctx.get_libs_dir(arch.arch),
22+
'libz{}.so'.format(self.version_link)))
23+
24+
def build_arch(self, arch):
25+
super(ZlibRecipe, self).build_arch(arch)
26+
build_dir = self.get_build_dir(arch.arch)
27+
28+
with current_directory(build_dir):
29+
env = self.get_recipe_env(arch)
30+
31+
shprint(sh.cmake,
32+
'-DP4A_LINK_VERSION={}'.format(self.version.replace('.', '')),
33+
'-DANDROID_ABI={}'.format(arch.arch),
34+
'-DANDROID_NATIVE_API_LEVEL={}'.format(self.ctx.ndk_api),
35+
36+
'-DCMAKE_TOOLCHAIN_FILE={}'.format(
37+
join(self.ctx.ndk_dir, 'build', 'cmake',
38+
'android.toolchain.cmake')),
39+
_env=env)
40+
shprint(sh.make, '-j' + str(cpu_count()), _env=env)
41+
42+
# copy shared libs to libs collection
43+
so_libs = glob.glob(join(build_dir, '*.so'))
44+
self.install_libs(arch, *so_libs)
45+
46+
47+
recipe = ZlibRecipe()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--- zlib/CMakeLists.txt.orig 2017-01-15 18:29:40.000000000 +0100
2+
+++ zlib/CMakeLists.txt 2019-02-16 23:05:55.699949378 +0100
3+
@@ -186,7 +186,6 @@ endif(MINGW)
4+
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
5+
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
6+
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
7+
-set_target_properties(zlib PROPERTIES SOVERSION 1)
8+
9+
if(NOT CYGWIN)
10+
# This property causes shared libraries on Linux to have the full version
11+
@@ -196,12 +195,14 @@ if(NOT CYGWIN)
12+
#
13+
# This has no effect with MSVC, on that platform the version info for
14+
# the DLL comes from the resource file win32/zlib1.rc
15+
- set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
16+
endif()
17+
18+
if(UNIX)
19+
- # On unix-like platforms the library is almost always called libz
20+
- set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
21+
+ # On unix-like platforms the library is almost always called libz,
22+
+ # and in python-for-android we rename the library to avoid conflicts with
23+
+ # with android's system libs
24+
+ set(libzname "z${P4A_LINK_VERSION}")
25+
+ set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME ${libzname})
26+
if(NOT APPLE)
27+
set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
28+
endif()

0 commit comments

Comments
 (0)