forked from stasel/WebRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
216 lines (176 loc) · 8.02 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
#!/bin/sh
## WebRTC library build script
## Created by Stasel
## BSD-3 License
##
## Example usage: MACOS=true IOS=true BUILD_VP9=true PATCH=false REMOVE_OUT=false sh build.sh
# Configs
DEBUG="${DEBUG:-false}"
BUILD_VP9="${BUILD_VP9:-false}"
BRANCH="${BRANCH:-master}"
COMMIT="${COMMIT:-832ce5eae6}"
PATCH="${PATCH:-true}"
REMOVE_OUT="${REMOVE_OUT:-true}"
IOS="${IOS:-false}"
MACOS="${MACOS:-false}"
MAC_CATALYST="${MAC_CATALYST:-false}"
OUTPUT_DIR="./out"
XCFRAMEWORK_DIR="out/WebRTC.xcframework"
COMMON_GN_ARGS="is_debug=${DEBUG} rtc_libvpx_build_vp9=${BUILD_VP9} is_component_build=false rtc_include_tests=false rtc_enable_objc_symbol_export=true enable_stripping=true enable_dsyms=false use_lld=true"
PLISTBUDDY_EXEC="/usr/libexec/PlistBuddy"
build_iOS() {
local arch=$1
local environment=$2
local gen_dir="${OUTPUT_DIR}/ios-${arch}-${environment}"
local gen_args="${COMMON_GN_ARGS} target_cpu=\"${arch}\" target_os=\"ios\" target_environment=\"${environment}\" ios_deployment_target=\"12.0\" ios_enable_code_signing=false"
gn gen "${gen_dir}" --args="${gen_args}"
ninja -C "${gen_dir}" framework_objc || exit 1
}
build_macOS() {
local arch=$1
local gen_dir="${OUTPUT_DIR}/macos-${arch}"
local gen_args="${COMMON_GN_ARGS} target_cpu=\"${arch}\" target_os=\"mac\""
gn gen "${gen_dir}" --args="${gen_args}"
ninja -C "${gen_dir}" mac_framework_objc || exit 1
}
# Catalyst builds are not working properly yet.
# See: https://groups.google.com/g/discuss-webrtc/c/VZXS4V4mSY4
build_catalyst() {
local arch=$1
local gen_dir="${OUTPUT_DIR}/catalyst-${arch}"
local gen_args="${COMMON_GN_ARGS} target_cpu=\"${arch}\" target_environment=\"catalyst\" target_os=\"ios\" ios_deployment_target=\"14.0\" ios_enable_code_signing=false"
gn gen "${gen_dir}" --args="${gen_args}"
ninja -C "${gen_dir}" framework_objc || exit 1
}
plist_add_library() {
local index=$1
local identifier=$2
local platform=$3
local platform_variant=$4
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries: dict" "${INFO_PLIST}"
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries:${index}:LibraryIdentifier string ${identifier}" "${INFO_PLIST}"
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries:${index}:LibraryPath string WebRTC.framework" "${INFO_PLIST}"
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries:${index}:SupportedArchitectures array" "${INFO_PLIST}"
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries:${index}:SupportedPlatform string ${platform}" "${INFO_PLIST}"
if [ ! -z "$platform_variant" ]; then
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries:${index}:SupportedPlatformVariant string ${platform_variant}" "${INFO_PLIST}"
fi
}
plist_add_architecture() {
local index=$1
local arch=$2
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries:${index}:SupportedArchitectures: string ${arch}" "${INFO_PLIST}"
}
# Step 1: Download and install depot tools
if [ ! -d depot_tools ]; then
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
else
cd depot_tools
git pull origin main
cd ..
fi
export PATH=$(pwd)/depot_tools:$PATH
# Step 2 - Download and build WebRTC
if [ ! -d src ]; then
fetch --nohooks webrtc_ios
fi
cd src
git fetch --all
git checkout $BRANCH
#git checkout --detach $COMMIT
cd ..
gclient sync --with_branch_heads --with_tags
cd src
# Step 2.5 - Apply patches
# sed -i '' 's/"-mllvm",$/# "-mllvm",/g' ./build/config/compiler/BUILD.gn
# sed -i '' 's/"-instcombine-lower-dbg-declare=0",$/# "-instcombine-lower-dbg-declare=0",/g' ./build/config/compiler/BUILD.gn
if [ "$PATCH" = true ]; then
git apply ../patches/*.patch
fi
# Step 3 - Compile and build all frameworks
if [ "$REMOVE_OUT" = true ]; then
rm -rf $OUTPUT_DIR
fi
if [ "$IOS" = true ]; then
build_iOS "x64" "simulator"
build_iOS "arm64" "simulator"
build_iOS "arm64" "device"
fi
if [ "$MACOS" = true ]; then
build_macOS "x64"
build_macOS "arm64"
fi
if [ "$MAC_CATALYST" = true ]; then
build_catalyst "x64"
build_catalyst "arm64"
fi
# Step 4 - Manually create XCFramework.
# Unfortunately we cannot use xcodebuild `-xcodebuild -create-xcframework` because of an error:
# "Both ios-arm64-simulator and ios-x86_64-simulator represent two equivalent library definitions."
# Therefore, we craft the XCFramework manually with multi architecture binaries created by lipo.
# We also use plistbuddy to create the plist for the XCFramework
INFO_PLIST="${XCFRAMEWORK_DIR}/Info.plist"
rm -rf "${XCFRAMEWORK_DIR}"
mkdir "${XCFRAMEWORK_DIR}"
"$PLISTBUDDY_EXEC" -c "Add :CFBundlePackageType string XFWK" "${INFO_PLIST}"
"$PLISTBUDDY_EXEC" -c "Add :XCFrameworkFormatVersion string 1.0" "${INFO_PLIST}"
"$PLISTBUDDY_EXEC" -c "Add :AvailableLibraries array" "${INFO_PLIST}"
# Step 5.1 - Add iOS libs to XCFramework
LIB_COUNT=0
if [[ "$IOS" = true ]]; then
IOS_LIB_IDENTIFIER="ios-arm64"
IOS_SIM_LIB_IDENTIFIER="ios-x86_64_arm64-simulator"
mkdir "${XCFRAMEWORK_DIR}/${IOS_LIB_IDENTIFIER}"
mkdir "${XCFRAMEWORK_DIR}/${IOS_SIM_LIB_IDENTIFIER}"
LIB_IOS_INDEX=0
LIB_IOS_SIMULATOR_INDEX=1
plist_add_library $LIB_IOS_INDEX $IOS_LIB_IDENTIFIER "ios"
plist_add_library $LIB_IOS_SIMULATOR_INDEX $IOS_SIM_LIB_IDENTIFIER "ios" "simulator"
cp -r out/ios-arm64-device/WebRTC.framework "${XCFRAMEWORK_DIR}/${IOS_LIB_IDENTIFIER}"
cp -r out/ios-x64-simulator/WebRTC.framework "${XCFRAMEWORK_DIR}/${IOS_SIM_LIB_IDENTIFIER}"
LIPO_IOS_FLAGS="out/ios-arm64-device/WebRTC.framework/WebRTC"
LIPO_IOS_SIM_FLAGS="out/ios-x64-simulator/WebRTC.framework/WebRTC out/ios-arm64-simulator/WebRTC.framework/WebRTC"
plist_add_architecture $LIB_IOS_INDEX "arm64"
plist_add_architecture $LIB_IOS_SIMULATOR_INDEX "arm64"
plist_add_architecture $LIB_IOS_SIMULATOR_INDEX "x86_64"
lipo -create -output "${XCFRAMEWORK_DIR}/${IOS_LIB_IDENTIFIER}/WebRTC.framework/WebRTC" ${LIPO_IOS_FLAGS}
lipo -create -output "${XCFRAMEWORK_DIR}/${IOS_SIM_LIB_IDENTIFIER}/WebRTC.framework/WebRTC" ${LIPO_IOS_SIM_FLAGS}
# codesign simulator framework for local development.
# This makes it possible for Swift Packages to run Unit Tests and show SwiftUI Previews.
xcrun codesign -s - "${XCFRAMEWORK_DIR}/${IOS_SIM_LIB_IDENTIFIER}/WebRTC.framework/WebRTC"
LIB_COUNT=$((LIB_COUNT+2))
fi
# Step 5.2 - Add macOS libs to XCFramework
if [ "$MACOS" = true ]; then
MAC_LIB_IDENTIFIER="macos-x86_64_arm64"
mkdir "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}"
plist_add_library $LIB_COUNT "${MAC_LIB_IDENTIFIER}" "macos"
plist_add_architecture $LIB_COUNT "x86_64"
plist_add_architecture $LIB_COUNT "arm64"
cp -RP out/macos-x64/WebRTC.framework "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}"
lipo -create -output "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}/WebRTC.framework/Versions/A/WebRTC" out/macos-x64/WebRTC.framework/WebRTC out/macos-arm64/WebRTC.framework/WebRTC
LIB_COUNT=$((LIB_COUNT+1))
fi
# Step 5.3 - macOS catalyst libs to XCFramework
if [ "$MAC_CATALYST" = true ]; then
CATALYST_LIB_IDENTIFIER="ios-x86_64_arm64-maccatalyst"
mkdir "${XCFRAMEWORK_DIR}/${CATALYST_LIB_IDENTIFIER}"
plist_add_library $LIB_COUNT "${CATALYST_LIB_IDENTIFIER}" "ios" "maccatalyst"
plist_add_architecture $LIB_COUNT "x86_64"
plist_add_architecture $LIB_COUNT "arm64"
cp -RP out/catalyst-x64/WebRTC.framework "${XCFRAMEWORK_DIR}/${CATALYST_LIB_IDENTIFIER}"
lipo -create -output "${XCFRAMEWORK_DIR}/${CATALYST_LIB_IDENTIFIER}/WebRTC.framework/Versions/A/WebRTC" out/catalyst-x64/WebRTC.framework/WebRTC out/catalyst-arm64/WebRTC.framework/WebRTC
LIB_COUNT=$((LIB_COUNT+1))
fi
# Step 6 - Add license file to the framework
cp LICENSE ${XCFRAMEWORK_DIR}
# Step 7 - archive the framework
cd out
NOW=$(date -u +"%Y-%m-%dT%H-%M-%S")
OUTPUT_NAME=WebRTC-$NOW.xcframework.zip
zip --symlinks -r $OUTPUT_NAME WebRTC.xcframework/
# Step 8 calculate SHA256 checksum
CHECKSUM=$(shasum -a 256 $OUTPUT_NAME | awk '{ print $1 }')
COMMIT_HASH=$(git rev-parse HEAD)
echo "{ \"file\": \"${OUTPUT_NAME}\", \"checksum\": \"${CHECKSUM}\", \"commit\": \"${COMMIT_HASH}\", \"branch\": \"${BRANCH}\" }" > metadata.json
cat metadata.json