-
Notifications
You must be signed in to change notification settings - Fork 7
/
bbuild-anykernel.sh
executable file
·530 lines (434 loc) · 14.2 KB
/
bbuild-anykernel.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/bin/bash
# Boeffla Kernel Universal Build Script
#
# Version 1.3, 11.10.2016
#
# (C) Lord Boeffla (aka andip71)
#######################################
# Parameters to be configured manually
#######################################
BOEFFLA_VERSION="1.0-test-CM14.0-OnePlus2"
TOOLCHAIN="/opt/toolchains/aarch64-linux-android-4.9-20150123/bin/aarch64-linux-android-"
ARCHITECTURE=arm64
COMPILER_FLAGS_KERNEL="-Wno-maybe-uninitialized"
COMPILER_FLAGS_MODULE="-Wno-maybe-uninitialized"
KERNEL_IMAGE="Image.gz-dtb"
COMPILE_DTB="n"
DTBTOOL=""
DTBTOOL_CMD=""
MODULES_IN_SYSTEM="y"
OUTPUT_FOLDER=""
DEFCONFIG="boeffla_defconfig"
DEFCONFIG_VARIANT=""
KERNEL_NAME="Boeffla-Kernel"
FINISH_MAIL_TO=""
SMB_SHARE_KERNEL=""
SMB_FOLDER_KERNEL=""
SMB_AUTH_KERNEL=""
SMB_SHARE_BACKUP=""
SMB_FOLDER_BACKUP=""
SMB_AUTH_BACKUP=""
NUM_CPUS="" # number of cpu cores used for build (leave empty for auto detection)
#######################################
# automatic parameters, do not touch !
#######################################
COLOR_RED="\033[0;31m"
COLOR_GREEN="\033[1;32m"
COLOR_NEUTRAL="\033[0m"
SOURCE_PATH=$PWD
cd ..
ROOT_PATH=$PWD
ROOT_DIR_NAME=`basename "$PWD"`
cd $SOURCE_PATH
BUILD_PATH="$ROOT_PATH/build"
REPACK_PATH="$ROOT_PATH/repack"
BOEFFLA_DATE=$(date +%Y%m%d)
GIT_BRANCH=`git symbolic-ref --short HEAD`
if [ -z "$NUM_CPUS" ]; then
NUM_CPUS=`grep -c ^processor /proc/cpuinfo`
fi
# overwrite settings with repo specific custom file, if it exists
if [ -f $ROOT_PATH/x-settings.sh ]; then
. $ROOT_PATH/x-settings.sh
fi
# overwrite settings with user specific custom file, if it exists
if [ -f ~/x-settings.sh ]; then
. ~/x-settings.sh
fi
BOEFFLA_FILENAME="${KERNEL_NAME,,}-$BOEFFLA_VERSION"
# set environment
export ARCH=$ARCHITECTURE
export CROSS_COMPILE="${CCACHE} $TOOLCHAIN"
#####################
# internal functions
#####################
step0_copy_code()
{
echo -e $COLOR_GREEN"\n0 - copy code\n"$COLOR_NEUTRAL
# remove old build folder and create empty one
rm -r -f $BUILD_PATH
mkdir $BUILD_PATH
# copy code from source folder to build folder
# (usage of * prevents .git folder to be copied)
cp -r $SOURCE_PATH/* $BUILD_PATH
# Replace version information in mkcompile_h with the one from x-settings.sh
sed "s/\`echo \$LINUX_COMPILE_BY | \$UTS_TRUNCATE\`/$KERNEL_NAME-$BOEFFLA_VERSION-$BOEFFLA_DATE/g" -i $BUILD_PATH/scripts/mkcompile_h
sed "s/\`echo \$LINUX_COMPILE_HOST | \$UTS_TRUNCATE\`/andip71/g" -i $BUILD_PATH/scripts/mkcompile_h
}
step1_make_clean()
{
echo -e $COLOR_GREEN"\n1 - make clean\n"$COLOR_NEUTRAL
# jump to build path and make clean
cd $BUILD_PATH
make clean
}
step2_make_config()
{
echo -e $COLOR_GREEN"\n2 - make config\n"$COLOR_NEUTRAL
echo
# build make string depending on if we need to compile to an output folder
# and if we need to have a defconfig variant
MAKESTRING="arch=$ARCHITECTURE $DEFCONFIG"
if [ ! -z "$OUTPUT_FOLDER" ]; then
rm -rf $BUILD_PATH/output
mkdir $BUILD_PATH/output
MAKESTRING="O=$OUTPUT_FOLDER $MAKESTRING"
fi
if [ ! -z "$DEFCONFIG_VARIANT" ]; then
MAKESTRING="$MAKESTRING VARIANT_DEFCONFIG=$DEFCONFIG_VARIANT"
fi
# jump to build path and make config
cd $BUILD_PATH
echo "Makestring: $MAKESTRING"
make $MAKESTRING
}
step3_compile()
{
echo -e $COLOR_GREEN"\n3 - compile\n"$COLOR_NEUTRAL
TIMESTAMP1=$(date +%s)
# remove a previous kernel image
rm $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/$KERNEL_IMAGE &>/dev/null
# jump to build path
cd $BUILD_PATH
# compile source
if [ -z "$OUTPUT_FOLDER" ]; then
make -j$NUM_CPUS CFLAGS_KERNEL="$COMPILER_FLAGS_KERNEL" CFLAGS_MODULE="$COMPILER_FLAGS_MODULE" 2>&1 |tee ../compile.log
else
make -j$NUM_CPUS O=$OUTPUT_FOLDER CFLAGS_KERNEL="$COMPILER_FLAGS_KERNEL" CFLAGS_MODULE="$COMPILER_FLAGS_MODULE" 2>&1 |tee ../compile.log
fi
# compile dtb if required
if [ "y" == "$COMPILE_DTB" ]; then
echo -e ">>> compiling DTB\n"
echo
# Compile dtb (device tree blob) file
if [ -f $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/dt.img ]; then
rm $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/dt.img
fi
chmod 777 tools_boeffla/$DTBTOOL
tools_boeffla/$DTBTOOL $DTBTOOL_CMD -o $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/dt.img -s 2048 -p $BUILD_PATH/$OUTPUT_FOLDER/scripts/dtc/ $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/
fi
TIMESTAMP2=$(date +%s)
# Log compile time (screen output)
echo "compile time:" $(($TIMESTAMP2 - $TIMESTAMP1)) "seconds"
# if kernel image does not exist, exit processing
if [ ! -e $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/$KERNEL_IMAGE ]; then
echo -e $COLOR_RED
echo ""
echo "Compile was NOT successful !! Aborting."
echo ""
echo -e $COLOR_NEUTRAL
exit
fi
# Log kernel image size (screen output)
echo "Kernel image size (bytes):"
stat -c%s $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/$KERNEL_IMAGE
# Log compile time and parameters (log file output)
echo -e "\n***************************************************" >> ../compile.log
echo -e "\ncompile time:" $(($TIMESTAMP2 - $TIMESTAMP1)) "seconds" >> ../compile.log
echo "Kernel image size (bytes):" >> ../compile.log
stat -c%s $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/$KERNEL_IMAGE >> ../compile.log
echo -e "\n***************************************************" >> ../compile.log
echo -e "\nroot path:" $ROOT_PATH >> ../compile.log
echo "toolchain compile:" >> ../compile.log
grep "^CROSS_COMPILE" $BUILD_PATH/Makefile >> ../compile.log
echo "toolchain stripping:" $TOOLCHAIN >> ../compile.log
}
step4_prepare_anykernel()
{
echo -e $COLOR_GREEN"\n4 - prepare anykernel\n"$COLOR_NEUTRAL
# Cleanup folder if still existing
echo -e ">>> cleanup repack folder\n"
{
rm -r -f $REPACK_PATH
mkdir -p $REPACK_PATH
} 2>/dev/null
# copy anykernel template over
cd $REPACK_PATH
cp -R $BUILD_PATH/anykernel_boeffla/* .
# delete placeholder files
find . -name placeholder -delete
# copy kernel image
cp $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/$KERNEL_IMAGE $REPACK_PATH/zImage
{
# copy dtb (if we have one)
if [ "y" == "$COMPILE_DTB" ]; then
cp $BUILD_PATH/$OUTPUT_FOLDER/arch/$ARCHITECTURE/boot/dt.img $REPACK_PATH/dtb
fi
# copy modules to either modules folder (CM and derivates) or directly in ramdisk (Samsung stock)
if [ "y" == "$MODULES_IN_SYSTEM" ]; then
MODULES_PATH=$REPACK_PATH/modules
else
MODULES_PATH=$REPACK_PATH/ramdisk/lib/modules
fi
mkdir -p $MODULES_PATH
# copy generated modules
find $BUILD_PATH -name '*.ko' -exec cp -av {} $MODULES_PATH \;
# copy static modules and rename from ko_ to ko, only if there are some
if [ "$(ls -A $BUILD_PATH/modules_boeffla)" ]; then
cp $BUILD_PATH/modules_boeffla/* $MODULES_PATH
cd $MODULES_PATH
for i in *.ko_; do mv $i ${i%ko_}ko; echo Static module: ${i%ko_}ko; done
fi
# set module permissions
chmod 644 $MODULES_PATH/*
# strip modules
echo -e ">>> strip modules\n"
${TOOLCHAIN}strip --strip-unneeded $MODULES_PATH/*
} 2>/dev/null
# replace variables in anykernel script
cd $REPACK_PATH
KERNELNAME="Flashing $KERNEL_NAME $BOEFFLA_VERSION"
sed -i "s;###kernelname###;${KERNELNAME};" META-INF/com/google/android/update-binary;
COPYRIGHT="(c) Lord Boeffla (aka andip71), $(date +%Y.%m.%d-%H:%M:%S)"
sed -i "s;###copyright###;${COPYRIGHT};" META-INF/com/google/android/update-binary;
}
step5_create_anykernel_zip()
{
echo -e $COLOR_GREEN"\n5 - create anykernel zip\n"$COLOR_NEUTRAL
# Creating recovery flashable zip
echo -e ">>> create flashable zip\n"
# create zip file
cd $REPACK_PATH
zip -r9 $BOEFFLA_FILENAME.recovery.zip * -x $BOEFFLA_FILENAME.recovery.zip
# sign recovery zip if there are keys available
if [ -f "$BUILD_PATH/tools_boeffla/testkey.x509.pem" ]; then
echo -e ">>> signing recovery zip\n"
java -jar $BUILD_PATH/tools_boeffla/signapk.jar -w $BUILD_PATH/tools_boeffla/testkey.x509.pem $BUILD_PATH/tools_boeffla/testkey.pk8 $BOEFFLA_FILENAME.recovery.zip $BOEFFLA_FILENAME.recovery.zip_signed
cp $BOEFFLA_FILENAME.recovery.zip_signed $BOEFFLA_FILENAME.recovery.zip
rm $BOEFFLA_FILENAME.recovery.zip_signed
fi
md5sum $BOEFFLA_FILENAME.recovery.zip > $BOEFFLA_FILENAME.recovery.zip.md5
# Creating additional files for load&flash
echo -e ">>> create load&flash files\n"
cp $BOEFFLA_FILENAME.recovery.zip cm-kernel.zip
md5sum cm-kernel.zip > checksum
}
step7_analyse_log()
{
echo -e $COLOR_GREEN"\n7 - analyse log\n"$COLOR_NEUTRAL
# Check compile result and patch file success
echo -e "\n***************************************************"
echo -e "Check for compile errors:"
cd $ROOT_PATH
echo -e $COLOR_RED
grep " error" compile.log
grep "forbidden warning" compile.log
echo -e $COLOR_NEUTRAL
echo -e "***************************************************"
}
step8_transfer_kernel()
{
echo -e $COLOR_GREEN"\n8 - transfer kernel\n"$COLOR_NEUTRAL
# transfer only if SMB share configured
if [ ! -z "$SMB_SHARE_KERNEL" ]; then
smbclient $SMB_SHARE_KERNEL -U $SMB_AUTH_KERNEL -c "mkdir $SMB_FOLDER_KERNEL\\$BOEFFLA_VERSION"
smbclient $SMB_SHARE_KERNEL -U $SMB_AUTH_KERNEL -c "put $REPACK_PATH/$BOEFFLA_FILENAME.recovery.zip $SMB_FOLDER_KERNEL\\$BOEFFLA_VERSION\\$BOEFFLA_FILENAME.recovery.zip"
smbclient $SMB_SHARE_KERNEL -U $SMB_AUTH_KERNEL -c "put $REPACK_PATH/$BOEFFLA_FILENAME.recovery.zip.md5 $SMB_FOLDER_KERNEL\\$BOEFFLA_VERSION\\$BOEFFLA_FILENAME.recovery.zip.md5"
smbclient $SMB_SHARE_KERNEL -U $SMB_AUTH_KERNEL -c "put $REPACK_PATH/checksum $SMB_FOLDER_KERNEL\\$BOEFFLA_VERSION\\checksum"
smbclient $SMB_SHARE_KERNEL -U $SMB_AUTH_KERNEL -c "put $REPACK_PATH/cm-kernel.zip $SMB_FOLDER_KERNEL\\$BOEFFLA_VERSION\\cm-kernel.zip"
return
fi
# transfer only if ssh ftp configured
if [ ! -z "$SSH_FTP_REMOTE" ]; then
rm -rf ~/bbuild_transfer
mkdir ~/bbuild_transfer
echo "$SSH_FTP_PW" | sshfs "$SSH_FTP_REMOTE" ~/bbuild_transfer -p "$SSH_FTP_PORT" -o password_stdin
mkdir -p ~/bbuild_transfer/$BOEFFLA_VERSION
cp $REPACK_PATH/$BOEFFLA_FILENAME.recovery.zip ~/bbuild_transfer/$BOEFFLA_VERSION
cp $REPACK_PATH/$BOEFFLA_FILENAME.recovery.zip.md5 ~/bbuild_transfer/$BOEFFLA_VERSION
cp $REPACK_PATH/checksum ~/bbuild_transfer/$BOEFFLA_VERSION
cp $REPACK_PATH/cm-kernel.zip ~/bbuild_transfer/$BOEFFLA_VERSION
sync
sleep 1
fusermount -u ~/bbuild_transfer
rm -rf ~/bbuild_transfer
return
fi
echo -e "No kernel smb share or ssh ftp configured, not transfering files.\n"
}
step9_send_finished_mail()
{
echo -e $COLOR_GREEN"\n9 - send finish mail\n"$COLOR_NEUTRAL
# send a mail to inform about finished compilation
if [ -z "$FINISH_MAIL_TO" ]; then
echo -e "No mail address configured, not sending mail.\n"
else
cat $ROOT_PATH/compile.log | /usr/bin/mailx -s "Compilation for $KERNEL_NAME $BOEFFLA_VERSION finished!!!" $FINISH_MAIL_TO
fi
}
stepR_rewrite_config()
{
echo -e $COLOR_GREEN"\nr - rewrite config\n"$COLOR_NEUTRAL
# copy defconfig, run make oldconfig and copy it back
cd $SOURCE_PATH
cp arch/$ARCHITECTURE/configs/$DEFCONFIG .config
make oldconfig
cp .config arch/$ARCHITECTURE/configs/$DEFCONFIG
make mrproper
# commit change
git add arch/$ARCHITECTURE/configs/$DEFCONFIG
git commit
}
stepC_cleanup()
{
echo -e $COLOR_GREEN"\nc - cleanup\n"$COLOR_NEUTRAL
# remove old build and repack folders, remove any logs
{
rm -r -f $BUILD_PATH
rm -r -f $REPACK_PATH
rm $ROOT_PATH/*.log
} 2>/dev/null
}
stepB_backup()
{
echo -e $COLOR_GREEN"\nb - backup\n"$COLOR_NEUTRAL
# Create a tar backup in parent folder, gzip it and copy to verlies
BACKUP_FILE="$ROOT_DIR_NAME""_$(date +"%Y-%m-%d_%H-%M").tar.gz"
cd $ROOT_PATH
tar --use-compress-program=pigz -cvf $BACKUP_FILE source x-settings.sh
cd $SOURCE_PATH
# transfer backup only if smbshare configured
if [ -z "$SMB_SHARE_BACKUP" ]; then
echo -e "No backup smb share configured, not transfering backup.\n"
else
# copy backup to a SMB network storage and delete backup afterwards
smbclient $SMB_SHARE_BACKUP -U $SMB_AUTH_BACKUP -c "put $ROOT_PATH/$BACKUP_FILE $SMB_FOLDER_BACKUP\\$BACKUP_FILE"
rm $ROOT_PATH/$BACKUP_FILE
fi
}
display_help()
{
echo
echo
echo "Function menu (anykernel version)"
echo "======================================================================"
echo
echo "0 = copy code | 5 = create anykernel"
echo "1 = make clean | "
echo "2 = make config | 7 = analyse log"
echo "3 = compile | 8 = transfer kernel"
echo "4 = prepare anykernel | 9 = send finish mail"
echo
echo "rel = all, execute steps 0-9 - without CCACHE | r = rewrite config"
echo "a = all, execute steps 0-9 | c = cleanup"
echo "u = upd, execute steps 3-9 | b = backup"
echo
echo "======================================================================"
echo
echo "Parameters:"
echo
echo " Boeffla version: $BOEFFLA_VERSION"
echo " Boeffla date: $BOEFFLA_DATE"
echo " Kernel name: $KERNEL_NAME"
echo " Git branch: $GIT_BRANCH"
echo " CPU Cores: $NUM_CPUS"
echo
echo " Toolchain: $TOOLCHAIN"
echo " Root path: $ROOT_PATH"
echo " Root dir: $ROOT_DIR_NAME"
echo " Source path: $SOURCE_PATH"
echo " Build path: $BUILD_PATH"
echo " Repack path: $REPACK_PATH"
echo " Kernel Filename: $BOEFFLA_FILENAME"
echo
echo "======================================================================"
}
################
# main function
################
unset CCACHE_DISABLE
case "$1" in
rel)
export CCACHE_DISABLE=1
step0_copy_code
step1_make_clean
step2_make_config
step3_compile
step4_prepare_anykernel
step5_create_anykernel_zip
step7_analyse_log
step8_transfer_kernel
step9_send_finished_mail
;;
a)
step0_copy_code
step1_make_clean
step2_make_config
step3_compile
step4_prepare_anykernel
step5_create_anykernel_zip
step7_analyse_log
step8_transfer_kernel
step9_send_finished_mail
;;
u)
step3_compile
step4_prepare_anykernel
step5_create_anykernel_zip
step7_analyse_log
step8_transfer_kernel
step9_send_finished_mail
;;
0)
step0_copy_code
;;
1)
step1_make_clean
;;
2)
step2_make_config
;;
3)
step3_compile
;;
4)
step4_prepare_anykernel
;;
5)
step5_create_anykernel_zip
;;
6)
# do nothing
;;
7)
step7_analyse_log
;;
8)
step8_transfer_kernel
;;
9)
step9_send_finished_mail
;;
b)
stepB_backup
;;
c)
stepC_cleanup
;;
r)
stepR_rewrite_config
;;
*)
display_help
;;
esac