-
Notifications
You must be signed in to change notification settings - Fork 198
/
build.sh
executable file
·463 lines (401 loc) · 19.3 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
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
#!/bin/bash
# Docker build script
# Copyright (c) 2017 Julian Xhokaxhiu
# Copyright (C) 2017-2018 Nicola Corna <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -eEuo pipefail
repo_log="$LOGS_DIR/repo-$(date +%Y%m%d).log"
# cd to working directory
cd "$SRC_DIR"
if [ -f /root/userscripts/begin.sh ]; then
echo ">> [$(date)] Running begin.sh"
/root/userscripts/begin.sh || echo ">> [$(date)] Warning: begin.sh failed!"
fi
# If requested, clean the OUT dir in order to avoid clutter
if [ "$CLEAN_OUTDIR" = true ]; then
echo ">> [$(date)] Cleaning '$ZIP_DIR'"
rm -rf "${ZIP_DIR:?}/"*
fi
# Treat DEVICE_LIST as DEVICE_LIST_<first_branch>
first_branch=$(cut -d ',' -f 1 <<< "$BRANCH_NAME")
if [ -n "$DEVICE_LIST" ]; then
device_list_first_branch="DEVICE_LIST_${first_branch//[^[:alnum:]]/_}"
device_list_first_branch=${device_list_first_branch^^}
read -r "${device_list_first_branch?}" <<< "$DEVICE_LIST,${!device_list_first_branch:-}"
fi
# If needed, migrate from the old SRC_DIR structure
if [ -d "$SRC_DIR/.repo" ]; then
branch_dir=$(repo info -o | sed -ne 's/Manifest branch: refs\/heads\///p' | sed 's/[^[:alnum:]]/_/g')
branch_dir=${branch_dir^^}
echo ">> [$(date)] WARNING: old source dir detected, moving source from \"\$SRC_DIR\" to \"\$SRC_DIR/$branch_dir\""
if [ -d "$branch_dir" ] && [ -z "$(ls -A "$branch_dir")" ]; then
echo ">> [$(date)] ERROR: $branch_dir already exists and is not empty; aborting"
fi
mkdir -p "$branch_dir"
find . -maxdepth 1 ! -name "$branch_dir" ! -path . -exec mv {} "$branch_dir" \;
fi
jobs_arg=()
if [ -n "${PARALLEL_JOBS-}" ]; then
if [[ "$PARALLEL_JOBS" =~ ^[1-9][0-9]*$ ]]; then
jobs_arg+=( "-j$PARALLEL_JOBS" )
else
echo "PARALLEL_JOBS is not a positive number: $PARALLEL_JOBS"
exit 1
fi
fi
retry_fetches_arg=()
if [ -n "${RETRY_FETCHES-}" ]; then
if [[ "$RETRY_FETCHES" =~ ^[1-9][0-9]*$ ]]; then
retry_fetches_arg+=( "--retry-fetches=$RETRY_FETCHES" )
else
echo "RETRY_FETCHES is not a positive number: $RETRY_FETCHES"
exit 1
fi
fi
if [ "$LOCAL_MIRROR" = true ]; then
cd "$MIRROR_DIR"
if [ ! -d .repo ]; then
echo ">> [$(date)] Initializing mirror repository" | tee -a "$repo_log"
( yes||: ) | repo init -u https://github.com/LineageOS/mirror --mirror --no-clone-bundle -p linux --git-lfs &>> "$repo_log"
fi
# Copy local manifests to the appropriate folder in order take them into consideration
echo ">> [$(date)] Copying '$LMANIFEST_DIR/*.xml' to '.repo/local_manifests/'"
mkdir -p .repo/local_manifests
rsync -a --delete --include '*.xml' --exclude '*' "$LMANIFEST_DIR/" .repo/local_manifests/
rm -f .repo/local_manifests/proprietary.xml
if [ "$INCLUDE_PROPRIETARY" = true ]; then
wget -q -O .repo/local_manifests/proprietary.xml "https://raw.githubusercontent.com/TheMuppets/manifests/mirror/default.xml"
/root/build_manifest.py --remote "https://gitlab.com" --remotename "gitlab_https" \
"https://gitlab.com/the-muppets/manifest/raw/mirror/default.xml" .repo/local_manifests/proprietary_gitlab.xml
fi
echo ">> [$(date)] Syncing mirror repository" | tee -a "$repo_log"
repo sync "${jobs_arg[@]}" "${retry_fetches_arg[@]}" --force-sync --no-clone-bundle &>> "$repo_log"
fi
for branch in ${BRANCH_NAME//,/ }; do
branch_dir=${branch//[^[:alnum:]]/_}
branch_dir=${branch_dir^^}
device_list_cur_branch="DEVICE_LIST_$branch_dir"
devices=${!device_list_cur_branch}
if [ -n "$branch" ] && [ -n "$devices" ]; then
vendor=lineage
apps_permissioncontroller_patch=""
modules_permission_patch=""
case "$branch" in
cm-14.1*)
vendor="cm"
themuppets_branch="cm-14.1"
android_version="7.1.2"
frameworks_base_patch="android_frameworks_base-N.patch"
;;
lineage-15.1*)
themuppets_branch="lineage-15.1"
android_version="8.1"
frameworks_base_patch="android_frameworks_base-O.patch"
;;
lineage-16.0*)
themuppets_branch="lineage-16.0"
android_version="9"
frameworks_base_patch="android_frameworks_base-P.patch"
;;
lineage-17.1*)
themuppets_branch="lineage-17.1"
android_version="10"
frameworks_base_patch="android_frameworks_base-Q.patch"
;;
lineage-18.1*)
themuppets_branch="lineage-18.1"
android_version="11"
frameworks_base_patch="android_frameworks_base-R.patch"
apps_permissioncontroller_patch="packages_apps_PermissionController-R.patch"
;;
lineage-19.1*)
themuppets_branch="lineage-19.1"
android_version="12"
frameworks_base_patch="android_frameworks_base-S.patch"
modules_permission_patch="packages_modules_Permission-S.patch"
;;
lineage-20.0*)
themuppets_branch="lineage-20.0"
android_version="13"
frameworks_base_patch="android_frameworks_base-Android13.patch"
modules_permission_patch="packages_modules_Permission-Android13.patch"
;;
*)
echo ">> [$(date)] Building branch $branch is not (yet) suppported"
exit 1
;;
esac
android_version_major=$(cut -d '.' -f 1 <<< $android_version)
mkdir -p "$SRC_DIR/$branch_dir"
cd "$SRC_DIR/$branch_dir"
echo ">> [$(date)] Branch: $branch"
echo ">> [$(date)] Devices: $devices"
# Remove previous changes of vendor/cm, vendor/lineage and frameworks/base (if they exist)
# TODO: maybe reset everything using https://source.android.com/setup/develop/repo#forall
for path in "vendor/cm" "vendor/lineage" "frameworks/base" "packages/apps/PermissionController" "packages/modules/Permission"; do
if [ -d "$path" ]; then
cd "$path"
git reset -q --hard
git clean -q -fd
cd "$SRC_DIR/$branch_dir"
fi
done
echo ">> [$(date)] (Re)initializing branch repository" | tee -a "$repo_log"
if [ "$LOCAL_MIRROR" = true ]; then
( yes||: ) | repo init -u https://github.com/LineageOS/android.git --reference "$MIRROR_DIR" -b "$branch" --git-lfs &>> "$repo_log"
else
( yes||: ) | repo init -u https://github.com/LineageOS/android.git -b "$branch" --git-lfs &>> "$repo_log"
fi
# Copy local manifests to the appropriate folder in order take them into consideration
echo ">> [$(date)] Copying '$LMANIFEST_DIR/*.xml' to '.repo/local_manifests/'"
mkdir -p .repo/local_manifests
rsync -a --delete --include '*.xml' --exclude '*' "$LMANIFEST_DIR/" .repo/local_manifests/
rm -f .repo/local_manifests/proprietary.xml
if [ "$INCLUDE_PROPRIETARY" = true ]; then
wget -q -O .repo/local_manifests/proprietary.xml "https://raw.githubusercontent.com/TheMuppets/manifests/$themuppets_branch/muppets.xml"
/root/build_manifest.py --remote "https://gitlab.com" --remotename "gitlab_https" \
"https://gitlab.com/the-muppets/manifest/raw/$themuppets_branch/muppets.xml" .repo/local_manifests/proprietary_gitlab.xml
fi
echo ">> [$(date)] Syncing branch repository" | tee -a "$repo_log"
builddate=$(date +%Y%m%d)
repo sync "${jobs_arg[@]}" "${retry_fetches_arg[@]}" -c --force-sync &>> "$repo_log"
if [ ! -d "vendor/$vendor" ]; then
echo ">> [$(date)] Missing \"vendor/$vendor\", aborting"
exit 1
fi
# Set up our overlay
mkdir -p "vendor/$vendor/overlay/microg/"
sed -i "1s;^;PRODUCT_PACKAGE_OVERLAYS := vendor/$vendor/overlay/microg\n;" "vendor/$vendor/config/common.mk"
makefile_containing_version="vendor/$vendor/config/common.mk"
if [ -f "vendor/$vendor/config/version.mk" ]; then
makefile_containing_version="vendor/$vendor/config/version.mk"
fi
los_ver_major=$(sed -n -e 's/^\s*PRODUCT_VERSION_MAJOR = //p' "$makefile_containing_version")
los_ver_minor=$(sed -n -e 's/^\s*PRODUCT_VERSION_MINOR = //p' "$makefile_containing_version")
los_ver="$los_ver_major.$los_ver_minor"
# If needed, apply the microG's signature spoofing patch
if [ "$SIGNATURE_SPOOFING" = "yes" ] || [ "$SIGNATURE_SPOOFING" = "restricted" ]; then
# Determine which patch should be applied to the current Android source tree
cd frameworks/base
if [ "$SIGNATURE_SPOOFING" = "yes" ]; then
echo ">> [$(date)] Applying the standard signature spoofing patch ($frameworks_base_patch) to frameworks/base"
echo ">> [$(date)] WARNING: the standard signature spoofing patch introduces a security threat"
patch --quiet --force -p1 -i "/root/signature_spoofing_patches/$frameworks_base_patch"
else
echo ">> [$(date)] Applying the restricted signature spoofing patch (based on $frameworks_base_patch) to frameworks/base"
sed 's/android:protectionLevel="dangerous"/android:protectionLevel="signature|privileged"/' "/root/signature_spoofing_patches/$frameworks_base_patch" | patch --quiet --force -p1
fi
git clean -q -f
cd ../..
if [ -n "$apps_permissioncontroller_patch" ] && [ "$SIGNATURE_SPOOFING" = "yes" ]; then
cd packages/apps/PermissionController
echo ">> [$(date)] Applying the apps/PermissionController patch ($apps_permissioncontroller_patch) to packages/apps/PermissionController"
patch --quiet --force -p1 -i "/root/signature_spoofing_patches/$apps_permissioncontroller_patch"
git clean -q -f
cd ../../..
fi
if [ -n "$modules_permission_patch" ] && [ "$SIGNATURE_SPOOFING" = "yes" ]; then
cd packages/modules/Permission
echo ">> [$(date)] Applying the modules/Permission patch ($modules_permission_patch) to packages/modules/Permission"
patch --quiet --force -p1 -i "/root/signature_spoofing_patches/$modules_permission_patch"
git clean -q -f
cd ../../..
fi
# Override device-specific settings for the location providers
mkdir -p "vendor/$vendor/overlay/microg/frameworks/base/core/res/res/values/"
cp /root/signature_spoofing_patches/frameworks_base_config.xml "vendor/$vendor/overlay/microg/frameworks/base/core/res/res/values/config.xml"
fi
echo ">> [$(date)] Setting \"$RELEASE_TYPE\" as release type"
sed -i "/\$(filter .*\$(${vendor^^}_BUILDTYPE)/,/endif/d" "$makefile_containing_version"
# Set a custom updater URI if a OTA URL is provided
echo ">> [$(date)] Adding OTA URL overlay (for custom URL $OTA_URL)"
if [ -n "$OTA_URL" ]; then
if [ -d "packages/apps/Updater/app/src/main/res/values" ]; then
# "New" Updater project structure
updater_values_dir="packages/apps/Updater/app/src/main/res/values"
elif [ -d "packages/apps/Updater/res/values" ]; then
# "Old" Updater project structure
updater_values_dir="packages/apps/Updater/res/values"
else
echo ">> [$(date)] ERROR: no 'values' dir of Updater app found"
exit 1
fi
updater_url_overlay_dir="vendor/$vendor/overlay/microg/${updater_values_dir}/"
mkdir -p "$updater_url_overlay_dir"
if grep -q updater_server_url ${updater_values_dir}/strings.xml; then
# "New" updater configuration: full URL (with placeholders {device}, {type} and {incr})
sed "s|{name}|updater_server_url|g; s|{url}|$OTA_URL/v1/{device}/{type}/{incr}|g" /root/packages_updater_strings.xml > "$updater_url_overlay_dir/strings.xml"
elif grep -q conf_update_server_url_def ${updater_values_dir}/strings.xml; then
# "Old" updater configuration: just the URL
sed "s|{name}|conf_update_server_url_def|g; s|{url}|$OTA_URL|g" /root/packages_updater_strings.xml > "$updater_url_overlay_dir/strings.xml"
else
echo ">> [$(date)] ERROR: no known Updater URL property found"
exit 1
fi
fi
# Add custom packages to be installed
if [ -n "$CUSTOM_PACKAGES" ]; then
echo ">> [$(date)] Adding custom packages ($CUSTOM_PACKAGES)"
sed -i "1s;^;PRODUCT_PACKAGES += $CUSTOM_PACKAGES\n\n;" "vendor/$vendor/config/common.mk"
fi
if [ "$SIGN_BUILDS" = true ]; then
echo ">> [$(date)] Adding keys path ($KEYS_DIR)"
# Soong (Android 9+) complains if the signing keys are outside the build path
ln -sf "$KEYS_DIR" user-keys
if [ "$android_version_major" -lt "10" ]; then
sed -i "1s;^;PRODUCT_DEFAULT_DEV_CERTIFICATE := user-keys/releasekey\nPRODUCT_OTA_PUBLIC_KEYS := user-keys/releasekey\nPRODUCT_EXTRA_RECOVERY_KEYS := user-keys/releasekey\n\n;" "vendor/$vendor/config/common.mk"
fi
if [ "$android_version_major" -ge "10" ]; then
sed -i "1s;^;PRODUCT_DEFAULT_DEV_CERTIFICATE := user-keys/releasekey\nPRODUCT_OTA_PUBLIC_KEYS := user-keys/releasekey\n\n;" "vendor/$vendor/config/common.mk"
fi
fi
# Prepare the environment
echo ">> [$(date)] Preparing build environment"
set +eu
# shellcheck source=/dev/null
source build/envsetup.sh > /dev/null
set -eu
if [ -f /root/userscripts/before.sh ]; then
echo ">> [$(date)] Running before.sh"
/root/userscripts/before.sh || echo ">> [$(date)] Warning: before.sh failed!"
fi
for codename in ${devices//,/ }; do
if [ -n "$codename" ]; then
if [ "$BUILD_OVERLAY" = true ]; then
lowerdir=$SRC_DIR/$branch_dir
upperdir=$TMP_DIR/device
workdir=$TMP_DIR/workdir
merged=$TMP_DIR/merged
mkdir -p "$upperdir" "$workdir" "$merged"
mount -t overlay overlay -o lowerdir="$lowerdir",upperdir="$upperdir",workdir="$workdir" "$merged"
source_dir="$TMP_DIR/merged"
else
source_dir="$SRC_DIR/$branch_dir"
fi
cd "$source_dir"
if [ "$ZIP_SUBDIR" = true ]; then
zipsubdir=$codename
mkdir -p "$ZIP_DIR/$zipsubdir"
else
zipsubdir=
fi
if [ "$LOGS_SUBDIR" = true ]; then
logsubdir=$codename
mkdir -p "$LOGS_DIR/$logsubdir"
else
logsubdir=
fi
DEBUG_LOG="$LOGS_DIR/$logsubdir/lineage-$los_ver-$builddate-$RELEASE_TYPE-$codename.log"
set +eu
breakfast "$codename" "$BUILD_TYPE" &>> "$DEBUG_LOG"
breakfast_returncode=$?
set -eu
if [ $breakfast_returncode -ne 0 ]; then
echo ">> [$(date)] breakfast failed for $codename, $branch branch" | tee -a "$DEBUG_LOG"
# call post-build.sh so the failure is logged in a way that is more visible
if [ -f /root/userscripts/post-build.sh ]; then
echo ">> [$(date)] Running post-build.sh for $codename" >> "$DEBUG_LOG"
/root/userscripts/post-build.sh "$codename" false "$branch" &>> "$DEBUG_LOG" || echo ">> [$(date)] Warning: post-build.sh failed!"
fi
continue
fi
if [ -f /root/userscripts/pre-build.sh ]; then
echo ">> [$(date)] Running pre-build.sh for $codename" >> "$DEBUG_LOG"
/root/userscripts/pre-build.sh "$codename" &>> "$DEBUG_LOG" || echo ">> [$(date)] Warning: pre-build.sh failed!"
fi
# Start the build
echo ">> [$(date)] Starting build for $codename, $branch branch" | tee -a "$DEBUG_LOG"
build_successful=false
if (set +eu ; mka "${jobs_arg[@]}" bacon) &>> "$DEBUG_LOG"; then
# Move produced ZIP files to the main OUT directory
echo ">> [$(date)] Moving build artifacts for $codename to '$ZIP_DIR/$zipsubdir'" | tee -a "$DEBUG_LOG"
cd out/target/product/"$codename"
files_to_hash=()
for build in lineage-*.zip; do
cp -v system/build.prop "$ZIP_DIR/$zipsubdir/$build.prop" &>> "$DEBUG_LOG"
mv "$build" "$ZIP_DIR/$zipsubdir/" &>> "$DEBUG_LOG"
files_to_hash+=( "$build" )
done
cd "$source_dir/out/target/product/$codename/obj/PACKAGING/target_files_intermediates/lineage_$codename-target_files-eng.root/IMAGES/"
for image in recovery boot vendor_boot dtbo super_empty vbmeta vendor_kernel_boot; do
if [ -f "$image.img" ]; then
recovery_name="lineage-$los_ver-$builddate-$RELEASE_TYPE-$codename-$image.img"
echo ">> [$(date)] Copying $image.img" to "$ZIP_DIR/$zipsubdir/$recovery_name" >> "$DEBUG_LOG"
cp "$image.img" "$ZIP_DIR/$zipsubdir/$recovery_name" &>> "$DEBUG_LOG"
files_to_hash+=( "$recovery_name" )
fi
done
cd "$ZIP_DIR/$zipsubdir"
for f in "${files_to_hash[@]}"; do
sha256sum "$f" > "$ZIP_DIR/$zipsubdir/$f.sha256sum"
done
cd "$source_dir"
build_successful=true
else
echo ">> [$(date)] Failed build for $codename" | tee -a "$DEBUG_LOG"
fi
# Remove old zips and logs
if [ "$DELETE_OLD_ZIPS" -gt "0" ]; then
if [ "$ZIP_SUBDIR" = true ]; then
/usr/bin/python /root/clean_up.py -n "$DELETE_OLD_ZIPS" -V "$los_ver" -N 1 "$ZIP_DIR/$zipsubdir"
else
/usr/bin/python /root/clean_up.py -n "$DELETE_OLD_ZIPS" -V "$los_ver" -N 1 -c "$codename" "$ZIP_DIR"
fi
fi
if [ "$DELETE_OLD_LOGS" -gt "0" ]; then
if [ "$LOGS_SUBDIR" = true ]; then
/usr/bin/python /root/clean_up.py -n "$DELETE_OLD_LOGS" -V "$los_ver" -N 1 "$LOGS_DIR/$logsubdir"
else
/usr/bin/python /root/clean_up.py -n "$DELETE_OLD_LOGS" -V "$los_ver" -N 1 -c "$codename" "$LOGS_DIR"
fi
fi
if [ -f /root/userscripts/post-build.sh ]; then
echo ">> [$(date)] Running post-build.sh for $codename" >> "$DEBUG_LOG"
/root/userscripts/post-build.sh "$codename" $build_successful "$branch" &>> "$DEBUG_LOG" || echo ">> [$(date)] Warning: post-build.sh failed!"
fi
echo ">> [$(date)] Finishing build for $codename" | tee -a "$DEBUG_LOG"
if [ "$BUILD_OVERLAY" = true ]; then
# The Jack server must be stopped manually, as we want to unmount $TMP_DIR/merged
cd "$TMP_DIR"
if [ -f "$TMP_DIR/merged/prebuilts/sdk/tools/jack-admin" ]; then
"$TMP_DIR/merged/prebuilts/sdk/tools/jack-admin kill-server" &> /dev/null || true
fi
lsof | grep "$TMP_DIR/merged" | awk '{ print $2 }' | sort -u | xargs -r kill &> /dev/null || true
while lsof | grep -q "$TMP_DIR"/merged; do
sleep 1
done
umount "$TMP_DIR/merged"
fi
if [ "$CLEAN_AFTER_BUILD" = true ]; then
echo ">> [$(date)] Cleaning source dir for device $codename" | tee -a "$DEBUG_LOG"
if [ "$BUILD_OVERLAY" = true ]; then
cd "$TMP_DIR"
rm -rf ./* || true
else
cd "$source_dir"
(set +eu ; mka "${jobs_arg[@]}" clean) &>> "$DEBUG_LOG"
fi
fi
fi
done
fi
done
if [ "$DELETE_OLD_LOGS" -gt "0" ]; then
find "$LOGS_DIR" -maxdepth 1 -name 'repo-*.log' | sort | head -n -"$DELETE_OLD_LOGS" | xargs -r rm || true
fi
if [ -f /root/userscripts/end.sh ]; then
echo ">> [$(date)] Running end.sh"
/root/userscripts/end.sh || echo ">> [$(date)] Warning: end.sh failed!"
fi