blob: c4b5305e3020092808f782fbfca71cb06829b733 [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
Chirayu Desai501ffd62022-03-17 06:27:33 +0530113 export BINARIES_LOCATION="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin
114
115 export SIMG2IMG="$BINARIES_LOCATION"/simg2img
116
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +0300117 if [ -z "$PATCHELF" ]; then
118 export PATCHELF="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/patchelf
119 fi
Michael Bestasf55ac292022-03-23 23:15:23 +0200120
121 if [ -z "$SIGSCAN" ]; then
122 export SIGSCAN="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/SigScan
123 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700124}
125
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300126# Helper functions for parsing a spec.
127# notes: an optional "|SHA1" that may appear in the format is stripped
128# early from the spec in the parse_file_list function, and
129# should not be present inside the input parameter passed
130# to these functions.
131
132#
133# input: spec in the form of "src[:dst][;args]"
134# output: "src"
135#
136function src_file() {
137 local SPEC="$1"
138 local SPLIT=(${SPEC//:/ })
139 local ARGS="$(target_args ${SPEC})"
140 # Regardless of there being a ":" delimiter or not in the spec,
141 # the source file is always either the first, or the only entry.
142 local SRC="${SPLIT[0]}"
143 # Remove target_args suffix, if present
144 echo "${SRC%;${ARGS}}"
145}
146
Steve Kondik5bd66602016-07-15 10:39:58 -0700147#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300148# input: spec in the form of "src[:dst][;args]"
149# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700150#
151function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800152 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300153 local SPLIT=(${SPEC//:/ })
154 local ARGS="$(target_args ${SPEC})"
155 local DST=
156 case ${#SPLIT[@]} in
157 1)
158 # The spec doesn't have a : delimiter
159 DST="${SPLIT[0]}"
160 ;;
161 *)
162 # The spec actually has a src:dst format
163 DST="${SPLIT[1]}"
164 ;;
165 esac
166 # Remove target_args suffix, if present
167 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700168}
169
170#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300171# input: spec in the form of "src[:dst][;args]"
172# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700173#
174function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300175 local SPEC="$1"
176 local SPLIT=(${SPEC//;/ })
177 local ARGS=
178 case ${#SPLIT[@]} in
179 1)
180 # No ";" delimiter in the spec.
181 ;;
182 *)
183 # The "args" are whatever comes after the ";" character.
184 # Basically the spec stripped of whatever is to the left of ";".
185 ARGS="${SPEC#${SPLIT[0]};}"
186 ;;
187 esac
188 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700189}
190
191#
192# prefix_match:
193#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300194# input:
195# - $1: prefix
196# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
197# output:
198# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700199#
200function prefix_match() {
201 local PREFIX="$1"
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100202 local NEW_ARRAY=()
Vladimir Oltean7220f362018-04-02 22:37:09 +0300203 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
204 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700205 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300206 local ARGS=$(target_args "$LINE")
SamarV-1215556e2d2024-03-19 08:50:02 +0530207 if [[ -z "${ARGS}" || "${ARGS}" =~ 'SYMLINK' ]]; then
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100208 NEW_ARRAY+=("${FILE#$PREFIX}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300209 else
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100210 NEW_ARRAY+=("${FILE#$PREFIX};${ARGS}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300211 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700212 fi
213 done
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100214 printf '%s\n' "${NEW_ARRAY[@]}" | LC_ALL=C sort
Steve Kondik5bd66602016-07-15 10:39:58 -0700215}
216
217#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400218# prefix_match_file:
219#
220# $1: the prefix to match on
221# $2: the file to match the prefix for
222#
223# Internal function which returns true if a filename contains the
224# specified prefix.
225#
226function prefix_match_file() {
227 local PREFIX="$1"
228 local FILE="$2"
229 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
230 return 0
231 else
232 return 1
233 fi
234}
235
236#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700237# suffix_match_file:
238#
239# $1: the suffix to match on
240# $2: the file to match the suffix for
241#
242# Internal function which returns true if a filename contains the
243# specified suffix.
244#
245function suffix_match_file() {
246 local SUFFIX="$1"
247 local FILE="$2"
248 if [[ "$FILE" = *"$SUFFIX" ]]; then
249 return 0
250 else
251 return 1
252 fi
253}
254
255#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400256# truncate_file
257#
258# $1: the filename to truncate
259# $2: the argument to output the truncated filename to
260#
261# Internal function which truncates a filename by removing the first dir
262# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
263#
264function truncate_file() {
265 local FILE="$1"
266 RETURN_FILE="$2"
267 local FIND="${FILE%%/*}"
268 local LOCATION="${#FIND}+1"
269 echo ${FILE:$LOCATION}
270}
271
272#
Steve Kondik5bd66602016-07-15 10:39:58 -0700273# write_product_copy_files:
274#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400275# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400276#
Steve Kondik5bd66602016-07-15 10:39:58 -0700277# Creates the PRODUCT_COPY_FILES section in the product makefile for all
278# items in the list which do not start with a dash (-).
279#
280function write_product_copy_files() {
281 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
282 local TARGET=
283 local FILE=
284 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400285 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700286
287 if [ "$COUNT" -eq "0" ]; then
288 return 0
289 fi
290
291 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
292 for (( i=1; i<COUNT+1; i++ )); do
293 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
294 LINEEND=" \\"
295 if [ "$i" -eq "$COUNT" ]; then
296 LINEEND=""
297 fi
298
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300299 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400300 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400301 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400302 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400303 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400304 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400305 local OUTTARGET=$(truncate_file $TARGET)
306 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
307 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200308 elif prefix_match_file "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"
312 elif prefix_match_file "system/system_ext/" $TARGET ; then
313 local OUTTARGET=$(truncate_file $TARGET)
314 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
315 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400316 elif prefix_match_file "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"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400320 elif prefix_match_file "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 "system/vendor/odm/" $TARGET ; then
325 local OUTTARGET=$(truncate_file $TARGET)
326 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
327 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
328 elif prefix_match_file "vendor/" $TARGET ; then
329 local OUTTARGET=$(truncate_file $TARGET)
330 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
331 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Alexander Koskovich44c8fac2022-01-22 22:27:29 -0700332 elif prefix_match_file "vendor_dlkm/" $TARGET ; then
333 local OUTTARGET=$(truncate_file $TARGET)
334 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_DLKM)/%s%s\n' \
335 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400336 elif prefix_match_file "system/vendor/" $TARGET ; then
337 local OUTTARGET=$(truncate_file $TARGET)
338 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
339 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400340 elif prefix_match_file "system/" $TARGET ; then
341 local OUTTARGET=$(truncate_file $TARGET)
342 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
343 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Mohd Farazd683fe42022-07-23 14:33:59 +0200344 elif prefix_match_file "recovery/" $TARGET ; then
345 local OUTTARGET=$(truncate_file $TARGET)
346 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_RECOVERY)/%s%s\n' \
347 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
348 elif prefix_match_file "vendor_ramdisk/" $TARGET ; then
349 local OUTTARGET=$(truncate_file $TARGET)
350 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_RAMDISK)/%s%s\n' \
351 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400352 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400353 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400354 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
355 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700356 done
357 return 0
358}
359
360#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700361# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700362#
363# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200364# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700365# $3: type-specific extra flags
366# $4: Name of the array holding the target list
367#
368# Internal function which writes out the BUILD_PREBUILT stanzas
369# for all modules in the list. This is called by write_product_packages
370# after the modules are categorized.
371#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700372function write_blueprint_packages() {
373
374 local CLASS="$1"
375 local PARTITION="$2"
376 local EXTRA="$3"
377
378 # Yes, this is a horrible hack - we create a new array using indirection
379 local ARR_NAME="$4[@]"
380 local FILELIST=("${!ARR_NAME}")
381
382 local FILE=
383 local ARGS=
384 local BASENAME=
385 local EXTENSION=
386 local PKGNAME=
387 local SRC=
Tim Zimmermannd9320762021-12-23 07:06:17 +0100388 local STEM=
TheStrix6e24acc2020-04-10 18:20:19 +0530389 local OVERRIDEPKG=
SamarV-1217e6b4082024-02-26 14:39:34 +0530390 local REQUIREDPKG=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700391
392 for P in "${FILELIST[@]}"; do
393 FILE=$(target_file "$P")
394 ARGS=$(target_args "$P")
Tim Zimmermannd9320762021-12-23 07:06:17 +0100395 ARGS=(${ARGS//;/ })
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700396
397 BASENAME=$(basename "$FILE")
398 DIRNAME=$(dirname "$FILE")
399 EXTENSION=${BASENAME##*.}
400 PKGNAME=${BASENAME%.*}
401
Tim Zimmermanne93bb1b2021-12-23 10:15:26 +0100402 if [ "$CLASS" = "EXECUTABLES" ] && [ "$EXTENSION" != "sh" ]; then
403 PKGNAME="$BASENAME"
404 EXTENSION=""
405 fi
406
Tim Zimmermannd9320762021-12-23 07:06:17 +0100407 # Allow overriding module name
408 STEM=
409 for ARG in "${ARGS[@]}"; do
410 if [[ "$ARG" =~ "MODULE" ]]; then
411 STEM="$PKGNAME"
412 PKGNAME=${ARG#*=}
413 fi
414 done
415
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700416 # Add to final package list
417 PACKAGE_LIST+=("$PKGNAME")
418
419 SRC="proprietary"
420 if [ "$PARTITION" = "system" ]; then
421 SRC+="/system"
422 elif [ "$PARTITION" = "vendor" ]; then
423 SRC+="/vendor"
424 elif [ "$PARTITION" = "product" ]; then
425 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200426 elif [ "$PARTITION" = "system_ext" ]; then
427 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700428 elif [ "$PARTITION" = "odm" ]; then
429 SRC+="/odm"
430 fi
431
432 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
433 printf 'cc_prebuilt_library_shared {\n'
434 printf '\tname: "%s",\n' "$PKGNAME"
Tim Zimmermannd9320762021-12-23 07:06:17 +0100435 if [ ! -z "$STEM" ]; then
436 printf '\tstem: "%s",\n' "$STEM"
437 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700438 printf '\towner: "%s",\n' "$VENDOR"
439 printf '\tstrip: {\n'
440 printf '\t\tnone: true,\n'
441 printf '\t},\n'
442 printf '\ttarget: {\n'
443 if [ "$EXTRA" = "both" ]; then
444 printf '\t\tandroid_arm: {\n'
445 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
446 printf '\t\t},\n'
447 printf '\t\tandroid_arm64: {\n'
448 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
449 printf '\t\t},\n'
450 elif [ "$EXTRA" = "64" ]; then
451 printf '\t\tandroid_arm64: {\n'
452 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
453 printf '\t\t},\n'
454 else
455 printf '\t\tandroid_arm: {\n'
456 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
457 printf '\t\t},\n'
458 fi
459 printf '\t},\n'
460 if [ "$EXTRA" != "none" ]; then
461 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
462 fi
dianlujitao848101c2020-09-12 00:15:13 +0800463 printf '\tcheck_elf_files: false,\n'
Chirayu Desaia7741012021-12-04 07:17:28 +0530464 elif [ "$CLASS" = "APEX" ]; then
465 printf 'prebuilt_apex {\n'
466 printf '\tname: "%s",\n' "$PKGNAME"
467 printf '\towner: "%s",\n' "$VENDOR"
468 SRC="$SRC/apex"
469 printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
470 printf '\tfilename: "%s",\n' "$FILE"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700471 elif [ "$CLASS" = "APPS" ]; then
472 printf 'android_app_import {\n'
473 printf '\tname: "%s",\n' "$PKGNAME"
474 printf '\towner: "%s",\n' "$VENDOR"
475 if [ "$EXTRA" = "priv-app" ]; then
476 SRC="$SRC/priv-app"
477 else
478 SRC="$SRC/app"
479 fi
480 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
LuK1337508e85f2021-08-23 18:18:57 +0200481 USE_PLATFORM_CERTIFICATE="true"
482 for ARG in "${ARGS[@]}"; do
483 if [ "$ARG" = "PRESIGNED" ]; then
484 USE_PLATFORM_CERTIFICATE="false"
Michael Bestasab47e912024-03-06 13:32:05 +0200485 printf '\tpreprocessed: true,\n'
LuK1337508e85f2021-08-23 18:18:57 +0200486 printf '\tpresigned: true,\n'
487 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
488 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200489 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200490 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
SamarV-1217e6b4082024-02-26 14:39:34 +0530491 elif [[ "$ARG" =~ "REQUIRED" ]]; then
492 REQUIREDPKG=${ARG#*=}
493 REQUIRED_PACKAGES_LIST+="$REQUIREDPKG,"
494 printf '\trequired: ["%s"],\n' "${REQUIREDPKG//,/\", \"}"
SamarV-1215556e2d2024-03-19 08:50:02 +0530495 elif [[ "$ARG" =~ "SYMLINK" ]]; then
496 continue
LuK1337508e85f2021-08-23 18:18:57 +0200497 elif [ ! -z "$ARG" ]; then
498 USE_PLATFORM_CERTIFICATE="false"
499 printf '\tcertificate: "%s",\n' "$ARG"
500 fi
501 done
502 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700503 printf '\tcertificate: "platform",\n'
504 fi
505 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
506 printf 'dex_import {\n'
507 printf '\tname: "%s",\n' "$PKGNAME"
508 printf '\towner: "%s",\n' "$VENDOR"
509 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
510 elif [ "$CLASS" = "ETC" ]; then
511 if [ "$EXTENSION" = "xml" ]; then
512 printf 'prebuilt_etc_xml {\n'
513 else
514 printf 'prebuilt_etc {\n'
515 fi
516 printf '\tname: "%s",\n' "$PKGNAME"
517 printf '\towner: "%s",\n' "$VENDOR"
518 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200519 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700520 elif [ "$CLASS" = "EXECUTABLES" ]; then
521 if [ "$EXTENSION" = "sh" ]; then
522 printf 'sh_binary {\n'
523 else
524 printf 'cc_prebuilt_binary {\n'
525 fi
526 printf '\tname: "%s",\n' "$PKGNAME"
527 printf '\towner: "%s",\n' "$VENDOR"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200528 if [ "$EXTENSION" != "sh" ]; then
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200529 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200530 printf '\tcheck_elf_files: false,\n'
Tim Zimmermann54606d62021-12-23 09:26:54 +0100531 printf '\tstrip: {\n'
532 printf '\t\tnone: true,\n'
533 printf '\t},\n'
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200534 printf '\tprefer: true,\n'
535 else
536 printf '\tsrc: "%s/bin/%s",\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200537 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700538 unset EXTENSION
539 else
540 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
541 fi
542 if [ "$CLASS" = "APPS" ]; then
543 printf '\tdex_preopt: {\n'
544 printf '\t\tenabled: false,\n'
545 printf '\t},\n'
Jyotiraditya Panda45f50af2024-02-19 05:35:33 +0900546 if [ "$DIRNAME" != "." ] && [[ "$DIRNAME" == */* ]]; then
547 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
548 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700549 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200550 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700551 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200552 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700553 fi
554 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200555 if [ "$CLASS" = "ETC" ] ; then
556 if [ "$DIRNAME" != "." ]; then
557 printf '\tsub_dir: "%s",\n' "$DIRNAME"
558 fi
559 fi
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200560 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700561 printf '\tprefer: true,\n'
562 fi
563 if [ "$EXTRA" = "priv-app" ]; then
564 printf '\tprivileged: true,\n'
565 fi
566 if [ "$PARTITION" = "vendor" ]; then
567 printf '\tsoc_specific: true,\n'
568 elif [ "$PARTITION" = "product" ]; then
569 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200570 elif [ "$PARTITION" = "system_ext" ]; then
571 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700572 elif [ "$PARTITION" = "odm" ]; then
573 printf '\tdevice_specific: true,\n'
574 fi
575 printf '}\n\n'
576 done
577}
578
579#
Steve Kondik5bd66602016-07-15 10:39:58 -0700580# write_product_packages:
581#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700582# This function will create prebuilt entries in the
583# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700584# product makefile for all files in the blob list which
585# start with a single dash (-) character.
586#
587function write_product_packages() {
588 PACKAGE_LIST=()
589
Chenyang Zhongc487f382022-02-10 21:40:41 -0500590 # Sort the package list for comm
591 PRODUCT_PACKAGES_LIST=($( printf '%s\n' "${PRODUCT_PACKAGES_LIST[@]}" | LC_ALL=C sort))
592
Steve Kondik5bd66602016-07-15 10:39:58 -0700593 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
594
595 if [ "$COUNT" = "0" ]; then
596 return 0
597 fi
598
599 # Figure out what's 32-bit, what's 64-bit, and what's multilib
600 # I really should not be doing this in bash due to shitty array passing :(
601 local T_LIB32=( $(prefix_match "lib/") )
602 local T_LIB64=( $(prefix_match "lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500603 local MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
604 local LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
605 local LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik5bd66602016-07-15 10:39:58 -0700606
607 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700608 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700609 fi
610 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700611 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700612 fi
613 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700614 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700615 fi
616
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400617 local T_S_LIB32=( $(prefix_match "system/lib/") )
618 local T_S_LIB64=( $(prefix_match "system/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500619 local S_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
620 local S_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
621 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 -0400622
623 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700624 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400625 fi
626 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700627 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400628 fi
629 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700630 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400631 fi
632
Steve Kondik5bd66602016-07-15 10:39:58 -0700633 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
634 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500635 local V_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
636 local V_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
637 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 -0700638
639 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700640 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700641 fi
642 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700643 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700644 fi
645 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700646 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500647 fi
648
649 local T_P_LIB32=( $(prefix_match "product/lib/") )
650 local T_P_LIB64=( $(prefix_match "product/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500651 local P_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
652 local P_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
653 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 -0500654
655 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700656 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500657 fi
658 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700659 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500660 fi
661 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700662 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700663 fi
664
Luca Stefani776be462020-09-09 15:53:58 +0200665 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
666 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500667 local SE_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
668 local SE_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
669 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 +0200670
671 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
672 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
673 fi
674 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
675 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
676 fi
677 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
678 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
679 fi
680
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700681 local T_O_LIB32=( $(prefix_match "odm/lib/") )
682 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500683 local O_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
684 local O_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
685 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 -0700686
687 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700688 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700689 fi
690 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700691 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700692 fi
693 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700694 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700695 fi
696
Chirayu Desaia7741012021-12-04 07:17:28 +0530697 # APEX
698 local APEX=( $(prefix_match "apex/") )
699 if [ "${#APEX[@]}" -gt "0" ]; then
700 write_blueprint_packages "APEX" "" "" "APEX" >> "$ANDROIDBP"
701 fi
702 local S_APEX=( $(prefix_match "system/apex/") )
703 if [ "${#S_APEX[@]}" -gt "0" ]; then
704 write_blueprint_packages "APEX" "system" "" "S_APEX" >> "$ANDROIDBP"
705 fi
706 local V_APEX=( $(prefix_match "vendor/apex/") )
707 if [ "${#V_APEX[@]}" -gt "0" ]; then
708 write_blueprint_packages "APEX" "vendor" "" "V_APEX" >> "$ANDROIDBP"
709 fi
710 local SE_APEX=( $(prefix_match "system_ext/apex/") )
711 if [ "${#SE_APEX[@]}" -gt "0" ]; then
712 write_blueprint_packages "APEX" "system_ext" "" "SE_APEX" >> "$ANDROIDBP"
713 fi
714
Steve Kondik5bd66602016-07-15 10:39:58 -0700715 # Apps
716 local APPS=( $(prefix_match "app/") )
717 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100718 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700719 fi
720 local PRIV_APPS=( $(prefix_match "priv-app/") )
721 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100722 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700723 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400724 local S_APPS=( $(prefix_match "system/app/") )
725 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100726 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400727 fi
728 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
729 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100730 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400731 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700732 local V_APPS=( $(prefix_match "vendor/app/") )
733 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100734 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700735 fi
736 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
737 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100738 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500739 fi
740 local P_APPS=( $(prefix_match "product/app/") )
741 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100742 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500743 fi
744 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
745 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100746 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700747 fi
Luca Stefani776be462020-09-09 15:53:58 +0200748 local SE_APPS=( $(prefix_match "system_ext/app/") )
749 if [ "${#SE_APPS[@]}" -gt "0" ]; then
750 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
751 fi
752 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
753 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
754 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
755 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700756 local O_APPS=( $(prefix_match "odm/app/") )
757 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100758 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700759 fi
760 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
761 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100762 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700763 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700764
765 # Framework
766 local FRAMEWORK=( $(prefix_match "framework/") )
767 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700768 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700769 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400770 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
771 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700772 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400773 fi
Christian Oder974b5902017-10-08 23:15:52 +0200774 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200775 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700776 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500777 fi
778 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
779 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700780 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200781 fi
Luca Stefani776be462020-09-09 15:53:58 +0200782 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700783 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200784 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
785 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700786 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
787 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700788 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700789 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700790
791 # Etc
792 local ETC=( $(prefix_match "etc/") )
793 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700794 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700795 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400796 local S_ETC=( $(prefix_match "system/etc/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200797 if [ "${#S_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700798 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400799 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700800 local V_ETC=( $(prefix_match "vendor/etc/") )
801 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700802 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500803 fi
804 local P_ETC=( $(prefix_match "product/etc/") )
805 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700806 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700807 fi
Luca Stefani776be462020-09-09 15:53:58 +0200808 local SE_ETC=( $(prefix_match "system_ext/etc/") )
809 if [ "${#SE_ETC[@]}" -gt "0" ]; then
810 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
811 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700812 local O_ETC=( $(prefix_match "odm/etc/") )
813 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700814 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700815 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700816
817 # Executables
818 local BIN=( $(prefix_match "bin/") )
819 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700820 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700821 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400822 local S_BIN=( $(prefix_match "system/bin/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200823 if [ "${#S_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700824 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400825 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700826 local V_BIN=( $(prefix_match "vendor/bin/") )
827 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700828 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500829 fi
830 local P_BIN=( $(prefix_match "product/bin/") )
831 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700832 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700833 fi
Luca Stefani776be462020-09-09 15:53:58 +0200834 local SE_BIN=( $(prefix_match "system_ext/bin/") )
835 if [ "${#SE_BIN[@]}" -gt "0" ]; then
836 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
837 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700838 local O_BIN=( $(prefix_match "odm/bin/") )
839 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700840 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700841 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700842
SamarV-1215556e2d2024-03-19 08:50:02 +0530843 write_package_definition "${PACKAGE_LIST[@]}" >> "$PRODUCTMK"
844}
Steve Kondik5bd66602016-07-15 10:39:58 -0700845
SamarV-1215556e2d2024-03-19 08:50:02 +0530846
847#
848# write_symlink_packages:
849#
850# Creates symlink entries in the Android.bp and related PRODUCT_PACKAGES
851# list in the product makefile for all files in the blob list which has
852# SYMLINK argument.
853#
854function write_symlink_packages() {
855 local FILE=
856 local ARGS=
857 local ARCH=
858 local BASENAME=
859 local PKGNAME=
860 local PREFIX=
861 local SYMLINK_BASENAME=
862 local SYMLINK_PACKAGES=()
863
864 # Sort the symlinks list for comm
865 PRODUCT_SYMLINKS_LIST=($( printf '%s\n' "${PRODUCT_SYMLINKS_LIST[@]}" | LC_ALL=C sort))
866
867 local COUNT=${#PRODUCT_SYMLINKS_LIST[@]}
868
869 if [ "$COUNT" = "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700870 return 0
871 fi
872
SamarV-1215556e2d2024-03-19 08:50:02 +0530873 for LINE in "${PRODUCT_SYMLINKS_LIST[@]}"; do
Bruno Martinsbe7f3cb2024-06-23 15:54:02 +0100874 FILE=$(target_file "$LINE")
Bruno Martinsf96fd122024-03-28 14:31:02 +0000875 if [[ "$LINE" =~ '/lib64/' || "$LINE" =~ '/lib/arm64/' ]]; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530876 ARCH="64"
Bruno Martinsf96fd122024-03-28 14:31:02 +0000877 elif [[ "$LINE" =~ '/lib/' ]]; then
878 ARCH="32"
Steve Kondik5bd66602016-07-15 10:39:58 -0700879 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530880 BASENAME=$(basename "$FILE")
881 ARGS=$(target_args "$LINE")
882 ARGS=(${ARGS//;/ })
883 for ARG in "${ARGS[@]}"; do
884 if [[ "$ARG" =~ "SYMLINK" ]]; then
885 SYMLINKS=${ARG#*=}
886 SYMLINKS=(${SYMLINKS//,/ })
887 for SYMLINK in "${SYMLINKS[@]}"; do
888 SYMLINK_BASENAME=$(basename "$SYMLINK")
Bruno Martinsad05f512024-06-28 00:34:33 +0100889 PKGNAME="${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}"
890 if [[ "${SYMLINK_PACKAGES[@]}" =~ "$PKGNAME" ]]; then
891 PKGNAME+="_$(grep -o "$PKGNAME" <<< ${SYMLINK_PACKAGES[*]} | wc -l)"
892 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530893 {
894 printf 'install_symlink {\n'
895 printf '\tname: "%s",\n' "$PKGNAME"
Mashopy06100c92024-04-23 21:52:04 +0200896 if prefix_match_file "vendor/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530897 PREFIX='vendor/'
898 printf '\tsoc_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200899 elif prefix_match_file "product/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530900 PREFIX='product/'
901 printf '\tproduct_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200902 elif prefix_match_file "system_ext/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530903 PREFIX='system_ext/'
904 printf '\tsystem_ext_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200905 elif prefix_match_file "odm/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530906 PREFIX='odm/'
907 printf '\tdevice_specific: true,\n'
908 fi
909 printf '\tinstalled_location: "%s",\n' "${SYMLINK#"$PREFIX"}"
910 printf '\tsymlink_target: "/%s",\n' "$FILE"
911 printf '}\n\n'
912 } >> "$ANDROIDBP"
913 SYMLINK_PACKAGES+=("$PKGNAME")
914 done
915 fi
916 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700917 done
SamarV-1215556e2d2024-03-19 08:50:02 +0530918
919 write_package_definition "${SYMLINK_PACKAGES[@]}" >> "$PRODUCTMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700920}
921
922#
Michael Bestasfe71eb32023-06-11 18:59:10 +0300923# write_single_product_copy_files:
924#
925# $1: the file to be copied
926#
927# Creates a PRODUCT_COPY_FILES section in the product makefile for the
928# item provided in $1.
929#
930function write_single_product_copy_files() {
931 local FILE="$1"
932 if [ -z "$FILE" ]; then
933 echo "A file must be provided to write_single_product_copy_files()!"
934 exit 1
935 fi
936
937 local TARGET=$(target_file "$FILE")
938 local OUTTARGET=$(truncate_file $TARGET)
939
940 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
941 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s\n' \
942 "$OUTDIR" "$TARGET" "$OUTTARGET" >> "$PRODUCTMK"
943}
944
945#
946# write_single_product_packages:
947#
948# $1: the package to be built
949#
950# Creates a PRODUCT_PACKAGES section in the product makefile for the
951# item provided in $1.
952#
953function write_single_product_packages() {
954 local PACKAGE="$1"
955 if [ -z "$PACKAGE" ]; then
956 echo "A package must be provided to write_single_product_packages()!"
957 exit 1
958 fi
959
960 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
961 printf ' %s%s\n' "$PACKAGE" >> "$PRODUCTMK"
962}
963
964#
Michael Bestas431a8002023-06-11 20:04:45 +0300965# write_rro_androidmanifest:
966#
967# $2: target package for the RRO overlay
968#
969# Creates an AndroidManifest.xml for an RRO overlay.
970#
971function write_rro_androidmanifest() {
972 local TARGET_PACKAGE="$1"
973
974 cat << EOF
975<manifest xmlns:android="http://schemas.android.com/apk/res/android"
976 package="$TARGET_PACKAGE.vendor"
977 android:versionCode="1"
978 android:versionName="1.0">
979 <application android:hasCode="false" />
980 <overlay
981 android:targetPackage="$TARGET_PACKAGE"
982 android:isStatic="true"
983 android:priority="0"/>
984</manifest>
985EOF
986}
987
988#
989# write_rro_blueprint:
990#
991# $1: package name for the RRO overlay
992# $2: target partition for the RRO overlay
993#
994# Creates an Android.bp for an RRO overlay.
995#
996function write_rro_blueprint() {
997 local PKGNAME="$1"
998 local PARTITION="$2"
999
1000 printf 'runtime_resource_overlay {\n'
1001 printf '\tname: "%s",\n' "$PKGNAME"
1002 printf '\ttheme: "%s",\n' "$PKGNAME"
1003 printf '\tsdk_version: "%s",\n' "current"
1004 printf '\taaptflags: ["%s"],\n' "--keep-raw-values"
1005
1006 if [ "$PARTITION" = "vendor" ]; then
1007 printf '\tsoc_specific: true,\n'
1008 elif [ "$PARTITION" = "product" ]; then
1009 printf '\tproduct_specific: true,\n'
1010 elif [ "$PARTITION" = "system_ext" ]; then
1011 printf '\tsystem_ext_specific: true,\n'
1012 elif [ "$PARTITION" = "odm" ]; then
1013 printf '\tdevice_specific: true,\n'
1014 fi
1015 printf '}\n'
1016}
1017
1018#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001019# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -07001020#
1021# $1: file which will be written to
1022#
Michael Bestasa2934df2020-12-19 03:50:32 +02001023# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -07001024# note that this is not an append operation, and should
1025# be executed first!
1026#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001027function write_blueprint_header() {
1028 if [ -f $1 ]; then
1029 rm $1
1030 fi
1031
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001032 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1033
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001034 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001035// Automatically generated file. DO NOT MODIFY
1036//
1037// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001038
1039EOF
1040}
1041
1042#
1043# write_makefile_header:
1044#
1045# $1: file which will be written to
1046#
Michael Bestasa2934df2020-12-19 03:50:32 +02001047# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001048# note that this is not an append operation, and should
1049# be executed first!
1050#
1051function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -05001052 if [ -f $1 ]; then
1053 rm $1
1054 fi
1055
Steve Kondik5bd66602016-07-15 10:39:58 -07001056 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1057
Jake Whatley9843b322017-01-25 21:49:16 -05001058 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001059# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -07001060#
Steve Kondik5bd66602016-07-15 10:39:58 -07001061# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1062
1063EOF
1064}
1065
1066#
Michael Bestas431a8002023-06-11 20:04:45 +03001067# write_xml_header:
1068#
1069# $1: file which will be written to
1070#
1071# writes out the warning message regarding manual file modifications.
1072# note that this is not an append operation, and should
1073# be executed first!
1074#
1075function write_xml_header() {
1076 if [ -f $1 ]; then
1077 rm $1
1078 fi
1079
1080 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1081 [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
1082
1083 cat << EOF >> $1
1084<?xml version="1.0" encoding="utf-8"?>
1085<!--
1086 Automatically generated file. DO NOT MODIFY
1087
1088 This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1089-->
1090EOF
1091}
1092
1093#
1094# write_rro_package:
1095#
1096# $1: the RRO package name
1097# $2: the RRO target package
1098# $3: the partition for the RRO overlay
1099#
1100# Generates the file structure for an RRO overlay.
1101#
1102function write_rro_package() {
1103 local PKGNAME="$1"
1104 if [ -z "$PKGNAME" ]; then
1105 echo "A package name must be provided to write_rro_package()!"
1106 exit 1
1107 fi
1108
1109 local TARGET_PACKAGE="$2"
1110 if [ -z "$TARGET_PACKAGE" ]; then
1111 echo "A target package must be provided to write_rro_package()!"
1112 exit 1
1113 fi
1114
1115 local PARTITION="$3"
1116 if [ -z "$PARTITION" ]; then
1117 PARTITION="vendor"
1118 fi
1119
1120 local RROBP="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/Android.bp
1121 local RROMANIFEST="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/AndroidManifest.xml
1122
1123 write_blueprint_header "$RROBP"
1124 write_xml_header "$RROMANIFEST"
1125
1126 write_rro_blueprint "$PKGNAME" "$PARTITION" >> "$RROBP"
1127 write_rro_androidmanifest "$TARGET_PACKAGE" >> "$RROMANIFEST"
1128}
1129
1130#
SamarV-1215556e2d2024-03-19 08:50:02 +05301131# write_package_definition:
1132#
1133# $@: list of packages
1134#
1135# writes out the final PRODUCT_PACKAGES list
1136#
1137function write_package_definition() {
1138 local PACKAGE_LIST=("${@}")
1139 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
1140
1141 if [ "$PACKAGE_COUNT" -eq "0" ]; then
1142 return 0
1143 fi
1144
1145 printf '\n%s\n' "PRODUCT_PACKAGES += \\"
1146 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
SamarV-1217e6b4082024-02-26 14:39:34 +05301147 local SKIP=false
SamarV-1215556e2d2024-03-19 08:50:02 +05301148 local LINEEND=" \\"
1149 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
1150 LINEEND=""
1151 fi
SamarV-1217e6b4082024-02-26 14:39:34 +05301152 for PKG in $(tr "," "\n" <<< "$REQUIRED_PACKAGES_LIST"); do
1153 if [[ $PKG == "${PACKAGE_LIST[$i - 1]}" ]]; then
1154 SKIP=true
1155 break
1156 fi
1157 done
1158 # Skip adding of the package to product makefile if it's in the required list
1159 if [[ $SKIP == false ]]; then
1160 printf ' %s%s\n' "${PACKAGE_LIST[$i - 1]}" "$LINEEND" >> "$PRODUCTMK"
1161 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301162 done
1163}
1164
1165#
Steve Kondik5bd66602016-07-15 10:39:58 -07001166# write_headers:
1167#
1168# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001169# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001170#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001171# Calls write_makefile_header for each of the makefiles and
1172# write_blueprint_header for Android.bp and creates the initial
1173# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001174#
1175function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001176 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001177
1178 GUARD="$2"
1179 if [ -z "$GUARD" ]; then
1180 GUARD="TARGET_DEVICE"
1181 fi
1182
Steve Kondik5bd66602016-07-15 10:39:58 -07001183 cat << EOF >> "$ANDROIDMK"
1184LOCAL_PATH := \$(call my-dir)
1185
1186EOF
1187 if [ "$COMMON" -ne 1 ]; then
1188 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001189ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001190
1191EOF
1192 else
1193 if [ -z "$1" ]; then
1194 echo "Argument with devices to be added to guard must be set!"
1195 exit 1
1196 fi
1197 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001198ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001199
1200EOF
1201 fi
1202
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001203 write_makefile_header "$BOARDMK"
1204 write_makefile_header "$PRODUCTMK"
1205 write_blueprint_header "$ANDROIDBP"
1206
1207 cat << EOF >> "$ANDROIDBP"
1208soong_namespace {
1209}
1210
1211EOF
1212
1213 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1214 cat << EOF >> "$PRODUCTMK"
1215PRODUCT_SOONG_NAMESPACES += \\
1216 vendor/$VENDOR/$DEVICE
1217
1218EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001219}
1220
1221#
1222# write_footers:
1223#
1224# Closes the inital guard and any other finalization tasks. Must
1225# be called as the final step.
1226#
1227function write_footers() {
1228 cat << EOF >> "$ANDROIDMK"
1229endif
1230EOF
1231}
1232
1233# Return success if adb is up and not in recovery
1234function _adb_connected {
1235 {
Jake Whatley9843b322017-01-25 21:49:16 -05001236 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001237 then
1238 return 0
1239 fi
1240 } 2>/dev/null
1241
1242 return 1
1243};
1244
1245#
1246# parse_file_list:
1247#
1248# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001249# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001250#
1251# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1252#
1253function parse_file_list() {
1254 if [ -z "$1" ]; then
1255 echo "An input file is expected!"
1256 exit 1
1257 elif [ ! -f "$1" ]; then
1258 echo "Input file "$1" does not exist!"
1259 exit 1
1260 fi
1261
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001262 if [ -n "$2" ]; then
1263 echo "Using section \"$2\""
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301264 LIST=$EXTRACT_TMP_DIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001265 # Match all lines starting with first line found to start* with '#'
1266 # comment and contain** $2, and ending with first line to be empty*.
1267 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1268 # **the $2 match is case-insensitive
1269 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001270 else
1271 LIST=$1
1272 fi
1273
Steve Kondik5bd66602016-07-15 10:39:58 -07001274 PRODUCT_PACKAGES_LIST=()
1275 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001276 PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +05301277 PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001278 PRODUCT_COPY_FILES_LIST=()
1279 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001280 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001281
1282 while read -r line; do
1283 if [ -z "$line" ]; then continue; fi
1284
1285 # If the line has a pipe delimiter, a sha1 hash should follow.
1286 # This indicates the file should be pinned and not overwritten
1287 # when extracting files.
1288 local SPLIT=(${line//\|/ })
1289 local COUNT=${#SPLIT[@]}
1290 local SPEC=${SPLIT[0]}
1291 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001292 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001293 if [ "$COUNT" -gt "1" ]; then
1294 HASH=${SPLIT[1]}
1295 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001296 if [ "$COUNT" -gt "2" ]; then
1297 FIXUP_HASH=${SPLIT[2]}
1298 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301299 if [[ "$SPEC" =~ 'SYMLINK=' ]]; then
1300 PRODUCT_SYMLINKS_LIST+=("${SPEC#-}")
1301 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001302 # if line starts with a dash, it needs to be packaged
1303 if [[ "$SPEC" =~ ^- ]]; then
1304 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1305 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001306 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Chirayu Desaia7741012021-12-04 07:17:28 +05301307 # if line contains apex, apk, jar or vintf fragment, it needs to be packaged
1308 elif suffix_match_file ".apex" "$(src_file "$SPEC")" || \
1309 suffix_match_file ".apk" "$(src_file "$SPEC")" || \
Michael Bestasea90aef2021-11-15 22:18:04 +02001310 suffix_match_file ".jar" "$(src_file "$SPEC")" || \
1311 [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
1312 PRODUCT_PACKAGES_LIST+=("$SPEC")
1313 PRODUCT_PACKAGES_HASHES+=("$HASH")
1314 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001315 else
1316 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1317 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001318 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001319 fi
1320
Chirayu Desaif1a21302022-09-13 00:29:33 +05301321 done < <(grep -v -E '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001322}
1323
1324#
1325# write_makefiles:
1326#
1327# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001328# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001329#
SamarV-1215556e2d2024-03-19 08:50:02 +05301330# Calls write_product_copy_files, write_product_packages and
1331# lastly write_symlink_packages on the given file and appends
1332# to the Android.bp as well as the product makefile.
Steve Kondik5bd66602016-07-15 10:39:58 -07001333#
1334function write_makefiles() {
1335 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001336 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001337 write_product_packages
SamarV-1215556e2d2024-03-19 08:50:02 +05301338 write_symlink_packages
Steve Kondik5bd66602016-07-15 10:39:58 -07001339}
1340
1341#
1342# append_firmware_calls_to_makefiles:
1343#
1344# Appends to Android.mk the calls to all images present in radio folder
1345# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1346#
1347function append_firmware_calls_to_makefiles() {
1348 cat << EOF >> "$ANDROIDMK"
1349ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1350
1351RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1352\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1353 \$(call add-radio-file,radio/\$(f)))
1354\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1355
1356endif
1357
1358EOF
1359}
1360
1361#
1362# get_file:
1363#
1364# $1: input file
1365# $2: target file/folder
1366# $3: source of the file (can be "adb" or a local folder)
1367#
1368# Silently extracts the input file to defined target
1369# Returns success if file can be pulled from the device or found locally
1370#
1371function get_file() {
1372 local SRC="$3"
1373
1374 if [ "$SRC" = "adb" ]; then
1375 # try to pull
LuK13370f7f0d12022-08-19 21:49:56 +02001376 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1377 adb pull "${1#/system}" "$2" >/dev/null 2>&1 && return 0
1378 adb pull "system/$1" "$2" >/dev/null 2>&1 && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001379
1380 return 1
1381 else
1382 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001383 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1384 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001385 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001386
LuK1337dbb77cc2023-12-04 19:03:10 +01001387 # try /vendor/odm for devices without /odm partition
1388 [[ "$1" == /system/odm/* ]] && cp -r "$SRC/vendor/${1#/system}" "$2" 2>/dev/null && return 0
1389
Steve Kondik5bd66602016-07-15 10:39:58 -07001390 return 1
1391 fi
1392};
1393
1394#
1395# oat2dex:
1396#
1397# $1: extracted apk|jar (to check if deodex is required)
1398# $2: odexed apk|jar to deodex
1399# $3: source of the odexed apk|jar
1400#
1401# Convert apk|jar .odex in the corresposing classes.dex
1402#
1403function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001404 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001405 local OEM_TARGET="$2"
1406 local SRC="$3"
1407 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001408 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001409
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001410 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1411 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1412 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001413 fi
1414
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001415 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001416 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001417 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001418
codeworkx85eda752018-09-23 12:36:57 +02001419 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001420 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001421 fi
1422
Steve Kondik5bd66602016-07-15 10:39:58 -07001423 # Extract existing boot.oats to the temp folder
1424 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001425 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001426 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301427 mkdir -p "$EXTRACT_TMP_DIR/system/framework/$ARCH"
1428 if get_file "/system/framework/$ARCH" "$EXTRACT_TMP_DIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001429 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001430 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301431 rmdir "$EXTRACT_TMP_DIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001432 fi
1433 done
1434 fi
1435
1436 if [ -z "$ARCHES" ]; then
1437 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1438 fi
1439
theimpulson9a911af2019-08-14 03:25:12 +00001440 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001441 return;
1442 fi
1443
theimpulson9a911af2019-08-14 03:25:12 +00001444 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001445 return 0 # target apk|jar is already odexed, return
1446 fi
1447
1448 for ARCH in $ARCHES; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301449 BOOTOAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001450
Joe Maplesfb3941c2018-01-05 14:51:33 -05001451 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1452 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001453
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301454 if get_file "$OAT" "$EXTRACT_TMP_DIR" "$SRC"; then
1455 if get_file "$VDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1456 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$VDEX")" > /dev/null
1457 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${OEM_TARGET%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001458 for CLASS in $CLASSES; do
1459 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1460 # Check if we have to deal with CompactDex
1461 if [[ "$CLASS" == *.cdex ]]; then
1462 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301463 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001464 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301465 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001466 fi
1467 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001468 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301469 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$EXTRACT_TMP_DIR/$(basename "$OAT")"
1470 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001471 fi
theimpulson9a911af2019-08-14 03:25:12 +00001472 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301473 JAROAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001474 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001475 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001476 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001477 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001478 # try to extract classes.dex from boot.vdex for frameworks jars
1479 # fallback to boot.oat if vdex is not available
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301480 if get_file "$JARVDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1481 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$JARVDEX")" > /dev/null
1482 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${JARVDEX%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001483 for CLASS in $CLASSES; do
1484 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1485 # Check if we have to deal with CompactDex
1486 if [[ "$CLASS" == *.cdex ]]; then
1487 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301488 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001489 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301490 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001491 fi
1492 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001493 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301494 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$JAROAT/$OEM_TARGET"
1495 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001496 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001497 else
1498 continue
1499 fi
1500
Steve Kondik5bd66602016-07-15 10:39:58 -07001501 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001502
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301503 rm -rf "$EXTRACT_TMP_DIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001504}
1505
1506#
1507# init_adb_connection:
1508#
1509# Starts adb server and waits for the device
1510#
1511function init_adb_connection() {
1512 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1513 if ! _adb_connected; then
1514 echo "No device is online. Waiting for one..."
1515 echo "Please connect USB and/or enable USB debugging"
1516 until _adb_connected; do
1517 sleep 1
1518 done
1519 echo "Device Found."
1520 fi
1521
1522 # Retrieve IP and PORT info if we're using a TCP connection
Chirayu Desaif1a21302022-09-13 00:29:33 +05301523 TCPIPPORT=$(adb devices | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
Steve Kondik5bd66602016-07-15 10:39:58 -07001524 | head -1 | awk '{print $1}')
1525 adb root &> /dev/null
1526 sleep 0.3
1527 if [ -n "$TCPIPPORT" ]; then
1528 # adb root just killed our connection
1529 # so reconnect...
1530 adb connect "$TCPIPPORT"
1531 fi
1532 adb wait-for-device &> /dev/null
1533 sleep 0.3
1534}
1535
1536#
1537# fix_xml:
1538#
1539# $1: xml file to fix
1540#
1541function fix_xml() {
1542 local XML="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301543 local TEMP_XML="$EXTRACT_TMP_DIR/`basename "$XML"`.temp"
Steve Kondik5bd66602016-07-15 10:39:58 -07001544
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001545 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1546 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001547
1548 mv "$TEMP_XML" "$XML"
1549}
1550
Vladimir Olteande985fe2019-01-17 03:07:34 +02001551function get_hash() {
1552 local FILE="$1"
1553
1554 if [ "$(uname)" == "Darwin" ]; then
1555 shasum "${FILE}" | awk '{print $1}'
1556 else
1557 sha1sum "${FILE}" | awk '{print $1}'
1558 fi
1559}
1560
Vladimir Olteana7d20492019-01-17 03:05:52 +02001561function print_spec() {
1562 local SPEC_PRODUCT_PACKAGE="$1"
1563 local SPEC_SRC_FILE="$2"
1564 local SPEC_DST_FILE="$3"
1565 local SPEC_ARGS="$4"
1566 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001567 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001568
1569 local PRODUCT_PACKAGE=""
1570 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1571 PRODUCT_PACKAGE="-"
1572 fi
1573 local SRC=""
1574 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1575 SRC="${SPEC_SRC_FILE}:"
1576 fi
1577 local DST=""
1578 if [ ! -z "${SPEC_DST_FILE}" ]; then
1579 DST="${SPEC_DST_FILE}"
1580 fi
1581 local ARGS=""
1582 if [ ! -z "${SPEC_ARGS}" ]; then
1583 ARGS=";${SPEC_ARGS}"
1584 fi
1585 local HASH=""
1586 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1587 HASH="|${SPEC_HASH}"
1588 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001589 local FIXUP_HASH=""
1590 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1591 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1592 fi
1593 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1594}
1595
1596# To be overridden by device-level extract-files.sh
1597# Parameters:
1598# $1: spec name of a blob. Can be used for filtering.
1599# If the spec is "src:dest", then $1 is "dest".
1600# If the spec is "src", then $1 is "src".
1601# $2: path to blob file. Can be used for fixups.
1602#
1603function blob_fixup() {
1604 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001605}
1606
Steve Kondik5bd66602016-07-15 10:39:58 -07001607#
1608# extract:
1609#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001610# Positional parameters:
1611# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001612# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001613# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1614#
1615# Non-positional parameters (coming after $2):
1616# --section: preferred way of selecting the portion to parse and extract from
1617# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001618# --kang: if present, this option will activate the printing of hashes for the
1619# extracted blobs. Useful with --section for subsequent pinning of
1620# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001621#
1622function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001623 # Consume positional parameters
1624 local PROPRIETARY_FILES_TXT="$1"; shift
1625 local SRC="$1"; shift
1626 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001627 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001628
1629 # Consume optional, non-positional parameters
1630 while [ "$#" -gt 0 ]; do
1631 case "$1" in
1632 -s|--section)
1633 SECTION="$2"; shift
1634 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001635 -k|--kang)
1636 KANG=true
1637 DISABLE_PINNING=1
1638 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001639 *)
1640 # Backwards-compatibility with the old behavior, where $3, if
1641 # present, denoted an optional positional ${SECTION} argument.
1642 # Users of ${SECTION} are encouraged to migrate from setting it as
1643 # positional $3, to non-positional --section ${SECTION}, the
1644 # reason being that it doesn't scale to have more than 1 optional
1645 # positional argument.
1646 SECTION="$1"
1647 ;;
1648 esac
1649 shift
1650 done
1651
Steve Kondik5bd66602016-07-15 10:39:58 -07001652 if [ -z "$OUTDIR" ]; then
1653 echo "Output dir not set!"
1654 exit 1
1655 fi
1656
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001657 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001658
1659 # Allow failing, so we can try $DEST and/or $FILE
1660 set +e
1661
1662 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1663 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001664 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001665 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001666 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001667 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301668 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001669
1670 if [ "$SRC" = "adb" ]; then
1671 init_adb_connection
1672 fi
1673
Dan Pasanen0cc05012017-03-21 09:06:11 -05001674 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301675 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001676
1677 # Check if we're working with the same zip that was passed last time.
1678 # If so, let's just use what's already extracted.
1679 MD5=`md5sum "$SRC"| awk '{print $1}'`
1680 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1681
1682 if [ "$MD5" != "$OLDMD5" ]; then
1683 rm -rf "$DUMPDIR"
1684 mkdir "$DUMPDIR"
1685 unzip "$SRC" -d "$DUMPDIR"
1686 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1687
1688 # Stop if an A/B OTA zip is detected. We cannot extract these.
1689 if [ -a "$DUMPDIR"/payload.bin ]; then
1690 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1691 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001692 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001693
Luca Stefani776be462020-09-09 15:53:58 +02001694 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001695 do
1696 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001697 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1698 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1699 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1700 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1701 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001702 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1703 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1704 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1705 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1706 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
Chirayu Desai62ed12a2021-11-26 05:47:25 +05301707 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1708 rm "$DUMPDIR"/"$PARTITION".img
dianlujitao85ddca62020-04-21 23:03:20 +08001709 fi
1710 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001711 fi
1712
1713 SRC="$DUMPDIR"
1714 fi
1715
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301716 if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301717 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301718 mkdir -p "$DUMPDIR"
1719
1720 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1721 do
1722 echo "Extracting "$PARTITION""
1723 local IMAGE="$SRC"/"$PARTITION".img
1724 if [ -f "$IMAGE" ]; then
1725 if [[ $(file -b "$IMAGE") == Linux* ]]; then
1726 extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
1727 elif [[ $(file -b "$IMAGE") == Android* ]]; then
Chirayu Desai501ffd62022-03-17 06:27:33 +05301728 "$SIMG2IMG" "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301729 extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
1730 else
1731 echo "Unsupported "$IMAGE""
1732 fi
1733 fi
1734 done
1735
1736 SRC="$DUMPDIR"
1737 fi
1738
Steve Kondik5bd66602016-07-15 10:39:58 -07001739 if [ "$VENDOR_STATE" -eq "0" ]; then
1740 echo "Cleaning output directory ($OUTPUT_ROOT).."
1741 rm -rf "${OUTPUT_TMP:?}"
1742 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001743 if [ -d "$OUTPUT_ROOT" ]; then
1744 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1745 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001746 VENDOR_STATE=1
1747 fi
1748
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001749 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001750
1751 for (( i=1; i<COUNT+1; i++ )); do
1752
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001753 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001754 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001755 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001756 local OUTPUT_DIR=
1757 local TMP_DIR=
1758 local SRC_FILE=
1759 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001760 local IS_PRODUCT_PACKAGE=false
1761
1762 # Note: this relies on the fact that the ${FILELIST[@]} array
1763 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1764 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1765 IS_PRODUCT_PACKAGE=true
1766 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001767
Michael Bestasbda30202020-12-28 04:44:52 +02001768 OUTPUT_DIR="${OUTPUT_ROOT}"
1769 TMP_DIR="${OUTPUT_TMP}"
1770 SRC_FILE="/system/${SPEC_SRC_FILE}"
1771 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001772
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001773 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001774 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001775 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001776 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001777
Gabriele M58270a32017-11-13 23:15:29 +01001778 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001779 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001780 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001781 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001782 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001783 if [ -f "${VENDOR_REPO_FILE}" ]; then
1784 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001785 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001786 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001787 fi
1788 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001789 local TMP_HASH=$(get_hash "${PINNED}")
1790 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001791 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001792 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1793 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001794 fi
1795 fi
1796 fi
1797 fi
1798
Vladimir Olteana7d20492019-01-17 03:05:52 +02001799 if [ "${KANG}" = false ]; then
1800 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1801 fi
1802
Gabriele M58270a32017-11-13 23:15:29 +01001803 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001804 if [ "${FIXUP_HASH}" != "x" ]; then
1805 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1806 else
1807 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1808 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001809 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001810 FOUND=false
1811 # Try Lineage target first.
1812 # Also try to search for files stripped of
1813 # the "/system" prefix, if we're actually extracting
1814 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001815 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001816 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1817 FOUND=true
1818 break
1819 }
1820 done
1821
1822 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001823 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001824 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001825 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001826
Arian5f98d792021-09-09 15:24:25 +02001827 # Blob fixup pipeline has 2 parts: one that is fixed and
1828 # one that is user-configurable
1829 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1830 # Deodex apk|jar if that's the case
1831 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1832 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301833 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
1834 touch -t 200901010000 "$EXTRACT_TMP_DIR/classes"*
1835 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
1836 rm "$EXTRACT_TMP_DIR/classes"*
Arian5f98d792021-09-09 15:24:25 +02001837 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1838 fi
1839 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1840 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001841 fi
Arian5f98d792021-09-09 15:24:25 +02001842 # Now run user-supplied fixup function
1843 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1844 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001845
Arian5f98d792021-09-09 15:24:25 +02001846 if [ -f "${VENDOR_REPO_FILE}" ]; then
1847 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1848 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001849 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001850 chmod 755 "${VENDOR_REPO_FILE}"
1851 else
1852 chmod 644 "${VENDOR_REPO_FILE}"
1853 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001854 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001855
Arian5f98d792021-09-09 15:24:25 +02001856 if [ "${KANG}" = true ]; then
1857 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1858 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001859
Arian5f98d792021-09-09 15:24:25 +02001860 # Check and print whether the fixup pipeline actually did anything.
1861 # This isn't done right after the fixup pipeline because we want this print
1862 # to come after print_spec above, when in kang mode.
1863 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1864 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1865 # Now sanity-check the spec for this blob.
1866 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1867 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1868 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1869 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001870 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001871 fi
1872
Steve Kondik5bd66602016-07-15 10:39:58 -07001873 done
1874
1875 # Don't allow failing
1876 set -e
1877}
1878
1879#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001880# extract2:
1881#
1882# Positional parameters:
1883# $1: file containing the list of items to extract (aka proprietary-files.txt)
1884#
1885# Non-positional parameters (coming after $2):
1886# --section: selects the portion to parse and extracts from proprietary-files.txt
1887# --kang: if present, this option will activate the printing of hashes for the
1888# extracted blobs. Useful with --section for subsequent pinning of
1889# blobs taken from other origins.
1890#
1891function extract2() {
1892 # Consume positional parameters
1893 local PROPRIETARY_FILES_TXT="$1"; shift
1894 local SECTION=""
1895 local KANG=false
1896
1897 # Consume optional, non-positional parameters
1898 while [ "$#" -gt 0 ]; do
1899 case "$1" in
1900 --adb)
1901 ADB=true
1902 ;;
1903 --system)
1904 SYSTEM_SRC="$2"; shift
1905 ;;
1906 --vendor)
1907 VENDOR_SRC="$2"; shift
1908 ;;
1909 --odm)
1910 ODM_SRC="$2"; shift
1911 ;;
1912 --product)
1913 PRODUCT_SRC="$2"; shift
1914 ;;
1915 -s|--section)
1916 SECTION="$2"; shift
1917 ;;
1918 -k|--kang)
1919 KANG=true
1920 DISABLE_PINNING=1
1921 ;;
1922 esac
1923 shift
1924 done
1925
1926 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1927 echo "No sources set! You must select --adb or pass paths to partition dumps."
1928 exit 1
1929 fi
1930
1931 if [ -z "$OUTDIR" ]; then
1932 echo "Output dir not set!"
1933 exit 1
1934 fi
1935
1936 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1937
1938 # Allow failing, so we can try $DEST and/or $FILE
1939 set +e
1940
1941 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1942 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1943 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1944 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1945 local COUNT=${#FILELIST[@]}
1946 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301947 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001948
1949 if [ "$ADB" = true ]; then
1950 init_adb_connection
1951 fi
1952
1953 if [ "$VENDOR_STATE" -eq "0" ]; then
1954 echo "Cleaning output directory ($OUTPUT_ROOT).."
1955 rm -rf "${OUTPUT_TMP:?}"
1956 mkdir -p "${OUTPUT_TMP:?}"
1957 if [ -d "$OUTPUT_ROOT" ]; then
1958 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1959 fi
1960 VENDOR_STATE=1
1961 fi
1962
1963 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1964
1965 for (( i=1; i<COUNT+1; i++ )); do
1966
1967 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1968 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1969 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1970 local OUTPUT_DIR=
1971 local TMP_DIR=
1972 local SRC_FILE=
1973 local DST_FILE=
1974 local IS_PRODUCT_PACKAGE=false
1975
1976 # Note: this relies on the fact that the ${FILELIST[@]} array
1977 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1978 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1979 IS_PRODUCT_PACKAGE=true
1980 fi
1981
1982 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1983 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1984 TMP_DIR="${OUTPUT_TMP}/rootfs"
1985 else
1986 OUTPUT_DIR="${OUTPUT_ROOT}"
1987 TMP_DIR="${OUTPUT_TMP}"
1988 fi
1989 SRC_FILE="${SPEC_SRC_FILE}"
1990 DST_FILE="${SPEC_DST_FILE}"
1991
1992 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1993 local BLOB_DISPLAY_NAME="${DST_FILE}"
1994 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1995
1996 # Check pinned files
1997 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1998 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1999 local KEEP=""
2000 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
2001 if [ -f "${VENDOR_REPO_FILE}" ]; then
2002 local PINNED="${VENDOR_REPO_FILE}"
2003 else
2004 local PINNED="${TMP_DIR}${DST_FILE}"
2005 fi
2006 if [ -f "$PINNED" ]; then
2007 local TMP_HASH=$(get_hash "${PINNED}")
2008 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
2009 KEEP="1"
2010 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
2011 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
2012 fi
2013 fi
2014 fi
2015 fi
2016
2017 if [ "${KANG}" = false ]; then
2018 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
2019 fi
2020
2021 if [ "$KEEP" = "1" ]; then
2022 printf ' + keeping pinned file with hash %s\n' "${HASH}"
2023 else
2024 FOUND=false
2025 PARTITION_SOURCE_DIR=
2026 # Try Lineage target first.
2027 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
2028 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
2029 if [ "$PARTITION" = "system" ]; then
2030 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
2031 elif [ "$PARTITION" = "vendor" ]; then
2032 PARTITION_SOURCE_DIR="$VENDOR_SRC"
2033 elif [ "$PARTITION" = "product" ]; then
2034 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
2035 elif [ "$PARTITION" = "odm" ]; then
2036 PARTITION_SOURCE_DIR="$ODM_SRC"
2037 fi
2038 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
2039 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2040 FOUND=true
2041 break
2042 }
2043 # Search with the full system/ prefix if the file was not found on the system partition
2044 # because we may be searching in a mounted system-as-root system.img
2045 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
2046 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2047 FOUND=true
2048 break
2049 }
2050 fi
2051 done
2052
2053 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
2054 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
2055 fi
2056
2057 if [ "${FOUND}" = false ]; then
2058 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
2059 continue
2060 fi
2061 fi
2062
2063 # Blob fixup pipeline has 2 parts: one that is fixed and
2064 # one that is user-configurable
2065 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2066 # Deodex apk|jar if that's the case
2067 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
2068 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302069 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
2070 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
2071 rm "$EXTRACT_TMP_DIR/classes"*
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04002072 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2073 fi
2074 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2075 fix_xml "${VENDOR_REPO_FILE}"
2076 fi
2077 # Now run user-supplied fixup function
2078 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2079 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2080
2081 if [ -f "${VENDOR_REPO_FILE}" ]; then
2082 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2083 local TYPE="${DIR##*/}"
2084 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
2085 chmod 755 "${VENDOR_REPO_FILE}"
2086 else
2087 chmod 644 "${VENDOR_REPO_FILE}"
2088 fi
2089 fi
2090
2091 if [ "${KANG}" = true ]; then
2092 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2093 fi
2094
2095 # Check and print whether the fixup pipeline actually did anything.
2096 # This isn't done right after the fixup pipeline because we want this print
2097 # to come after print_spec above, when in kang mode.
2098 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2099 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2100 # Now sanity-check the spec for this blob.
2101 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2102 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
2103 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
2104 fi
2105 fi
2106
2107 done
2108
2109 # Don't allow failing
2110 set -e
2111}
2112
2113#
Steve Kondik5bd66602016-07-15 10:39:58 -07002114# extract_firmware:
2115#
2116# $1: file containing the list of items to extract
2117# $2: path to extracted radio folder
2118#
2119function extract_firmware() {
2120 if [ -z "$OUTDIR" ]; then
2121 echo "Output dir not set!"
2122 exit 1
2123 fi
2124
2125 parse_file_list "$1"
2126
2127 # Don't allow failing
2128 set -e
2129
2130 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
2131 local COUNT=${#FILELIST[@]}
2132 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00002133 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07002134
2135 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
2136 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
2137 rm -rf "${OUTPUT_DIR:?}/"*
2138 VENDOR_RADIO_STATE=1
2139 fi
2140
2141 echo "Extracting $COUNT files in $1 from $SRC:"
2142
2143 for (( i=1; i<COUNT+1; i++ )); do
2144 local FILE="${FILELIST[$i-1]}"
2145 printf ' - %s \n' "/radio/$FILE"
2146
2147 if [ ! -d "$OUTPUT_DIR" ]; then
2148 mkdir -p "$OUTPUT_DIR"
2149 fi
2150 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
2151 chmod 644 "$OUTPUT_DIR/$FILE"
2152 done
2153}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002154
2155function extract_img_data() {
2156 local image_file="$1"
2157 local out_dir="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302158 local logFile="$EXTRACT_TMP_DIR/debugfs.log"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002159
2160 if [ ! -d "$out_dir" ]; then
2161 mkdir -p "$out_dir"
2162 fi
2163
2164 if [[ "$HOST_OS" == "Darwin" ]]; then
2165 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
2166 echo "[-] Failed to extract data from '$image_file'"
2167 abort 1
2168 }
2169 else
2170 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2171 do
2172 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2173 echo "[-] Failed to extract data from '$image_file'"
2174 abort 1
2175 }
2176 done
2177 fi
2178
2179 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2180 if grep -Fq "$symlink_err" "$logFile"; then
2181 echo "[-] Symlinks have not been properly processed from $image_file"
Michael Bestas1b570252021-12-02 21:05:36 +02002182 echo "[!] You might not have a compatible debugfs version"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002183 abort 1
2184 fi
2185}
2186
2187declare -ra VENDOR_SKIP_FILES=(
2188 "bin/toybox_vendor"
2189 "bin/toolbox"
2190 "bin/grep"
2191 "build.prop"
2192 "compatibility_matrix.xml"
2193 "default.prop"
2194 "etc/NOTICE.xml.gz"
2195 "etc/vintf/compatibility_matrix.xml"
2196 "etc/vintf/manifest.xml"
2197 "etc/wifi/wpa_supplicant.conf"
2198 "manifest.xml"
2199 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2200 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2201 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2202 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2203 "overlay/framework-res__auto_generated_rro.apk"
2204 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2205)
2206
2207function array_contains() {
2208 local element
2209 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2210 return 1
2211}
2212
2213function generate_prop_list_from_image() {
2214 local image_file="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302215 local image_dir="$EXTRACT_TMP_DIR/image-temp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002216 local output_list="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302217 local output_list_tmp="$EXTRACT_TMP_DIR/_proprietary-blobs.txt"
Michael Bestasca5d78a2021-12-02 20:58:40 +02002218 local -n skipped_files="$3"
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002219 local component="$4"
2220 local partition="$component"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002221
Chirayu Desai203cc522021-12-04 01:18:45 +05302222 mkdir -p "$image_dir"
2223
2224 if [[ $(file -b "$image_file") == Linux* ]]; then
2225 extract_img_data "$image_file" "$image_dir"
2226 elif [[ $(file -b "$image_file") == Android* ]]; then
Chirayu Desai501ffd62022-03-17 06:27:33 +05302227 "$SIMG2IMG" "$image_file" "$image_dir"/"$(basename "$image_file").raw"
Chirayu Desai203cc522021-12-04 01:18:45 +05302228 extract_img_data "$image_dir"/"$(basename "$image_file").raw" "$image_dir"
2229 rm "$image_dir"/"$(basename "$image_file").raw"
2230 else
2231 echo "Unsupported "$image_file""
2232 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002233
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002234 if [ -z "$component" ]; then
2235 partition="vendor"
2236 elif [[ "$component" == "carriersettings" ]]; then
2237 partition="product"
2238 fi
2239
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002240 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2241 do
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002242 if [[ "$component" == "carriersettings" ]] && ! prefix_match_file "etc/CarrierSettings" "$FILE" ; then
2243 continue
2244 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002245 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2246 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2247 continue
2248 fi
2249 # Skip device defined skipped files since they will be re-generated at build time
Michael Bestasca5d78a2021-12-02 20:58:40 +02002250 if array_contains "$FILE" "${skipped_files[@]}"; then
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002251 continue
2252 fi
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002253 echo "$partition/$FILE" >> "$output_list_tmp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002254 done
2255
2256 # Sort merged file with all lists
Michael Bestas1e6efb32021-11-15 19:28:56 +02002257 LC_ALL=C sort -u "$output_list_tmp" > "$output_list"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002258
2259 # Clean-up
2260 rm -f "$output_list_tmp"
2261}
Bruno Martins0f425f12021-04-10 14:57:32 +01002262
2263function colored_echo() {
2264 IFS=" "
2265 local color=$1;
2266 shift
2267 if ! [[ $color =~ '^[0-9]$' ]] ; then
2268 case $(echo $color | tr '[:upper:]' '[:lower:]') in
2269 black) color=0 ;;
2270 red) color=1 ;;
2271 green) color=2 ;;
2272 yellow) color=3 ;;
2273 blue) color=4 ;;
2274 magenta) color=5 ;;
2275 cyan) color=6 ;;
2276 white|*) color=7 ;; # white or invalid color
2277 esac
2278 fi
Bruno Martins5064db22021-06-21 14:47:40 +01002279 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002280 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01002281 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002282}