forked from baalajimaestro/Generic-ROM-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
410 lines (372 loc) · 12.1 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
#!/bin/bash
# Copyright (C) 2019 baalajimaestro
#
# Licensed under the Raphielscape Public License, Version 1.b (the "License");
# you may not use this file except in compliance with the License.
#
# CI Runner Script for Building a ROM
# We need this directive
# shellcheck disable=1090
##### Build Env Dependencies
build_env()
{
TELEGRAM_TOKEN=$(cat /tmp/tg_token)
TELEGRAM_CHAT=$(cat /tmp/tg_chat)
export GH_PERSONAL_TOKEN=$(cat /tmp/gh_token)
export TELEGRAM_TOKEN
export TELEGRAM_CHAT
cd ~
git clone https://baalajimaestro:${GH_PERSONAL_TOKEN}@github.com/baalajimaestro/google-git-cookies.git > /dev/null 2>&1
cd google-git-cookies
bash run.sh
cd ..
rm -rf google-git-cookies
git config --global user.email "[email protected]"
git config --global user.name "baalajimaestro"
echo "Google Git Cookie Set!"
}
cyan=' '
yellow=' '
reset=' '
validate_arg() {
valid=$(echo $1 | sed s'/^[\-][a-z0-9A-Z\-]*/valid/'g)
[ "x$1" == "x$0" ] && return 0;
[ "x$1" == "x" ] && return 0;
[ "$valid" == "valid" ] && return 0 || return 1;
}
print_help() {
echo "Usage: `basename $0` [OPTION]";
echo " -i, --init-source \ Declare you need repo init" ;
echo " -U, --url \ Supply Repo Init URL" ;
echo " -b, --branch \ Supply branch to sync" ;
echo " -s, --sync-android \ Sync current source" ;
echo " -b, --brand \ Brand name" ;
echo " -d, --device \ Device name" ;
echo " -dt --device-tree \ Specify Device Tree for unofficial build" ;
echo " -t, --target \ Make target" ;
echo " -c, --clean \ Clean target" ;
echo " -ca, --cleanall \ Clean entire out" ;
echo " -tg, --telegram \ Enable telegram message" ;
echo " -u, --upload \ Enable drive upload" ;
echo " -r, --Release \ Enable drive upload, tg msg and clean" ;
exit
}
prev_arg=
while [ "$1" != "" ]; do
cur_arg=$1
# find arguments of the form --arg=val and split to --arg val
if [ -n "`echo $cur_arg | grep -o =`" ]; then
cur_arg=`echo $1 | cut -d'=' -f 1`
next_arg=`echo $1 | cut -d'=' -f 2`
else
cur_arg=$1
next_arg=$2
fi
case $cur_arg in
-i | --init-source )
prepare_source_scr=$next_arg
;;
-U | --url )
repo_init_url=$next_arg
;;
-B | --branch )
repo_branch=$next_arg
;;
-s | --sync-android )
sync_android_scr=1
;;
-b | --brand )
brand_scr=$next_arg
export brand_scr
;;
-d | --device )
device_scr=$next_arg
export device_scr
;;
-t | --target )
build_type_scr=$next_arg
build_orig_scr=$next_arg
;;
-tg | --telegram )
telegram_scr=1
;;
-u | --upload )
upload_scr=1
;;
-dt | --device-tree )
device_tree=$next_arg
;;
-r | --release )
telegram_scr=1
upload_scr=1
clean_scr=1
;;
-c | --clean )
clean_scr=1
;;
-ca | --clean-all )
cleanall_scr=1
;;
*)
validate_arg $cur_arg;
if [ $? -eq 0 ]; then
echo "Unrecognised option $cur_arg passed"
print_help
else
validate_arg $prev_arg
if [ $? -eq 1 ]; then
echo "Argument $cur_arg passed without flag option"
print_help
fi
fi
;;
esac
prev_arg=$1
shift
done
acquire_build_lock() {
lock_name="android_build_lock"
lock="$HOME/${lock_name}"
exec 200>${lock}
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Attempting to acquire lock $($yellow)$lock$($cyan)"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
# loop if we can't get the lock
while true; do
flock -n 200
if [ $? -eq 0 ]; then
break
else
printf "%c" "."
sleep 5
fi
done
# set the pid
pid=$$
echo ${pid} 1>&200
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Lock $($yellow)${lock}$($cyan) acquired. PID is $($yellow)${pid}$($cyan)"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
}
remove_build_lock() {
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Removing $($yellow)$lock$($cyan)"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
exec 200>&-
}
prepare_source() {
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Initializing $($yellow)$prepare_source_scr$($cyan)"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
source_android_scr=$prepare_source_scr
repo init -u $repo_init_url -b $repo_branch --depth 1
python3 /drone/src/xml_strip.py
sync_android_scr=1
##### if this fails idc, its your problem biatch
}
function_check() {
if [ ! $TELEGRAM_TOKEN ] && [ ! $TELEGRAM_CHAT ]; then
printf "You don't have TELEGRAM_TOKEN,TELEGRAM_CHAT set"
exit
fi
if [ ! -f telegram ];
then
echo "Telegram binary not present. Installing.."
wget -q https://raw.githubusercontent.com/Dyneteve/misc/master/telegram
chmod +x telegram
fi
if [ ! -d $HOME/buildscript ];
then
mkdir $HOME/buildscript
fi
}
sync_source() {
if [ $sync_android_scr ]; then
printf "%s\n\n" $($cyan)
printf "%s\n" "*********************************************"
printf '%s\n' "Repo Sync Started"
printf "%s\n" "*********************************************"
printf "%s\n\n" $($reset)
repo sync --force-sync --current-branch --no-tags --no-clone-bundle --optimized-fetch --prune -j$(nproc) -q > sync.log 2>&1
bash telegram -f sync.log
printf "%s\n\n" $($cyan)
printf "%s\n" "*********************************************"
printf '%s\n' "Repo Sync Finished"
printf "%s\n" "*********************************************"
printf "%s\n\n" $($reset)
fi
}
start_env() {
rm -rf venv
virtualenv2 venv
source venv/bin/activate
}
setup_paths() {
source build/envsetup.sh
####### Workaround the Missing Lunch combos for official trees
if [ $prepare_source_scr ]; then
printf "%s\n\n" $($cyan)
printf "%s\n" "***************************************"
printf '%s\n' "Breakfasting device trees from rom repo"
printf "%s\n" "***************************************"
printf "%s\n\n" $($reset)
if ! breakfast "${device_scr}"; then
printf "%s\n\n" $($cyan)
printf "%s\n" "*****************************************************"
printf '%s\n' "Breakfast failed! Lunching device trees from rom repo"
printf "%s\n" "*****************************************************"
printf "%s\n\n" $($reset)
lunch "$prepare_source_scr"_"$device_scr"-userdebug
fi
OUT_SCR=out/target/product/$device_scr
DEVICEPATH_SCR=device/$brand_scr/$device_scr
if [ $prepare_source_scr ] && [ ! -d $DEVICEPATH_SCR ]; then
if [ $device_tree ]; then
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Unofficial Device Tree Detected"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
git clone $device_tree device/$brand/$device
python3 unofficial_builder.py
lunch "$prepare_source_scr"_"$device_scr"-userdebug
else
printf "%s\n Couldnt fetch DT"
exit
fi
fi
if [ -z "$build_type_scr" ]; then
build_type_scr=bacon
fi
fi
}
clean_target() {
if [ $clean_scr ] && [ ! $cleanall_scr ]; then
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Cleaning target $($yellow) $device_scr $($cyan)"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
rm -rf $OUT_SCR
sleep 2
elif [ $cleanall_scr ]; then
printf "%s\n\n" $($cyan)
printf "%s\n" "**************************"
printf '%s\n' "Cleaning entire out"
printf "%s\n" "**************************"
printf "%s\n\n" $($reset)
rm -rf out
sleep 2s
fi
}
upload() {
# if [ $telegram_scr ] && [ ! $(grep -c "#### build completed successfully" build.log) -eq 1 ]; then
# bash telegram -D -M "
# *Build for $device_scr failed!*"
# bash telegram -f build.log
# exit
# fi
case $build_type_scr in
bacon)
file=$(ls $OUT_SCR/*201*.zip | tail -n 1)
;;
bootimage)
file=$OUT_SCR/boot.img
;;
recoveryimage)
file=$OUT_SCR/recovery.img
;;
dtbo)
file=$OUT_SCR/dtbo.img
;;
systemimage)
file=$OUT_SCR/system.img
;;
vendorimage)
file=$OUT_SCR/vendor.img
esac
if [ -f $HOME/buildscript/*.img ]; then
rm -f $HOME/buildscript/*.img
fi
git clone https://github.com/baalajimaestro/Generic-ROM-Builder -b binary binary
cd binary
touch "$(date +%d%m%y)-${prepare_source_scr}-$(cat /tmp/build_no)"
git add .
git commit -m "[MaestroCI]: Releasing Build ${prepare_source_scr}-$(date +%d%m%y)"
git tag "$(date +%d%m%y)-${prepare_source_scr}-$(cat /tmp/build_no)"
git remote rm origin
git remote add origin https://baalajimaestro:${GH_PERSONAL_TOKEN}@github.com/baalajimaestro/Generic-ROM-Builder.git
git push origin binary --follow-tags
build_date_scr=$(date +%F_%H-%M)
if [ ! -z $build_orig_scr ] && [ $upload_scr ]; then
bash upload-github-release-asset.sh github_api_token=$GH_PERSONAL_TOKEN owner=baalajimaestro repo=$(cat /tmp/gh_repo) tag="$(date +%d%m%y)-${prepare_source_scr}-$(cat /tmp/build_no)" filename=$file
file=`ls $HOME/buildscript/*.img | tail -n 1`
id=$(gdrive upload --parent $G_FOLDER $file | grep "Uploaded" | cut -d " " -f 2)
elif [ -z $build_orig_scr ] && [ $upload_scr ]; then
bash upload-github-release-asset.sh github_api_token=$GH_PERSONAL_TOKEN owner=baalajimaestro repo=$(cat /tmp/gh_repo) tag="$(date +%d%m%y)-${prepare_source_scr}-$(cat /tmp/build_no)" filename=$file
fi
if [ $telegram_scr ] && [ $upload_scr ]; then
bash telegram -D -M "
*Build for $device_scr done!*
Download from Github Releases"
fi
}
build() {
# if [ -f build.log ]; then
# rm -f build.log
# fi
if [ -f out/.lock ]; then
rm -f out/.lock
fi
export USE_CCACHE=0
cd $DEVICEPATH_SCR
mk_scr=`grep .mk AndroidProducts.mk | cut -d "/" -f "2"`
product_scr=`grep "PRODUCT_NAME :=" $mk_scr | cut -d " " -f 3`
cd ../../..
printf "%s\n\n" $($cyan)
printf "%s\n" "***********************************************"
printf '%s\n' "Started build with target $($yellow)"$build_type_scr""$($cyan)" for"$($yellow)" $device_scr $($cyan)"
printf "%s\n" "***********************************************"
printf "%s\n\n" $($reset)
sleep 2
if [ "$telegram_scr" ]; then
bash telegram -D -M "
*Build for $device_scr started!*
Product: *$product_scr*
Target: *$build_type_scr*
Started on: *$HOSTNAME*
Time: *$(date "+%r")* "
fi
mka bacon | grep $device_scr
printf "%s\n\n" $($cyan)
printf "%s\n" "***********************************************"
printf '%s\n' "Finished build with target $($yellow)"$build_type_scr""$($cyan)" for"$($yellow)" $device_scr $($cyan)"
printf "%s\n" "***********************************************"
printf "%s\n\n" $($reset)
sleep 2
}
build_env
if [ ! -z "$device_scr" ] && [ ! -z "$brand_scr" ]; then
acquire_build_lock
if [ $prepare_source_scr ]; then
prepare_source
fi
function_check
start_env
sync_source
setup_paths
clean_target
build $brand_scr $device_scr
remove_build_lock
upload
else
print_help
fi