forked from nathanchance/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash-kernel
executable file
·930 lines (734 loc) · 29 KB
/
flash-kernel
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2016-2018 Nathan Chancellor
#
# Flash Kernel compilation script
###############
# #
# FUNCTIONS #
# #
###############
# Gather parameters
function parse_parameters() {
PARAMS="${*}"
EXTRA_MAKE_FLAGS=()
while [[ ${#} -ge 1 ]]; do
case ${1} in
# AnyKernel2 branch
"-ak2-b"|"--anykernel2-branch")
shift && enforce_value "${@}"
ANYKERNEL2_BRANCH=${1} ;;
# Android version
"-av"|"--android-version")
shift && enforce_value "${@}"
ANDROID_VERSION=${1} ;;
# Branch to compile
"-b"|"--branch")
shift && enforce_value "${@}"
KERNEL_BRANCH=${1} ;;
# Config to use for compiling
"-c"|"--config")
shift && enforce_value "${@}"
DEFCONFIG=${1} ;;
# Compiler to use
"-cc"|"--compiler")
shift && enforce_value "${@}"
COMPILER=${1} ;;
# Clang toolchain to use for compiling
"-ct"|"--clang-toolchain")
shift && enforce_value "${@}"
CLANG_FOLDER=${1} ;;
# Device to compile for
"-d"|"--device")
shift && enforce_value "${@}"
DEVICE=${1} ;;
# Extra make flags if necessary (like mismatch config)
"-f"|"--flags")
shift && enforce_value "${@}"
EXTRA_MAKE_FLAGS+=( "${1}" ) ;;
# 64-bit GCC binary to use for compiling (like the system ones)
"-gb"|"--gcc-binary")
shift && enforce_value "${@}"
GCC=${1} ;;
# 32-bit GCC binary to use for compiling (like the system ones)
"-gb-32"|"--gcc-32-bit-binary")
shift && enforce_value "${@}"
GCC_32_BIT=${1} ;;
# 64-bit GCC toolchain to use for compiling
"-gt"|"--gcc-toolchain")
shift && enforce_value "${@}"
GCC_FOLDER=${1} ;;
# 32-bit GCC toolchain to use for compiling
"-gt-32"|"--gcc-32-bit-toolchain")
shift && enforce_value "${@}"
GCC_32_BIT_FOLDER=${1} ;;
# Do not package kernel image
"-io"|"--image-only")
IMAGE_ONLY=true ;;
# Build with Kernel Address Sanitizer
"-k"|"--kasan")
KASAN=true ;;
# Where to move zip after compilation
"-m"|"--mode")
shift && enforce_value "${@}"
MODE=${1} ;;
# Don't move/delete zips in FILE_MOVE folder
"-n"|"--no-clean")
NOCLEAN=true ;;
# Test versioning (# in kernel version)
"-r"|"--revision")
shift && enforce_value "${@}"
REVISION=${1} ;;
# Send updates to Telegram
"-t"|"--telegram")
TG=true
TG_MSG_FILE=$(mktemp)
trap 'rm -f "${TG_MSG_FILE}"' EXIT ;;
# Version number
"-v"|"--version")
shift && enforce_value "${@}"
VERSION=${1} ;;
# How much script output to show
"-V"|"--verbosity-level")
shift && enforce_value "${@}"
VERBOSITY_LEVEL=${1}
case ${VERBOSITY_LEVEL} in
"0") AG_LINES=0; HIDDEN_COMMANDS=true ;;
"1") SHOW_OUTPUT=true; HIDDEN_COMMANDS=true ;;
"2") SHOW_OUTPUT=true; VERBOSE_COMMANDS=true ;;
"3") EXTRA_MAKE_FLAGS+=( "V=1" ); SHOW_OUTPUT=true; HIDDEN_COMMANDS=true ;;
"4") EXTRA_MAKE_FLAGS+=( "V=1" ); SHOW_OUTPUT=true; VERBOSE_COMMANDS=true ;;
esac ;;
# Disables -Werror config
"-Wno-error")
NO_WERROR=true ;;
"") ;;
*)
die "Invalid parameter" ;;
esac
shift
done
# Default parameters
[[ -z ${DEVICE} ]] && DEVICE="wahoo"
[[ -z ${MODE} ]] && MODE="personal"
[[ -z ${VERBOSITY_LEVEL} ]] && HIDDEN_COMMANDS=true
# Easy to use variables for if statements
[[ ${MODE} = "alpha" || ${MODE} = "beta" || ${MODE} = "rc" ]] && TEST=true
[[ ${MODE} = "personal" ]] && PERSONAL=true
[[ ${MODE} = "stable" ]] && STABLE=true
[[ ${MODE} = "tmp" ]] && TMP=true
if [[ -n ${TG} ]]; then
{
echo "\`\`\`"
echo "Currently executing..."
echo
echo "$(basename "${0}") ${PARAMS}"
echo "\`\`\`"
} > "${TG_MSG_FILE}"
notify "$(cat "${TG_MSG_FILE}")"
fi
}
# 'cp -v' alias
function cp() {
command cp ${VERBOSE_COMMANDS:+ "-v"} "${@}"
}
# 'mv -v' alias
function mv() {
command mv ${VERBOSE_COMMANDS:+ "-v"} "${@}"
}
# 'rm -v' alias
function rm() {
command rm ${VERBOSE_COMMANDS:+ "-v"} "${@}"
}
# Device "board" flagsi
function device_flags() {
case ${DEVICE} in
"marlin")
# Required flags
[[ -z ${ANDROID_VERSION} ]] && ANDROID_VERSION=8.1.0
AK2_SFX=${ANDROID_VERSION}
BUILD_FUNCTION=build_kernel
[[ -z ${COMPILER} ]] && COMPILER=clang
[[ -z ${DEFCONFIG} ]] && DEFCONFIG="flash_defconfig"
DEVICE_NAME_FILE=${DEVICE^}
DEVICE_NAME_GITHUB=marlin
DEVICE_NAME_WEB=${DEVICE}
if [[ -z ${CLANG_FOLDER} ]]; then
# SC2153: Possible misspelling: TC_FOLDER may not be assigned, but GCC_FOLDER is.
# shellcheck disable=SC2153
case ${MODE} in
"personal") CLANG_FOLDER=${TC_FOLDER}/flash-clang-7.x ;;
*) CLANG_FOLDER=${TC_FOLDER}/flash-clang-7.x ;;
esac
fi
[[ -z ${GCC_FOLDER} ]] && GCC_FOLDER=${TC_FOLDER}/btg-linaro-gcc-7.3.1-arm64
[[ -z ${GCC_32_BIT_FOLDER} ]] && GCC_32_BIT_FOLDER=${TC_FOLDER}/btg-linaro-gcc-7.3.1-arm
[[ -n ${STABLE} ]] && KERNEL_BRANCH=oreo-m4
MATCH_FLAG="${DEVICE_NAME_FILE}-v${VERSION::1}"
WANTS_COMPAT_VDSO=true ;;
"op6")
# Required flags
[[ -z ${ANDROID_VERSION} ]] && ANDROID_VERSION=8.1.0
AK2_SFX=${ANDROID_VERSION}
BUILD_FUNCTION=build_kernel
[[ -z ${COMPILER} ]] && COMPILER=clang
if [[ -z ${CLANG_FOLDER} ]]; then
case ${MODE} in
"personal") CLANG_FOLDER=${TC_FOLDER}/flash-clang-7.x ;;
*) CLANG_FOLDER=${TC_FOLDER}/flash-clang-7.x ;;
esac
fi
[[ -z ${DEFCONFIG} ]] && DEFCONFIG="flash_defconfig"
DEVICE_NAME_FILE=${DEVICE^^}
DEVICE_NAME_GITHUB=${DEVICE}
DEVICE_NAME_WEB=${DEVICE}
[[ -z ${GCC_FOLDER} ]] && GCC_FOLDER=${TC_FOLDER}/btg-gcc-8.2.1-arm64
[[ -n ${STABLE} ]] && KERNEL_BRANCH=8.1.0
MATCH_FLAG="${DEVICE_NAME_FILE}-v${VERSION::1}" ;;
"wahoo")
# Required flags
[[ -z ${ANDROID_VERSION} ]] && ANDROID_VERSION=8.1.0
AK2_SFX=${ANDROID_VERSION}
BUILD_FUNCTION=build_kernel
[[ -z ${COMPILER} ]] && COMPILER=clang
if [[ -z ${CLANG_FOLDER} ]]; then
case ${MODE} in
"personal") CLANG_FOLDER=${TC_FOLDER}/flash-clang-7.x ;;
*) CLANG_FOLDER=${TC_FOLDER}/flash-clang-7.x ;;
esac
fi
[[ -z ${DEFCONFIG} ]] && DEFCONFIG="flash_defconfig"
DEVICE_NAME_FILE=${DEVICE^}
DEVICE_NAME_GITHUB=${DEVICE}
DEVICE_NAME_WEB=${DEVICE}
[[ -z ${GCC_FOLDER} ]] && GCC_FOLDER=${TC_FOLDER}/btg-gcc-8.2.1-arm64
[[ -z ${GCC_32_BIT_FOLDER} ]] && GCC_32_BIT_FOLDER=${TC_FOLDER}/btg-gcc-8.2.1-arm
[[ -n ${STABLE} ]] && KERNEL_BRANCH=oreo-m2
MATCH_FLAG="${DEVICE_NAME_FILE}-v${VERSION::1}"
WANTS_COMPAT_VDSO=true ;;
*)
die "Invalid device!" ;;
esac
}
# Set up other variables
function generate_variables() {
# Folders
SOURCE_FOLDER=${KERNEL_FOLDER}/flash/${DEVICE_NAME_GITHUB}
OUT_FOLDER=${SOURCE_FOLDER}/out
ANYKERNEL2_FOLDER=${KERNEL_FOLDER}/flash/anykernel2-${DEVICE_NAME_GITHUB}
FILE_MOVE_HEAD=${WEB_FOLDER}/downloads
case ${MODE} in
"alpha"|"beta"|"bisect"|"rc"|"stable")
FILE_MOVE=${FILE_MOVE_HEAD}/kernels/${DEVICE_NAME_WEB}/${ANDROID_VERSION}/${MODE}
FILE_MOVE_OLD=$(readlink -f "${FILE_MOVE}/../archive")
if [[ -n ${TG} ]]; then
case ${DEVICE} in
"marlin") export CHAT_ID=-1001132593794 ;;
"op6") export CHAT_ID=-1001192694857 ;;
"wahoo") export CHAT_ID=-1001298464571 ;;
esac
fi ;;
"tmp")
FILE_MOVE=${FILE_MOVE_HEAD}/.tmp/${DEVICE_NAME_WEB}/${ANDROID_VERSION} ;;
"personal")
FILE_MOVE=${FILE_MOVE_HEAD}/.fk/${DEVICE_NAME_WEB} ;;
*)
die "Invalid mode specified!" ;;
esac
# Compilation variables
ARCHITECTURE=arm64
# Normal 64-bit GCC toolchain
if [[ -z ${GCC_BINARY} ]]; then
if [[ ! -d ${GCC_FOLDER} ]]; then
GCC_FOLDER=${TC_FOLDER}/${GCC_FOLDER}
[[ ! -d ${GCC_FOLDER} ]] && die "Invalid GCC folder specified!"
fi
GCC=$(find "${GCC_FOLDER}/bin" \( -type f -o -type l \) -name '*-gcc' | head -n1)
fi
[[ -z ${GCC} ]] && die "64-bit GCC toolchain could not be found!"
CC=${GCC%gcc}
# 32-bit GCC toolchain if required
if [[ -n ${GCC_32_BIT} || -n ${GCC_32_BIT_FOLDER} ]]; then
if [[ -z ${GCC_32_BIT} ]]; then
if [[ ! -d ${GCC_32_BIT_FOLDER} ]]; then
GCC_32_BIT_FOLDER=${TC_FOLDER}/${GCC_32_BIT_FOLDER}
[[ ! -d ${GCC_32_BIT_FOLDER} ]] && die "Invalid GCC folder specified!"
fi
GCC_32_BIT=$(find "${GCC_32_BIT_FOLDER}/bin" \( -type f -o -type l \) -name '*-gcc' | head -n1)
fi
[[ -z ${GCC_32_BIT} ]] && die "32-bit GCC toolchain could not be found!"
CC_32_BIT=${GCC_32_BIT%gcc}
fi
# Handle Clang
if [[ ${COMPILER} = "clang" ]]; then
if [[ ! -d ${CLANG_FOLDER} ]]; then
CLANG_FOLDER=${TC_FOLDER}/${CLANG_FOLDER}
[[ ! -d ${CLANG_FOLDER} ]] && die "Invalid Clang folder specified!"
fi
CLANG=${CLANG_FOLDER}/bin/clang
[[ ! -f ${CLANG} ]] && die "Clang toolchain could not be found!"
CLANG_VERSION=$(clang_version "${CLANG}")
case "$(basename "${CLANG_FOLDER}")" in
"flash"*)
CLANG_LD_PATH=${CLANG_FOLDER}/lib
if [[ -n ${PERSONAL} ]]; then
KCFLAGS="-mllvm -polly \
-mllvm -polly-run-dce \
-mllvm -polly-run-inliner \
-mllvm -polly-opt-fusion=max \
-mllvm -polly-ast-use-context \
-mllvm -polly-vectorizer=stripmine \
-mllvm -polly-detect-keep-going"
fi ;;
*)
CLANG_LD_PATH=${CLANG_FOLDER}/lib64 ;;
esac
LLVM_DIS=${CLANG_FOLDER}/bin/llvm-dis
fi
}
# Clean up
function clean_up() {
# Clean AnyKernel2 folder if building a zip
if [[ -z ${IMAGE_ONLY} ]]; then
cd "${ANYKERNEL2_FOLDER}" || die "AnyKernel2 source is missing!"
[[ -z ${ANYKERNEL2_BRANCH} ]] && ANYKERNEL2_BRANCH=${DEVICE_NAME_GITHUB}-flash-${AK2_SFX}
git checkout ${HIDDEN_COMMANDS:+ "-q"} ${ANYKERNEL2_BRANCH}
git clean -fxd ${HIDDEN_COMMANDS:+ "-q"}
fi
# Only checkout if kernel_branch exists and we aren't bisecting or rebasing
cd "${SOURCE_FOLDER}" || die "Kernel source is missing!"
if [[ -n ${KERNEL_BRANCH} ]]; then
[[ ${KERNEL_BRANCH} =~ "queue-" ]] && CACHED_BRANCH=$(git cb)
[[ -z $(git status | ag "bisec|rebas") ]] && git checkout ${HIDDEN_COMMANDS:+ "-q"} "${KERNEL_BRANCH}"
# Otherwise, notate current branch for changelog
else
KERNEL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
fi
[[ -z ${HIDDEN_COMMANDS} ]] && echo
}
# Make wrapper for make function for GCC
function make_wrapper_gcc() {
PATH=${BIN_FOLDER}:${PATH} make "${JOBS_FLAG}" \
ARCH=${ARCHITECTURE} \
CROSS_COMPILE="${CCACHE} ${CC}" \
CROSS_COMPILE_ARM32="${CC_32_BIT}" \
KBUILD_BUILD_VERSION="${KBUILD_BUILD_VERSION}" \
LOCALVERSION="${LOCALVERSION}" \
O="${OUT_FOLDER}" \
"${@}"
}
# Make wrapper for make function for Clang
function make_wrapper_clang() {
LD_LIBRARY_PATH="${CLANG_LD_PATH}:${LD_LIBARY_PATH}" \
PATH=${BIN_FOLDER}:${PATH} make "${JOBS_FLAG}" \
ARCH=${ARCHITECTURE} \
CC="${CCACHE} ${CLANG}" \
CLANG_TRIPLE=aarch64-linux-gnu- \
CROSS_COMPILE="${CC}" \
CROSS_COMPILE_ARM32="${CC_32_BIT}" \
HOSTCC="${CCACHE} ${CLANG}" \
KBUILD_COMPILER_STRING="${CLANG_VERSION}" \
KBUILD_BUILD_VERSION="${KBUILD_BUILD_VERSION}" \
KCFLAGS="${KCFLAGS}" \
LLVM_DIS="${LLVM_DIS}" \
LOCALVERSION="${LOCALVERSION}" \
O="${OUT_FOLDER}" \
"${@}"
}
# Silence output function
function kmake() {
# Only show errors and warnings when compiling (unless debug)
if [[ -n ${SHOW_OUTPUT} ]]; then
make_wrapper_${COMPILER} "${@}"
else
make_wrapper_${COMPILER} "${@}" |& ag --no-color --after=${AG_LINES:-10} "error:|warning:" && echo
fi
}
# Setup compilation
function generate_version() {
# This is set in the defconfig
CONFIG_LOCALVERSION="-FlashKernel"
# If the mode is not personal, add the device name to LOCALVERSION
if [[ -z ${PERSONAL} ]]; then
LOCALVERSION="-${DEVICE_NAME_FILE}"
LOCALVERSION_ZIP="${LOCALVERSION}"
fi
# Add version if present
if [[ -n ${VERSION} ]]; then
VERSION_TEXT="-v${VERSION}"
LOCALVERSION="${LOCALVERSION}${VERSION_TEXT}"
fi
# If the mode isn't stable, add hash information to the file for debugging
if [[ -z ${STABLE} ]]; then
# Furthermore, if it is a personal build, use a shorter hash length
[[ -n ${PERSONAL} ]] && NUM=8 || NUM=12
SHA="-g$(git rev-parse --verify --short=${NUM} HEAD)"
LOCALVERSION="${LOCALVERSION}${SHA}"
# Don't add the hash to the zip name for personal builds, use the date instead
if [[ -n ${PERSONAL} ]]; then
unset SHA
DATE="-$(date +%Y%m%d-%H%M)"
fi
fi
# If there are uncommitted changes, mark as dirty
if [[ $( git diff-index --name-only HEAD ) ]]; then
DIRTY="-dirty"
LOCALVERSION="${LOCALVERSION}${DIRTY}"
fi
# If REVISION exists, use it for the file
if [[ -n ${REVISION} ]]; then
REVISION_FOR_FILE=".${MODE::1}${REVISION}"
# If REVISION is greater than or equal to 1, pass it to the build system
[[ ${REVISION} -ge 1 ]] && KBUILD_BUILD_VERSION=${REVISION}
fi
# Export KERNEL_NAME based on the above information
KERNEL_NAME="${CONFIG_LOCALVERSION/-/}${LOCALVERSION_ZIP}${VERSION_TEXT}${REVISION_FOR_FILE}${SHA}${DATE}"
# Set final package names
UNSIGNED_ZIP=${KERNEL_NAME,,}-unsigned.zip
SIGNED_ZIP=${KERNEL_NAME,,}.zip
}
# Print variables
function print_variables() {
echo "${BOLD}Device:${RST} ${DEVICE}\n"
echo "${BOLD}Branch:${RST} ${KERNEL_BRANCH}\n"
[[ ${COMPILER} = "clang" ]] && echo "${BOLD}Clang:${RST} ${CLANG}\n"
echo "${BOLD}GCC:${RST} ${GCC}\n"
}
# Export version variable
function evv() {
FILE=out/include/generated/compile.h
export "$(grep "${1}" "${FILE}" | cut -d'"' -f1 | awk '{print $2}')"="$(grep "${1}" "${FILE}" | cut -d'"' -f2)"
}
# Show the version as if looking at "/proc/version"
function parse_version() {
evv UTS_VERSION
evv LINUX_COMPILE_BY
evv LINUX_COMPILE_HOST
evv LINUX_COMPILER
VERSION_STRING=$(cat out/include/config/kernel.release)
UTS_VERSION=${UTS_VERSION#*PREEMPT }
echo "${BOLD}Version:${RST} ${VERSION_STRING} (${LINUX_COMPILER})"
echo " ${LINUX_COMPILE_BY}@${LINUX_COMPILE_HOST} #${KBUILD_BUILD_NUMBER:-1}"
echo " ${UTS_VERSION}"
echo
}
# Build kernel image
function build_kernel() {
# Clean source folder
git clean -dfx ${HIDDEN_COMMANDS:+ "-q"}
# Point to proper defconfig and generate version files
kmake ${DEFCONFIG} prepare
# If we want to build with KASAN, do it now
if [[ -n ${KASAN} ]]; then
./scripts/config --file out/.config -e CONFIG_KASAN \
-e CONFIG_KASAN_INLINE \
-e CONFIG_TEST_KASAN \
-e CONFIG_KCOV \
-e CONFIG_SLUB \
-e CONFIG_SLUB_DEBUG \
-e CONFIG_SLUB_DEBUG_ON \
-d CONFIG_SLUB_DEBUG_PANIC_ON \
-d CONFIG_KASAN_OUTLINE \
-d CONFIG_KERNEL_LZ4 \
--set-val CONFIG_FRAME_WARN 0
kmake olddefconfig
fi
# If requested, disable -Werror
if [[ -n ${NO_WERROR} ]]; then
./scripts/config --file out/.config -d CONFIG_CC_WERROR
kmake olddefconfig
fi
# If requested, build the 32-bit compatible VDSO
if [[ -n ${WANTS_COMPAT_VDSO} ]]; then
./scripts/config --file out/.config -e CONFIG_COMPAT_VDSO
kmake olddefconfig
fi
if [[ -n ${PERSONAL} ]]; then
./scripts/config --file out/.config -e CONFIG_WIREGUARD_DEBUG
kmake olddefconfig
fi
# Notify Telegram of build starting
SHORT_CHANGELOG=${HOME}/shortlogs/${DEVICE_NAME_FILE,,}-${VERSION//./}
[[ ! -f ${SHORT_CHANGELOG} && -n ${TEST} ]] && die "Missing short changelog!"
if [[ -n ${TG} && -z ${ALREADY_NOTIFIED} ]]; then
{
echo "\`\`\`"
[[ ${MODE} = "rc" ]] && MODE=${MODE^^} || MODE=${MODE^}
echo "${MODE} kernel build starting..."
if [[ -n ${PERSONAL} ]]; then
echo
echo "Device: ${DEVICE_NAME_FILE}"
fi
echo
echo "Version: $(cat "${OUT_FOLDER}/include/config/kernel.release")"
echo
echo "Zip file: ${SIGNED_ZIP}"
echo
echo "Short changelog:"
tail -n +2 "${SHORT_CHANGELOG}"
echo "\`\`\`"
} > "${TG_MSG_FILE}"
notify "$(cat "${TG_MSG_FILE}")"
fi
# Make the kernel
kmake ${EXTRA_MAKE_FLAGS:+ "${EXTRA_MAKE_FLAGS[@]}"}
[[ -n ${CACHED_BRANCH} ]] && git ch ${HIDDEN_COMMANDS:+ "-q"} "${CACHED_BRANCH}"
case ${DEVICE} in
"marlin")
FILES_TO_MOVE=( "arch/arm64/boot/Image.lz4:${ANYKERNEL2_FOLDER}/kernel" )
for DTB in out/arch/arm64/boot/dts/htc/*.dtb; do
FILES_TO_MOVE+=( "${DTB#*/}:${ANYKERNEL2_FOLDER}/dtbs" )
done ;;
"op6")
FILES_TO_MOVE=( "arch/arm64/boot/Image.gz:${ANYKERNEL2_FOLDER}/kernel" )
for DTB in out/arch/arm64/boot/dts/qcom/*.dtb; do
FILES_TO_MOVE+=( "${DTB#*/}:${ANYKERNEL2_FOLDER}/dtbs" )
done ;;
"wahoo")
FILES_TO_MOVE=( "arch/arm64/boot/Image.lz4:${ANYKERNEL2_FOLDER}/kernel"
"arch/arm64/boot/dtbo.img:${ANYKERNEL2_FOLDER}" )
for DTB in out/arch/arm64/boot/dts/qcom/*.dtb; do
FILES_TO_MOVE+=( "${DTB#*/}:${ANYKERNEL2_FOLDER}/dtbs" )
done ;;
esac
# Move them if they exist, error out if not
KIMG=${FILES_TO_MOVE[0]%:*}
if [[ -f "${OUT_FOLDER}/${KIMG}" ]]; then
parse_version
echo "${GRN}$(basename "${KIMG}") compiled successfully!${RST}\n"
move_kernel_files
else
echo "${RED}Kernel image not found!${RST}\n"
ending_info
fi
}
# Move kernel files after compilation
function move_kernel_files() {
if [[ -z ${IMAGE_ONLY} ]]; then
# Move the kernel image
for FTM in "${FILES_TO_MOVE[@]}"; do
mkdir -p "${FTM#*:}"
cp "${OUT_FOLDER}/${FTM%:*}" "${FTM#*:}"
done
echo "Version: $(cat "${OUT_FOLDER}/include/config/kernel.release")" > "${ANYKERNEL2_FOLDER}/version"
# Grab modules too if they are present
MODULES="$(find "${OUT_FOLDER}" -name '*.ko')"
if [[ -n ${MODULES} ]]; then
MODULES_FOLDER=${ANYKERNEL2_FOLDER}/ramdisk/modules
mkdir -p "${MODULES_FOLDER}"
for MOD in ${MODULES}; do
"${CC}"strip --strip-unneeded "${MOD}"
"${OUT_FOLDER}"/scripts/sign-file sha512 \
"${OUT_FOLDER}/certs/signing_key.pem" \
"${OUT_FOLDER}/certs/signing_key.x509" \
"${MOD}"
cp "${MOD}" "${MODULES_FOLDER}"
done
fi
fi
}
# Package zip
function package_zip() {
header "PACKAGING AND SIGNING ZIP"
echo "${BOLD}Signed zip name:${RST} ${SIGNED_ZIP}\n"
[[ -n ${MODULES_FOLDER} ]] && \
echo "${BOLD}Kernel modules:${RST} $(find "${MODULES_FOLDER}" -type f | wc -l)\n"
cd "${ANYKERNEL2_FOLDER}" || die "AnyKernel2 folder shift failed!"
# Package the zip without the README
zip ${HIDDEN_COMMANDS:+ "-q"} -r9 "${UNSIGNED_ZIP}" -x README.md -- *
# Sign zip
java -jar "${BIN_FOLDER}/zipsigner-2.1.jar" \
"${UNSIGNED_ZIP}" \
"${SIGNED_ZIP}"
# If packaging failed, error out
if [[ ! -f ${SIGNED_ZIP} ]]; then
echo "${RED}Kernel zip not found after signing!${RST}\n"
ending_info
else
echo "${GRN}Kernel zip successfully generated!${RST}\n"
fi
}
# Setup folders
function setup_folders() {
echo " - Setting up final folders"
# If FILE_MOVE doesn't exist, make it
[[ ! -d ${FILE_MOVE} ]] && mkdir -p "${FILE_MOVE}"
FILE_MOVE=$(readlink -f "${FILE_MOVE}")
# If it isn't a stable build, clean the folder
if [[ -z ${STABLE} ]]; then
if [[ -z ${NOCLEAN} ]]; then
if [[ -n ${PERSONAL} ]]; then
rm -rf "${FILE_MOVE:?}"/*
else
rm -rf "${FILE_MOVE:?}"/*"${MATCH_FLAG}"*
fi
fi
else
# Clean old test builds
cd "${FILE_MOVE}/.." || die "Shift to zip folder failed!"
rm -rf {alpha,beta,rc,tmp}/*
git add .
if [[ -n $(git status --porcelain) ]]; then
git commit --gpg-sign ${HIDDEN_COMMANDS:+ "--quiet"} --signoff --message "${DEVICE_NAME_FILE}: Remove old test builds"
git push ${HIDDEN_COMMANDS:+ "--quiet"}
fi
# Move the old files to an archive folder unless no clean is requested
if [[ -z ${NOCLEAN} ]]; then
[[ ! -d ${FILE_MOVE_OLD} ]] && mkdir -p "${FILE_MOVE_OLD}"
rm -f "${FILE_MOVE}"/{.version,.changelog}
OLD_FILES="$(find "${FILE_MOVE}" -maxdepth 1 -type f -name "*${MATCH_FLAG,,}*" 2>/dev/null)"
if [[ -n ${OLD_FILES} ]]; then
for OLD_FILE in ${OLD_FILES}; do
mv ${VERBOSE_COMMANDS:+ "-v"} "${OLD_FILE}" "${FILE_MOVE_OLD}"
done
fi
fi
fi
}
# Move files and generate md5 files
function move_flashable_files() {
echo " - Moving files"
(
cd "${FILE_MOVE}" || die "Shift to ending folder failed!"
mv "${ANYKERNEL2_FOLDER}/${SIGNED_ZIP}" "${FILE_MOVE}"
md5sum "${SIGNED_ZIP}" > "${FILE_MOVE}/${SIGNED_ZIP}.md5"
if [[ -n ${TEST} || -n ${STABLE} ]]; then
cp "${SHORT_CHANGELOG}" "${KERNEL_NAME,,}-changelog.txt"
if [[ -n ${STABLE} ]]; then
ln -sf "${KERNEL_NAME,,}-changelog.txt" .changelog
echo "${VERSION}" > .version
fi
fi
)
}
#Ppush kernel files (zips/images) to GitHub
function push_flashable_files() {
echo " - Pushing files to GitHub"
cd "${FILE_MOVE}" || die "Zip folder doesn't exist!"
git add ..
git commit --gpg-sign ${HIDDEN_COMMANDS:+ "--quiet"} --signoff --message "${KERNEL_NAME}"
git push ${HIDDEN_COMMANDS:+ "--quiet"}
}
# Tag for stable releases
function tag_stable() {
echo " - Tagging ${KERNEL_NAME} as stable"
for TAG_FOLDER in ${SOURCE_FOLDER} ${ANYKERNEL2_FOLDER}; do
cd "${TAG_FOLDER}" || die "Issue with moving into ${TAG_FOLDER}!"
git tag --sign --annotate "${KERNEL_NAME}" --message "${KERNEL_NAME}
$(tail -n +2 "${SHORT_CHANGELOG}")"
git push ${HIDDEN_COMMANDS:+ "--quiet"} origin "${KERNEL_NAME}"
done
}
# After build tasks
function after_build_tasks() {
if [[ -z ${IMAGE_ONLY} ]]; then
package_zip
header "SHUFFLING FILES"
setup_folders
move_flashable_files
if [[ -z ${PERSONAL} ]]; then
[[ -z ${TMP} ]] && push_flashable_files
[[ -n ${STABLE} ]] && tag_stable
fi
echo
fi
}
# Print file info
function ending_info() {
header "SCRIPT FINISHED!"
END=$(date +"%s")
DURATION=$(format_time "${START}" "${END}")
{
echo
echo
echo
echo "COMMAND: $(basename "${BASH_SOURCE[0]}") ${PARAMS}"
echo "DATE: $(date +"%A %B %d %Y %H:%M %Z")"
} >> "${LOG}"
if [[ "${*}" =~ "-s" ]]; then
{
echo "DURATION: ${DURATION,,}"
echo "VERSION: ${VERSION_STRING} (${LINUX_COMPILER})"
echo " ${LINUX_COMPILE_BY}@${LINUX_COMPILE_HOST} #${KBUILD_BUILD_NUMBER:-1}"
echo " ${UTS_VERSION}"
} >> "${LOG}"
if [[ -z ${IMAGE_ONLY} ]]; then
FILE_LOCATION=$(web_link "${FILE_MOVE}")
echo "LINK: ${FILE_LOCATION}" >> "${LOG}"
echo "${RED}FILE LOCATION: ${FILE_LOCATION}"
echo "SIZE: $(du -h "${FILE_MOVE}/${SIGNED_ZIP}" | awk '{print $1}')${RST}"
fi
if [[ -n ${TG} ]]; then
{
echo "\`${MODE} kernel build successful in ${DURATION,,}\`"
if [[ -z ${IMAGE_ONLY} ]]; then
echo
echo "[${FILE_LOCATION}](${FILE_LOCATION})"
echo
echo "\`\`\`"
echo "Short changelog:"
tail -n +2 "${SHORT_CHANGELOG}"
echo "\`\`\`"
fi
} > "${TG_MSG_FILE}"
notify "$(cat "${TG_MSG_FILE}")"
fi
else
echo "DURATION: ${DURATION,,}" >> "${LOG}"
if [[ -n ${TG} ]]; then
load_botinfo
notify "\`Build failed in ${DURATION,,}\`"
fi
fi
echo "${RED}DURATION: ${DURATION}${RST}"
echo
echo "\a"
exit
}
################
# #
# SCRIPT START #
# #
################
source "$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" || return; pwd)/common"
trap 'echo; die "Manually aborted!"' SIGINT SIGTERM
# Start tracking time
START=$(date +"%s")
clear
echo "${RED}"
echo
echo "================================================================================================"
echo
echo
echo " ___________________________________ __ ______ _______________________ ________________ "
echo " ___ ____/__ /___ |_ ___/__ / / / ___ //_/__ ____/__ __ \__ | / /__ ____/__ / "
echo " __ /_ __ / __ /| |____ \__ /_/ / __ ,< __ __/ __ /_/ /_ |/ /__ __/ __ / "
echo " _ __/ _ /___ ___ |___/ /_ __ / _ /| | _ /___ _ _, _/_ /| / _ /___ _ /___ "
echo " /_/ /_____/_/ |_/____/ /_/ /_/ /_/ |_| /_____/ /_/ |_| /_/ |_/ /_____/ /_____/ "
echo
echo
echo
echo "================================================================================================"
echo
#################
# MAKING KERNEL #
#################
header "COMPILING KERNEL"
# Parse command line parameters and setup options
parse_parameters "${@}"
device_flags
generate_variables
# Clean up the source
clean_up
# Generate variables such as versions
generate_version
# Print relevant variables
print_variables
# Build kernel
${BUILD_FUNCTION}
# After build tasks
after_build_tasks
# Print file info and time
ending_info -s