blob: 635027f49c70f11e8e612c3734e43d6f55a94ec9 [file] [log] [blame]
Steve Kondik5bd66602016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020020PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070021PRODUCT_PACKAGES_LIST=()
22PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020023PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070024PACKAGE_LIST=()
25VENDOR_STATE=-1
26VENDOR_RADIO_STATE=-1
27COMMON=-1
28ARCHES=
29FULLY_DEODEXED=-1
30
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040031TMPDIR=$(mktemp -d)
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +030032HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik5bd66602016-07-15 10:39:58 -070033
34#
35# cleanup
36#
37# kill our tmpfiles with fire on exit
38#
39function cleanup() {
40 rm -rf "${TMPDIR:?}"
41}
42
Gabriele Mb8e54572017-10-11 12:55:51 +020043trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070044
45#
46# setup_vendor
47#
48# $1: device name
49# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000050# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070051# $4: is common device - optional, default to false
52# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050053# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070054#
55# Must be called before any other functions can be used. This
56# sets up the internal state for a new vendor configuration.
57#
58function setup_vendor() {
59 local DEVICE="$1"
60 if [ -z "$DEVICE" ]; then
61 echo "\$DEVICE must be set before including this script!"
62 exit 1
63 fi
64
65 export VENDOR="$2"
66 if [ -z "$VENDOR" ]; then
67 echo "\$VENDOR must be set before including this script!"
68 exit 1
69 fi
70
theimpulson9a911af2019-08-14 03:25:12 +000071 export OMNI_ROOT="$3"
72 if [ ! -d "$OMNI_ROOT" ]; then
73 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070074 exit 1
75 fi
76
77 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000078 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
79 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070080 fi
81
Jake Whatley9843b322017-01-25 21:49:16 -050082 VNDNAME="$6"
83 if [ -z "$VNDNAME" ]; then
84 VNDNAME="$DEVICE"
85 fi
86
theimpulsonbb72ab82019-08-14 06:03:32 +000087 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -070088 export ANDROIDBP="$OMNI_ROOT"/"$OUTDIR"/Android.bp
theimpulson9a911af2019-08-14 03:25:12 +000089 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
90 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070091
92 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
93 COMMON=1
94 else
95 COMMON=0
96 fi
97
Gabriele Mc44696d2017-05-01 18:22:04 +020098 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070099 VENDOR_STATE=1
100 VENDOR_RADIO_STATE=1
101 else
102 VENDOR_STATE=0
103 VENDOR_RADIO_STATE=0
104 fi
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +0300105
106 if [ -z "$PATCHELF" ]; then
107 export PATCHELF="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/patchelf
108 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700109}
110
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300111# Helper functions for parsing a spec.
112# notes: an optional "|SHA1" that may appear in the format is stripped
113# early from the spec in the parse_file_list function, and
114# should not be present inside the input parameter passed
115# to these functions.
116
117#
118# input: spec in the form of "src[:dst][;args]"
119# output: "src"
120#
121function src_file() {
122 local SPEC="$1"
123 local SPLIT=(${SPEC//:/ })
124 local ARGS="$(target_args ${SPEC})"
125 # Regardless of there being a ":" delimiter or not in the spec,
126 # the source file is always either the first, or the only entry.
127 local SRC="${SPLIT[0]}"
128 # Remove target_args suffix, if present
129 echo "${SRC%;${ARGS}}"
130}
131
Steve Kondik5bd66602016-07-15 10:39:58 -0700132#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300133# input: spec in the form of "src[:dst][;args]"
134# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700135#
136function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800137 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300138 local SPLIT=(${SPEC//:/ })
139 local ARGS="$(target_args ${SPEC})"
140 local DST=
141 case ${#SPLIT[@]} in
142 1)
143 # The spec doesn't have a : delimiter
144 DST="${SPLIT[0]}"
145 ;;
146 *)
147 # The spec actually has a src:dst format
148 DST="${SPLIT[1]}"
149 ;;
150 esac
151 # Remove target_args suffix, if present
152 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700153}
154
155#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300156# input: spec in the form of "src[:dst][;args]"
157# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700158#
159function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300160 local SPEC="$1"
161 local SPLIT=(${SPEC//;/ })
162 local ARGS=
163 case ${#SPLIT[@]} in
164 1)
165 # No ";" delimiter in the spec.
166 ;;
167 *)
168 # The "args" are whatever comes after the ";" character.
169 # Basically the spec stripped of whatever is to the left of ";".
170 ARGS="${SPEC#${SPLIT[0]};}"
171 ;;
172 esac
173 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700174}
175
176#
177# prefix_match:
178#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300179# input:
180# - $1: prefix
181# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
182# output:
183# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700184#
185function prefix_match() {
186 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300187 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
188 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700189 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300190 local ARGS=$(target_args "$LINE")
191 if [ -z "${ARGS}" ]; then
192 echo "${FILE#$PREFIX}"
193 else
194 echo "${FILE#$PREFIX};${ARGS}"
195 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700196 fi
197 done
198}
199
200#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400201# prefix_match_file:
202#
203# $1: the prefix to match on
204# $2: the file to match the prefix for
205#
206# Internal function which returns true if a filename contains the
207# specified prefix.
208#
209function prefix_match_file() {
210 local PREFIX="$1"
211 local FILE="$2"
212 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
213 return 0
214 else
215 return 1
216 fi
217}
218
219#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700220# suffix_match_file:
221#
222# $1: the suffix to match on
223# $2: the file to match the suffix for
224#
225# Internal function which returns true if a filename contains the
226# specified suffix.
227#
228function suffix_match_file() {
229 local SUFFIX="$1"
230 local FILE="$2"
231 if [[ "$FILE" = *"$SUFFIX" ]]; then
232 return 0
233 else
234 return 1
235 fi
236}
237
238#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400239# truncate_file
240#
241# $1: the filename to truncate
242# $2: the argument to output the truncated filename to
243#
244# Internal function which truncates a filename by removing the first dir
245# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
246#
247function truncate_file() {
248 local FILE="$1"
249 RETURN_FILE="$2"
250 local FIND="${FILE%%/*}"
251 local LOCATION="${#FIND}+1"
252 echo ${FILE:$LOCATION}
253}
254
255#
Steve Kondik5bd66602016-07-15 10:39:58 -0700256# write_product_copy_files:
257#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400258# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400259#
Steve Kondik5bd66602016-07-15 10:39:58 -0700260# Creates the PRODUCT_COPY_FILES section in the product makefile for all
261# items in the list which do not start with a dash (-).
262#
263function write_product_copy_files() {
264 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
265 local TARGET=
266 local FILE=
267 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400268 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700269
270 if [ "$COUNT" -eq "0" ]; then
271 return 0
272 fi
273
274 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
275 for (( i=1; i<COUNT+1; i++ )); do
276 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
277 LINEEND=" \\"
278 if [ "$i" -eq "$COUNT" ]; then
279 LINEEND=""
280 fi
281
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300282 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400283 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400284 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400285 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400286 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400287 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400288 local OUTTARGET=$(truncate_file $TARGET)
289 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
290 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200291 elif prefix_match_file "system_ext/" $TARGET ; then
292 local OUTTARGET=$(truncate_file $TARGET)
293 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
294 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
295 elif prefix_match_file "system/system_ext/" $TARGET ; then
296 local OUTTARGET=$(truncate_file $TARGET)
297 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
298 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400299 elif prefix_match_file "odm/" $TARGET ; then
300 local OUTTARGET=$(truncate_file $TARGET)
301 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
302 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400303 elif prefix_match_file "vendor/odm/" $TARGET ; then
304 local OUTTARGET=$(truncate_file $TARGET)
305 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
306 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
307 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
308 local OUTTARGET=$(truncate_file $TARGET)
309 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
310 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
311 elif prefix_match_file "vendor/" $TARGET ; then
312 local OUTTARGET=$(truncate_file $TARGET)
313 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
314 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
315 elif prefix_match_file "system/vendor/" $TARGET ; then
316 local OUTTARGET=$(truncate_file $TARGET)
317 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
318 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400319 elif prefix_match_file "system/" $TARGET ; then
320 local OUTTARGET=$(truncate_file $TARGET)
321 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
322 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400323 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400324 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400325 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
326 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700327 done
328 return 0
329}
330
331#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700332# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700333#
334# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200335# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700336# $3: type-specific extra flags
337# $4: Name of the array holding the target list
338#
339# Internal function which writes out the BUILD_PREBUILT stanzas
340# for all modules in the list. This is called by write_product_packages
341# after the modules are categorized.
342#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700343function write_blueprint_packages() {
344
345 local CLASS="$1"
346 local PARTITION="$2"
347 local EXTRA="$3"
348
349 # Yes, this is a horrible hack - we create a new array using indirection
350 local ARR_NAME="$4[@]"
351 local FILELIST=("${!ARR_NAME}")
352
353 local FILE=
354 local ARGS=
355 local BASENAME=
356 local EXTENSION=
357 local PKGNAME=
358 local SRC=
359
360 for P in "${FILELIST[@]}"; do
361 FILE=$(target_file "$P")
362 ARGS=$(target_args "$P")
363
364 BASENAME=$(basename "$FILE")
365 DIRNAME=$(dirname "$FILE")
366 EXTENSION=${BASENAME##*.}
367 PKGNAME=${BASENAME%.*}
368
369 # Add to final package list
370 PACKAGE_LIST+=("$PKGNAME")
371
372 SRC="proprietary"
373 if [ "$PARTITION" = "system" ]; then
374 SRC+="/system"
375 elif [ "$PARTITION" = "vendor" ]; then
376 SRC+="/vendor"
377 elif [ "$PARTITION" = "product" ]; then
378 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200379 elif [ "$PARTITION" = "system_ext" ]; then
380 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700381 elif [ "$PARTITION" = "odm" ]; then
382 SRC+="/odm"
383 fi
384
385 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
386 printf 'cc_prebuilt_library_shared {\n'
387 printf '\tname: "%s",\n' "$PKGNAME"
388 printf '\towner: "%s",\n' "$VENDOR"
389 printf '\tstrip: {\n'
390 printf '\t\tnone: true,\n'
391 printf '\t},\n'
392 printf '\ttarget: {\n'
393 if [ "$EXTRA" = "both" ]; then
394 printf '\t\tandroid_arm: {\n'
395 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
396 printf '\t\t},\n'
397 printf '\t\tandroid_arm64: {\n'
398 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
399 printf '\t\t},\n'
400 elif [ "$EXTRA" = "64" ]; then
401 printf '\t\tandroid_arm64: {\n'
402 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
403 printf '\t\t},\n'
404 else
405 printf '\t\tandroid_arm: {\n'
406 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
407 printf '\t\t},\n'
408 fi
409 printf '\t},\n'
410 if [ "$EXTRA" != "none" ]; then
411 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
412 fi
dianlujitao848101c2020-09-12 00:15:13 +0800413 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700414 elif [ "$CLASS" = "APPS" ]; then
415 printf 'android_app_import {\n'
416 printf '\tname: "%s",\n' "$PKGNAME"
417 printf '\towner: "%s",\n' "$VENDOR"
418 if [ "$EXTRA" = "priv-app" ]; then
419 SRC="$SRC/priv-app"
420 else
421 SRC="$SRC/app"
422 fi
423 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
424 if [ "$ARGS" = "PRESIGNED" ]; then
425 printf '\tpresigned: true,\n'
426 elif [ ! -z "$ARGS" ]; then
427 printf '\tcertificate: "%s",\n' "$ARGS"
428 else
429 printf '\tcertificate: "platform",\n'
430 fi
431 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
432 printf 'dex_import {\n'
433 printf '\tname: "%s",\n' "$PKGNAME"
434 printf '\towner: "%s",\n' "$VENDOR"
435 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
436 elif [ "$CLASS" = "ETC" ]; then
437 if [ "$EXTENSION" = "xml" ]; then
438 printf 'prebuilt_etc_xml {\n'
439 else
440 printf 'prebuilt_etc {\n'
441 fi
442 printf '\tname: "%s",\n' "$PKGNAME"
443 printf '\towner: "%s",\n' "$VENDOR"
444 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200445 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700446 elif [ "$CLASS" = "EXECUTABLES" ]; then
447 if [ "$EXTENSION" = "sh" ]; then
448 printf 'sh_binary {\n'
449 else
450 printf 'cc_prebuilt_binary {\n'
451 fi
452 printf '\tname: "%s",\n' "$PKGNAME"
453 printf '\towner: "%s",\n' "$VENDOR"
454 if [ "$ARGS" = "rootfs" ]; then
455 SRC="$SRC/rootfs"
456 if [ "$EXTRA" = "sbin" ]; then
457 SRC="$SRC/sbin"
458 printf '\tdist {\n'
459 printf '\t\tdest: "%s",\n' "root/sbin"
460 printf '\t},'
461 fi
462 else
463 SRC="$SRC/bin"
464 fi
465 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
466 unset EXTENSION
467 else
468 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
469 fi
470 if [ "$CLASS" = "APPS" ]; then
471 printf '\tdex_preopt: {\n'
472 printf '\t\tenabled: false,\n'
473 printf '\t},\n'
474 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200475 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700476 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200477 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700478 fi
479 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200480 if [ "$CLASS" = "ETC" ] ; then
481 if [ "$DIRNAME" != "." ]; then
482 printf '\tsub_dir: "%s",\n' "$DIRNAME"
483 fi
484 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700485 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
486 printf '\tprefer: true,\n'
487 fi
488 if [ "$EXTRA" = "priv-app" ]; then
489 printf '\tprivileged: true,\n'
490 fi
491 if [ "$PARTITION" = "vendor" ]; then
492 printf '\tsoc_specific: true,\n'
493 elif [ "$PARTITION" = "product" ]; then
494 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200495 elif [ "$PARTITION" = "system_ext" ]; then
496 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700497 elif [ "$PARTITION" = "odm" ]; then
498 printf '\tdevice_specific: true,\n'
499 fi
500 printf '}\n\n'
501 done
502}
503
504#
505# write_makefile_packages:
506#
507# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200508# $2: /odm, /product, /system_ext, or /vendor partition
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700509# $3: type-specific extra flags
510# $4: Name of the array holding the target list
511#
512# Internal function which writes out the BUILD_PREBUILT stanzas
513# for all modules in the list. This is called by write_product_packages
514# after the modules are categorized.
515#
516function write_makefile_packages() {
Steve Kondik5bd66602016-07-15 10:39:58 -0700517
518 local CLASS="$1"
razorlovesa0d296b2019-07-29 02:21:34 -0500519 local PARTITION="$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700520 local EXTRA="$3"
521
522 # Yes, this is a horrible hack - we create a new array using indirection
523 local ARR_NAME="$4[@]"
524 local FILELIST=("${!ARR_NAME}")
525
526 local FILE=
527 local ARGS=
528 local BASENAME=
529 local EXTENSION=
530 local PKGNAME=
531 local SRC=
532
533 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300534 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700535 ARGS=$(target_args "$P")
536
537 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100538 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700539 EXTENSION=${BASENAME##*.}
Mohd Farazcb0f4872019-10-08 16:13:50 +0530540 EXTENSION="."$EXTENSION
541 if [ "$EXTENSION" = ".jar" ]; then
542 EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
543 elif [ "$EXTENSION" = ".apk" ]; then
544 EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
545 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700546 PKGNAME=${BASENAME%.*}
547
548 # Add to final package list
549 PACKAGE_LIST+=("$PKGNAME")
550
551 SRC="proprietary"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400552 if [ "$PARTITION" = "system" ]; then
553 SRC+="/system"
554 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700555 SRC+="/vendor"
razorlovesa0d296b2019-07-29 02:21:34 -0500556 elif [ "$PARTITION" = "product" ]; then
557 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200558 elif [ "$PARTITION" = "system_ext" ]; then
559 SRC+="/system_ext"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700560 elif [ "$PARTITION" = "odm" ]; then
561 SRC+="/odm"
Steve Kondik5bd66602016-07-15 10:39:58 -0700562 fi
563
564 printf 'include $(CLEAR_VARS)\n'
565 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
566 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
567 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
568 if [ "$EXTRA" = "both" ]; then
569 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
570 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
571 #if [ "$VENDOR_PKG" = "true" ]; then
572 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
573 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
574 #else
575 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
576 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
577 #fi
578 elif [ "$EXTRA" = "64" ]; then
579 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
580 else
581 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
582 fi
583 if [ "$EXTRA" != "none" ]; then
584 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
585 fi
586 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200587 if [ "$EXTRA" = "priv-app" ]; then
588 SRC="$SRC/priv-app"
589 else
590 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700591 fi
592 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
593 local CERT=platform
594 if [ ! -z "$ARGS" ]; then
595 CERT="$ARGS"
596 fi
597 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
598 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
599 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200600 local CERT=platform
601 if [ ! -z "$ARGS" ]; then
602 CERT="$ARGS"
603 fi
604 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700605 elif [ "$CLASS" = "ETC" ]; then
606 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
607 elif [ "$CLASS" = "EXECUTABLES" ]; then
608 if [ "$ARGS" = "rootfs" ]; then
609 SRC="$SRC/rootfs"
610 if [ "$EXTRA" = "sbin" ]; then
611 SRC="$SRC/sbin"
612 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
613 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
614 fi
615 else
616 SRC="$SRC/bin"
617 fi
618 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
619 unset EXTENSION
620 else
621 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
622 fi
623 printf 'LOCAL_MODULE_TAGS := optional\n'
624 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400625 if [ "$CLASS" = "APPS" ]; then
626 printf 'LOCAL_DEX_PREOPT := false\n'
627 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700628 if [ ! -z "$EXTENSION" ]; then
Mohd Farazcb0f4872019-10-08 16:13:50 +0530629 printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
Steve Kondik5bd66602016-07-15 10:39:58 -0700630 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100631 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
632 if [ "$DIRNAME" != "." ]; then
633 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
634 fi
635 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700636 if [ "$EXTRA" = "priv-app" ]; then
637 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
638 fi
razorlovesa0d296b2019-07-29 02:21:34 -0500639 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800640 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesa0d296b2019-07-29 02:21:34 -0500641 elif [ "$PARTITION" = "product" ]; then
642 printf 'LOCAL_PRODUCT_MODULE := true\n'
Luca Stefani776be462020-09-09 15:53:58 +0200643 elif [ "$PARTITION" = "system_ext" ]; then
644 printf 'LOCAL_SYSTEM_EXT_MODULE := true\n'
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700645 elif [ "$PARTITION" = "odm" ]; then
646 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700647 fi
648 printf 'include $(BUILD_PREBUILT)\n\n'
649 done
650}
651
652#
653# write_product_packages:
654#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700655# This function will create prebuilt entries in the
656# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700657# product makefile for all files in the blob list which
658# start with a single dash (-) character.
659#
660function write_product_packages() {
661 PACKAGE_LIST=()
662
663 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
664
665 if [ "$COUNT" = "0" ]; then
666 return 0
667 fi
668
669 # Figure out what's 32-bit, what's 64-bit, and what's multilib
670 # I really should not be doing this in bash due to shitty array passing :(
671 local T_LIB32=( $(prefix_match "lib/") )
672 local T_LIB64=( $(prefix_match "lib64/") )
673 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
674 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
675 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
676
677 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700678 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700679 fi
680 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700681 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700682 fi
683 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700684 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700685 fi
686
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400687 local T_S_LIB32=( $(prefix_match "system/lib/") )
688 local T_S_LIB64=( $(prefix_match "system/lib64/") )
689 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
690 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
691 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
692
693 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700694 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400695 fi
696 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700697 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400698 fi
699 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700700 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400701 fi
702
Steve Kondik5bd66602016-07-15 10:39:58 -0700703 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
704 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
705 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
706 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
707 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
708
709 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700710 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700711 fi
712 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700713 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700714 fi
715 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700716 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500717 fi
718
719 local T_P_LIB32=( $(prefix_match "product/lib/") )
720 local T_P_LIB64=( $(prefix_match "product/lib64/") )
721 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
722 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
723 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
724
725 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700726 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500727 fi
728 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700729 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500730 fi
731 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700732 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700733 fi
734
Luca Stefani776be462020-09-09 15:53:58 +0200735 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
736 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
737 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
738 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
739 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
740
741 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
742 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
743 fi
744 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
745 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
746 fi
747 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
748 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
749 fi
750
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700751 local T_O_LIB32=( $(prefix_match "odm/lib/") )
752 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
753 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
754 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
755 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
756
757 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700758 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700759 fi
760 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700761 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700762 fi
763 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700764 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700765 fi
766
Steve Kondik5bd66602016-07-15 10:39:58 -0700767 # Apps
768 local APPS=( $(prefix_match "app/") )
769 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100770 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700771 fi
772 local PRIV_APPS=( $(prefix_match "priv-app/") )
773 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100774 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700775 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400776 local S_APPS=( $(prefix_match "system/app/") )
777 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100778 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400779 fi
780 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
781 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100782 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400783 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700784 local V_APPS=( $(prefix_match "vendor/app/") )
785 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100786 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700787 fi
788 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
789 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100790 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500791 fi
792 local P_APPS=( $(prefix_match "product/app/") )
793 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100794 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500795 fi
796 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
797 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100798 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700799 fi
Luca Stefani776be462020-09-09 15:53:58 +0200800 local SE_APPS=( $(prefix_match "system_ext/app/") )
801 if [ "${#SE_APPS[@]}" -gt "0" ]; then
802 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
803 fi
804 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
805 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
806 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
807 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700808 local O_APPS=( $(prefix_match "odm/app/") )
809 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100810 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700811 fi
812 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
813 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100814 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700815 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700816
817 # Framework
818 local FRAMEWORK=( $(prefix_match "framework/") )
819 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700820 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700821 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400822 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
823 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700824 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400825 fi
Christian Oder974b5902017-10-08 23:15:52 +0200826 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200827 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700828 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500829 fi
830 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
831 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700832 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200833 fi
Luca Stefani776be462020-09-09 15:53:58 +0200834 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700835 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200836 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
837 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700838 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
839 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700840 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700841 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700842
843 # Etc
844 local ETC=( $(prefix_match "etc/") )
845 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700846 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700847 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400848 local S_ETC=( $(prefix_match "system/etc/") )
849 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700850 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400851 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700852 local V_ETC=( $(prefix_match "vendor/etc/") )
853 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700854 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500855 fi
856 local P_ETC=( $(prefix_match "product/etc/") )
857 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700858 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700859 fi
Luca Stefani776be462020-09-09 15:53:58 +0200860 local SE_ETC=( $(prefix_match "system_ext/etc/") )
861 if [ "${#SE_ETC[@]}" -gt "0" ]; then
862 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
863 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700864 local O_ETC=( $(prefix_match "odm/etc/") )
865 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700866 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700867 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700868
869 # Executables
870 local BIN=( $(prefix_match "bin/") )
871 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700872 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700873 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400874 local S_BIN=( $(prefix_match "system/bin/") )
875 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700876 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400877 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700878 local V_BIN=( $(prefix_match "vendor/bin/") )
879 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700880 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500881 fi
882 local P_BIN=( $(prefix_match "product/bin/") )
883 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700884 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700885 fi
Luca Stefani776be462020-09-09 15:53:58 +0200886 local SE_BIN=( $(prefix_match "system_ext/bin/") )
887 if [ "${#SE_BIN[@]}" -gt "0" ]; then
888 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
889 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700890 local O_BIN=( $(prefix_match "odm/bin/") )
891 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700892 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700893 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700894 local SBIN=( $(prefix_match "sbin/") )
895 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700896 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700897 fi
898
899
900 # Actually write out the final PRODUCT_PACKAGES list
901 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
902
903 if [ "$PACKAGE_COUNT" -eq "0" ]; then
904 return 0
905 fi
906
907 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
908 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
909 local LINEEND=" \\"
910 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
911 LINEEND=""
912 fi
913 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
914 done
915}
916
917#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700918# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700919#
920# $1: file which will be written to
921#
922# writes out the copyright header with the current year.
923# note that this is not an append operation, and should
924# be executed first!
925#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700926function write_blueprint_header() {
927 if [ -f $1 ]; then
928 rm $1
929 fi
930
931 YEAR=$(date +"%Y")
932
933 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
934
935 printf "/**\n" > $1
936 NUM_REGEX='^[0-9]+$'
937 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
938 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
939 else
940 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
941 fi
942
943 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
944 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
945 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
946 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
947 else
948 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
949 fi
950
951 cat << EOF >> $1
952 *
953 * Licensed under the Apache License, Version 2.0 (the "License");
954 * you may not use this file except in compliance with the License.
955 * You may obtain a copy of the License at
956 *
957 * http://www.apache.org/licenses/LICENSE-2.0
958 *
959 * Unless required by applicable law or agreed to in writing, software
960 * distributed under the License is distributed on an "AS IS" BASIS,
961 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
962 * See the License for the specific language governing permissions and
963 * limitations under the License.
964 *
965 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
966 */
967
968EOF
969}
970
971#
972# write_makefile_header:
973#
974# $1: file which will be written to
975#
976# writes out the copyright header with the current year.
977# note that this is not an append operation, and should
978# be executed first!
979#
980function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500981 if [ -f $1 ]; then
982 rm $1
983 fi
984
Steve Kondik5bd66602016-07-15 10:39:58 -0700985 YEAR=$(date +"%Y")
986
987 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
988
Jake Whatley9843b322017-01-25 21:49:16 -0500989 NUM_REGEX='^[0-9]+$'
990 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
991 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
992 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
993 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
994 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
995 fi
996 if [ $YEAR -eq 2017 ]; then
997 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
998 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
999 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
1000 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
1001 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
1002 else
1003 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
1004 fi
1005 else
1006 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
1007 fi
1008
1009 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -07001010#
1011# Licensed under the Apache License, Version 2.0 (the "License");
1012# you may not use this file except in compliance with the License.
1013# You may obtain a copy of the License at
1014#
1015# http://www.apache.org/licenses/LICENSE-2.0
1016#
1017# Unless required by applicable law or agreed to in writing, software
1018# distributed under the License is distributed on an "AS IS" BASIS,
1019# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1020# See the License for the specific language governing permissions and
1021# limitations under the License.
1022
1023# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1024
1025EOF
1026}
1027
1028#
1029# write_headers:
1030#
1031# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001032# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001033#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001034# Calls write_makefile_header for each of the makefiles and
1035# write_blueprint_header for Android.bp and creates the initial
1036# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001037#
1038function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001039 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001040
1041 GUARD="$2"
1042 if [ -z "$GUARD" ]; then
1043 GUARD="TARGET_DEVICE"
1044 fi
1045
Steve Kondik5bd66602016-07-15 10:39:58 -07001046 cat << EOF >> "$ANDROIDMK"
1047LOCAL_PATH := \$(call my-dir)
1048
1049EOF
1050 if [ "$COMMON" -ne 1 ]; then
1051 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001052ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001053
1054EOF
1055 else
1056 if [ -z "$1" ]; then
1057 echo "Argument with devices to be added to guard must be set!"
1058 exit 1
1059 fi
1060 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001061ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001062
1063EOF
1064 fi
1065
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001066 write_makefile_header "$BOARDMK"
1067 write_makefile_header "$PRODUCTMK"
1068 write_blueprint_header "$ANDROIDBP"
1069
1070 cat << EOF >> "$ANDROIDBP"
1071soong_namespace {
1072}
1073
1074EOF
1075
1076 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1077 cat << EOF >> "$PRODUCTMK"
1078PRODUCT_SOONG_NAMESPACES += \\
1079 vendor/$VENDOR/$DEVICE
1080
1081EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001082}
1083
1084#
1085# write_footers:
1086#
1087# Closes the inital guard and any other finalization tasks. Must
1088# be called as the final step.
1089#
1090function write_footers() {
1091 cat << EOF >> "$ANDROIDMK"
1092endif
1093EOF
1094}
1095
1096# Return success if adb is up and not in recovery
1097function _adb_connected {
1098 {
Jake Whatley9843b322017-01-25 21:49:16 -05001099 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001100 then
1101 return 0
1102 fi
1103 } 2>/dev/null
1104
1105 return 1
1106};
1107
1108#
1109# parse_file_list:
1110#
1111# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001112# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001113#
1114# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1115#
1116function parse_file_list() {
1117 if [ -z "$1" ]; then
1118 echo "An input file is expected!"
1119 exit 1
1120 elif [ ! -f "$1" ]; then
1121 echo "Input file "$1" does not exist!"
1122 exit 1
1123 fi
1124
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001125 if [ -n "$2" ]; then
1126 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001127 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001128 # Match all lines starting with first line found to start* with '#'
1129 # comment and contain** $2, and ending with first line to be empty*.
1130 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1131 # **the $2 match is case-insensitive
1132 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001133 else
1134 LIST=$1
1135 fi
1136
1137
Steve Kondik5bd66602016-07-15 10:39:58 -07001138 PRODUCT_PACKAGES_LIST=()
1139 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001140 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001141 PRODUCT_COPY_FILES_LIST=()
1142 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001143 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001144
1145 while read -r line; do
1146 if [ -z "$line" ]; then continue; fi
1147
1148 # If the line has a pipe delimiter, a sha1 hash should follow.
1149 # This indicates the file should be pinned and not overwritten
1150 # when extracting files.
1151 local SPLIT=(${line//\|/ })
1152 local COUNT=${#SPLIT[@]}
1153 local SPEC=${SPLIT[0]}
1154 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001155 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001156 if [ "$COUNT" -gt "1" ]; then
1157 HASH=${SPLIT[1]}
1158 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001159 if [ "$COUNT" -gt "2" ]; then
1160 FIXUP_HASH=${SPLIT[2]}
1161 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001162
1163 # if line starts with a dash, it needs to be packaged
1164 if [[ "$SPEC" =~ ^- ]]; then
1165 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1166 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001167 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001168 else
1169 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1170 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001171 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001172 fi
1173
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001174 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001175}
1176
1177#
1178# write_makefiles:
1179#
1180# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001181# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001182#
1183# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001184# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -07001185# the product makefile.
1186#
1187function write_makefiles() {
1188 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001189 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001190 write_product_packages
1191}
1192
1193#
1194# append_firmware_calls_to_makefiles:
1195#
1196# Appends to Android.mk the calls to all images present in radio folder
1197# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1198#
1199function append_firmware_calls_to_makefiles() {
1200 cat << EOF >> "$ANDROIDMK"
1201ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1202
1203RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1204\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1205 \$(call add-radio-file,radio/\$(f)))
1206\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1207
1208endif
1209
1210EOF
1211}
1212
1213#
1214# get_file:
1215#
1216# $1: input file
1217# $2: target file/folder
1218# $3: source of the file (can be "adb" or a local folder)
1219#
1220# Silently extracts the input file to defined target
1221# Returns success if file can be pulled from the device or found locally
1222#
1223function get_file() {
1224 local SRC="$3"
1225
1226 if [ "$SRC" = "adb" ]; then
1227 # try to pull
1228 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1229
1230 return 1
1231 else
1232 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001233 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1234 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001235 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001236
1237 return 1
1238 fi
1239};
1240
1241#
1242# oat2dex:
1243#
1244# $1: extracted apk|jar (to check if deodex is required)
1245# $2: odexed apk|jar to deodex
1246# $3: source of the odexed apk|jar
1247#
1248# Convert apk|jar .odex in the corresposing classes.dex
1249#
1250function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001251 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001252 local OEM_TARGET="$2"
1253 local SRC="$3"
1254 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001255 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001256
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001257 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1258 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1259 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001260 fi
1261
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001262 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001263 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001264 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001265
codeworkx85eda752018-09-23 12:36:57 +02001266 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001267 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001268 fi
1269
Steve Kondik5bd66602016-07-15 10:39:58 -07001270 # Extract existing boot.oats to the temp folder
1271 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001272 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001273 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001274 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001275 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001276 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001277 else
1278 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001279 fi
1280 done
1281 fi
1282
1283 if [ -z "$ARCHES" ]; then
1284 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1285 fi
1286
theimpulson9a911af2019-08-14 03:25:12 +00001287 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001288 return;
1289 fi
1290
theimpulson9a911af2019-08-14 03:25:12 +00001291 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001292 return 0 # target apk|jar is already odexed, return
1293 fi
1294
1295 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001296 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001297
Joe Maplesfb3941c2018-01-05 14:51:33 -05001298 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1299 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001300
Joe Maplesfb3941c2018-01-05 14:51:33 -05001301 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1302 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1303 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001304 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1305 for CLASS in $CLASSES; do
1306 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1307 # Check if we have to deal with CompactDex
1308 if [[ "$CLASS" == *.cdex ]]; then
1309 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1310 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1311 else
1312 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1313 fi
1314 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001315 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001316 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1317 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001318 fi
theimpulson9a911af2019-08-14 03:25:12 +00001319 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001320 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001321 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001322 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001323 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001324 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001325 # try to extract classes.dex from boot.vdex for frameworks jars
1326 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001327 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001328 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001329 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1330 for CLASS in $CLASSES; do
1331 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1332 # Check if we have to deal with CompactDex
1333 if [[ "$CLASS" == *.cdex ]]; then
1334 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1335 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1336 else
1337 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1338 fi
1339 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001340 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001341 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1342 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001343 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001344 else
1345 continue
1346 fi
1347
Steve Kondik5bd66602016-07-15 10:39:58 -07001348 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001349
1350 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001351}
1352
1353#
1354# init_adb_connection:
1355#
1356# Starts adb server and waits for the device
1357#
1358function init_adb_connection() {
1359 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1360 if ! _adb_connected; then
1361 echo "No device is online. Waiting for one..."
1362 echo "Please connect USB and/or enable USB debugging"
1363 until _adb_connected; do
1364 sleep 1
1365 done
1366 echo "Device Found."
1367 fi
1368
1369 # Retrieve IP and PORT info if we're using a TCP connection
1370 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1371 | head -1 | awk '{print $1}')
1372 adb root &> /dev/null
1373 sleep 0.3
1374 if [ -n "$TCPIPPORT" ]; then
1375 # adb root just killed our connection
1376 # so reconnect...
1377 adb connect "$TCPIPPORT"
1378 fi
1379 adb wait-for-device &> /dev/null
1380 sleep 0.3
1381}
1382
1383#
1384# fix_xml:
1385#
1386# $1: xml file to fix
1387#
1388function fix_xml() {
1389 local XML="$1"
1390 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1391
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001392 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1393 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001394
1395 mv "$TEMP_XML" "$XML"
1396}
1397
Vladimir Olteande985fe2019-01-17 03:07:34 +02001398function get_hash() {
1399 local FILE="$1"
1400
1401 if [ "$(uname)" == "Darwin" ]; then
1402 shasum "${FILE}" | awk '{print $1}'
1403 else
1404 sha1sum "${FILE}" | awk '{print $1}'
1405 fi
1406}
1407
Vladimir Olteana7d20492019-01-17 03:05:52 +02001408function print_spec() {
1409 local SPEC_PRODUCT_PACKAGE="$1"
1410 local SPEC_SRC_FILE="$2"
1411 local SPEC_DST_FILE="$3"
1412 local SPEC_ARGS="$4"
1413 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001414 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001415
1416 local PRODUCT_PACKAGE=""
1417 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1418 PRODUCT_PACKAGE="-"
1419 fi
1420 local SRC=""
1421 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1422 SRC="${SPEC_SRC_FILE}:"
1423 fi
1424 local DST=""
1425 if [ ! -z "${SPEC_DST_FILE}" ]; then
1426 DST="${SPEC_DST_FILE}"
1427 fi
1428 local ARGS=""
1429 if [ ! -z "${SPEC_ARGS}" ]; then
1430 ARGS=";${SPEC_ARGS}"
1431 fi
1432 local HASH=""
1433 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1434 HASH="|${SPEC_HASH}"
1435 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001436 local FIXUP_HASH=""
1437 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1438 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1439 fi
1440 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1441}
1442
1443# To be overridden by device-level extract-files.sh
1444# Parameters:
1445# $1: spec name of a blob. Can be used for filtering.
1446# If the spec is "src:dest", then $1 is "dest".
1447# If the spec is "src", then $1 is "src".
1448# $2: path to blob file. Can be used for fixups.
1449#
1450function blob_fixup() {
1451 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001452}
1453
Steve Kondik5bd66602016-07-15 10:39:58 -07001454#
1455# extract:
1456#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001457# Positional parameters:
1458# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001459# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001460# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1461#
1462# Non-positional parameters (coming after $2):
1463# --section: preferred way of selecting the portion to parse and extract from
1464# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001465# --kang: if present, this option will activate the printing of hashes for the
1466# extracted blobs. Useful with --section for subsequent pinning of
1467# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001468#
1469function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001470 # Consume positional parameters
1471 local PROPRIETARY_FILES_TXT="$1"; shift
1472 local SRC="$1"; shift
1473 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001474 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001475
1476 # Consume optional, non-positional parameters
1477 while [ "$#" -gt 0 ]; do
1478 case "$1" in
1479 -s|--section)
1480 SECTION="$2"; shift
1481 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001482 -k|--kang)
1483 KANG=true
1484 DISABLE_PINNING=1
1485 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001486 *)
1487 # Backwards-compatibility with the old behavior, where $3, if
1488 # present, denoted an optional positional ${SECTION} argument.
1489 # Users of ${SECTION} are encouraged to migrate from setting it as
1490 # positional $3, to non-positional --section ${SECTION}, the
1491 # reason being that it doesn't scale to have more than 1 optional
1492 # positional argument.
1493 SECTION="$1"
1494 ;;
1495 esac
1496 shift
1497 done
1498
Steve Kondik5bd66602016-07-15 10:39:58 -07001499 if [ -z "$OUTDIR" ]; then
1500 echo "Output dir not set!"
1501 exit 1
1502 fi
1503
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001504 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001505
1506 # Allow failing, so we can try $DEST and/or $FILE
1507 set +e
1508
1509 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1510 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001511 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001512 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001513 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001514 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001515 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1516
1517 if [ "$SRC" = "adb" ]; then
1518 init_adb_connection
1519 fi
1520
Dan Pasanen0cc05012017-03-21 09:06:11 -05001521 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001522 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001523
1524 # Check if we're working with the same zip that was passed last time.
1525 # If so, let's just use what's already extracted.
1526 MD5=`md5sum "$SRC"| awk '{print $1}'`
1527 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1528
1529 if [ "$MD5" != "$OLDMD5" ]; then
1530 rm -rf "$DUMPDIR"
1531 mkdir "$DUMPDIR"
1532 unzip "$SRC" -d "$DUMPDIR"
1533 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1534
1535 # Stop if an A/B OTA zip is detected. We cannot extract these.
1536 if [ -a "$DUMPDIR"/payload.bin ]; then
1537 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1538 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001539 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001540
Luca Stefani776be462020-09-09 15:53:58 +02001541 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001542 do
1543 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001544 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1545 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1546 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1547 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1548 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001549 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1550 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1551 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1552 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1553 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1554 echo "Requesting sudo access to mount the "$PARTITION".img"
1555 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1556 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1557 sudo umount "$DUMPDIR"/tmp
1558 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1559 fi
1560 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001561 fi
1562
1563 SRC="$DUMPDIR"
1564 fi
1565
Steve Kondik5bd66602016-07-15 10:39:58 -07001566 if [ "$VENDOR_STATE" -eq "0" ]; then
1567 echo "Cleaning output directory ($OUTPUT_ROOT).."
1568 rm -rf "${OUTPUT_TMP:?}"
1569 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001570 if [ -d "$OUTPUT_ROOT" ]; then
1571 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1572 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001573 VENDOR_STATE=1
1574 fi
1575
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001576 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001577
1578 for (( i=1; i<COUNT+1; i++ )); do
1579
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001580 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001581 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001582 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001583 local OUTPUT_DIR=
1584 local TMP_DIR=
1585 local SRC_FILE=
1586 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001587 local IS_PRODUCT_PACKAGE=false
1588
1589 # Note: this relies on the fact that the ${FILELIST[@]} array
1590 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1591 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1592 IS_PRODUCT_PACKAGE=true
1593 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001594
Vladimir Olteand6391332018-06-24 20:42:01 +03001595 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001596 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1597 TMP_DIR="${OUTPUT_TMP}/rootfs"
1598 SRC_FILE="/${SPEC_SRC_FILE}"
1599 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001600 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001601 OUTPUT_DIR="${OUTPUT_ROOT}"
1602 TMP_DIR="${OUTPUT_TMP}"
1603 SRC_FILE="/system/${SPEC_SRC_FILE}"
1604 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001605 fi
1606
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001607 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001608 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001609 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001610 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001611
Gabriele M58270a32017-11-13 23:15:29 +01001612 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001613 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001614 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001615 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001616 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001617 if [ -f "${VENDOR_REPO_FILE}" ]; then
1618 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001619 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001620 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001621 fi
1622 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001623 local TMP_HASH=$(get_hash "${PINNED}")
1624 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001625 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001626 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1627 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001628 fi
1629 fi
1630 fi
1631 fi
1632
Vladimir Olteana7d20492019-01-17 03:05:52 +02001633 if [ "${KANG}" = false ]; then
1634 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1635 fi
1636
Gabriele M58270a32017-11-13 23:15:29 +01001637 if [ "$KEEP" = "1" ]; then
Vladimir Olteana7d20492019-01-17 03:05:52 +02001638 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001639 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001640 FOUND=false
1641 # Try Lineage target first.
1642 # Also try to search for files stripped of
1643 # the "/system" prefix, if we're actually extracting
1644 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001645 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001646 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1647 FOUND=true
1648 break
1649 }
1650 done
1651
1652 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001653 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001654 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001655 fi
1656 fi
1657
Vladimir Olteande985fe2019-01-17 03:07:34 +02001658 # Blob fixup pipeline has 2 parts: one that is fixed and
1659 # one that is user-configurable
1660 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1661 # Deodex apk|jar if that's the case
1662 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1663 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1664 if [ -f "$TMPDIR/classes.dex" ]; then
dianlujitaoded7c1e2020-04-06 12:45:36 +08001665 touch -t 200901010000 "$TMPDIR/classes"*
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001666 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1667 rm "$TMPDIR/classes"*
Vladimir Olteande985fe2019-01-17 03:07:34 +02001668 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001669 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001670 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1671 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001672 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001673 # Now run user-supplied fixup function
1674 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1675 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001676
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001677 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001678 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001679 local TYPE="${DIR##*/}"
1680 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001681 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001682 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001683 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001684 fi
1685 fi
1686
Vladimir Olteana7d20492019-01-17 03:05:52 +02001687 if [ "${KANG}" = true ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001688 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1689 fi
1690
1691 # Check and print whether the fixup pipeline actually did anything.
1692 # This isn't done right after the fixup pipeline because we want this print
1693 # to come after print_spec above, when in kang mode.
1694 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1695 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1696 # Now sanity-check the spec for this blob.
1697 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1698 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1699 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1700 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001701 fi
1702
Steve Kondik5bd66602016-07-15 10:39:58 -07001703 done
1704
1705 # Don't allow failing
1706 set -e
1707}
1708
1709#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001710# extract2:
1711#
1712# Positional parameters:
1713# $1: file containing the list of items to extract (aka proprietary-files.txt)
1714#
1715# Non-positional parameters (coming after $2):
1716# --section: selects the portion to parse and extracts from proprietary-files.txt
1717# --kang: if present, this option will activate the printing of hashes for the
1718# extracted blobs. Useful with --section for subsequent pinning of
1719# blobs taken from other origins.
1720#
1721function extract2() {
1722 # Consume positional parameters
1723 local PROPRIETARY_FILES_TXT="$1"; shift
1724 local SECTION=""
1725 local KANG=false
1726
1727 # Consume optional, non-positional parameters
1728 while [ "$#" -gt 0 ]; do
1729 case "$1" in
1730 --adb)
1731 ADB=true
1732 ;;
1733 --system)
1734 SYSTEM_SRC="$2"; shift
1735 ;;
1736 --vendor)
1737 VENDOR_SRC="$2"; shift
1738 ;;
1739 --odm)
1740 ODM_SRC="$2"; shift
1741 ;;
1742 --product)
1743 PRODUCT_SRC="$2"; shift
1744 ;;
1745 -s|--section)
1746 SECTION="$2"; shift
1747 ;;
1748 -k|--kang)
1749 KANG=true
1750 DISABLE_PINNING=1
1751 ;;
1752 esac
1753 shift
1754 done
1755
1756 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1757 echo "No sources set! You must select --adb or pass paths to partition dumps."
1758 exit 1
1759 fi
1760
1761 if [ -z "$OUTDIR" ]; then
1762 echo "Output dir not set!"
1763 exit 1
1764 fi
1765
1766 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1767
1768 # Allow failing, so we can try $DEST and/or $FILE
1769 set +e
1770
1771 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1772 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1773 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1774 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1775 local COUNT=${#FILELIST[@]}
1776 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1777 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1778
1779 if [ "$ADB" = true ]; then
1780 init_adb_connection
1781 fi
1782
1783 if [ "$VENDOR_STATE" -eq "0" ]; then
1784 echo "Cleaning output directory ($OUTPUT_ROOT).."
1785 rm -rf "${OUTPUT_TMP:?}"
1786 mkdir -p "${OUTPUT_TMP:?}"
1787 if [ -d "$OUTPUT_ROOT" ]; then
1788 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1789 fi
1790 VENDOR_STATE=1
1791 fi
1792
1793 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1794
1795 for (( i=1; i<COUNT+1; i++ )); do
1796
1797 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1798 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1799 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1800 local OUTPUT_DIR=
1801 local TMP_DIR=
1802 local SRC_FILE=
1803 local DST_FILE=
1804 local IS_PRODUCT_PACKAGE=false
1805
1806 # Note: this relies on the fact that the ${FILELIST[@]} array
1807 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1808 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1809 IS_PRODUCT_PACKAGE=true
1810 fi
1811
1812 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1813 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1814 TMP_DIR="${OUTPUT_TMP}/rootfs"
1815 else
1816 OUTPUT_DIR="${OUTPUT_ROOT}"
1817 TMP_DIR="${OUTPUT_TMP}"
1818 fi
1819 SRC_FILE="${SPEC_SRC_FILE}"
1820 DST_FILE="${SPEC_DST_FILE}"
1821
1822 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1823 local BLOB_DISPLAY_NAME="${DST_FILE}"
1824 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1825
1826 # Check pinned files
1827 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1828 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1829 local KEEP=""
1830 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1831 if [ -f "${VENDOR_REPO_FILE}" ]; then
1832 local PINNED="${VENDOR_REPO_FILE}"
1833 else
1834 local PINNED="${TMP_DIR}${DST_FILE}"
1835 fi
1836 if [ -f "$PINNED" ]; then
1837 local TMP_HASH=$(get_hash "${PINNED}")
1838 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1839 KEEP="1"
1840 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1841 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1842 fi
1843 fi
1844 fi
1845 fi
1846
1847 if [ "${KANG}" = false ]; then
1848 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1849 fi
1850
1851 if [ "$KEEP" = "1" ]; then
1852 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1853 else
1854 FOUND=false
1855 PARTITION_SOURCE_DIR=
1856 # Try Lineage target first.
1857 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1858 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1859 if [ "$PARTITION" = "system" ]; then
1860 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1861 elif [ "$PARTITION" = "vendor" ]; then
1862 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1863 elif [ "$PARTITION" = "product" ]; then
1864 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1865 elif [ "$PARTITION" = "odm" ]; then
1866 PARTITION_SOURCE_DIR="$ODM_SRC"
1867 fi
1868 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1869 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1870 FOUND=true
1871 break
1872 }
1873 # Search with the full system/ prefix if the file was not found on the system partition
1874 # because we may be searching in a mounted system-as-root system.img
1875 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1876 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1877 FOUND=true
1878 break
1879 }
1880 fi
1881 done
1882
1883 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1884 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1885 fi
1886
1887 if [ "${FOUND}" = false ]; then
1888 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1889 continue
1890 fi
1891 fi
1892
1893 # Blob fixup pipeline has 2 parts: one that is fixed and
1894 # one that is user-configurable
1895 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1896 # Deodex apk|jar if that's the case
1897 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1898 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1899 if [ -f "$TMPDIR/classes.dex" ]; then
1900 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1901 rm "$TMPDIR/classes"*
1902 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1903 fi
1904 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1905 fix_xml "${VENDOR_REPO_FILE}"
1906 fi
1907 # Now run user-supplied fixup function
1908 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1909 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1910
1911 if [ -f "${VENDOR_REPO_FILE}" ]; then
1912 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1913 local TYPE="${DIR##*/}"
1914 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1915 chmod 755 "${VENDOR_REPO_FILE}"
1916 else
1917 chmod 644 "${VENDOR_REPO_FILE}"
1918 fi
1919 fi
1920
1921 if [ "${KANG}" = true ]; then
1922 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1923 fi
1924
1925 # Check and print whether the fixup pipeline actually did anything.
1926 # This isn't done right after the fixup pipeline because we want this print
1927 # to come after print_spec above, when in kang mode.
1928 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1929 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1930 # Now sanity-check the spec for this blob.
1931 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1932 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1933 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1934 fi
1935 fi
1936
1937 done
1938
1939 # Don't allow failing
1940 set -e
1941}
1942
1943#
Steve Kondik5bd66602016-07-15 10:39:58 -07001944# extract_firmware:
1945#
1946# $1: file containing the list of items to extract
1947# $2: path to extracted radio folder
1948#
1949function extract_firmware() {
1950 if [ -z "$OUTDIR" ]; then
1951 echo "Output dir not set!"
1952 exit 1
1953 fi
1954
1955 parse_file_list "$1"
1956
1957 # Don't allow failing
1958 set -e
1959
1960 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1961 local COUNT=${#FILELIST[@]}
1962 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001963 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001964
1965 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1966 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1967 rm -rf "${OUTPUT_DIR:?}/"*
1968 VENDOR_RADIO_STATE=1
1969 fi
1970
1971 echo "Extracting $COUNT files in $1 from $SRC:"
1972
1973 for (( i=1; i<COUNT+1; i++ )); do
1974 local FILE="${FILELIST[$i-1]}"
1975 printf ' - %s \n' "/radio/$FILE"
1976
1977 if [ ! -d "$OUTPUT_DIR" ]; then
1978 mkdir -p "$OUTPUT_DIR"
1979 fi
1980 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1981 chmod 644 "$OUTPUT_DIR/$FILE"
1982 done
1983}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001984
1985function extract_img_data() {
1986 local image_file="$1"
1987 local out_dir="$2"
1988 local logFile="$TMPDIR/debugfs.log"
1989
1990 if [ ! -d "$out_dir" ]; then
1991 mkdir -p "$out_dir"
1992 fi
1993
1994 if [[ "$HOST_OS" == "Darwin" ]]; then
1995 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1996 echo "[-] Failed to extract data from '$image_file'"
1997 abort 1
1998 }
1999 else
2000 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2001 do
2002 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2003 echo "[-] Failed to extract data from '$image_file'"
2004 abort 1
2005 }
2006 done
2007 fi
2008
2009 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2010 if grep -Fq "$symlink_err" "$logFile"; then
2011 echo "[-] Symlinks have not been properly processed from $image_file"
2012 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
2013 abort 1
2014 fi
2015}
2016
2017declare -ra VENDOR_SKIP_FILES=(
2018 "bin/toybox_vendor"
2019 "bin/toolbox"
2020 "bin/grep"
2021 "build.prop"
2022 "compatibility_matrix.xml"
2023 "default.prop"
2024 "etc/NOTICE.xml.gz"
2025 "etc/vintf/compatibility_matrix.xml"
2026 "etc/vintf/manifest.xml"
2027 "etc/wifi/wpa_supplicant.conf"
2028 "manifest.xml"
2029 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2030 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2031 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2032 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2033 "overlay/framework-res__auto_generated_rro.apk"
2034 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2035)
2036
2037function array_contains() {
2038 local element
2039 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2040 return 1
2041}
2042
2043function generate_prop_list_from_image() {
2044 local image_file="$1"
2045 local image_dir="$TMPDIR/image-temp"
2046 local output_list="$2"
2047 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
2048 local -n skipped_vendor_files="$3"
2049
2050 extract_img_data "$image_file" "$image_dir"
2051
2052 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2053 do
2054 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2055 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2056 continue
2057 fi
2058 # Skip device defined skipped files since they will be re-generated at build time
2059 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
2060 continue
2061 fi
2062 if suffix_match_file ".apk" "$FILE" ; then
2063 echo "-vendor/$FILE" >> "$output_list_tmp"
2064 else
2065 echo "vendor/$FILE" >> "$output_list_tmp"
2066 fi
2067 done
2068
2069 # Sort merged file with all lists
2070 sort -u "$output_list_tmp" > "$output_list"
2071
2072 # Clean-up
2073 rm -f "$output_list_tmp"
2074}