blob: 514171ec4e2ad47bcade075d4df803096fc0dc03 [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"
Mohd Farazd683fe42022-07-23 14:33:59 +0200340 elif prefix_match_file "recovery/" $TARGET ; then
341 local OUTTARGET=$(truncate_file $TARGET)
342 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_RECOVERY)/%s%s\n' \
343 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
344 elif prefix_match_file "vendor_ramdisk/" $TARGET ; then
345 local OUTTARGET=$(truncate_file $TARGET)
346 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_RAMDISK)/%s%s\n' \
347 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400348 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400349 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400350 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
351 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700352 done
353 return 0
354}
355
356#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700357# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700358#
359# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200360# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700361# $3: type-specific extra flags
362# $4: Name of the array holding the target list
363#
364# Internal function which writes out the BUILD_PREBUILT stanzas
365# for all modules in the list. This is called by write_product_packages
366# after the modules are categorized.
367#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700368function write_blueprint_packages() {
369
370 local CLASS="$1"
371 local PARTITION="$2"
372 local EXTRA="$3"
373
374 # Yes, this is a horrible hack - we create a new array using indirection
375 local ARR_NAME="$4[@]"
376 local FILELIST=("${!ARR_NAME}")
377
378 local FILE=
379 local ARGS=
380 local BASENAME=
381 local EXTENSION=
382 local PKGNAME=
383 local SRC=
Tim Zimmermannd9320762021-12-23 07:06:17 +0100384 local STEM=
TheStrix6e24acc2020-04-10 18:20:19 +0530385 local OVERRIDEPKG=
SamarV-1217e6b4082024-02-26 14:39:34 +0530386 local REQUIREDPKG=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700387
388 for P in "${FILELIST[@]}"; do
389 FILE=$(target_file "$P")
390 ARGS=$(target_args "$P")
Tim Zimmermannd9320762021-12-23 07:06:17 +0100391 ARGS=(${ARGS//;/ })
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700392
393 BASENAME=$(basename "$FILE")
394 DIRNAME=$(dirname "$FILE")
395 EXTENSION=${BASENAME##*.}
396 PKGNAME=${BASENAME%.*}
397
Tim Zimmermanne93bb1b2021-12-23 10:15:26 +0100398 if [ "$CLASS" = "EXECUTABLES" ] && [ "$EXTENSION" != "sh" ]; then
399 PKGNAME="$BASENAME"
400 EXTENSION=""
401 fi
402
Tim Zimmermannd9320762021-12-23 07:06:17 +0100403 # Allow overriding module name
404 STEM=
405 for ARG in "${ARGS[@]}"; do
406 if [[ "$ARG" =~ "MODULE" ]]; then
407 STEM="$PKGNAME"
408 PKGNAME=${ARG#*=}
409 fi
410 done
411
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700412 # Add to final package list
413 PACKAGE_LIST+=("$PKGNAME")
414
415 SRC="proprietary"
416 if [ "$PARTITION" = "system" ]; then
417 SRC+="/system"
418 elif [ "$PARTITION" = "vendor" ]; then
419 SRC+="/vendor"
420 elif [ "$PARTITION" = "product" ]; then
421 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200422 elif [ "$PARTITION" = "system_ext" ]; then
423 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700424 elif [ "$PARTITION" = "odm" ]; then
425 SRC+="/odm"
426 fi
427
428 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
429 printf 'cc_prebuilt_library_shared {\n'
430 printf '\tname: "%s",\n' "$PKGNAME"
Tim Zimmermannd9320762021-12-23 07:06:17 +0100431 if [ ! -z "$STEM" ]; then
432 printf '\tstem: "%s",\n' "$STEM"
433 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700434 printf '\towner: "%s",\n' "$VENDOR"
435 printf '\tstrip: {\n'
436 printf '\t\tnone: true,\n'
437 printf '\t},\n'
438 printf '\ttarget: {\n'
439 if [ "$EXTRA" = "both" ]; then
440 printf '\t\tandroid_arm: {\n'
441 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
442 printf '\t\t},\n'
443 printf '\t\tandroid_arm64: {\n'
444 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
445 printf '\t\t},\n'
446 elif [ "$EXTRA" = "64" ]; then
447 printf '\t\tandroid_arm64: {\n'
448 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
449 printf '\t\t},\n'
450 else
451 printf '\t\tandroid_arm: {\n'
452 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
453 printf '\t\t},\n'
454 fi
455 printf '\t},\n'
456 if [ "$EXTRA" != "none" ]; then
457 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
458 fi
dianlujitao848101c2020-09-12 00:15:13 +0800459 printf '\tcheck_elf_files: false,\n'
Chirayu Desaia7741012021-12-04 07:17:28 +0530460 elif [ "$CLASS" = "APEX" ]; then
461 printf 'prebuilt_apex {\n'
462 printf '\tname: "%s",\n' "$PKGNAME"
463 printf '\towner: "%s",\n' "$VENDOR"
464 SRC="$SRC/apex"
465 printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
466 printf '\tfilename: "%s",\n' "$FILE"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700467 elif [ "$CLASS" = "APPS" ]; then
468 printf 'android_app_import {\n'
469 printf '\tname: "%s",\n' "$PKGNAME"
470 printf '\towner: "%s",\n' "$VENDOR"
471 if [ "$EXTRA" = "priv-app" ]; then
472 SRC="$SRC/priv-app"
473 else
474 SRC="$SRC/app"
475 fi
476 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
LuK1337508e85f2021-08-23 18:18:57 +0200477 USE_PLATFORM_CERTIFICATE="true"
478 for ARG in "${ARGS[@]}"; do
479 if [ "$ARG" = "PRESIGNED" ]; then
480 USE_PLATFORM_CERTIFICATE="false"
Michael Bestasab47e912024-03-06 13:32:05 +0200481 printf '\tpreprocessed: true,\n'
LuK1337508e85f2021-08-23 18:18:57 +0200482 printf '\tpresigned: true,\n'
483 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
484 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200485 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200486 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
SamarV-1217e6b4082024-02-26 14:39:34 +0530487 elif [[ "$ARG" =~ "REQUIRED" ]]; then
488 REQUIREDPKG=${ARG#*=}
489 REQUIRED_PACKAGES_LIST+="$REQUIREDPKG,"
490 printf '\trequired: ["%s"],\n' "${REQUIREDPKG//,/\", \"}"
SamarV-1215556e2d2024-03-19 08:50:02 +0530491 elif [[ "$ARG" =~ "SYMLINK" ]]; then
492 continue
LuK1337508e85f2021-08-23 18:18:57 +0200493 elif [ ! -z "$ARG" ]; then
494 USE_PLATFORM_CERTIFICATE="false"
495 printf '\tcertificate: "%s",\n' "$ARG"
496 fi
497 done
498 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700499 printf '\tcertificate: "platform",\n'
500 fi
501 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
502 printf 'dex_import {\n'
503 printf '\tname: "%s",\n' "$PKGNAME"
504 printf '\towner: "%s",\n' "$VENDOR"
505 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
506 elif [ "$CLASS" = "ETC" ]; then
507 if [ "$EXTENSION" = "xml" ]; then
508 printf 'prebuilt_etc_xml {\n'
509 else
510 printf 'prebuilt_etc {\n'
511 fi
512 printf '\tname: "%s",\n' "$PKGNAME"
513 printf '\towner: "%s",\n' "$VENDOR"
514 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200515 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700516 elif [ "$CLASS" = "EXECUTABLES" ]; then
517 if [ "$EXTENSION" = "sh" ]; then
518 printf 'sh_binary {\n'
519 else
520 printf 'cc_prebuilt_binary {\n'
521 fi
522 printf '\tname: "%s",\n' "$PKGNAME"
523 printf '\towner: "%s",\n' "$VENDOR"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200524 if [ "$EXTENSION" != "sh" ]; then
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200525 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200526 printf '\tcheck_elf_files: false,\n'
Tim Zimmermann54606d62021-12-23 09:26:54 +0100527 printf '\tstrip: {\n'
528 printf '\t\tnone: true,\n'
529 printf '\t},\n'
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200530 printf '\tprefer: true,\n'
531 else
532 printf '\tsrc: "%s/bin/%s",\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200533 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700534 unset EXTENSION
535 else
536 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
537 fi
538 if [ "$CLASS" = "APPS" ]; then
539 printf '\tdex_preopt: {\n'
540 printf '\t\tenabled: false,\n'
541 printf '\t},\n'
Jyotiraditya Panda45f50af2024-02-19 05:35:33 +0900542 if [ "$DIRNAME" != "." ] && [[ "$DIRNAME" == */* ]]; then
543 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
544 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700545 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200546 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700547 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200548 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700549 fi
550 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200551 if [ "$CLASS" = "ETC" ] ; then
552 if [ "$DIRNAME" != "." ]; then
553 printf '\tsub_dir: "%s",\n' "$DIRNAME"
554 fi
555 fi
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200556 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700557 printf '\tprefer: true,\n'
558 fi
559 if [ "$EXTRA" = "priv-app" ]; then
560 printf '\tprivileged: true,\n'
561 fi
562 if [ "$PARTITION" = "vendor" ]; then
563 printf '\tsoc_specific: true,\n'
564 elif [ "$PARTITION" = "product" ]; then
565 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200566 elif [ "$PARTITION" = "system_ext" ]; then
567 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700568 elif [ "$PARTITION" = "odm" ]; then
569 printf '\tdevice_specific: true,\n'
570 fi
571 printf '}\n\n'
572 done
573}
574
575#
Steve Kondik5bd66602016-07-15 10:39:58 -0700576# write_product_packages:
577#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700578# This function will create prebuilt entries in the
579# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700580# product makefile for all files in the blob list which
581# start with a single dash (-) character.
582#
583function write_product_packages() {
584 PACKAGE_LIST=()
585
Chenyang Zhongc487f382022-02-10 21:40:41 -0500586 # Sort the package list for comm
587 PRODUCT_PACKAGES_LIST=($( printf '%s\n' "${PRODUCT_PACKAGES_LIST[@]}" | LC_ALL=C sort))
588
Steve Kondik5bd66602016-07-15 10:39:58 -0700589 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
590
591 if [ "$COUNT" = "0" ]; then
592 return 0
593 fi
594
595 # Figure out what's 32-bit, what's 64-bit, and what's multilib
596 # I really should not be doing this in bash due to shitty array passing :(
597 local T_LIB32=( $(prefix_match "lib/") )
598 local T_LIB64=( $(prefix_match "lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500599 local MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
600 local LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
601 local LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik5bd66602016-07-15 10:39:58 -0700602
603 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700604 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700605 fi
606 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700607 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700608 fi
609 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700610 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700611 fi
612
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400613 local T_S_LIB32=( $(prefix_match "system/lib/") )
614 local T_S_LIB64=( $(prefix_match "system/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500615 local S_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
616 local S_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
617 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 -0400618
619 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700620 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400621 fi
622 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700623 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400624 fi
625 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700626 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400627 fi
628
Steve Kondik5bd66602016-07-15 10:39:58 -0700629 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
630 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500631 local V_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
632 local V_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
633 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 -0700634
635 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700636 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700637 fi
638 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700639 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700640 fi
641 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700642 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500643 fi
644
645 local T_P_LIB32=( $(prefix_match "product/lib/") )
646 local T_P_LIB64=( $(prefix_match "product/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500647 local P_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
648 local P_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
649 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 -0500650
651 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700652 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500653 fi
654 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700655 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500656 fi
657 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700658 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700659 fi
660
Luca Stefani776be462020-09-09 15:53:58 +0200661 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
662 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500663 local SE_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
664 local SE_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
665 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 +0200666
667 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
668 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
669 fi
670 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
671 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
672 fi
673 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
674 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
675 fi
676
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700677 local T_O_LIB32=( $(prefix_match "odm/lib/") )
678 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500679 local O_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
680 local O_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
681 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 -0700682
683 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700684 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700685 fi
686 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700687 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700688 fi
689 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700690 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700691 fi
692
Chirayu Desaia7741012021-12-04 07:17:28 +0530693 # APEX
694 local APEX=( $(prefix_match "apex/") )
695 if [ "${#APEX[@]}" -gt "0" ]; then
696 write_blueprint_packages "APEX" "" "" "APEX" >> "$ANDROIDBP"
697 fi
698 local S_APEX=( $(prefix_match "system/apex/") )
699 if [ "${#S_APEX[@]}" -gt "0" ]; then
700 write_blueprint_packages "APEX" "system" "" "S_APEX" >> "$ANDROIDBP"
701 fi
702 local V_APEX=( $(prefix_match "vendor/apex/") )
703 if [ "${#V_APEX[@]}" -gt "0" ]; then
704 write_blueprint_packages "APEX" "vendor" "" "V_APEX" >> "$ANDROIDBP"
705 fi
706 local SE_APEX=( $(prefix_match "system_ext/apex/") )
707 if [ "${#SE_APEX[@]}" -gt "0" ]; then
708 write_blueprint_packages "APEX" "system_ext" "" "SE_APEX" >> "$ANDROIDBP"
709 fi
710
Steve Kondik5bd66602016-07-15 10:39:58 -0700711 # Apps
712 local APPS=( $(prefix_match "app/") )
713 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100714 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700715 fi
716 local PRIV_APPS=( $(prefix_match "priv-app/") )
717 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100718 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700719 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400720 local S_APPS=( $(prefix_match "system/app/") )
721 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100722 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400723 fi
724 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
725 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100726 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400727 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700728 local V_APPS=( $(prefix_match "vendor/app/") )
729 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100730 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700731 fi
732 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
733 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100734 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500735 fi
736 local P_APPS=( $(prefix_match "product/app/") )
737 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100738 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500739 fi
740 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
741 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100742 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700743 fi
Luca Stefani776be462020-09-09 15:53:58 +0200744 local SE_APPS=( $(prefix_match "system_ext/app/") )
745 if [ "${#SE_APPS[@]}" -gt "0" ]; then
746 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
747 fi
748 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
749 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
750 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
751 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700752 local O_APPS=( $(prefix_match "odm/app/") )
753 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100754 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700755 fi
756 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
757 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100758 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700759 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700760
761 # Framework
762 local FRAMEWORK=( $(prefix_match "framework/") )
763 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700764 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700765 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400766 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
767 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700768 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400769 fi
Christian Oder974b5902017-10-08 23:15:52 +0200770 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200771 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700772 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500773 fi
774 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
775 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700776 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200777 fi
Luca Stefani776be462020-09-09 15:53:58 +0200778 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700779 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200780 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
781 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700782 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
783 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700784 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700785 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700786
787 # Etc
788 local ETC=( $(prefix_match "etc/") )
789 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700790 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700791 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400792 local S_ETC=( $(prefix_match "system/etc/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200793 if [ "${#S_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700794 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400795 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700796 local V_ETC=( $(prefix_match "vendor/etc/") )
797 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700798 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500799 fi
800 local P_ETC=( $(prefix_match "product/etc/") )
801 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700802 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700803 fi
Luca Stefani776be462020-09-09 15:53:58 +0200804 local SE_ETC=( $(prefix_match "system_ext/etc/") )
805 if [ "${#SE_ETC[@]}" -gt "0" ]; then
806 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
807 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700808 local O_ETC=( $(prefix_match "odm/etc/") )
809 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700810 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700811 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700812
813 # Executables
814 local BIN=( $(prefix_match "bin/") )
815 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700816 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700817 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400818 local S_BIN=( $(prefix_match "system/bin/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200819 if [ "${#S_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700820 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400821 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700822 local V_BIN=( $(prefix_match "vendor/bin/") )
823 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700824 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500825 fi
826 local P_BIN=( $(prefix_match "product/bin/") )
827 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700828 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700829 fi
Luca Stefani776be462020-09-09 15:53:58 +0200830 local SE_BIN=( $(prefix_match "system_ext/bin/") )
831 if [ "${#SE_BIN[@]}" -gt "0" ]; then
832 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
833 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700834 local O_BIN=( $(prefix_match "odm/bin/") )
835 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700836 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700837 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700838
SamarV-1215556e2d2024-03-19 08:50:02 +0530839 write_package_definition "${PACKAGE_LIST[@]}" >> "$PRODUCTMK"
840}
Steve Kondik5bd66602016-07-15 10:39:58 -0700841
SamarV-1215556e2d2024-03-19 08:50:02 +0530842
843#
844# write_symlink_packages:
845#
846# Creates symlink entries in the Android.bp and related PRODUCT_PACKAGES
847# list in the product makefile for all files in the blob list which has
848# SYMLINK argument.
849#
850function write_symlink_packages() {
851 local FILE=
852 local ARGS=
853 local ARCH=
854 local BASENAME=
855 local PKGNAME=
856 local PREFIX=
857 local SYMLINK_BASENAME=
858 local SYMLINK_PACKAGES=()
859
860 # Sort the symlinks list for comm
861 PRODUCT_SYMLINKS_LIST=($( printf '%s\n' "${PRODUCT_SYMLINKS_LIST[@]}" | LC_ALL=C sort))
862
863 local COUNT=${#PRODUCT_SYMLINKS_LIST[@]}
864
865 if [ "$COUNT" = "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700866 return 0
867 fi
868
SamarV-1215556e2d2024-03-19 08:50:02 +0530869 for LINE in "${PRODUCT_SYMLINKS_LIST[@]}"; do
Bruno Martinsbe7f3cb2024-06-23 15:54:02 +0100870 FILE=$(target_file "$LINE")
Bruno Martinsf96fd122024-03-28 14:31:02 +0000871 if [[ "$LINE" =~ '/lib64/' || "$LINE" =~ '/lib/arm64/' ]]; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530872 ARCH="64"
Bruno Martinsf96fd122024-03-28 14:31:02 +0000873 elif [[ "$LINE" =~ '/lib/' ]]; then
874 ARCH="32"
Steve Kondik5bd66602016-07-15 10:39:58 -0700875 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530876 BASENAME=$(basename "$FILE")
877 ARGS=$(target_args "$LINE")
878 ARGS=(${ARGS//;/ })
879 for ARG in "${ARGS[@]}"; do
880 if [[ "$ARG" =~ "SYMLINK" ]]; then
881 SYMLINKS=${ARG#*=}
882 SYMLINKS=(${SYMLINKS//,/ })
883 for SYMLINK in "${SYMLINKS[@]}"; do
884 SYMLINK_BASENAME=$(basename "$SYMLINK")
Bruno Martinsad05f512024-06-28 00:34:33 +0100885 PKGNAME="${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}"
886 if [[ "${SYMLINK_PACKAGES[@]}" =~ "$PKGNAME" ]]; then
887 PKGNAME+="_$(grep -o "$PKGNAME" <<< ${SYMLINK_PACKAGES[*]} | wc -l)"
888 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530889 {
890 printf 'install_symlink {\n'
891 printf '\tname: "%s",\n' "$PKGNAME"
Mashopy06100c92024-04-23 21:52:04 +0200892 if prefix_match_file "vendor/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530893 PREFIX='vendor/'
894 printf '\tsoc_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200895 elif prefix_match_file "product/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530896 PREFIX='product/'
897 printf '\tproduct_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200898 elif prefix_match_file "system_ext/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530899 PREFIX='system_ext/'
900 printf '\tsystem_ext_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200901 elif prefix_match_file "odm/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530902 PREFIX='odm/'
903 printf '\tdevice_specific: true,\n'
904 fi
905 printf '\tinstalled_location: "%s",\n' "${SYMLINK#"$PREFIX"}"
906 printf '\tsymlink_target: "/%s",\n' "$FILE"
907 printf '}\n\n'
908 } >> "$ANDROIDBP"
909 SYMLINK_PACKAGES+=("$PKGNAME")
910 done
911 fi
912 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700913 done
SamarV-1215556e2d2024-03-19 08:50:02 +0530914
915 write_package_definition "${SYMLINK_PACKAGES[@]}" >> "$PRODUCTMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700916}
917
918#
Michael Bestasfe71eb32023-06-11 18:59:10 +0300919# write_single_product_copy_files:
920#
921# $1: the file to be copied
922#
923# Creates a PRODUCT_COPY_FILES section in the product makefile for the
924# item provided in $1.
925#
926function write_single_product_copy_files() {
927 local FILE="$1"
928 if [ -z "$FILE" ]; then
929 echo "A file must be provided to write_single_product_copy_files()!"
930 exit 1
931 fi
932
933 local TARGET=$(target_file "$FILE")
934 local OUTTARGET=$(truncate_file $TARGET)
935
936 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
937 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s\n' \
938 "$OUTDIR" "$TARGET" "$OUTTARGET" >> "$PRODUCTMK"
939}
940
941#
942# write_single_product_packages:
943#
944# $1: the package to be built
945#
946# Creates a PRODUCT_PACKAGES section in the product makefile for the
947# item provided in $1.
948#
949function write_single_product_packages() {
950 local PACKAGE="$1"
951 if [ -z "$PACKAGE" ]; then
952 echo "A package must be provided to write_single_product_packages()!"
953 exit 1
954 fi
955
956 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
957 printf ' %s%s\n' "$PACKAGE" >> "$PRODUCTMK"
958}
959
960#
Michael Bestas431a8002023-06-11 20:04:45 +0300961# write_rro_androidmanifest:
962#
963# $2: target package for the RRO overlay
964#
965# Creates an AndroidManifest.xml for an RRO overlay.
966#
967function write_rro_androidmanifest() {
968 local TARGET_PACKAGE="$1"
969
970 cat << EOF
971<manifest xmlns:android="http://schemas.android.com/apk/res/android"
972 package="$TARGET_PACKAGE.vendor"
973 android:versionCode="1"
974 android:versionName="1.0">
975 <application android:hasCode="false" />
976 <overlay
977 android:targetPackage="$TARGET_PACKAGE"
978 android:isStatic="true"
979 android:priority="0"/>
980</manifest>
981EOF
982}
983
984#
985# write_rro_blueprint:
986#
987# $1: package name for the RRO overlay
988# $2: target partition for the RRO overlay
989#
990# Creates an Android.bp for an RRO overlay.
991#
992function write_rro_blueprint() {
993 local PKGNAME="$1"
994 local PARTITION="$2"
995
996 printf 'runtime_resource_overlay {\n'
997 printf '\tname: "%s",\n' "$PKGNAME"
998 printf '\ttheme: "%s",\n' "$PKGNAME"
999 printf '\tsdk_version: "%s",\n' "current"
1000 printf '\taaptflags: ["%s"],\n' "--keep-raw-values"
1001
1002 if [ "$PARTITION" = "vendor" ]; then
1003 printf '\tsoc_specific: true,\n'
1004 elif [ "$PARTITION" = "product" ]; then
1005 printf '\tproduct_specific: true,\n'
1006 elif [ "$PARTITION" = "system_ext" ]; then
1007 printf '\tsystem_ext_specific: true,\n'
1008 elif [ "$PARTITION" = "odm" ]; then
1009 printf '\tdevice_specific: true,\n'
1010 fi
1011 printf '}\n'
1012}
1013
1014#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001015# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -07001016#
1017# $1: file which will be written to
1018#
Michael Bestasa2934df2020-12-19 03:50:32 +02001019# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -07001020# note that this is not an append operation, and should
1021# be executed first!
1022#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001023function write_blueprint_header() {
1024 if [ -f $1 ]; then
1025 rm $1
1026 fi
1027
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001028 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1029
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001030 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001031// Automatically generated file. DO NOT MODIFY
1032//
1033// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001034
1035EOF
1036}
1037
1038#
1039# write_makefile_header:
1040#
1041# $1: file which will be written to
1042#
Michael Bestasa2934df2020-12-19 03:50:32 +02001043# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001044# note that this is not an append operation, and should
1045# be executed first!
1046#
1047function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -05001048 if [ -f $1 ]; then
1049 rm $1
1050 fi
1051
Steve Kondik5bd66602016-07-15 10:39:58 -07001052 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1053
Jake Whatley9843b322017-01-25 21:49:16 -05001054 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001055# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -07001056#
Steve Kondik5bd66602016-07-15 10:39:58 -07001057# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1058
1059EOF
1060}
1061
1062#
Michael Bestas431a8002023-06-11 20:04:45 +03001063# write_xml_header:
1064#
1065# $1: file which will be written to
1066#
1067# writes out the warning message regarding manual file modifications.
1068# note that this is not an append operation, and should
1069# be executed first!
1070#
1071function write_xml_header() {
1072 if [ -f $1 ]; then
1073 rm $1
1074 fi
1075
1076 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1077 [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
1078
1079 cat << EOF >> $1
1080<?xml version="1.0" encoding="utf-8"?>
1081<!--
1082 Automatically generated file. DO NOT MODIFY
1083
1084 This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1085-->
1086EOF
1087}
1088
1089#
1090# write_rro_package:
1091#
1092# $1: the RRO package name
1093# $2: the RRO target package
1094# $3: the partition for the RRO overlay
1095#
1096# Generates the file structure for an RRO overlay.
1097#
1098function write_rro_package() {
1099 local PKGNAME="$1"
1100 if [ -z "$PKGNAME" ]; then
1101 echo "A package name must be provided to write_rro_package()!"
1102 exit 1
1103 fi
1104
1105 local TARGET_PACKAGE="$2"
1106 if [ -z "$TARGET_PACKAGE" ]; then
1107 echo "A target package must be provided to write_rro_package()!"
1108 exit 1
1109 fi
1110
1111 local PARTITION="$3"
1112 if [ -z "$PARTITION" ]; then
1113 PARTITION="vendor"
1114 fi
1115
1116 local RROBP="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/Android.bp
1117 local RROMANIFEST="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/AndroidManifest.xml
1118
1119 write_blueprint_header "$RROBP"
1120 write_xml_header "$RROMANIFEST"
1121
1122 write_rro_blueprint "$PKGNAME" "$PARTITION" >> "$RROBP"
1123 write_rro_androidmanifest "$TARGET_PACKAGE" >> "$RROMANIFEST"
1124}
1125
1126#
SamarV-1215556e2d2024-03-19 08:50:02 +05301127# write_package_definition:
1128#
1129# $@: list of packages
1130#
1131# writes out the final PRODUCT_PACKAGES list
1132#
1133function write_package_definition() {
1134 local PACKAGE_LIST=("${@}")
1135 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
1136
1137 if [ "$PACKAGE_COUNT" -eq "0" ]; then
1138 return 0
1139 fi
1140
1141 printf '\n%s\n' "PRODUCT_PACKAGES += \\"
1142 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
SamarV-1217e6b4082024-02-26 14:39:34 +05301143 local SKIP=false
SamarV-1215556e2d2024-03-19 08:50:02 +05301144 local LINEEND=" \\"
1145 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
1146 LINEEND=""
1147 fi
SamarV-1217e6b4082024-02-26 14:39:34 +05301148 for PKG in $(tr "," "\n" <<< "$REQUIRED_PACKAGES_LIST"); do
1149 if [[ $PKG == "${PACKAGE_LIST[$i - 1]}" ]]; then
1150 SKIP=true
1151 break
1152 fi
1153 done
1154 # Skip adding of the package to product makefile if it's in the required list
1155 if [[ $SKIP == false ]]; then
1156 printf ' %s%s\n' "${PACKAGE_LIST[$i - 1]}" "$LINEEND" >> "$PRODUCTMK"
1157 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301158 done
1159}
1160
1161#
Steve Kondik5bd66602016-07-15 10:39:58 -07001162# write_headers:
1163#
1164# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001165# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001166#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001167# Calls write_makefile_header for each of the makefiles and
1168# write_blueprint_header for Android.bp and creates the initial
1169# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001170#
1171function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001172 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001173
1174 GUARD="$2"
1175 if [ -z "$GUARD" ]; then
1176 GUARD="TARGET_DEVICE"
1177 fi
1178
Steve Kondik5bd66602016-07-15 10:39:58 -07001179 cat << EOF >> "$ANDROIDMK"
1180LOCAL_PATH := \$(call my-dir)
1181
1182EOF
1183 if [ "$COMMON" -ne 1 ]; then
1184 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001185ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001186
1187EOF
1188 else
1189 if [ -z "$1" ]; then
1190 echo "Argument with devices to be added to guard must be set!"
1191 exit 1
1192 fi
1193 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001194ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001195
1196EOF
1197 fi
1198
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001199 write_makefile_header "$BOARDMK"
1200 write_makefile_header "$PRODUCTMK"
1201 write_blueprint_header "$ANDROIDBP"
1202
1203 cat << EOF >> "$ANDROIDBP"
1204soong_namespace {
1205}
1206
1207EOF
1208
1209 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1210 cat << EOF >> "$PRODUCTMK"
1211PRODUCT_SOONG_NAMESPACES += \\
1212 vendor/$VENDOR/$DEVICE
1213
1214EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001215}
1216
1217#
1218# write_footers:
1219#
1220# Closes the inital guard and any other finalization tasks. Must
1221# be called as the final step.
1222#
1223function write_footers() {
1224 cat << EOF >> "$ANDROIDMK"
1225endif
1226EOF
1227}
1228
1229# Return success if adb is up and not in recovery
1230function _adb_connected {
1231 {
Jake Whatley9843b322017-01-25 21:49:16 -05001232 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001233 then
1234 return 0
1235 fi
1236 } 2>/dev/null
1237
1238 return 1
1239};
1240
1241#
1242# parse_file_list:
1243#
1244# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001245# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001246#
1247# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1248#
1249function parse_file_list() {
1250 if [ -z "$1" ]; then
1251 echo "An input file is expected!"
1252 exit 1
1253 elif [ ! -f "$1" ]; then
1254 echo "Input file "$1" does not exist!"
1255 exit 1
1256 fi
1257
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001258 if [ -n "$2" ]; then
1259 echo "Using section \"$2\""
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301260 LIST=$EXTRACT_TMP_DIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001261 # Match all lines starting with first line found to start* with '#'
1262 # comment and contain** $2, and ending with first line to be empty*.
1263 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1264 # **the $2 match is case-insensitive
1265 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001266 else
1267 LIST=$1
1268 fi
1269
Steve Kondik5bd66602016-07-15 10:39:58 -07001270 PRODUCT_PACKAGES_LIST=()
1271 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001272 PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +05301273 PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001274 PRODUCT_COPY_FILES_LIST=()
1275 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001276 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001277
1278 while read -r line; do
1279 if [ -z "$line" ]; then continue; fi
1280
1281 # If the line has a pipe delimiter, a sha1 hash should follow.
1282 # This indicates the file should be pinned and not overwritten
1283 # when extracting files.
1284 local SPLIT=(${line//\|/ })
1285 local COUNT=${#SPLIT[@]}
1286 local SPEC=${SPLIT[0]}
1287 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001288 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001289 if [ "$COUNT" -gt "1" ]; then
1290 HASH=${SPLIT[1]}
1291 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001292 if [ "$COUNT" -gt "2" ]; then
1293 FIXUP_HASH=${SPLIT[2]}
1294 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301295 if [[ "$SPEC" =~ 'SYMLINK=' ]]; then
1296 PRODUCT_SYMLINKS_LIST+=("${SPEC#-}")
1297 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001298 # if line starts with a dash, it needs to be packaged
1299 if [[ "$SPEC" =~ ^- ]]; then
1300 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1301 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001302 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Chirayu Desaia7741012021-12-04 07:17:28 +05301303 # if line contains apex, apk, jar or vintf fragment, it needs to be packaged
1304 elif suffix_match_file ".apex" "$(src_file "$SPEC")" || \
1305 suffix_match_file ".apk" "$(src_file "$SPEC")" || \
Michael Bestasea90aef2021-11-15 22:18:04 +02001306 suffix_match_file ".jar" "$(src_file "$SPEC")" || \
1307 [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
1308 PRODUCT_PACKAGES_LIST+=("$SPEC")
1309 PRODUCT_PACKAGES_HASHES+=("$HASH")
1310 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001311 else
1312 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1313 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001314 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001315 fi
1316
Chirayu Desaif1a21302022-09-13 00:29:33 +05301317 done < <(grep -v -E '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001318}
1319
1320#
1321# write_makefiles:
1322#
1323# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001324# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001325#
SamarV-1215556e2d2024-03-19 08:50:02 +05301326# Calls write_product_copy_files, write_product_packages and
1327# lastly write_symlink_packages on the given file and appends
1328# to the Android.bp as well as the product makefile.
Steve Kondik5bd66602016-07-15 10:39:58 -07001329#
1330function write_makefiles() {
1331 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001332 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001333 write_product_packages
SamarV-1215556e2d2024-03-19 08:50:02 +05301334 write_symlink_packages
Steve Kondik5bd66602016-07-15 10:39:58 -07001335}
1336
1337#
1338# append_firmware_calls_to_makefiles:
1339#
1340# Appends to Android.mk the calls to all images present in radio folder
1341# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1342#
1343function append_firmware_calls_to_makefiles() {
1344 cat << EOF >> "$ANDROIDMK"
1345ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1346
1347RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1348\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1349 \$(call add-radio-file,radio/\$(f)))
1350\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1351
1352endif
1353
1354EOF
1355}
1356
1357#
1358# get_file:
1359#
1360# $1: input file
1361# $2: target file/folder
1362# $3: source of the file (can be "adb" or a local folder)
1363#
1364# Silently extracts the input file to defined target
1365# Returns success if file can be pulled from the device or found locally
1366#
1367function get_file() {
1368 local SRC="$3"
1369
1370 if [ "$SRC" = "adb" ]; then
1371 # try to pull
LuK13370f7f0d12022-08-19 21:49:56 +02001372 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1373 adb pull "${1#/system}" "$2" >/dev/null 2>&1 && return 0
1374 adb pull "system/$1" "$2" >/dev/null 2>&1 && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001375
1376 return 1
1377 else
1378 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001379 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1380 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001381 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001382
LuK1337dbb77cc2023-12-04 19:03:10 +01001383 # try /vendor/odm for devices without /odm partition
1384 [[ "$1" == /system/odm/* ]] && cp -r "$SRC/vendor/${1#/system}" "$2" 2>/dev/null && return 0
1385
Steve Kondik5bd66602016-07-15 10:39:58 -07001386 return 1
1387 fi
1388};
1389
1390#
1391# oat2dex:
1392#
1393# $1: extracted apk|jar (to check if deodex is required)
1394# $2: odexed apk|jar to deodex
1395# $3: source of the odexed apk|jar
1396#
1397# Convert apk|jar .odex in the corresposing classes.dex
1398#
1399function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001400 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001401 local OEM_TARGET="$2"
1402 local SRC="$3"
1403 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001404 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001405
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001406 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1407 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1408 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001409 fi
1410
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001411 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001412 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001413 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001414
codeworkx85eda752018-09-23 12:36:57 +02001415 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001416 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001417 fi
1418
Steve Kondik5bd66602016-07-15 10:39:58 -07001419 # Extract existing boot.oats to the temp folder
1420 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001421 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001422 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301423 mkdir -p "$EXTRACT_TMP_DIR/system/framework/$ARCH"
1424 if get_file "/system/framework/$ARCH" "$EXTRACT_TMP_DIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001425 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001426 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301427 rmdir "$EXTRACT_TMP_DIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001428 fi
1429 done
1430 fi
1431
1432 if [ -z "$ARCHES" ]; then
1433 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1434 fi
1435
theimpulson9a911af2019-08-14 03:25:12 +00001436 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001437 return;
1438 fi
1439
theimpulson9a911af2019-08-14 03:25:12 +00001440 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001441 return 0 # target apk|jar is already odexed, return
1442 fi
1443
1444 for ARCH in $ARCHES; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301445 BOOTOAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001446
Joe Maplesfb3941c2018-01-05 14:51:33 -05001447 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1448 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001449
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301450 if get_file "$OAT" "$EXTRACT_TMP_DIR" "$SRC"; then
1451 if get_file "$VDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1452 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$VDEX")" > /dev/null
1453 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${OEM_TARGET%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001454 for CLASS in $CLASSES; do
1455 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1456 # Check if we have to deal with CompactDex
1457 if [[ "$CLASS" == *.cdex ]]; then
1458 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301459 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001460 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301461 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001462 fi
1463 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001464 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301465 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$EXTRACT_TMP_DIR/$(basename "$OAT")"
1466 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001467 fi
theimpulson9a911af2019-08-14 03:25:12 +00001468 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301469 JAROAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001470 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001471 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001472 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001473 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001474 # try to extract classes.dex from boot.vdex for frameworks jars
1475 # fallback to boot.oat if vdex is not available
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301476 if get_file "$JARVDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1477 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$JARVDEX")" > /dev/null
1478 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${JARVDEX%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001479 for CLASS in $CLASSES; do
1480 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1481 # Check if we have to deal with CompactDex
1482 if [[ "$CLASS" == *.cdex ]]; then
1483 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301484 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001485 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301486 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001487 fi
1488 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001489 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301490 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$JAROAT/$OEM_TARGET"
1491 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001492 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001493 else
1494 continue
1495 fi
1496
Steve Kondik5bd66602016-07-15 10:39:58 -07001497 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001498
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301499 rm -rf "$EXTRACT_TMP_DIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001500}
1501
1502#
1503# init_adb_connection:
1504#
1505# Starts adb server and waits for the device
1506#
1507function init_adb_connection() {
1508 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1509 if ! _adb_connected; then
1510 echo "No device is online. Waiting for one..."
1511 echo "Please connect USB and/or enable USB debugging"
1512 until _adb_connected; do
1513 sleep 1
1514 done
1515 echo "Device Found."
1516 fi
1517
1518 # Retrieve IP and PORT info if we're using a TCP connection
Chirayu Desaif1a21302022-09-13 00:29:33 +05301519 TCPIPPORT=$(adb devices | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
Steve Kondik5bd66602016-07-15 10:39:58 -07001520 | head -1 | awk '{print $1}')
1521 adb root &> /dev/null
1522 sleep 0.3
1523 if [ -n "$TCPIPPORT" ]; then
1524 # adb root just killed our connection
1525 # so reconnect...
1526 adb connect "$TCPIPPORT"
1527 fi
1528 adb wait-for-device &> /dev/null
1529 sleep 0.3
1530}
1531
1532#
1533# fix_xml:
1534#
1535# $1: xml file to fix
1536#
1537function fix_xml() {
1538 local XML="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301539 local TEMP_XML="$EXTRACT_TMP_DIR/`basename "$XML"`.temp"
Steve Kondik5bd66602016-07-15 10:39:58 -07001540
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001541 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1542 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001543
1544 mv "$TEMP_XML" "$XML"
1545}
1546
Vladimir Olteande985fe2019-01-17 03:07:34 +02001547function get_hash() {
1548 local FILE="$1"
1549
1550 if [ "$(uname)" == "Darwin" ]; then
1551 shasum "${FILE}" | awk '{print $1}'
1552 else
1553 sha1sum "${FILE}" | awk '{print $1}'
1554 fi
1555}
1556
Vladimir Olteana7d20492019-01-17 03:05:52 +02001557function print_spec() {
1558 local SPEC_PRODUCT_PACKAGE="$1"
1559 local SPEC_SRC_FILE="$2"
1560 local SPEC_DST_FILE="$3"
1561 local SPEC_ARGS="$4"
1562 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001563 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001564
1565 local PRODUCT_PACKAGE=""
1566 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1567 PRODUCT_PACKAGE="-"
1568 fi
1569 local SRC=""
1570 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1571 SRC="${SPEC_SRC_FILE}:"
1572 fi
1573 local DST=""
1574 if [ ! -z "${SPEC_DST_FILE}" ]; then
1575 DST="${SPEC_DST_FILE}"
1576 fi
1577 local ARGS=""
1578 if [ ! -z "${SPEC_ARGS}" ]; then
1579 ARGS=";${SPEC_ARGS}"
1580 fi
1581 local HASH=""
1582 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1583 HASH="|${SPEC_HASH}"
1584 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001585 local FIXUP_HASH=""
1586 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1587 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1588 fi
1589 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1590}
1591
1592# To be overridden by device-level extract-files.sh
1593# Parameters:
1594# $1: spec name of a blob. Can be used for filtering.
1595# If the spec is "src:dest", then $1 is "dest".
1596# If the spec is "src", then $1 is "src".
1597# $2: path to blob file. Can be used for fixups.
1598#
1599function blob_fixup() {
1600 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001601}
1602
Steve Kondik5bd66602016-07-15 10:39:58 -07001603#
1604# extract:
1605#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001606# Positional parameters:
1607# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001608# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001609# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1610#
1611# Non-positional parameters (coming after $2):
1612# --section: preferred way of selecting the portion to parse and extract from
1613# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001614# --kang: if present, this option will activate the printing of hashes for the
1615# extracted blobs. Useful with --section for subsequent pinning of
1616# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001617#
1618function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001619 # Consume positional parameters
1620 local PROPRIETARY_FILES_TXT="$1"; shift
1621 local SRC="$1"; shift
1622 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001623 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001624
1625 # Consume optional, non-positional parameters
1626 while [ "$#" -gt 0 ]; do
1627 case "$1" in
1628 -s|--section)
1629 SECTION="$2"; shift
1630 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001631 -k|--kang)
1632 KANG=true
1633 DISABLE_PINNING=1
1634 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001635 *)
1636 # Backwards-compatibility with the old behavior, where $3, if
1637 # present, denoted an optional positional ${SECTION} argument.
1638 # Users of ${SECTION} are encouraged to migrate from setting it as
1639 # positional $3, to non-positional --section ${SECTION}, the
1640 # reason being that it doesn't scale to have more than 1 optional
1641 # positional argument.
1642 SECTION="$1"
1643 ;;
1644 esac
1645 shift
1646 done
1647
Steve Kondik5bd66602016-07-15 10:39:58 -07001648 if [ -z "$OUTDIR" ]; then
1649 echo "Output dir not set!"
1650 exit 1
1651 fi
1652
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001653 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001654
1655 # Allow failing, so we can try $DEST and/or $FILE
1656 set +e
1657
1658 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1659 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001660 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001661 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001662 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001663 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301664 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001665
1666 if [ "$SRC" = "adb" ]; then
1667 init_adb_connection
1668 fi
1669
Dan Pasanen0cc05012017-03-21 09:06:11 -05001670 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301671 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001672
1673 # Check if we're working with the same zip that was passed last time.
1674 # If so, let's just use what's already extracted.
1675 MD5=`md5sum "$SRC"| awk '{print $1}'`
1676 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1677
1678 if [ "$MD5" != "$OLDMD5" ]; then
1679 rm -rf "$DUMPDIR"
1680 mkdir "$DUMPDIR"
1681 unzip "$SRC" -d "$DUMPDIR"
1682 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1683
1684 # Stop if an A/B OTA zip is detected. We cannot extract these.
1685 if [ -a "$DUMPDIR"/payload.bin ]; then
1686 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1687 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001688 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001689
Luca Stefani776be462020-09-09 15:53:58 +02001690 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001691 do
1692 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001693 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1694 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1695 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1696 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1697 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001698 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1699 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1700 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1701 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1702 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
Chirayu Desai62ed12a2021-11-26 05:47:25 +05301703 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1704 rm "$DUMPDIR"/"$PARTITION".img
dianlujitao85ddca62020-04-21 23:03:20 +08001705 fi
1706 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001707 fi
1708
1709 SRC="$DUMPDIR"
1710 fi
1711
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301712 if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301713 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301714 mkdir -p "$DUMPDIR"
1715
1716 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1717 do
1718 echo "Extracting "$PARTITION""
1719 local IMAGE="$SRC"/"$PARTITION".img
1720 if [ -f "$IMAGE" ]; then
1721 if [[ $(file -b "$IMAGE") == Linux* ]]; then
1722 extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
1723 elif [[ $(file -b "$IMAGE") == Android* ]]; then
1724 simg2img "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
1725 extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
1726 else
1727 echo "Unsupported "$IMAGE""
1728 fi
1729 fi
1730 done
1731
1732 SRC="$DUMPDIR"
1733 fi
1734
Steve Kondik5bd66602016-07-15 10:39:58 -07001735 if [ "$VENDOR_STATE" -eq "0" ]; then
1736 echo "Cleaning output directory ($OUTPUT_ROOT).."
1737 rm -rf "${OUTPUT_TMP:?}"
1738 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001739 if [ -d "$OUTPUT_ROOT" ]; then
1740 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1741 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001742 VENDOR_STATE=1
1743 fi
1744
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001745 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001746
1747 for (( i=1; i<COUNT+1; i++ )); do
1748
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001749 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001750 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001751 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001752 local OUTPUT_DIR=
1753 local TMP_DIR=
1754 local SRC_FILE=
1755 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001756 local IS_PRODUCT_PACKAGE=false
1757
1758 # Note: this relies on the fact that the ${FILELIST[@]} array
1759 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1760 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1761 IS_PRODUCT_PACKAGE=true
1762 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001763
Michael Bestasbda30202020-12-28 04:44:52 +02001764 OUTPUT_DIR="${OUTPUT_ROOT}"
1765 TMP_DIR="${OUTPUT_TMP}"
1766 SRC_FILE="/system/${SPEC_SRC_FILE}"
1767 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001768
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001769 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001770 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001771 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001772 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001773
Gabriele M58270a32017-11-13 23:15:29 +01001774 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001775 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001776 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001777 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001778 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001779 if [ -f "${VENDOR_REPO_FILE}" ]; then
1780 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001781 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001782 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001783 fi
1784 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001785 local TMP_HASH=$(get_hash "${PINNED}")
1786 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001787 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001788 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1789 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001790 fi
1791 fi
1792 fi
1793 fi
1794
Vladimir Olteana7d20492019-01-17 03:05:52 +02001795 if [ "${KANG}" = false ]; then
1796 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1797 fi
1798
Gabriele M58270a32017-11-13 23:15:29 +01001799 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001800 if [ "${FIXUP_HASH}" != "x" ]; then
1801 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1802 else
1803 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1804 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001805 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001806 FOUND=false
1807 # Try Lineage target first.
1808 # Also try to search for files stripped of
1809 # the "/system" prefix, if we're actually extracting
1810 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001811 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001812 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1813 FOUND=true
1814 break
1815 }
1816 done
1817
1818 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001819 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001820 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001821 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001822
Arian5f98d792021-09-09 15:24:25 +02001823 # Blob fixup pipeline has 2 parts: one that is fixed and
1824 # one that is user-configurable
1825 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1826 # Deodex apk|jar if that's the case
1827 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1828 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301829 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
1830 touch -t 200901010000 "$EXTRACT_TMP_DIR/classes"*
1831 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
1832 rm "$EXTRACT_TMP_DIR/classes"*
Arian5f98d792021-09-09 15:24:25 +02001833 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1834 fi
1835 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1836 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001837 fi
Arian5f98d792021-09-09 15:24:25 +02001838 # Now run user-supplied fixup function
1839 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1840 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001841
Arian5f98d792021-09-09 15:24:25 +02001842 if [ -f "${VENDOR_REPO_FILE}" ]; then
1843 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1844 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001845 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001846 chmod 755 "${VENDOR_REPO_FILE}"
1847 else
1848 chmod 644 "${VENDOR_REPO_FILE}"
1849 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001850 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001851
Arian5f98d792021-09-09 15:24:25 +02001852 if [ "${KANG}" = true ]; then
1853 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1854 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001855
Arian5f98d792021-09-09 15:24:25 +02001856 # Check and print whether the fixup pipeline actually did anything.
1857 # This isn't done right after the fixup pipeline because we want this print
1858 # to come after print_spec above, when in kang mode.
1859 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1860 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1861 # Now sanity-check the spec for this blob.
1862 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1863 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1864 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1865 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001866 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001867 fi
1868
Steve Kondik5bd66602016-07-15 10:39:58 -07001869 done
1870
1871 # Don't allow failing
1872 set -e
1873}
1874
1875#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001876# extract2:
1877#
1878# Positional parameters:
1879# $1: file containing the list of items to extract (aka proprietary-files.txt)
1880#
1881# Non-positional parameters (coming after $2):
1882# --section: selects the portion to parse and extracts from proprietary-files.txt
1883# --kang: if present, this option will activate the printing of hashes for the
1884# extracted blobs. Useful with --section for subsequent pinning of
1885# blobs taken from other origins.
1886#
1887function extract2() {
1888 # Consume positional parameters
1889 local PROPRIETARY_FILES_TXT="$1"; shift
1890 local SECTION=""
1891 local KANG=false
1892
1893 # Consume optional, non-positional parameters
1894 while [ "$#" -gt 0 ]; do
1895 case "$1" in
1896 --adb)
1897 ADB=true
1898 ;;
1899 --system)
1900 SYSTEM_SRC="$2"; shift
1901 ;;
1902 --vendor)
1903 VENDOR_SRC="$2"; shift
1904 ;;
1905 --odm)
1906 ODM_SRC="$2"; shift
1907 ;;
1908 --product)
1909 PRODUCT_SRC="$2"; shift
1910 ;;
1911 -s|--section)
1912 SECTION="$2"; shift
1913 ;;
1914 -k|--kang)
1915 KANG=true
1916 DISABLE_PINNING=1
1917 ;;
1918 esac
1919 shift
1920 done
1921
1922 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1923 echo "No sources set! You must select --adb or pass paths to partition dumps."
1924 exit 1
1925 fi
1926
1927 if [ -z "$OUTDIR" ]; then
1928 echo "Output dir not set!"
1929 exit 1
1930 fi
1931
1932 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1933
1934 # Allow failing, so we can try $DEST and/or $FILE
1935 set +e
1936
1937 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1938 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1939 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1940 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1941 local COUNT=${#FILELIST[@]}
1942 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301943 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001944
1945 if [ "$ADB" = true ]; then
1946 init_adb_connection
1947 fi
1948
1949 if [ "$VENDOR_STATE" -eq "0" ]; then
1950 echo "Cleaning output directory ($OUTPUT_ROOT).."
1951 rm -rf "${OUTPUT_TMP:?}"
1952 mkdir -p "${OUTPUT_TMP:?}"
1953 if [ -d "$OUTPUT_ROOT" ]; then
1954 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1955 fi
1956 VENDOR_STATE=1
1957 fi
1958
1959 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1960
1961 for (( i=1; i<COUNT+1; i++ )); do
1962
1963 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1964 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1965 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1966 local OUTPUT_DIR=
1967 local TMP_DIR=
1968 local SRC_FILE=
1969 local DST_FILE=
1970 local IS_PRODUCT_PACKAGE=false
1971
1972 # Note: this relies on the fact that the ${FILELIST[@]} array
1973 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1974 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1975 IS_PRODUCT_PACKAGE=true
1976 fi
1977
1978 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1979 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1980 TMP_DIR="${OUTPUT_TMP}/rootfs"
1981 else
1982 OUTPUT_DIR="${OUTPUT_ROOT}"
1983 TMP_DIR="${OUTPUT_TMP}"
1984 fi
1985 SRC_FILE="${SPEC_SRC_FILE}"
1986 DST_FILE="${SPEC_DST_FILE}"
1987
1988 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1989 local BLOB_DISPLAY_NAME="${DST_FILE}"
1990 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1991
1992 # Check pinned files
1993 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1994 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1995 local KEEP=""
1996 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1997 if [ -f "${VENDOR_REPO_FILE}" ]; then
1998 local PINNED="${VENDOR_REPO_FILE}"
1999 else
2000 local PINNED="${TMP_DIR}${DST_FILE}"
2001 fi
2002 if [ -f "$PINNED" ]; then
2003 local TMP_HASH=$(get_hash "${PINNED}")
2004 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
2005 KEEP="1"
2006 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
2007 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
2008 fi
2009 fi
2010 fi
2011 fi
2012
2013 if [ "${KANG}" = false ]; then
2014 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
2015 fi
2016
2017 if [ "$KEEP" = "1" ]; then
2018 printf ' + keeping pinned file with hash %s\n' "${HASH}"
2019 else
2020 FOUND=false
2021 PARTITION_SOURCE_DIR=
2022 # Try Lineage target first.
2023 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
2024 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
2025 if [ "$PARTITION" = "system" ]; then
2026 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
2027 elif [ "$PARTITION" = "vendor" ]; then
2028 PARTITION_SOURCE_DIR="$VENDOR_SRC"
2029 elif [ "$PARTITION" = "product" ]; then
2030 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
2031 elif [ "$PARTITION" = "odm" ]; then
2032 PARTITION_SOURCE_DIR="$ODM_SRC"
2033 fi
2034 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
2035 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2036 FOUND=true
2037 break
2038 }
2039 # Search with the full system/ prefix if the file was not found on the system partition
2040 # because we may be searching in a mounted system-as-root system.img
2041 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
2042 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2043 FOUND=true
2044 break
2045 }
2046 fi
2047 done
2048
2049 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
2050 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
2051 fi
2052
2053 if [ "${FOUND}" = false ]; then
2054 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
2055 continue
2056 fi
2057 fi
2058
2059 # Blob fixup pipeline has 2 parts: one that is fixed and
2060 # one that is user-configurable
2061 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2062 # Deodex apk|jar if that's the case
2063 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
2064 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302065 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
2066 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
2067 rm "$EXTRACT_TMP_DIR/classes"*
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04002068 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2069 fi
2070 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2071 fix_xml "${VENDOR_REPO_FILE}"
2072 fi
2073 # Now run user-supplied fixup function
2074 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2075 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2076
2077 if [ -f "${VENDOR_REPO_FILE}" ]; then
2078 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2079 local TYPE="${DIR##*/}"
2080 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
2081 chmod 755 "${VENDOR_REPO_FILE}"
2082 else
2083 chmod 644 "${VENDOR_REPO_FILE}"
2084 fi
2085 fi
2086
2087 if [ "${KANG}" = true ]; then
2088 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2089 fi
2090
2091 # Check and print whether the fixup pipeline actually did anything.
2092 # This isn't done right after the fixup pipeline because we want this print
2093 # to come after print_spec above, when in kang mode.
2094 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2095 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2096 # Now sanity-check the spec for this blob.
2097 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2098 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
2099 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
2100 fi
2101 fi
2102
2103 done
2104
2105 # Don't allow failing
2106 set -e
2107}
2108
2109#
Steve Kondik5bd66602016-07-15 10:39:58 -07002110# extract_firmware:
2111#
2112# $1: file containing the list of items to extract
2113# $2: path to extracted radio folder
2114#
2115function extract_firmware() {
2116 if [ -z "$OUTDIR" ]; then
2117 echo "Output dir not set!"
2118 exit 1
2119 fi
2120
2121 parse_file_list "$1"
2122
2123 # Don't allow failing
2124 set -e
2125
2126 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
2127 local COUNT=${#FILELIST[@]}
2128 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00002129 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07002130
2131 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
2132 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
2133 rm -rf "${OUTPUT_DIR:?}/"*
2134 VENDOR_RADIO_STATE=1
2135 fi
2136
2137 echo "Extracting $COUNT files in $1 from $SRC:"
2138
2139 for (( i=1; i<COUNT+1; i++ )); do
2140 local FILE="${FILELIST[$i-1]}"
2141 printf ' - %s \n' "/radio/$FILE"
2142
2143 if [ ! -d "$OUTPUT_DIR" ]; then
2144 mkdir -p "$OUTPUT_DIR"
2145 fi
2146 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
2147 chmod 644 "$OUTPUT_DIR/$FILE"
2148 done
2149}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002150
2151function extract_img_data() {
2152 local image_file="$1"
2153 local out_dir="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302154 local logFile="$EXTRACT_TMP_DIR/debugfs.log"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002155
2156 if [ ! -d "$out_dir" ]; then
2157 mkdir -p "$out_dir"
2158 fi
2159
2160 if [[ "$HOST_OS" == "Darwin" ]]; then
2161 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
2162 echo "[-] Failed to extract data from '$image_file'"
2163 abort 1
2164 }
2165 else
2166 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2167 do
2168 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2169 echo "[-] Failed to extract data from '$image_file'"
2170 abort 1
2171 }
2172 done
2173 fi
2174
2175 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2176 if grep -Fq "$symlink_err" "$logFile"; then
2177 echo "[-] Symlinks have not been properly processed from $image_file"
Michael Bestas1b570252021-12-02 21:05:36 +02002178 echo "[!] You might not have a compatible debugfs version"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002179 abort 1
2180 fi
2181}
2182
2183declare -ra VENDOR_SKIP_FILES=(
2184 "bin/toybox_vendor"
2185 "bin/toolbox"
2186 "bin/grep"
2187 "build.prop"
2188 "compatibility_matrix.xml"
2189 "default.prop"
2190 "etc/NOTICE.xml.gz"
2191 "etc/vintf/compatibility_matrix.xml"
2192 "etc/vintf/manifest.xml"
2193 "etc/wifi/wpa_supplicant.conf"
2194 "manifest.xml"
2195 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2196 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2197 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2198 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2199 "overlay/framework-res__auto_generated_rro.apk"
2200 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2201)
2202
2203function array_contains() {
2204 local element
2205 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2206 return 1
2207}
2208
2209function generate_prop_list_from_image() {
2210 local image_file="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302211 local image_dir="$EXTRACT_TMP_DIR/image-temp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002212 local output_list="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302213 local output_list_tmp="$EXTRACT_TMP_DIR/_proprietary-blobs.txt"
Michael Bestasca5d78a2021-12-02 20:58:40 +02002214 local -n skipped_files="$3"
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002215 local component="$4"
2216 local partition="$component"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002217
Chirayu Desai203cc522021-12-04 01:18:45 +05302218 mkdir -p "$image_dir"
2219
2220 if [[ $(file -b "$image_file") == Linux* ]]; then
2221 extract_img_data "$image_file" "$image_dir"
2222 elif [[ $(file -b "$image_file") == Android* ]]; then
2223 simg2img "$image_file" "$image_dir"/"$(basename "$image_file").raw"
2224 extract_img_data "$image_dir"/"$(basename "$image_file").raw" "$image_dir"
2225 rm "$image_dir"/"$(basename "$image_file").raw"
2226 else
2227 echo "Unsupported "$image_file""
2228 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002229
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002230 if [ -z "$component" ]; then
2231 partition="vendor"
2232 elif [[ "$component" == "carriersettings" ]]; then
2233 partition="product"
2234 fi
2235
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002236 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2237 do
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002238 if [[ "$component" == "carriersettings" ]] && ! prefix_match_file "etc/CarrierSettings" "$FILE" ; then
2239 continue
2240 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002241 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2242 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2243 continue
2244 fi
2245 # Skip device defined skipped files since they will be re-generated at build time
Michael Bestasca5d78a2021-12-02 20:58:40 +02002246 if array_contains "$FILE" "${skipped_files[@]}"; then
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002247 continue
2248 fi
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002249 echo "$partition/$FILE" >> "$output_list_tmp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002250 done
2251
2252 # Sort merged file with all lists
Michael Bestas1e6efb32021-11-15 19:28:56 +02002253 LC_ALL=C sort -u "$output_list_tmp" > "$output_list"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002254
2255 # Clean-up
2256 rm -f "$output_list_tmp"
2257}
Bruno Martins0f425f12021-04-10 14:57:32 +01002258
2259function colored_echo() {
2260 IFS=" "
2261 local color=$1;
2262 shift
2263 if ! [[ $color =~ '^[0-9]$' ]] ; then
2264 case $(echo $color | tr '[:upper:]' '[:lower:]') in
2265 black) color=0 ;;
2266 red) color=1 ;;
2267 green) color=2 ;;
2268 yellow) color=3 ;;
2269 blue) color=4 ;;
2270 magenta) color=5 ;;
2271 cyan) color=6 ;;
2272 white|*) color=7 ;; # white or invalid color
2273 esac
2274 fi
Bruno Martins5064db22021-06-21 14:47:40 +01002275 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002276 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01002277 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002278}