-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscript.sh
1185 lines (1004 loc) · 37.8 KB
/
script.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
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
: <<'notice'
*
* Script information:
* Universal and advanced script for Android kernel building.
* Indentation space is 4 and is space characters.
*
* SPDX-License-Identifier: GPL-3.0
*
* Copyright (C) Dimitar Yurukov <mscalindt@protonmail.com>
*
notice
function variables() {
ESSENTIAL_VARIABLES() {
TOOLCHAIN_DIR=DronaKernel/build/non-oc/toolchain
KERNEL_DIR=DronaKernel/build/non-oc/kernel
KERNEL_OUTPUT_DIR=workdir
KERNEL_DEFCONFIG=drona-rosy_defconfig
KERNEL_ARCH=arm64
}
SCRIPT_VARIABLES() {
USE_CCACHE=1
ZIP_BUILDER=1
RECURSIVE_KERNEL_CLONE=0
NORMAL_COMPILATION=0
}
OPTIONAL_VARIABLES() {
anykernel() {
essential_variables() {
AK_DIR=workdir/ak3
KERNEL_NAME=dronakernel
}
remote_variables() {
AK_REPO=https://github.com/shivamjadon/AnyKernel3
AK_BRANCH=master
}
zip_filename_variables() {
APPEND_VERSION=0
APPEND_DEVICE=0
APPEND_ANDROID_TARGET=0
APPEND_DATE=0
CUSTOM_ZIP_NAME=Drona-Kernel-Rosy-V4
}
essential_variables
remote_variables
zip_filename_variables
}
toolchain() {
remote_variables() {
TOOLCHAIN_REPO=https://github.com/shivamjadon/aarch64-linux-android-4.9
TOOLCHAIN_BRANCH=master
}
remote_variables
}
clang() {
essential_variables() {
CLANG_DIR=
CLANG_BIN=
CLANG_PREFIX=
}
remote_variables() {
CLANG_REPO=
CLANG_BRANCH=
}
essential_variables
remote_variables
}
kernel() {
remote_variables() {
KERNEL_REPO=https://github.com/shivamjadon/Drona_Kernel_Rosy
KERNEL_BRANCH=q-caf
}
options() {
KERNEL_BUILD_USER=shivam
KERNEL_BUILD_HOST=veronica
KERNEL_LOCALVERSION=-3.18
}
remote_variables
options
}
miscellaneous() {
sync_variables() {
# NOTE: True sync. Any local changes are discarded. All remote changes are pulled.
SYNC_AK_DIR=0
SYNC_TC_DIR=0
SYNC_KERNEL_DIR=0
}
sync_variables
}
anykernel
toolchain
clang
kernel
miscellaneous
}
ESSENTIAL_VARIABLES
SCRIPT_VARIABLES
OPTIONAL_VARIABLES
}
function additional_variables() {
red='\033[1;31m'
green='\033[1;32m'
white='\033[1;37m'
darkwhite='\033[0;37m'
cyan='\033[1;36m'
agrsv_rm=1
akc=nah
tcc=nope
cgc=no
klc=noway
clg=bad
out=and
nml=boujee
ak_clone_depth=1
tc_clone_depth=1
kl_clone_depth=10
current_date=$(date +'%Y%m%d')
scachefile="$HOME"/.bkscache
scachefile2="$HOME"/.bkscache2
ak_dir="$HOME"/${AK_DIR}
tc_dir="$HOME"/${TOOLCHAIN_DIR}
cg_dir="$HOME"/${CLANG_DIR}
kl_dir="$HOME"/${KERNEL_DIR}
out_dir="$HOME"/${KERNEL_OUTPUT_DIR}
ak_kl_img="$HOME"/${AK_DIR}/Image.gz-dtb
out_kl_img="$HOME"/${KERNEL_OUTPUT_DIR}/arch/arm64/boot/Image.gz-dtb
nml_kl_img="$HOME"/${KERNEL_DIR}/arch/arm64/boot/Image.gz-dtb
}
function env_checks() {
bash_check() {
bash_ver=${BASH_VERSION}
bash_ver_cut=$(printf "%s" "$bash_ver" | cut -c -1)
if [ "$bash_ver_cut" = "2" ] || [ "$bash_ver_cut" = "3" ]; then
printf "\n%bThis script requires bash 4+%b\n\n" "$red" "$darkwhite"
exit 1
fi
}
root_check() {
if [ $EUID = 0 ]; then
printf "\n%bYou should not run this script as root.%b\n\n" "$red" "$darkwhite"
exit 1
fi
}
bash_check
root_check
}
function traps() {
abort() {
printf "\n\n%bThe script was forcefully aborted.%b\n\n" "$white" "$darkwhite"
exit 130
}
trap abort SIGINT
}
function die_codes() {
die_20() {
printf "\n%bYou changed one or more variables' names.%b\n\n" "$red" "$darkwhite"
exit 20
}
die_21() {
printf "\n%bYou did not define all essential variables for the current configuration.%b\n\n" "$red" "$darkwhite"
exit 21
}
die_30() {
printf "\n%bUnexpected path issue.%b\n\n" "$red" "$darkwhite"
exit 30
}
die_31() {
printf "\n%bThe cloning of a source failed.%b\n\n" "$red" "$darkwhite"
exit 31
}
die_40() {
printf "\n%bThe kernel was not compiled correctly.%b\n\n" "$red" "$darkwhite"
exit 40
}
}
function configuration_checker() {
changed_variables() {
if [ ! -v TOOLCHAIN_DIR ] || [ ! -v KERNEL_DIR ] || \
[ ! -v KERNEL_OUTPUT_DIR ] || [ ! -v KERNEL_DEFCONFIG ] || \
[ ! -v KERNEL_ARCH ]; then
die_20
fi
if [ ! -v USE_CCACHE ] || [ ! -v ZIP_BUILDER ] || \
[ ! -v RECURSIVE_KERNEL_CLONE ] || [ ! -v NORMAL_COMPILATION ]; then
die_20
fi
if [ ! -v AK_DIR ] || [ ! -v KERNEL_NAME ] || \
[ ! -v AK_REPO ] || [ ! -v AK_BRANCH ] || \
[ ! -v APPEND_VERSION ] || [ ! -v APPEND_DEVICE ] || \
[ ! -v APPEND_ANDROID_TARGET ] || [ ! -v APPEND_DATE ] || \
[ ! -v CUSTOM_ZIP_NAME ]; then
die_20
fi
if [ ! -v TOOLCHAIN_REPO ] || [ ! -v TOOLCHAIN_BRANCH ]; then
die_20
fi
if [ ! -v CLANG_DIR ] || [ ! -v CLANG_BIN ] || \
[ ! -v CLANG_PREFIX ] || [ ! -v CLANG_REPO ] || \
[ ! -v CLANG_BRANCH ]; then
die_20
fi
if [ ! -v KERNEL_REPO ] || [ ! -v KERNEL_BRANCH ] || \
[ ! -v KERNEL_BUILD_USER ] || [ ! -v KERNEL_BUILD_HOST ] || \
[ ! -v KERNEL_LOCALVERSION ]; then
die_20
fi
if [ ! -v SYNC_AK_DIR ] || [ ! -v SYNC_TC_DIR ] || \
[ ! -v SYNC_KERNEL_DIR ]; then
die_20
fi
}
undefined_variables() {
if [ -z "$TOOLCHAIN_DIR" ] || [ -z "$KERNEL_DIR" ] || \
[ -z "$KERNEL_OUTPUT_DIR" ] || [ -z "$KERNEL_DEFCONFIG" ] || \
[ -z "$KERNEL_ARCH" ]; then
die_21
fi
if [ -n "$AK_DIR" ]; then
if [ -z "$KERNEL_NAME" ]; then
die_21
fi
fi
if [ -n "$CLANG_DIR" ]; then
if [ -z "$CLANG_BIN" ] || [ -z "$CLANG_PREFIX" ]; then
die_21
fi
fi
}
missing_variables() {
if [ ! -d "$tc_dir" ] && [ -z "$TOOLCHAIN_REPO" ] && [ -z "$TOOLCHAIN_BRANCH" ]; then
printf "\n%bToolchain is missing, and you did not define repo and branch variables for it.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ ! -d "$kl_dir" ] && [ -z "$KERNEL_REPO" ] && [ -z "$KERNEL_BRANCH" ]; then
printf "\n%bKernel is missing, and you did not define repo and branch variables for it.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ -n "$AK_DIR" ]; then
if [ ! -d "$ak_dir" ] && [ -z "$AK_REPO" ] && [ -z "$AK_BRANCH" ]; then
printf "\n%bAnyKernel is missing, and you did not define repo and branch variables for it.%b\n\n" "$red" "$darkwhite"
exit 1
fi
fi
if [ -n "$CLANG_DIR" ]; then
if [ ! -d "$cg_dir" ] && [ -z "$CLANG_REPO" ] && [ -z "$CLANG_BRANCH" ]; then
printf "\n%bClang is missing, and you did not define repo and branch variables for it.%b\n\n" "$red" "$darkwhite"
exit 1
fi
fi
}
incorrect_variables() {
if [ "$KERNEL_ARCH" != "arm64" ] && [ "$KERNEL_ARCH" != "arm" ]; then
printf "\n%bIncorrect input for KERNEL_ARCH variable.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$ZIP_BUILDER" = 1 ] && [ -z "$AK_DIR" ]; then
printf "\n%bZip builder is enabled, but AK directory is not defined...%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ -n "$CLANG_DIR" ] && [ "$NORMAL_COMPILATION" = 1 ]; then
printf "\n%bYou cannot do normal compilation with Clang.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$SYNC_AK_DIR" = 1 ] && [ -z "$AK_DIR" ]; then
printf "\n%bSync for AK is enabled, but AK directory is not defined...%b\n\n" "$red" "$darkwhite"
exit 1
fi
}
check_the_toggles() {
if [ "$USE_CCACHE" != 0 ] && [ "$USE_CCACHE" != 1 ]; then
printf "\n%bIncorrect USE_CCACHE variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$ZIP_BUILDER" != 0 ] && [ "$ZIP_BUILDER" != 1 ]; then
printf "\n%bIncorrect ZIP_BUILDER variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$RECURSIVE_KERNEL_CLONE" != 0 ] && [ "$RECURSIVE_KERNEL_CLONE" != 1 ]; then
printf "\n%bIncorrect RECURSIVE_KERNEL_CLONE variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$NORMAL_COMPILATION" != 0 ] && [ "$NORMAL_COMPILATION" != 1 ]; then
printf "\n%bIncorrect NORMAL_COMPILATION variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$APPEND_DATE" != 0 ] && [ "$APPEND_DATE" != 1 ]; then
printf "\n%bIncorrect APPEND_DATE variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$SYNC_AK_DIR" != 0 ] && [ "$SYNC_AK_DIR" != 1 ]; then
printf "\n%bIncorrect SYNC_AK_DIR variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$SYNC_TC_DIR" != 0 ] && [ "$SYNC_TC_DIR" != 1 ]; then
printf "\n%bIncorrect SYNC_TC_DIR variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$SYNC_KERNEL_DIR" != 0 ] && [ "$SYNC_KERNEL_DIR" != 1 ]; then
printf "\n%bIncorrect SYNC_KERNEL_DIR variable, only 0 or 1 is allowed as input for toggles.%b\n\n" "$red" "$darkwhite"
exit 1
fi
}
check_for_slash() {
tcd_first_char=$(printf "%s" "$TOOLCHAIN_DIR" | cut -c -1)
tcd_last_char=$(printf "%s" "$TOOLCHAIN_DIR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c -1)
kld_first_char=$(printf "%s" "$KERNEL_DIR" | cut -c -1)
kld_last_char=$(printf "%s" "$KERNEL_DIR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c -1)
kldo_first_char=$(printf "%s" "$KERNEL_OUTPUT_DIR" | cut -c -1)
kldo_last_char=$(printf "%s" "$KERNEL_OUTPUT_DIR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c -1)
if [ "$tcd_first_char" = "/" ]; then
printf "\n%bRemove the first slash (/) in TOOLCHAIN_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
elif [ "$tcd_last_char" = "/" ]; then
printf "\n%bRemove the last slash (/) in TOOLCHAIN_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$kld_first_char" = "/" ]; then
printf "\n%bRemove the first slash (/) in KERNEL_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
elif [ "$kld_last_char" = "/" ]; then
printf "\n%bRemove the last slash (/) in KERNEL_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ "$kldo_first_char" = "/" ]; then
printf "\n%bRemove the first slash (/) in KERNEL_OUTPUT_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
elif [ "$kldo_last_char" = "/" ]; then
printf "\n%bRemove the last slash (/) in KERNEL_OUTPUT_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
fi
if [ -n "$AK_DIR" ]; then
akd_first_char=$(printf "%s" "$AK_DIR" | cut -c -1)
akd_last_char=$(printf "%s" "$AK_DIR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c -1)
if [ "$akd_first_char" = "/" ]; then
printf "\n%bRemove the first slash (/) in AK_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
elif [ "$akd_last_char" = "/" ]; then
printf "\n%bRemove the last slash (/) in AK_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
fi
fi
if [ -n "$CLANG_DIR" ]; then
cgd_first_char=$(printf "%s" "$CLANG_DIR" | cut -c -1)
cgd_last_char=$(printf "%s" "$CLANG_DIR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c -1)
if [ "$cgd_first_char" = "/" ]; then
printf "\n%bRemove the first slash (/) in CLANG_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
elif [ "$cgd_last_char" = "/" ]; then
printf "\n%bRemove the last slash (/) in CLANG_DIR variable.%b\n\n" "$red" "$darkwhite"
exit 1
fi
fi
}
changed_variables
undefined_variables
missing_variables
incorrect_variables
check_the_toggles
check_for_slash
}
function package_checker() {
ccache_binary() {
if [ "$USE_CCACHE" = 1 ]; then
if ! command -v ccache > /dev/null 2>&1; then
printf "\n%bccache not found.%b\n\n" "$red" "$darkwhite"
if command -v sudo > /dev/null 2>&1; then
if command -v apt > /dev/null 2>&1; then
printf "%bTIP: sudo apt install ccache%b\n\n" "$white" "$darkwhite"
elif command -v pacman > /dev/null 2>&1; then
printf "%bTIP: sudo pacman -S ccache%b\n\n" "$white" "$darkwhite"
elif command -v dnf > /dev/null 2>&1; then
printf "%bTIP: sudo dnf install ccache%b\n\n" "$white" "$darkwhite"
elif command -v zypper > /dev/null 2>&1; then
printf "%bTIP: sudo zypper install ccache%b\n\n" "$white" "$darkwhite"
elif command -v emerge > /dev/null 2>&1; then
printf "%bTIP: sudo emerge -a ccache%b\n\n" "$white" "$darkwhite"
fi
else
if command -v apt > /dev/null 2>&1; then
printf "%bTIP: su root -c 'apt install ccache'%b\n\n" "$white" "$darkwhite"
elif command -v pacman > /dev/null 2>&1; then
printf "%bTIP: su root -c 'pacman -S ccache'%b\n\n" "$white" "$darkwhite"
elif command -v dnf > /dev/null 2>&1; then
printf "%bTIP: su root -c 'dnf install ccache'%b\n\n" "$white" "$darkwhite"
elif command -v zypper > /dev/null 2>&1; then
printf "%bTIP: su root -c 'zypper install ccache'%b\n\n" "$white" "$darkwhite"
elif command -v emerge > /dev/null 2>&1; then
printf "%bTIP: su root -c 'emerge -a ccache'%b\n\n" "$white" "$darkwhite"
fi
fi
exit 1
fi
fi
}
git_binary() {
if [ -n "$AK_REPO" ] || [ -n "$AK_BRANCH" ] || \
[ -n "$TOOLCHAIN_REPO" ] || [ -n "$TOOLCHAIN_BRANCH" ] || \
[ -n "$CLANG_REPO" ] || [ -n "$CLANG_BRANCH" ] || \
[ -n "$KERNEL_REPO" ] || [ -n "$KERNEL_BRANCH" ] || \
[ "$SYNC_AK_DIR" = 1 ] || [ "$SYNC_TC_DIR" = 1 ] || \
[ "$SYNC_KERNEL_DIR" = 1 ]; then
if ! command -v git > /dev/null 2>&1; then
printf "\n%bgit not found.%b\n\n" "$red" "$darkwhite"
if command -v sudo > /dev/null 2>&1; then
if command -v apt > /dev/null 2>&1; then
printf "%bTIP: sudo apt install git%b\n\n" "$white" "$darkwhite"
elif command -v pacman > /dev/null 2>&1; then
printf "%bTIP: sudo pacman -S git%b\n\n" "$white" "$darkwhite"
elif command -v dnf > /dev/null 2>&1; then
printf "%bTIP: sudo dnf install git%b\n\n" "$white" "$darkwhite"
elif command -v zypper > /dev/null 2>&1; then
printf "%bTIP: sudo zypper install git%b\n\n" "$white" "$darkwhite"
elif command -v emerge > /dev/null 2>&1; then
printf "%bTIP: sudo emerge -a git%b\n\n" "$white" "$darkwhite"
fi
else
if command -v apt > /dev/null 2>&1; then
printf "%bTIP: su root -c 'apt install git'%b\n\n" "$white" "$darkwhite"
elif command -v pacman > /dev/null 2>&1; then
printf "%bTIP: su root -c 'pacman -S git'%b\n\n" "$white" "$darkwhite"
elif command -v dnf > /dev/null 2>&1; then
printf "%bTIP: su root -c 'dnf install git'%b\n\n" "$white" "$darkwhite"
elif command -v zypper > /dev/null 2>&1; then
printf "%bTIP: su root -c 'zypper install git'%b\n\n" "$white" "$darkwhite"
elif command -v emerge > /dev/null 2>&1; then
printf "%bTIP: su root -c 'emerge -a git'%b\n\n" "$white" "$darkwhite"
fi
fi
exit 1
fi
fi
}
zip_binary() {
if [ "$ZIP_BUILDER" = 1 ]; then
if ! command -v zip > /dev/null 2>&1; then
printf "\n%bzip not found.%b\n\n" "$red" "$darkwhite"
if command -v sudo > /dev/null 2>&1; then
if command -v apt > /dev/null 2>&1; then
printf "%bTIP: sudo apt install zip%b\n\n" "$white" "$darkwhite"
elif command -v pacman > /dev/null 2>&1; then
printf "%bTIP: sudo pacman -S zip%b\n\n" "$white" "$darkwhite"
elif command -v dnf > /dev/null 2>&1; then
printf "%bTIP: sudo dnf install zip%b\n\n" "$white" "$darkwhite"
elif command -v zypper > /dev/null 2>&1; then
printf "%bTIP: sudo zypper install zip%b\n\n" "$white" "$darkwhite"
elif command -v emerge > /dev/null 2>&1; then
printf "%bTIP: sudo emerge -a zip%b\n\n" "$white" "$darkwhite"
fi
else
if command -v apt > /dev/null 2>&1; then
printf "%bTIP: su root -c 'apt install zip'%b\n\n" "$white" "$darkwhite"
elif command -v pacman > /dev/null 2>&1; then
printf "%bTIP: su root -c 'pacman -S zip'%b\n\n" "$white" "$darkwhite"
elif command -v dnf > /dev/null 2>&1; then
printf "%bTIP: su root -c 'dnf install zip'%b\n\n" "$white" "$darkwhite"
elif command -v zypper > /dev/null 2>&1; then
printf "%bTIP: su root -c 'zypper install zip'%b\n\n" "$white" "$darkwhite"
elif command -v emerge > /dev/null 2>&1; then
printf "%bTIP: su root -c 'emerge -a zip'%b\n\n" "$white" "$darkwhite"
fi
fi
exit 1
fi
fi
}
ccache_binary
git_binary
zip_binary
}
function cloning() {
anykernel() {
if [ -n "$AK_DIR" ]; then
if [ ! -d "$ak_dir" ]; then
akc=y
printf "\n%bStarting clone of AK with depth %d...%b\n" "$white" "$ak_clone_depth" "$darkwhite"
git clone --branch "${AK_BRANCH}" --depth "${ak_clone_depth}" "${AK_REPO}" "${ak_dir}"
fi
fi
}
toolchain() {
if [ ! -d "$tc_dir" ]; then
tcc=y
printf "\n%bStarting clone of the toolchain with depth %d...%b\n" "$white" "$tc_clone_depth" "$darkwhite"
git clone --branch "${TOOLCHAIN_BRANCH}" --depth "${tc_clone_depth}" "${TOOLCHAIN_REPO}" "${tc_dir}"
fi
}
clang() {
if [ -n "$CLANG_DIR" ]; then
if [ ! -d "$cg_dir" ]; then
cgc=y
printf "\n%bStarting clone of Clang with depth %d...%b\n" "$white" "$tc_clone_depth" "$darkwhite"
git clone --branch "${CLANG_BRANCH}" --depth "${tc_clone_depth}" "${CLANG_REPO}" "${cg_dir}"
fi
fi
}
kernel() {
if [ ! -d "$kl_dir" ]; then
klc=y
printf "\n%bStarting clone of the kernel with depth %d...%b\n" "$white" "$kl_clone_depth" "$darkwhite"
if [ "$RECURSIVE_KERNEL_CLONE" = 1 ]; then
git clone --recursive --branch "${KERNEL_BRANCH}" --depth "${kl_clone_depth}" "${KERNEL_REPO}" "${kl_dir}"
else
git clone --branch "${KERNEL_BRANCH}" --depth "${kl_clone_depth}" "${KERNEL_REPO}" "${kl_dir}"
fi
fi
}
check_directories() {
if [ -n "$AK_DIR" ]; then
if [ ! -d "$ak_dir" ]; then
die_31
fi
fi
if [ ! -d "$tc_dir" ]; then
die_31
fi
if [ -n "$CLANG_DIR" ]; then
if [ ! -d "$cg_dir" ]; then
die_31
fi
fi
if [ ! -d "$kl_dir" ]; then
die_31
fi
}
sync_directories() {
if [ "$SYNC_AK_DIR" = 1 ]; then
if [ "$akc" != y ]; then
printf "\n%bStarting sync of AK source...%b\n" "$white" "$darkwhite"
cd "${ak_dir}" || die_30
git reset --hard "@{upstream}"
git clean -fd
git pull --rebase=preserve
fi
fi
if [ "$SYNC_TC_DIR" = 1 ]; then
if [ "$tcc" != y ]; then
printf "\n%bStarting sync of the toolchain source...%b\n" "$white" "$darkwhite"
cd "${tc_dir}" || die_30
git reset --hard "@{upstream}"
git clean -fd
git pull --rebase=preserve
fi
if [ -n "$CLANG_DIR" ]; then
if [ "$cgc" != y ]; then
printf "\n%bStarting sync of Clang source...%b\n" "$white" "$darkwhite"
cd "${cg_dir}" || die_30
git reset --hard "@{upstream}"
git clean -fd
git pull --rebase=preserve
fi
fi
fi
if [ "$SYNC_KERNEL_DIR" = 1 ]; then
if [ "$klc" != y ]; then
printf "\n%bStarting sync of the kernel source...%b\n" "$white" "$darkwhite"
cd "${kl_dir}" || die_30
git reset --hard "@{upstream}"
git clean -fd
git pull --rebase=preserve
fi
fi
}
anykernel
toolchain
clang
kernel
check_directories
sync_directories
}
function choices() {
compilation_method() {
if [ -n "$CLANG_DIR" ]; then
clg=1
printf "\n%bClang detected, starting compilation.%b\n\n" "$white" "$darkwhite"
elif [ "$NORMAL_COMPILATION" = 0 ]; then
out=1
printf "\n%bStarting output folder compilation.%b\n\n" "$white" "$darkwhite"
else
nml=1
printf "\n%bStarting normal compilation.%b\n\n" "$white" "$darkwhite"
fi
}
compilation_method
}
function automatic_configuration() {
set_subarch() {
if [ "$KERNEL_ARCH" = "arm64" ]; then
kernel_subarch=arm64
else
kernel_subarch=arm
fi
}
get_toolchain_prefix() {
cd "${tc_dir}"/lib/gcc || die_30
cd -- * || die_30
tc_prefix=$(basename "$PWD")-
}
ccache_path() {
if [ "$USE_CCACHE" = 1 ]; then
ccache_loc=$(command -v ccache)
fi
}
user() {
if [ -z "$KERNEL_BUILD_USER" ]; then
idkme=$(id -un)
fi
}
host() {
if [ -z "$KERNEL_BUILD_HOST" ]; then
idkmy=$(uname -n)
fi
}
set_subarch
get_toolchain_prefix
ccache_path
user
host
}
function time_log_start1() {
start1=$(date +'%s')
}
function compilation() {
clang() {
if [ "$clg" = 1 ]; then
cd "${kl_dir}" || die_30
if [ -n "$KERNEL_BUILD_USER" ]; then
export KBUILD_BUILD_USER=${KERNEL_BUILD_USER}
else
export KBUILD_BUILD_USER=${idkme}
fi
if [ -n "$KERNEL_BUILD_HOST" ]; then
export KBUILD_BUILD_HOST=${KERNEL_BUILD_HOST}
else
export KBUILD_BUILD_HOST=${idkmy}
fi
if [ -n "$KERNEL_LOCALVERSION" ]; then
export LOCALVERSION=${KERNEL_LOCALVERSION}
fi
export ARCH=${KERNEL_ARCH}
export SUBARCH=${kernel_subarch}
make O="${out_dir}" \
ARCH="${KERNEL_ARCH}" \
"${KERNEL_DEFCONFIG}"
if [ "$USE_CCACHE" = 1 ]; then
cpaths="${ccache_loc} ${cg_dir}/bin:${tc_dir}/bin:${PATH}"
else
cpaths="${cg_dir}/bin:${tc_dir}/bin:${PATH}"
fi
tc_paths=${cpaths} \
make O="${out_dir}" \
ARCH="${KERNEL_ARCH}" \
CC="${CLANG_BIN}" \
CLANG_TRIPLE="${CLANG_PREFIX}" \
CROSS_COMPILE="${tc_prefix}" \
-j"$(nproc --all)"
makeexit1=$(printf "%d" "$?")
fi
}
output_folder() {
if [ "$out" = 1 ]; then
cd "${kl_dir}" || die_30
if [ -n "$KERNEL_BUILD_USER" ]; then
export KBUILD_BUILD_USER=${KERNEL_BUILD_USER}
else
export KBUILD_BUILD_USER=${idkme}
fi
if [ -n "$KERNEL_BUILD_HOST" ]; then
export KBUILD_BUILD_HOST=${KERNEL_BUILD_HOST}
else
export KBUILD_BUILD_HOST=${idkmy}
fi
if [ -n "$KERNEL_LOCALVERSION" ]; then
export LOCALVERSION=${KERNEL_LOCALVERSION}
fi
export ARCH=${KERNEL_ARCH}
export SUBARCH=${kernel_subarch}
if [ "$USE_CCACHE" = 1 ]; then
export CROSS_COMPILE="${ccache_loc} ${tc_dir}/bin/${tc_prefix}"
else
export CROSS_COMPILE="${tc_dir}/bin/${tc_prefix}"
fi
make O="${out_dir}" \
ARCH="${KERNEL_ARCH}" \
"${KERNEL_DEFCONFIG}"
make O="${out_dir}" \
ARCH="${KERNEL_ARCH}" \
-j"$(nproc --all)"
makeexit2=$(printf "%d" "$?")
fi
}
normal() {
if [ "$nml" = 1 ]; then
cd "${kl_dir}" || die_30
if [ -n "$KERNEL_BUILD_USER" ]; then
export KBUILD_BUILD_USER=${KERNEL_BUILD_USER}
else
export KBUILD_BUILD_USER=${idkme}
fi
if [ -n "$KERNEL_BUILD_HOST" ]; then
export KBUILD_BUILD_HOST=${KERNEL_BUILD_HOST}
else
export KBUILD_BUILD_HOST=${idkmy}
fi
if [ -n "$KERNEL_LOCALVERSION" ]; then
export LOCALVERSION=${KERNEL_LOCALVERSION}
fi
export ARCH=${KERNEL_ARCH}
export SUBARCH=${kernel_subarch}
if [ "$USE_CCACHE" = 1 ]; then
CROSS_COMPILE="${ccache_loc} ${tc_dir}/bin/${tc_prefix}"
else
CROSS_COMPILE="${tc_dir}/bin/${tc_prefix}"
fi
make "${KERNEL_DEFCONFIG}"
CROSS_COMPILE=${CROSS_COMPILE} make -j"$(nproc --all)"
makeexit3=$(printf "%d" "$?")
fi
}
clang
output_folder
normal
}
function time_log_end1() {
end1=$(date +'%s')
comptime=$((end1-start1))
}
function compilation_report() {
if [ "$clg" = 1 ]; then
if [ "$makeexit1" != 0 ]; then
die_40
fi
elif [ "$out" = 1 ]; then
if [ "$makeexit2" != 0 ]; then
die_40
fi
elif [ "$nml" = 1 ]; then
if [ "$makeexit3" != 0 ]; then
die_40
fi
fi
if [ "$clg" = 1 ] || [ "$out" = 1 ]; then
if [ -f "$out_kl_img" ]; then
printf "\n%bThe kernel is compiled successfully!%b\n\n" "$green" "$darkwhite"
else
die_40
fi
elif [ "$nml" = 1 ]; then
if [ -f "$nml_kl_img" ]; then
printf "\n%bThe kernel is compiled successfully!%b\n\n" "$green" "$darkwhite"
else
die_40
fi
fi
}
function stats() {
convert_bytes_func() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,P,E,Z,Y}B)
while ((b > 1000)); do
d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))"
b=$((b / 1000))
(( s++ ))
done
echo "$b$d ${S[$s]}"
}
get_size_of_image_in_bytes() {
if [ "$out" = 1 ]; then
bytesoutimg=$(wc -c < "${out_kl_img}")
else
bytesnmlimg=$(wc -c < "${nml_kl_img}")
fi
}
convert_bytes_of_image() {
if [ "$out" = 1 ]; then
sizeoutimg=$(convert_bytes_func "${bytesoutimg}")
else
sizenmlimg=$(convert_bytes_func "${bytesnmlimg}")
fi
}
kernel_stats() {
printf "%b> Defconfig: %s%b\n" "$white" "$KERNEL_DEFCONFIG" "$darkwhite"
if [ -n "$KERNEL_LOCALVERSION" ]; then
printf "%b> Localversion: %s%b\n" "$white" "$KERNEL_LOCALVERSION" "$darkwhite"
fi
if [ -n "$KERNEL_BUILD_USER" ]; then
printf "%b> User: %s%b\n" "$white" "$KERNEL_BUILD_USER" "$darkwhite"
else
printf "%b> User: %s%b\n" "$white" "$idkme" "$darkwhite"
fi
if [ -n "$KERNEL_BUILD_HOST" ]; then
printf "%b> Host: %s%b\n" "$white" "$KERNEL_BUILD_HOST" "$darkwhite"
else
printf "%b> Host: %s%b\n" "$white" "$idkmy" "$darkwhite"
fi
}
compilation_stats() {
comptimemin=$((comptime / 60))
comptimesec=$((comptime % 60))
if [ "$comptimemin" = 1 ] && [ "$comptimesec" = 1 ]; then
printf "%b> Compilation took: %d minute and %d second%b\n" "$white" "$comptimemin" "$comptimesec" "$darkwhite"
elif [ "$comptimemin" = 1 ] && [ "$comptimesec" != 1 ]; then
printf "%b> Compilation took: %d minute and %d seconds%b\n" "$white" "$comptimemin" "$comptimesec" "$darkwhite"
elif [ "$comptimemin" != 1 ] && [ "$comptimesec" = 1 ]; then
printf "%b> Compilation took: %d minutes and %d second%b\n" "$white" "$comptimemin" "$comptimesec" "$darkwhite"
elif [ "$comptimemin" != 1 ] && [ "$comptimesec" != 1 ]; then
printf "%b> Compilation took: %d minutes and %d seconds%b\n" "$white" "$comptimemin" "$comptimesec" "$darkwhite"
fi
if [ "$clg" = 1 ]; then
if [ "$USE_CCACHE" = 1 ]; then
printf "%b> Compilation details: out-%s-ccache%b\n" "$white" "$CLANG_BIN" "$darkwhite"
else
printf "%b> Compilation details: out-%s%b\n" "$white" "$CLANG_BIN" "$darkwhite"
fi
elif [ "$out" = 1 ]; then
if [ "$USE_CCACHE" = 1 ]; then
printf "%b> Compilation details: out-gcc-ccache%b\n" "$white" "$darkwhite"
else
printf "%b> Compilation details: out-gcc%b\n" "$white" "$darkwhite"
fi
elif [ "$nml" = 1 ]; then
if [ "$USE_CCACHE" = 1 ]; then
printf "%b> Compilation details: normal-gcc-ccache%b\n" "$white" "$darkwhite"
else
printf "%b> Compilation details: normal-gcc%b\n" "$white" "$darkwhite"
fi
fi
}
image_stats() {
read_stored_image_size() {
if [ -f "$scachefile" ]; then
grep -Fq "directory=$kl_dir" "$scachefile"
grepexit=$(printf "%d" "$?")
if [ "$grepexit" = 1 ]; then
rm -f "${scachefile}"
fi
fi
if [ -f "$scachefile" ]; then
if [ "$out" = 1 ]; then
if grep -Fq "out.kernel.image.size" "${scachefile}"; then
sizestoredoutimg=$(grep out.kernel.image.size "${scachefile}" | cut -d "=" -f2)
fi
else
if grep -Fq "nml.kernel.image.size" "${scachefile}"; then
sizestorednmlimg=$(grep nml.kernel.image.size "${scachefile}" | cut -d "=" -f2)
fi
fi
fi
}
output_image_stats() {
if [ -f "$scachefile" ]; then
if [ "$out" = 1 ]; then
if grep -Fq out.kernel.image.size "${scachefile}"; then
printf "%b> Image size: %s (PREVIOUSLY: %s)%b\n" "$white" "$sizeoutimg" "$sizestoredoutimg" "$darkwhite"
fi