-
Notifications
You must be signed in to change notification settings - Fork 163
/
util.sh
497 lines (457 loc) · 15.6 KB
/
util.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# Detect host platform.
# Set PLATFORM environment variable to override default behavior.
# Supported platform types - 'linux', 'win', 'mac'
# 'msys' is the git bash shell, built using mingw-w64, running under Microsoft
# Windows.
function detect-platform() {
# set PLATFORM to android on linux host to build android
case "$OSTYPE" in
darwin*) PLATFORM=${PLATFORM:-mac} ;;
linux*) PLATFORM=${PLATFORM:-linux} ;;
win32*|msys*) PLATFORM=${PLATFORM:-win} ;;
*) echo "Building on unsupported OS: $OSTYPE"; exit 1; ;;
esac
}
# Sanity check for the host os/platform supporting cross-compilation with the
# target os.
# $1: The platform type.
# $2: The target os.
function check::platform() {
local platform="$1"
local target_os="$2"
if [[ $target_os = 'android' && $platform != 'linux' ]]; then
# Codifying the webrtc documentation for android compilation support for non-linux platforms
echo "Android compilation is only supported on Linux (https://webrtc.org/native-code/android/#getting-the-code)"
exit 1
fi
}
# This cleans the output directory.
# $1: The output directory.
function clean() {
local outdir="$1"
rm -rf $outdir/* $outdir/.gclient*
}
# Makes sure depot tools are present.
# $1: The platform type.
# $2: The depot tools url.
# $3: The depot tools directory.
function check::depot-tools() {
local platform="$1"
local depot_tools_url="$2"
local depot_tools_dir="$3"
if [ ! -d $depot_tools_dir ]; then
git clone -q $depot_tools_url $depot_tools_dir
if [ $platform = 'win' ]; then
# run gclient.bat to get python
pushd $depot_tools_dir >/dev/null
./gclient.bat
popd >/dev/null
fi
else
pushd $depot_tools_dir >/dev/null
git reset --hard -q
popd >/dev/null
fi
}
# Makes sure package is installed. Depends on sudo to be installed first.
# $1: The name of the package
# $2: Existence check binary. Defaults to name of the package.
function ensure-package() {
local name="$1"
local binary="${2:-$1}"
if ! which $binary > /dev/null ; then
sudo apt-get update -qq
sudo apt-get install -y $name
fi
}
# Makes sure all webrtcbuilds dependencies are present.
# $1: The platform type.
function check::webrtcbuilds::deps() {
local platform="$1"
case $platform in
mac)
which brew > /dev/null || (echo "Building with 'mac' requires Homebrew. Visit https://brew.sh"; exit 1)
# for GNU version of cp: gcp
which gcp > /dev/null || brew install coreutils
;;
linux)
if ! grep -v \# /etc/apt/sources.list | grep -q multiverse ; then
echo "*** Warning: The Multiverse repository is probably not enabled ***"
echo "*** which is required for things like msttcorefonts. ***"
fi
if ! which sudo > /dev/null ; then
apt-get update -qq
apt-get install -y sudo
fi
ensure-package curl
ensure-package git
ensure-package python
ensure-package lbzip2
ensure-package lsb-release lsb_release
;;
win)
VISUAL_STUDIO_TOOLS=${VS140COMNTOOLS:-}
if [ -z VISUAL_STUDIO_TOOLS ]; then
echo "Building with 'win' requires Microsoft Visual Studio 2015"
exit 1
fi
;;
esac
}
# Makes sure all WebRTC build dependencies are present.
# $1: The platform type.
function check::webrtc::deps() {
local platform="$1"
local outdir="$2"
local target_os="$3"
case $platform in
linux)
# Automatically accepts ttf-mscorefonts EULA
echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections
sudo $outdir/src/build/install-build-deps.sh --no-syms --no-arm --no-chromeos-fonts --no-nacl --no-prompt
;;
esac
if [ $target_os = 'android' ]; then
sudo $outdir/src/build/install-build-deps-android.sh
fi
}
# Checks out a specific revision
# $1: The target OS type.
# $2: The output directory.
# $3: Revision represented as a git SHA.
function checkout() {
local target_os="$1"
local outdir="$2"
local revision="$3"
pushd $outdir >/dev/null
local prev_target_os=$(cat $outdir/.webrtcbuilds_target_os 2>/dev/null)
if [[ -n "$prev_target_os" && "$target_os" != "$prev_target_os" ]]; then
echo The target OS has changed. Refetching sources for the new target OS
rm -rf src .gclient*
fi
# Fetch only the first-time, otherwise sync.
if [ ! -d src ]; then
case $target_os in
android)
yes | fetch --nohooks webrtc_android
;;
ios)
fetch --nohooks webrtc_ios
;;
*)
fetch --nohooks webrtc
;;
esac
fi
# Checkout the specific revision after fetch or for older branches, run
# setup_links.py to replace all directories which now must be symlinks then
# try again.
gclient sync --force --revision $revision ||
(test -f src/setup_links.py && src/setup_links.py --force --no-prompt && gclient sync --force --revision $revision)
# Cache the target OS
echo $target_os > $outdir/.webrtcbuilds_target_os
popd >/dev/null
}
# Patches a checkout for building static standalone libs
# $1: The platform type.
# $2: The output directory.
# $3: Enable RTTI or not. '0' or '1'.
function patch() {
local platform="$1"
local outdir="$2"
local rtti_enabled="$3"
pushd $outdir/src >/dev/null
# This removes the examples from being built.
sed -i.bak 's|"//webrtc/examples",|#"//webrtc/examples",|' BUILD.gn
# This configures whether to build with RTTI enabled or not.
[ "$rtti_enabled" = 1 ] && sed -i.bak \
's|"//build/config/compiler:no_rtti",|"//build/config/compiler:rtti",|' \
build/config/BUILDCONFIG.gn
popd >/dev/null
}
# This function compiles a single library using Microsoft Visual C++ for a
# Microsoft Windows (32/64-bit) target. This function is separate from the
# other compile functions because of differences using the Microsoft tools:
#
# The Microsoft Windows tools use different file extensions than the other tools:
# '.obj' as the object file extension, instead of '.o'
# '.lib' as the static library file extension, instead of '.a'
# '.dll' as the shared library file extension, instead of '.so'
#
# The Microsoft Windows tools have different names than the other tools:
# 'lib' as the librarian, instead of 'ar'. 'lib' must be found through the path
# variable $VS140COMNTOOLS.
#
# The Microsoft tools that target Microsoft Windows run only under
# Microsoft Windows, so the build and target systems are the same.
#
# $1 the output directory, 'Debug', 'Debug_x64', 'Release', or 'Release_x64'
# $2 additional gn arguments
function compile-win() {
local outputdir="$1"
local gn_args="$2"
gn gen $outputdir --args="$gn_args"
pushd $outputdir >/dev/null
ninja -C .
rm -f libwebrtc_full.lib
local objlist=$(strings .ninja_deps | grep -o '.*\.obj')
local blacklist="unittest_main.obj|video_capture_external.obj|\
device_info_external.obj"
echo "$objlist" | tr ' ' '\n' | grep -v -E $blacklist >libwebrtc_full.list
# On Windows, asm files are compiled to object files that have .obj and .o
# extensions.
local extras=$(find \
./obj/third_party/libvpx/libvpx_* \
./obj/third_party/libjpeg_turbo/simd_asm \
./obj/third_party/boringssl/boringssl_asm -name '*.o' -o -name '*.obj')
echo "$extras" | tr ' ' '\n' >>libwebrtc_full.list
"$VS140COMNTOOLS../../VC/bin/lib" /OUT:webrtc_full.lib @libwebrtc_full.list
popd >/dev/null
}
# This function compile and combine build artifact objects into one library.
# $1 the output directory, 'Debug', 'Release'
# $2 additional gn arguments
function compile-unix() {
local outputdir="$1"
local gn_args="$2"
local blacklist="unittest|examples|/yasm|protobuf_lite|main.o|\
video_capture_external.o|device_info_external.o"
gn gen $outputdir --args="$gn_args"
pushd $outputdir >/dev/null
ninja -C .
rm -f libwebrtc_full.a
# Produce an ordered objects list by parsing .ninja_deps for strings
# matching .o files.
local objlist=$(strings .ninja_deps | grep -o '.*\.o')
echo "$objlist" | tr ' ' '\n' | grep -v -E $blacklist >libwebrtc_full.list
# various intrinsics aren't included by default in .ninja_deps
local extras=$(find \
./obj/third_party/libvpx/libvpx_* \
./obj/third_party/libjpeg_turbo/simd_asm \
./obj/third_party/boringssl/boringssl_asm -name '*.o')
echo "$extras" | tr ' ' '\n' >>libwebrtc_full.list
# generate the archive
cat libwebrtc_full.list | xargs ar -crs libwebrtc_full.a
# generate an index list
ranlib libwebrtc_full.a
popd >/dev/null
}
# This compiles the library.
# $1: The platform type.
# $2: The output directory.
# $3: The target os for cross-compilation.
# $4: The target cpu for cross-compilation.
# $5: The build configurations.
# $6: Enable iterator debug or not. '0' or '1'.
function compile() {
local platform="$1"
local outdir="$2"
local target_os="$3"
local target_cpu="$4"
local configs="$5"
local disable_iterator_debug="$6"
local common_args="is_component_build=false rtc_include_tests=false treat_warnings_as_errors=false"
local target_args="target_os=\"$target_os\" target_cpu=\"$target_cpu\""
[ "$disable_iterator_debug" = 1 ] && common_args+=' enable_iterator_debugging=false'
pushd $outdir/src >/dev/null
for cfg in $configs; do
[ "$cfg" = 'Release' ] && common_args+=' is_debug=false'
case $platform in
win)
# 32-bit build
compile-win "out/$cfg" "$common_args $target_args"
# 64-bit build
GYP_DEFINES="target_arch=x64 $GYP_DEFINES"
compile-win "out/${cfg}_x64" "$common_args $target_args"
;;
*)
# On Linux when not cross-compiling, use clang = false and sysroot = false
# to build using gcc. Comment this out to use clang.
if [[ $platform = 'linux' && $target_os != 'android' && $target_cpu != amd* ]]; then
target_args+=" is_clang=false use_sysroot=false"
fi
# Debug builds are component builds (shared libraries) by default unless
# is_component_build=false is passed to gn gen --args. Release builds are
# static by default.
compile-unix "out/$cfg" "$common_args $target_args"
;;
esac
done
popd >/dev/null
}
# This prepares artifacts to be packaged.
# $1: The platform type.
# $2: The output directory.
# $3: The package filename.
# $4: The project's resource dirctory.
# $5: The build configurations.
# $6: : The revision number.
function package::prepare() {
local platform="$1"
local outdir="$2"
local package_filename="$3"
local resourcedir="$4"
local configs="$5"
local revision_number="$6"
if [ $platform = 'mac' ]; then
CP='gcp'
else
CP='cp'
fi
pushd $outdir >/dev/null
# create directory structure
mkdir -p $package_filename/include $package_filename/lib
for cfg in $configs; do
mkdir -p $package_filename/lib/$cfg
done
# find and copy header files
# copy all non third-party header files first then copy only the third-party
# header files that are required such as abseil-cpp for optional.h
pushd src >/dev/null
local headersSourceDir=webrtc
local headersDestDir=$outdir/$package_filename/include
# Revision 19846 is the following, where upstream moved src/webrtc to src/
# https://webrtc.googlesource.com/src/+/92ea95e34af5966555903026f45164afbd7e2088
if [[ $revision_number -ge 19846 ]]; then
headersSourceDir=.
fi
find $headersSourceDir -path './third_party*' -prune -o -name '*.h' -exec $CP --parents '{}' $headersDestDir ';'
# Revision 23941 introduced absl::optional from abseil-cpp
if [[ $revision_number -ge 23941 ]]; then
pushd $headersSourceDir/third_party/abseil-cpp >/dev/null
find . -name '*.h' -exec $CP --parents '{}' $headersDestDir ';'
popd >/dev/null
fi
popd >/dev/null
# find and copy libraries
pushd src/out >/dev/null
find . -maxdepth 3 \( -name '*.so' -o -name '*.dll' -o -name '*webrtc_full*' -o -name *.jar \) \
-exec $CP --parents '{}' $outdir/$package_filename/lib ';'
popd >/dev/null
# for linux, add pkgconfig files
if [ $platform = 'linux' ]; then
for cfg in $configs; do
mkdir -p $package_filename/lib/$cfg/pkgconfig
CONFIG=$cfg envsubst '$CONFIG' < $resourcedir/pkgconfig/libwebrtc_full.pc.in > \
$package_filename/lib/$cfg/pkgconfig/libwebrtc_full.pc
done
fi
popd >/dev/null
}
# This packages a compiled build into a zip file in the output directory.
# $1: The platform type.
# $2: The output directory.
# $3: The package filename.
function package::zip() {
local platform="$1"
local outdir="$2"
local package_filename="$3"
pushd $outdir >/dev/null
# remove first for cleaner builds
rm -f $package_filename.zip
# zip up the package
if [ $platform = 'win' ]; then
$DEPOT_TOOLS/win_toolchain/7z/7z.exe a -tzip $package_filename.zip $package_filename
else
zip -r $package_filename.zip $package_filename >/dev/null
fi
popd >/dev/null
}
# This packages into a debian package in the output directory.
# $1: The output directory.
# $2: The package filename.
# $3: The package name.
# $4: The package version.
# $5: The architecture.
function package::debian() {
local outdir="$1"
local package_filename="$2"
local package_name="$3"
local package_version="$4"
local arch="$5"
local debianize="debianize/$package_filename"
echo "Debianize WebRTC"
pushd $outdir >/dev/null
mkdir -p $debianize/DEBIAN
mkdir -p $debianize/opt
mv $package_filename $debianize/opt/webrtc
cat << EOF > $debianize/DEBIAN/control
Package: $package_name
Architecture: $arch
Maintainer: webrtcbuilds manintainers
Depends: debconf (>= 0.5.00)
Priority: optional
Version: $package_version
Description: webrtc static library
This package provides webrtc library generated with webrtcbuilds
EOF
fakeroot dpkg-deb --build $debianize
mv debianize/*.deb .
rm -rf debianize
popd >/dev/null
}
# This returns the latest revision from the git repo.
# $1: The git repo URL
function latest-rev() {
local repo_url="$1"
git ls-remote $repo_url HEAD | cut -f1
}
# This returns the associated revision number for a given git sha revision
# $1: The git repo URL
# $2: The revision git sha string
function revision-number() {
local repo_url="$1"
local revision="$2"
# This says curl the revision log with text format, base64 decode it using
# openssl since its more portable than just 'base64', take the last line which
# contains the commit revision number and output only the matching {#nnn} part
openssl base64 -d -A <<< $(curl --silent $repo_url/+/$revision?format=TEXT) \
| tail -1 | egrep -o '{#([0-9]+)}' | tr -d '{}#'
}
# This returns a short revision sha.
# $1: The revision string
function short-rev() {
local revision="$1"
echo $revision | cut -c -7
}
# This returns a short revision sha.
# $1: The target cpu for cross-compilation.
function debian-arch() {
local target_cpu="$1"
# set PLATFORM to android on linux host to build android
case "$target_cpu" in
x86*) echo "i386" ;;
x64*) echo "amd64" ;;
*) echo "$target_cpu" ;;
esac
}
# This interprets a pattern and returns the interpreted one.
# $1: The pattern.
# $2: The output directory.
# $3: The platform type.
# $4: The target os for cross-compilation.
# $5: The target cpu for cross-compilation.
# $6: The branch.
# $7: The revision.
# $8: The revision number.
function interpret-pattern() {
local pattern="$1"
local platform="$2"
local outdir="$3"
local target_os="$4"
local target_cpu="$5"
local branch="$6"
local revision="$7"
local revision_number="$8"
local debian_arch="$(debian-arch $target_cpu)"
local short_revision="$(short-rev $revision)"
pattern=${pattern//%p%/$platform}
pattern=${pattern//%to%/$target_os}
pattern=${pattern//%tc%/$target_cpu}
pattern=${pattern//%b%/$branch}
pattern=${pattern//%r%/$revision}
pattern=${pattern//%rn%/$revision_number}
pattern=${pattern//%da%/$debian_arch}
pattern=${pattern//%sr%/$short_revision}
echo "$pattern"
}