blob: 7c98b62ddc2bd2c009bad999a4f7235b89c705c7 [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
Michael Bestasf55ac292022-03-23 23:15:23 +0200109
110 if [ -z "$SIGSCAN" ]; then
111 export SIGSCAN="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/SigScan
112 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700113}
114
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300115# Helper functions for parsing a spec.
116# notes: an optional "|SHA1" that may appear in the format is stripped
117# early from the spec in the parse_file_list function, and
118# should not be present inside the input parameter passed
119# to these functions.
120
121#
122# input: spec in the form of "src[:dst][;args]"
123# output: "src"
124#
125function src_file() {
126 local SPEC="$1"
127 local SPLIT=(${SPEC//:/ })
128 local ARGS="$(target_args ${SPEC})"
129 # Regardless of there being a ":" delimiter or not in the spec,
130 # the source file is always either the first, or the only entry.
131 local SRC="${SPLIT[0]}"
132 # Remove target_args suffix, if present
133 echo "${SRC%;${ARGS}}"
134}
135
Steve Kondik5bd66602016-07-15 10:39:58 -0700136#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300137# input: spec in the form of "src[:dst][;args]"
138# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700139#
140function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800141 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300142 local SPLIT=(${SPEC//:/ })
143 local ARGS="$(target_args ${SPEC})"
144 local DST=
145 case ${#SPLIT[@]} in
146 1)
147 # The spec doesn't have a : delimiter
148 DST="${SPLIT[0]}"
149 ;;
150 *)
151 # The spec actually has a src:dst format
152 DST="${SPLIT[1]}"
153 ;;
154 esac
155 # Remove target_args suffix, if present
156 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700157}
158
159#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300160# input: spec in the form of "src[:dst][;args]"
161# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700162#
163function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300164 local SPEC="$1"
165 local SPLIT=(${SPEC//;/ })
166 local ARGS=
167 case ${#SPLIT[@]} in
168 1)
169 # No ";" delimiter in the spec.
170 ;;
171 *)
172 # The "args" are whatever comes after the ";" character.
173 # Basically the spec stripped of whatever is to the left of ";".
174 ARGS="${SPEC#${SPLIT[0]};}"
175 ;;
176 esac
177 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700178}
179
180#
181# prefix_match:
182#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300183# input:
184# - $1: prefix
185# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
186# output:
187# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700188#
189function prefix_match() {
190 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300191 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
192 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700193 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300194 local ARGS=$(target_args "$LINE")
195 if [ -z "${ARGS}" ]; then
196 echo "${FILE#$PREFIX}"
197 else
198 echo "${FILE#$PREFIX};${ARGS}"
199 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700200 fi
201 done
202}
203
204#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400205# prefix_match_file:
206#
207# $1: the prefix to match on
208# $2: the file to match the prefix for
209#
210# Internal function which returns true if a filename contains the
211# specified prefix.
212#
213function prefix_match_file() {
214 local PREFIX="$1"
215 local FILE="$2"
216 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
217 return 0
218 else
219 return 1
220 fi
221}
222
223#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700224# suffix_match_file:
225#
226# $1: the suffix to match on
227# $2: the file to match the suffix for
228#
229# Internal function which returns true if a filename contains the
230# specified suffix.
231#
232function suffix_match_file() {
233 local SUFFIX="$1"
234 local FILE="$2"
235 if [[ "$FILE" = *"$SUFFIX" ]]; then
236 return 0
237 else
238 return 1
239 fi
240}
241
242#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400243# truncate_file
244#
245# $1: the filename to truncate
246# $2: the argument to output the truncated filename to
247#
248# Internal function which truncates a filename by removing the first dir
249# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
250#
251function truncate_file() {
252 local FILE="$1"
253 RETURN_FILE="$2"
254 local FIND="${FILE%%/*}"
255 local LOCATION="${#FIND}+1"
256 echo ${FILE:$LOCATION}
257}
258
259#
Steve Kondik5bd66602016-07-15 10:39:58 -0700260# write_product_copy_files:
261#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400262# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400263#
Steve Kondik5bd66602016-07-15 10:39:58 -0700264# Creates the PRODUCT_COPY_FILES section in the product makefile for all
265# items in the list which do not start with a dash (-).
266#
267function write_product_copy_files() {
268 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
269 local TARGET=
270 local FILE=
271 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400272 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700273
274 if [ "$COUNT" -eq "0" ]; then
275 return 0
276 fi
277
278 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
279 for (( i=1; i<COUNT+1; i++ )); do
280 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
281 LINEEND=" \\"
282 if [ "$i" -eq "$COUNT" ]; then
283 LINEEND=""
284 fi
285
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300286 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400287 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400288 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400289 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400290 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400291 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400292 local OUTTARGET=$(truncate_file $TARGET)
293 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
294 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200295 elif prefix_match_file "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"
299 elif prefix_match_file "system/system_ext/" $TARGET ; then
300 local OUTTARGET=$(truncate_file $TARGET)
301 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
302 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400303 elif prefix_match_file "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"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400307 elif prefix_match_file "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 "system/vendor/odm/" $TARGET ; then
312 local OUTTARGET=$(truncate_file $TARGET)
313 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
314 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
315 elif prefix_match_file "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"
319 elif prefix_match_file "system/vendor/" $TARGET ; then
320 local OUTTARGET=$(truncate_file $TARGET)
321 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
322 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400323 elif prefix_match_file "system/" $TARGET ; then
324 local OUTTARGET=$(truncate_file $TARGET)
325 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
326 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400327 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400328 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400329 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
330 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700331 done
332 return 0
333}
334
335#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700336# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700337#
338# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200339# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700340# $3: type-specific extra flags
341# $4: Name of the array holding the target list
342#
343# Internal function which writes out the BUILD_PREBUILT stanzas
344# for all modules in the list. This is called by write_product_packages
345# after the modules are categorized.
346#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700347function write_blueprint_packages() {
348
349 local CLASS="$1"
350 local PARTITION="$2"
351 local EXTRA="$3"
352
353 # Yes, this is a horrible hack - we create a new array using indirection
354 local ARR_NAME="$4[@]"
355 local FILELIST=("${!ARR_NAME}")
356
357 local FILE=
358 local ARGS=
359 local BASENAME=
360 local EXTENSION=
361 local PKGNAME=
362 local SRC=
TheStrix6e24acc2020-04-10 18:20:19 +0530363 local OVERRIDEPKG=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700364
365 for P in "${FILELIST[@]}"; do
366 FILE=$(target_file "$P")
367 ARGS=$(target_args "$P")
368
369 BASENAME=$(basename "$FILE")
370 DIRNAME=$(dirname "$FILE")
371 EXTENSION=${BASENAME##*.}
372 PKGNAME=${BASENAME%.*}
373
374 # Add to final package list
375 PACKAGE_LIST+=("$PKGNAME")
376
377 SRC="proprietary"
378 if [ "$PARTITION" = "system" ]; then
379 SRC+="/system"
380 elif [ "$PARTITION" = "vendor" ]; then
381 SRC+="/vendor"
382 elif [ "$PARTITION" = "product" ]; then
383 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200384 elif [ "$PARTITION" = "system_ext" ]; then
385 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700386 elif [ "$PARTITION" = "odm" ]; then
387 SRC+="/odm"
388 fi
389
390 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
391 printf 'cc_prebuilt_library_shared {\n'
392 printf '\tname: "%s",\n' "$PKGNAME"
393 printf '\towner: "%s",\n' "$VENDOR"
394 printf '\tstrip: {\n'
395 printf '\t\tnone: true,\n'
396 printf '\t},\n'
397 printf '\ttarget: {\n'
398 if [ "$EXTRA" = "both" ]; then
399 printf '\t\tandroid_arm: {\n'
400 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
401 printf '\t\t},\n'
402 printf '\t\tandroid_arm64: {\n'
403 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
404 printf '\t\t},\n'
405 elif [ "$EXTRA" = "64" ]; then
406 printf '\t\tandroid_arm64: {\n'
407 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
408 printf '\t\t},\n'
409 else
410 printf '\t\tandroid_arm: {\n'
411 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
412 printf '\t\t},\n'
413 fi
414 printf '\t},\n'
415 if [ "$EXTRA" != "none" ]; then
416 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
417 fi
dianlujitao848101c2020-09-12 00:15:13 +0800418 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700419 elif [ "$CLASS" = "APPS" ]; then
420 printf 'android_app_import {\n'
421 printf '\tname: "%s",\n' "$PKGNAME"
422 printf '\towner: "%s",\n' "$VENDOR"
423 if [ "$EXTRA" = "priv-app" ]; then
424 SRC="$SRC/priv-app"
425 else
426 SRC="$SRC/app"
427 fi
428 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
TheStrix6e24acc2020-04-10 18:20:19 +0530429 ARGS=(${ARGS//;/ })
LuK1337508e85f2021-08-23 18:18:57 +0200430 USE_PLATFORM_CERTIFICATE="true"
431 for ARG in "${ARGS[@]}"; do
432 if [ "$ARG" = "PRESIGNED" ]; then
433 USE_PLATFORM_CERTIFICATE="false"
434 printf '\tpresigned: true,\n'
435 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
436 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200437 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200438 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
439 elif [ ! -z "$ARG" ]; then
440 USE_PLATFORM_CERTIFICATE="false"
441 printf '\tcertificate: "%s",\n' "$ARG"
442 fi
443 done
444 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700445 printf '\tcertificate: "platform",\n'
446 fi
447 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
448 printf 'dex_import {\n'
449 printf '\tname: "%s",\n' "$PKGNAME"
450 printf '\towner: "%s",\n' "$VENDOR"
451 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
452 elif [ "$CLASS" = "ETC" ]; then
453 if [ "$EXTENSION" = "xml" ]; then
454 printf 'prebuilt_etc_xml {\n'
455 else
456 printf 'prebuilt_etc {\n'
457 fi
458 printf '\tname: "%s",\n' "$PKGNAME"
459 printf '\towner: "%s",\n' "$VENDOR"
460 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200461 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700462 elif [ "$CLASS" = "EXECUTABLES" ]; then
463 if [ "$EXTENSION" = "sh" ]; then
464 printf 'sh_binary {\n'
465 else
466 printf 'cc_prebuilt_binary {\n'
467 fi
468 printf '\tname: "%s",\n' "$PKGNAME"
469 printf '\towner: "%s",\n' "$VENDOR"
Michael Bestasbda30202020-12-28 04:44:52 +0200470 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200471 if [ "$EXTENSION" != "sh" ]; then
472 printf '\tcheck_elf_files: false,\n'
473 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700474 unset EXTENSION
475 else
476 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
477 fi
478 if [ "$CLASS" = "APPS" ]; then
479 printf '\tdex_preopt: {\n'
480 printf '\t\tenabled: false,\n'
481 printf '\t},\n'
482 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200483 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700484 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200485 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700486 fi
487 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200488 if [ "$CLASS" = "ETC" ] ; then
489 if [ "$DIRNAME" != "." ]; then
490 printf '\tsub_dir: "%s",\n' "$DIRNAME"
491 fi
492 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700493 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
494 printf '\tprefer: true,\n'
495 fi
496 if [ "$EXTRA" = "priv-app" ]; then
497 printf '\tprivileged: true,\n'
498 fi
499 if [ "$PARTITION" = "vendor" ]; then
500 printf '\tsoc_specific: true,\n'
501 elif [ "$PARTITION" = "product" ]; then
502 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200503 elif [ "$PARTITION" = "system_ext" ]; then
504 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700505 elif [ "$PARTITION" = "odm" ]; then
506 printf '\tdevice_specific: true,\n'
507 fi
508 printf '}\n\n'
509 done
510}
511
512#
Steve Kondik5bd66602016-07-15 10:39:58 -0700513# write_product_packages:
514#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700515# This function will create prebuilt entries in the
516# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700517# product makefile for all files in the blob list which
518# start with a single dash (-) character.
519#
520function write_product_packages() {
521 PACKAGE_LIST=()
522
523 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
524
525 if [ "$COUNT" = "0" ]; then
526 return 0
527 fi
528
529 # Figure out what's 32-bit, what's 64-bit, and what's multilib
530 # I really should not be doing this in bash due to shitty array passing :(
531 local T_LIB32=( $(prefix_match "lib/") )
532 local T_LIB64=( $(prefix_match "lib64/") )
533 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
534 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
535 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
536
537 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700538 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700539 fi
540 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700541 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700542 fi
543 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700544 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700545 fi
546
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400547 local T_S_LIB32=( $(prefix_match "system/lib/") )
548 local T_S_LIB64=( $(prefix_match "system/lib64/") )
549 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
550 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
551 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
552
553 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700554 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400555 fi
556 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700557 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400558 fi
559 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700560 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400561 fi
562
Steve Kondik5bd66602016-07-15 10:39:58 -0700563 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
564 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
565 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
566 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
567 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
568
569 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700570 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700571 fi
572 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700573 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700574 fi
575 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700576 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500577 fi
578
579 local T_P_LIB32=( $(prefix_match "product/lib/") )
580 local T_P_LIB64=( $(prefix_match "product/lib64/") )
581 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
582 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
583 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
584
585 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700586 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500587 fi
588 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700589 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500590 fi
591 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700592 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700593 fi
594
Luca Stefani776be462020-09-09 15:53:58 +0200595 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
596 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
597 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
598 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
599 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
600
601 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
602 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
603 fi
604 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
605 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
606 fi
607 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
608 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
609 fi
610
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700611 local T_O_LIB32=( $(prefix_match "odm/lib/") )
612 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
613 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
614 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
615 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
616
617 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700618 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700619 fi
620 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700621 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700622 fi
623 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700624 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700625 fi
626
Steve Kondik5bd66602016-07-15 10:39:58 -0700627 # Apps
628 local APPS=( $(prefix_match "app/") )
629 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100630 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700631 fi
632 local PRIV_APPS=( $(prefix_match "priv-app/") )
633 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100634 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700635 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400636 local S_APPS=( $(prefix_match "system/app/") )
637 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100638 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400639 fi
640 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
641 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100642 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400643 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700644 local V_APPS=( $(prefix_match "vendor/app/") )
645 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100646 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700647 fi
648 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
649 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100650 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500651 fi
652 local P_APPS=( $(prefix_match "product/app/") )
653 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100654 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500655 fi
656 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
657 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100658 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700659 fi
Luca Stefani776be462020-09-09 15:53:58 +0200660 local SE_APPS=( $(prefix_match "system_ext/app/") )
661 if [ "${#SE_APPS[@]}" -gt "0" ]; then
662 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
663 fi
664 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
665 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
666 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
667 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700668 local O_APPS=( $(prefix_match "odm/app/") )
669 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100670 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700671 fi
672 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
673 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100674 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700675 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700676
677 # Framework
678 local FRAMEWORK=( $(prefix_match "framework/") )
679 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700680 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700681 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400682 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
683 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700684 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400685 fi
Christian Oder974b5902017-10-08 23:15:52 +0200686 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200687 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700688 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500689 fi
690 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
691 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700692 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200693 fi
Luca Stefani776be462020-09-09 15:53:58 +0200694 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700695 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200696 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
697 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700698 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
699 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700700 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700701 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700702
703 # Etc
704 local ETC=( $(prefix_match "etc/") )
705 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700706 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700707 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400708 local S_ETC=( $(prefix_match "system/etc/") )
709 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700710 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400711 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700712 local V_ETC=( $(prefix_match "vendor/etc/") )
713 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700714 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500715 fi
716 local P_ETC=( $(prefix_match "product/etc/") )
717 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700718 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700719 fi
Luca Stefani776be462020-09-09 15:53:58 +0200720 local SE_ETC=( $(prefix_match "system_ext/etc/") )
721 if [ "${#SE_ETC[@]}" -gt "0" ]; then
722 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
723 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700724 local O_ETC=( $(prefix_match "odm/etc/") )
725 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700726 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700727 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700728
729 # Executables
730 local BIN=( $(prefix_match "bin/") )
731 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700732 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700733 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400734 local S_BIN=( $(prefix_match "system/bin/") )
735 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700736 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400737 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700738 local V_BIN=( $(prefix_match "vendor/bin/") )
739 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700740 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500741 fi
742 local P_BIN=( $(prefix_match "product/bin/") )
743 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700744 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700745 fi
Luca Stefani776be462020-09-09 15:53:58 +0200746 local SE_BIN=( $(prefix_match "system_ext/bin/") )
747 if [ "${#SE_BIN[@]}" -gt "0" ]; then
748 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
749 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700750 local O_BIN=( $(prefix_match "odm/bin/") )
751 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700752 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700753 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700754
755 # Actually write out the final PRODUCT_PACKAGES list
756 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
757
758 if [ "$PACKAGE_COUNT" -eq "0" ]; then
759 return 0
760 fi
761
762 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
763 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
764 local LINEEND=" \\"
765 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
766 LINEEND=""
767 fi
768 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
769 done
770}
771
772#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700773# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700774#
775# $1: file which will be written to
776#
Michael Bestasa2934df2020-12-19 03:50:32 +0200777# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -0700778# note that this is not an append operation, and should
779# be executed first!
780#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700781function write_blueprint_header() {
782 if [ -f $1 ]; then
783 rm $1
784 fi
785
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700786 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
787
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700788 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200789// Automatically generated file. DO NOT MODIFY
790//
791// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700792
793EOF
794}
795
796#
797# write_makefile_header:
798#
799# $1: file which will be written to
800#
Michael Bestasa2934df2020-12-19 03:50:32 +0200801# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700802# note that this is not an append operation, and should
803# be executed first!
804#
805function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500806 if [ -f $1 ]; then
807 rm $1
808 fi
809
Steve Kondik5bd66602016-07-15 10:39:58 -0700810 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
811
Jake Whatley9843b322017-01-25 21:49:16 -0500812 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200813# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -0700814#
Steve Kondik5bd66602016-07-15 10:39:58 -0700815# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
816
817EOF
818}
819
820#
821# write_headers:
822#
823# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500824# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700825#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700826# Calls write_makefile_header for each of the makefiles and
827# write_blueprint_header for Android.bp and creates the initial
828# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -0700829#
830function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700831 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500832
833 GUARD="$2"
834 if [ -z "$GUARD" ]; then
835 GUARD="TARGET_DEVICE"
836 fi
837
Steve Kondik5bd66602016-07-15 10:39:58 -0700838 cat << EOF >> "$ANDROIDMK"
839LOCAL_PATH := \$(call my-dir)
840
841EOF
842 if [ "$COMMON" -ne 1 ]; then
843 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500844ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700845
846EOF
847 else
848 if [ -z "$1" ]; then
849 echo "Argument with devices to be added to guard must be set!"
850 exit 1
851 fi
852 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500853ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700854
855EOF
856 fi
857
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700858 write_makefile_header "$BOARDMK"
859 write_makefile_header "$PRODUCTMK"
860 write_blueprint_header "$ANDROIDBP"
861
862 cat << EOF >> "$ANDROIDBP"
863soong_namespace {
864}
865
866EOF
867
868 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
869 cat << EOF >> "$PRODUCTMK"
870PRODUCT_SOONG_NAMESPACES += \\
871 vendor/$VENDOR/$DEVICE
872
873EOF
Steve Kondik5bd66602016-07-15 10:39:58 -0700874}
875
876#
877# write_footers:
878#
879# Closes the inital guard and any other finalization tasks. Must
880# be called as the final step.
881#
882function write_footers() {
883 cat << EOF >> "$ANDROIDMK"
884endif
885EOF
886}
887
888# Return success if adb is up and not in recovery
889function _adb_connected {
890 {
Jake Whatley9843b322017-01-25 21:49:16 -0500891 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700892 then
893 return 0
894 fi
895 } 2>/dev/null
896
897 return 1
898};
899
900#
901# parse_file_list:
902#
903# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400904# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700905#
906# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
907#
908function parse_file_list() {
909 if [ -z "$1" ]; then
910 echo "An input file is expected!"
911 exit 1
912 elif [ ! -f "$1" ]; then
913 echo "Input file "$1" does not exist!"
914 exit 1
915 fi
916
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200917 if [ -n "$2" ]; then
918 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400919 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +0200920 # Match all lines starting with first line found to start* with '#'
921 # comment and contain** $2, and ending with first line to be empty*.
922 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
923 # **the $2 match is case-insensitive
924 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400925 else
926 LIST=$1
927 fi
928
929
Steve Kondik5bd66602016-07-15 10:39:58 -0700930 PRODUCT_PACKAGES_LIST=()
931 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +0200932 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -0700933 PRODUCT_COPY_FILES_LIST=()
934 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +0200935 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -0700936
937 while read -r line; do
938 if [ -z "$line" ]; then continue; fi
939
940 # If the line has a pipe delimiter, a sha1 hash should follow.
941 # This indicates the file should be pinned and not overwritten
942 # when extracting files.
943 local SPLIT=(${line//\|/ })
944 local COUNT=${#SPLIT[@]}
945 local SPEC=${SPLIT[0]}
946 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +0200947 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -0700948 if [ "$COUNT" -gt "1" ]; then
949 HASH=${SPLIT[1]}
950 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +0200951 if [ "$COUNT" -gt "2" ]; then
952 FIXUP_HASH=${SPLIT[2]}
953 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700954
955 # if line starts with a dash, it needs to be packaged
956 if [[ "$SPEC" =~ ^- ]]; then
957 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
958 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +0200959 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -0700960 else
961 PRODUCT_COPY_FILES_LIST+=("$SPEC")
962 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +0200963 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -0700964 fi
965
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400966 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700967}
968
969#
970# write_makefiles:
971#
972# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400973# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700974#
975# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700976# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -0700977# the product makefile.
978#
979function write_makefiles() {
980 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400981 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700982 write_product_packages
983}
984
985#
986# append_firmware_calls_to_makefiles:
987#
988# Appends to Android.mk the calls to all images present in radio folder
989# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
990#
991function append_firmware_calls_to_makefiles() {
992 cat << EOF >> "$ANDROIDMK"
993ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
994
995RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
996\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
997 \$(call add-radio-file,radio/\$(f)))
998\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
999
1000endif
1001
1002EOF
1003}
1004
1005#
1006# get_file:
1007#
1008# $1: input file
1009# $2: target file/folder
1010# $3: source of the file (can be "adb" or a local folder)
1011#
1012# Silently extracts the input file to defined target
1013# Returns success if file can be pulled from the device or found locally
1014#
1015function get_file() {
1016 local SRC="$3"
1017
1018 if [ "$SRC" = "adb" ]; then
1019 # try to pull
1020 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1021
1022 return 1
1023 else
1024 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001025 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1026 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001027 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001028
1029 return 1
1030 fi
1031};
1032
1033#
1034# oat2dex:
1035#
1036# $1: extracted apk|jar (to check if deodex is required)
1037# $2: odexed apk|jar to deodex
1038# $3: source of the odexed apk|jar
1039#
1040# Convert apk|jar .odex in the corresposing classes.dex
1041#
1042function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001043 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001044 local OEM_TARGET="$2"
1045 local SRC="$3"
1046 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001047 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001048
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001049 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1050 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1051 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001052 fi
1053
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001054 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001055 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001056 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001057
codeworkx85eda752018-09-23 12:36:57 +02001058 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001059 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001060 fi
1061
Steve Kondik5bd66602016-07-15 10:39:58 -07001062 # Extract existing boot.oats to the temp folder
1063 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001064 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001065 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001066 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001067 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001068 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001069 else
1070 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001071 fi
1072 done
1073 fi
1074
1075 if [ -z "$ARCHES" ]; then
1076 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1077 fi
1078
theimpulson9a911af2019-08-14 03:25:12 +00001079 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001080 return;
1081 fi
1082
theimpulson9a911af2019-08-14 03:25:12 +00001083 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001084 return 0 # target apk|jar is already odexed, return
1085 fi
1086
1087 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001088 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001089
Joe Maplesfb3941c2018-01-05 14:51:33 -05001090 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1091 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001092
Joe Maplesfb3941c2018-01-05 14:51:33 -05001093 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1094 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1095 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001096 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1097 for CLASS in $CLASSES; do
1098 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1099 # Check if we have to deal with CompactDex
1100 if [[ "$CLASS" == *.cdex ]]; then
1101 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1102 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1103 else
1104 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1105 fi
1106 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001107 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001108 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1109 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001110 fi
theimpulson9a911af2019-08-14 03:25:12 +00001111 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001112 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001113 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001114 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001115 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001116 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001117 # try to extract classes.dex from boot.vdex for frameworks jars
1118 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001119 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001120 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001121 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1122 for CLASS in $CLASSES; do
1123 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1124 # Check if we have to deal with CompactDex
1125 if [[ "$CLASS" == *.cdex ]]; then
1126 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1127 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1128 else
1129 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1130 fi
1131 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001132 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001133 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1134 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001135 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001136 else
1137 continue
1138 fi
1139
Steve Kondik5bd66602016-07-15 10:39:58 -07001140 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001141
1142 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001143}
1144
1145#
1146# init_adb_connection:
1147#
1148# Starts adb server and waits for the device
1149#
1150function init_adb_connection() {
1151 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1152 if ! _adb_connected; then
1153 echo "No device is online. Waiting for one..."
1154 echo "Please connect USB and/or enable USB debugging"
1155 until _adb_connected; do
1156 sleep 1
1157 done
1158 echo "Device Found."
1159 fi
1160
1161 # Retrieve IP and PORT info if we're using a TCP connection
1162 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1163 | head -1 | awk '{print $1}')
1164 adb root &> /dev/null
1165 sleep 0.3
1166 if [ -n "$TCPIPPORT" ]; then
1167 # adb root just killed our connection
1168 # so reconnect...
1169 adb connect "$TCPIPPORT"
1170 fi
1171 adb wait-for-device &> /dev/null
1172 sleep 0.3
1173}
1174
1175#
1176# fix_xml:
1177#
1178# $1: xml file to fix
1179#
1180function fix_xml() {
1181 local XML="$1"
1182 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1183
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001184 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1185 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001186
1187 mv "$TEMP_XML" "$XML"
1188}
1189
Vladimir Olteande985fe2019-01-17 03:07:34 +02001190function get_hash() {
1191 local FILE="$1"
1192
1193 if [ "$(uname)" == "Darwin" ]; then
1194 shasum "${FILE}" | awk '{print $1}'
1195 else
1196 sha1sum "${FILE}" | awk '{print $1}'
1197 fi
1198}
1199
Vladimir Olteana7d20492019-01-17 03:05:52 +02001200function print_spec() {
1201 local SPEC_PRODUCT_PACKAGE="$1"
1202 local SPEC_SRC_FILE="$2"
1203 local SPEC_DST_FILE="$3"
1204 local SPEC_ARGS="$4"
1205 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001206 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001207
1208 local PRODUCT_PACKAGE=""
1209 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1210 PRODUCT_PACKAGE="-"
1211 fi
1212 local SRC=""
1213 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1214 SRC="${SPEC_SRC_FILE}:"
1215 fi
1216 local DST=""
1217 if [ ! -z "${SPEC_DST_FILE}" ]; then
1218 DST="${SPEC_DST_FILE}"
1219 fi
1220 local ARGS=""
1221 if [ ! -z "${SPEC_ARGS}" ]; then
1222 ARGS=";${SPEC_ARGS}"
1223 fi
1224 local HASH=""
1225 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1226 HASH="|${SPEC_HASH}"
1227 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001228 local FIXUP_HASH=""
1229 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1230 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1231 fi
1232 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1233}
1234
1235# To be overridden by device-level extract-files.sh
1236# Parameters:
1237# $1: spec name of a blob. Can be used for filtering.
1238# If the spec is "src:dest", then $1 is "dest".
1239# If the spec is "src", then $1 is "src".
1240# $2: path to blob file. Can be used for fixups.
1241#
1242function blob_fixup() {
1243 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001244}
1245
Steve Kondik5bd66602016-07-15 10:39:58 -07001246#
1247# extract:
1248#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001249# Positional parameters:
1250# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001251# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001252# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1253#
1254# Non-positional parameters (coming after $2):
1255# --section: preferred way of selecting the portion to parse and extract from
1256# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001257# --kang: if present, this option will activate the printing of hashes for the
1258# extracted blobs. Useful with --section for subsequent pinning of
1259# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001260#
1261function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001262 # Consume positional parameters
1263 local PROPRIETARY_FILES_TXT="$1"; shift
1264 local SRC="$1"; shift
1265 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001266 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001267
1268 # Consume optional, non-positional parameters
1269 while [ "$#" -gt 0 ]; do
1270 case "$1" in
1271 -s|--section)
1272 SECTION="$2"; shift
1273 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001274 -k|--kang)
1275 KANG=true
1276 DISABLE_PINNING=1
1277 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001278 *)
1279 # Backwards-compatibility with the old behavior, where $3, if
1280 # present, denoted an optional positional ${SECTION} argument.
1281 # Users of ${SECTION} are encouraged to migrate from setting it as
1282 # positional $3, to non-positional --section ${SECTION}, the
1283 # reason being that it doesn't scale to have more than 1 optional
1284 # positional argument.
1285 SECTION="$1"
1286 ;;
1287 esac
1288 shift
1289 done
1290
Steve Kondik5bd66602016-07-15 10:39:58 -07001291 if [ -z "$OUTDIR" ]; then
1292 echo "Output dir not set!"
1293 exit 1
1294 fi
1295
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001296 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001297
1298 # Allow failing, so we can try $DEST and/or $FILE
1299 set +e
1300
1301 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1302 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001303 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001304 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001305 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001306 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001307 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1308
1309 if [ "$SRC" = "adb" ]; then
1310 init_adb_connection
1311 fi
1312
Dan Pasanen0cc05012017-03-21 09:06:11 -05001313 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001314 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001315
1316 # Check if we're working with the same zip that was passed last time.
1317 # If so, let's just use what's already extracted.
1318 MD5=`md5sum "$SRC"| awk '{print $1}'`
1319 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1320
1321 if [ "$MD5" != "$OLDMD5" ]; then
1322 rm -rf "$DUMPDIR"
1323 mkdir "$DUMPDIR"
1324 unzip "$SRC" -d "$DUMPDIR"
1325 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1326
1327 # Stop if an A/B OTA zip is detected. We cannot extract these.
1328 if [ -a "$DUMPDIR"/payload.bin ]; then
1329 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1330 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001331 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001332
Luca Stefani776be462020-09-09 15:53:58 +02001333 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001334 do
1335 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001336 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1337 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1338 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1339 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1340 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001341 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1342 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1343 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1344 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1345 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1346 echo "Requesting sudo access to mount the "$PARTITION".img"
1347 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1348 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1349 sudo umount "$DUMPDIR"/tmp
1350 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1351 fi
1352 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001353 fi
1354
1355 SRC="$DUMPDIR"
1356 fi
1357
Steve Kondik5bd66602016-07-15 10:39:58 -07001358 if [ "$VENDOR_STATE" -eq "0" ]; then
1359 echo "Cleaning output directory ($OUTPUT_ROOT).."
1360 rm -rf "${OUTPUT_TMP:?}"
1361 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001362 if [ -d "$OUTPUT_ROOT" ]; then
1363 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1364 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001365 VENDOR_STATE=1
1366 fi
1367
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001368 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001369
1370 for (( i=1; i<COUNT+1; i++ )); do
1371
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001372 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001373 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001374 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001375 local OUTPUT_DIR=
1376 local TMP_DIR=
1377 local SRC_FILE=
1378 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001379 local IS_PRODUCT_PACKAGE=false
1380
1381 # Note: this relies on the fact that the ${FILELIST[@]} array
1382 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1383 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1384 IS_PRODUCT_PACKAGE=true
1385 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001386
Michael Bestasbda30202020-12-28 04:44:52 +02001387 OUTPUT_DIR="${OUTPUT_ROOT}"
1388 TMP_DIR="${OUTPUT_TMP}"
1389 SRC_FILE="/system/${SPEC_SRC_FILE}"
1390 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001391
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001392 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001393 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001394 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001395 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001396
Gabriele M58270a32017-11-13 23:15:29 +01001397 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001398 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001399 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001400 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001401 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001402 if [ -f "${VENDOR_REPO_FILE}" ]; then
1403 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001404 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001405 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001406 fi
1407 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001408 local TMP_HASH=$(get_hash "${PINNED}")
1409 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001410 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001411 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1412 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001413 fi
1414 fi
1415 fi
1416 fi
1417
Vladimir Olteana7d20492019-01-17 03:05:52 +02001418 if [ "${KANG}" = false ]; then
1419 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1420 fi
1421
Gabriele M58270a32017-11-13 23:15:29 +01001422 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001423 if [ "${FIXUP_HASH}" != "x" ]; then
1424 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1425 else
1426 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1427 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001428 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001429 FOUND=false
1430 # Try Lineage target first.
1431 # Also try to search for files stripped of
1432 # the "/system" prefix, if we're actually extracting
1433 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001434 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001435 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1436 FOUND=true
1437 break
1438 }
1439 done
1440
1441 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001442 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001443 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001444 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001445
Arian5f98d792021-09-09 15:24:25 +02001446 # Blob fixup pipeline has 2 parts: one that is fixed and
1447 # one that is user-configurable
1448 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1449 # Deodex apk|jar if that's the case
1450 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1451 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1452 if [ -f "$TMPDIR/classes.dex" ]; then
1453 touch -t 200901010000 "$TMPDIR/classes"*
1454 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1455 rm "$TMPDIR/classes"*
1456 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1457 fi
1458 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1459 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001460 fi
Arian5f98d792021-09-09 15:24:25 +02001461 # Now run user-supplied fixup function
1462 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1463 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001464
Arian5f98d792021-09-09 15:24:25 +02001465 if [ -f "${VENDOR_REPO_FILE}" ]; then
1466 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1467 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001468 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001469 chmod 755 "${VENDOR_REPO_FILE}"
1470 else
1471 chmod 644 "${VENDOR_REPO_FILE}"
1472 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001473 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001474
Arian5f98d792021-09-09 15:24:25 +02001475 if [ "${KANG}" = true ]; then
1476 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1477 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001478
Arian5f98d792021-09-09 15:24:25 +02001479 # Check and print whether the fixup pipeline actually did anything.
1480 # This isn't done right after the fixup pipeline because we want this print
1481 # to come after print_spec above, when in kang mode.
1482 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1483 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1484 # Now sanity-check the spec for this blob.
1485 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1486 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1487 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1488 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001489 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001490 fi
1491
Steve Kondik5bd66602016-07-15 10:39:58 -07001492 done
1493
1494 # Don't allow failing
1495 set -e
1496}
1497
1498#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001499# extract2:
1500#
1501# Positional parameters:
1502# $1: file containing the list of items to extract (aka proprietary-files.txt)
1503#
1504# Non-positional parameters (coming after $2):
1505# --section: selects the portion to parse and extracts from proprietary-files.txt
1506# --kang: if present, this option will activate the printing of hashes for the
1507# extracted blobs. Useful with --section for subsequent pinning of
1508# blobs taken from other origins.
1509#
1510function extract2() {
1511 # Consume positional parameters
1512 local PROPRIETARY_FILES_TXT="$1"; shift
1513 local SECTION=""
1514 local KANG=false
1515
1516 # Consume optional, non-positional parameters
1517 while [ "$#" -gt 0 ]; do
1518 case "$1" in
1519 --adb)
1520 ADB=true
1521 ;;
1522 --system)
1523 SYSTEM_SRC="$2"; shift
1524 ;;
1525 --vendor)
1526 VENDOR_SRC="$2"; shift
1527 ;;
1528 --odm)
1529 ODM_SRC="$2"; shift
1530 ;;
1531 --product)
1532 PRODUCT_SRC="$2"; shift
1533 ;;
1534 -s|--section)
1535 SECTION="$2"; shift
1536 ;;
1537 -k|--kang)
1538 KANG=true
1539 DISABLE_PINNING=1
1540 ;;
1541 esac
1542 shift
1543 done
1544
1545 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1546 echo "No sources set! You must select --adb or pass paths to partition dumps."
1547 exit 1
1548 fi
1549
1550 if [ -z "$OUTDIR" ]; then
1551 echo "Output dir not set!"
1552 exit 1
1553 fi
1554
1555 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1556
1557 # Allow failing, so we can try $DEST and/or $FILE
1558 set +e
1559
1560 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1561 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1562 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1563 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1564 local COUNT=${#FILELIST[@]}
1565 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1566 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1567
1568 if [ "$ADB" = true ]; then
1569 init_adb_connection
1570 fi
1571
1572 if [ "$VENDOR_STATE" -eq "0" ]; then
1573 echo "Cleaning output directory ($OUTPUT_ROOT).."
1574 rm -rf "${OUTPUT_TMP:?}"
1575 mkdir -p "${OUTPUT_TMP:?}"
1576 if [ -d "$OUTPUT_ROOT" ]; then
1577 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1578 fi
1579 VENDOR_STATE=1
1580 fi
1581
1582 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1583
1584 for (( i=1; i<COUNT+1; i++ )); do
1585
1586 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1587 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1588 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1589 local OUTPUT_DIR=
1590 local TMP_DIR=
1591 local SRC_FILE=
1592 local DST_FILE=
1593 local IS_PRODUCT_PACKAGE=false
1594
1595 # Note: this relies on the fact that the ${FILELIST[@]} array
1596 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1597 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1598 IS_PRODUCT_PACKAGE=true
1599 fi
1600
1601 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1602 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1603 TMP_DIR="${OUTPUT_TMP}/rootfs"
1604 else
1605 OUTPUT_DIR="${OUTPUT_ROOT}"
1606 TMP_DIR="${OUTPUT_TMP}"
1607 fi
1608 SRC_FILE="${SPEC_SRC_FILE}"
1609 DST_FILE="${SPEC_DST_FILE}"
1610
1611 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1612 local BLOB_DISPLAY_NAME="${DST_FILE}"
1613 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1614
1615 # Check pinned files
1616 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1617 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1618 local KEEP=""
1619 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1620 if [ -f "${VENDOR_REPO_FILE}" ]; then
1621 local PINNED="${VENDOR_REPO_FILE}"
1622 else
1623 local PINNED="${TMP_DIR}${DST_FILE}"
1624 fi
1625 if [ -f "$PINNED" ]; then
1626 local TMP_HASH=$(get_hash "${PINNED}")
1627 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1628 KEEP="1"
1629 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1630 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1631 fi
1632 fi
1633 fi
1634 fi
1635
1636 if [ "${KANG}" = false ]; then
1637 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1638 fi
1639
1640 if [ "$KEEP" = "1" ]; then
1641 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1642 else
1643 FOUND=false
1644 PARTITION_SOURCE_DIR=
1645 # Try Lineage target first.
1646 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1647 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1648 if [ "$PARTITION" = "system" ]; then
1649 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1650 elif [ "$PARTITION" = "vendor" ]; then
1651 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1652 elif [ "$PARTITION" = "product" ]; then
1653 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1654 elif [ "$PARTITION" = "odm" ]; then
1655 PARTITION_SOURCE_DIR="$ODM_SRC"
1656 fi
1657 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1658 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1659 FOUND=true
1660 break
1661 }
1662 # Search with the full system/ prefix if the file was not found on the system partition
1663 # because we may be searching in a mounted system-as-root system.img
1664 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1665 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1666 FOUND=true
1667 break
1668 }
1669 fi
1670 done
1671
1672 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1673 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1674 fi
1675
1676 if [ "${FOUND}" = false ]; then
1677 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1678 continue
1679 fi
1680 fi
1681
1682 # Blob fixup pipeline has 2 parts: one that is fixed and
1683 # one that is user-configurable
1684 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1685 # Deodex apk|jar if that's the case
1686 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1687 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1688 if [ -f "$TMPDIR/classes.dex" ]; then
1689 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1690 rm "$TMPDIR/classes"*
1691 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1692 fi
1693 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1694 fix_xml "${VENDOR_REPO_FILE}"
1695 fi
1696 # Now run user-supplied fixup function
1697 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1698 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1699
1700 if [ -f "${VENDOR_REPO_FILE}" ]; then
1701 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1702 local TYPE="${DIR##*/}"
1703 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1704 chmod 755 "${VENDOR_REPO_FILE}"
1705 else
1706 chmod 644 "${VENDOR_REPO_FILE}"
1707 fi
1708 fi
1709
1710 if [ "${KANG}" = true ]; then
1711 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1712 fi
1713
1714 # Check and print whether the fixup pipeline actually did anything.
1715 # This isn't done right after the fixup pipeline because we want this print
1716 # to come after print_spec above, when in kang mode.
1717 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1718 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1719 # Now sanity-check the spec for this blob.
1720 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1721 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1722 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1723 fi
1724 fi
1725
1726 done
1727
1728 # Don't allow failing
1729 set -e
1730}
1731
1732#
Steve Kondik5bd66602016-07-15 10:39:58 -07001733# extract_firmware:
1734#
1735# $1: file containing the list of items to extract
1736# $2: path to extracted radio folder
1737#
1738function extract_firmware() {
1739 if [ -z "$OUTDIR" ]; then
1740 echo "Output dir not set!"
1741 exit 1
1742 fi
1743
1744 parse_file_list "$1"
1745
1746 # Don't allow failing
1747 set -e
1748
1749 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1750 local COUNT=${#FILELIST[@]}
1751 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001752 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001753
1754 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1755 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1756 rm -rf "${OUTPUT_DIR:?}/"*
1757 VENDOR_RADIO_STATE=1
1758 fi
1759
1760 echo "Extracting $COUNT files in $1 from $SRC:"
1761
1762 for (( i=1; i<COUNT+1; i++ )); do
1763 local FILE="${FILELIST[$i-1]}"
1764 printf ' - %s \n' "/radio/$FILE"
1765
1766 if [ ! -d "$OUTPUT_DIR" ]; then
1767 mkdir -p "$OUTPUT_DIR"
1768 fi
1769 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1770 chmod 644 "$OUTPUT_DIR/$FILE"
1771 done
1772}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001773
1774function extract_img_data() {
1775 local image_file="$1"
1776 local out_dir="$2"
1777 local logFile="$TMPDIR/debugfs.log"
1778
1779 if [ ! -d "$out_dir" ]; then
1780 mkdir -p "$out_dir"
1781 fi
1782
1783 if [[ "$HOST_OS" == "Darwin" ]]; then
1784 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1785 echo "[-] Failed to extract data from '$image_file'"
1786 abort 1
1787 }
1788 else
1789 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1790 do
1791 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1792 echo "[-] Failed to extract data from '$image_file'"
1793 abort 1
1794 }
1795 done
1796 fi
1797
1798 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1799 if grep -Fq "$symlink_err" "$logFile"; then
1800 echo "[-] Symlinks have not been properly processed from $image_file"
1801 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1802 abort 1
1803 fi
1804}
1805
1806declare -ra VENDOR_SKIP_FILES=(
1807 "bin/toybox_vendor"
1808 "bin/toolbox"
1809 "bin/grep"
1810 "build.prop"
1811 "compatibility_matrix.xml"
1812 "default.prop"
1813 "etc/NOTICE.xml.gz"
1814 "etc/vintf/compatibility_matrix.xml"
1815 "etc/vintf/manifest.xml"
1816 "etc/wifi/wpa_supplicant.conf"
1817 "manifest.xml"
1818 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1819 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1820 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1821 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1822 "overlay/framework-res__auto_generated_rro.apk"
1823 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1824)
1825
1826function array_contains() {
1827 local element
1828 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1829 return 1
1830}
1831
1832function generate_prop_list_from_image() {
1833 local image_file="$1"
1834 local image_dir="$TMPDIR/image-temp"
1835 local output_list="$2"
1836 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1837 local -n skipped_vendor_files="$3"
1838
1839 extract_img_data "$image_file" "$image_dir"
1840
1841 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1842 do
1843 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1844 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1845 continue
1846 fi
1847 # Skip device defined skipped files since they will be re-generated at build time
1848 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1849 continue
1850 fi
1851 if suffix_match_file ".apk" "$FILE" ; then
1852 echo "-vendor/$FILE" >> "$output_list_tmp"
1853 else
1854 echo "vendor/$FILE" >> "$output_list_tmp"
1855 fi
1856 done
1857
1858 # Sort merged file with all lists
1859 sort -u "$output_list_tmp" > "$output_list"
1860
1861 # Clean-up
1862 rm -f "$output_list_tmp"
1863}
Bruno Martins0f425f12021-04-10 14:57:32 +01001864
1865function colored_echo() {
1866 IFS=" "
1867 local color=$1;
1868 shift
1869 if ! [[ $color =~ '^[0-9]$' ]] ; then
1870 case $(echo $color | tr '[:upper:]' '[:lower:]') in
1871 black) color=0 ;;
1872 red) color=1 ;;
1873 green) color=2 ;;
1874 yellow) color=3 ;;
1875 blue) color=4 ;;
1876 magenta) color=5 ;;
1877 cyan) color=6 ;;
1878 white|*) color=7 ;; # white or invalid color
1879 esac
1880 fi
Bruno Martins5064db22021-06-21 14:47:40 +01001881 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01001882 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01001883 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01001884}