-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathbuild.sh
More file actions
230 lines (186 loc) · 9.25 KB
/
Copy pathbuild.sh
File metadata and controls
230 lines (186 loc) · 9.25 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
#!/bin/sh
## WebRTC library build script
## Created by Stasel
## BSD-3 License
##
## Example usage (from the repository root): BRANCH=branch-heads/7727 MACOS=true IOS=true sh scripts/build.sh
# Configs
DEBUG="${DEBUG:-false}"
BRANCH="${BRANCH:-main}"
IOS="${IOS:-false}"
MACOS="${MACOS:-false}"
MAC_CATALYST="${MAC_CATALYST:-false}"
ROOT_DIR="$(pwd)"
OUTPUT_DIR="${ROOT_DIR}/out"
XCFRAMEWORK_DIR="${OUTPUT_DIR}/WebRTC.xcframework"
COMMON_GN_ARGS="is_debug=${DEBUG} rtc_libvpx_build_vp9=true is_component_build=false rtc_include_tests=false rtc_enable_objc_symbol_export=true enable_stripping=true enable_dsyms=false use_lld=true rtc_ios_use_opengl_rendering=true rtc_system_openh264=true rtc_use_h265=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}"
gn args --list ${gen_dir} > ${gen_dir}/gn-args.txt
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}"
gn args --list ${gen_dir} > ${gen_dir}/gn-args.txt
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
# Must link Catalyst with Apple's linker instead of lld (use_lld=false)
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 use_lld=false"
gn gen "${gen_dir}" --args="${gen_args}"
gn args --list ${gen_dir} > ${gen_dir}/gn-args.txt
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}"
}
fix_privacy_manifest() {
local framework=$1
local nested="${framework}/Versions/A/Versions"
if [ -f "${nested}/A/Resources/PrivacyInfo.xcprivacy" ]; then
mv "${nested}/A/Resources/PrivacyInfo.xcprivacy" "${framework}/Versions/A/Resources/" || exit 1
rm -rf "${nested}"
fi
}
# 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 || exit 1
fi
cd src
git fetch --all || exit 1
git checkout "$BRANCH" || exit 1
cd ..
gclient sync --with_branch_heads --with_tags || exit 1
# Step 2.5 - Temp patch for macOS arm64 builds
bash "${ROOT_DIR}/scripts/patches/disable_apple_linker.sh" "${ROOT_DIR}/src/build/toolchain/apple/toolchain.gni" || exit 1
cd src
# Step 3 - Compile and build all frameworks
rm -rf $OUTPUT_DIR
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 "${OUTPUT_DIR}/ios-arm64-device/WebRTC.framework" "${XCFRAMEWORK_DIR}/${IOS_LIB_IDENTIFIER}"
cp -r "${OUTPUT_DIR}/ios-x64-simulator/WebRTC.framework" "${XCFRAMEWORK_DIR}/${IOS_SIM_LIB_IDENTIFIER}"
LIPO_IOS_FLAGS="${OUTPUT_DIR}/ios-arm64-device/WebRTC.framework/WebRTC"
LIPO_IOS_SIM_FLAGS="${OUTPUT_DIR}/ios-x64-simulator/WebRTC.framework/WebRTC ${OUTPUT_DIR}/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 "${OUTPUT_DIR}/macos-x64/WebRTC.framework" "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}"
# The generated macOS framework bundle contains only the umbrella header:
# since M141 the other public headers are left behind in the intermediate
# gen/ directory and never staged into the bundle, which makes the
# framework unusable (https://github.com/stasel/WebRTC/issues/132).
# Copy them in until this is fixed upstream.
cp "${OUTPUT_DIR}/macos-x64/gen/sdk/WebRTC.framework/Headers/"*.h "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}/WebRTC.framework/Versions/A/Headers/" || exit 1
fix_privacy_manifest "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}/WebRTC.framework"
lipo -create -output "${XCFRAMEWORK_DIR}/${MAC_LIB_IDENTIFIER}/WebRTC.framework/Versions/A/WebRTC" "${OUTPUT_DIR}/macos-x64/WebRTC.framework/WebRTC" "${OUTPUT_DIR}/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 "${OUTPUT_DIR}/catalyst-x64/WebRTC.framework" "${XCFRAMEWORK_DIR}/${CATALYST_LIB_IDENTIFIER}"
fix_privacy_manifest "${XCFRAMEWORK_DIR}/${CATALYST_LIB_IDENTIFIER}/WebRTC.framework"
lipo -create -output "${XCFRAMEWORK_DIR}/${CATALYST_LIB_IDENTIFIER}/WebRTC.framework/Versions/A/WebRTC" "${OUTPUT_DIR}/catalyst-x64/WebRTC.framework/WebRTC" "${OUTPUT_DIR}/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 "${OUTPUT_DIR}"
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 -C ${ROOT_DIR}/src rev-parse HEAD)
echo "{ \"file\": \"${OUTPUT_NAME}\", \"checksum\": \"${CHECKSUM}\", \"commit\": \"${COMMIT_HASH}\", \"branch\": \"${BRANCH}\" }" > metadata.json
cat metadata.json