forked from sinofool/build-libcurl-ios
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild_libcurl_dist.sh
executable file
·290 lines (231 loc) · 7.66 KB
/
build_libcurl_dist.sh
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env bash
# Configuration ###############################################################
# Each slice must follow the format "ARCH HOST PLATFORM"
SLICES=(
"x86_64 x86_64-apple-darwin iphonesimulator"
"arm64 arm-apple-darwin iphonesimulator"
"arm64 arm-apple-darwin iphoneos"
)
NGHTTP2_VERSION="1.58.0"
IPHONEOS_DEPLOYMENT_TARGET="17.0"
###############################################################################
CURL_SRC_DIR="${1:-}"
set -euo pipefail
if [ ! -d "$CURL_SRC_DIR" ]; then
echo "Expected the cURL directory as argument"
exit 1
fi
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
THREADS="$(sysctl -n hw.logicalcpu_max)"
DEFAULT_DIST_DIR="${CURRENT_DIR}/dist"
DIST_DIR=${DIST_DIR:-$DEFAULT_DIST_DIR}
DEFAULT_BUILD_DIR="${CURRENT_DIR}/build"
BUILD_DIR=${BUILD_DIR:-$DEFAULT_BUILD_DIR}
mkdir -p "$BUILD_DIR"
CURL_BUILD_DIR="$BUILD_DIR/curl"
NGHTTP2_TARBALL_URL="https://github.com/nghttp2/nghttp2/releases/download/v${NGHTTP2_VERSION}/nghttp2-${NGHTTP2_VERSION}.tar.bz2"
NGHTTP2_DIR="$BUILD_DIR/nghttp2"
NGHTTP2_BUILD_DIR="$NGHTTP2_DIR/build"
NGHTTP2_SRC_DIR="$NGHTTP2_DIR/src"
NGHTTP2_TARBALL_PATH="$NGHTTP2_DIR/nghttp2-${NGHTTP2_VERSION}.tar.bz2"
###############################################################################
function dirname_for_slice() {
SLICE="$1"
read -r -a SLICE_PARTS <<< "$SLICE"
ARCH="${SLICE_PARTS[0]}"
HOST="${SLICE_PARTS[1]}"
PLATFORM="${SLICE_PARTS[2]}"
echo "$PLATFORM/$ARCH"
}
function prepare_building_for_slice() {
SLICE="$1"
read -r -a SLICE_PARTS <<< "$SLICE"
ARCH="${SLICE_PARTS[0]}"
HOST="${SLICE_PARTS[1]}"
PLATFORM="${SLICE_PARTS[2]}"
DESTINATION="$PLATFORM"
if [[ "$DESTINATION" =~ "simulator" ]]; then
DESTINATION="simulator"
fi
SDKROOT="$(xcrun --sdk "$PLATFORM" --show-sdk-path)"
CC="$(xcrun -f clang)"
CPP="$CC -E"
export CC
export CPP
export CFLAGS="-Os -miphoneos-version-min=${IPHONEOS_DEPLOYMENT_TARGET} -isysroot ${SDKROOT} -target ${ARCH}-apple-ios${IPHONEOS_DEPLOYMENT_TARGET}-${DESTINATION}"
export CPPFLAGS="-arch ${ARCH} -I${SDKROOT}/usr/include"
export LDFLAGS="-arch ${ARCH} -isysroot ${SDKROOT}"
}
function build_nghttp2_slice() {
SLICE="$1"
read -r -a SLICE_PARTS <<< "$SLICE"
ARCH="${SLICE_PARTS[0]}"
HOST="${SLICE_PARTS[1]}"
PLATFORM="${SLICE_PARTS[2]}"
mkdir -p "$NGHTTP2_BUILD_DIR"
SLICE_DIRNAME=$(dirname_for_slice "$SLICE")
PREFIX="${NGHTTP2_BUILD_DIR}/${SLICE_DIRNAME}"
if [ ! -f "$NGHTTP2_TARBALL_PATH" ]; then
curl -Lo "$NGHTTP2_TARBALL_PATH" "$NGHTTP2_TARBALL_URL"
(
pushd "$NGHTTP2_DIR"
tar -xf "$NGHTTP2_TARBALL_PATH"
mv "nghttp2-$NGHTTP2_VERSION" "$NGHTTP2_SRC_DIR"
popd
)
fi
(
pushd "$NGHTTP2_SRC_DIR"
make clean || :
./configure \
--disable-shared \
--enable-lib-only \
--host="${HOST}" --prefix="${PREFIX}"
xcrun make -j "$THREADS"
xcrun make install
popd
)
}
function build_curl_slice() {
SLICE="$1"
read -r -a SLICE_PARTS <<< "$SLICE"
ARCH="${SLICE_PARTS[0]}"
HOST="${SLICE_PARTS[1]}"
PLATFORM="${SLICE_PARTS[2]}"
mkdir -p "$CURL_BUILD_DIR"
SLICE_DIRNAME=$(dirname_for_slice "$SLICE")
PREFIX="${CURL_BUILD_DIR}/${SLICE_DIRNAME}"
NGHTTP2_SLICE_DIR="${NGHTTP2_BUILD_DIR}/${SLICE_DIRNAME}"
echo "BUILDING CURL FOR ${PLATFORM}"
echo " ARCH = ${ARCH}"
echo " HOST = ${HOST}"
echo " SDKROOT = ${SDKROOT}"
echo " BUILD DIR = ${PREFIX}"
echo
(
pushd "$CURL_SRC_DIR"
make clean || :
./configure \
--disable-dependency-tracking \
--disable-shared \
--enable-static \
\
--disable-debug \
--disable-curldebug \
--enable-optimize \
--enable-warnings \
--enable-symbol-hiding \
\
--disable-ares \
\
--enable-http \
--disable-ftp \
--disable-file \
--disable-ldap \
--disable-ldaps \
--disable-rtsp \
--disable-proxy \
--disable-dict \
--disable-telnet \
--disable-tftp \
--disable-pop3 \
--disable-imap \
--disable-smb \
--disable-smtp \
--disable-gopher \
--disable-manual \
--disable-libcurl-option \
--enable-ipv6 \
\
--enable-threaded-resolver \
--disable-sspi \
--disable-crypto-auth \
--disable-tls-srp \
\
--without-winssl \
--without-schannel \
--with-secure-transport \
\
--without-libidn2 \
\
--with-nghttp2="$NGHTTP2_SLICE_DIR" \
\
--host="${HOST}" --prefix="${PREFIX}"
xcrun make -j "$THREADS"
xcrun make install
popd
)
}
function check_identical_headers() {
LIB_BUILD_DIR="$1"
# Ensure all the generated 'include' directories are identical
for SLICE_INDEX in $(seq 1 $((${#SLICES[@]} - 1))); do
PREV_SLICE_INDEX=$((SLICE_INDEX - 1))
PREV_SLICE_DIR="$(dirname_for_slice "${SLICES[PREV_SLICE_INDEX]}")"
PREV_INCLUDE_DIR="$LIB_BUILD_DIR/${PREV_SLICE_DIR}/include"
SLICE_DIR="$(dirname_for_slice "${SLICES[$SLICE_INDEX]}")"
INCLUDE_DIR="$LIB_BUILD_DIR/${SLICE_DIR}/include"
if ! diff -r "$PREV_INCLUDE_DIR" "$INCLUDE_DIR" > /dev/null 2>&1 ; then
echo "Error: The generated /include directories are not identical"
echo "\"$PREV_INCLUDE_DIR\" and \"$INCLUDE_DIR\" are different"
exit 2
fi
done
}
function build_xcframework() {
LIBNAME="$1"
LIB_BUILD_DIR="$2"
# lipo slices together per platform
# https://developer.apple.com/forums/thread/666335
LIB_ARGS=()
PLATFORM_COUNT=0
for PLATFORM in "$LIB_BUILD_DIR"/*; do
PLATFORM=$(basename "$PLATFORM")
OUTPUT="$LIB_BUILD_DIR/$PLATFORM/lib${LIBNAME}.a"
xcrun lipo -create "$LIB_BUILD_DIR/$PLATFORM"/*"/lib/lib${LIBNAME}.a" -output "$OUTPUT"
LIB_ARGS+=('-library' "$OUTPUT")
PLATFORM_COUNT=$(( PLATFORM_COUNT + 1 ))
done
echo
INCLUDE_DIR="$LIB_BUILD_DIR/$(dirname_for_slice "${SLICES[0]}")/include"
XCFRAMEWORK_PATH="$DIST_DIR/$LIBNAME.xcframework"
xcrun xcodebuild -create-xcframework \
"${LIB_ARGS[@]}" \
-headers "${INCLUDE_DIR}" \
-output "${XCFRAMEWORK_PATH}"
# Fix the 'Headers' directory location in the xcframework, and update the
# Info.plist accordingly for all slices.
XCFRAMEWORK_HEADERS_PATH=$(find "${XCFRAMEWORK_PATH}" -name 'Headers')
mv "$XCFRAMEWORK_HEADERS_PATH" "$XCFRAMEWORK_PATH"
for I in $(seq 0 $((PLATFORM_COUNT - 1))); do
xcrun plutil -replace "AvailableLibraries.$I.HeadersPath" -string '../Headers' "$XCFRAMEWORK_PATH/Info.plist"
done
echo "Built $LIBNAME XCFramework in $DIST_DIR"
}
function log() {
echo -e "\e[34;1m[BUILD_LIBCURL_IOS]\e[0;1m $1\e[0m"
}
rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"
# Build a "slice" for each supported platform x arch combo
for SLICE in "${SLICES[@]}"; do
log "Preparing building slice $SLICE"
prepare_building_for_slice "$SLICE"
log "Building slice $SLICE of nghttp2"
build_nghttp2_slice "$SLICE"
log "Succeeded building slice $SLICE of nghttp2"
log "Building slice $SLICE of curl"
build_curl_slice "$SLICE"
log "Succeeded building slice $SLICE of curl"
done
log "Checking headers"
check_identical_headers "$NGHTTP2_BUILD_DIR"
check_identical_headers "$CURL_BUILD_DIR"
log "Building xcframeworks"
build_xcframework "nghttp2" "$NGHTTP2_BUILD_DIR"
build_xcframework "curl" "$CURL_BUILD_DIR"
NGHTTP2_VERSION=$(grep 'nghttp2 VERSION' "$NGHTTP2_SRC_DIR/CMakeLists.txt" | sed -Ee 's/.+VERSION ([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\)/\1/')
echo "$NGHTTP2_VERSION" > "${DIST_DIR}/nghttp2.xcframework/VERSION"
CURL_VERSION=$(/usr/libexec/PlistBuddy "$CURL_SRC_DIR/lib/libcurl.plist" -c 'Print :CFBundleVersion')
echo "$CURL_VERSION" > "${DIST_DIR}/curl.xcframework/VERSION"
log "Frameworks built successfully!"