blob: eed6387481a2f6dc9bc2ca5a91e5b765585970c1 [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=()
SamarV-1215556e2d2024-03-19 08:50:02 +053024PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -070025PACKAGE_LIST=()
SamarV-1217e6b4082024-02-26 14:39:34 +053026REQUIRED_PACKAGES_LIST=
Steve Kondik5bd66602016-07-15 10:39:58 -070027VENDOR_STATE=-1
28VENDOR_RADIO_STATE=-1
29COMMON=-1
30ARCHES=
31FULLY_DEODEXED=-1
32
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053033SKIP_CLEANUP=${SKIP_CLEANUP:-0}
Chirayu Desai5da0bf82022-10-19 02:58:42 +053034EXTRACT_TMP_DIR=$(mktemp -d)
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +030035HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik5bd66602016-07-15 10:39:58 -070036
37#
38# cleanup
39#
40# kill our tmpfiles with fire on exit
41#
42function cleanup() {
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053043 if [ "$SKIP_CLEANUP" == "true" ] || [ "$SKIP_CLEANUP" == "1" ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +053044 echo "Skipping cleanup of $EXTRACT_TMP_DIR"
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053045 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +053046 rm -rf "${EXTRACT_TMP_DIR:?}"
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053047 fi
Steve Kondik5bd66602016-07-15 10:39:58 -070048}
49
Gabriele Mb8e54572017-10-11 12:55:51 +020050trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070051
52#
53# setup_vendor
54#
55# $1: device name
56# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000057# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070058# $4: is common device - optional, default to false
59# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050060# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070061#
62# Must be called before any other functions can be used. This
63# sets up the internal state for a new vendor configuration.
64#
65function setup_vendor() {
66 local DEVICE="$1"
67 if [ -z "$DEVICE" ]; then
68 echo "\$DEVICE must be set before including this script!"
69 exit 1
70 fi
71
72 export VENDOR="$2"
73 if [ -z "$VENDOR" ]; then
74 echo "\$VENDOR must be set before including this script!"
75 exit 1
76 fi
77
theimpulson9a911af2019-08-14 03:25:12 +000078 export OMNI_ROOT="$3"
79 if [ ! -d "$OMNI_ROOT" ]; then
80 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070081 exit 1
82 fi
83
84 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000085 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
86 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070087 fi
88
Jake Whatley9843b322017-01-25 21:49:16 -050089 VNDNAME="$6"
90 if [ -z "$VNDNAME" ]; then
91 VNDNAME="$DEVICE"
92 fi
93
theimpulsonbb72ab82019-08-14 06:03:32 +000094 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -070095 export ANDROIDBP="$OMNI_ROOT"/"$OUTDIR"/Android.bp
theimpulson9a911af2019-08-14 03:25:12 +000096 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
97 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070098
99 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
100 COMMON=1
101 else
102 COMMON=0
103 fi
104
Gabriele Mc44696d2017-05-01 18:22:04 +0200105 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700106 VENDOR_STATE=1
107 VENDOR_RADIO_STATE=1
108 else
109 VENDOR_STATE=0
110 VENDOR_RADIO_STATE=0
111 fi
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +0300112
113 if [ -z "$PATCHELF" ]; then
114 export PATCHELF="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/patchelf
115 fi
Michael Bestasf55ac292022-03-23 23:15:23 +0200116
117 if [ -z "$SIGSCAN" ]; then
118 export SIGSCAN="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/SigScan
119 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700120}
121
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300122# Helper functions for parsing a spec.
123# notes: an optional "|SHA1" that may appear in the format is stripped
124# early from the spec in the parse_file_list function, and
125# should not be present inside the input parameter passed
126# to these functions.
127
128#
129# input: spec in the form of "src[:dst][;args]"
130# output: "src"
131#
132function src_file() {
133 local SPEC="$1"
134 local SPLIT=(${SPEC//:/ })
135 local ARGS="$(target_args ${SPEC})"
136 # Regardless of there being a ":" delimiter or not in the spec,
137 # the source file is always either the first, or the only entry.
138 local SRC="${SPLIT[0]}"
139 # Remove target_args suffix, if present
140 echo "${SRC%;${ARGS}}"
141}
142
Steve Kondik5bd66602016-07-15 10:39:58 -0700143#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300144# input: spec in the form of "src[:dst][;args]"
145# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700146#
147function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800148 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300149 local SPLIT=(${SPEC//:/ })
150 local ARGS="$(target_args ${SPEC})"
151 local DST=
152 case ${#SPLIT[@]} in
153 1)
154 # The spec doesn't have a : delimiter
155 DST="${SPLIT[0]}"
156 ;;
157 *)
158 # The spec actually has a src:dst format
159 DST="${SPLIT[1]}"
160 ;;
161 esac
162 # Remove target_args suffix, if present
163 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700164}
165
166#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300167# input: spec in the form of "src[:dst][;args]"
168# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700169#
170function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300171 local SPEC="$1"
172 local SPLIT=(${SPEC//;/ })
173 local ARGS=
174 case ${#SPLIT[@]} in
175 1)
176 # No ";" delimiter in the spec.
177 ;;
178 *)
179 # The "args" are whatever comes after the ";" character.
180 # Basically the spec stripped of whatever is to the left of ";".
181 ARGS="${SPEC#${SPLIT[0]};}"
182 ;;
183 esac
184 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700185}
186
187#
188# prefix_match:
189#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300190# input:
191# - $1: prefix
192# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
193# output:
194# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700195#
196function prefix_match() {
197 local PREFIX="$1"
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100198 local NEW_ARRAY=()
Vladimir Oltean7220f362018-04-02 22:37:09 +0300199 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
200 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700201 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300202 local ARGS=$(target_args "$LINE")
SamarV-1215556e2d2024-03-19 08:50:02 +0530203 if [[ -z "${ARGS}" || "${ARGS}" =~ 'SYMLINK' ]]; then
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100204 NEW_ARRAY+=("${FILE#$PREFIX}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300205 else
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100206 NEW_ARRAY+=("${FILE#$PREFIX};${ARGS}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300207 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700208 fi
209 done
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100210 printf '%s\n' "${NEW_ARRAY[@]}" | LC_ALL=C sort
Steve Kondik5bd66602016-07-15 10:39:58 -0700211}
212
213#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400214# prefix_match_file:
215#
216# $1: the prefix to match on
217# $2: the file to match the prefix for
218#
219# Internal function which returns true if a filename contains the
220# specified prefix.
221#
222function prefix_match_file() {
223 local PREFIX="$1"
224 local FILE="$2"
225 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
226 return 0
227 else
228 return 1
229 fi
230}
231
232#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700233# suffix_match_file:
234#
235# $1: the suffix to match on
236# $2: the file to match the suffix for
237#
238# Internal function which returns true if a filename contains the
239# specified suffix.
240#
241function suffix_match_file() {
242 local SUFFIX="$1"
243 local FILE="$2"
244 if [[ "$FILE" = *"$SUFFIX" ]]; then
245 return 0
246 else
247 return 1
248 fi
249}
250
251#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400252# truncate_file
253#
254# $1: the filename to truncate
255# $2: the argument to output the truncated filename to
256#
257# Internal function which truncates a filename by removing the first dir
258# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
259#
260function truncate_file() {
261 local FILE="$1"
262 RETURN_FILE="$2"
263 local FIND="${FILE%%/*}"
264 local LOCATION="${#FIND}+1"
265 echo ${FILE:$LOCATION}
266}
267
268#
Steve Kondik5bd66602016-07-15 10:39:58 -0700269# write_product_copy_files:
270#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400271# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400272#
Steve Kondik5bd66602016-07-15 10:39:58 -0700273# Creates the PRODUCT_COPY_FILES section in the product makefile for all
274# items in the list which do not start with a dash (-).
275#
276function write_product_copy_files() {
277 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
278 local TARGET=
279 local FILE=
280 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400281 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700282
283 if [ "$COUNT" -eq "0" ]; then
284 return 0
285 fi
286
287 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
288 for (( i=1; i<COUNT+1; i++ )); do
289 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
290 LINEEND=" \\"
291 if [ "$i" -eq "$COUNT" ]; then
292 LINEEND=""
293 fi
294
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300295 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400296 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400297 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400298 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400299 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400300 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400301 local OUTTARGET=$(truncate_file $TARGET)
302 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
303 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200304 elif prefix_match_file "system_ext/" $TARGET ; then
305 local OUTTARGET=$(truncate_file $TARGET)
306 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
307 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
308 elif prefix_match_file "system/system_ext/" $TARGET ; then
309 local OUTTARGET=$(truncate_file $TARGET)
310 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
311 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400312 elif prefix_match_file "odm/" $TARGET ; then
313 local OUTTARGET=$(truncate_file $TARGET)
314 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
315 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400316 elif prefix_match_file "vendor/odm/" $TARGET ; then
317 local OUTTARGET=$(truncate_file $TARGET)
318 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
319 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
320 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
321 local OUTTARGET=$(truncate_file $TARGET)
322 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
323 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
324 elif prefix_match_file "vendor/" $TARGET ; then
325 local OUTTARGET=$(truncate_file $TARGET)
326 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
327 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Alexander Koskovich44c8fac2022-01-22 22:27:29 -0700328 elif prefix_match_file "vendor_dlkm/" $TARGET ; then
329 local OUTTARGET=$(truncate_file $TARGET)
330 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_DLKM)/%s%s\n' \
331 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400332 elif prefix_match_file "system/vendor/" $TARGET ; then
333 local OUTTARGET=$(truncate_file $TARGET)
334 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
335 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400336 elif prefix_match_file "system/" $TARGET ; then
337 local OUTTARGET=$(truncate_file $TARGET)
338 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
339 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400340 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400341 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400342 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
343 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700344 done
345 return 0
346}
347
348#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700349# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700350#
351# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200352# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700353# $3: type-specific extra flags
354# $4: Name of the array holding the target list
355#
356# Internal function which writes out the BUILD_PREBUILT stanzas
357# for all modules in the list. This is called by write_product_packages
358# after the modules are categorized.
359#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700360function write_blueprint_packages() {
361
362 local CLASS="$1"
363 local PARTITION="$2"
364 local EXTRA="$3"
365
366 # Yes, this is a horrible hack - we create a new array using indirection
367 local ARR_NAME="$4[@]"
368 local FILELIST=("${!ARR_NAME}")
369
370 local FILE=
371 local ARGS=
372 local BASENAME=
373 local EXTENSION=
374 local PKGNAME=
375 local SRC=
TheStrix6e24acc2020-04-10 18:20:19 +0530376 local OVERRIDEPKG=
SamarV-1217e6b4082024-02-26 14:39:34 +0530377 local REQUIREDPKG=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700378
379 for P in "${FILELIST[@]}"; do
380 FILE=$(target_file "$P")
381 ARGS=$(target_args "$P")
382
383 BASENAME=$(basename "$FILE")
384 DIRNAME=$(dirname "$FILE")
385 EXTENSION=${BASENAME##*.}
386 PKGNAME=${BASENAME%.*}
387
388 # Add to final package list
389 PACKAGE_LIST+=("$PKGNAME")
390
391 SRC="proprietary"
392 if [ "$PARTITION" = "system" ]; then
393 SRC+="/system"
394 elif [ "$PARTITION" = "vendor" ]; then
395 SRC+="/vendor"
396 elif [ "$PARTITION" = "product" ]; then
397 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200398 elif [ "$PARTITION" = "system_ext" ]; then
399 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700400 elif [ "$PARTITION" = "odm" ]; then
401 SRC+="/odm"
402 fi
403
404 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
405 printf 'cc_prebuilt_library_shared {\n'
406 printf '\tname: "%s",\n' "$PKGNAME"
407 printf '\towner: "%s",\n' "$VENDOR"
408 printf '\tstrip: {\n'
409 printf '\t\tnone: true,\n'
410 printf '\t},\n'
411 printf '\ttarget: {\n'
412 if [ "$EXTRA" = "both" ]; then
413 printf '\t\tandroid_arm: {\n'
414 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
415 printf '\t\t},\n'
416 printf '\t\tandroid_arm64: {\n'
417 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
418 printf '\t\t},\n'
419 elif [ "$EXTRA" = "64" ]; then
420 printf '\t\tandroid_arm64: {\n'
421 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
422 printf '\t\t},\n'
423 else
424 printf '\t\tandroid_arm: {\n'
425 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
426 printf '\t\t},\n'
427 fi
428 printf '\t},\n'
429 if [ "$EXTRA" != "none" ]; then
430 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
431 fi
dianlujitao848101c2020-09-12 00:15:13 +0800432 printf '\tcheck_elf_files: false,\n'
Chirayu Desaia7741012021-12-04 07:17:28 +0530433 elif [ "$CLASS" = "APEX" ]; then
434 printf 'prebuilt_apex {\n'
435 printf '\tname: "%s",\n' "$PKGNAME"
436 printf '\towner: "%s",\n' "$VENDOR"
437 SRC="$SRC/apex"
438 printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
439 printf '\tfilename: "%s",\n' "$FILE"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700440 elif [ "$CLASS" = "APPS" ]; then
441 printf 'android_app_import {\n'
442 printf '\tname: "%s",\n' "$PKGNAME"
443 printf '\towner: "%s",\n' "$VENDOR"
444 if [ "$EXTRA" = "priv-app" ]; then
445 SRC="$SRC/priv-app"
446 else
447 SRC="$SRC/app"
448 fi
449 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
TheStrix6e24acc2020-04-10 18:20:19 +0530450 ARGS=(${ARGS//;/ })
LuK1337508e85f2021-08-23 18:18:57 +0200451 USE_PLATFORM_CERTIFICATE="true"
452 for ARG in "${ARGS[@]}"; do
453 if [ "$ARG" = "PRESIGNED" ]; then
454 USE_PLATFORM_CERTIFICATE="false"
Michael Bestasab47e912024-03-06 13:32:05 +0200455 printf '\tpreprocessed: true,\n'
LuK1337508e85f2021-08-23 18:18:57 +0200456 printf '\tpresigned: true,\n'
457 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
458 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200459 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200460 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
SamarV-1217e6b4082024-02-26 14:39:34 +0530461 elif [[ "$ARG" =~ "REQUIRED" ]]; then
462 REQUIREDPKG=${ARG#*=}
463 REQUIRED_PACKAGES_LIST+="$REQUIREDPKG,"
464 printf '\trequired: ["%s"],\n' "${REQUIREDPKG//,/\", \"}"
SamarV-1215556e2d2024-03-19 08:50:02 +0530465 elif [[ "$ARG" =~ "SYMLINK" ]]; then
466 continue
LuK1337508e85f2021-08-23 18:18:57 +0200467 elif [ ! -z "$ARG" ]; then
468 USE_PLATFORM_CERTIFICATE="false"
469 printf '\tcertificate: "%s",\n' "$ARG"
470 fi
471 done
472 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700473 printf '\tcertificate: "platform",\n'
474 fi
475 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
476 printf 'dex_import {\n'
477 printf '\tname: "%s",\n' "$PKGNAME"
478 printf '\towner: "%s",\n' "$VENDOR"
479 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
480 elif [ "$CLASS" = "ETC" ]; then
481 if [ "$EXTENSION" = "xml" ]; then
482 printf 'prebuilt_etc_xml {\n'
483 else
484 printf 'prebuilt_etc {\n'
485 fi
486 printf '\tname: "%s",\n' "$PKGNAME"
487 printf '\towner: "%s",\n' "$VENDOR"
488 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200489 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700490 elif [ "$CLASS" = "EXECUTABLES" ]; then
491 if [ "$EXTENSION" = "sh" ]; then
492 printf 'sh_binary {\n'
493 else
494 printf 'cc_prebuilt_binary {\n'
495 fi
496 printf '\tname: "%s",\n' "$PKGNAME"
497 printf '\towner: "%s",\n' "$VENDOR"
Michael Bestasbda30202020-12-28 04:44:52 +0200498 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200499 if [ "$EXTENSION" != "sh" ]; then
500 printf '\tcheck_elf_files: false,\n'
501 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700502 unset EXTENSION
503 else
504 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
505 fi
506 if [ "$CLASS" = "APPS" ]; then
507 printf '\tdex_preopt: {\n'
508 printf '\t\tenabled: false,\n'
509 printf '\t},\n'
Jyotiraditya Panda45f50af2024-02-19 05:35:33 +0900510 if [ "$DIRNAME" != "." ] && [[ "$DIRNAME" == */* ]]; then
511 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
512 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700513 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200514 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700515 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200516 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700517 fi
518 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200519 if [ "$CLASS" = "ETC" ] ; then
520 if [ "$DIRNAME" != "." ]; then
521 printf '\tsub_dir: "%s",\n' "$DIRNAME"
522 fi
523 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700524 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
525 printf '\tprefer: true,\n'
526 fi
527 if [ "$EXTRA" = "priv-app" ]; then
528 printf '\tprivileged: true,\n'
529 fi
530 if [ "$PARTITION" = "vendor" ]; then
531 printf '\tsoc_specific: true,\n'
532 elif [ "$PARTITION" = "product" ]; then
533 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200534 elif [ "$PARTITION" = "system_ext" ]; then
535 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700536 elif [ "$PARTITION" = "odm" ]; then
537 printf '\tdevice_specific: true,\n'
538 fi
539 printf '}\n\n'
540 done
541}
542
543#
Steve Kondik5bd66602016-07-15 10:39:58 -0700544# write_product_packages:
545#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700546# This function will create prebuilt entries in the
547# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700548# product makefile for all files in the blob list which
549# start with a single dash (-) character.
550#
551function write_product_packages() {
552 PACKAGE_LIST=()
553
Chenyang Zhongc487f382022-02-10 21:40:41 -0500554 # Sort the package list for comm
555 PRODUCT_PACKAGES_LIST=($( printf '%s\n' "${PRODUCT_PACKAGES_LIST[@]}" | LC_ALL=C sort))
556
Steve Kondik5bd66602016-07-15 10:39:58 -0700557 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
558
559 if [ "$COUNT" = "0" ]; then
560 return 0
561 fi
562
563 # Figure out what's 32-bit, what's 64-bit, and what's multilib
564 # I really should not be doing this in bash due to shitty array passing :(
565 local T_LIB32=( $(prefix_match "lib/") )
566 local T_LIB64=( $(prefix_match "lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500567 local MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
568 local LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
569 local LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik5bd66602016-07-15 10:39:58 -0700570
571 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700572 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700573 fi
574 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700575 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700576 fi
577 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700578 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700579 fi
580
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400581 local T_S_LIB32=( $(prefix_match "system/lib/") )
582 local T_S_LIB64=( $(prefix_match "system/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500583 local S_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
584 local S_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
585 local S_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400586
587 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700588 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400589 fi
590 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700591 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400592 fi
593 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700594 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400595 fi
596
Steve Kondik5bd66602016-07-15 10:39:58 -0700597 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
598 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500599 local V_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
600 local V_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
601 local V_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik5bd66602016-07-15 10:39:58 -0700602
603 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700604 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700605 fi
606 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700607 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700608 fi
609 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700610 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500611 fi
612
613 local T_P_LIB32=( $(prefix_match "product/lib/") )
614 local T_P_LIB64=( $(prefix_match "product/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500615 local P_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
616 local P_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
617 local P_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
razorlovesa0d296b2019-07-29 02:21:34 -0500618
619 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700620 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500621 fi
622 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700623 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500624 fi
625 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700626 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700627 fi
628
Luca Stefani776be462020-09-09 15:53:58 +0200629 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
630 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500631 local SE_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
632 local SE_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
633 local SE_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
Luca Stefani776be462020-09-09 15:53:58 +0200634
635 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
636 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
637 fi
638 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
639 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
640 fi
641 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
642 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
643 fi
644
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700645 local T_O_LIB32=( $(prefix_match "odm/lib/") )
646 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500647 local O_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
648 local O_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
649 local O_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700650
651 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700652 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700653 fi
654 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700655 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700656 fi
657 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700658 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700659 fi
660
Chirayu Desaia7741012021-12-04 07:17:28 +0530661 # APEX
662 local APEX=( $(prefix_match "apex/") )
663 if [ "${#APEX[@]}" -gt "0" ]; then
664 write_blueprint_packages "APEX" "" "" "APEX" >> "$ANDROIDBP"
665 fi
666 local S_APEX=( $(prefix_match "system/apex/") )
667 if [ "${#S_APEX[@]}" -gt "0" ]; then
668 write_blueprint_packages "APEX" "system" "" "S_APEX" >> "$ANDROIDBP"
669 fi
670 local V_APEX=( $(prefix_match "vendor/apex/") )
671 if [ "${#V_APEX[@]}" -gt "0" ]; then
672 write_blueprint_packages "APEX" "vendor" "" "V_APEX" >> "$ANDROIDBP"
673 fi
674 local SE_APEX=( $(prefix_match "system_ext/apex/") )
675 if [ "${#SE_APEX[@]}" -gt "0" ]; then
676 write_blueprint_packages "APEX" "system_ext" "" "SE_APEX" >> "$ANDROIDBP"
677 fi
678
Steve Kondik5bd66602016-07-15 10:39:58 -0700679 # Apps
680 local APPS=( $(prefix_match "app/") )
681 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100682 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700683 fi
684 local PRIV_APPS=( $(prefix_match "priv-app/") )
685 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100686 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700687 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400688 local S_APPS=( $(prefix_match "system/app/") )
689 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100690 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400691 fi
692 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
693 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100694 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400695 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700696 local V_APPS=( $(prefix_match "vendor/app/") )
697 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100698 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700699 fi
700 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
701 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100702 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500703 fi
704 local P_APPS=( $(prefix_match "product/app/") )
705 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100706 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500707 fi
708 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
709 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100710 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700711 fi
Luca Stefani776be462020-09-09 15:53:58 +0200712 local SE_APPS=( $(prefix_match "system_ext/app/") )
713 if [ "${#SE_APPS[@]}" -gt "0" ]; then
714 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
715 fi
716 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
717 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
718 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
719 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700720 local O_APPS=( $(prefix_match "odm/app/") )
721 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100722 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700723 fi
724 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
725 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100726 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700727 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700728
729 # Framework
730 local FRAMEWORK=( $(prefix_match "framework/") )
731 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700732 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700733 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400734 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
735 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700736 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400737 fi
Christian Oder974b5902017-10-08 23:15:52 +0200738 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200739 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700740 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500741 fi
742 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
743 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700744 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200745 fi
Luca Stefani776be462020-09-09 15:53:58 +0200746 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700747 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200748 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
749 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700750 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
751 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700752 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700753 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700754
755 # Etc
756 local ETC=( $(prefix_match "etc/") )
757 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700758 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700759 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400760 local S_ETC=( $(prefix_match "system/etc/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200761 if [ "${#S_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700762 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400763 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700764 local V_ETC=( $(prefix_match "vendor/etc/") )
765 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700766 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500767 fi
768 local P_ETC=( $(prefix_match "product/etc/") )
769 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700770 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700771 fi
Luca Stefani776be462020-09-09 15:53:58 +0200772 local SE_ETC=( $(prefix_match "system_ext/etc/") )
773 if [ "${#SE_ETC[@]}" -gt "0" ]; then
774 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
775 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700776 local O_ETC=( $(prefix_match "odm/etc/") )
777 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700778 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700779 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700780
781 # Executables
782 local BIN=( $(prefix_match "bin/") )
783 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700784 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700785 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400786 local S_BIN=( $(prefix_match "system/bin/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200787 if [ "${#S_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700788 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400789 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700790 local V_BIN=( $(prefix_match "vendor/bin/") )
791 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700792 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500793 fi
794 local P_BIN=( $(prefix_match "product/bin/") )
795 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700796 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700797 fi
Luca Stefani776be462020-09-09 15:53:58 +0200798 local SE_BIN=( $(prefix_match "system_ext/bin/") )
799 if [ "${#SE_BIN[@]}" -gt "0" ]; then
800 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
801 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700802 local O_BIN=( $(prefix_match "odm/bin/") )
803 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700804 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700805 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700806
SamarV-1215556e2d2024-03-19 08:50:02 +0530807 write_package_definition "${PACKAGE_LIST[@]}" >> "$PRODUCTMK"
808}
Steve Kondik5bd66602016-07-15 10:39:58 -0700809
SamarV-1215556e2d2024-03-19 08:50:02 +0530810
811#
812# write_symlink_packages:
813#
814# Creates symlink entries in the Android.bp and related PRODUCT_PACKAGES
815# list in the product makefile for all files in the blob list which has
816# SYMLINK argument.
817#
818function write_symlink_packages() {
819 local FILE=
820 local ARGS=
821 local ARCH=
822 local BASENAME=
823 local PKGNAME=
824 local PREFIX=
825 local SYMLINK_BASENAME=
826 local SYMLINK_PACKAGES=()
827
828 # Sort the symlinks list for comm
829 PRODUCT_SYMLINKS_LIST=($( printf '%s\n' "${PRODUCT_SYMLINKS_LIST[@]}" | LC_ALL=C sort))
830
831 local COUNT=${#PRODUCT_SYMLINKS_LIST[@]}
832
833 if [ "$COUNT" = "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700834 return 0
835 fi
836
SamarV-1215556e2d2024-03-19 08:50:02 +0530837 for LINE in "${PRODUCT_SYMLINKS_LIST[@]}"; do
Bruno Martinsbe7f3cb2024-06-23 15:54:02 +0100838 FILE=$(target_file "$LINE")
Bruno Martinsf96fd122024-03-28 14:31:02 +0000839 if [[ "$LINE" =~ '/lib64/' || "$LINE" =~ '/lib/arm64/' ]]; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530840 ARCH="64"
Bruno Martinsf96fd122024-03-28 14:31:02 +0000841 elif [[ "$LINE" =~ '/lib/' ]]; then
842 ARCH="32"
Steve Kondik5bd66602016-07-15 10:39:58 -0700843 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530844 BASENAME=$(basename "$FILE")
845 ARGS=$(target_args "$LINE")
846 ARGS=(${ARGS//;/ })
847 for ARG in "${ARGS[@]}"; do
848 if [[ "$ARG" =~ "SYMLINK" ]]; then
849 SYMLINKS=${ARG#*=}
850 SYMLINKS=(${SYMLINKS//,/ })
851 for SYMLINK in "${SYMLINKS[@]}"; do
852 SYMLINK_BASENAME=$(basename "$SYMLINK")
Bruno Martinsad05f512024-06-28 00:34:33 +0100853 PKGNAME="${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}"
854 if [[ "${SYMLINK_PACKAGES[@]}" =~ "$PKGNAME" ]]; then
855 PKGNAME+="_$(grep -o "$PKGNAME" <<< ${SYMLINK_PACKAGES[*]} | wc -l)"
856 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530857 {
858 printf 'install_symlink {\n'
859 printf '\tname: "%s",\n' "$PKGNAME"
Mashopy06100c92024-04-23 21:52:04 +0200860 if prefix_match_file "vendor/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530861 PREFIX='vendor/'
862 printf '\tsoc_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200863 elif prefix_match_file "product/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530864 PREFIX='product/'
865 printf '\tproduct_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200866 elif prefix_match_file "system_ext/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530867 PREFIX='system_ext/'
868 printf '\tsystem_ext_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200869 elif prefix_match_file "odm/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530870 PREFIX='odm/'
871 printf '\tdevice_specific: true,\n'
872 fi
873 printf '\tinstalled_location: "%s",\n' "${SYMLINK#"$PREFIX"}"
874 printf '\tsymlink_target: "/%s",\n' "$FILE"
875 printf '}\n\n'
876 } >> "$ANDROIDBP"
877 SYMLINK_PACKAGES+=("$PKGNAME")
878 done
879 fi
880 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700881 done
SamarV-1215556e2d2024-03-19 08:50:02 +0530882
883 write_package_definition "${SYMLINK_PACKAGES[@]}" >> "$PRODUCTMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700884}
885
886#
Michael Bestasfe71eb32023-06-11 18:59:10 +0300887# write_single_product_copy_files:
888#
889# $1: the file to be copied
890#
891# Creates a PRODUCT_COPY_FILES section in the product makefile for the
892# item provided in $1.
893#
894function write_single_product_copy_files() {
895 local FILE="$1"
896 if [ -z "$FILE" ]; then
897 echo "A file must be provided to write_single_product_copy_files()!"
898 exit 1
899 fi
900
901 local TARGET=$(target_file "$FILE")
902 local OUTTARGET=$(truncate_file $TARGET)
903
904 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
905 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s\n' \
906 "$OUTDIR" "$TARGET" "$OUTTARGET" >> "$PRODUCTMK"
907}
908
909#
910# write_single_product_packages:
911#
912# $1: the package to be built
913#
914# Creates a PRODUCT_PACKAGES section in the product makefile for the
915# item provided in $1.
916#
917function write_single_product_packages() {
918 local PACKAGE="$1"
919 if [ -z "$PACKAGE" ]; then
920 echo "A package must be provided to write_single_product_packages()!"
921 exit 1
922 fi
923
924 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
925 printf ' %s%s\n' "$PACKAGE" >> "$PRODUCTMK"
926}
927
928#
Michael Bestas431a8002023-06-11 20:04:45 +0300929# write_rro_androidmanifest:
930#
931# $2: target package for the RRO overlay
932#
933# Creates an AndroidManifest.xml for an RRO overlay.
934#
935function write_rro_androidmanifest() {
936 local TARGET_PACKAGE="$1"
937
938 cat << EOF
939<manifest xmlns:android="http://schemas.android.com/apk/res/android"
940 package="$TARGET_PACKAGE.vendor"
941 android:versionCode="1"
942 android:versionName="1.0">
943 <application android:hasCode="false" />
944 <overlay
945 android:targetPackage="$TARGET_PACKAGE"
946 android:isStatic="true"
947 android:priority="0"/>
948</manifest>
949EOF
950}
951
952#
953# write_rro_blueprint:
954#
955# $1: package name for the RRO overlay
956# $2: target partition for the RRO overlay
957#
958# Creates an Android.bp for an RRO overlay.
959#
960function write_rro_blueprint() {
961 local PKGNAME="$1"
962 local PARTITION="$2"
963
964 printf 'runtime_resource_overlay {\n'
965 printf '\tname: "%s",\n' "$PKGNAME"
966 printf '\ttheme: "%s",\n' "$PKGNAME"
967 printf '\tsdk_version: "%s",\n' "current"
968 printf '\taaptflags: ["%s"],\n' "--keep-raw-values"
969
970 if [ "$PARTITION" = "vendor" ]; then
971 printf '\tsoc_specific: true,\n'
972 elif [ "$PARTITION" = "product" ]; then
973 printf '\tproduct_specific: true,\n'
974 elif [ "$PARTITION" = "system_ext" ]; then
975 printf '\tsystem_ext_specific: true,\n'
976 elif [ "$PARTITION" = "odm" ]; then
977 printf '\tdevice_specific: true,\n'
978 fi
979 printf '}\n'
980}
981
982#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700983# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700984#
985# $1: file which will be written to
986#
Michael Bestasa2934df2020-12-19 03:50:32 +0200987# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -0700988# note that this is not an append operation, and should
989# be executed first!
990#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700991function write_blueprint_header() {
992 if [ -f $1 ]; then
993 rm $1
994 fi
995
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700996 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
997
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700998 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200999// Automatically generated file. DO NOT MODIFY
1000//
1001// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001002
1003EOF
1004}
1005
1006#
1007# write_makefile_header:
1008#
1009# $1: file which will be written to
1010#
Michael Bestasa2934df2020-12-19 03:50:32 +02001011# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001012# note that this is not an append operation, and should
1013# be executed first!
1014#
1015function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -05001016 if [ -f $1 ]; then
1017 rm $1
1018 fi
1019
Steve Kondik5bd66602016-07-15 10:39:58 -07001020 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1021
Jake Whatley9843b322017-01-25 21:49:16 -05001022 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001023# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -07001024#
Steve Kondik5bd66602016-07-15 10:39:58 -07001025# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1026
1027EOF
1028}
1029
1030#
Michael Bestas431a8002023-06-11 20:04:45 +03001031# write_xml_header:
1032#
1033# $1: file which will be written to
1034#
1035# writes out the warning message regarding manual file modifications.
1036# note that this is not an append operation, and should
1037# be executed first!
1038#
1039function write_xml_header() {
1040 if [ -f $1 ]; then
1041 rm $1
1042 fi
1043
1044 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1045 [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
1046
1047 cat << EOF >> $1
1048<?xml version="1.0" encoding="utf-8"?>
1049<!--
1050 Automatically generated file. DO NOT MODIFY
1051
1052 This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1053-->
1054EOF
1055}
1056
1057#
1058# write_rro_package:
1059#
1060# $1: the RRO package name
1061# $2: the RRO target package
1062# $3: the partition for the RRO overlay
1063#
1064# Generates the file structure for an RRO overlay.
1065#
1066function write_rro_package() {
1067 local PKGNAME="$1"
1068 if [ -z "$PKGNAME" ]; then
1069 echo "A package name must be provided to write_rro_package()!"
1070 exit 1
1071 fi
1072
1073 local TARGET_PACKAGE="$2"
1074 if [ -z "$TARGET_PACKAGE" ]; then
1075 echo "A target package must be provided to write_rro_package()!"
1076 exit 1
1077 fi
1078
1079 local PARTITION="$3"
1080 if [ -z "$PARTITION" ]; then
1081 PARTITION="vendor"
1082 fi
1083
1084 local RROBP="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/Android.bp
1085 local RROMANIFEST="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/AndroidManifest.xml
1086
1087 write_blueprint_header "$RROBP"
1088 write_xml_header "$RROMANIFEST"
1089
1090 write_rro_blueprint "$PKGNAME" "$PARTITION" >> "$RROBP"
1091 write_rro_androidmanifest "$TARGET_PACKAGE" >> "$RROMANIFEST"
1092}
1093
1094#
SamarV-1215556e2d2024-03-19 08:50:02 +05301095# write_package_definition:
1096#
1097# $@: list of packages
1098#
1099# writes out the final PRODUCT_PACKAGES list
1100#
1101function write_package_definition() {
1102 local PACKAGE_LIST=("${@}")
1103 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
1104
1105 if [ "$PACKAGE_COUNT" -eq "0" ]; then
1106 return 0
1107 fi
1108
1109 printf '\n%s\n' "PRODUCT_PACKAGES += \\"
1110 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
SamarV-1217e6b4082024-02-26 14:39:34 +05301111 local SKIP=false
SamarV-1215556e2d2024-03-19 08:50:02 +05301112 local LINEEND=" \\"
1113 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
1114 LINEEND=""
1115 fi
SamarV-1217e6b4082024-02-26 14:39:34 +05301116 for PKG in $(tr "," "\n" <<< "$REQUIRED_PACKAGES_LIST"); do
1117 if [[ $PKG == "${PACKAGE_LIST[$i - 1]}" ]]; then
1118 SKIP=true
1119 break
1120 fi
1121 done
1122 # Skip adding of the package to product makefile if it's in the required list
1123 if [[ $SKIP == false ]]; then
1124 printf ' %s%s\n' "${PACKAGE_LIST[$i - 1]}" "$LINEEND" >> "$PRODUCTMK"
1125 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301126 done
1127}
1128
1129#
Steve Kondik5bd66602016-07-15 10:39:58 -07001130# write_headers:
1131#
1132# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001133# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001134#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001135# Calls write_makefile_header for each of the makefiles and
1136# write_blueprint_header for Android.bp and creates the initial
1137# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001138#
1139function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001140 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001141
1142 GUARD="$2"
1143 if [ -z "$GUARD" ]; then
1144 GUARD="TARGET_DEVICE"
1145 fi
1146
Steve Kondik5bd66602016-07-15 10:39:58 -07001147 cat << EOF >> "$ANDROIDMK"
1148LOCAL_PATH := \$(call my-dir)
1149
1150EOF
1151 if [ "$COMMON" -ne 1 ]; then
1152 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001153ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001154
1155EOF
1156 else
1157 if [ -z "$1" ]; then
1158 echo "Argument with devices to be added to guard must be set!"
1159 exit 1
1160 fi
1161 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001162ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001163
1164EOF
1165 fi
1166
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001167 write_makefile_header "$BOARDMK"
1168 write_makefile_header "$PRODUCTMK"
1169 write_blueprint_header "$ANDROIDBP"
1170
1171 cat << EOF >> "$ANDROIDBP"
1172soong_namespace {
1173}
1174
1175EOF
1176
1177 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1178 cat << EOF >> "$PRODUCTMK"
1179PRODUCT_SOONG_NAMESPACES += \\
1180 vendor/$VENDOR/$DEVICE
1181
1182EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001183}
1184
1185#
1186# write_footers:
1187#
1188# Closes the inital guard and any other finalization tasks. Must
1189# be called as the final step.
1190#
1191function write_footers() {
1192 cat << EOF >> "$ANDROIDMK"
1193endif
1194EOF
1195}
1196
1197# Return success if adb is up and not in recovery
1198function _adb_connected {
1199 {
Jake Whatley9843b322017-01-25 21:49:16 -05001200 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001201 then
1202 return 0
1203 fi
1204 } 2>/dev/null
1205
1206 return 1
1207};
1208
1209#
1210# parse_file_list:
1211#
1212# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001213# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001214#
1215# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1216#
1217function parse_file_list() {
1218 if [ -z "$1" ]; then
1219 echo "An input file is expected!"
1220 exit 1
1221 elif [ ! -f "$1" ]; then
1222 echo "Input file "$1" does not exist!"
1223 exit 1
1224 fi
1225
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001226 if [ -n "$2" ]; then
1227 echo "Using section \"$2\""
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301228 LIST=$EXTRACT_TMP_DIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001229 # Match all lines starting with first line found to start* with '#'
1230 # comment and contain** $2, and ending with first line to be empty*.
1231 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1232 # **the $2 match is case-insensitive
1233 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001234 else
1235 LIST=$1
1236 fi
1237
Steve Kondik5bd66602016-07-15 10:39:58 -07001238 PRODUCT_PACKAGES_LIST=()
1239 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001240 PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +05301241 PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001242 PRODUCT_COPY_FILES_LIST=()
1243 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001244 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001245
1246 while read -r line; do
1247 if [ -z "$line" ]; then continue; fi
1248
1249 # If the line has a pipe delimiter, a sha1 hash should follow.
1250 # This indicates the file should be pinned and not overwritten
1251 # when extracting files.
1252 local SPLIT=(${line//\|/ })
1253 local COUNT=${#SPLIT[@]}
1254 local SPEC=${SPLIT[0]}
1255 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001256 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001257 if [ "$COUNT" -gt "1" ]; then
1258 HASH=${SPLIT[1]}
1259 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001260 if [ "$COUNT" -gt "2" ]; then
1261 FIXUP_HASH=${SPLIT[2]}
1262 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301263 if [[ "$SPEC" =~ 'SYMLINK=' ]]; then
1264 PRODUCT_SYMLINKS_LIST+=("${SPEC#-}")
1265 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001266 # if line starts with a dash, it needs to be packaged
1267 if [[ "$SPEC" =~ ^- ]]; then
1268 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1269 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001270 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Chirayu Desaia7741012021-12-04 07:17:28 +05301271 # if line contains apex, apk, jar or vintf fragment, it needs to be packaged
1272 elif suffix_match_file ".apex" "$(src_file "$SPEC")" || \
1273 suffix_match_file ".apk" "$(src_file "$SPEC")" || \
Michael Bestasea90aef2021-11-15 22:18:04 +02001274 suffix_match_file ".jar" "$(src_file "$SPEC")" || \
1275 [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
1276 PRODUCT_PACKAGES_LIST+=("$SPEC")
1277 PRODUCT_PACKAGES_HASHES+=("$HASH")
1278 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001279 else
1280 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1281 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001282 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001283 fi
1284
Chirayu Desaif1a21302022-09-13 00:29:33 +05301285 done < <(grep -v -E '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001286}
1287
1288#
1289# write_makefiles:
1290#
1291# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001292# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001293#
SamarV-1215556e2d2024-03-19 08:50:02 +05301294# Calls write_product_copy_files, write_product_packages and
1295# lastly write_symlink_packages on the given file and appends
1296# to the Android.bp as well as the product makefile.
Steve Kondik5bd66602016-07-15 10:39:58 -07001297#
1298function write_makefiles() {
1299 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001300 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001301 write_product_packages
SamarV-1215556e2d2024-03-19 08:50:02 +05301302 write_symlink_packages
Steve Kondik5bd66602016-07-15 10:39:58 -07001303}
1304
1305#
1306# append_firmware_calls_to_makefiles:
1307#
1308# Appends to Android.mk the calls to all images present in radio folder
1309# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1310#
1311function append_firmware_calls_to_makefiles() {
1312 cat << EOF >> "$ANDROIDMK"
1313ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1314
1315RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1316\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1317 \$(call add-radio-file,radio/\$(f)))
1318\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1319
1320endif
1321
1322EOF
1323}
1324
1325#
1326# get_file:
1327#
1328# $1: input file
1329# $2: target file/folder
1330# $3: source of the file (can be "adb" or a local folder)
1331#
1332# Silently extracts the input file to defined target
1333# Returns success if file can be pulled from the device or found locally
1334#
1335function get_file() {
1336 local SRC="$3"
1337
1338 if [ "$SRC" = "adb" ]; then
1339 # try to pull
LuK13370f7f0d12022-08-19 21:49:56 +02001340 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1341 adb pull "${1#/system}" "$2" >/dev/null 2>&1 && return 0
1342 adb pull "system/$1" "$2" >/dev/null 2>&1 && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001343
1344 return 1
1345 else
1346 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001347 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1348 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001349 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001350
LuK1337dbb77cc2023-12-04 19:03:10 +01001351 # try /vendor/odm for devices without /odm partition
1352 [[ "$1" == /system/odm/* ]] && cp -r "$SRC/vendor/${1#/system}" "$2" 2>/dev/null && return 0
1353
Steve Kondik5bd66602016-07-15 10:39:58 -07001354 return 1
1355 fi
1356};
1357
1358#
1359# oat2dex:
1360#
1361# $1: extracted apk|jar (to check if deodex is required)
1362# $2: odexed apk|jar to deodex
1363# $3: source of the odexed apk|jar
1364#
1365# Convert apk|jar .odex in the corresposing classes.dex
1366#
1367function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001368 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001369 local OEM_TARGET="$2"
1370 local SRC="$3"
1371 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001372 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001373
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001374 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1375 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1376 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001377 fi
1378
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001379 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001380 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001381 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001382
codeworkx85eda752018-09-23 12:36:57 +02001383 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001384 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001385 fi
1386
Steve Kondik5bd66602016-07-15 10:39:58 -07001387 # Extract existing boot.oats to the temp folder
1388 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001389 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001390 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301391 mkdir -p "$EXTRACT_TMP_DIR/system/framework/$ARCH"
1392 if get_file "/system/framework/$ARCH" "$EXTRACT_TMP_DIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001393 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001394 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301395 rmdir "$EXTRACT_TMP_DIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001396 fi
1397 done
1398 fi
1399
1400 if [ -z "$ARCHES" ]; then
1401 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1402 fi
1403
theimpulson9a911af2019-08-14 03:25:12 +00001404 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001405 return;
1406 fi
1407
theimpulson9a911af2019-08-14 03:25:12 +00001408 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001409 return 0 # target apk|jar is already odexed, return
1410 fi
1411
1412 for ARCH in $ARCHES; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301413 BOOTOAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001414
Joe Maplesfb3941c2018-01-05 14:51:33 -05001415 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1416 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001417
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301418 if get_file "$OAT" "$EXTRACT_TMP_DIR" "$SRC"; then
1419 if get_file "$VDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1420 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$VDEX")" > /dev/null
1421 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${OEM_TARGET%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001422 for CLASS in $CLASSES; do
1423 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1424 # Check if we have to deal with CompactDex
1425 if [[ "$CLASS" == *.cdex ]]; then
1426 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301427 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001428 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301429 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001430 fi
1431 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001432 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301433 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$EXTRACT_TMP_DIR/$(basename "$OAT")"
1434 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001435 fi
theimpulson9a911af2019-08-14 03:25:12 +00001436 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301437 JAROAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001438 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001439 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001440 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001441 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001442 # try to extract classes.dex from boot.vdex for frameworks jars
1443 # fallback to boot.oat if vdex is not available
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301444 if get_file "$JARVDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1445 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$JARVDEX")" > /dev/null
1446 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${JARVDEX%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001447 for CLASS in $CLASSES; do
1448 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1449 # Check if we have to deal with CompactDex
1450 if [[ "$CLASS" == *.cdex ]]; then
1451 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301452 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001453 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301454 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001455 fi
1456 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001457 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301458 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$JAROAT/$OEM_TARGET"
1459 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001460 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001461 else
1462 continue
1463 fi
1464
Steve Kondik5bd66602016-07-15 10:39:58 -07001465 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001466
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301467 rm -rf "$EXTRACT_TMP_DIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001468}
1469
1470#
1471# init_adb_connection:
1472#
1473# Starts adb server and waits for the device
1474#
1475function init_adb_connection() {
1476 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1477 if ! _adb_connected; then
1478 echo "No device is online. Waiting for one..."
1479 echo "Please connect USB and/or enable USB debugging"
1480 until _adb_connected; do
1481 sleep 1
1482 done
1483 echo "Device Found."
1484 fi
1485
1486 # Retrieve IP and PORT info if we're using a TCP connection
Chirayu Desaif1a21302022-09-13 00:29:33 +05301487 TCPIPPORT=$(adb devices | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
Steve Kondik5bd66602016-07-15 10:39:58 -07001488 | head -1 | awk '{print $1}')
1489 adb root &> /dev/null
1490 sleep 0.3
1491 if [ -n "$TCPIPPORT" ]; then
1492 # adb root just killed our connection
1493 # so reconnect...
1494 adb connect "$TCPIPPORT"
1495 fi
1496 adb wait-for-device &> /dev/null
1497 sleep 0.3
1498}
1499
1500#
1501# fix_xml:
1502#
1503# $1: xml file to fix
1504#
1505function fix_xml() {
1506 local XML="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301507 local TEMP_XML="$EXTRACT_TMP_DIR/`basename "$XML"`.temp"
Steve Kondik5bd66602016-07-15 10:39:58 -07001508
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001509 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1510 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001511
1512 mv "$TEMP_XML" "$XML"
1513}
1514
Vladimir Olteande985fe2019-01-17 03:07:34 +02001515function get_hash() {
1516 local FILE="$1"
1517
1518 if [ "$(uname)" == "Darwin" ]; then
1519 shasum "${FILE}" | awk '{print $1}'
1520 else
1521 sha1sum "${FILE}" | awk '{print $1}'
1522 fi
1523}
1524
Vladimir Olteana7d20492019-01-17 03:05:52 +02001525function print_spec() {
1526 local SPEC_PRODUCT_PACKAGE="$1"
1527 local SPEC_SRC_FILE="$2"
1528 local SPEC_DST_FILE="$3"
1529 local SPEC_ARGS="$4"
1530 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001531 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001532
1533 local PRODUCT_PACKAGE=""
1534 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1535 PRODUCT_PACKAGE="-"
1536 fi
1537 local SRC=""
1538 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1539 SRC="${SPEC_SRC_FILE}:"
1540 fi
1541 local DST=""
1542 if [ ! -z "${SPEC_DST_FILE}" ]; then
1543 DST="${SPEC_DST_FILE}"
1544 fi
1545 local ARGS=""
1546 if [ ! -z "${SPEC_ARGS}" ]; then
1547 ARGS=";${SPEC_ARGS}"
1548 fi
1549 local HASH=""
1550 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1551 HASH="|${SPEC_HASH}"
1552 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001553 local FIXUP_HASH=""
1554 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1555 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1556 fi
1557 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1558}
1559
1560# To be overridden by device-level extract-files.sh
1561# Parameters:
1562# $1: spec name of a blob. Can be used for filtering.
1563# If the spec is "src:dest", then $1 is "dest".
1564# If the spec is "src", then $1 is "src".
1565# $2: path to blob file. Can be used for fixups.
1566#
1567function blob_fixup() {
1568 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001569}
1570
Steve Kondik5bd66602016-07-15 10:39:58 -07001571#
1572# extract:
1573#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001574# Positional parameters:
1575# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001576# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001577# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1578#
1579# Non-positional parameters (coming after $2):
1580# --section: preferred way of selecting the portion to parse and extract from
1581# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001582# --kang: if present, this option will activate the printing of hashes for the
1583# extracted blobs. Useful with --section for subsequent pinning of
1584# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001585#
1586function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001587 # Consume positional parameters
1588 local PROPRIETARY_FILES_TXT="$1"; shift
1589 local SRC="$1"; shift
1590 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001591 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001592
1593 # Consume optional, non-positional parameters
1594 while [ "$#" -gt 0 ]; do
1595 case "$1" in
1596 -s|--section)
1597 SECTION="$2"; shift
1598 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001599 -k|--kang)
1600 KANG=true
1601 DISABLE_PINNING=1
1602 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001603 *)
1604 # Backwards-compatibility with the old behavior, where $3, if
1605 # present, denoted an optional positional ${SECTION} argument.
1606 # Users of ${SECTION} are encouraged to migrate from setting it as
1607 # positional $3, to non-positional --section ${SECTION}, the
1608 # reason being that it doesn't scale to have more than 1 optional
1609 # positional argument.
1610 SECTION="$1"
1611 ;;
1612 esac
1613 shift
1614 done
1615
Steve Kondik5bd66602016-07-15 10:39:58 -07001616 if [ -z "$OUTDIR" ]; then
1617 echo "Output dir not set!"
1618 exit 1
1619 fi
1620
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001621 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001622
1623 # Allow failing, so we can try $DEST and/or $FILE
1624 set +e
1625
1626 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1627 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001628 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001629 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001630 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001631 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301632 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001633
1634 if [ "$SRC" = "adb" ]; then
1635 init_adb_connection
1636 fi
1637
Dan Pasanen0cc05012017-03-21 09:06:11 -05001638 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301639 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001640
1641 # Check if we're working with the same zip that was passed last time.
1642 # If so, let's just use what's already extracted.
1643 MD5=`md5sum "$SRC"| awk '{print $1}'`
1644 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1645
1646 if [ "$MD5" != "$OLDMD5" ]; then
1647 rm -rf "$DUMPDIR"
1648 mkdir "$DUMPDIR"
1649 unzip "$SRC" -d "$DUMPDIR"
1650 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1651
1652 # Stop if an A/B OTA zip is detected. We cannot extract these.
1653 if [ -a "$DUMPDIR"/payload.bin ]; then
1654 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1655 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001656 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001657
Luca Stefani776be462020-09-09 15:53:58 +02001658 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001659 do
1660 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001661 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1662 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1663 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1664 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1665 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001666 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1667 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1668 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1669 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1670 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
Chirayu Desai62ed12a2021-11-26 05:47:25 +05301671 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1672 rm "$DUMPDIR"/"$PARTITION".img
dianlujitao85ddca62020-04-21 23:03:20 +08001673 fi
1674 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001675 fi
1676
1677 SRC="$DUMPDIR"
1678 fi
1679
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301680 if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301681 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301682 mkdir -p "$DUMPDIR"
1683
1684 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1685 do
1686 echo "Extracting "$PARTITION""
1687 local IMAGE="$SRC"/"$PARTITION".img
1688 if [ -f "$IMAGE" ]; then
1689 if [[ $(file -b "$IMAGE") == Linux* ]]; then
1690 extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
1691 elif [[ $(file -b "$IMAGE") == Android* ]]; then
1692 simg2img "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
1693 extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
1694 else
1695 echo "Unsupported "$IMAGE""
1696 fi
1697 fi
1698 done
1699
1700 SRC="$DUMPDIR"
1701 fi
1702
Steve Kondik5bd66602016-07-15 10:39:58 -07001703 if [ "$VENDOR_STATE" -eq "0" ]; then
1704 echo "Cleaning output directory ($OUTPUT_ROOT).."
1705 rm -rf "${OUTPUT_TMP:?}"
1706 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001707 if [ -d "$OUTPUT_ROOT" ]; then
1708 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1709 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001710 VENDOR_STATE=1
1711 fi
1712
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001713 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001714
1715 for (( i=1; i<COUNT+1; i++ )); do
1716
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001717 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001718 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001719 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001720 local OUTPUT_DIR=
1721 local TMP_DIR=
1722 local SRC_FILE=
1723 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001724 local IS_PRODUCT_PACKAGE=false
1725
1726 # Note: this relies on the fact that the ${FILELIST[@]} array
1727 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1728 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1729 IS_PRODUCT_PACKAGE=true
1730 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001731
Michael Bestasbda30202020-12-28 04:44:52 +02001732 OUTPUT_DIR="${OUTPUT_ROOT}"
1733 TMP_DIR="${OUTPUT_TMP}"
1734 SRC_FILE="/system/${SPEC_SRC_FILE}"
1735 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001736
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001737 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001738 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001739 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001740 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001741
Gabriele M58270a32017-11-13 23:15:29 +01001742 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001743 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001744 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001745 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001746 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001747 if [ -f "${VENDOR_REPO_FILE}" ]; then
1748 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001749 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001750 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001751 fi
1752 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001753 local TMP_HASH=$(get_hash "${PINNED}")
1754 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001755 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001756 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1757 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001758 fi
1759 fi
1760 fi
1761 fi
1762
Vladimir Olteana7d20492019-01-17 03:05:52 +02001763 if [ "${KANG}" = false ]; then
1764 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1765 fi
1766
Gabriele M58270a32017-11-13 23:15:29 +01001767 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001768 if [ "${FIXUP_HASH}" != "x" ]; then
1769 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1770 else
1771 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1772 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001773 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001774 FOUND=false
1775 # Try Lineage target first.
1776 # Also try to search for files stripped of
1777 # the "/system" prefix, if we're actually extracting
1778 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001779 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001780 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1781 FOUND=true
1782 break
1783 }
1784 done
1785
1786 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001787 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001788 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001789 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001790
Arian5f98d792021-09-09 15:24:25 +02001791 # Blob fixup pipeline has 2 parts: one that is fixed and
1792 # one that is user-configurable
1793 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1794 # Deodex apk|jar if that's the case
1795 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1796 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301797 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
1798 touch -t 200901010000 "$EXTRACT_TMP_DIR/classes"*
1799 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
1800 rm "$EXTRACT_TMP_DIR/classes"*
Arian5f98d792021-09-09 15:24:25 +02001801 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1802 fi
1803 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1804 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001805 fi
Arian5f98d792021-09-09 15:24:25 +02001806 # Now run user-supplied fixup function
1807 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1808 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001809
Arian5f98d792021-09-09 15:24:25 +02001810 if [ -f "${VENDOR_REPO_FILE}" ]; then
1811 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1812 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001813 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001814 chmod 755 "${VENDOR_REPO_FILE}"
1815 else
1816 chmod 644 "${VENDOR_REPO_FILE}"
1817 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001818 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001819
Arian5f98d792021-09-09 15:24:25 +02001820 if [ "${KANG}" = true ]; then
1821 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1822 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001823
Arian5f98d792021-09-09 15:24:25 +02001824 # Check and print whether the fixup pipeline actually did anything.
1825 # This isn't done right after the fixup pipeline because we want this print
1826 # to come after print_spec above, when in kang mode.
1827 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1828 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1829 # Now sanity-check the spec for this blob.
1830 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1831 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1832 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1833 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001834 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001835 fi
1836
Steve Kondik5bd66602016-07-15 10:39:58 -07001837 done
1838
1839 # Don't allow failing
1840 set -e
1841}
1842
1843#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001844# extract2:
1845#
1846# Positional parameters:
1847# $1: file containing the list of items to extract (aka proprietary-files.txt)
1848#
1849# Non-positional parameters (coming after $2):
1850# --section: selects the portion to parse and extracts from proprietary-files.txt
1851# --kang: if present, this option will activate the printing of hashes for the
1852# extracted blobs. Useful with --section for subsequent pinning of
1853# blobs taken from other origins.
1854#
1855function extract2() {
1856 # Consume positional parameters
1857 local PROPRIETARY_FILES_TXT="$1"; shift
1858 local SECTION=""
1859 local KANG=false
1860
1861 # Consume optional, non-positional parameters
1862 while [ "$#" -gt 0 ]; do
1863 case "$1" in
1864 --adb)
1865 ADB=true
1866 ;;
1867 --system)
1868 SYSTEM_SRC="$2"; shift
1869 ;;
1870 --vendor)
1871 VENDOR_SRC="$2"; shift
1872 ;;
1873 --odm)
1874 ODM_SRC="$2"; shift
1875 ;;
1876 --product)
1877 PRODUCT_SRC="$2"; shift
1878 ;;
1879 -s|--section)
1880 SECTION="$2"; shift
1881 ;;
1882 -k|--kang)
1883 KANG=true
1884 DISABLE_PINNING=1
1885 ;;
1886 esac
1887 shift
1888 done
1889
1890 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1891 echo "No sources set! You must select --adb or pass paths to partition dumps."
1892 exit 1
1893 fi
1894
1895 if [ -z "$OUTDIR" ]; then
1896 echo "Output dir not set!"
1897 exit 1
1898 fi
1899
1900 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1901
1902 # Allow failing, so we can try $DEST and/or $FILE
1903 set +e
1904
1905 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1906 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1907 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1908 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1909 local COUNT=${#FILELIST[@]}
1910 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301911 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001912
1913 if [ "$ADB" = true ]; then
1914 init_adb_connection
1915 fi
1916
1917 if [ "$VENDOR_STATE" -eq "0" ]; then
1918 echo "Cleaning output directory ($OUTPUT_ROOT).."
1919 rm -rf "${OUTPUT_TMP:?}"
1920 mkdir -p "${OUTPUT_TMP:?}"
1921 if [ -d "$OUTPUT_ROOT" ]; then
1922 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1923 fi
1924 VENDOR_STATE=1
1925 fi
1926
1927 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1928
1929 for (( i=1; i<COUNT+1; i++ )); do
1930
1931 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1932 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1933 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1934 local OUTPUT_DIR=
1935 local TMP_DIR=
1936 local SRC_FILE=
1937 local DST_FILE=
1938 local IS_PRODUCT_PACKAGE=false
1939
1940 # Note: this relies on the fact that the ${FILELIST[@]} array
1941 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1942 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1943 IS_PRODUCT_PACKAGE=true
1944 fi
1945
1946 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1947 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1948 TMP_DIR="${OUTPUT_TMP}/rootfs"
1949 else
1950 OUTPUT_DIR="${OUTPUT_ROOT}"
1951 TMP_DIR="${OUTPUT_TMP}"
1952 fi
1953 SRC_FILE="${SPEC_SRC_FILE}"
1954 DST_FILE="${SPEC_DST_FILE}"
1955
1956 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1957 local BLOB_DISPLAY_NAME="${DST_FILE}"
1958 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1959
1960 # Check pinned files
1961 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1962 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1963 local KEEP=""
1964 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1965 if [ -f "${VENDOR_REPO_FILE}" ]; then
1966 local PINNED="${VENDOR_REPO_FILE}"
1967 else
1968 local PINNED="${TMP_DIR}${DST_FILE}"
1969 fi
1970 if [ -f "$PINNED" ]; then
1971 local TMP_HASH=$(get_hash "${PINNED}")
1972 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1973 KEEP="1"
1974 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1975 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1976 fi
1977 fi
1978 fi
1979 fi
1980
1981 if [ "${KANG}" = false ]; then
1982 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1983 fi
1984
1985 if [ "$KEEP" = "1" ]; then
1986 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1987 else
1988 FOUND=false
1989 PARTITION_SOURCE_DIR=
1990 # Try Lineage target first.
1991 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1992 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1993 if [ "$PARTITION" = "system" ]; then
1994 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1995 elif [ "$PARTITION" = "vendor" ]; then
1996 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1997 elif [ "$PARTITION" = "product" ]; then
1998 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1999 elif [ "$PARTITION" = "odm" ]; then
2000 PARTITION_SOURCE_DIR="$ODM_SRC"
2001 fi
2002 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
2003 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2004 FOUND=true
2005 break
2006 }
2007 # Search with the full system/ prefix if the file was not found on the system partition
2008 # because we may be searching in a mounted system-as-root system.img
2009 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
2010 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2011 FOUND=true
2012 break
2013 }
2014 fi
2015 done
2016
2017 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
2018 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
2019 fi
2020
2021 if [ "${FOUND}" = false ]; then
2022 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
2023 continue
2024 fi
2025 fi
2026
2027 # Blob fixup pipeline has 2 parts: one that is fixed and
2028 # one that is user-configurable
2029 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2030 # Deodex apk|jar if that's the case
2031 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
2032 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302033 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
2034 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
2035 rm "$EXTRACT_TMP_DIR/classes"*
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04002036 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2037 fi
2038 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2039 fix_xml "${VENDOR_REPO_FILE}"
2040 fi
2041 # Now run user-supplied fixup function
2042 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2043 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2044
2045 if [ -f "${VENDOR_REPO_FILE}" ]; then
2046 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2047 local TYPE="${DIR##*/}"
2048 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
2049 chmod 755 "${VENDOR_REPO_FILE}"
2050 else
2051 chmod 644 "${VENDOR_REPO_FILE}"
2052 fi
2053 fi
2054
2055 if [ "${KANG}" = true ]; then
2056 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2057 fi
2058
2059 # Check and print whether the fixup pipeline actually did anything.
2060 # This isn't done right after the fixup pipeline because we want this print
2061 # to come after print_spec above, when in kang mode.
2062 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2063 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2064 # Now sanity-check the spec for this blob.
2065 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2066 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
2067 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
2068 fi
2069 fi
2070
2071 done
2072
2073 # Don't allow failing
2074 set -e
2075}
2076
2077#
Steve Kondik5bd66602016-07-15 10:39:58 -07002078# extract_firmware:
2079#
2080# $1: file containing the list of items to extract
2081# $2: path to extracted radio folder
2082#
2083function extract_firmware() {
2084 if [ -z "$OUTDIR" ]; then
2085 echo "Output dir not set!"
2086 exit 1
2087 fi
2088
2089 parse_file_list "$1"
2090
2091 # Don't allow failing
2092 set -e
2093
2094 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
2095 local COUNT=${#FILELIST[@]}
2096 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00002097 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07002098
2099 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
2100 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
2101 rm -rf "${OUTPUT_DIR:?}/"*
2102 VENDOR_RADIO_STATE=1
2103 fi
2104
2105 echo "Extracting $COUNT files in $1 from $SRC:"
2106
2107 for (( i=1; i<COUNT+1; i++ )); do
2108 local FILE="${FILELIST[$i-1]}"
2109 printf ' - %s \n' "/radio/$FILE"
2110
2111 if [ ! -d "$OUTPUT_DIR" ]; then
2112 mkdir -p "$OUTPUT_DIR"
2113 fi
2114 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
2115 chmod 644 "$OUTPUT_DIR/$FILE"
2116 done
2117}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002118
2119function extract_img_data() {
2120 local image_file="$1"
2121 local out_dir="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302122 local logFile="$EXTRACT_TMP_DIR/debugfs.log"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002123
2124 if [ ! -d "$out_dir" ]; then
2125 mkdir -p "$out_dir"
2126 fi
2127
2128 if [[ "$HOST_OS" == "Darwin" ]]; then
2129 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
2130 echo "[-] Failed to extract data from '$image_file'"
2131 abort 1
2132 }
2133 else
2134 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2135 do
2136 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2137 echo "[-] Failed to extract data from '$image_file'"
2138 abort 1
2139 }
2140 done
2141 fi
2142
2143 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2144 if grep -Fq "$symlink_err" "$logFile"; then
2145 echo "[-] Symlinks have not been properly processed from $image_file"
2146 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
2147 abort 1
2148 fi
2149}
2150
2151declare -ra VENDOR_SKIP_FILES=(
2152 "bin/toybox_vendor"
2153 "bin/toolbox"
2154 "bin/grep"
2155 "build.prop"
2156 "compatibility_matrix.xml"
2157 "default.prop"
2158 "etc/NOTICE.xml.gz"
2159 "etc/vintf/compatibility_matrix.xml"
2160 "etc/vintf/manifest.xml"
2161 "etc/wifi/wpa_supplicant.conf"
2162 "manifest.xml"
2163 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2164 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2165 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2166 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2167 "overlay/framework-res__auto_generated_rro.apk"
2168 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2169)
2170
2171function array_contains() {
2172 local element
2173 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2174 return 1
2175}
2176
2177function generate_prop_list_from_image() {
2178 local image_file="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302179 local image_dir="$EXTRACT_TMP_DIR/image-temp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002180 local output_list="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302181 local output_list_tmp="$EXTRACT_TMP_DIR/_proprietary-blobs.txt"
Michael Bestasca5d78a2021-12-02 20:58:40 +02002182 local -n skipped_files="$3"
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002183 local component="$4"
2184 local partition="$component"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002185
Chirayu Desai203cc522021-12-04 01:18:45 +05302186 mkdir -p "$image_dir"
2187
2188 if [[ $(file -b "$image_file") == Linux* ]]; then
2189 extract_img_data "$image_file" "$image_dir"
2190 elif [[ $(file -b "$image_file") == Android* ]]; then
2191 simg2img "$image_file" "$image_dir"/"$(basename "$image_file").raw"
2192 extract_img_data "$image_dir"/"$(basename "$image_file").raw" "$image_dir"
2193 rm "$image_dir"/"$(basename "$image_file").raw"
2194 else
2195 echo "Unsupported "$image_file""
2196 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002197
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002198 if [ -z "$component" ]; then
2199 partition="vendor"
2200 elif [[ "$component" == "carriersettings" ]]; then
2201 partition="product"
2202 fi
2203
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002204 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2205 do
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002206 if [[ "$component" == "carriersettings" ]] && ! prefix_match_file "etc/CarrierSettings" "$FILE" ; then
2207 continue
2208 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002209 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2210 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2211 continue
2212 fi
2213 # Skip device defined skipped files since they will be re-generated at build time
Michael Bestasca5d78a2021-12-02 20:58:40 +02002214 if array_contains "$FILE" "${skipped_files[@]}"; then
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002215 continue
2216 fi
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002217 echo "$partition/$FILE" >> "$output_list_tmp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002218 done
2219
2220 # Sort merged file with all lists
Michael Bestas1e6efb32021-11-15 19:28:56 +02002221 LC_ALL=C sort -u "$output_list_tmp" > "$output_list"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002222
2223 # Clean-up
2224 rm -f "$output_list_tmp"
2225}
Bruno Martins0f425f12021-04-10 14:57:32 +01002226
2227function colored_echo() {
2228 IFS=" "
2229 local color=$1;
2230 shift
2231 if ! [[ $color =~ '^[0-9]$' ]] ; then
2232 case $(echo $color | tr '[:upper:]' '[:lower:]') in
2233 black) color=0 ;;
2234 red) color=1 ;;
2235 green) color=2 ;;
2236 yellow) color=3 ;;
2237 blue) color=4 ;;
2238 magenta) color=5 ;;
2239 cyan) color=6 ;;
2240 white|*) color=7 ;; # white or invalid color
2241 esac
2242 fi
Bruno Martins5064db22021-06-21 14:47:40 +01002243 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002244 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01002245 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002246}