-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·455 lines (362 loc) · 11.9 KB
/
build.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/bash
# Examples:
#
# Build for desktop
# > ./build.sh build release arm64-apple-macos13.0
#
# Build for iphone
# > ./build.sh build release arm64-apple-ios12.0
#
# Build for iphone
# > ./build.sh build release x86_64-apple-ios12.0-simulator
# Package layout
#
# ├── Info.plist
# ├── [ios-arm64]
# │ ├── mylib.a
# │ └── [include]
# ├── [ios-arm64_x86_64-simulator]
# │ ├── mylib.a
# │ └── [include]
# └── [macos-arm64_x86_64]
# ├── mylib.a
# └── [include]
#--------------------------------------------------------------------
# Script params
LIBNAME="libcurl"
# What to do (build, test)
BUILDWHAT="$1"
# Build type (release, debug)
BUILDTYPE="$2"
# Build target, i.e. arm64-apple-macos13.0, aarch64-apple-ios12.0, x86_64-apple-ios12.0-simulator, ...
BUILDTARGET="$3"
# Build Output
BUILDOUT="$4"
#--------------------------------------------------------------------
# Functions
Log()
{
echo ">>>>>> $@"
}
exitWithError()
{
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "$@"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit -1
}
exitOnError()
{
if [[ 0 -eq $? ]]; then return 0; fi
exitWithError $@
}
gitCheckout()
{
local LIBGIT="$1"
local LIBGITVER="$2"
local LIBBUILD="$3"
# Check out c++ library if needed
if [ ! -d "${LIBBUILD}" ]; then
Log "Checking out: ${LIBGIT} -> ${LIBGITVER}"
if [ ! -z "${LIBGITVER}" ]; then
git clone --recurse-submodules --depth 1 -b ${LIBGITVER} ${LIBGIT} ${LIBBUILD}
else
git clone --recurse-submodules ${LIBGIT} ${LIBBUILD}
fi
fi
if [ ! -d "${LIBBUILD}" ]; then
exitWithError "Failed to checkout $LIBGIT"
fi
}
extractVersion()
{
local tuple="$1"
local key="$2"
local version=""
IFS='-' read -ra components <<< "$tuple"
for component in "${components[@]}"; do
if [[ $component == ${key}* ]]; then
version="${component#$key}"
break
fi
done
echo "$version"
}
#--------------------------------------------------------------------
# Options
# Sync command
SYNC="rsync -a"
# Default build what
if [ -z "${BUILDWHAT}" ]; then
BUILDWHAT="build"
fi
# Default build type
if [ -z "${BUILDTYPE}" ]; then
BUILDTYPE="release"
fi
if [ -z "${BUILDTARGET}" ]; then
BUILDTARGET="arm64-apple-macos"
fi
# ios-arm64_x86_64-simulator
if [[ $BUILDTARGET == *"ios"* ]]; then
TGT_OS="ios"
else
TGT_OS="macos"
fi
if [[ $BUILDTARGET == *"arm64"* ]]; then
if [[ $BUILDTARGET == *"x86_64"* ]]; then
TGT_ARCH="arm64_x86_64"
elif [[ $BUILDTARGET == *"x86"* ]]; then
TGT_ARCH="arm64_x86"
else
TGT_ARCH="arm64"
fi
elif [[ $BUILDTARGET == *"x86_64"* ]]; then
TGT_ARCH="x86_64"
elif [[ $BUILDTARGET == *"x86"* ]]; then
TGT_ARCH="x86"
else
exitWithError "Invalid architecture : $BUILDTARGET"
fi
TGT_OPTS=
if [[ $BUILDTARGET == *"simulator"* ]]; then
TGT_OPTS="-simulator"
fi
# NUMCPUS=1
NUMCPUS=$(sysctl -n hw.physicalcpu)
#--------------------------------------------------------------------
# Get root script path
if [ ! -z "$0" ] && [ ! -z "$(which realpath)" ]; then
SCRIPTPATH=$(realpath $0)
fi
ROOTDIR="$GITHUB_WORKSPACE"
if [ -z "$ROOTDIR" ]; then
if [[ -z "$SCRIPTPATH" ]] || [[ "." == "$SCRIPTPATH" ]]; then
ROOTDIR=$(pwd)
elif [ ! -z "$SCRIPTPATH" ]; then
ROOTDIR=$(dirname $SCRIPTPATH)
else
SCRIPTPATH=.
ROOTDIR=.
fi
fi
#--------------------------------------------------------------------
# Defaults
if [ -z $BUILDOUT ]; then
BUILDOUT="${ROOTDIR}/build"
else
# Get path to current directory if needed to use as custom directory
if [ "$BUILDOUT" == "." ] || [ "$BUILDOUT" == "./" ]; then
BUILDOUT="$(pwd)"
fi
fi
# Add build type to output folder
BUILDOUT="${BUILDOUT}/${BUILDTYPE}"
# Make custom output directory if it doesn't exist
if [ ! -z "$BUILDOUT" ] && [ ! -d "$BUILDOUT" ]; then
mkdir -p "$BUILDOUT"
fi
if [ ! -d "$BUILDOUT" ]; then
exitWithError "Failed to create diretory : $BUILDOUT"
fi
TARGET="${TGT_OS}-${TGT_ARCH}${TGT_OPTS}"
LIBROOT="${BUILDOUT}/${BUILDTARGET}/lib3"
LIBINST="${BUILDOUT}/${BUILDTARGET}/install"
PKGNAME="${LIBNAME}.a.xcframework"
PKGROOT="${BUILDOUT}/pkg/${PKGNAME}"
PKGFILE="${BUILDOUT}/pkg/${PKGNAME}.zip"
# iOS toolchain
if [[ $BUILDTARGET == *"ios"* ]]; then
TGT_OSVER=$(extractVersion "$BUILDTARGET" "ios")
if [ -z "$TGT_OSVER" ]; then
TGT_OSVER="14.0"
fi
gitCheckout "https://github.com/leetal/ios-cmake.git" "4.3.0" "${LIBROOT}/ios-cmake"
if [[ $BUILDWHAT == *"xbuild"* ]]; then
TOOLCHAIN="${TOOLCHAIN} -GXcode"
fi
# https://github.com/leetal/ios-cmake/blob/master/ios.toolchain.cmake
if [[ $BUILDTARGET == *"simulator"* ]]; then
if [ "${TGT_ARCH}" == "x86" ]; then
TGT_PLATFORM="SIMULATOR"
elif [ "${TGT_ARCH}" == "x86_64" ]; then
TGT_PLATFORM="SIMULATOR64"
else
TGT_PLATFORM="SIMULATORARM64"
fi
else
if [ "${TGT_ARCH}" == "x86" ]; then
TGT_PLATFORM="OS"
elif [ "${TGT_ARCH}" == "x86_64" ]; then
TGT_ARCH="arm64_x86_64"
TGT_PLATFORM="OS64COMBINED"
else
TGT_PLATFORM="OS64"
fi
fi
TARGET="${TGT_OS}-${TGT_ARCH}${TGT_OPTS}"
TOOLCHAIN="${TOOLCHAIN} \
-DCMAKE_TOOLCHAIN_FILE=${LIBROOT}/ios-cmake/ios.toolchain.cmake \
-DPLATFORM=${TGT_PLATFORM} \
-DENABLE_BITCODE=OFF \
-DDEPLOYMENT_TARGET=$TGT_OSVER \
"
else
TGT_OSVER=$(extractVersion "$BUILDTARGET" "macos")
if [ -z "$TGT_OSVER" ]; then
TGT_OSVER="13.2"
fi
TOOLCHAIN="${TOOLCHAIN} \
-DCMAKE_OSX_DEPLOYMENT_TARGET=$TGT_OSVER \
-DCMAKE_OSX_ARCHITECTURES=$TGT_ARCH
"
fi
TOOLCHAIN="${TOOLCHAIN} \
-DCMAKE_CXX_STANDARD=17 \
"
#--------------------------------------------------------------------
showParams()
{
echo ""
Log "#--------------------------------------------------------------------"
Log "LIBNAME : ${LIBNAME}"
Log "BUILDWHAT : ${BUILDWHAT}"
Log "BUILDTYPE : ${BUILDTYPE}"
Log "BUILDTARGET : ${BUILDTARGET}"
Log "0 : ${0}"
Log "SCRIPTPATH : ${SCRIPTPATH}"
Log "ROOTDIR : ${ROOTDIR}"
Log "BUILDOUT : ${BUILDOUT}"
Log "TARGET : ${TARGET}"
Log "OSVER : ${TGT_OSVER}"
Log "ARCH : ${TGT_ARCH}"
Log "PLATFORM : ${TGT_PLATFORM}"
Log "PKGNAME : ${PKGNAME}"
Log "PKGROOT : ${PKGROOT}"
Log "LIBROOT : ${LIBROOT}"
Log "#--------------------------------------------------------------------"
echo ""
}
showParams
#-------------------------------------------------------------------
# Rebuild lib and copy files if needed
#-------------------------------------------------------------------
if [[ $BUILDWHAT == *"clean"* ]]; then
if [ -d "${LIBROOT}" ]; then
rm -Rf "${LIBROOT}"
fi
fi
if [ ! -d "${LIBROOT}" ]; then
Log "Reinitializing install..."
mkdir -p "${LIBROOT}"
REBUILDLIBS="YES"
fi
LIBBUILD="${LIBROOT}/${LIBNAME}"
LIBBUILDOUT="${LIBBUILD}/build"
LIBINSTFULL="${LIBINST}/${BUILDTARGET}/${BUILDTYPE}"
#-------------------------------------------------------------------
# Checkout and build library
#-------------------------------------------------------------------
if [ ! -z "${REBUILDLIBS}" ] \
|| [[ $BUILDWHAT == *"rebuild"* ]] \
|| [ ! -f "${LIBINSTFULL}/lib/libcurl.a" ]; then
# Remove existing package
rm -Rf "${PKGROOT}/${TARGET}"
echo "\n====================== BUILD CURL =====================\n"
gitCheckout "https://github.com/curl/curl.git" "curl-8_2_0" "${LIBBUILD}"
cd "${LIBBUILD}"
echo "\n====================== CONFIGURING =====================\n"
cmake . -B ./build -DCMAKE_BUILD_TYPE=${BUILDTYPE} \
${TOOLCHAIN} \
-DHTTP_ONLY=ON \
-DCURL_USE_LIBSSH=OFF \
-DCURL_USE_LIBSSH2=OFF \
-DBUILD_CURL_EXE=OFF \
-DCURL_USE_SECTRANSP=ON \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO \
-DCMAKE_INSTALL_PREFIX="${LIBINSTFULL}"
exitOnError "Failed to configure ${LIBNAME}"
if [[ $BUILDWHAT == *"xbuild"* ]]; then
echo "\n==================== XCODE BUILDING ====================\n"
# Get targets: xcodebuild -list -project mylib.xcodeproj
xcodebuild -project "${LIBBUILDOUT}/curl.xcodeproj" \
-target curl_static \
-configuration Release \
-sdk iphonesimulator
exitOnError "Failed to xbuild ${LIBNAME}"
mkdir -p "${LIBINSTFULL}/include"
cp -R "${LIBROOT}/${LIBNAME}/include/." "${LIBINSTFULL}/include/"
mkdir -p "${LIBINSTFULL}/lib"
cp -R "${LIBBUILDOUT}/lib/Release/." "${LIBINSTFULL}/lib/"
else
echo "\n======================= BUILDING =======================\n"
cmake --build ./build -j$NUMCPUS
exitOnError "Failed to build ${LIBNAME}"
echo "\n====================== INSTALLING ======================\n"
cmake --install ./build
exitOnError "Failed to install ${LIBNAME}"
fi
cd "${BUILDOUT}"
fi
#-------------------------------------------------------------------
# Create target package
#-------------------------------------------------------------------
if [ ! -z "${REBUILDLIBS}" ] \
|| [ ! -d "${PKGROOT}/${TARGET}" ]; then
INCPATH="include"
LIBPATH="${LIBNAME}.a"
# Re initialize directory
if [ -d "${PKGROOT}/${TARGET}" ]; then
rm -Rf "${PKGROOT}/${TARGET}"
fi
mkdir -p "${PKGROOT}/${TARGET}"
# Copy include files
cp -R "${LIBINSTFULL}/." "${PKGROOT}/${TARGET}/"
mv ${LIBINSTFULL}/lib/libcurl.a "${PKGROOT}/${TARGET}/${LIBNAME}.a"
exitOnError "Failed to build library ${LIBNAME}"
lipo -info "${PKGROOT}/${TARGET}/${LIBNAME}.a"
INCPATH="include"
LIBPATH="${LIBNAME}.a"
# Copy manifest
cp "${ROOTDIR}/Info.target.plist.in" "${PKGROOT}/${TARGET}/Info.target.plist"
sed -i '' "s|%%TARGET%%|${TARGET}|g" "${PKGROOT}/${TARGET}/Info.target.plist"
sed -i '' "s|%%OS%%|${TGT_OS}|g" "${PKGROOT}/${TARGET}/Info.target.plist"
sed -i '' "s|%%ARCH%%|${TGT_ARCH}|g" "${PKGROOT}/${TARGET}/Info.target.plist"
sed -i '' "s|%%INCPATH%%|${INCPATH}|g" "${PKGROOT}/${TARGET}/Info.target.plist"
sed -i '' "s|%%LIBPATH%%|${LIBPATH}|g" "${PKGROOT}/${TARGET}/Info.target.plist"
EXTRA=
if [[ $BUILDTARGET == *"simulator"* ]]; then
EXTRA="<key>SupportedPlatformVariant</key><string>simulator</string>"
fi
sed -i '' "s|%%EXTRA%%|${EXTRA}|g" "${PKGROOT}/${TARGET}/Info.target.plist"
fi
#-------------------------------------------------------------------
# Create full package
#-------------------------------------------------------------------
if [ -d "${PKGROOT}" ]; then
cd "${PKGROOT}"
TARGETINFO=
for SUB in */; do
echo "Adding: $SUB"
if [ -f "${SUB}/Info.target.plist" ]; then
TARGETINFO="$TARGETINFO$(cat "${SUB}/Info.target.plist")"
fi
done
if [ ! -z "$TARGETINFO" ]; then
TARGETINFO=""${TARGETINFO//$'\n'/\\n}""
cp "${ROOTDIR}/Info.plist.in" "${PKGROOT}/Info.plist"
sed -i '' "s|%%TARGETS%%|${TARGETINFO}|g" "${PKGROOT}/Info.plist"
cd "${PKGROOT}/.."
# Remove old package if any
if [ -f "${PKGFILE}" ]; then
rm "${PKGFILE}"
fi
# Create new package
zip -r "${PKGFILE}" "$PKGNAME" -x "*.DS_Store"
# Calculate sha256
openssl dgst -sha256 < "${PKGFILE}" > "${PKGFILE}.sha256.txt"
cd "${BUILDOUT}"
fi
fi
showParams