blob: 002e238219985241767cec4c5aa7e5ca8919affe [file] [log] [blame]
Steve Kondik5bd66602016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020020PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070021PRODUCT_PACKAGES_LIST=()
22PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020023PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070024PACKAGE_LIST=()
25VENDOR_STATE=-1
26VENDOR_RADIO_STATE=-1
27COMMON=-1
28ARCHES=
29FULLY_DEODEXED=-1
30
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040031TMPDIR=$(mktemp -d)
Steve Kondik5bd66602016-07-15 10:39:58 -070032
33#
34# cleanup
35#
36# kill our tmpfiles with fire on exit
37#
38function cleanup() {
39 rm -rf "${TMPDIR:?}"
40}
41
Gabriele Mb8e54572017-10-11 12:55:51 +020042trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070043
44#
45# setup_vendor
46#
47# $1: device name
48# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000049# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070050# $4: is common device - optional, default to false
51# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050052# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070053#
54# Must be called before any other functions can be used. This
55# sets up the internal state for a new vendor configuration.
56#
57function setup_vendor() {
58 local DEVICE="$1"
59 if [ -z "$DEVICE" ]; then
60 echo "\$DEVICE must be set before including this script!"
61 exit 1
62 fi
63
64 export VENDOR="$2"
65 if [ -z "$VENDOR" ]; then
66 echo "\$VENDOR must be set before including this script!"
67 exit 1
68 fi
69
theimpulson9a911af2019-08-14 03:25:12 +000070 export OMNI_ROOT="$3"
71 if [ ! -d "$OMNI_ROOT" ]; then
72 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070073 exit 1
74 fi
75
76 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000077 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
78 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070079 fi
80
Jake Whatley9843b322017-01-25 21:49:16 -050081 VNDNAME="$6"
82 if [ -z "$VNDNAME" ]; then
83 VNDNAME="$DEVICE"
84 fi
85
theimpulsonbb72ab82019-08-14 06:03:32 +000086 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -070087 export ANDROIDBP="$OMNI_ROOT"/"$OUTDIR"/Android.bp
theimpulson9a911af2019-08-14 03:25:12 +000088 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
89 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070090
91 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
92 COMMON=1
93 else
94 COMMON=0
95 fi
96
Gabriele Mc44696d2017-05-01 18:22:04 +020097 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070098 VENDOR_STATE=1
99 VENDOR_RADIO_STATE=1
100 else
101 VENDOR_STATE=0
102 VENDOR_RADIO_STATE=0
103 fi
104}
105
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300106# Helper functions for parsing a spec.
107# notes: an optional "|SHA1" that may appear in the format is stripped
108# early from the spec in the parse_file_list function, and
109# should not be present inside the input parameter passed
110# to these functions.
111
112#
113# input: spec in the form of "src[:dst][;args]"
114# output: "src"
115#
116function src_file() {
117 local SPEC="$1"
118 local SPLIT=(${SPEC//:/ })
119 local ARGS="$(target_args ${SPEC})"
120 # Regardless of there being a ":" delimiter or not in the spec,
121 # the source file is always either the first, or the only entry.
122 local SRC="${SPLIT[0]}"
123 # Remove target_args suffix, if present
124 echo "${SRC%;${ARGS}}"
125}
126
Steve Kondik5bd66602016-07-15 10:39:58 -0700127#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300128# input: spec in the form of "src[:dst][;args]"
129# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700130#
131function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800132 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300133 local SPLIT=(${SPEC//:/ })
134 local ARGS="$(target_args ${SPEC})"
135 local DST=
136 case ${#SPLIT[@]} in
137 1)
138 # The spec doesn't have a : delimiter
139 DST="${SPLIT[0]}"
140 ;;
141 *)
142 # The spec actually has a src:dst format
143 DST="${SPLIT[1]}"
144 ;;
145 esac
146 # Remove target_args suffix, if present
147 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700148}
149
150#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300151# input: spec in the form of "src[:dst][;args]"
152# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700153#
154function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300155 local SPEC="$1"
156 local SPLIT=(${SPEC//;/ })
157 local ARGS=
158 case ${#SPLIT[@]} in
159 1)
160 # No ";" delimiter in the spec.
161 ;;
162 *)
163 # The "args" are whatever comes after the ";" character.
164 # Basically the spec stripped of whatever is to the left of ";".
165 ARGS="${SPEC#${SPLIT[0]};}"
166 ;;
167 esac
168 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700169}
170
171#
172# prefix_match:
173#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300174# input:
175# - $1: prefix
176# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
177# output:
178# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700179#
180function prefix_match() {
181 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300182 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
183 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700184 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300185 local ARGS=$(target_args "$LINE")
186 if [ -z "${ARGS}" ]; then
187 echo "${FILE#$PREFIX}"
188 else
189 echo "${FILE#$PREFIX};${ARGS}"
190 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700191 fi
192 done
193}
194
195#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400196# prefix_match_file:
197#
198# $1: the prefix to match on
199# $2: the file to match the prefix for
200#
201# Internal function which returns true if a filename contains the
202# specified prefix.
203#
204function prefix_match_file() {
205 local PREFIX="$1"
206 local FILE="$2"
207 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
208 return 0
209 else
210 return 1
211 fi
212}
213
214#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700215# suffix_match_file:
216#
217# $1: the suffix to match on
218# $2: the file to match the suffix for
219#
220# Internal function which returns true if a filename contains the
221# specified suffix.
222#
223function suffix_match_file() {
224 local SUFFIX="$1"
225 local FILE="$2"
226 if [[ "$FILE" = *"$SUFFIX" ]]; then
227 return 0
228 else
229 return 1
230 fi
231}
232
233#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400234# truncate_file
235#
236# $1: the filename to truncate
237# $2: the argument to output the truncated filename to
238#
239# Internal function which truncates a filename by removing the first dir
240# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
241#
242function truncate_file() {
243 local FILE="$1"
244 RETURN_FILE="$2"
245 local FIND="${FILE%%/*}"
246 local LOCATION="${#FIND}+1"
247 echo ${FILE:$LOCATION}
248}
249
250#
Steve Kondik5bd66602016-07-15 10:39:58 -0700251# write_product_copy_files:
252#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400253# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400254#
Steve Kondik5bd66602016-07-15 10:39:58 -0700255# Creates the PRODUCT_COPY_FILES section in the product makefile for all
256# items in the list which do not start with a dash (-).
257#
258function write_product_copy_files() {
259 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
260 local TARGET=
261 local FILE=
262 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400263 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700264
265 if [ "$COUNT" -eq "0" ]; then
266 return 0
267 fi
268
269 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
270 for (( i=1; i<COUNT+1; i++ )); do
271 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
272 LINEEND=" \\"
273 if [ "$i" -eq "$COUNT" ]; then
274 LINEEND=""
275 fi
276
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300277 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400278 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400279 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400280 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400281 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400282 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400283 local OUTTARGET=$(truncate_file $TARGET)
284 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
285 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200286 elif prefix_match_file "system_ext/" $TARGET ; then
287 local OUTTARGET=$(truncate_file $TARGET)
288 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
289 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
290 elif prefix_match_file "system/system_ext/" $TARGET ; then
291 local OUTTARGET=$(truncate_file $TARGET)
292 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
293 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400294 elif prefix_match_file "odm/" $TARGET ; then
295 local OUTTARGET=$(truncate_file $TARGET)
296 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
297 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400298 elif prefix_match_file "vendor/odm/" $TARGET ; then
299 local OUTTARGET=$(truncate_file $TARGET)
300 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
301 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
302 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
303 local OUTTARGET=$(truncate_file $TARGET)
304 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
305 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
306 elif prefix_match_file "vendor/" $TARGET ; then
307 local OUTTARGET=$(truncate_file $TARGET)
308 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
309 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
310 elif prefix_match_file "system/vendor/" $TARGET ; then
311 local OUTTARGET=$(truncate_file $TARGET)
312 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
313 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400314 elif prefix_match_file "system/" $TARGET ; then
315 local OUTTARGET=$(truncate_file $TARGET)
316 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
317 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400318 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400319 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400320 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
321 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700322 done
323 return 0
324}
325
326#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700327# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700328#
329# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200330# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700331# $3: type-specific extra flags
332# $4: Name of the array holding the target list
333#
334# Internal function which writes out the BUILD_PREBUILT stanzas
335# for all modules in the list. This is called by write_product_packages
336# after the modules are categorized.
337#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700338function write_blueprint_packages() {
339
340 local CLASS="$1"
341 local PARTITION="$2"
342 local EXTRA="$3"
343
344 # Yes, this is a horrible hack - we create a new array using indirection
345 local ARR_NAME="$4[@]"
346 local FILELIST=("${!ARR_NAME}")
347
348 local FILE=
349 local ARGS=
350 local BASENAME=
351 local EXTENSION=
352 local PKGNAME=
353 local SRC=
354
355 for P in "${FILELIST[@]}"; do
356 FILE=$(target_file "$P")
357 ARGS=$(target_args "$P")
358
359 BASENAME=$(basename "$FILE")
360 DIRNAME=$(dirname "$FILE")
361 EXTENSION=${BASENAME##*.}
362 PKGNAME=${BASENAME%.*}
363
364 # Add to final package list
365 PACKAGE_LIST+=("$PKGNAME")
366
367 SRC="proprietary"
368 if [ "$PARTITION" = "system" ]; then
369 SRC+="/system"
370 elif [ "$PARTITION" = "vendor" ]; then
371 SRC+="/vendor"
372 elif [ "$PARTITION" = "product" ]; then
373 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200374 elif [ "$PARTITION" = "system_ext" ]; then
375 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700376 elif [ "$PARTITION" = "odm" ]; then
377 SRC+="/odm"
378 fi
379
380 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
381 printf 'cc_prebuilt_library_shared {\n'
382 printf '\tname: "%s",\n' "$PKGNAME"
383 printf '\towner: "%s",\n' "$VENDOR"
384 printf '\tstrip: {\n'
385 printf '\t\tnone: true,\n'
386 printf '\t},\n'
387 printf '\ttarget: {\n'
388 if [ "$EXTRA" = "both" ]; then
389 printf '\t\tandroid_arm: {\n'
390 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
391 printf '\t\t},\n'
392 printf '\t\tandroid_arm64: {\n'
393 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
394 printf '\t\t},\n'
395 elif [ "$EXTRA" = "64" ]; then
396 printf '\t\tandroid_arm64: {\n'
397 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
398 printf '\t\t},\n'
399 else
400 printf '\t\tandroid_arm: {\n'
401 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
402 printf '\t\t},\n'
403 fi
404 printf '\t},\n'
405 if [ "$EXTRA" != "none" ]; then
406 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
407 fi
dianlujitao848101c2020-09-12 00:15:13 +0800408 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700409 elif [ "$CLASS" = "APPS" ]; then
410 printf 'android_app_import {\n'
411 printf '\tname: "%s",\n' "$PKGNAME"
412 printf '\towner: "%s",\n' "$VENDOR"
413 if [ "$EXTRA" = "priv-app" ]; then
414 SRC="$SRC/priv-app"
415 else
416 SRC="$SRC/app"
417 fi
418 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
419 if [ "$ARGS" = "PRESIGNED" ]; then
420 printf '\tpresigned: true,\n'
421 elif [ ! -z "$ARGS" ]; then
422 printf '\tcertificate: "%s",\n' "$ARGS"
423 else
424 printf '\tcertificate: "platform",\n'
425 fi
426 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
427 printf 'dex_import {\n'
428 printf '\tname: "%s",\n' "$PKGNAME"
429 printf '\towner: "%s",\n' "$VENDOR"
430 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
431 elif [ "$CLASS" = "ETC" ]; then
432 if [ "$EXTENSION" = "xml" ]; then
433 printf 'prebuilt_etc_xml {\n'
434 else
435 printf 'prebuilt_etc {\n'
436 fi
437 printf '\tname: "%s",\n' "$PKGNAME"
438 printf '\towner: "%s",\n' "$VENDOR"
439 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200440 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700441 elif [ "$CLASS" = "EXECUTABLES" ]; then
442 if [ "$EXTENSION" = "sh" ]; then
443 printf 'sh_binary {\n'
444 else
445 printf 'cc_prebuilt_binary {\n'
446 fi
447 printf '\tname: "%s",\n' "$PKGNAME"
448 printf '\towner: "%s",\n' "$VENDOR"
449 if [ "$ARGS" = "rootfs" ]; then
450 SRC="$SRC/rootfs"
451 if [ "$EXTRA" = "sbin" ]; then
452 SRC="$SRC/sbin"
453 printf '\tdist {\n'
454 printf '\t\tdest: "%s",\n' "root/sbin"
455 printf '\t},'
456 fi
457 else
458 SRC="$SRC/bin"
459 fi
460 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
461 unset EXTENSION
462 else
463 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
464 fi
465 if [ "$CLASS" = "APPS" ]; then
466 printf '\tdex_preopt: {\n'
467 printf '\t\tenabled: false,\n'
468 printf '\t},\n'
469 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200470 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700471 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200472 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700473 fi
474 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200475 if [ "$CLASS" = "ETC" ] ; then
476 if [ "$DIRNAME" != "." ]; then
477 printf '\tsub_dir: "%s",\n' "$DIRNAME"
478 fi
479 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700480 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
481 printf '\tprefer: true,\n'
482 fi
483 if [ "$EXTRA" = "priv-app" ]; then
484 printf '\tprivileged: true,\n'
485 fi
486 if [ "$PARTITION" = "vendor" ]; then
487 printf '\tsoc_specific: true,\n'
488 elif [ "$PARTITION" = "product" ]; then
489 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200490 elif [ "$PARTITION" = "system_ext" ]; then
491 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700492 elif [ "$PARTITION" = "odm" ]; then
493 printf '\tdevice_specific: true,\n'
494 fi
495 printf '}\n\n'
496 done
497}
498
499#
500# write_makefile_packages:
501#
502# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200503# $2: /odm, /product, /system_ext, or /vendor partition
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700504# $3: type-specific extra flags
505# $4: Name of the array holding the target list
506#
507# Internal function which writes out the BUILD_PREBUILT stanzas
508# for all modules in the list. This is called by write_product_packages
509# after the modules are categorized.
510#
511function write_makefile_packages() {
Steve Kondik5bd66602016-07-15 10:39:58 -0700512
513 local CLASS="$1"
razorlovesa0d296b2019-07-29 02:21:34 -0500514 local PARTITION="$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700515 local EXTRA="$3"
516
517 # Yes, this is a horrible hack - we create a new array using indirection
518 local ARR_NAME="$4[@]"
519 local FILELIST=("${!ARR_NAME}")
520
521 local FILE=
522 local ARGS=
523 local BASENAME=
524 local EXTENSION=
525 local PKGNAME=
526 local SRC=
527
528 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300529 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700530 ARGS=$(target_args "$P")
531
532 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100533 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700534 EXTENSION=${BASENAME##*.}
Mohd Farazcb0f4872019-10-08 16:13:50 +0530535 EXTENSION="."$EXTENSION
536 if [ "$EXTENSION" = ".jar" ]; then
537 EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
538 elif [ "$EXTENSION" = ".apk" ]; then
539 EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
540 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700541 PKGNAME=${BASENAME%.*}
542
543 # Add to final package list
544 PACKAGE_LIST+=("$PKGNAME")
545
546 SRC="proprietary"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400547 if [ "$PARTITION" = "system" ]; then
548 SRC+="/system"
549 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700550 SRC+="/vendor"
razorlovesa0d296b2019-07-29 02:21:34 -0500551 elif [ "$PARTITION" = "product" ]; then
552 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200553 elif [ "$PARTITION" = "system_ext" ]; then
554 SRC+="/system_ext"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700555 elif [ "$PARTITION" = "odm" ]; then
556 SRC+="/odm"
Steve Kondik5bd66602016-07-15 10:39:58 -0700557 fi
558
559 printf 'include $(CLEAR_VARS)\n'
560 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
561 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
562 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
563 if [ "$EXTRA" = "both" ]; then
564 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
565 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
566 #if [ "$VENDOR_PKG" = "true" ]; then
567 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
568 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
569 #else
570 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
571 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
572 #fi
573 elif [ "$EXTRA" = "64" ]; then
574 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
575 else
576 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
577 fi
578 if [ "$EXTRA" != "none" ]; then
579 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
580 fi
581 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200582 if [ "$EXTRA" = "priv-app" ]; then
583 SRC="$SRC/priv-app"
584 else
585 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700586 fi
587 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
588 local CERT=platform
589 if [ ! -z "$ARGS" ]; then
590 CERT="$ARGS"
591 fi
592 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
593 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
594 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200595 local CERT=platform
596 if [ ! -z "$ARGS" ]; then
597 CERT="$ARGS"
598 fi
599 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700600 elif [ "$CLASS" = "ETC" ]; then
601 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
602 elif [ "$CLASS" = "EXECUTABLES" ]; then
603 if [ "$ARGS" = "rootfs" ]; then
604 SRC="$SRC/rootfs"
605 if [ "$EXTRA" = "sbin" ]; then
606 SRC="$SRC/sbin"
607 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
608 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
609 fi
610 else
611 SRC="$SRC/bin"
612 fi
613 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
614 unset EXTENSION
615 else
616 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
617 fi
618 printf 'LOCAL_MODULE_TAGS := optional\n'
619 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400620 if [ "$CLASS" = "APPS" ]; then
621 printf 'LOCAL_DEX_PREOPT := false\n'
622 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700623 if [ ! -z "$EXTENSION" ]; then
Mohd Farazcb0f4872019-10-08 16:13:50 +0530624 printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
Steve Kondik5bd66602016-07-15 10:39:58 -0700625 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100626 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
627 if [ "$DIRNAME" != "." ]; then
628 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
629 fi
630 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700631 if [ "$EXTRA" = "priv-app" ]; then
632 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
633 fi
razorlovesa0d296b2019-07-29 02:21:34 -0500634 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800635 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesa0d296b2019-07-29 02:21:34 -0500636 elif [ "$PARTITION" = "product" ]; then
637 printf 'LOCAL_PRODUCT_MODULE := true\n'
Luca Stefani776be462020-09-09 15:53:58 +0200638 elif [ "$PARTITION" = "system_ext" ]; then
639 printf 'LOCAL_SYSTEM_EXT_MODULE := true\n'
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700640 elif [ "$PARTITION" = "odm" ]; then
641 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700642 fi
643 printf 'include $(BUILD_PREBUILT)\n\n'
644 done
645}
646
647#
648# write_product_packages:
649#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700650# This function will create prebuilt entries in the
651# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700652# product makefile for all files in the blob list which
653# start with a single dash (-) character.
654#
655function write_product_packages() {
656 PACKAGE_LIST=()
657
658 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
659
660 if [ "$COUNT" = "0" ]; then
661 return 0
662 fi
663
664 # Figure out what's 32-bit, what's 64-bit, and what's multilib
665 # I really should not be doing this in bash due to shitty array passing :(
666 local T_LIB32=( $(prefix_match "lib/") )
667 local T_LIB64=( $(prefix_match "lib64/") )
668 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
669 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
670 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
671
672 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700673 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700674 fi
675 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700676 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700677 fi
678 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700679 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700680 fi
681
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400682 local T_S_LIB32=( $(prefix_match "system/lib/") )
683 local T_S_LIB64=( $(prefix_match "system/lib64/") )
684 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
685 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
686 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
687
688 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700689 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400690 fi
691 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700692 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400693 fi
694 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700695 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400696 fi
697
Steve Kondik5bd66602016-07-15 10:39:58 -0700698 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
699 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
700 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
701 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
702 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
703
704 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700705 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700706 fi
707 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700708 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700709 fi
710 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700711 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500712 fi
713
714 local T_P_LIB32=( $(prefix_match "product/lib/") )
715 local T_P_LIB64=( $(prefix_match "product/lib64/") )
716 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
717 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
718 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
719
720 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700721 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500722 fi
723 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700724 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500725 fi
726 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700727 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700728 fi
729
Luca Stefani776be462020-09-09 15:53:58 +0200730 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
731 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
732 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
733 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
734 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
735
736 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
737 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
738 fi
739 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
740 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
741 fi
742 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
743 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
744 fi
745
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700746 local T_O_LIB32=( $(prefix_match "odm/lib/") )
747 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
748 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
749 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
750 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
751
752 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700753 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700754 fi
755 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700756 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700757 fi
758 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700759 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700760 fi
761
Steve Kondik5bd66602016-07-15 10:39:58 -0700762 # Apps
763 local APPS=( $(prefix_match "app/") )
764 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100765 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700766 fi
767 local PRIV_APPS=( $(prefix_match "priv-app/") )
768 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100769 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700770 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400771 local S_APPS=( $(prefix_match "system/app/") )
772 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100773 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400774 fi
775 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
776 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100777 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400778 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700779 local V_APPS=( $(prefix_match "vendor/app/") )
780 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100781 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700782 fi
783 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
784 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100785 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500786 fi
787 local P_APPS=( $(prefix_match "product/app/") )
788 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100789 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500790 fi
791 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
792 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100793 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700794 fi
Luca Stefani776be462020-09-09 15:53:58 +0200795 local SE_APPS=( $(prefix_match "system_ext/app/") )
796 if [ "${#SE_APPS[@]}" -gt "0" ]; then
797 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
798 fi
799 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
800 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
801 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
802 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700803 local O_APPS=( $(prefix_match "odm/app/") )
804 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100805 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700806 fi
807 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
808 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100809 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700810 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700811
812 # Framework
813 local FRAMEWORK=( $(prefix_match "framework/") )
814 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700815 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700816 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400817 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
818 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700819 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400820 fi
Christian Oder974b5902017-10-08 23:15:52 +0200821 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200822 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700823 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500824 fi
825 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
826 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700827 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200828 fi
Luca Stefani776be462020-09-09 15:53:58 +0200829 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700830 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200831 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
832 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700833 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
834 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700835 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700836 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700837
838 # Etc
839 local ETC=( $(prefix_match "etc/") )
840 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700841 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700842 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400843 local S_ETC=( $(prefix_match "system/etc/") )
844 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700845 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400846 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700847 local V_ETC=( $(prefix_match "vendor/etc/") )
848 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700849 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500850 fi
851 local P_ETC=( $(prefix_match "product/etc/") )
852 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700853 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700854 fi
Luca Stefani776be462020-09-09 15:53:58 +0200855 local SE_ETC=( $(prefix_match "system_ext/etc/") )
856 if [ "${#SE_ETC[@]}" -gt "0" ]; then
857 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
858 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700859 local O_ETC=( $(prefix_match "odm/etc/") )
860 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700861 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700862 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700863
864 # Executables
865 local BIN=( $(prefix_match "bin/") )
866 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700867 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700868 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400869 local S_BIN=( $(prefix_match "system/bin/") )
870 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700871 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400872 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700873 local V_BIN=( $(prefix_match "vendor/bin/") )
874 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700875 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500876 fi
877 local P_BIN=( $(prefix_match "product/bin/") )
878 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700879 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700880 fi
Luca Stefani776be462020-09-09 15:53:58 +0200881 local SE_BIN=( $(prefix_match "system_ext/bin/") )
882 if [ "${#SE_BIN[@]}" -gt "0" ]; then
883 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
884 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700885 local O_BIN=( $(prefix_match "odm/bin/") )
886 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700887 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700888 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700889 local SBIN=( $(prefix_match "sbin/") )
890 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700891 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700892 fi
893
894
895 # Actually write out the final PRODUCT_PACKAGES list
896 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
897
898 if [ "$PACKAGE_COUNT" -eq "0" ]; then
899 return 0
900 fi
901
902 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
903 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
904 local LINEEND=" \\"
905 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
906 LINEEND=""
907 fi
908 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
909 done
910}
911
912#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700913# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700914#
915# $1: file which will be written to
916#
917# writes out the copyright header with the current year.
918# note that this is not an append operation, and should
919# be executed first!
920#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700921function write_blueprint_header() {
922 if [ -f $1 ]; then
923 rm $1
924 fi
925
926 YEAR=$(date +"%Y")
927
928 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
929
930 printf "/**\n" > $1
931 NUM_REGEX='^[0-9]+$'
932 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
933 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
934 else
935 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
936 fi
937
938 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
939 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
940 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
941 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
942 else
943 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
944 fi
945
946 cat << EOF >> $1
947 *
948 * Licensed under the Apache License, Version 2.0 (the "License");
949 * you may not use this file except in compliance with the License.
950 * You may obtain a copy of the License at
951 *
952 * http://www.apache.org/licenses/LICENSE-2.0
953 *
954 * Unless required by applicable law or agreed to in writing, software
955 * distributed under the License is distributed on an "AS IS" BASIS,
956 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
957 * See the License for the specific language governing permissions and
958 * limitations under the License.
959 *
960 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
961 */
962
963EOF
964}
965
966#
967# write_makefile_header:
968#
969# $1: file which will be written to
970#
971# writes out the copyright header with the current year.
972# note that this is not an append operation, and should
973# be executed first!
974#
975function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500976 if [ -f $1 ]; then
977 rm $1
978 fi
979
Steve Kondik5bd66602016-07-15 10:39:58 -0700980 YEAR=$(date +"%Y")
981
982 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
983
Jake Whatley9843b322017-01-25 21:49:16 -0500984 NUM_REGEX='^[0-9]+$'
985 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
986 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
987 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
988 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
989 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
990 fi
991 if [ $YEAR -eq 2017 ]; then
992 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
993 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
994 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
995 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
996 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
997 else
998 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
999 fi
1000 else
1001 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
1002 fi
1003
1004 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -07001005#
1006# Licensed under the Apache License, Version 2.0 (the "License");
1007# you may not use this file except in compliance with the License.
1008# You may obtain a copy of the License at
1009#
1010# http://www.apache.org/licenses/LICENSE-2.0
1011#
1012# Unless required by applicable law or agreed to in writing, software
1013# distributed under the License is distributed on an "AS IS" BASIS,
1014# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1015# See the License for the specific language governing permissions and
1016# limitations under the License.
1017
1018# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1019
1020EOF
1021}
1022
1023#
1024# write_headers:
1025#
1026# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001027# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001028#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001029# Calls write_makefile_header for each of the makefiles and
1030# write_blueprint_header for Android.bp and creates the initial
1031# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001032#
1033function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001034 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001035
1036 GUARD="$2"
1037 if [ -z "$GUARD" ]; then
1038 GUARD="TARGET_DEVICE"
1039 fi
1040
Steve Kondik5bd66602016-07-15 10:39:58 -07001041 cat << EOF >> "$ANDROIDMK"
1042LOCAL_PATH := \$(call my-dir)
1043
1044EOF
1045 if [ "$COMMON" -ne 1 ]; then
1046 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001047ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001048
1049EOF
1050 else
1051 if [ -z "$1" ]; then
1052 echo "Argument with devices to be added to guard must be set!"
1053 exit 1
1054 fi
1055 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001056ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001057
1058EOF
1059 fi
1060
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001061 write_makefile_header "$BOARDMK"
1062 write_makefile_header "$PRODUCTMK"
1063 write_blueprint_header "$ANDROIDBP"
1064
1065 cat << EOF >> "$ANDROIDBP"
1066soong_namespace {
1067}
1068
1069EOF
1070
1071 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1072 cat << EOF >> "$PRODUCTMK"
1073PRODUCT_SOONG_NAMESPACES += \\
1074 vendor/$VENDOR/$DEVICE
1075
1076EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001077}
1078
1079#
1080# write_footers:
1081#
1082# Closes the inital guard and any other finalization tasks. Must
1083# be called as the final step.
1084#
1085function write_footers() {
1086 cat << EOF >> "$ANDROIDMK"
1087endif
1088EOF
1089}
1090
1091# Return success if adb is up and not in recovery
1092function _adb_connected {
1093 {
Jake Whatley9843b322017-01-25 21:49:16 -05001094 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001095 then
1096 return 0
1097 fi
1098 } 2>/dev/null
1099
1100 return 1
1101};
1102
1103#
1104# parse_file_list:
1105#
1106# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001107# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001108#
1109# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1110#
1111function parse_file_list() {
1112 if [ -z "$1" ]; then
1113 echo "An input file is expected!"
1114 exit 1
1115 elif [ ! -f "$1" ]; then
1116 echo "Input file "$1" does not exist!"
1117 exit 1
1118 fi
1119
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001120 if [ -n "$2" ]; then
1121 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001122 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001123 # Match all lines starting with first line found to start* with '#'
1124 # comment and contain** $2, and ending with first line to be empty*.
1125 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1126 # **the $2 match is case-insensitive
1127 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001128 else
1129 LIST=$1
1130 fi
1131
1132
Steve Kondik5bd66602016-07-15 10:39:58 -07001133 PRODUCT_PACKAGES_LIST=()
1134 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001135 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001136 PRODUCT_COPY_FILES_LIST=()
1137 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001138 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001139
1140 while read -r line; do
1141 if [ -z "$line" ]; then continue; fi
1142
1143 # If the line has a pipe delimiter, a sha1 hash should follow.
1144 # This indicates the file should be pinned and not overwritten
1145 # when extracting files.
1146 local SPLIT=(${line//\|/ })
1147 local COUNT=${#SPLIT[@]}
1148 local SPEC=${SPLIT[0]}
1149 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001150 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001151 if [ "$COUNT" -gt "1" ]; then
1152 HASH=${SPLIT[1]}
1153 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001154 if [ "$COUNT" -gt "2" ]; then
1155 FIXUP_HASH=${SPLIT[2]}
1156 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001157
1158 # if line starts with a dash, it needs to be packaged
1159 if [[ "$SPEC" =~ ^- ]]; then
1160 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1161 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001162 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001163 else
1164 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1165 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001166 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001167 fi
1168
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001169 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001170}
1171
1172#
1173# write_makefiles:
1174#
1175# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001176# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001177#
1178# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001179# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -07001180# the product makefile.
1181#
1182function write_makefiles() {
1183 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001184 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001185 write_product_packages
1186}
1187
1188#
1189# append_firmware_calls_to_makefiles:
1190#
1191# Appends to Android.mk the calls to all images present in radio folder
1192# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1193#
1194function append_firmware_calls_to_makefiles() {
1195 cat << EOF >> "$ANDROIDMK"
1196ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1197
1198RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1199\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1200 \$(call add-radio-file,radio/\$(f)))
1201\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1202
1203endif
1204
1205EOF
1206}
1207
1208#
1209# get_file:
1210#
1211# $1: input file
1212# $2: target file/folder
1213# $3: source of the file (can be "adb" or a local folder)
1214#
1215# Silently extracts the input file to defined target
1216# Returns success if file can be pulled from the device or found locally
1217#
1218function get_file() {
1219 local SRC="$3"
1220
1221 if [ "$SRC" = "adb" ]; then
1222 # try to pull
1223 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1224
1225 return 1
1226 else
1227 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001228 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1229 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001230 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001231
1232 return 1
1233 fi
1234};
1235
1236#
1237# oat2dex:
1238#
1239# $1: extracted apk|jar (to check if deodex is required)
1240# $2: odexed apk|jar to deodex
1241# $3: source of the odexed apk|jar
1242#
1243# Convert apk|jar .odex in the corresposing classes.dex
1244#
1245function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001246 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001247 local OEM_TARGET="$2"
1248 local SRC="$3"
1249 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001250 local OAT=
XiNGRZd7793bc2019-12-24 10:37:13 +08001251 local HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik5bd66602016-07-15 10:39:58 -07001252
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001253 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1254 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1255 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001256 fi
1257
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001258 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001259 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001260 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001261
codeworkx85eda752018-09-23 12:36:57 +02001262 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001263 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001264 fi
1265
Steve Kondik5bd66602016-07-15 10:39:58 -07001266 # Extract existing boot.oats to the temp folder
1267 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001268 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001269 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001270 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001271 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001272 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001273 else
1274 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001275 fi
1276 done
1277 fi
1278
1279 if [ -z "$ARCHES" ]; then
1280 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1281 fi
1282
theimpulson9a911af2019-08-14 03:25:12 +00001283 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001284 return;
1285 fi
1286
theimpulson9a911af2019-08-14 03:25:12 +00001287 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001288 return 0 # target apk|jar is already odexed, return
1289 fi
1290
1291 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001292 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001293
Joe Maplesfb3941c2018-01-05 14:51:33 -05001294 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1295 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001296
Joe Maplesfb3941c2018-01-05 14:51:33 -05001297 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1298 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1299 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001300 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1301 for CLASS in $CLASSES; do
1302 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1303 # Check if we have to deal with CompactDex
1304 if [[ "$CLASS" == *.cdex ]]; then
1305 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1306 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1307 else
1308 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1309 fi
1310 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001311 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001312 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1313 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001314 fi
theimpulson9a911af2019-08-14 03:25:12 +00001315 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001316 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001317 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001318 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001319 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001320 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001321 # try to extract classes.dex from boot.vdex for frameworks jars
1322 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001323 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001324 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001325 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1326 for CLASS in $CLASSES; do
1327 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1328 # Check if we have to deal with CompactDex
1329 if [[ "$CLASS" == *.cdex ]]; then
1330 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1331 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1332 else
1333 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1334 fi
1335 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001336 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001337 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1338 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001339 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001340 else
1341 continue
1342 fi
1343
Steve Kondik5bd66602016-07-15 10:39:58 -07001344 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001345
1346 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001347}
1348
1349#
1350# init_adb_connection:
1351#
1352# Starts adb server and waits for the device
1353#
1354function init_adb_connection() {
1355 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1356 if ! _adb_connected; then
1357 echo "No device is online. Waiting for one..."
1358 echo "Please connect USB and/or enable USB debugging"
1359 until _adb_connected; do
1360 sleep 1
1361 done
1362 echo "Device Found."
1363 fi
1364
1365 # Retrieve IP and PORT info if we're using a TCP connection
1366 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1367 | head -1 | awk '{print $1}')
1368 adb root &> /dev/null
1369 sleep 0.3
1370 if [ -n "$TCPIPPORT" ]; then
1371 # adb root just killed our connection
1372 # so reconnect...
1373 adb connect "$TCPIPPORT"
1374 fi
1375 adb wait-for-device &> /dev/null
1376 sleep 0.3
1377}
1378
1379#
1380# fix_xml:
1381#
1382# $1: xml file to fix
1383#
1384function fix_xml() {
1385 local XML="$1"
1386 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1387
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001388 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1389 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001390
1391 mv "$TEMP_XML" "$XML"
1392}
1393
Vladimir Olteande985fe2019-01-17 03:07:34 +02001394function get_hash() {
1395 local FILE="$1"
1396
1397 if [ "$(uname)" == "Darwin" ]; then
1398 shasum "${FILE}" | awk '{print $1}'
1399 else
1400 sha1sum "${FILE}" | awk '{print $1}'
1401 fi
1402}
1403
Vladimir Olteana7d20492019-01-17 03:05:52 +02001404function print_spec() {
1405 local SPEC_PRODUCT_PACKAGE="$1"
1406 local SPEC_SRC_FILE="$2"
1407 local SPEC_DST_FILE="$3"
1408 local SPEC_ARGS="$4"
1409 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001410 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001411
1412 local PRODUCT_PACKAGE=""
1413 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1414 PRODUCT_PACKAGE="-"
1415 fi
1416 local SRC=""
1417 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1418 SRC="${SPEC_SRC_FILE}:"
1419 fi
1420 local DST=""
1421 if [ ! -z "${SPEC_DST_FILE}" ]; then
1422 DST="${SPEC_DST_FILE}"
1423 fi
1424 local ARGS=""
1425 if [ ! -z "${SPEC_ARGS}" ]; then
1426 ARGS=";${SPEC_ARGS}"
1427 fi
1428 local HASH=""
1429 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1430 HASH="|${SPEC_HASH}"
1431 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001432 local FIXUP_HASH=""
1433 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1434 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1435 fi
1436 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1437}
1438
1439# To be overridden by device-level extract-files.sh
1440# Parameters:
1441# $1: spec name of a blob. Can be used for filtering.
1442# If the spec is "src:dest", then $1 is "dest".
1443# If the spec is "src", then $1 is "src".
1444# $2: path to blob file. Can be used for fixups.
1445#
1446function blob_fixup() {
1447 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001448}
1449
Steve Kondik5bd66602016-07-15 10:39:58 -07001450#
1451# extract:
1452#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001453# Positional parameters:
1454# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001455# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001456# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1457#
1458# Non-positional parameters (coming after $2):
1459# --section: preferred way of selecting the portion to parse and extract from
1460# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001461# --kang: if present, this option will activate the printing of hashes for the
1462# extracted blobs. Useful with --section for subsequent pinning of
1463# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001464#
1465function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001466 # Consume positional parameters
1467 local PROPRIETARY_FILES_TXT="$1"; shift
1468 local SRC="$1"; shift
1469 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001470 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001471
1472 # Consume optional, non-positional parameters
1473 while [ "$#" -gt 0 ]; do
1474 case "$1" in
1475 -s|--section)
1476 SECTION="$2"; shift
1477 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001478 -k|--kang)
1479 KANG=true
1480 DISABLE_PINNING=1
1481 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001482 *)
1483 # Backwards-compatibility with the old behavior, where $3, if
1484 # present, denoted an optional positional ${SECTION} argument.
1485 # Users of ${SECTION} are encouraged to migrate from setting it as
1486 # positional $3, to non-positional --section ${SECTION}, the
1487 # reason being that it doesn't scale to have more than 1 optional
1488 # positional argument.
1489 SECTION="$1"
1490 ;;
1491 esac
1492 shift
1493 done
1494
Steve Kondik5bd66602016-07-15 10:39:58 -07001495 if [ -z "$OUTDIR" ]; then
1496 echo "Output dir not set!"
1497 exit 1
1498 fi
1499
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001500 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001501
1502 # Allow failing, so we can try $DEST and/or $FILE
1503 set +e
1504
1505 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1506 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001507 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001508 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001509 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001510 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001511 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1512
1513 if [ "$SRC" = "adb" ]; then
1514 init_adb_connection
1515 fi
1516
Dan Pasanen0cc05012017-03-21 09:06:11 -05001517 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001518 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001519
1520 # Check if we're working with the same zip that was passed last time.
1521 # If so, let's just use what's already extracted.
1522 MD5=`md5sum "$SRC"| awk '{print $1}'`
1523 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1524
1525 if [ "$MD5" != "$OLDMD5" ]; then
1526 rm -rf "$DUMPDIR"
1527 mkdir "$DUMPDIR"
1528 unzip "$SRC" -d "$DUMPDIR"
1529 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1530
1531 # Stop if an A/B OTA zip is detected. We cannot extract these.
1532 if [ -a "$DUMPDIR"/payload.bin ]; then
1533 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1534 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001535 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001536
Luca Stefani776be462020-09-09 15:53:58 +02001537 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001538 do
1539 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001540 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1541 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1542 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1543 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1544 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001545 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1546 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1547 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1548 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1549 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1550 echo "Requesting sudo access to mount the "$PARTITION".img"
1551 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1552 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1553 sudo umount "$DUMPDIR"/tmp
1554 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1555 fi
1556 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001557 fi
1558
1559 SRC="$DUMPDIR"
1560 fi
1561
Steve Kondik5bd66602016-07-15 10:39:58 -07001562 if [ "$VENDOR_STATE" -eq "0" ]; then
1563 echo "Cleaning output directory ($OUTPUT_ROOT).."
1564 rm -rf "${OUTPUT_TMP:?}"
1565 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001566 if [ -d "$OUTPUT_ROOT" ]; then
1567 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1568 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001569 VENDOR_STATE=1
1570 fi
1571
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001572 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001573
1574 for (( i=1; i<COUNT+1; i++ )); do
1575
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001576 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001577 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001578 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001579 local OUTPUT_DIR=
1580 local TMP_DIR=
1581 local SRC_FILE=
1582 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001583 local IS_PRODUCT_PACKAGE=false
1584
1585 # Note: this relies on the fact that the ${FILELIST[@]} array
1586 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1587 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1588 IS_PRODUCT_PACKAGE=true
1589 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001590
Vladimir Olteand6391332018-06-24 20:42:01 +03001591 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001592 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1593 TMP_DIR="${OUTPUT_TMP}/rootfs"
1594 SRC_FILE="/${SPEC_SRC_FILE}"
1595 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001596 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001597 OUTPUT_DIR="${OUTPUT_ROOT}"
1598 TMP_DIR="${OUTPUT_TMP}"
1599 SRC_FILE="/system/${SPEC_SRC_FILE}"
1600 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001601 fi
1602
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001603 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001604 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001605 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001606 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001607
Gabriele M58270a32017-11-13 23:15:29 +01001608 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001609 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001610 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001611 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001612 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001613 if [ -f "${VENDOR_REPO_FILE}" ]; then
1614 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001615 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001616 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001617 fi
1618 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001619 local TMP_HASH=$(get_hash "${PINNED}")
1620 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001621 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001622 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1623 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001624 fi
1625 fi
1626 fi
1627 fi
1628
Vladimir Olteana7d20492019-01-17 03:05:52 +02001629 if [ "${KANG}" = false ]; then
1630 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1631 fi
1632
Gabriele M58270a32017-11-13 23:15:29 +01001633 if [ "$KEEP" = "1" ]; then
Vladimir Olteana7d20492019-01-17 03:05:52 +02001634 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001635 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001636 FOUND=false
1637 # Try Lineage target first.
1638 # Also try to search for files stripped of
1639 # the "/system" prefix, if we're actually extracting
1640 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001641 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001642 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1643 FOUND=true
1644 break
1645 }
1646 done
1647
1648 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001649 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001650 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001651 fi
1652 fi
1653
Vladimir Olteande985fe2019-01-17 03:07:34 +02001654 # Blob fixup pipeline has 2 parts: one that is fixed and
1655 # one that is user-configurable
1656 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1657 # Deodex apk|jar if that's the case
1658 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1659 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1660 if [ -f "$TMPDIR/classes.dex" ]; then
dianlujitaoded7c1e2020-04-06 12:45:36 +08001661 touch -t 200901010000 "$TMPDIR/classes"*
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001662 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1663 rm "$TMPDIR/classes"*
Vladimir Olteande985fe2019-01-17 03:07:34 +02001664 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001665 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001666 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1667 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001668 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001669 # Now run user-supplied fixup function
1670 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1671 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001672
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001673 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001674 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001675 local TYPE="${DIR##*/}"
1676 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001677 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001678 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001679 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001680 fi
1681 fi
1682
Vladimir Olteana7d20492019-01-17 03:05:52 +02001683 if [ "${KANG}" = true ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001684 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1685 fi
1686
1687 # Check and print whether the fixup pipeline actually did anything.
1688 # This isn't done right after the fixup pipeline because we want this print
1689 # to come after print_spec above, when in kang mode.
1690 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1691 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1692 # Now sanity-check the spec for this blob.
1693 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1694 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1695 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1696 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001697 fi
1698
Steve Kondik5bd66602016-07-15 10:39:58 -07001699 done
1700
1701 # Don't allow failing
1702 set -e
1703}
1704
1705#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001706# extract2:
1707#
1708# Positional parameters:
1709# $1: file containing the list of items to extract (aka proprietary-files.txt)
1710#
1711# Non-positional parameters (coming after $2):
1712# --section: selects the portion to parse and extracts from proprietary-files.txt
1713# --kang: if present, this option will activate the printing of hashes for the
1714# extracted blobs. Useful with --section for subsequent pinning of
1715# blobs taken from other origins.
1716#
1717function extract2() {
1718 # Consume positional parameters
1719 local PROPRIETARY_FILES_TXT="$1"; shift
1720 local SECTION=""
1721 local KANG=false
1722
1723 # Consume optional, non-positional parameters
1724 while [ "$#" -gt 0 ]; do
1725 case "$1" in
1726 --adb)
1727 ADB=true
1728 ;;
1729 --system)
1730 SYSTEM_SRC="$2"; shift
1731 ;;
1732 --vendor)
1733 VENDOR_SRC="$2"; shift
1734 ;;
1735 --odm)
1736 ODM_SRC="$2"; shift
1737 ;;
1738 --product)
1739 PRODUCT_SRC="$2"; shift
1740 ;;
1741 -s|--section)
1742 SECTION="$2"; shift
1743 ;;
1744 -k|--kang)
1745 KANG=true
1746 DISABLE_PINNING=1
1747 ;;
1748 esac
1749 shift
1750 done
1751
1752 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1753 echo "No sources set! You must select --adb or pass paths to partition dumps."
1754 exit 1
1755 fi
1756
1757 if [ -z "$OUTDIR" ]; then
1758 echo "Output dir not set!"
1759 exit 1
1760 fi
1761
1762 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1763
1764 # Allow failing, so we can try $DEST and/or $FILE
1765 set +e
1766
1767 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1768 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1769 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1770 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1771 local COUNT=${#FILELIST[@]}
1772 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1773 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1774
1775 if [ "$ADB" = true ]; then
1776 init_adb_connection
1777 fi
1778
1779 if [ "$VENDOR_STATE" -eq "0" ]; then
1780 echo "Cleaning output directory ($OUTPUT_ROOT).."
1781 rm -rf "${OUTPUT_TMP:?}"
1782 mkdir -p "${OUTPUT_TMP:?}"
1783 if [ -d "$OUTPUT_ROOT" ]; then
1784 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1785 fi
1786 VENDOR_STATE=1
1787 fi
1788
1789 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1790
1791 for (( i=1; i<COUNT+1; i++ )); do
1792
1793 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1794 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1795 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1796 local OUTPUT_DIR=
1797 local TMP_DIR=
1798 local SRC_FILE=
1799 local DST_FILE=
1800 local IS_PRODUCT_PACKAGE=false
1801
1802 # Note: this relies on the fact that the ${FILELIST[@]} array
1803 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1804 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1805 IS_PRODUCT_PACKAGE=true
1806 fi
1807
1808 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1809 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1810 TMP_DIR="${OUTPUT_TMP}/rootfs"
1811 else
1812 OUTPUT_DIR="${OUTPUT_ROOT}"
1813 TMP_DIR="${OUTPUT_TMP}"
1814 fi
1815 SRC_FILE="${SPEC_SRC_FILE}"
1816 DST_FILE="${SPEC_DST_FILE}"
1817
1818 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1819 local BLOB_DISPLAY_NAME="${DST_FILE}"
1820 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1821
1822 # Check pinned files
1823 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1824 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1825 local KEEP=""
1826 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1827 if [ -f "${VENDOR_REPO_FILE}" ]; then
1828 local PINNED="${VENDOR_REPO_FILE}"
1829 else
1830 local PINNED="${TMP_DIR}${DST_FILE}"
1831 fi
1832 if [ -f "$PINNED" ]; then
1833 local TMP_HASH=$(get_hash "${PINNED}")
1834 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1835 KEEP="1"
1836 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1837 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1838 fi
1839 fi
1840 fi
1841 fi
1842
1843 if [ "${KANG}" = false ]; then
1844 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1845 fi
1846
1847 if [ "$KEEP" = "1" ]; then
1848 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1849 else
1850 FOUND=false
1851 PARTITION_SOURCE_DIR=
1852 # Try Lineage target first.
1853 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1854 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1855 if [ "$PARTITION" = "system" ]; then
1856 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1857 elif [ "$PARTITION" = "vendor" ]; then
1858 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1859 elif [ "$PARTITION" = "product" ]; then
1860 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1861 elif [ "$PARTITION" = "odm" ]; then
1862 PARTITION_SOURCE_DIR="$ODM_SRC"
1863 fi
1864 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1865 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1866 FOUND=true
1867 break
1868 }
1869 # Search with the full system/ prefix if the file was not found on the system partition
1870 # because we may be searching in a mounted system-as-root system.img
1871 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1872 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1873 FOUND=true
1874 break
1875 }
1876 fi
1877 done
1878
1879 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1880 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1881 fi
1882
1883 if [ "${FOUND}" = false ]; then
1884 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1885 continue
1886 fi
1887 fi
1888
1889 # Blob fixup pipeline has 2 parts: one that is fixed and
1890 # one that is user-configurable
1891 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1892 # Deodex apk|jar if that's the case
1893 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1894 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1895 if [ -f "$TMPDIR/classes.dex" ]; then
1896 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1897 rm "$TMPDIR/classes"*
1898 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1899 fi
1900 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1901 fix_xml "${VENDOR_REPO_FILE}"
1902 fi
1903 # Now run user-supplied fixup function
1904 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1905 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1906
1907 if [ -f "${VENDOR_REPO_FILE}" ]; then
1908 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1909 local TYPE="${DIR##*/}"
1910 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1911 chmod 755 "${VENDOR_REPO_FILE}"
1912 else
1913 chmod 644 "${VENDOR_REPO_FILE}"
1914 fi
1915 fi
1916
1917 if [ "${KANG}" = true ]; then
1918 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1919 fi
1920
1921 # Check and print whether the fixup pipeline actually did anything.
1922 # This isn't done right after the fixup pipeline because we want this print
1923 # to come after print_spec above, when in kang mode.
1924 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1925 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1926 # Now sanity-check the spec for this blob.
1927 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1928 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1929 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1930 fi
1931 fi
1932
1933 done
1934
1935 # Don't allow failing
1936 set -e
1937}
1938
1939#
Steve Kondik5bd66602016-07-15 10:39:58 -07001940# extract_firmware:
1941#
1942# $1: file containing the list of items to extract
1943# $2: path to extracted radio folder
1944#
1945function extract_firmware() {
1946 if [ -z "$OUTDIR" ]; then
1947 echo "Output dir not set!"
1948 exit 1
1949 fi
1950
1951 parse_file_list "$1"
1952
1953 # Don't allow failing
1954 set -e
1955
1956 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1957 local COUNT=${#FILELIST[@]}
1958 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001959 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001960
1961 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1962 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1963 rm -rf "${OUTPUT_DIR:?}/"*
1964 VENDOR_RADIO_STATE=1
1965 fi
1966
1967 echo "Extracting $COUNT files in $1 from $SRC:"
1968
1969 for (( i=1; i<COUNT+1; i++ )); do
1970 local FILE="${FILELIST[$i-1]}"
1971 printf ' - %s \n' "/radio/$FILE"
1972
1973 if [ ! -d "$OUTPUT_DIR" ]; then
1974 mkdir -p "$OUTPUT_DIR"
1975 fi
1976 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1977 chmod 644 "$OUTPUT_DIR/$FILE"
1978 done
1979}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001980
1981function extract_img_data() {
1982 local image_file="$1"
1983 local out_dir="$2"
1984 local logFile="$TMPDIR/debugfs.log"
1985
1986 if [ ! -d "$out_dir" ]; then
1987 mkdir -p "$out_dir"
1988 fi
1989
1990 if [[ "$HOST_OS" == "Darwin" ]]; then
1991 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1992 echo "[-] Failed to extract data from '$image_file'"
1993 abort 1
1994 }
1995 else
1996 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1997 do
1998 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1999 echo "[-] Failed to extract data from '$image_file'"
2000 abort 1
2001 }
2002 done
2003 fi
2004
2005 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2006 if grep -Fq "$symlink_err" "$logFile"; then
2007 echo "[-] Symlinks have not been properly processed from $image_file"
2008 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
2009 abort 1
2010 fi
2011}
2012
2013declare -ra VENDOR_SKIP_FILES=(
2014 "bin/toybox_vendor"
2015 "bin/toolbox"
2016 "bin/grep"
2017 "build.prop"
2018 "compatibility_matrix.xml"
2019 "default.prop"
2020 "etc/NOTICE.xml.gz"
2021 "etc/vintf/compatibility_matrix.xml"
2022 "etc/vintf/manifest.xml"
2023 "etc/wifi/wpa_supplicant.conf"
2024 "manifest.xml"
2025 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2026 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2027 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2028 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2029 "overlay/framework-res__auto_generated_rro.apk"
2030 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2031)
2032
2033function array_contains() {
2034 local element
2035 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2036 return 1
2037}
2038
2039function generate_prop_list_from_image() {
2040 local image_file="$1"
2041 local image_dir="$TMPDIR/image-temp"
2042 local output_list="$2"
2043 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
2044 local -n skipped_vendor_files="$3"
2045
2046 extract_img_data "$image_file" "$image_dir"
2047
2048 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2049 do
2050 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2051 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2052 continue
2053 fi
2054 # Skip device defined skipped files since they will be re-generated at build time
2055 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
2056 continue
2057 fi
2058 if suffix_match_file ".apk" "$FILE" ; then
2059 echo "-vendor/$FILE" >> "$output_list_tmp"
2060 else
2061 echo "vendor/$FILE" >> "$output_list_tmp"
2062 fi
2063 done
2064
2065 # Sort merged file with all lists
2066 sort -u "$output_list_tmp" > "$output_list"
2067
2068 # Clean-up
2069 rm -f "$output_list_tmp"
2070}