-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_libpng
executable file
·231 lines (210 loc) · 7.01 KB
/
build_libpng
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
#!/bin/bash
#
# Cross-compiles libpng library for Android
#
# This script download libpng source code from libpng official website (https://www.libpng.net/)
#
# This script cross-compiles libpng 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 libpng_BASE_URL='https://download.sourceforge.net/libpng'
readonly OUTPUT_DIR="$PWD/output"
readonly DOWNLOADS_DIR='downloads'
readonly LOG_FILE='build.log'
print_usage() {
echo 'usage:'
echo ' build_libpng [-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 ' [--with-zlib ZLIB_PREFIX] (Build with zlib support)'
echo ' [--libpng-version LIBPNG_VERSION] (Default: 1.6.37)'
echo ' [--static-only] (Default: Build static and shared libs)'
echo
echo 'exmples:'
echo ' # Build libpng-1.6.37 static and shared libs, for android-21, all supported architectures'
echo ' build_libpng'
echo
echo ' # Build libpng-1.1.10 static and shared libs, for android-21, armeabi-v7a'
echo ' build_libpng --ndk-dir /path/to/android-ndk --android-abi arm --libpng-version 1.1.10'
echo
echo ' # Build libpng-1.6.37 static libs, for android-24, arm64-v8a with zlib support'
echo ' build_libpng --with-zlib /zlib/install/prefix --android-abi arm64 --android-api-level 24 --static-only'
}
err() {
echo -e "${RED}$*${NC}" >&2
}
cleanup() {
echo "Cleaning ..."
rm -rf $OUTPUT_DIR $DOWNLOADS_DIR $LOG_FILE
rm -rf `find . -type d -name libpng-*`
echo "Everything is clean."
}
check_error() {
if [[ $? -ne 0 ]]; then
err $@
exit 1
fi
}
############################################
# Download libpng source code
# Globals:
# libpng_BASE_URL
# DOWNLOADS_DIR
# Arguments:
# libpng version to download (i.e., 1.6.37)
############################################
download_libpng() {
local libpng=libpng-$1.tar.gz
if [[ ! -d $DOWNLOADS_DIR || ! -f "$DOWNLOADS_DIR/$libpng" ]]; then
echo "Downloading $libpng ..."
mkdir -p $DOWNLOADS_DIR && cd $DOWNLOADS_DIR
curl -sfSL $libpng_BASE_URL/$libpng -o $libpng 2>>"../$LOG_FILE"
check_error "Failed to download $libpng. See '$LOG_FILE' for details."
cd ..
fi
}
############################################
# Extract libpng source code
# Globals:
# DOWNLOADS_DIR
# Arguments:
# libpng version to extract (i.e., 1.6.37)
############################################
extract_libpng() {
local libpng=libpng-$1.tar.gz
rm -rf $libpng
echo "Extracting $libpng ..."
tar -xzf "$DOWNLOADS_DIR/$libpng" 2>>$LOG_FILE
check_error "Failed to extract $libpng. 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 libpng
# Globals:
# OUTPUT_DIR
# Arguments:
# libpng version to build
# Android ABI (i.e., arm, arm64, x86, x86-64)
# Optional zlib prefix
# Build type argument (i.e., --enable-shared=no)
############################################
build_libpng() {
local src_dir=libpng-$1
local out_dir
local zlib_options=''
local abi
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
out_dir="$OUTPUT_DIR/$src_dir/$abi"
if [[ ! -z $3 ]]; then
zlib_options = "--with-zlib-prefix=$3/$abi"
fi
cd $src_dir
echo "Building $src_dir for $abi ..."
echo "Target: $TARGET Prefix: $out_dir"
./configure --host $TARGET --prefix=$out_dir $zlib_options $4 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 libpng_version='1.6.37'
local zlib_prefix=''
local build_type=''
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 ;;
--libpng-version) libpng_version="$2"; shift; shift ;;
--with-zlib) zlib_prefix=$2; shift; shift ;;
--static-only) build_type='--enable-shared=no'; shift ;;
*) err "Unknown argument $1"; print_usage; exit 1 ;;
esac
done
download_libpng $libpng_version
if [[ $android_abi -eq 'all' ]]; then
for abi in arm arm64 x86 x86-64; do
extract_libpng $libpng_version
setup_env $android_ndk $abi $android_api_level
build_libpng $libpng_version $abi $zlib_prefix $build_type
done
else
extract_libpng $libpng_version
setup_env $android_ndk $android_abi $android_api_level
build_libpng $libpng_version $android_abi $zlib_prefix $build_type
fi
exit 0
}
main $@