-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathbuild_ssl.sh
More file actions
executable file
·247 lines (203 loc) · 6.76 KB
/
build_ssl.sh
File metadata and controls
executable file
·247 lines (203 loc) · 6.76 KB
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
240
241
242
243
244
245
246
247
#!/bin/bash -x
## Prerequisites
## The script supports builds from Linux and macOS.
## set BUILD_DIR and OUTPUT_ROOT variables to use custom build and output paths.
## Set NDK_ROOT_PREFIX to use custom Android NDK root path that contains various
## NDK versions.
set -eo pipefail
[ -n "$OUTPUT_ROOT" ] || OUTPUT_ROOT="$(pwd)"
[ -n "$BUILD_DIR" ] || BUILD_DIR="$(pwd)/build"
# Set Android NDK path prefix under the Android SDK
if [ -z "$NDK_ROOT_PREFIX" ]; then
if [[ "$(uname)" == "Darwin" ]]; then
NDK_ROOT_PREFIX="$HOME/Library/Android/sdk/ndk"
else
NDK_ROOT_PREFIX="$HOME/Android/Sdk/ndk"
fi
fi
ssl_versions=("1.1.1w" "3.1.8")
architectures=("arm64" "arm" "x86_64" "x86")
build_types=('' 'no-asm')
SCRIPT_PARENT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" && pwd)"
get_qt_arch() {
# takes OpenSSL arch as argument
case $1 in
arm64)
echo "arm64-v8a"
;;
arm)
echo "armeabi-v7a"
;;
*)
echo $1
;;
esac
}
get_ssl_build_dir() {
# takes OpenSSL version as argument
case $1 in
1.1.*)
echo "ssl_1.1"
;;
3.*)
echo "ssl_3"
;;
*)
echo $1
;;
esac
}
get_ssl_patch_file_name() {
case $1 in
1.1.*) echo "ssl_1.patch" ;;
3.*) echo "ssl_3.patch" ;;
*)
echo "Unhandled SSL version argument '$1'"
exit 1
;;
esac
}
get_ssl_ndk_version() {
# takes OpenSSL version as argument
case $1 in
1.1.*)
echo "21.4.7075529"
;;
3.*)
echo "25.2.9519653"
;;
*)
echo $1
;;
esac
}
download_ssl_version() {
ssl_version=$1
download_dir=$2
ssl_filename="openssl-$ssl_version.tar.gz"
echo "Downloading OpenSSL $ssl_filename"
if [ ! -f "$ssl_filename" ]; then
curl -sfL -o "$download_dir/$ssl_filename" "https://www.openssl.org/source/$ssl_filename" \
|| (echo "Downloading sources failed!"; exit 1)
fi
}
extract_package() {
ssl_version=$1
src_path=$2
output_dir=$3
echo "Extracting OpenSSL $ssl_version under $output_dir"
rm -fr "openssl-$ssl_version"
tar xf "$src_path/openssl-$ssl_version.tar.gz" || exit 1
}
configure_ssl() {
ssl_version=$1
arch=$2
ndk=$3
build_type=$4
patch_file=$5
log_file=$6
nkd_path="$NDK_ROOT_PREFIX/$ndk"
if [ ! -e "$nkd_path" ]; then
echo "NDK path $nkd_path does not exist"
exit 1
fi
export ANDROID_NDK_ROOT="$nkd_path"
export ANDROID_NDK_HOME="$nkd_path"
declare hosts=("linux-x86_64" "linux-x86" "darwin-x86_64" "darwin-x86")
for host in "${hosts[@]}"; do
if [ -d "$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$host/bin" ]; then
ANDROID_TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$host/bin"
export PATH="$ANDROID_TOOLCHAIN:$PATH"
break
fi
done
case $ssl_version in
1.1.*) ANDROID_API=21 ;;
3.*) ANDROID_API=23 ;;
esac
# Qt expects OpenSSL binaries that have the filename 'libssl_3.so'
# instead of the usual 'libssl.so'. We configure OpenSSL to compile
# such a variant by tweaking the SHLIB_VARIANT. However, OpenSSL
# has built-in logic that will cause it to adjust the symbollink
# version tags accordingly. For Qt binaries, we do NOT want this
# behavior. The following code is a workaround that patches OpenSSL
# source code to set the shlib_variant, and then disables the
# versioning behavior in the relevant Perl script.
patch < "$patch_file"
echo "Configuring OpenSSL $ssl_version for ABI $arch with NDK $ndk"
config_params=()
if [ -n "$build_type" ]; then
config_params+=( "${build_type}" )
fi
config_params+=( "shared" "android-${arch}" "-U__ANDROID_API__"
"-D__ANDROID_API__=${ANDROID_API}" )
if [ "$arch" = "arm64" -o "$arch" = "x86_64" ]; then
echo "Configuring OpenSSL to 16KB page sizes"
config_params+=( "-Wl,-z,max-page-size=16384" "-Wl,-z,common-page-size=16384" )
fi
echo "Configure parameters: ${config_params[@]}"
./Configure "${config_params[@]}" 2>&1 1>${log_file} | tee -a ${log_file} || exit 1
make depend
}
build_ssl() {
log_file=$1
echo "Building..."
make -j$(nproc) SHLIB_VERSION_NUMBER= build_libs 2>&1 1>>${log_file} \
| tee -a ${log_file} || exit 1
}
strip_libs() {
find . -name "libcrypto_*.so" -exec llvm-strip --strip-all {} \;
find . -name "libssl_*.so" -exec llvm-strip --strip-all {} \;
}
copy_build_artefacts() {
output_dir=$1
cp libcrypto_*.so "$output_dir/" || exit 1
cp libssl_*.so "$output_dir/" || exit 1
cp libcrypto.a libssl.a "$output_dir" || exit 1
# Create relative non-versioned symlinks
ln -s $(find . -name "libcrypto_*.so" -exec basename {} \;) "${output_dir}/libcrypto.so"
ln -s $(find . -name "libssl_*.so" -exec basename {} \;) "${output_dir}/libssl.so"
ln -s "../include" "$output_dir/include"
}
[[ -e "$BUILD_DIR" ]] && rm -fr "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
pushd "$BUILD_DIR"
for build_type in "${build_types[@]}"; do
if [[ "${build_type}" ]]; then
mkdir -p $build_type
pushd $build_type
fi
for ssl_version in "${ssl_versions[@]}"; do
ndk=$(get_ssl_ndk_version $ssl_version)
version_build_dir=$(get_ssl_build_dir $ssl_version)
ssl_patch_file="${SCRIPT_PARENT_DIR}/$(get_ssl_patch_file_name $ssl_version)"
mkdir -p $version_build_dir
pushd $version_build_dir
download_ssl_version $ssl_version $BUILD_DIR
for arch in "${architectures[@]}"; do
qt_arch=$(get_qt_arch $arch)
extract_package $ssl_version $BUILD_DIR $version_build_dir
mv "openssl-$ssl_version" "openssl-$ssl_version-$arch"
pushd "openssl-$ssl_version-$arch" || exit 1
log_file="build_${arch}_${ssl_version}.log"
configure_ssl ${ssl_version} ${arch} "${ndk}" "${build_type}" "${ssl_patch_file}" "${log_file}"
# Delete existing build artefacts
output_dir="$OUTPUT_ROOT/$build_type/$version_build_dir/$qt_arch"
rm -fr "$output_dir"
mkdir -p "$output_dir" || exit 1
build_ssl ${log_file}
strip_libs
copy_build_artefacts ${output_dir}
# Copy the include dir only once since since it's the same for all abis
if [ ! -d "$output_dir/../include" ]; then
cp -a include "$output_dir/../" || exit 1
# Clean include folder
find "$output_dir/../" -name "*.in" -delete
find "$output_dir/../" -name "*.def" -delete
fi
popd
done
popd
done
[[ "${build_type}" ]] && popd
done