-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·365 lines (296 loc) · 11.2 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
#!zsh
set -e # abort if any command fails
MIN_IOS_VERSION=13
MIN_MAC_VERSION=11
PROJ_ROOT=${PWD}
DEPS_ROOT=${PROJ_ROOT}/deps
BUILD_ROOT=${PROJ_ROOT}/build
BCWALLY_ROOT=${PROJ_ROOT}/BCWally
BUILD_LOG=${PROJ_ROOT}/buildlog.txt
CPU_COUNT=$(sysctl hw.ncpu | awk '{print $2}')
mkdir -p ${BUILD_ROOT}
echo -n > ${BUILD_LOG}
# Terminal colors
RED=`tput setaf 1`
GREEN=`tput setaf 2`
BLUE=`tput setaf 4`
CYAN=`tput setaf 6`
RESET=`tput sgr0`
progress_section() (
MESSAGE="=== ${1} ==="
echo ${MESSAGE}
echo "${CYAN}${MESSAGE}${RESET}" >&3
)
progress_item() (
MESSAGE="== ${1} =="
echo ${MESSAGE}
echo "${BLUE}${MESSAGE}${RESET}" >&3
)
progress_success() (
MESSAGE="==== ${1} ===="
echo ${MESSAGE}
echo "${GREEN}${MESSAGE}${RESET}" >&3
)
progress_error() (
MESSAGE="** ${1} **"
echo ${MESSAGE}
echo "${RED}${MESSAGE}${RESET}" >&3
)
get_dependencies() (
progress_section "Getting Dependencies"
git submodule update --init --recursive
)
build_init()
{
LIB_NAME=$1
TARGET=$2
HOST=$3
SDK=$4
BITCODE=$5
VERSION=$6
SDK_PATH=`xcrun -sdk ${SDK} --show-sdk-path`
BUILD_ARCH_DIR=${BUILD_ROOT}/${TARGET}
PREFIX=${BUILD_ARCH_DIR}/${LIB_NAME}
export CFLAGS="-O3 -isysroot ${SDK_PATH} -target ${TARGET} ${BITCODE} ${VERSION} -Wno-overriding-t-option"
export CXXFLAGS="-O3 -isysroot ${SDK_PATH} -target ${TARGET} ${BITCODE} ${VERSION} -Wno-overriding-t-option"
export LDFLAGS="-target ${TARGET} ${BITCODE}"
export CC="$(xcrun --sdk ${SDK} -f clang) -isysroot ${SDK_PATH} -target ${TARGET} ${BITCODE} ${VERSION}"
export CXX="$(xcrun --sdk ${SDK} -f clang++) -isysroot ${SDK_PATH} -target ${TARGET} ${BITCODE} ${VERSION}"
progress_item "${LIB_NAME} ${TARGET}"
}
build_clibwally()
(
build_init wallycore $@
pushd ${DEPS_ROOT}/bc-libwally-core
./tools/cleanup.sh
./tools/autogen.sh
PKG_CONFIG_ALLOW_CROSS=1 \
PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig \
./configure \
--disable-shared \
--host=${HOST} \
--enable-static \
--prefix=${PREFIX}
make clean
make -j${CPU_COUNT}
make install
make clean
popd
# Remove unused headers
pushd ${PREFIX}/include
rm secp256k1*.h
rm wally_elements.h
rm wally.hpp
popd
)
build_c_libraries()
(
progress_section "Building C Libraries"
# TARGET HOST SDK BITCODE VERSION
ARM_IOS=( arm64-apple-ios arm-apple-darwin iphoneos -fembed-bitcode -mios-version-min=${MIN_IOS_VERSION})
X86_CATALYST=(x86_64-apple-ios-macabi x86_64-apple-darwin macosx -fembed-bitcode -mmacosx-version-min=${MIN_MAC_VERSION})
ARM_CATALYST=(arm64-apple-ios-macabi arm-apple-darwin macosx -fembed-bitcode -mmacosx-version-min=${MIN_MAC_VERSION})
X86_IOS_SIM=( x86_64-apple-ios-simulator x86_64-apple-darwin iphonesimulator -fembed-bitcode-marker -mios-simulator-version-min=${MIN_IOS_VERSION})
ARM_IOS_SIM=( arm64-apple-ios-simulator arm-apple-darwin iphonesimulator -fembed-bitcode-marker -mios-simulator-version-min=${MIN_IOS_VERSION})
X86_MAC=( x86_64-apple-darwin x86_64-apple-darwin macosx -fembed-bitcode -mmacosx-version-min=${MIN_MAC_VERSION})
ARM_MAC=( arm64-apple-darwin arm-apple-darwin macosx -fembed-bitcode -mmacosx-version-min=${MIN_MAC_VERSION})
build_clibwally ${ARM_IOS[@]}
build_clibwally ${X86_CATALYST[@]}
build_clibwally ${ARM_CATALYST[@]}
build_clibwally ${X86_IOS_SIM[@]}
build_clibwally ${ARM_IOS_SIM[@]}
build_clibwally ${X86_MAC[@]}
build_clibwally ${ARM_MAC[@]}
)
build_swift_framework()
(
FRAMEWORK=$1
LIBS=$2
TARGET=$3
SDK=$4
PLATFORM_DIR=$5
CATALYST=$6
BITCODE=$7
VERSION=$8
CONFIGURATION=Debug
TARGET_ELEMS=("${(@s/-/)TARGET}")
ARCHS=${TARGET_ELEMS[1]}
LIBS_NAMES=("${(@s/ /)LIBS}")
LIBS_PATHS=()
for e in $LIBS_NAMES; do
LIBS_PATHS+=\"${BUILD_ROOT}/${TARGET}/${e}/lib\"
done
FRAMEWORK_ROOT=${PROJ_ROOT}/${FRAMEWORK}
PROJECT=${FRAMEWORK_ROOT}/${FRAMEWORK}.xcodeproj
SCHEME=${FRAMEWORK}
DEST_DIR=${BUILD_ROOT}/${TARGET}
FRAMEWORK_DIR_NAME=${FRAMEWORK}.framework
rm -rf ${DEST_DIR}/${FRAMEWORK_DIR_NAME}
ARGS=(\
-project ${PROJECT} \
-scheme ${SCHEME} \
-configuration ${CONFIGURATION} \
-sdk ${SDK} \
${VERSION} \
LIBRARY_SEARCH_PATHS="${LIBS_PATHS}" \
ONLY_ACTIVE_ARCH=YES \
ARCHS=${ARCHS} \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES \
SUPPORTS_MACCATALYST=${CATALYST} \
BITCODE_GENERATION_MODE=${BITCODE} \
CODE_SIGN_IDENTITY= \
CODE_SIGNING_ALLOWED=YES \
CODE_SIGNING_REQUIRED=NO \
)
# (
# printf $'\n'
# printf " <%s> " $@
# printf $'\n'
# printf " <%s> " $LIBS_NAMES
# printf $'\n'
# printf " <%s> " $ARGS
# printf $'\n'
# printf $'\n'
# ) >&3
# exit 0
progress_item "${FRAMEWORK} ${TARGET}"
# This has the complete swift module information
xcodebuild clean build ${ARGS[@]}
# This has the complete Bitcode information
ARCHIVE_PATH=${DEST_DIR}/${FRAMEWORK}.xcarchive
xcodebuild archive -archivePath ${ARCHIVE_PATH} ${ARGS[@]}
BUILD_DIR=`xcodebuild ${ARGS[@]} -showBuildSettings | grep -o '\<BUILD_DIR = .*' | cut -d ' ' -f 3`
if [[ ${PLATFORM_DIR} == NONE ]]
then
FRAMEWORK_SOURCE_DIR=${BUILD_DIR}/${CONFIGURATION}
else
FRAMEWORK_SOURCE_DIR=${BUILD_DIR}/${CONFIGURATION}-${PLATFORM_DIR}
fi
cp -R ${FRAMEWORK_SOURCE_DIR}/${FRAMEWORK_DIR_NAME} ${DEST_DIR}/
xcodebuild clean ${ARGS[@]}
# Copy the binary from the framework in the archive to the main framework so we have correct Swift module information
# **and** complete Bitcode information.
cp ${ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_DIR_NAME}/${FRAMEWORK} ${DEST_DIR}/${FRAMEWORK_DIR_NAME}/
# Delete the archive, we no longer need it.
rm -rf ${ARCHIVE_PATH}
#echo diff -rq "${FRAMEWORK_SOURCE_DIR}/${FRAMEWORK_DIR_NAME}" "${DEST_DIR}/${FRAMEWORK_DIR_NAME}"
)
build_swift_frameworks()
(
progress_section "Building Swift Frameworks"
# TARGET SDK PLATFORM_DIR CATALYST BITCODE VERSION
ARM_IOS=( arm64-apple-ios iphoneos iphoneos NO bitcode IPHONEOS_DEPLOYMENT_TARGET=${MIN_IOS_VERSION})
X86_CATALYST=( x86_64-apple-ios-macabi macosx maccatalyst YES bitcode MACOSX_DEPLOYMENT_TARGET=${MIN_MAC_VERSION})
ARM_CATALYST=( arm64-apple-ios-macabi macosx maccatalyst YES bitcode MACOSX_DEPLOYMENT_TARGET=${MIN_MAC_VERSION})
X86_IOS_SIM=( x86_64-apple-ios-simulator iphonesimulator iphonesimulator NO marker IPHONEOS_DEPLOYMENT_TARGET=${MIN_IOS_VERSION})
ARM_IOS_SIM=( arm64-apple-ios-simulator iphonesimulator iphonesimulator NO marker IPHONEOS_DEPLOYMENT_TARGET=${MIN_IOS_VERSION})
X86_MAC=( x86_64-apple-darwin macosx NONE NO bitcode MACOSX_DEPLOYMENT_TARGET=${MIN_MAC_VERSION})
ARM_MAC=( arm64-apple-darwin macosx NONE NO bitcode MACOSX_DEPLOYMENT_TARGET=${MIN_MAC_VERSION})
build_swift_framework BCWally "secp256k1 wallycore" ${ARM_IOS[@]}
build_swift_framework BCWally "secp256k1 wallycore" ${X86_CATALYST[@]}
build_swift_framework BCWally "secp256k1 wallycore" ${ARM_CATALYST[@]}
build_swift_framework BCWally "secp256k1 wallycore" ${X86_IOS_SIM[@]}
build_swift_framework BCWally "secp256k1 wallycore" ${ARM_IOS_SIM[@]}
build_swift_framework BCWally "secp256k1 wallycore" ${X86_MAC[@]}
build_swift_framework BCWally "secp256k1 wallycore" ${ARM_MAC[@]}
)
lipo_swift_framework_variant()
(
FRAMEWORK=$1
PLATFORM=$2
FRAMEWORK_DIR_NAME=${FRAMEWORK}.framework
PLATFORMFRAMEWORK=${PLATFORM}/${FRAMEWORK_DIR_NAME}
FRAMEWORK1DIR=${BUILD_ROOT}/arm64-${PLATFORMFRAMEWORK}
FRAMEWORK2DIR=${BUILD_ROOT}/x86_64-${PLATFORMFRAMEWORK}
DESTDIR=${BUILD_ROOT}/${PLATFORMFRAMEWORK}
progress_item "${FRAMEWORK} ${PLATFORM}"
TRAPZERR() { }
set +e; FRAMEWORK_LINK=`readlink ${FRAMEWORK1DIR}/${FRAMEWORK}`; set -e
TRAPZERR() { return $(( 128 + $1 )) }
ARCHIVE_PATH=${FRAMEWORK_LINK:-$FRAMEWORK}
FRAMEWORK1ARCHIVE=${FRAMEWORK1DIR}/${ARCHIVE_PATH}
FRAMEWORK2ARCHIVE=${FRAMEWORK2DIR}/${ARCHIVE_PATH}
DESTARCHIVE=${DESTDIR}/${ARCHIVE_PATH}
mkdir -p ${BUILD_ROOT}/${PLATFORM}
rm -rf ${DESTDIR}
cp -R ${FRAMEWORK1DIR} ${DESTDIR}
rm -f ${DESTARCHIVE}
lipo -create ${FRAMEWORK1ARCHIVE} ${FRAMEWORK2ARCHIVE} -output ${DESTARCHIVE}
# Merge the Modules directories
cp -R ${FRAMEWORK2DIR}/Modules/* ${DESTDIR}/Modules
)
lipo_swift_framework()
(
FRAMEWORK=$1
lipo_swift_framework_variant ${FRAMEWORK} apple-ios-macabi
lipo_swift_framework_variant ${FRAMEWORK} apple-ios-simulator
lipo_swift_framework_variant ${FRAMEWORK} apple-darwin
)
lipo_swift_frameworks()
(
progress_section "Building fat Swift frameworks"
lipo_swift_framework BCWally
)
build_swift_xcframework()
(
FRAMEWORK_NAME=$1
PLATFORM_FRAMEWORK_NAME=${FRAMEWORK_NAME}.framework
XC_FRAMEWORK_NAME=${FRAMEWORK_NAME}.xcframework
XC_FRAMEWORK_PATH=${BUILD_ROOT}/${XC_FRAMEWORK_NAME}
progress_item "${XC_FRAMEWORK_NAME}"
rm -rf ${XC_FRAMEWORK_PATH}
xcodebuild -create-xcframework \
-framework ${BUILD_ROOT}/arm64-apple-ios/${PLATFORM_FRAMEWORK_NAME} \
-framework ${BUILD_ROOT}/apple-darwin/${PLATFORM_FRAMEWORK_NAME} \
-framework ${BUILD_ROOT}/apple-ios-macabi/${PLATFORM_FRAMEWORK_NAME} \
-framework ${BUILD_ROOT}/apple-ios-simulator/${PLATFORM_FRAMEWORK_NAME} \
-output ${XC_FRAMEWORK_PATH}
# As of September 22, 2020, the step above is broken:
# it creates unusable XCFrameworks; missing files like Modules/CryptoBase.swiftmodule/Project/x86_64-apple-ios-simulator.swiftsourceinfo
# The frameworks we started with were fine. So we're going to brute-force replace the frameworks in the XCFramework with the originials.
rm -rf ${XC_FRAMEWORK_PATH}/ios-arm64/${PLATFORM_FRAMEWORK_NAME}
cp -R ${BUILD_ROOT}/arm64-apple-ios/${PLATFORM_FRAMEWORK_NAME} ${XC_FRAMEWORK_PATH}/ios-arm64/
rm -rf ${XC_FRAMEWORK_PATH}/ios-arm64_x86_64-maccatalyst/${PLATFORM_FRAMEWORK_NAME}
cp -R ${BUILD_ROOT}/apple-ios-macabi/${PLATFORM_FRAMEWORK_NAME} ${XC_FRAMEWORK_PATH}/ios-arm64_x86_64-maccatalyst/
rm -rf ${XC_FRAMEWORK_PATH}/ios-arm64_x86_64-simulator/${PLATFORM_FRAMEWORK_NAME}
cp -R ${BUILD_ROOT}/apple-ios-simulator/${PLATFORM_FRAMEWORK_NAME} ${XC_FRAMEWORK_PATH}/ios-arm64_x86_64-simulator/
rm -rf ${XC_FRAMEWORK_PATH}/macos-arm64_x86_64/${PLATFORM_FRAMEWORK_NAME}
cp -R ${BUILD_ROOT}/apple-darwin/${PLATFORM_FRAMEWORK_NAME} ${XC_FRAMEWORK_PATH}/macos-arm64_x86_64/
)
build_swift_xcframeworks()
(
progress_section "Building Swift XCFrameworks"
build_swift_xcframework BCWally
)
build_all()
(
CONTEXT=subshell
get_dependencies
build_c_libraries
build_swift_frameworks
lipo_swift_frameworks
build_swift_xcframeworks
)
TRAPZERR() {
if [[ ${CONTEXT} == "top" ]]
then
progress_error "Build error."
echo "Log tail:" >&3
tail -n 10 ${BUILD_LOG} >&3
fi
return $(( 128 + $1 ))
}
TRAPINT() {
if [[ ${CONTEXT} == "top" ]]
then
progress_error "Build stopped."
fi
return $(( 128 + $1 ))
}
(
CONTEXT=top
exec 3>/dev/tty
build_all
progress_success "Done!"
) >>&| ${BUILD_LOG}