-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild_zlib
executable file
·239 lines (217 loc) · 7.05 KB
/
build_zlib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash
#
# Cross-compiles zlib library for Android
#
# This script download zlib source code from zlib official website (https://www.zlib.net/)
#
# This script cross-compiles zlib library for Android (armeabi-v7a, arm64-v8a, x86, x86-64)
# using Android NDK and autoconf, as shown in [Use the NDK with other build systems]
# (https://developer.android.com/ndk/guides/other_build_systems#autoconf).
#
# Dependencies:
# - Android NDK r22+
# - curl
# - make
#
# Android Arch Supported:
# - arm (armeabi-v7a)
# - arm64 (arm64-v8a)
# - x86
# - x86-64
# Constants
readonly RED='\033[0;31m'
readonly NC='\033[0m'
readonly ZLIB_BASE_URL='https://www.zlib.net/fossils'
readonly OUTPUT_DIR='output'
readonly DOWNLOADS_DIR='downloads'
readonly LOG_FILE='build.log'
print_usage() {
echo 'usage:'
echo ' build_zlib [-h | --help] (Print this help)'
echo ' [-c | --clean] (Clean output and download directories)'
echo ' [--ndk-dir ANDROID_NDK_DIR] (Default: $ANDROID_NDK)'
echo ' [--android-abi ANDROID_ABI] (Default: all, Values: all | arm | arm64 | x86 | x86-64)'
echo ' [--android-api-level API_LEVEL] (Default: 21)'
echo ' [--zlib-version ZLIB_VERSION] (Default: 1.2.11)'
echo ' [--build-type BUILD_TYPE] (Default: Build Debug and Realease, Values: all | debug | release)'
echo ' [-s | --static] (Default: Build static and shared libs)'
echo
echo 'exmples:'
echo ' # Build zlib-1.2.11 static and shared libs, for android-21, all supported architectures'
echo ' build_zlib'
echo
echo ' # Build zlib-1.1.10 static and shared libs, for android-21, armeabi-v7a'
echo ' build_zlib --ndk-dir /path/to/android-ndk --android-abi arm --zlib-version 1.1.10'
echo
echo ' # Build zlib-1.2.11 static libs, for android-24, arm64-v8a'
echo ' build_zlib --android-abi arm64 --android-api-level 24 --static'
}
err() {
echo -e "${RED}$*${NC}" >&2
}
cleanup() {
echo "Cleaning ..."
rm -rf $OUTPUT_DIR $LOG_FILE
rm -rf `find . -type d -name zlib-*`
echo "Everything is clean."
}
check_error() {
if [[ $? -ne 0 ]]; then
err $@
exit 1
fi
}
############################################
# Download zlib source code
# Globals:
# ZLIB_BASE_URL
# DOWNLOADS_DIR
# Arguments:
# Zlib version to download (i.e., 1.2.11)
############################################
download_zlib() {
local zlib=zlib-$1.tar.gz
if [[ ! -d $DOWNLOADS_DIR || ! -f "$DOWNLOADS_DIR/$zlib" ]]; then
echo "Downloading $zlib ..."
mkdir -p $DOWNLOADS_DIR && cd $DOWNLOADS_DIR
curl -sfSL $ZLIB_BASE_URL/$zlib -o $zlib 2>>"../$LOG_FILE"
check_error "Failed to download $zlib. See '$LOG_FILE' for details."
cd ..
fi
}
############################################
# Extract zlib source code
# Globals:
# DOWNLOADS_DIR
# Arguments:
# Zlib version to extract (i.e., 1.2.11)
############################################
extract_zlib() {
local zlib=zlib-$1.tar.gz
rm -rf $zlib
echo "Extracting $zlib ..."
tar -xzf "$DOWNLOADS_DIR/$zlib" 2>>$LOG_FILE
check_error "Failed to extract $zlib. See '$LOG_FILE' for details."
}
############################################
# Export cross build environment variables
# Arguments:
# Android NDK path
# Android ABI (i.e., arm, arm64, x86, x86-64)
# Android API Level (i.e., 21)
############################################
setup_env() {
local uname=`(uname -s || echo unknown) 2>>$LOG_FILE`
local ndk_toolchain
local ndk_target
case "$uname" in
Linux* | linux* | GNU | GNU/* | solaris*) ndk_toolchain=linux-x86_64 ;;
Darwin* | darwin*) ndk_toolchain=darwin-x86_64 ;;
unknown) err 'Failed to get device OS name.'; exit 1 ;;
*) err "OS \'$uname\' is not supported."; exit 1 ;;
esac
case "$2" in
arm) ndk_target=armv7a-linux-androideabi ;;
arm64) ndk_target=aarch64-linux-android ;;
x86) ndk_target=i686-linux-android ;;
x86-64) ndk_target=x86_64-linux-android ;;
*) err "ABI '$2' is not supported."; exit 1 ;;
esac
export NDK=$1
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$ndk_toolchain
export TARGET=$ndk_target
export API=$3
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip
}
############################################
# Build zlib
# Globals:
# OUTPUT_DIR
# Arguments:
# Zlib version to build
# Android ABI (i.e., arm, arm64, x86, x86-64)
# Build type (i.e., all, debug, release)
# Build arguments (i.e., --static)
############################################
build_zlib() {
local src_dir=zlib-$1
local out_dir
local abi
local build_args=$4
case "$2" in
arm) abi='armeabi-v7a' ;;
arm64) abi='arm64-v8a' ;;
x86) abi='x86' ;;
x86-64) abi='x86-64' ;;
*) err "ABI \'$2\' is not supported."; exit 1 ;;
esac
if [[ "$3" == 'Debug' ]]; then
build_args+=' --debug'
fi
out_dir="$OUTPUT_DIR/$src_dir/$3/$abi"
cd $src_dir
echo "Building $src_dir for $abi ($3) ..."
./configure --prefix="../$out_dir" $build_args 1>/dev/null 2>>"../$LOG_FILE"
check_error "Error building $src_dir. See '$LOG_FILE' for details."
make -s 1>/dev/null 2>>"../$LOG_FILE"
check_error "Error building $src_dir. See '$LOG_FILE' for details."
make -s install 1>/dev/null 2>>"../$LOG_FILE"
check_error "Error building $src_dir. See '$LOG_FILE' for details."
cd ..
rm -rf $src_dir
echo "Done."
}
############################################
# Entry point
# Arguments:
# Program arguments (See usage)
############################################
main() {
local android_ndk=$ANDROID_NDK
local android_abi='all'
local android_api_level='21'
local zlib_version='1.2.11'
local build_type='all'
local build_args=''
while [[ $# -ge 1 ]]; do
case "$1" in
-h | --help) print_usage; exit 0 ;;
-c | --clean) cleanup; exit 0 ;;
--ndk-dir) android_ndk="$2"; shift; shift ;;
--android-abi) android_abi="$2"; shift; shift ;;
--android-api-level) android_api_level="$2"; shift; shift ;;
--zlib-version) zlib_version="$2"; shift; shift ;;
--build-type) build_type="$2"; shift; shift ;;
-s | --static) build_args+=' --static'; shift ;;
*) err "Unknown argument $1"; print_usage; exit 1 ;;
esac
done
case "$build_type" in
"debug") build_type='Debug' ;;
"release") build_type='Release' ;;
"all") build_type='Debug Release' ;;
*) err "Unknown build type $build_type"; print_usage; exit 1 ;;
esac
download_zlib $zlib_version
for type in $build_type; do
if [[ $android_abi == 'all' ]]; then
for abi in arm arm64 x86 x86-64; do
extract_zlib $zlib_version
setup_env $android_ndk $abi $android_api_level
build_zlib $zlib_version $abi $type "$build_args"
done
else
extract_zlib $zlib_version
setup_env $android_ndk $android_abi $android_api_level
build_zlib $zlib_version $android_abi $type "$build_args"
fi
done
exit 0
}
main $@