-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaosp.sh
executable file
·1730 lines (1522 loc) · 58.2 KB
/
aosp.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
source $(dirname $0)/lang.sh
#1 which rom
#2 branch
AOSP_SETUP_ROOT=$(pwd)
declare -i env_run_last_return
declare -i env_run_time
# generated to avoid install deps repeatedly. EDIT env_run_time=3 or higher to skip install deps
env_run_last_return=0
env_run_time=0
aosp_source_dir_working=
aosp_setup_dir_check_ok=0
# use pkg manager
str_to_arr(){
# arg 1: string
# arg 2: split symbol
OLD_IFS="$IFS"
IFS="$2"
str_to_arr_result=($1)
IFS="$OLD_IFS"
}
# use pkg manager
pkg_mgr(){
pkg_cmd_list=(apt pacman dnf eopkg zypper)
for pkg_cmd in "${pkg_cmd_list[@]}"; do if [[ "$(command -v ${pkg_cmd})" != "" ]]; then pkg_cmd=${pkg_cmd}; break; fi; done
echo $pkg_cmd
}
######################### PATCH & FIX UNIT #########################
patch_when_low_ram(){
# a patch that fix build on low ram PC less than 25Gb
# at least 25GB recommended
get_pc_ram_raw=($(free -m | grep ${pc_mem_str}))
get_pc_ram=${get_pc_ram_raw[1]}
declare -i pc_ram
pc_ram=$get_pc_ram
get_pc_swap_ram_raw=($(free -m | sed -n '2p'))
get_pc_swap_ram=${get_pc_swap_ram_raw[1]}
declare -i pc_sawp_ram=0
pc_sawp_ram=$get_pc_swap_ram
# need to patch when ram less than 25Gb
declare -i pc_ram_patch
pc_ram_patch=0
if [[ $pc_ram -lt 25600 ]] && [[ $pc_sawp_ram -lt 30000 ]];then
echo -e "\n\033[1;32m=>\033[0m ${auto_add_ram_str_1} ${pc_ram}${auto_add_ram_str_2} $pc_sawp_ram"
pc_ram_patch=1
else
echo -e "\n\033[1;32m=>\033[0m RAM: ${pc_sawp_ram}Mb"
fi
if [[ $pc_ram_patch == 1 ]];then
# zram swap patch
if [[ ! -f /usr/local/sbin/zram-swap.sh ]];then
git clone https://github.com/foundObjects/zram-swap.git ~/zram-swap
cd ~/zram-swap && sudo ./install.sh
fi
cd $AOSP_SETUP_ROOT
sudo /usr/local/sbin/zram-swap.sh stop
sudo sed -i 's/#_zram_fixedsize="2G"/_zram_fixedsize="64G"/g' /etc/default/zram-swap
sudo /usr/local/sbin/zram-swap.sh start
# remove directory because do not need patch another time
sudo rm -rf ~/zram-swap
fi
# more patch for cmd.BuiltTool("metalava"). locate line and add java mem when running.
metalava_patch_file=${aosp_source_dir_working}/build/soong/java/droidstubs.go
echo -e "\033[1;32m=>\033[0m ${patch_out_of_mem_str} $metalava_patch_file"
if [[ -f $metalava_patch_file ]];then
declare -i locate_metalava_0
declare -i locate_metalava_1
locate_metalava_0=$(grep 'cmd.BuiltTool("metalava")' -ns $metalava_patch_file | awk -F ':' '{print $1}')
locate_metalava_1=$(grep 'Flag(config.JavacVmFlags).' -ns $metalava_patch_file | awk -F ':' '{print $1}')
declare -i locate_metalava_3=$locate_metalava_1-$locate_metalava_0
# make sure codes in the same method
if [[ $locate_metalava_3 -le 6 ]];then
# the second line declare the mem
if [[ ! $(grep 'Flag("-J-Xmx' -l $metalava_patch_file) ]];then
sed -i '/Flag(config.JavacVmFlags)./a Flag("-J-XmxMEMm")\.' $metalava_patch_file
fi
sed -i 's/Flag("-J-Xmx.*/Flag("-J-Xmx8192m")\./' $metalava_patch_file
fi
echo -e "\n\033[1;32m=>\033[0m ${patch_out_of_mem_info_str}\n"
else
echo -e "\n\033[1;33m=>\033[0m ${try_fix_out_of_mem_str}\n"
fi
}
ssh_enlong_patch(){
if [[ $run_on_vm -eq 1 ]];then
sudo sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 30/g' /etc/ssh/sshd_config
sudo sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 86400/g' /etc/ssh/sshd_config
# try: fix git early eof
git config --global http.postBuffer 1048576000
git config --global core.compression -1
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
sudo systemctl restart sshd
fi
}
git_fix_openssl(){
# now ubuntu
sudo apt-get update
sudo apt-get install build-essential fakeroot dpkg-dev libcurl4-openssl-dev
sudo apt-get build-dep git
mkdir ~/git-openssl
cd ~/git-openssl
apt-get source git
cd git-*
sed -i 's/libcurl4-gnutls-dev/libcurl4-openssl-dev/g' debian/control
sed -i '/TEST =test/ d' debian/rules
sudo dpkg-buildpackage -rfakeroot -b
}
ccache_fix(){
# Only ccache fix when build failed
# Custom Ccache
custom_ccache_dir=
if [[ ! $(grep 'Generated ccache config' $HOME/.bashrc) ]];then
default_ccache_dir=/home/$USER/.aosp_ccache
if [[ $custom_ccache_dir == "" ]];then
custom_ccache_dir=$default_ccache_dir
fi
mkdir -p /home/$USER/.ccache
mkdir -p $custom_ccache_dir
sudo mount --bind /home/$USER/.ccache $custom_ccache_dir
sudo chmod -R 777 $custom_ccache_dir
sed -i '$a \
# Generated ccache config \
export USE_CCACHE=1 \
export CCACHE_EXEC=\/usr\/bin\/ccache \
export CCACHE_DIR='"$custom_ccache_dir"' \
ccache -M 50G -F 0' $HOME/.bashrc
fi
}
lineage_sdk_patch(){
cd $aosp_source_dir_working
rom_spec_str=$rom_spec_str
git clone https://github.com/LineageOS/android_packages_resources_devicesettings.git -b lineage-20.0 packages/resources/devicesettings
git clone https://github.com/LineageOS/android_hardware_lineage_interfaces -b lineage-20.0 hardware/lineage/interfaces
git clone https://github.com/LineageOS/android_hardware_lineage_livedisplay.git -b lineage-20.0 hardware/lineage/livedisplay
# add trust usb & trust usb defaults
rom_build_soong_bp=vendor/${rom_spec_str}/build/soong/Android.bp
if [[ ! $(grep 'name: "trust_usb_control_defaults"' $rom_build_soong_bp) ]];then
sed -i '1a \
trust_usb_control { \
name: "trust_usb_control_defaults", \
soong_config_variables: { \
target_trust_usb_control_path: { \
cppflags: ["-DUSB_CONTROL_PATH=\\"%s\\""], \
}, \
target_trust_usb_control_enable: { \
cppflags: ["-DUSB_CONTROL_ENABLE=\\"%s\\""], \
}, \
target_trust_usb_control_disable: { \
cppflags: ["-DUSB_CONTROL_DISABLE=\\"%s\\""], \
}, \
}, \
}' $rom_build_soong_bp
fi
if [[ ! $(grep 'name: "trust_usb_control"' $rom_build_soong_bp) ]];then
sed -i '1a \
\/\/ aosp-setup: lineage sdk patch \
soong_config_module_type { \
name: "trust_usb_control", \
module_type: "cc_defaults", \
config_namespace: "lineageGlobalVars", \
value_variables: [ \
"target_trust_usb_control_path", \
"target_trust_usb_control_enable", \
"target_trust_usb_control_disable", \
], \
properties: ["cppflags"], \
}' $rom_build_soong_bp
fi
cd $AOSP_SETUP_ROOT
}
dt_str_patch(){
# patch device tree string
# 1 - device tree directory
if [[ ! $1 =~ '/' ]];then echo -e "\033[1;33m=>\033[0m ${dt_bringup_name_error_str}";return;fi
cd $aosp_source_dir_working
rom_spec_str=$rom_spec_str
dt_dir=device/$(dirname ${1})/$(basename ${1})
cd $dt_dir
dt_device_name="$(grep 'PRODUCT_DEVICE' *.mk --max-count=1 | sed 's/[[:space:]]//g' | sed 's/.*:=//g')"
dt_main_mk=$(grep 'PRODUCT_DEVICE :=' *.mk --max-count=1 | sed 's/[[:space:]]//g' | sed 's/:PRODUCT_DEVICE.*//g' | head -1)
dt_old_str=$(echo $dt_main_mk | sed 's/_.*//g')
dt_new_main_mk="${rom_spec_str}_${dt_device_name}.mk"
if [[ ! $(grep 'revision="android-15' ../../../.repo/manifests/default.xml) ]] && [[ $(pwd) != "psyche" ]] ;then
sed -i 's/'"${dt_old_str}"'/'"${rom_spec_str}"'/g' AndroidProducts.mk
sed -i 's/'"${dt_old_str}"'/'"${rom_spec_str}"'/g' $dt_main_mk
sed -i 's/vendor\/'"${dt_old_str}"'/vendor\/'"${rom_spec_str}"'/g' BoardConfig*.mk
if [[ -f $dt_main_mk ]] && [[ ! -f $dt_new_main_mk ]];then
mv $dt_main_mk $dt_new_main_mk
fi
if [[ -f ${dt_old_str}.dependencies ]] && [[ ! -f ${rom_spec_str}.dependencies ]];then
mv ${dt_old_str}.dependencies ${rom_spec_str}.dependencies
fi
# handle parts. if there are multiple name for device settings, user need to check mannually
if [[ -f ../../../packages/resources/devicesettings/Android.bp ]] && [[ -f parts/Android.bp ]];then
if [[ $(grep settings.resource ../../../packages/resources/devicesettings/Android.bp | grep -c 'name:') -eq 1 ]];then
old_parts_settings_str="$(grep settings.resources parts/Android.bp | sed 's/[[:space:]]//g')"
new_parts_settings_str="$(grep name: ../../../packages/resources/devicesettings/Android.bp | sed 's/[[:space:]]//g' | sed 's/name://g')"
sed -i 's/'"${old_parts_settings_str}"'/'"${new_parts_settings_str}"'/g' parts/Android.bp
fi
fi
fi
cd $AOSP_SETUP_ROOT
}
other_fix(){
# fix Disallowed PATH Tool error
disallowed_tg_file=${aosp_source_dir}/build/sonng/ui/path/config.go
# build continue after build error
m api-stubs-docs-non-updatable-update-current-api && m framework-bluetooth.stubs.source-update-current-api && m system-api-stubs-docs-non-updatable-update-current-api && m test-api-stubs-docs-non-updatable-update-current-api
}
so_deps(){
so_deps_list=($(readelf -a $1 | grep NEEDED | sed 's/.*Shared\ library://g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/[[:space:]]//g' | sort))
echo -e "\033[1;32m=>\033[0m Dep \033[4m$(basename $1)\033[0m\n"
for so_dep in "${so_deps_list[@]}"
do
if [[ $(find "$(dirname $1)/.." -iname $so_dep) ]];then
echo -e "\033[1;32m$so_dep\033[0m"
else
echo -e "$so_dep"
fi
done
echo
}
setup_patches(){
# check repo
repo_check
# low RAM patch less than 25Gb
patch_when_low_ram
}
########################## ERROR HANDLING UNIT ############################
# All error fix function under AOSP Source Dir (aosp_source_dir_working)
lineage_sdk_dump_error(){
# fix error for aleady defined Android.bp
#sh -c "$(cat out/error.log | grep 'already defined' | sed 's/Android.bp.*/Android.bp/g' | sed 's/.*hardware/hardware/g' | sed 's/^/rm &/g')"
echo
}
sysprop_dump_error_handle(){
# It's maybe not perfect
sysprop_dump_in_log=($(grep = out/error.log | grep -v 'Command:' | grep '\.'))
sysprop_real_dump_list=($(grep = out/error.log | grep -v 'Command:' | grep '\.' | sed 's/=.*//g' | uniq))
}
stubs_api_error_handle(){
source build/envsetup.sh
m api-stubs-docs-non-updatable-update-current-api && m framework-bluetooth.stubs.source-update-current-api && m system-api-stubs-docs-non-updatable-update-current-api && m test-api-stubs-docs-non-updatable-update-current-api
}
allow_list_error_handle(){
# file: build/soong/scripts/check_boot_jars/package_allowed_list.txt
# base hals
if [[ ! $(grep 'aosp-setup adds' build/soong/scripts/check_boot_jars/package_allowed_list.txt) ]];then
sh -c "$(echo '''
# aosp-setup adds
com\.oplus\.os
com\.oplus\.os\..*
oplus\.content\.res
oplus\.content\.res\..*
vendor\.lineage\.livedisplay
vendor\.lineage\.livedisplay\..*
vendor\.lineage\.touch
vendor\.lineage\.touch\..*
ink\.kaleidoscope
ink\.kaleidoscope\..*
''' >> build/soong/scripts/check_boot_jars/package_allowed_list.txt)"
fi
# some individual hal
allow_hal="$(cat out/error.log | sed 's/build\/soong\/scripts\/check_boot_jars\/package_allowed_list.txt.*//g' | sed 's/.*whose\ package\ name//g' | sed 's/is\ empty.*//g' | sed 's/"//g' | sed 's/[[:space:]]//g')"
allow_hal_1="$(echo $allow_hal | sed 's/\./\\./g')"
allow_hal_2="$(echo ${allow_hal_1}\\..*)"
echo $allow_hal_1 >> build/soong/scripts/check_boot_jars/package_allowed_list.txt
echo $allow_hal_2 >> build/soong/scripts/check_boot_jars/package_allowed_list.txt
}
handle_build_error(){
#error: found duplicate sysprop assignments:
#persist.sys.sf.native_mode=258
#persist.sys.sf.native_mode=2
# out/soong/.intermediates/frameworks/base/framework-minus-apex/android_common/aligned/framework-minus-apex.jar contains class file ink.kaleidoscope.ParallelSpaceManager$$ExternalSyntheticLambda0, whose package name "ink.kaleidoscope" is empty or not in the allow list build/soong/scripts/check_boot_jars/package_allowed_list.txt of packages allowed on the bootclasspath
if [[ -f aosh.sh ]] && [[ -f lang.sh ]];then
cd ${aosp_source_dir_working}
fi
local default_error_log=out/error.log
local failed_cmd=$(grep Command out/error.log | sed 's/Command://g')
if [[ $(grep 'Read-only file system' $default_error_log) ]] && [[ $(grep 'ccache:' $default_error_log) ]];then
local error_type="ccache_readonly_error"
elif [[ $(grep 'duplicate sysprop' $default_error_log) ]];then
local error_type="sysprop_dump_error"
elif [[ $(grep 'update-current-api' $default_error_log) ]];then
local error_type="stubs_update_api_error"
elif [[ $(grep 'package_allowed_list.txt' $default_error_log) ]];then
local error_type="allow_list_error"
else
local error_type="unhandled"
fi
case $error_type in
"ccache_readonly_error")
# It seems user still need to run command mannually
ccache_fix
sudo mount --bind /home/$USER/.ccache $custom_ccache_dir
;;
# typeattribute/ expandtypeattribute
"sysprop_dump_error")
sysprop_dump_error_handle
;;
"stubs_update_api_error")
stubs_api_error_handle
;;
"allow_list_error")
allow_list_error_handle
;;
"unhandled")
exit 1
esac
}
######################### MIRROR UNIT (OK) #########################
select_mirror(){
if [[ $(which git) == "" ]];then echo -e '\nPlease install git';exit 1;fi
sel_github_list=(
'https://mirror.ghproxy.com/https://github.com'
'https://githubfast.com'
'https://github.moeyy.xyz/https://github.com'
'https://ghproxy.net/https://github.com'
'https://kkgithub.com'
'https://gitdl.cn/https://github.com'
'https://ghp.ci/https://github.com'
'https://github.store'
'https://slink.ltd/https://github.com'
'https://github.site'
'https://gitclone.com'
)
sel_aosp_list=('tuna tsinghua' 'ustc' 'beijing bfsu' 'nanfang sci (not)' 'google')
git_aosp_repo_mirror_reset "github" "aosp"
while (( "$#" ))
do
case "$1" in
"github")
## handle github.com
echo -e "\n${choose_git_mirror_str}"
select gm in "${sel_github_list[@]}"
do
if [[ $gm != "" ]];then
echo -e "\033[1;32m=>\033[0m ${sel_is_str} $gm"
git config --global url."${gm}".insteadof https://github.com
case $gm in
'https://kkgithub.com')
git config --global url.https://raw.kkgithub.com.insteadof https://raw.githubusercontent.com
;;
'https://gitdl.cn/https://github.com')
git config --global url.https://gitdl.cn/https://raw.githubusercontent.com.insteadof https://raw.githubusercontent.com
;;
'https://ghproxy.net/https://github.com')
git config --global url.https://ghproxy.net/https://raw.githubusercontent.com.insteadof https://raw.githubusercontent.com
;;
'https://github.store')
git config --global url.https://raw.github.store.insteadof https://raw.githubusercontent.com
;;
'https://github.site')
git config --global url.https://raw.github.site.insteadof https://raw.githubusercontent.com
;;
*)
# create random raw github mirror list
ramdom_raw_github_mirror_list=(
'https://mirror.ghproxy.com/https://raw.githubusercontent.com'
'https://fastly.jsdelivr.net/gh'
'https://github.moeyy.xyz/https://raw.githubusercontent.com'
'https://ghproxy.net/https://raw.githubusercontent.com'
'https://raw.github.store'
'https://github.site'
)
declare -i raw_github_list_num=${#ramdom_raw_github_mirror_list[@]}
raw_github_gen_i_raw=$(expr $RANDOM % $raw_github_list_num)
# sometimes RANDOM could be null, auto select 0 if this happends
if [[ $raw_github_gen_i_raw == "" ]];then
declare -i raw_github_gen_i=0
else
declare -i raw_github_gen_i=$raw_github_gen_i_raw
fi
ramdom_raw_github_mirror=${ramdom_raw_github_mirror_list[${raw_github_gen_i}]}
git config --global url."${ramdom_raw_github_mirror}".insteadof https://raw.githubusercontent.com
;;
esac
else
echo -e "\033[1;32m=>\033[0m don't use github mirror"
fi
break
done
;;
"aosp")
## handle AOSP
echo -e "\n${choose_aosp_mirror_str}\n"
select aos in "${sel_aosp_list[@]}"
do
case $aos in
'tuna tsinghua')
aom='https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/'
;;
'ustc')
select ustc_pro in "git" "https" "http"
do
aom="${ustc_pro}://mirrors.ustc.edu.cn/aosp/"
break
done
;;
'beijing bfsu')
aom='https://mirrors.bfsu.edu.cn/git/AOSP/'
;;
'nanfang sci (not)')
aom='https://mirrors.sustech.edu.cn/AOSP/'
;;
*)
aom='https://android.googlesource.com'
;;
esac
echo -e "\033[1;32m=>\033[0m ${sel_is_str} $aom"
git config --global url."${aom}".insteadof https://android.googlesource.com
break
done
;;
esac
shift
done
}
git_aosp_repo_mirror_reset(){
while (( "$#" )); do
case $1 in
"github")
local insteadof_git_list=($(git config --global --list | grep insteadof | grep github | sed 's/insteadof=.*/insteadof/g' | sort))
for insteadof_git in "${insteadof_git_list[@]}"
do
git config --global --unset ${insteadof_git}
done
;;
"aosp")
local insteadof_aosp_list=($(git config --global --list | grep insteadof | grep android | sed 's/insteadof=.*/insteadof/g' | sort))
for insteadof_aosp in "${insteadof_aosp_list[@]}"
do
git config --global --unset ${insteadof_aosp}
done
;;
esac
shift
done
# REPO URL
export REPO_URL='https://gerrit.googlesource.com/git-repo'
}
mirror_unit_main(){
# for aosp | git mirrors
if [[ $keep_mirror_arg -eq 0 ]];then
echo -e "${use_mirror_str}"
select use_mirror_sel in "Yes" "No"
do
case $use_mirror_sel in
"Yes")
sel_mirror_list=$(echo $sel_mirror_list_str | sort)
eval "select_mirror ${sel_mirror_list[@]}"
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
;;
"No" | *)
git_aosp_repo_mirror_reset "github" "aosp"
echo -e "\033[1;36m=>\033[0m ${skip_mirror_str}"
;;
esac
break
done
else
echo -e "\033[1;32m=>\033[0m ${keep_mirror_str}"
fi
}
######################### DEPS UNIT #########################
ubuntu_deps(){
sudo apt update -y
sudo apt install software-properties-common -y
lsb_release="$(lsb_release -d | cut -d ':' -f 2 | sed -e 's/^[[:space:]]*//')"
other_pkgs="lsb-core ncurses-dev android-platform-tools-base pngcrush python-all-dev schedtool "
case $lsb_release in
"Mint 18"* | "Ubuntu 16"*)
other_pkgs+="libesd0-dev"
;;
"Ubuntu 2"* | "Pop!_OS 2"*)
other_pkgs+="libncurses5 curl python-is-python3"
;;
"Debian GNU/Linux 10"* | "Debian GNU/Linux 11"*)
other_pkgs+="libncurses5"
;;
"Deepin 2"*)
# Deepin absent for common: ncurses-dev android-platform-tools-base pngcrush python-all-dev schedtool
# Deepin could find: libncurses-dev android-sdk-platform-tools-common
other_pkgs="libncurses-dev android-sdk-platform-tools-common"
;;
*)
other_pkgs+="libncurses5"
;;
esac
LATEST_MAKE_VERSION="4.3"
sudo apt install -y adb autoconf automake axel bc bison build-essential \
ccache clang cmake curl expat fastboot flex g++ \
g++-multilib gawk gcc gcc-multilib git git-lfs gnupg gperf \
htop imagemagick lib32ncurses5-dev lib32z1-dev libtinfo5 libc6-dev libcap-dev \
libexpat1-dev libgmp-dev '^liblz4-.*' '^liblzma.*' libmpc-dev libmpfr-dev libncurses5-dev \
libsdl1.2-dev libssl-dev libtool libxml2 libxml2-utils '^lzma.*' lzop \
maven ncftp patch patchelf pkg-config \
pngquant python2.7 python3 python3-pyelftools python-all-dev re2c squashfs-tools subversion \
texinfo unzip w3m xsltproc zip zlib1g-dev lzip p7zip p7zip-full \
libxml-simple-perl libswitch-perl apt-utils rsync zip ${other_pkgs}
sudo systemctl restart udev
}
arch_deps(){
sudo sed -i "/\[multilib\]/,/Include/"'s/^#//' /etc/pacman.conf
if [[ ! $(grep '# aosp-setup add ustc archlinuxcn' /etc/pacman.conf) ]];then
sudo sed -i '$a \
\
# aosp-setup add ustc archlinuxcn \
[archlinuxcn] \
SigLevel = Optional TrustAll \
Server = https://mirrors.ustc.edu.cn/archlinuxcn/$arch \
' /etc/pacman.conf
fi
if [[ -f /usr/bin/repo ]];then sudo rm -f /usr/bin/repo;fi
if [[ $env_run_time -le 2 ]];then
sudo pacman-key --lsign-key "[email protected]"
sudo pacman -Sy --noconfirm archlinuxcn-keyring
fi
sudo pacman -Syy --noconfirm --needed git git-lfs multilib-devel fontconfig ttf-droid yay ccache make yay patch pkg-config maven gradle python-pyelftools
packages=(ncurses5-compat-libs lib32-ncurses5-compat-libs aosp-devel xml2 lineageos-devel python-pip python-setuptools p7zip)
yay -Sy --noconfirm --norebuild --noredownload ${packages[@]}
sudo pacman -S --noconfirm --needed android-tools # android-udev
}
fedora_deps(){
sudo dnf install -y \
android-tools autoconf213 bison bzip2 ccache clang curl flex gawk gcc-c++ git git-lfs p7zip glibc-devel glibc-static libstdc++-static libX11-devel make mesa-libGL-devel ncurses-devel openssl patch zlib-devel ncurses-devel.i686 readline-devel.i686 zlib-devel.i686 libX11-devel.i686 mesa-libGL-devel.i686 glibc-devel.i686 libstdc++.i686 libXrandr.i686 zip perl-Digest-SHA python2 python3-pyelftools wget lzop openssl openssl-devel java-1.8.0-openjdk-devel ImageMagick schedtool lzip vboot-utils vim ccache libxcrypt-compat libfreetype.so.6 freetype-devel
# The package libncurses5 is not available, so we need to hack our way by symlinking the required library.
sudo ln -s /usr/lib/libncurses.so.6 /usr/lib/libncurses.so.5
sudo ln -s /usr/lib/libncurses.so.6 /usr/lib/libtinfo.so.5
sudo ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
sudo ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libtinfo.so.5
sudo udevadm control --reload-rules
}
opensuse_deps(){
sudo zypper in mirrorsorcerer
sudo systemctl enable --now mirrorsorcerer
sudo zypper install android-tools autoconf213 bc bison bzip2 ccache clang curl flex gawk gpg2 gperf gcc-c++ git git-lfs glibc-devel ImageMagick java-11-openjdk java-1_8_0-openjdk java-1_8_0-openjdk-devel liblz4-1 libncurses5 libncurses6 libpopt0 libressl-devel libstdc++6 libX11-6 libxml2-tools libxslt1 libX11-devel libXrandr2 lzip lzop kernel-devel maven make megatools Mesa-libGL1 Mesa-libGL-devel mokutil nano neofetch ncurses5-devel ncurses-devel openssl opi patch perl-Digest-SHA1 python python-rpm-generators python3-pyelftools readline-devel schedtool screenfetch sha3sum squashfs vim wget wireguard-tools xf86-video-intel zip zlib-devel
# Devel Basis (https://darryldias.me/2020/devel-basis-on-opensuse-sle/)
sudo zypper install -t pattern devel_basis
# The package libncurses5 is not available, so we need to hack our way by symlinking the required library.
sudo ln -s /usr/lib/libncurses.so.6 /usr/lib/libncurses.so.5
sudo ln -s /usr/lib/libncurses.so.6 /usr/lib/libtinfo.so.5
sudo ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
sudo ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libtinfo.so.5
# libxcrypt-compat
sudo ln -s /usr/lib64/libcrypt.so.2 /usr/lib64/libcrypt.so.1
}
solus_deps(){
sudo eopkg it -c system.devel
sudo eopkg it openjdk-8-devel curl-devel git gnupg gperf libgcc-32bit libxslt-devel lzop ncurses-32bit-devel ncurses-devel readline-32bit-devel rsync schedtool sdl1-devel squashfs-tools unzip wxwidgets-devel zip zlib-32bit-devel lzip ccache
sudo usysconf run -f
}
######################### BUILD DEVICE(POST ENV) ENV UNIT #####################
aosp_setup_check(){
# This function must run before sync to ensure non-English directory not exist for android source
if [[ $1 -eq 1 ]];then clear;return;fi
# check directory do not have non-English words
aosp_setup_dir_str="$(echo $AOSP_SETUP_ROOT | sed 's/[a-zA-Z]//g' | sed 's/[:doirt:]//g')"
spec_symbol_list=('~' '`' '!' '@' '#' '$' '%' '^' '&' '-' '+' '=' '[' ']' '\' '|' '/' ':' '<' '>' '?')
for spec_symbol in "${spec_symbol_list[@]}"
do
aosp_setup_dir_str="$(echo ${aosp_setup_dir_str} | sed 's|\'"$spec_symbol"'||g')"
done
if [[ "${aosp_setup_dir_str}" != "" ]];then
sed -i '16s/aosp_setup_dir_check_ok=.*/aosp_setup_dir_check_ok=0/g' $(dirname $0)/${BASH_SOURCE}
echo -e "\033[1;33m=>\033[0m Found non-English direcotory string [\033[36m${aosp_setup_dir_str}\033[0m]\n\033[1;33m=>\033[0m Suggestions: rename it in Eng."
exit
else
sed -i '16s/aosp_setup_dir_check_ok=.*/aosp_setup_dir_check_ok=1/g' $(dirname $0)/${BASH_SOURCE}
fi
# Install deps for setup configuration
if [[ "$(command -v curl)" == "" ]] || [[ "$(command -v git)" == "" ]];then
case $pkg_cmd in
"apt")
sudo apt-get update -yq && sudo apt-get install curl git -yq
;;
"pacman")
sudo pacman -Syy && sudo pacman -Sy curl git
;;
"dnf")
sudo dnf update -y && sudo dnf install -y curl git
;;
"eopkg")
sudo eopkg update -y && sudo eopkg install -y curl git
;;
"zypper")
sudo zypper --non-interactive update && sudo zypper install --non-interactive curl git
;;
esac
fi
clear
}
repo_check(){
### handle git-repo
## Decline handle git-repo because scripts do it
if [[ "$(command -v repo)" == "" ]];then
repo_tg_path=/usr/bin/repo
sudo curl https://storage.googleapis.com/git-repo-downloads/repo -o $repo_tg_path --silent
sudo chown ${USER}:${USER} $repo_tg_path || chown ${USER}:${USER} $repo_tg_path
sudo chmod a+x $repo_tg_path || chmod a+x $repo_tg_path
sudo chmod 0755 $repo_tg_path || chmod 0755 $repo_tg_path
echo -e "\033[1;32m=>\033[0m ${repo_added_str}"
fi
touch $HOME/.bashrc
if [[ ! $(grep '# android sync-helper' $HOME/.bashrc) ]];then
cat <<BASHEOF >> $HOME/.bashrc
# android sync-helper
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
readonly REPO_URL
BASHEOF
fi
### handle repo bin path
touch $HOME/.profile
if [[ ! $(grep '# android sync-helper' $HOME/.profile) ]];then
cat <<PROFILEEOF >> $HOME/.profile
# android sync-helper
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
PROFILEEOF
fi
### handle disconnect ssh issue
# sudo sed -i 's/^export TMOUT=.*/export TMOUT=0/' /etc/profile && sudo sed -i "/#ClientAliveInterval/a\ClientAliveInterval 60" /etc/ssh/sshd_config && sudo sed -i "/#ClientAliveInterval/d" /etc/ssh/sshd_config && sudo sed -i '/ClientAliveCountMax/ s/^#//' /etc/ssh/sshd_config &&sudo /bin/systemctl restart sshd.service
}
git_config_user_info(){
# git config
# 1 - user.name
# 2 - user.email
if [[ $(git config user.name) != "" ]] && [[ $(git config user.email) != "" ]];then
return
else
echo -e "\n==> Config git "
fi
# with arg
if [[ $1 != "" ]];then
git config --global user.name "$1"
else
read -p 'Your name: ' git_name
git config --global user.name "${git_name}"
fi
if [[ $2 != "" ]] && [[ $2 =~ "@" ]];then
git config --global user.email "$2"
else
read -p 'Your email: ' git_email
git config --global user.email "${git_email}"
fi
}
setup_build_deps(){
# lineageos: bc bison build-essential ccache curl flex g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev libelf-dev liblz4-tool libncurses5 libncurses5-dev libsdl1.2-dev libssl-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev
# Ubuntu versions older than 20.04 (focal), libwxgtk3.0-dev
# Ubuntu versions older than 16.04 (xenial), libwxgtk2.8-dev
case $pkg_cmd in
"apt")
ubuntu_deps
;;
"pacman")
arch_deps
;;
"dnf")
fedora_deps
;;
"eopkg")
solus_deps
;;
"zypper")
opensuse_deps
;;
esac
check_machine_type
cd $AOSP_SETUP_ROOT
}
check_machine_type(){
if [[ $run_on_vm -eq 0 ]];then
on_physical_machine
else
on_cloud_vm_machine
fi
}
on_physical_machine(){
# This function is mainly for setup adb tools* on physical machine
# skip setup adb tools if run on vm
if [[ $run_on_vm -eq 1 ]];then return;fi
# adb path
if [[ $(grep 'add Android SDK platform' -ns $HOME/.bashrc) == "" ]];then
sed -i '$a \
# add Android SDK platform tools to path \
if [ -d "$HOME/platform-tools" ] ; then \
PATH="$HOME/platform-tools:$PATH" \
fi' $HOME/.bashrc
fi
# adb udev rules
if [[ ! -f /etc/udev/rules.d/51-android.rules ]];then
sudo curl --create-dirs -L -o /etc/udev/rules.d/51-android.rules -O -L https://raw.githubusercontent.com/M0Rf30/android-udev-rules/main/51-android.rules
fi
sudo chmod 644 /etc/udev/rules.d/51-android.rules
sudo chown root /etc/udev/rules.d/51-android.rules
}
on_cloud_vm_machine(){
# skip setup adb tools if run on vm
if [[ $run_on_vm -eq 0 ]];then return;fi
# ssh
ssh_enlong_patch
# check ssh-key
if [[ ! -f /home/${USER}/.ssh/id_ed25519.pub ]];then
echo 'n' | ssh-keygen -t ed25519 -f /home/${USER}/.ssh/id_ed25519 -N '' -q -C "${USER}@VM-${USER}"
fi
sudo chown -R ${USER}:${USER} /home/${USER}/.ssh
sudo chmod -R 700 /home/${USER}/.ssh
# add login info for profile
touch /home/${USER}/.profile
if [[ ! $(grep '>>> Your-key' /home/${USER}/.profile) ]];then
cat>>/home/${USER}/.profile<<BASHINFO
echo ">>> Your-key"
cat /home/${USER}/.ssh/id_ed25519.pub
BASHINFO
fi
}
env_install_mode(){
# config install android build dependencies
if [[ $only_env_mode -eq 1 ]];then
echo "OnlyEnv"
elif [[ $env_run_time -lt 3 ]] || [[ $env_run_last_return -gt 0 ]];then
env_run_time+=1
sed -i '14s/env_run_time=./env_run_time='"${env_run_time}"'/g' $(dirname $0)/${BASH_SOURCE}
echo "Need"
else
echo "NoNeed"
fi
}
android_env_setup(){
# setup(install) build deps
mirror_unit_main
git_config_user_info "${custom_git_username}" "${custom_git_email}"
case $1 in
"Need" | "OnlyEnv")
setup_build_deps && env_run_return=$? && sed -i '13s/env_run_last_return=./env_run_last_return='"${env_run_return}"'/g' $(dirname $0)/${BASH_SOURCE}
esac
setup_patches
if [[ $1 == "OnlyEnv" ]];then exit 0;fi
}
######################### PRE SYNC & SYNC UNIT #########################
rom_manifest_config(){
if [[ $1 != "" ]];then
if [[ $1 =~ "manifest" ]] || [[ $1 =~ "android" ]] && [[ ! $1 =~ "device" ]] && [[ ! $1 =~ "vendor" ]] && [[ ! $1 =~ "kernel" ]];then
ROM_MANIFEST=${aosp_manifest_url}
fi
else
#echo -e "\n${sel_rom_source_str}"
rom_sources=("LineageOS" "ArrowOS" "Pixel Experience" "RisingOS" "Crdroid" "AlphaDroid" "Evolution-X" "Project-Elixir" "Paranoid Android (AOSPA)" "PixysOS" "SuperiorOS" "PixelPlusUI")
select aosp_source in "${rom_sources[@]}"
do
case $aosp_source in
"LineageOS")
ROM_MANIFEST='https://github.com/LineageOS/android.git'
;;
"ArrowOS")
ROM_MANIFEST='https://github.com/ArrowOS/android_manifest.git'
;;
"Pixel Experience")
ROM_MANIFEST='https://github.com/PixelExperience/manifest.git'
;;
"RisingOS")
ROM_MANIFEST='https://github.com/RisingTechOSS/android.git'
;;
"Crdroid")
ROM_MANIFEST='https://github.com/crdroidandroid/android.git'
;;
"AlphaDroid")
ROM_MANIFEST='https://github.com/AlphaDroid-Project/manifest.git'
;;
"Evolution-X")
ROM_MANIFEST='https://github.com/Evolution-X/manifest.git'
;;
"Project-Elixir")
ROM_MANIFEST='https://github.com/Project-Elixir/manifest.git'
;;
"Paranoid Android (AOSPA)")
ROM_MANIFEST='https://github.com/AOSPA/manifest.git'
;;
"PixysOS")
ROM_MANIFEST='https://github.com/PixysOS/manifest.git'
;;
"SuperiorOS")
ROM_MANIFEST='https://github.com/SuperiorOS/manifest.git'
;;
"PixelPlusUI")
ROM_MANIFEST='https://github.com/PixelPlusUI/manifest.git'
;;
esac
break
done
fi
echo ${ROM_MANIFEST}
}
repo_sync_fail_handle(){
# 1 - failed repo directory list
if [[ ! -f build/envsetup.sh ]];then
if [[ $aosp_source_dir != "" ]];then
aosp_source_dir_working=$aosp_source_dir
fi
if [[ ${aosp_source_dir_working} != "" ]];then
if [[ ! -d .repo ]] && [[ -d $aosp_source_dir_working ]];then
cd $aosp_source_dir_working
fi
else
return 1
fi
fi
if [[ ${1} == "" ]];then
echo -e "\033[1;32m=>\033[0m ${repo_failed_usr_str}"
echo " ${repo_failed_usr_eg_str}"
read -p '=>' repo_fail_str
else
repo_fail_str=""
while [[ ${1} =~ '/' ]]
do
# do not pass a dangerous direcotory
if [[ ${1:0:1} == '/' ]];then continue;fi
repo_fail_str="${repo_fail_str} ${1}"
shift
done
fi
declare -i repo_fail_num=0
while [[ repo_fail_num -lt 4 ]]
do
let repo_fail_num++
echo -e "\033[1;32m=>\033[0m ${repo_failed_usr_str}"
echo " ${repo_failed_usr_eg_str}"
read -p '=>' repo_fail_str
repo_fail_list=($(echo ${repo_fail_str} | sort))
# handle by log
#declare -i repo_line_a=$(grep -n repos: t.log | awk -F ':' '{print $1}') && let repo_line_a++
#declare -i repo_line_b=$(grep -n 'Try re-running' t.log | awk -F ':' '{print $1}') && let repo_line_b--
#repo_fail_list=($(sed -n ''"${repo_line_a}"','"${repo_line_b}"'p' t.log | sort))
# recheck in aosp source directory - because remove is a dangerous command
if [[ ! -d build ]] || [[ ! -d bootable ]];then return 0;fi
for repo_fail in "${repo_fail_list[@]}"
do
eval "$(grep "${repo_fail}" .repo/manifest* -r | sed 's/ /\n/g' | grep name | grep -v '/')"
if [[ -d .repo/project-objects/${name}.git ]];then
rm -rf .repo/project-objects/${name}.git
else
repo_fail_in_po_list=($(find .repo/project-objects/ -maxdepth 2 -iname "*$(echo ${repo_fail} | sed 's/\//_/g')*"))
if [[ ! ${#repo_fail_in_po_list[@]} -eq 0 ]];then
echo "rm -rf ${repo_fail_in_po_list[@]}"
fi
fi
rm -rf .repo/projects/${repo_fail}.git
rm -rf ${repo_fail}
done
return
# synchronize again
# don't use --force-remove-dirty --prune
repo sync -c --no-repo-verify --no-clone-bundle --optimized-fetch --prune -j$(nproc --all) && break
repo_fail_str=""
done
}
handle_sync(){
# aosp source
str_to_arr $1 '/'
declare -i url_all_num
url_all_num=${#str_to_arr_result[@]}
os_str_num=url_all_num-2
manifest_str_num=url_all_num-1
rom_str=${str_to_arr_result[${os_str_num}]}
manifest_str="$(echo ${str_to_arr_result[${manifest_str_num}]} | sed 's/.git$//g')"
echo -e "\n\033[1;4;32m---------- ${rom_info_str} ------------\033[0m"
echo -e "\033[1;33mROM\033[0m: $rom_str"
echo -e "\033[1;33mmanifest\033[0m: $manifest_str"
echo -e "\033[1;4;32m-----------------------------\033[0m"
aosp_source_dir=android/${rom_str}