-
Notifications
You must be signed in to change notification settings - Fork 7
/
extract_utils.sh
2578 lines (2292 loc) · 83.1 KB
/
extract_utils.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
#
# SPDX-FileCopyrightText: 2016 The CyanogenMod Project
# SPDX-FileCopyrightText: 2017-2024 The LineageOS Project
# SPDX-FileCopyrightText: 2017-2020 Paranoid Android
# SPDX-License-Identifier: Apache-2.0
#
PRODUCT_COPY_FILES_HASHES=()
PRODUCT_COPY_FILES_FIXUP_HASHES=()
PRODUCT_COPY_FILES_SRC=()
PRODUCT_COPY_FILES_DEST=()
PRODUCT_COPY_FILES_ARGS=()
PRODUCT_PACKAGES_HASHES=()
PRODUCT_PACKAGES_FIXUP_HASHES=()
PRODUCT_PACKAGES_SRC=()
PRODUCT_PACKAGES_DEST=()
PRODUCT_PACKAGES_ARGS=()
PRODUCT_SYMLINKS_LIST=()
PACKAGE_LIST=()
REQUIRED_PACKAGES_LIST=
EXTRACT_SRC=
EXTRACT_STATE=-1
EXTRACT_RADIO_STATE=-1
VENDOR_STATE=-1
VENDOR_RADIO_STATE=-1
COMMON=-1
ARCHES=
FULLY_DEODEXED=-1
KEEP_DUMP=${KEEP_DUMP:-0}
SKIP_CLEANUP=${SKIP_CLEANUP:-0}
EXTRACT_TMP_DIR=$(mktemp -d)
HOST=$(uname | tr '[:upper:]' '[:lower:]')
#
# cleanup
#
# kill our tmpfiles with fire on exit
#
function cleanup() {
if [ "$SKIP_CLEANUP" == "true" ] || [ "$SKIP_CLEANUP" == "1" ]; then
echo "Skipping cleanup of $EXTRACT_TMP_DIR"
else
rm -rf "${EXTRACT_TMP_DIR:?}"
fi
}
trap cleanup 0
#
# setup_vendor_deps
#
# $1: Android root directory
# Sets up common dependencies for extraction
#
function setup_vendor_deps() {
export ANDROID_ROOT="$1"
if [ ! -d "$ANDROID_ROOT" ]; then
echo "\$ANDROID_ROOT must be set and valid before including this script!"
exit 1
fi
export BINARIES_LOCATION="$ANDROID_ROOT"/prebuilts/extract-tools/${HOST}-x86/bin
export CLANG_BINUTILS="$ANDROID_ROOT"/prebuilts/clang/host/${HOST}-x86/llvm-binutils-stable
export JDK_BINARIES_LOCATION="$ANDROID_ROOT"/prebuilts/jdk/jdk21/${HOST}-x86/bin
export COMMON_BINARIES_LOCATION="$ANDROID_ROOT"/prebuilts/extract-tools/common
export SIMG2IMG="$BINARIES_LOCATION"/simg2img
export LPUNPACK="$BINARIES_LOCATION"/lpunpack
export OTA_EXTRACTOR="$BINARIES_LOCATION"/ota_extractor
export SIGSCAN="$BINARIES_LOCATION"/SigScan
export STRIPZIP="$BINARIES_LOCATION"/stripzip
export OBJDUMP="$CLANG_BINUTILS"/llvm-objdump
export JAVA="$JDK_BINARIES_LOCATION"/java
export APKTOOL="$COMMON_BINARIES_LOCATION"/apktool/apktool.jar
for VERSION in 0_8 0_9 0_17_2; do
export PATCHELF_${VERSION}="$BINARIES_LOCATION"/patchelf-"${VERSION}"
done
if [ -z "$PATCHELF_VERSION" ]; then
export PATCHELF_VERSION=0_9
fi
if [ -z "$PATCHELF" ]; then
local PATCHELF_VARIABLE="PATCHELF_${PATCHELF_VERSION}"
export PATCHELF=${!PATCHELF_VARIABLE}
fi
}
#
# setup_vendor
#
# $1: device/component name
# $2: vendor name
# $3: Android root directory
# $4: is common device - optional, default to false
# $5: cleanup - optional, default to true
# $6: custom vendor makefile name - optional, default to false
# $7: is qti common component - optional, default to false
#
# Must be called before any other functions can be used. This
# sets up the internal state for a new vendor configuration.
#
function setup_vendor() {
export IS_QTI_COMPONENT="$7"
if [ -z "$IS_QTI_COMPONENT" ]; then
local DEVICE="$1"
if [ -z "$DEVICE" ]; then
echo "\$DEVICE must be set before including this script!"
exit 1
fi
else
local COMPONENT="$1"
if [ -z "$COMPONENT" ]; then
echo "\$COMPONENT must be set before including this script!"
exit 1
fi
fi
local VENDOR="$2"
if [ -z "$VENDOR" ]; then
echo "\$VENDOR must be set before including this script!"
exit 1
fi
export ANDROID_ROOT="$3"
if [ ! -d "$ANDROID_ROOT" ]; then
echo "\$ANDROID_ROOT must be set and valid before including this script!"
exit 1
fi
if [ -z "$IS_QTI_COMPONENT" ]; then
export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
else
export OUTDIR=vendor/"$VENDOR"/"$COMPONENT"
fi
if [ ! -d "$ANDROID_ROOT/$OUTDIR" ]; then
mkdir -p "$ANDROID_ROOT/$OUTDIR"
fi
VNDNAME="$6"
if [ -z "$VNDNAME" ]; then
if [ -z "$IS_QTI_COMPONENT" ]; then
VNDNAME="$DEVICE"
else
VNDNAME="$COMPONENT"
fi
fi
export PRODUCTMK="$ANDROID_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
export ANDROIDBP="$ANDROID_ROOT"/"$OUTDIR"/Android.bp
export ANDROIDMK="$ANDROID_ROOT"/"$OUTDIR"/Android.mk
export BOARDMK="$ANDROID_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
if [ "$4" == "true" ] || [ "$4" == "1" ]; then
COMMON=1
else
COMMON=0
fi
if [ "$5" == "false" ] || [ "$5" == "0" ]; then
VENDOR_STATE=1
VENDOR_RADIO_STATE=1
else
VENDOR_STATE=0
VENDOR_RADIO_STATE=0
fi
setup_vendor_deps "$ANDROID_ROOT"
}
# Helper functions for parsing a spec.
# notes: an optional "|SHA1" that may appear in the format is stripped
# early from the spec in the parse_file_list function, and
# should not be present inside the input parameter passed
# to these functions.
#
# input: spec in the form of "src[:dst][;args]"
# output: "src[:dst]"
#
function spec() {
# Remove the args by removing the longest trailing substring starting with ;
echo "${1%%;*}"
}
#
# input: spec in the form of "src[:dst]"
# output: "src"
#
function spec_src_file() {
# Remove the shortest trailing substring starting with :
# If there's no : to match against, src will be kept,
# otherwise, :dst will be removed
echo "${1%%:*}"
}
function spec_target_file() {
# Remove the shortest beginning substring ending in :
# If there's no : to match against, src will be kept,
# otherwise, src: will be removed
echo "${1##*:}"
}
#
# input: spec in the form of "src[:dst][;args]"
# output: "dst" if present, "src" otherwise.
#
function target_file() {
local SPEC=$(spec "$1")
spec_target_file "$SPEC"
}
function spec_target_args() {
# Remove the shortest beginning substring ending in ;
# If there isn't one, the entire string will be kept, so check
# against that
local ARGS="${2#*;}"
if [ "$1" = "$ARGS" ]; then
echo ""
else
echo "$ARGS"
fi
}
#
# input: spec in the form of "src[:dst][;args]"
# output: "args" if present, "" otherwise.
#
function target_args() {
local SPEC=$(spec "$1")
spec_target_args "$SPEC" "$1"
}
#
# prefix_match:
#
# input:
# - $1: prefix
# - (global variable) PRODUCT_PACKAGES_DEST: array of dst
# - (global variable) PRODUCT_PACKAGES_ARGS: array of args
# output:
# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
#
function prefix_match() {
local PREFIX="$1"
local NEW_ARRAY=()
local DEST_LIST=("${PRODUCT_PACKAGES_DEST[@]}")
local ARGS_LIST=("${PRODUCT_PACKAGES_ARGS[@]}")
local COUNT=${#DEST_LIST[@]}
for ((i = 1; i < COUNT + 1; i++)); do
local FILE="${DEST_LIST[$i - 1]}"
if [[ "$FILE" =~ ^"$PREFIX" ]]; then
local SPEC_ARGS="${ARGS_LIST[$i - 1]}"
local ARGS=(${SPEC_ARGS//;/ })
local FILTERED_ARGS=()
for ARG in "${ARGS[@]}"; do
if [[ "$ARG" =~ ^SYMLINK= ]]; then
continue
fi
FILTERED_ARGS+=("$ARG")
done
FILTERED_ARGS=$(IFS=";" echo "${FILTERED_ARGS[@]}")
if [ -z "$FILTERED_ARGS" ]; then
NEW_ARRAY+=("${FILE#"$PREFIX"}")
else
NEW_ARRAY+=("${FILE#"$PREFIX"};${FILTERED_ARGS}")
fi
fi
done
printf '%s\n' "${NEW_ARRAY[@]}" | LC_ALL=C sort
}
#
# prefix_match_file:
#
# $1: the prefix to match on
# $2: the file to match the prefix for
#
# Internal function which returns true if a filename contains the
# specified prefix.
#
function prefix_match_file() {
local PREFIX="$1"
local FILE="$2"
if [[ "$FILE" =~ ^"$PREFIX" ]]; then
return 0
else
return 1
fi
}
#
# suffix_match_file:
#
# $1: the suffix to match on
# $2: the file to match the suffix for
#
# Internal function which returns true if a filename contains the
# specified suffix.
#
function suffix_match_file() {
local SUFFIX="$1"
local FILE="$2"
if [[ "$FILE" = *"$SUFFIX" ]]; then
return 0
else
return 1
fi
}
#
# truncate_file
#
# $1: the filename to truncate
#
# Internal function which truncates a filename by removing the first dir
# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
#
function truncate_file() {
local FILE="$1"
local FIND="${FILE%%/*}"
local LOCATION="${#FIND}+1"
echo "${FILE:$LOCATION}"
}
#
# write_product_copy_files:
#
# Creates the PRODUCT_COPY_FILES section in the product makefile for all
# items in the list which do not start with a dash (-).
#
function write_product_copy_files() {
local COUNT=${#PRODUCT_COPY_FILES_DEST[@]}
local TARGET=
local FILE=
local LINEEND=
if [ "$COUNT" -eq "0" ]; then
return 0
fi
printf '%s\n' "PRODUCT_COPY_FILES += \\" >>"$PRODUCTMK"
for ((i = 1; i < COUNT + 1; i++)); do
TARGET="${PRODUCT_COPY_FILES_DEST[$i - 1]}"
LINEEND=" \\"
if [ "$i" -eq "$COUNT" ]; then
LINEEND=""
fi
if prefix_match_file "product/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "system/product/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "system_ext/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "system/system_ext/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "odm/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "vendor/odm/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "system/vendor/odm/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "vendor/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "vendor_dlkm/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_DLKM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "system/vendor/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "system/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "recovery/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_RECOVERY)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
elif prefix_match_file "vendor_ramdisk/" "$TARGET"; then
local OUTTARGET=$(truncate_file "$TARGET")
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_RAMDISK)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >>"$PRODUCTMK"
else
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >>"$PRODUCTMK"
fi
done
return 0
}
function lib_to_package_fixup_clang_rt_ubsan_standalone() {
case "$1" in
libclang_rt.ubsan_standalone-arm-android | \
libclang_rt.ubsan_standalone-aarch64-android)
echo "libclang_rt.ubsan_standalone"
;;
*)
return 1
;;
esac
}
function lib_to_package_fixup_proto_3_9_1() {
case "$1" in
libprotobuf-cpp-lite-3.9.1 | \
libprotobuf-cpp-full-3.9.1)
echo "$1-vendorcompat"
;;
*)
return 1
;;
esac
}
#
# lib_to_package_fixup
#
# $1: library name without the .so suffix
# $2: partition of the file for which we are generating shared libs
# $3: name of the file for which we are generating shared libs
#
#
# Can be overridden by device-level extract-files.sh
#
function lib_to_package_fixup() {
lib_to_package_fixup_clang_rt_ubsan_standalone "$1" ||
lib_to_package_fixup_proto_3_9_1 "$1"
}
#
# write_package_shared_libs:
#
# $1: File name inside target list
#
function write_package_shared_libs() {
local SRC="$1"
local LOCATION="$2"
local FILE="$3"
local PARTITION="$4"
local FILE_PATH="$ANDROID_ROOT/$OUTDIR/$SRC/$LOCATION/$FILE"
local LIBS=$("$OBJDUMP" -p "$FILE_PATH" 2>/dev/null | sed -n 's/^\s*NEEDED\s*\(.*\).so$/\1/p')
local PACKAGES=$(
while IFS= read -r LIB; do
lib_to_package_fixup "$LIB" "$PARTITION" "$FILE" || echo "$LIB"
done <<<"$LIBS"
)
local PACKAGES_LIST=$(echo "$PACKAGES" | sed 's/\(.\+\)/"\1",/g' | tr '\n' ' ')
printf '\t\t\tshared_libs: [%s],\n' "$PACKAGES_LIST"
}
#
# write_blueprint_packages:
#
# $1: The LOCAL_MODULE_CLASS for the given module list
# $2: /system, /odm, /product, /system_ext, or /vendor partition
# $3: type-specific extra flags
# $4: Target list separated by newlines
#
# Internal function which writes out the BUILD_PREBUILT stanzas
# for all modules in the list. This is called by write_product_packages
# after the modules are categorized.
#
function write_blueprint_packages() {
local CLASS="$1"
local PARTITION="$2"
local EXTRA="$3"
local FILELIST="$4"
local BASENAME=
local EXTENSION=
local PKGNAME=
local SRC=
local STEM=
local OVERRIDEPKG=
local REQUIREDPKG=
local DISABLE_CHECKELF=
local GENERATE_DEPS=
if [ -z "$EXTRA" ]; then
if [ "$CLASS" = "RFSA" ]; then
EXTRA="lib/rfsa"
elif [ "$CLASS" = "APEX" ]; then
EXTRA="apex"
elif [ "$CLASS" = "APPS" ]; then
EXTRA="app"
elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
EXTRA="framework"
elif [ "$CLASS" = "ETC" ]; then
EXTRA="etc"
elif [ "$CLASS" = "EXECUTABLES" ]; then
EXTRA="bin"
fi
fi
local SRC_REL=
if [ -n "$PARTITION" ]; then
SRC_REL+="$PARTITION"
fi
# SHARED_LIBRARIES's EXTRA is not a path
if [ -n "$EXTRA" ] &&
[ "$CLASS" != "SHARED_LIBRARIES" ]; then
if [ -n "$SRC_REL" ]; then
SRC_REL+="/"
fi
SRC_REL+="$EXTRA"
fi
# Automatically match everything except SHARED_LIBRARIES and RFSA
if [ -z "$FILELIST" ] &&
[ "$CLASS" != "SHARED_LIBRARIES" ] &&
[ "$CLASS" != "RFSA" ]; then
FILELIST=$(prefix_match "$SRC_REL/")
fi
local SRC="proprietary/$SRC_REL"
[ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
while IFS= read -r P; do
if [ "$P" = "" ]; then
continue
fi
# prefix_match results already only contain the dest part
local FILE=$(spec "$P")
local SPEC_ARGS=$(spec_target_args "$FILE" "$P")
local ARGS=(${SPEC_ARGS//;/ })
local DIRNAME="${FILE%/*}"
if [ "$DIRNAME" = "$FILE" ]; then
DIRNAME="."
fi
local BASENAME="${FILE##*/}"
EXTENSION=${BASENAME##*.}
PKGNAME=${BASENAME%.*}
if ([ "$CLASS" = "EXECUTABLES" ] && [ "$EXTENSION" != "sh" ]) || [ "$PKGNAME" = "" ]; then
PKGNAME="$BASENAME"
EXTENSION=""
fi
if [ "$CLASS" = "ETC" ] && [ "$EXTENSION" = "xml" ]; then
PKGNAME="$BASENAME"
fi
# Allow overriding module name
STEM=
OVERRIDEPKG=
if [ "$TARGET_ENABLE_CHECKELF" != "false" ]; then
DISABLE_CHECKELF=
GENERATE_DEPS="true"
else
DISABLE_CHECKELF="true"
fi
for ARG in "${ARGS[@]}"; do
if [[ "$ARG" =~ "MODULE_SUFFIX" ]]; then
STEM="$PKGNAME"
PKGNAME+=${ARG#*=}
elif [[ "$ARG" =~ "MODULE" ]]; then
STEM="$PKGNAME"
PKGNAME=${ARG#*=}
elif [[ "$ARG" =~ "OVERRIDES" ]]; then
OVERRIDEPKG=${ARG#*=}
OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
elif [[ "$ARG" == "DISABLE_CHECKELF" ]]; then
DISABLE_CHECKELF="true"
elif [[ "$ARG" == "DISABLE_DEPS" ]]; then
DISABLE_CHECKELF="true"
GENERATE_DEPS=
fi
done
# Add to final package list
PACKAGE_LIST+=("$PKGNAME")
if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
printf 'cc_prebuilt_library_shared {\n'
printf '\tname: "%s",\n' "$PKGNAME"
if [ -n "$STEM" ]; then
printf '\tstem: "%s",\n' "$STEM"
fi
printf '\towner: "%s",\n' "${VENDOR%\/*}"
printf '\tstrip: {\n'
printf '\t\tnone: true,\n'
printf '\t},\n'
printf '\ttarget: {\n'
if [ "$EXTRA" = "both" ] || [ "$EXTRA" = "32" ]; then
printf '\t\t%s: {\n' $(elf_format_android "$ANDROID_ROOT/$OUTDIR/$SRC/lib/$FILE")
printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
if [ -n "$GENERATE_DEPS" ]; then
write_package_shared_libs "$SRC" "lib" "$FILE" "$PARTITION"
fi
printf '\t\t},\n'
fi
if [ "$EXTRA" = "both" ] || [ "$EXTRA" = "64" ]; then
printf '\t\t%s: {\n' $(elf_format_android "$ANDROID_ROOT/$OUTDIR/$SRC/lib64/$FILE")
printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
if [ -n "$GENERATE_DEPS" ]; then
write_package_shared_libs "$SRC" "lib64" "$FILE" "$PARTITION"
fi
printf '\t\t},\n'
fi
printf '\t},\n'
printf '\tcompile_multilib: "%s",\n' "$EXTRA"
if [ -n "$DISABLE_CHECKELF" ]; then
printf '\tcheck_elf_files: false,\n'
fi
elif [ "$CLASS" = "RFSA" ]; then
printf 'prebuilt_rfsa {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\tfilename: "%s",\n' "$BASENAME"
printf '\towner: "%s",\n' "${VENDOR%\/*}"
printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
elif [ "$CLASS" = "APEX" ]; then
printf 'prebuilt_apex {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "${VENDOR%\/*}"
printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
printf '\tfilename: "%s",\n' "$FILE"
elif [ "$CLASS" = "APPS" ]; then
printf 'android_app_import {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "${VENDOR%\/*}"
printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
USE_PLATFORM_CERTIFICATE="true"
for ARG in "${ARGS[@]}"; do
if [ "$ARG" = "PRESIGNED" ]; then
USE_PLATFORM_CERTIFICATE="false"
printf '\tpreprocessed: true,\n'
printf '\tpresigned: true,\n'
elif [ "$ARG" = "SKIPAPKCHECKS" ]; then
printf '\tskip_preprocessed_apk_checks: true,\n'
elif [[ "$ARG" =~ "OVERRIDES" ]]; then
OVERRIDEPKG=${ARG#*=}
OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
elif [[ "$ARG" =~ "REQUIRED" ]]; then
REQUIREDPKG=${ARG#*=}
REQUIRED_PACKAGES_LIST+="$REQUIREDPKG,"
printf '\trequired: ["%s"],\n' "${REQUIREDPKG//,/\", \"}"
elif [[ "$ARG" =~ "SYMLINK" ]]; then
continue
elif [ -n "$ARG" ]; then
USE_PLATFORM_CERTIFICATE="false"
printf '\tcertificate: "%s",\n' "$ARG"
fi
done
if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
printf '\tcertificate: "platform",\n'
fi
elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
printf 'dex_import {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "${VENDOR%\/*}"
printf '\tjars: ["%s/%s"],\n' "$SRC" "$FILE"
elif [ "$CLASS" = "ETC" ]; then
if [ "$EXTENSION" = "xml" ]; then
printf 'prebuilt_etc_xml {\n'
else
printf 'prebuilt_etc {\n'
fi
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "${VENDOR%\/*}"
printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
printf '\tfilename_from_src: true,\n'
elif [ "$CLASS" = "EXECUTABLES" ]; then
local FILE_PATH="$ANDROID_ROOT/$OUTDIR/$SRC/$FILE"
local ELF_FORMAT=$(elf_format_android "$FILE_PATH")
if [ "$ELF_FORMAT" = "" ]; then
# This is not an elf file, assume it's a shell script that doesn't have an extension
# Setting extension here does not change the target extension, only the module type
EXTENSION="sh"
fi
if [ "$EXTENSION" = "sh" ]; then
printf 'sh_binary {\n'
else
printf 'cc_prebuilt_binary {\n'
fi
printf '\tname: "%s",\n' "$PKGNAME"
if [ -n "$STEM" ]; then
printf '\tstem: "%s",\n' "$STEM"
fi
if [ -n "$OVERRIDEPKG" ]; then
printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
fi
printf '\towner: "%s",\n' "${VENDOR%\/*}"
if [ "$EXTENSION" != "sh" ]; then
printf '\ttarget: {\n'
printf '\t\t%s: {\n' "$ELF_FORMAT"
printf '\t\t\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
if [ -n "$GENERATE_DEPS" ]; then
write_package_shared_libs "$SRC" "" "$FILE" "$PARTITION"
fi
printf '\t\t},\n'
printf '\t},\n'
if [[ "$ELF_FORMAT" =~ "64" ]]; then
printf '\tcompile_multilib: "%s",\n' "64"
else
printf '\tcompile_multilib: "%s",\n' "32"
fi
if [ -n "$DISABLE_CHECKELF" ]; then
printf '\tcheck_elf_files: false,\n'
fi
printf '\tstrip: {\n'
printf '\t\tnone: true,\n'
printf '\t},\n'
printf '\tprefer: true,\n'
else
printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
printf '\tfilename: "%s",\n' "$BASENAME"
fi
fi
if [ "$CLASS" = "APPS" ]; then
printf '\tdex_preopt: {\n'
printf '\t\tenabled: false,\n'
printf '\t},\n'
if [ "$DIRNAME" != "." ] && [[ "$DIRNAME" == */* ]]; then
printf '\trelative_install_path: "%s",\n' "$DIRNAME"
fi
fi
if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] || [ "$CLASS" = "RFSA" ]; then
if [ "$DIRNAME" != "." ]; then
if [ "$EXTENSION" = "sh" ]; then
printf '\tsub_dir: "%s",\n' "$DIRNAME"
else
printf '\trelative_install_path: "%s",\n' "$DIRNAME"
fi
fi
fi
if [ "$CLASS" = "ETC" ]; then
if [ "$DIRNAME" != "." ]; then
printf '\tsub_dir: "%s",\n' "$DIRNAME"
fi
fi
if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
printf '\tprefer: true,\n'
fi
if [ "$EXTRA" = "priv-app" ]; then
printf '\tprivileged: true,\n'
fi
if [ "$PARTITION" = "vendor" ]; then
printf '\tsoc_specific: true,\n'
elif [ "$PARTITION" = "product" ]; then
printf '\tproduct_specific: true,\n'
elif [ "$PARTITION" = "system_ext" ]; then
printf '\tsystem_ext_specific: true,\n'
elif [ "$PARTITION" = "odm" ]; then
printf '\tdevice_specific: true,\n'
fi
printf '}\n\n'
done <<<"$FILELIST"
}
function do_comm() {
LC_ALL=C comm "$1" <(echo "$2") <(echo "$3")
}
function elf_format_android() {
local ELF_FORMAT=$("$OBJDUMP" -a "$1" 2>/dev/null | sed -nE "s|^.+file format (.*)$|\1|p")
if [ "$ELF_FORMAT" = "elf64-littleaarch64" ]; then
echo "android_arm64"
elif [ "$ELF_FORMAT" = "elf32-littlearm" ] || [ "$ELF_FORMAT" = "elf32-hexagon" ]; then
echo "android_arm"
elif [ "$ELF_FORMAT" = "elf64-x86-64" ]; then
echo "android_x86_64"
elif [ "$ELF_FORMAT" = "elf32-i386" ]; then
echo "android_x86"
fi
}
#
# write_product_packages:
#
# This function will create prebuilt entries in the
# Android.bp and associated PRODUCT_PACKAGES list in the
# product makefile for all files in the blob list which
# start with a single dash (-) character.
#
function write_product_packages() {
PACKAGE_LIST=()
local COUNT=${#PRODUCT_PACKAGES_DEST[@]}
if [ "$COUNT" = "0" ]; then
return 0
fi
if [ "$TARGET_ENABLE_CHECKELF" == "false" ]; then
colored_echo yellow "WARNING: TARGET_ENABLE_CHECKELF = false is deprecated and will be removed in Android 16."
fi
# Figure out what's 32-bit, what's 64-bit, and what's multilib
local T_LIB32=$(prefix_match "lib/")
local T_LIB64=$(prefix_match "lib64/")
local MULTILIBS=$(do_comm -12 "$T_LIB32" "$T_LIB64")
local LIB32=$(do_comm -23 "$T_LIB32" "$MULTILIBS")
local LIB64=$(do_comm -23 "$T_LIB64" "$MULTILIBS")
{
write_blueprint_packages "SHARED_LIBRARIES" "" "both" "$MULTILIBS"
write_blueprint_packages "SHARED_LIBRARIES" "" "32" "$LIB32"
write_blueprint_packages "SHARED_LIBRARIES" "" "64" "$LIB64"
} >>"$ANDROIDBP"
local T_S_LIB32=$(prefix_match "system/lib/")
local T_S_LIB64=$(prefix_match "system/lib64/")
local S_MULTILIBS=$(do_comm -12 "$T_S_LIB32" "$T_S_LIB64")
local S_LIB32=$(do_comm -23 "$T_S_LIB32" "$S_MULTILIBS")
local S_LIB64=$(do_comm -23 "$T_S_LIB64" "$S_MULTILIBS")
{
write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "$S_MULTILIBS"
write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "$S_LIB32"
write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "$S_LIB64"
} >>"$ANDROIDBP"
local T_V_LIB32=$(prefix_match "vendor/lib/")
local T_V_LIB64=$(prefix_match "vendor/lib64/")
local V_RFSA=$(prefix_match "vendor/lib/rfsa/")
local V_MULTILIBS=$(do_comm -12 "$T_V_LIB32" "$T_V_LIB64")
local V_LIB32=$(do_comm -23 "$T_V_LIB32" "$V_MULTILIBS")
local V_LIB32=$(grep -v 'rfsa/' <(echo "$V_LIB32"))
local V_LIB64=$(do_comm -23 "$T_V_LIB64" "$V_MULTILIBS")
{
write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "$V_MULTILIBS"
write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "$V_LIB32"
write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "$V_LIB64"
write_blueprint_packages "RFSA" "vendor" "" "$V_RFSA"
} >>"$ANDROIDBP"
local T_P_LIB32=$(prefix_match "product/lib/")
local T_P_LIB64=$(prefix_match "product/lib64/")
local P_MULTILIBS=$(do_comm -12 "$T_P_LIB32" "$T_P_LIB64")
local P_LIB32=$(do_comm -23 "$T_P_LIB32" "$P_MULTILIBS")
local P_LIB64=$(do_comm -23 "$T_P_LIB64" "$P_MULTILIBS")
{
write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "$P_MULTILIBS"
write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "$P_LIB32"
write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "$P_LIB64"
} >>"$ANDROIDBP"
local T_SE_LIB32=$(prefix_match "system_ext/lib/")
local T_SE_LIB64=$(prefix_match "system_ext/lib64/")
local SE_MULTILIBS=$(do_comm -12 "$T_SE_LIB32" "$T_SE_LIB64")
local SE_LIB32=$(do_comm -23 "$T_SE_LIB32" "$SE_MULTILIBS")
local SE_LIB64=$(do_comm -23 "$T_SE_LIB64" "$SE_MULTILIBS")
{
write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "$SE_MULTILIBS"
write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "$SE_LIB32"
write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "$SE_LIB64"
} >>"$ANDROIDBP"
local T_O_LIB32=$(prefix_match "odm/lib/")
local T_O_LIB64=$(prefix_match "odm/lib64/")
local O_RFSA=$(prefix_match "odm/lib/rfsa/")
local O_MULTILIBS=$(do_comm -12 "$T_O_LIB32" "$T_O_LIB64")
local O_LIB32=$(do_comm -23 "$T_O_LIB32" "$O_MULTILIBS")
local O_LIB32=$(grep -v 'rfsa/' <(echo "$O_LIB32"))
local O_LIB64=$(do_comm -23 "$T_O_LIB64" "$O_MULTILIBS")
{
write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "$O_MULTILIBS"
write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "$O_LIB32"
write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "$O_LIB64"
write_blueprint_packages "RFSA" "odm" "" "$O_RFSA"
} >>"$ANDROIDBP"
# APEX
{
write_blueprint_packages "APEX" ""
write_blueprint_packages "APEX" "system"
write_blueprint_packages "APEX" "vendor"
write_blueprint_packages "APEX" "system_ext"
} >>"$ANDROIDBP"
# Apps
{
write_blueprint_packages "APPS" "" ""
write_blueprint_packages "APPS" "" "priv-app"
write_blueprint_packages "APPS" "system" ""
write_blueprint_packages "APPS" "system" "priv-app"
write_blueprint_packages "APPS" "vendor" ""
write_blueprint_packages "APPS" "vendor" "priv-app"
write_blueprint_packages "APPS" "product" ""
write_blueprint_packages "APPS" "product" "priv-app"
write_blueprint_packages "APPS" "system_ext" ""
write_blueprint_packages "APPS" "system_ext" "priv-app"
write_blueprint_packages "APPS" "odm" ""
write_blueprint_packages "APPS" "odm" "priv-app"
} >>"$ANDROIDBP"
# Framework
{
write_blueprint_packages "JAVA_LIBRARIES" ""
write_blueprint_packages "JAVA_LIBRARIES" "system"
write_blueprint_packages "JAVA_LIBRARIES" "vendor"
write_blueprint_packages "JAVA_LIBRARIES" "product"
write_blueprint_packages "JAVA_LIBRARIES" "system_ext"
write_blueprint_packages "JAVA_LIBRARIES" "odm"
} >>"$ANDROIDBP"
# Etc
{
write_blueprint_packages "ETC" ""
write_blueprint_packages "ETC" "system"
write_blueprint_packages "ETC" "vendor"
write_blueprint_packages "ETC" "product"
write_blueprint_packages "ETC" "system_ext"
write_blueprint_packages "ETC" "odm"
} >>"$ANDROIDBP"
# Executables
{
write_blueprint_packages "EXECUTABLES" ""
write_blueprint_packages "EXECUTABLES" "system"
write_blueprint_packages "EXECUTABLES" "vendor"
write_blueprint_packages "EXECUTABLES" "product"
write_blueprint_packages "EXECUTABLES" "system_ext"
write_blueprint_packages "EXECUTABLES" "odm"
} >>"$ANDROIDBP"
write_package_definition "${PACKAGE_LIST[@]}" >>"$PRODUCTMK"
}
#
# write_symlink_packages:
#
# Creates symlink entries in the Android.bp and related PRODUCT_PACKAGES
# list in the product makefile for all files in the blob list which has
# SYMLINK argument.
#
function write_symlink_packages() {
local FILE=
local ARCH=
local BASENAME=
local PKGNAME=
local PREFIX=
local SYMLINK_BASENAME=
local SYMLINK_PACKAGES=()
# Sort the symlinks list for comm
PRODUCT_SYMLINKS_LIST=($(printf '%s\n' "${PRODUCT_SYMLINKS_LIST[@]}" | LC_ALL=C sort))
local COUNT=${#PRODUCT_SYMLINKS_LIST[@]}
if [ "$COUNT" = "0" ]; then
return 0
fi
for LINE in "${PRODUCT_SYMLINKS_LIST[@]}"; do
FILE=$(target_file "$LINE")
if [[ "$LINE" =~ '/lib64/' || "$LINE" =~ '/lib/arm64/' ]]; then
ARCH="64"
elif [[ "$LINE" =~ '/lib/' ]]; then
ARCH="32"
fi
BASENAME=$(basename "$FILE")
local SPEC_ARGS=$(target_args "$LINE")
local ARGS=(${SPEC_ARGS//;/ })
for ARG in "${ARGS[@]}"; do
if [[ "$ARG" =~ "SYMLINK" ]]; then
SYMLINKS=${ARG#*=}
SYMLINKS=(${SYMLINKS//,/ })
for SYMLINK in "${SYMLINKS[@]}"; do
SYMLINK_BASENAME=$(basename "$SYMLINK")
PKGNAME="${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}"
if [[ "${SYMLINK_PACKAGES[@]}" =~ "$PKGNAME" ]]; then
PKGNAME+="_$(grep -o "$PKGNAME" <<<${SYMLINK_PACKAGES[*]} | wc -l)"
fi
{
printf 'install_symlink {\n'