-
Notifications
You must be signed in to change notification settings - Fork 66
/
build-headless-shell.sh
executable file
·274 lines (238 loc) · 6.12 KB
/
build-headless-shell.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
#!/bin/bash
SRC=$(realpath $(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd))
OUT=
SRCDIR=
CHANNEL=stable
ATTEMPTS=10
JOBS=$((`nproc` + 2))
JOBFAIL=30
DRYRUN=
TTL=64800
UPDATE=0
TARGETS=()
VERSION=
OPTIND=1
while getopts "o:s:c:a:j:k:nl:ut:v:" opt; do
case "$opt" in
o) OUT=$OPTARG ;;
s) SRCDIR=$OPTARG ;;
c) CHANNEL=$OPTARG ;;
a) ATTEMPTS=$OPTARG ;;
j) JOBS=$OPTARG ;;
k) JOBFAIL=$OPTARG ;;
n) DRYRUN=-n ;;
l) TTL=$OPTARG ;;
u) UPDATE=1 ;;
t) TARGETS+=($OPTARG) ;;
v) VERSION=$OPTARG ;;
esac
done
set -e
# determine targets
if [ ${#TARGETS[@]} -eq 0 ]; then
TARGETS=(amd64)
fi
# determine version
if [ -z "$VERSION" ]; then
VERSION=$(verhist -platform win64 -channel $CHANNEL -latest)
fi
# determine out dir
if [ -z "$OUT" ]; then
OUT=$(realpath "$SRC/out")
fi
# determine source dir
if [ -z "$SRCDIR" ]; then
if [ -d /media/src ]; then
SRCDIR=/media/src
else
SRCDIR=$OUT
fi
fi
# check source directory exists
if [ ! -d "$SRCDIR" ]; then
echo "ERROR: $SRCDIR does not exist!"
exit 1
fi
# create out dir
mkdir -p $OUT
# determine last update state
LAST=0
if [ -e $OUT/last ]; then
LAST=$(cat $OUT/last)
fi
if [ "$((`date +%s` - $LAST))" -gt $TTL ]; then
UPDATE=1
fi
echo "BUILD: $VERSION [${TARGETS[@]}] (u:$UPDATE j:$JOBS a:$ATTEMPTS)"
echo "SOURCE: $SRCDIR/chromium/src"
TMPDIR=$(mktemp -d -p /tmp headless-shell-$VERSION.XXXXX)
echo "TMPDIR: $TMPDIR"
# grab depot_tools
if [ ! -d $OUT/depot_tools ]; then
echo -e "\n\nRETRIEVING depot_tools ($(date))"
(set -x;
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $OUT/depot_tools
)
fi
# update to latest depot_tools
if [ "$UPDATE" -eq "1" ]; then
echo -e "\n\nUPDATING $OUT/depot_tools ($(date))"
(set -x;
git -C $OUT/depot_tools reset --hard
git -C $OUT/depot_tools checkout main
git -C $OUT/depot_tools pull
)
fi
# add depot_tools to path
export PATH=$OUT/depot_tools:$PATH
CHROMESRC=$SRCDIR/chromium/src
# retrieve chromium source tree
if [ ! -d $CHROMESRC ]; then
echo -e "\n\nRETRIEVING chromium -> $CHROMESRC ($(date))"
pushd $SRCDIR &> /dev/null
(set -x;
fetch --nohooks chromium
gclient runhooks
)
popd &> /dev/null
fi
useragent_files() {
find $CHROMESRC/headless -type f -iname \*.cc -print0 \
|xargs -r0 grep -EHi '"(Headless)?Chrome"' \
|awk -F: '{print $1}' \
|sed -e "s%^$CHROMESRC/%%" \
|sort \
|uniq
}
# update chromium source tree
if [ "$UPDATE" -eq "1" ]; then
echo -e "\n\nREBASING ($(date))"
USERAGENT_FILES=$(useragent_files)
(set -x;
git -C $CHROMESRC checkout $USERAGENT_FILES
git -C $CHROMESRC checkout main
git -C $CHROMESRC rebase-update
)
date +%s > $OUT/last
echo "LAST: $(cat $OUT/last) ($(date))"
fi
# determine sync status
SYNC=$UPDATE
if [ "$VERSION" != "$(git name-rev --tags --name-only $(git rev-parse HEAD))" ]; then
SYNC=1
fi
if [ "$SYNC" -eq "1" ]; then
echo -e "\n\nRESETTING $VERSION ($(date))"
# files in headless that contain the HeadlessChrome user-agent string
USERAGENT_FILES=$(useragent_files)
(set -x;
git -C $CHROMESRC checkout $USERAGENT_FILES
git -C $CHROMESRC checkout $VERSION
)
pushd $CHROMESRC &> /dev/null
(set -x;
gclient sync \
--with_branch_heads \
--with_tags \
--delete_unversioned_trees \
--reset
./build/linux/sysroot_scripts/install-sysroot.py --arch=arm64
)
# alter the user agent string
for f in $(useragent_files); do
perl -pi -e 's/"HeadlessChrome"/"Chrome"/' $f
done
popd &> /dev/null
fi
# build targets
for TARGET in ${TARGETS[@]}; do
NAME=headless-shell-$CHANNEL-$TARGET
PROJECT=$CHROMESRC/out/$NAME
mkdir -p $PROJECT
# generate build files
echo -e "\n\nGENERATING $TARGET $VERSION -> $PROJECT ($(date))"
EXTRA=
if [ "$TARGET" = "arm64" ]; then
EXTRA="target_cpu = \"arm64\""
fi
echo "import(\"//build/args/headless.gn\")
is_debug = false
is_official_build = true
symbol_level = 0
blink_symbol_level = 0
headless_use_prefs = true
chrome_pgo_phase = 0
$EXTRA
" > $PROJECT/args.gn
pushd $CHROMESRC &> /dev/null
(set -x;
gn gen ./out/$NAME
)
popd &> /dev/null
# build
RET=1
for i in $(seq 1 $ATTEMPTS); do
echo -e "\n\nSTARTING BUILD ATTEMPT $i FOR $TARGET $VERSION ($(date))"
RET=1
$OUT/depot_tools/ninja \
-j $JOBS \
-k $JOBFAIL \
$DRYRUN \
-C $PROJECT \
headless_shell && RET=$?
if [ $RET -eq 0 ]; then
echo "COMPLETED BUILD ATTEMPT $i FOR $TARGET $VERSION ($(date))"
break
fi
echo "BUILD ATTEMPT $i FOR $TARGET $VERSION FAILED ($(date))"
done
if [ $RET -ne 0 ]; then
echo -e "\n\nERROR: COULD NOT COMPLETE BUILD FOR $TARGET $VERSION, BUILD ATTEMPTS HAVE BEEN EXHAUSTED ($(date))"
exit 1
fi
# build stamp
echo $VERSION > $PROJECT/.stamp
done
# package
for TARGET in ${TARGETS[@]}; do
PROJECT=$CHROMESRC/out/headless-shell-$CHANNEL-$TARGET
WORKDIR=$TMPDIR/headless-shell
# strip
STRIP=strip
if [ "$TARGET" = "arm64" ]; then
STRIP=aarch64-linux-gnu-strip
fi
# copy files
mkdir -p $WORKDIR
(set -x;
cp -a $PROJECT/headless_shell $WORKDIR/headless-shell
cp -a $PROJECT/{.stamp,libEGL.so,libGLESv2.so,libvk_swiftshader.so,libvulkan.so.1,vk_swiftshader_icd.json} $WORKDIR
$STRIP $WORKDIR/headless-shell $WORKDIR/*.so{,.1}
chmod -x $WORKDIR/*.so{,.1}
du -s $WORKDIR/*
file $WORKDIR/headless-shell
)
if [ "$TARGET" = "amd64" ]; then
# verify headless-shell runs and reports correct version
$WORKDIR/headless-shell --remote-debugging-port=5000 &> /dev/null & PID=$!
sleep 1
UA=$(curl --silent --connect-timeout 5 http://localhost:5000/json/version|jq -r '.Browser')
kill -s SIGTERM $PID
set +e
wait $PID 2>/dev/null
set -e
if [ "$UA" != "Chrome/$VERSION" ]; then
echo -e "\n\nERROR: HEADLESS-SHELL REPORTED VERSION '$UA', NOT 'Chrome/$VERSION'! ($(date))"
exit 1
else
echo -e "\n\nHEADLESS SHELL REPORTED VERSION '$UA' ($(date))"
fi
fi
ARCHIVE=$OUT/headless-shell-$VERSION-$TARGET.tar.bz2
echo -e "\n\nPACKAGING $ARCHIVE ($(date))"
(set -x;
rm -f $ARCHIVE
tar -C $TMPDIR -cjf $ARCHIVE headless-shell
du -s $ARCHIVE
)
done