blob: f0e87179f43e88728f8196db48ea5a363a71c006 [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=
TheStrix6e24acc2020-04-10 18:20:19 +0530359 local OVERRIDEPKG=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700360
361 for P in "${FILELIST[@]}"; do
362 FILE=$(target_file "$P")
363 ARGS=$(target_args "$P")
364
365 BASENAME=$(basename "$FILE")
366 DIRNAME=$(dirname "$FILE")
367 EXTENSION=${BASENAME##*.}
368 PKGNAME=${BASENAME%.*}
369
370 # Add to final package list
371 PACKAGE_LIST+=("$PKGNAME")
372
373 SRC="proprietary"
374 if [ "$PARTITION" = "system" ]; then
375 SRC+="/system"
376 elif [ "$PARTITION" = "vendor" ]; then
377 SRC+="/vendor"
378 elif [ "$PARTITION" = "product" ]; then
379 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200380 elif [ "$PARTITION" = "system_ext" ]; then
381 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700382 elif [ "$PARTITION" = "odm" ]; then
383 SRC+="/odm"
384 fi
385
386 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
387 printf 'cc_prebuilt_library_shared {\n'
388 printf '\tname: "%s",\n' "$PKGNAME"
389 printf '\towner: "%s",\n' "$VENDOR"
390 printf '\tstrip: {\n'
391 printf '\t\tnone: true,\n'
392 printf '\t},\n'
393 printf '\ttarget: {\n'
394 if [ "$EXTRA" = "both" ]; then
395 printf '\t\tandroid_arm: {\n'
396 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
397 printf '\t\t},\n'
398 printf '\t\tandroid_arm64: {\n'
399 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
400 printf '\t\t},\n'
401 elif [ "$EXTRA" = "64" ]; then
402 printf '\t\tandroid_arm64: {\n'
403 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
404 printf '\t\t},\n'
405 else
406 printf '\t\tandroid_arm: {\n'
407 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
408 printf '\t\t},\n'
409 fi
410 printf '\t},\n'
411 if [ "$EXTRA" != "none" ]; then
412 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
413 fi
dianlujitao848101c2020-09-12 00:15:13 +0800414 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700415 elif [ "$CLASS" = "APPS" ]; then
416 printf 'android_app_import {\n'
417 printf '\tname: "%s",\n' "$PKGNAME"
418 printf '\towner: "%s",\n' "$VENDOR"
419 if [ "$EXTRA" = "priv-app" ]; then
420 SRC="$SRC/priv-app"
421 else
422 SRC="$SRC/app"
423 fi
424 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
TheStrix6e24acc2020-04-10 18:20:19 +0530425 ARGS=(${ARGS//;/ })
LuK1337508e85f2021-08-23 18:18:57 +0200426 USE_PLATFORM_CERTIFICATE="true"
427 for ARG in "${ARGS[@]}"; do
428 if [ "$ARG" = "PRESIGNED" ]; then
429 USE_PLATFORM_CERTIFICATE="false"
430 printf '\tpresigned: true,\n'
431 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
432 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200433 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200434 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
435 elif [ ! -z "$ARG" ]; then
436 USE_PLATFORM_CERTIFICATE="false"
437 printf '\tcertificate: "%s",\n' "$ARG"
438 fi
439 done
440 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700441 printf '\tcertificate: "platform",\n'
442 fi
443 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
444 printf 'dex_import {\n'
445 printf '\tname: "%s",\n' "$PKGNAME"
446 printf '\towner: "%s",\n' "$VENDOR"
447 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
448 elif [ "$CLASS" = "ETC" ]; then
449 if [ "$EXTENSION" = "xml" ]; then
450 printf 'prebuilt_etc_xml {\n'
451 else
452 printf 'prebuilt_etc {\n'
453 fi
454 printf '\tname: "%s",\n' "$PKGNAME"
455 printf '\towner: "%s",\n' "$VENDOR"
456 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200457 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700458 elif [ "$CLASS" = "EXECUTABLES" ]; then
459 if [ "$EXTENSION" = "sh" ]; then
460 printf 'sh_binary {\n'
461 else
462 printf 'cc_prebuilt_binary {\n'
463 fi
464 printf '\tname: "%s",\n' "$PKGNAME"
465 printf '\towner: "%s",\n' "$VENDOR"
Michael Bestasbda30202020-12-28 04:44:52 +0200466 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200467 if [ "$EXTENSION" != "sh" ]; then
468 printf '\tcheck_elf_files: false,\n'
469 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700470 unset EXTENSION
471 else
472 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
473 fi
474 if [ "$CLASS" = "APPS" ]; then
475 printf '\tdex_preopt: {\n'
476 printf '\t\tenabled: false,\n'
477 printf '\t},\n'
478 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200479 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700480 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200481 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700482 fi
483 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200484 if [ "$CLASS" = "ETC" ] ; then
485 if [ "$DIRNAME" != "." ]; then
486 printf '\tsub_dir: "%s",\n' "$DIRNAME"
487 fi
488 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700489 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
490 printf '\tprefer: true,\n'
491 fi
492 if [ "$EXTRA" = "priv-app" ]; then
493 printf '\tprivileged: true,\n'
494 fi
495 if [ "$PARTITION" = "vendor" ]; then
496 printf '\tsoc_specific: true,\n'
497 elif [ "$PARTITION" = "product" ]; then
498 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200499 elif [ "$PARTITION" = "system_ext" ]; then
500 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700501 elif [ "$PARTITION" = "odm" ]; then
502 printf '\tdevice_specific: true,\n'
503 fi
504 printf '}\n\n'
505 done
506}
507
508#
Steve Kondik5bd66602016-07-15 10:39:58 -0700509# write_product_packages:
510#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700511# This function will create prebuilt entries in the
512# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700513# product makefile for all files in the blob list which
514# start with a single dash (-) character.
515#
516function write_product_packages() {
517 PACKAGE_LIST=()
518
519 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
520
521 if [ "$COUNT" = "0" ]; then
522 return 0
523 fi
524
525 # Figure out what's 32-bit, what's 64-bit, and what's multilib
526 # I really should not be doing this in bash due to shitty array passing :(
527 local T_LIB32=( $(prefix_match "lib/") )
528 local T_LIB64=( $(prefix_match "lib64/") )
529 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
530 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
531 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
532
533 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700534 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700535 fi
536 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700537 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700538 fi
539 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700540 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700541 fi
542
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400543 local T_S_LIB32=( $(prefix_match "system/lib/") )
544 local T_S_LIB64=( $(prefix_match "system/lib64/") )
545 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
546 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
547 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
548
549 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700550 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400551 fi
552 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700553 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400554 fi
555 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700556 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400557 fi
558
Steve Kondik5bd66602016-07-15 10:39:58 -0700559 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
560 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
561 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
562 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
563 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
564
565 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700566 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700567 fi
568 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700569 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700570 fi
571 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700572 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500573 fi
574
575 local T_P_LIB32=( $(prefix_match "product/lib/") )
576 local T_P_LIB64=( $(prefix_match "product/lib64/") )
577 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
578 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
579 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
580
581 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700582 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500583 fi
584 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700585 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500586 fi
587 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700588 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700589 fi
590
Luca Stefani776be462020-09-09 15:53:58 +0200591 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
592 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
593 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
594 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
595 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
596
597 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
598 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
599 fi
600 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
601 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
602 fi
603 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
604 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
605 fi
606
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700607 local T_O_LIB32=( $(prefix_match "odm/lib/") )
608 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
609 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
610 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
611 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
612
613 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700614 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700615 fi
616 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700617 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700618 fi
619 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700620 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700621 fi
622
Steve Kondik5bd66602016-07-15 10:39:58 -0700623 # Apps
624 local APPS=( $(prefix_match "app/") )
625 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100626 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700627 fi
628 local PRIV_APPS=( $(prefix_match "priv-app/") )
629 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100630 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700631 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400632 local S_APPS=( $(prefix_match "system/app/") )
633 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100634 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400635 fi
636 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
637 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100638 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400639 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700640 local V_APPS=( $(prefix_match "vendor/app/") )
641 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100642 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700643 fi
644 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
645 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100646 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500647 fi
648 local P_APPS=( $(prefix_match "product/app/") )
649 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100650 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500651 fi
652 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
653 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100654 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700655 fi
Luca Stefani776be462020-09-09 15:53:58 +0200656 local SE_APPS=( $(prefix_match "system_ext/app/") )
657 if [ "${#SE_APPS[@]}" -gt "0" ]; then
658 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
659 fi
660 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
661 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
662 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
663 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700664 local O_APPS=( $(prefix_match "odm/app/") )
665 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100666 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700667 fi
668 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
669 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100670 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700671 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700672
673 # Framework
674 local FRAMEWORK=( $(prefix_match "framework/") )
675 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700676 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700677 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400678 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
679 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700680 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400681 fi
Christian Oder974b5902017-10-08 23:15:52 +0200682 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200683 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700684 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500685 fi
686 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
687 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700688 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200689 fi
Luca Stefani776be462020-09-09 15:53:58 +0200690 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700691 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200692 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
693 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700694 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
695 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700696 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700697 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700698
699 # Etc
700 local ETC=( $(prefix_match "etc/") )
701 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700702 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700703 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400704 local S_ETC=( $(prefix_match "system/etc/") )
705 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700706 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400707 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700708 local V_ETC=( $(prefix_match "vendor/etc/") )
709 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700710 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500711 fi
712 local P_ETC=( $(prefix_match "product/etc/") )
713 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700714 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700715 fi
Luca Stefani776be462020-09-09 15:53:58 +0200716 local SE_ETC=( $(prefix_match "system_ext/etc/") )
717 if [ "${#SE_ETC[@]}" -gt "0" ]; then
718 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
719 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700720 local O_ETC=( $(prefix_match "odm/etc/") )
721 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700722 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700723 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700724
725 # Executables
726 local BIN=( $(prefix_match "bin/") )
727 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700728 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700729 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400730 local S_BIN=( $(prefix_match "system/bin/") )
731 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700732 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400733 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700734 local V_BIN=( $(prefix_match "vendor/bin/") )
735 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700736 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500737 fi
738 local P_BIN=( $(prefix_match "product/bin/") )
739 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700740 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700741 fi
Luca Stefani776be462020-09-09 15:53:58 +0200742 local SE_BIN=( $(prefix_match "system_ext/bin/") )
743 if [ "${#SE_BIN[@]}" -gt "0" ]; then
744 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
745 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700746 local O_BIN=( $(prefix_match "odm/bin/") )
747 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700748 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700749 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700750
751 # Actually write out the final PRODUCT_PACKAGES list
752 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
753
754 if [ "$PACKAGE_COUNT" -eq "0" ]; then
755 return 0
756 fi
757
758 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
759 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
760 local LINEEND=" \\"
761 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
762 LINEEND=""
763 fi
764 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
765 done
766}
767
768#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700769# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700770#
771# $1: file which will be written to
772#
Michael Bestasa2934df2020-12-19 03:50:32 +0200773# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -0700774# note that this is not an append operation, and should
775# be executed first!
776#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700777function write_blueprint_header() {
778 if [ -f $1 ]; then
779 rm $1
780 fi
781
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700782 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
783
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700784 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200785// Automatically generated file. DO NOT MODIFY
786//
787// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700788
789EOF
790}
791
792#
793# write_makefile_header:
794#
795# $1: file which will be written to
796#
Michael Bestasa2934df2020-12-19 03:50:32 +0200797# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700798# note that this is not an append operation, and should
799# be executed first!
800#
801function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500802 if [ -f $1 ]; then
803 rm $1
804 fi
805
Steve Kondik5bd66602016-07-15 10:39:58 -0700806 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
807
Jake Whatley9843b322017-01-25 21:49:16 -0500808 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200809# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -0700810#
Steve Kondik5bd66602016-07-15 10:39:58 -0700811# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
812
813EOF
814}
815
816#
817# write_headers:
818#
819# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500820# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700821#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700822# Calls write_makefile_header for each of the makefiles and
823# write_blueprint_header for Android.bp and creates the initial
824# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -0700825#
826function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700827 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500828
829 GUARD="$2"
830 if [ -z "$GUARD" ]; then
831 GUARD="TARGET_DEVICE"
832 fi
833
Steve Kondik5bd66602016-07-15 10:39:58 -0700834 cat << EOF >> "$ANDROIDMK"
835LOCAL_PATH := \$(call my-dir)
836
837EOF
838 if [ "$COMMON" -ne 1 ]; then
839 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500840ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700841
842EOF
843 else
844 if [ -z "$1" ]; then
845 echo "Argument with devices to be added to guard must be set!"
846 exit 1
847 fi
848 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500849ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700850
851EOF
852 fi
853
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700854 write_makefile_header "$BOARDMK"
855 write_makefile_header "$PRODUCTMK"
856 write_blueprint_header "$ANDROIDBP"
857
858 cat << EOF >> "$ANDROIDBP"
859soong_namespace {
860}
861
862EOF
863
864 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
865 cat << EOF >> "$PRODUCTMK"
866PRODUCT_SOONG_NAMESPACES += \\
867 vendor/$VENDOR/$DEVICE
868
869EOF
Steve Kondik5bd66602016-07-15 10:39:58 -0700870}
871
872#
873# write_footers:
874#
875# Closes the inital guard and any other finalization tasks. Must
876# be called as the final step.
877#
878function write_footers() {
879 cat << EOF >> "$ANDROIDMK"
880endif
881EOF
882}
883
884# Return success if adb is up and not in recovery
885function _adb_connected {
886 {
Jake Whatley9843b322017-01-25 21:49:16 -0500887 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700888 then
889 return 0
890 fi
891 } 2>/dev/null
892
893 return 1
894};
895
896#
897# parse_file_list:
898#
899# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400900# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700901#
902# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
903#
904function parse_file_list() {
905 if [ -z "$1" ]; then
906 echo "An input file is expected!"
907 exit 1
908 elif [ ! -f "$1" ]; then
909 echo "Input file "$1" does not exist!"
910 exit 1
911 fi
912
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200913 if [ -n "$2" ]; then
914 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400915 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +0200916 # Match all lines starting with first line found to start* with '#'
917 # comment and contain** $2, and ending with first line to be empty*.
918 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
919 # **the $2 match is case-insensitive
920 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400921 else
922 LIST=$1
923 fi
924
925
Steve Kondik5bd66602016-07-15 10:39:58 -0700926 PRODUCT_PACKAGES_LIST=()
927 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +0200928 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -0700929 PRODUCT_COPY_FILES_LIST=()
930 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +0200931 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -0700932
933 while read -r line; do
934 if [ -z "$line" ]; then continue; fi
935
936 # If the line has a pipe delimiter, a sha1 hash should follow.
937 # This indicates the file should be pinned and not overwritten
938 # when extracting files.
939 local SPLIT=(${line//\|/ })
940 local COUNT=${#SPLIT[@]}
941 local SPEC=${SPLIT[0]}
942 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +0200943 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -0700944 if [ "$COUNT" -gt "1" ]; then
945 HASH=${SPLIT[1]}
946 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +0200947 if [ "$COUNT" -gt "2" ]; then
948 FIXUP_HASH=${SPLIT[2]}
949 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700950
951 # if line starts with a dash, it needs to be packaged
952 if [[ "$SPEC" =~ ^- ]]; then
953 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
954 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +0200955 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -0700956 else
957 PRODUCT_COPY_FILES_LIST+=("$SPEC")
958 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +0200959 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -0700960 fi
961
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400962 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700963}
964
965#
966# write_makefiles:
967#
968# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400969# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700970#
971# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700972# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -0700973# the product makefile.
974#
975function write_makefiles() {
976 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400977 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700978 write_product_packages
979}
980
981#
982# append_firmware_calls_to_makefiles:
983#
984# Appends to Android.mk the calls to all images present in radio folder
985# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
986#
987function append_firmware_calls_to_makefiles() {
988 cat << EOF >> "$ANDROIDMK"
989ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
990
991RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
992\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
993 \$(call add-radio-file,radio/\$(f)))
994\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
995
996endif
997
998EOF
999}
1000
1001#
1002# get_file:
1003#
1004# $1: input file
1005# $2: target file/folder
1006# $3: source of the file (can be "adb" or a local folder)
1007#
1008# Silently extracts the input file to defined target
1009# Returns success if file can be pulled from the device or found locally
1010#
1011function get_file() {
1012 local SRC="$3"
1013
1014 if [ "$SRC" = "adb" ]; then
1015 # try to pull
1016 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1017
1018 return 1
1019 else
1020 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001021 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1022 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001023 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001024
1025 return 1
1026 fi
1027};
1028
1029#
1030# oat2dex:
1031#
1032# $1: extracted apk|jar (to check if deodex is required)
1033# $2: odexed apk|jar to deodex
1034# $3: source of the odexed apk|jar
1035#
1036# Convert apk|jar .odex in the corresposing classes.dex
1037#
1038function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001039 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001040 local OEM_TARGET="$2"
1041 local SRC="$3"
1042 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001043 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001044
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001045 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1046 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1047 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001048 fi
1049
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001050 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001051 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001052 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001053
codeworkx85eda752018-09-23 12:36:57 +02001054 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001055 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001056 fi
1057
Steve Kondik5bd66602016-07-15 10:39:58 -07001058 # Extract existing boot.oats to the temp folder
1059 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001060 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001061 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001062 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001063 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001064 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001065 else
1066 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001067 fi
1068 done
1069 fi
1070
1071 if [ -z "$ARCHES" ]; then
1072 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1073 fi
1074
theimpulson9a911af2019-08-14 03:25:12 +00001075 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001076 return;
1077 fi
1078
theimpulson9a911af2019-08-14 03:25:12 +00001079 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001080 return 0 # target apk|jar is already odexed, return
1081 fi
1082
1083 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001084 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001085
Joe Maplesfb3941c2018-01-05 14:51:33 -05001086 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1087 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001088
Joe Maplesfb3941c2018-01-05 14:51:33 -05001089 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1090 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1091 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001092 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1093 for CLASS in $CLASSES; do
1094 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1095 # Check if we have to deal with CompactDex
1096 if [[ "$CLASS" == *.cdex ]]; then
1097 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1098 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1099 else
1100 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1101 fi
1102 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001103 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001104 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1105 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001106 fi
theimpulson9a911af2019-08-14 03:25:12 +00001107 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001108 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001109 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001110 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001111 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001112 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001113 # try to extract classes.dex from boot.vdex for frameworks jars
1114 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001115 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001116 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001117 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1118 for CLASS in $CLASSES; do
1119 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1120 # Check if we have to deal with CompactDex
1121 if [[ "$CLASS" == *.cdex ]]; then
1122 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1123 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1124 else
1125 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1126 fi
1127 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001128 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001129 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1130 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001131 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001132 else
1133 continue
1134 fi
1135
Steve Kondik5bd66602016-07-15 10:39:58 -07001136 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001137
1138 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001139}
1140
1141#
1142# init_adb_connection:
1143#
1144# Starts adb server and waits for the device
1145#
1146function init_adb_connection() {
1147 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1148 if ! _adb_connected; then
1149 echo "No device is online. Waiting for one..."
1150 echo "Please connect USB and/or enable USB debugging"
1151 until _adb_connected; do
1152 sleep 1
1153 done
1154 echo "Device Found."
1155 fi
1156
1157 # Retrieve IP and PORT info if we're using a TCP connection
1158 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1159 | head -1 | awk '{print $1}')
1160 adb root &> /dev/null
1161 sleep 0.3
1162 if [ -n "$TCPIPPORT" ]; then
1163 # adb root just killed our connection
1164 # so reconnect...
1165 adb connect "$TCPIPPORT"
1166 fi
1167 adb wait-for-device &> /dev/null
1168 sleep 0.3
1169}
1170
1171#
1172# fix_xml:
1173#
1174# $1: xml file to fix
1175#
1176function fix_xml() {
1177 local XML="$1"
1178 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1179
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001180 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1181 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001182
1183 mv "$TEMP_XML" "$XML"
1184}
1185
Vladimir Olteande985fe2019-01-17 03:07:34 +02001186function get_hash() {
1187 local FILE="$1"
1188
1189 if [ "$(uname)" == "Darwin" ]; then
1190 shasum "${FILE}" | awk '{print $1}'
1191 else
1192 sha1sum "${FILE}" | awk '{print $1}'
1193 fi
1194}
1195
Vladimir Olteana7d20492019-01-17 03:05:52 +02001196function print_spec() {
1197 local SPEC_PRODUCT_PACKAGE="$1"
1198 local SPEC_SRC_FILE="$2"
1199 local SPEC_DST_FILE="$3"
1200 local SPEC_ARGS="$4"
1201 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001202 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001203
1204 local PRODUCT_PACKAGE=""
1205 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1206 PRODUCT_PACKAGE="-"
1207 fi
1208 local SRC=""
1209 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1210 SRC="${SPEC_SRC_FILE}:"
1211 fi
1212 local DST=""
1213 if [ ! -z "${SPEC_DST_FILE}" ]; then
1214 DST="${SPEC_DST_FILE}"
1215 fi
1216 local ARGS=""
1217 if [ ! -z "${SPEC_ARGS}" ]; then
1218 ARGS=";${SPEC_ARGS}"
1219 fi
1220 local HASH=""
1221 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1222 HASH="|${SPEC_HASH}"
1223 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001224 local FIXUP_HASH=""
1225 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1226 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1227 fi
1228 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1229}
1230
1231# To be overridden by device-level extract-files.sh
1232# Parameters:
1233# $1: spec name of a blob. Can be used for filtering.
1234# If the spec is "src:dest", then $1 is "dest".
1235# If the spec is "src", then $1 is "src".
1236# $2: path to blob file. Can be used for fixups.
1237#
1238function blob_fixup() {
1239 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001240}
1241
Steve Kondik5bd66602016-07-15 10:39:58 -07001242#
1243# extract:
1244#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001245# Positional parameters:
1246# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001247# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001248# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1249#
1250# Non-positional parameters (coming after $2):
1251# --section: preferred way of selecting the portion to parse and extract from
1252# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001253# --kang: if present, this option will activate the printing of hashes for the
1254# extracted blobs. Useful with --section for subsequent pinning of
1255# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001256#
1257function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001258 # Consume positional parameters
1259 local PROPRIETARY_FILES_TXT="$1"; shift
1260 local SRC="$1"; shift
1261 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001262 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001263
1264 # Consume optional, non-positional parameters
1265 while [ "$#" -gt 0 ]; do
1266 case "$1" in
1267 -s|--section)
1268 SECTION="$2"; shift
1269 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001270 -k|--kang)
1271 KANG=true
1272 DISABLE_PINNING=1
1273 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001274 *)
1275 # Backwards-compatibility with the old behavior, where $3, if
1276 # present, denoted an optional positional ${SECTION} argument.
1277 # Users of ${SECTION} are encouraged to migrate from setting it as
1278 # positional $3, to non-positional --section ${SECTION}, the
1279 # reason being that it doesn't scale to have more than 1 optional
1280 # positional argument.
1281 SECTION="$1"
1282 ;;
1283 esac
1284 shift
1285 done
1286
Steve Kondik5bd66602016-07-15 10:39:58 -07001287 if [ -z "$OUTDIR" ]; then
1288 echo "Output dir not set!"
1289 exit 1
1290 fi
1291
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001292 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001293
1294 # Allow failing, so we can try $DEST and/or $FILE
1295 set +e
1296
1297 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1298 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001299 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001300 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001301 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001302 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001303 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1304
1305 if [ "$SRC" = "adb" ]; then
1306 init_adb_connection
1307 fi
1308
Dan Pasanen0cc05012017-03-21 09:06:11 -05001309 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001310 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001311
1312 # Check if we're working with the same zip that was passed last time.
1313 # If so, let's just use what's already extracted.
1314 MD5=`md5sum "$SRC"| awk '{print $1}'`
1315 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1316
1317 if [ "$MD5" != "$OLDMD5" ]; then
1318 rm -rf "$DUMPDIR"
1319 mkdir "$DUMPDIR"
1320 unzip "$SRC" -d "$DUMPDIR"
1321 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1322
1323 # Stop if an A/B OTA zip is detected. We cannot extract these.
1324 if [ -a "$DUMPDIR"/payload.bin ]; then
1325 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1326 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001327 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001328
Luca Stefani776be462020-09-09 15:53:58 +02001329 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001330 do
1331 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001332 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1333 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1334 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1335 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1336 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001337 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1338 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1339 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1340 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1341 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1342 echo "Requesting sudo access to mount the "$PARTITION".img"
1343 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1344 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1345 sudo umount "$DUMPDIR"/tmp
1346 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1347 fi
1348 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001349 fi
1350
1351 SRC="$DUMPDIR"
1352 fi
1353
Steve Kondik5bd66602016-07-15 10:39:58 -07001354 if [ "$VENDOR_STATE" -eq "0" ]; then
1355 echo "Cleaning output directory ($OUTPUT_ROOT).."
1356 rm -rf "${OUTPUT_TMP:?}"
1357 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001358 if [ -d "$OUTPUT_ROOT" ]; then
1359 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1360 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001361 VENDOR_STATE=1
1362 fi
1363
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001364 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001365
1366 for (( i=1; i<COUNT+1; i++ )); do
1367
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001368 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001369 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001370 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001371 local OUTPUT_DIR=
1372 local TMP_DIR=
1373 local SRC_FILE=
1374 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001375 local IS_PRODUCT_PACKAGE=false
1376
1377 # Note: this relies on the fact that the ${FILELIST[@]} array
1378 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1379 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1380 IS_PRODUCT_PACKAGE=true
1381 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001382
Michael Bestasbda30202020-12-28 04:44:52 +02001383 OUTPUT_DIR="${OUTPUT_ROOT}"
1384 TMP_DIR="${OUTPUT_TMP}"
1385 SRC_FILE="/system/${SPEC_SRC_FILE}"
1386 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001387
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001388 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001389 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001390 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001391 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001392
Gabriele M58270a32017-11-13 23:15:29 +01001393 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001394 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001395 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001396 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001397 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001398 if [ -f "${VENDOR_REPO_FILE}" ]; then
1399 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001400 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001401 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001402 fi
1403 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001404 local TMP_HASH=$(get_hash "${PINNED}")
1405 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001406 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001407 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1408 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001409 fi
1410 fi
1411 fi
1412 fi
1413
Vladimir Olteana7d20492019-01-17 03:05:52 +02001414 if [ "${KANG}" = false ]; then
1415 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1416 fi
1417
Gabriele M58270a32017-11-13 23:15:29 +01001418 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001419 if [ "${FIXUP_HASH}" != "x" ]; then
1420 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1421 else
1422 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1423 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001424 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001425 FOUND=false
1426 # Try Lineage target first.
1427 # Also try to search for files stripped of
1428 # the "/system" prefix, if we're actually extracting
1429 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001430 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001431 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1432 FOUND=true
1433 break
1434 }
1435 done
1436
1437 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001438 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001439 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001440 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001441
Arian5f98d792021-09-09 15:24:25 +02001442 # Blob fixup pipeline has 2 parts: one that is fixed and
1443 # one that is user-configurable
1444 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1445 # Deodex apk|jar if that's the case
1446 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1447 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1448 if [ -f "$TMPDIR/classes.dex" ]; then
1449 touch -t 200901010000 "$TMPDIR/classes"*
1450 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1451 rm "$TMPDIR/classes"*
1452 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1453 fi
1454 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1455 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001456 fi
Arian5f98d792021-09-09 15:24:25 +02001457 # Now run user-supplied fixup function
1458 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1459 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001460
Arian5f98d792021-09-09 15:24:25 +02001461 if [ -f "${VENDOR_REPO_FILE}" ]; then
1462 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1463 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001464 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001465 chmod 755 "${VENDOR_REPO_FILE}"
1466 else
1467 chmod 644 "${VENDOR_REPO_FILE}"
1468 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001469 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001470
Arian5f98d792021-09-09 15:24:25 +02001471 if [ "${KANG}" = true ]; then
1472 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1473 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001474
Arian5f98d792021-09-09 15:24:25 +02001475 # Check and print whether the fixup pipeline actually did anything.
1476 # This isn't done right after the fixup pipeline because we want this print
1477 # to come after print_spec above, when in kang mode.
1478 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1479 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1480 # Now sanity-check the spec for this blob.
1481 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1482 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1483 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1484 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001485 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001486 fi
1487
Steve Kondik5bd66602016-07-15 10:39:58 -07001488 done
1489
1490 # Don't allow failing
1491 set -e
1492}
1493
1494#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001495# extract2:
1496#
1497# Positional parameters:
1498# $1: file containing the list of items to extract (aka proprietary-files.txt)
1499#
1500# Non-positional parameters (coming after $2):
1501# --section: selects the portion to parse and extracts from proprietary-files.txt
1502# --kang: if present, this option will activate the printing of hashes for the
1503# extracted blobs. Useful with --section for subsequent pinning of
1504# blobs taken from other origins.
1505#
1506function extract2() {
1507 # Consume positional parameters
1508 local PROPRIETARY_FILES_TXT="$1"; shift
1509 local SECTION=""
1510 local KANG=false
1511
1512 # Consume optional, non-positional parameters
1513 while [ "$#" -gt 0 ]; do
1514 case "$1" in
1515 --adb)
1516 ADB=true
1517 ;;
1518 --system)
1519 SYSTEM_SRC="$2"; shift
1520 ;;
1521 --vendor)
1522 VENDOR_SRC="$2"; shift
1523 ;;
1524 --odm)
1525 ODM_SRC="$2"; shift
1526 ;;
1527 --product)
1528 PRODUCT_SRC="$2"; shift
1529 ;;
1530 -s|--section)
1531 SECTION="$2"; shift
1532 ;;
1533 -k|--kang)
1534 KANG=true
1535 DISABLE_PINNING=1
1536 ;;
1537 esac
1538 shift
1539 done
1540
1541 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1542 echo "No sources set! You must select --adb or pass paths to partition dumps."
1543 exit 1
1544 fi
1545
1546 if [ -z "$OUTDIR" ]; then
1547 echo "Output dir not set!"
1548 exit 1
1549 fi
1550
1551 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1552
1553 # Allow failing, so we can try $DEST and/or $FILE
1554 set +e
1555
1556 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1557 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1558 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1559 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1560 local COUNT=${#FILELIST[@]}
1561 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1562 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1563
1564 if [ "$ADB" = true ]; then
1565 init_adb_connection
1566 fi
1567
1568 if [ "$VENDOR_STATE" -eq "0" ]; then
1569 echo "Cleaning output directory ($OUTPUT_ROOT).."
1570 rm -rf "${OUTPUT_TMP:?}"
1571 mkdir -p "${OUTPUT_TMP:?}"
1572 if [ -d "$OUTPUT_ROOT" ]; then
1573 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1574 fi
1575 VENDOR_STATE=1
1576 fi
1577
1578 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1579
1580 for (( i=1; i<COUNT+1; i++ )); do
1581
1582 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1583 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1584 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1585 local OUTPUT_DIR=
1586 local TMP_DIR=
1587 local SRC_FILE=
1588 local DST_FILE=
1589 local IS_PRODUCT_PACKAGE=false
1590
1591 # Note: this relies on the fact that the ${FILELIST[@]} array
1592 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1593 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1594 IS_PRODUCT_PACKAGE=true
1595 fi
1596
1597 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1598 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1599 TMP_DIR="${OUTPUT_TMP}/rootfs"
1600 else
1601 OUTPUT_DIR="${OUTPUT_ROOT}"
1602 TMP_DIR="${OUTPUT_TMP}"
1603 fi
1604 SRC_FILE="${SPEC_SRC_FILE}"
1605 DST_FILE="${SPEC_DST_FILE}"
1606
1607 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1608 local BLOB_DISPLAY_NAME="${DST_FILE}"
1609 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1610
1611 # Check pinned files
1612 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1613 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1614 local KEEP=""
1615 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1616 if [ -f "${VENDOR_REPO_FILE}" ]; then
1617 local PINNED="${VENDOR_REPO_FILE}"
1618 else
1619 local PINNED="${TMP_DIR}${DST_FILE}"
1620 fi
1621 if [ -f "$PINNED" ]; then
1622 local TMP_HASH=$(get_hash "${PINNED}")
1623 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1624 KEEP="1"
1625 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1626 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1627 fi
1628 fi
1629 fi
1630 fi
1631
1632 if [ "${KANG}" = false ]; then
1633 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1634 fi
1635
1636 if [ "$KEEP" = "1" ]; then
1637 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1638 else
1639 FOUND=false
1640 PARTITION_SOURCE_DIR=
1641 # Try Lineage target first.
1642 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1643 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1644 if [ "$PARTITION" = "system" ]; then
1645 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1646 elif [ "$PARTITION" = "vendor" ]; then
1647 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1648 elif [ "$PARTITION" = "product" ]; then
1649 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1650 elif [ "$PARTITION" = "odm" ]; then
1651 PARTITION_SOURCE_DIR="$ODM_SRC"
1652 fi
1653 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1654 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1655 FOUND=true
1656 break
1657 }
1658 # Search with the full system/ prefix if the file was not found on the system partition
1659 # because we may be searching in a mounted system-as-root system.img
1660 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1661 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1662 FOUND=true
1663 break
1664 }
1665 fi
1666 done
1667
1668 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1669 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1670 fi
1671
1672 if [ "${FOUND}" = false ]; then
1673 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1674 continue
1675 fi
1676 fi
1677
1678 # Blob fixup pipeline has 2 parts: one that is fixed and
1679 # one that is user-configurable
1680 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1681 # Deodex apk|jar if that's the case
1682 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1683 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1684 if [ -f "$TMPDIR/classes.dex" ]; then
1685 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1686 rm "$TMPDIR/classes"*
1687 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1688 fi
1689 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1690 fix_xml "${VENDOR_REPO_FILE}"
1691 fi
1692 # Now run user-supplied fixup function
1693 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1694 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1695
1696 if [ -f "${VENDOR_REPO_FILE}" ]; then
1697 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1698 local TYPE="${DIR##*/}"
1699 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1700 chmod 755 "${VENDOR_REPO_FILE}"
1701 else
1702 chmod 644 "${VENDOR_REPO_FILE}"
1703 fi
1704 fi
1705
1706 if [ "${KANG}" = true ]; then
1707 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1708 fi
1709
1710 # Check and print whether the fixup pipeline actually did anything.
1711 # This isn't done right after the fixup pipeline because we want this print
1712 # to come after print_spec above, when in kang mode.
1713 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1714 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1715 # Now sanity-check the spec for this blob.
1716 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1717 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1718 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1719 fi
1720 fi
1721
1722 done
1723
1724 # Don't allow failing
1725 set -e
1726}
1727
1728#
Steve Kondik5bd66602016-07-15 10:39:58 -07001729# extract_firmware:
1730#
1731# $1: file containing the list of items to extract
1732# $2: path to extracted radio folder
1733#
1734function extract_firmware() {
1735 if [ -z "$OUTDIR" ]; then
1736 echo "Output dir not set!"
1737 exit 1
1738 fi
1739
1740 parse_file_list "$1"
1741
1742 # Don't allow failing
1743 set -e
1744
1745 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1746 local COUNT=${#FILELIST[@]}
1747 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001748 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001749
1750 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1751 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1752 rm -rf "${OUTPUT_DIR:?}/"*
1753 VENDOR_RADIO_STATE=1
1754 fi
1755
1756 echo "Extracting $COUNT files in $1 from $SRC:"
1757
1758 for (( i=1; i<COUNT+1; i++ )); do
1759 local FILE="${FILELIST[$i-1]}"
1760 printf ' - %s \n' "/radio/$FILE"
1761
1762 if [ ! -d "$OUTPUT_DIR" ]; then
1763 mkdir -p "$OUTPUT_DIR"
1764 fi
1765 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1766 chmod 644 "$OUTPUT_DIR/$FILE"
1767 done
1768}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001769
1770function extract_img_data() {
1771 local image_file="$1"
1772 local out_dir="$2"
1773 local logFile="$TMPDIR/debugfs.log"
1774
1775 if [ ! -d "$out_dir" ]; then
1776 mkdir -p "$out_dir"
1777 fi
1778
1779 if [[ "$HOST_OS" == "Darwin" ]]; then
1780 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1781 echo "[-] Failed to extract data from '$image_file'"
1782 abort 1
1783 }
1784 else
1785 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1786 do
1787 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1788 echo "[-] Failed to extract data from '$image_file'"
1789 abort 1
1790 }
1791 done
1792 fi
1793
1794 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1795 if grep -Fq "$symlink_err" "$logFile"; then
1796 echo "[-] Symlinks have not been properly processed from $image_file"
1797 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1798 abort 1
1799 fi
1800}
1801
1802declare -ra VENDOR_SKIP_FILES=(
1803 "bin/toybox_vendor"
1804 "bin/toolbox"
1805 "bin/grep"
1806 "build.prop"
1807 "compatibility_matrix.xml"
1808 "default.prop"
1809 "etc/NOTICE.xml.gz"
1810 "etc/vintf/compatibility_matrix.xml"
1811 "etc/vintf/manifest.xml"
1812 "etc/wifi/wpa_supplicant.conf"
1813 "manifest.xml"
1814 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1815 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1816 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1817 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1818 "overlay/framework-res__auto_generated_rro.apk"
1819 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1820)
1821
1822function array_contains() {
1823 local element
1824 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1825 return 1
1826}
1827
1828function generate_prop_list_from_image() {
1829 local image_file="$1"
1830 local image_dir="$TMPDIR/image-temp"
1831 local output_list="$2"
1832 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1833 local -n skipped_vendor_files="$3"
1834
1835 extract_img_data "$image_file" "$image_dir"
1836
1837 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1838 do
1839 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1840 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1841 continue
1842 fi
1843 # Skip device defined skipped files since they will be re-generated at build time
1844 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1845 continue
1846 fi
1847 if suffix_match_file ".apk" "$FILE" ; then
1848 echo "-vendor/$FILE" >> "$output_list_tmp"
1849 else
1850 echo "vendor/$FILE" >> "$output_list_tmp"
1851 fi
1852 done
1853
1854 # Sort merged file with all lists
1855 sort -u "$output_list_tmp" > "$output_list"
1856
1857 # Clean-up
1858 rm -f "$output_list_tmp"
1859}
Bruno Martins0f425f12021-04-10 14:57:32 +01001860
1861function colored_echo() {
1862 IFS=" "
1863 local color=$1;
1864 shift
1865 if ! [[ $color =~ '^[0-9]$' ]] ; then
1866 case $(echo $color | tr '[:upper:]' '[:lower:]') in
1867 black) color=0 ;;
1868 red) color=1 ;;
1869 green) color=2 ;;
1870 yellow) color=3 ;;
1871 blue) color=4 ;;
1872 magenta) color=5 ;;
1873 cyan) color=6 ;;
1874 white|*) color=7 ;; # white or invalid color
1875 esac
1876 fi
Bruno Martins5064db22021-06-21 14:47:40 +01001877 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01001878 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01001879 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01001880}