blob: a94c4b0b2af8c89403ed42e5c52809a239c40533 [file] [log] [blame]
Steve Kondik4e2aaab2016-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=()
Steve Kondik48f8df82016-08-14 03:55:08 -070019PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +020020PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070021PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -070022PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +020023PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070024PACKAGE_LIST=()
25VENDOR_STATE=-1
Louis Popia516c2f2016-07-25 15:51:13 +020026VENDOR_RADIO_STATE=-1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070027COMMON=-1
Luca Stefani7f9fff22016-07-18 13:47:55 +020028ARCHES=
29FULLY_DEODEXED=-1
30
Rashed Abdel-Tawab11186d62017-08-05 23:11:35 -040031TMPDIR=$(mktemp -d)
Steve Kondik4e2aaab2016-07-15 10:39:58 -070032
33#
Steve Kondik48f8df82016-08-14 03:55:08 -070034# cleanup
35#
36# kill our tmpfiles with fire on exit
37#
38function cleanup() {
39 rm -rf "${TMPDIR:?}"
40}
41
Gabriele M6c3c2c02017-10-11 12:55:51 +020042trap cleanup 0
Steve Kondik48f8df82016-08-14 03:55:08 -070043
44#
Steve Kondik4e2aaab2016-07-15 10:39:58 -070045# setup_vendor
46#
47# $1: device name
48# $2: vendor name
Luca Stefani5c60e4f2017-08-17 19:28:48 +020049# $3: Lineage root directory
Steve Kondik4e2aaab2016-07-15 10:39:58 -070050# $4: is common device - optional, default to false
51# $5: cleanup - optional, default to true
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040052# $6: custom vendor makefile name - optional, default to false
Steve Kondik4e2aaab2016-07-15 10:39:58 -070053#
54# Must be called before any other functions can be used. This
55# sets up the internal state for a new vendor configuration.
56#
57function setup_vendor() {
58 local DEVICE="$1"
59 if [ -z "$DEVICE" ]; then
60 echo "\$DEVICE must be set before including this script!"
61 exit 1
62 fi
63
64 export VENDOR="$2"
65 if [ -z "$VENDOR" ]; then
66 echo "\$VENDOR must be set before including this script!"
67 exit 1
68 fi
69
Jackeaglecf6f4de2019-09-24 04:07:22 -040070 export BLISS_ROOT="$3"
71 if [ ! -d "$BLISS_ROOT" ]; then
72 echo "\$BLISS_ROOT must be set and valid before including this script!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070073 exit 1
74 fi
75
76 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
Jackeaglecf6f4de2019-09-24 04:07:22 -040077 if [ ! -d "$BLISS_ROOT/$OUTDIR" ]; then
78 mkdir -p "$BLISS_ROOT/$OUTDIR"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070079 fi
80
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040081 VNDNAME="$6"
82 if [ -z "$VNDNAME" ]; then
83 VNDNAME="$DEVICE"
84 fi
85
Jackeaglecf6f4de2019-09-24 04:07:22 -040086 export PRODUCTMK="$BLISS_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -070087 export ANDROIDBP="$BLISS_ROOT"/"$OUTDIR"/Android.bp
Jackeaglecf6f4de2019-09-24 04:07:22 -040088 export ANDROIDMK="$BLISS_ROOT"/"$OUTDIR"/Android.mk
89 export BOARDMK="$BLISS_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -070090
91 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
92 COMMON=1
93 else
94 COMMON=0
95 fi
96
Gabriele Mb6effb32017-05-01 18:22:04 +020097 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -070098 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +020099 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700100 else
101 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +0200102 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700103 fi
104}
105
Vladimir Oltean95643282018-06-24 20:22:41 +0300106# Helper functions for parsing a spec.
107# notes: an optional "|SHA1" that may appear in the format is stripped
108# early from the spec in the parse_file_list function, and
109# should not be present inside the input parameter passed
110# to these functions.
111
112#
113# input: spec in the form of "src[:dst][;args]"
114# output: "src"
115#
116function src_file() {
117 local SPEC="$1"
118 local SPLIT=(${SPEC//:/ })
119 local ARGS="$(target_args ${SPEC})"
120 # Regardless of there being a ":" delimiter or not in the spec,
121 # the source file is always either the first, or the only entry.
122 local SRC="${SPLIT[0]}"
123 # Remove target_args suffix, if present
124 echo "${SRC%;${ARGS}}"
125}
126
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700127#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300128# input: spec in the form of "src[:dst][;args]"
129# output: "dst" if present, "src" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700130#
131function target_file() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300132 local SPEC="$1"
133 local SPLIT=(${SPEC//:/ })
134 local ARGS="$(target_args ${SPEC})"
135 local DST=
136 case ${#SPLIT[@]} in
137 1)
138 # The spec doesn't have a : delimiter
139 DST="${SPLIT[0]}"
140 ;;
141 *)
142 # The spec actually has a src:dst format
143 DST="${SPLIT[1]}"
144 ;;
145 esac
146 # Remove target_args suffix, if present
147 echo "${DST%;${ARGS}}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700148}
149
150#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300151# input: spec in the form of "src[:dst][;args]"
152# output: "args" if present, "" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700153#
154function target_args() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300155 local SPEC="$1"
156 local SPLIT=(${SPEC//;/ })
157 local ARGS=
158 case ${#SPLIT[@]} in
159 1)
160 # No ";" delimiter in the spec.
161 ;;
162 *)
163 # The "args" are whatever comes after the ";" character.
164 # Basically the spec stripped of whatever is to the left of ";".
165 ARGS="${SPEC#${SPLIT[0]};}"
166 ;;
167 esac
168 echo "${ARGS}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700169}
170
171#
172# prefix_match:
173#
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300174# input:
175# - $1: prefix
176# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
177# output:
178# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700179#
180function prefix_match() {
181 local PREFIX="$1"
Vladimir Olteana48b9fe2018-04-02 22:37:09 +0300182 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
183 local FILE=$(target_file "$LINE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700184 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300185 local ARGS=$(target_args "$LINE")
186 if [ -z "${ARGS}" ]; then
187 echo "${FILE#$PREFIX}"
188 else
189 echo "${FILE#$PREFIX};${ARGS}"
190 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700191 fi
192 done
193}
194
195#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400196# prefix_match_file:
197#
198# $1: the prefix to match on
199# $2: the file to match the prefix for
200#
201# Internal function which returns true if a filename contains the
202# specified prefix.
203#
204function prefix_match_file() {
205 local PREFIX="$1"
206 local FILE="$2"
207 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
208 return 0
209 else
210 return 1
211 fi
212}
213
214#
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -0700215# suffix_match_file:
216#
217# $1: the suffix to match on
218# $2: the file to match the suffix for
219#
220# Internal function which returns true if a filename contains the
221# specified suffix.
222#
223function suffix_match_file() {
224 local SUFFIX="$1"
225 local FILE="$2"
226 if [[ "$FILE" = *"$SUFFIX" ]]; then
227 return 0
228 else
229 return 1
230 fi
231}
232
233#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400234# truncate_file
235#
236# $1: the filename to truncate
237# $2: the argument to output the truncated filename to
238#
239# Internal function which truncates a filename by removing the first dir
240# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
241#
242function truncate_file() {
243 local FILE="$1"
244 RETURN_FILE="$2"
245 local FIND="${FILE%%/*}"
246 local LOCATION="${#FIND}+1"
247 echo ${FILE:$LOCATION}
248}
249
250#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700251# write_product_copy_files:
252#
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400253# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400254#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700255# Creates the PRODUCT_COPY_FILES section in the product makefile for all
256# items in the list which do not start with a dash (-).
257#
258function write_product_copy_files() {
259 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
260 local TARGET=
261 local FILE=
262 local LINEEND=
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400263 local TREBLE_COMPAT=$1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700264
265 if [ "$COUNT" -eq "0" ]; then
266 return 0
267 fi
268
269 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
270 for (( i=1; i<COUNT+1; i++ )); do
271 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
272 LINEEND=" \\"
273 if [ "$i" -eq "$COUNT" ]; then
274 LINEEND=""
275 fi
276
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300277 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawabb2f042d2019-10-05 00:09:41 -0400278 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400279 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawabb2f042d2019-10-05 00:09:41 -0400280 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400281 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawabb2f042d2019-10-05 00:09:41 -0400282 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400283 local OUTTARGET=$(truncate_file $TARGET)
284 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
285 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
286 elif prefix_match_file "odm/" $TARGET ; then
287 local OUTTARGET=$(truncate_file $TARGET)
288 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
289 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawabb2f042d2019-10-05 00:09:41 -0400290 elif prefix_match_file "vendor/odm/" $TARGET ; then
291 local OUTTARGET=$(truncate_file $TARGET)
292 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
293 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
294 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
295 local OUTTARGET=$(truncate_file $TARGET)
296 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
297 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
298 elif prefix_match_file "vendor/" $TARGET ; then
299 local OUTTARGET=$(truncate_file $TARGET)
300 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
301 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
302 elif prefix_match_file "system/vendor/" $TARGET ; then
303 local OUTTARGET=$(truncate_file $TARGET)
304 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
305 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400306 elif prefix_match_file "system/" $TARGET ; then
307 local OUTTARGET=$(truncate_file $TARGET)
308 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
309 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400310 else
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400311 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400312 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
313 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700314 done
315 return 0
316}
317
318#
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700319# write_blueprint_packages:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700320#
321# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400322# $2: /system, /odm, /product, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700323# $3: type-specific extra flags
324# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700325#
326# Internal function which writes out the BUILD_PREBUILT stanzas
327# for all modules in the list. This is called by write_product_packages
328# after the modules are categorized.
329#
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700330function write_blueprint_packages() {
331
332 local CLASS="$1"
333 local PARTITION="$2"
334 local EXTRA="$3"
335
336 # Yes, this is a horrible hack - we create a new array using indirection
337 local ARR_NAME="$4[@]"
338 local FILELIST=("${!ARR_NAME}")
339
340 local FILE=
341 local ARGS=
342 local BASENAME=
343 local EXTENSION=
344 local PKGNAME=
345 local SRC=
346
347 for P in "${FILELIST[@]}"; do
348 FILE=$(target_file "$P")
349 ARGS=$(target_args "$P")
350
351 BASENAME=$(basename "$FILE")
352 DIRNAME=$(dirname "$FILE")
353 EXTENSION=${BASENAME##*.}
354 PKGNAME=${BASENAME%.*}
355
356 # Add to final package list
357 PACKAGE_LIST+=("$PKGNAME")
358
359 SRC="proprietary"
360 if [ "$PARTITION" = "system" ]; then
361 SRC+="/system"
362 elif [ "$PARTITION" = "vendor" ]; then
363 SRC+="/vendor"
364 elif [ "$PARTITION" = "product" ]; then
365 SRC+="/product"
366 elif [ "$PARTITION" = "odm" ]; then
367 SRC+="/odm"
368 fi
369
370 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
371 printf 'cc_prebuilt_library_shared {\n'
372 printf '\tname: "%s",\n' "$PKGNAME"
373 printf '\towner: "%s",\n' "$VENDOR"
374 printf '\tstrip: {\n'
375 printf '\t\tnone: true,\n'
376 printf '\t},\n'
377 printf '\ttarget: {\n'
378 if [ "$EXTRA" = "both" ]; then
379 printf '\t\tandroid_arm: {\n'
380 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
381 printf '\t\t},\n'
382 printf '\t\tandroid_arm64: {\n'
383 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
384 printf '\t\t},\n'
385 elif [ "$EXTRA" = "64" ]; then
386 printf '\t\tandroid_arm64: {\n'
387 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
388 printf '\t\t},\n'
389 else
390 printf '\t\tandroid_arm: {\n'
391 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
392 printf '\t\t},\n'
393 fi
394 printf '\t},\n'
395 if [ "$EXTRA" != "none" ]; then
396 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
397 fi
398 elif [ "$CLASS" = "APPS" ]; then
399 printf 'android_app_import {\n'
400 printf '\tname: "%s",\n' "$PKGNAME"
401 printf '\towner: "%s",\n' "$VENDOR"
402 if [ "$EXTRA" = "priv-app" ]; then
403 SRC="$SRC/priv-app"
404 else
405 SRC="$SRC/app"
406 fi
407 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
408 if [ "$ARGS" = "PRESIGNED" ]; then
409 printf '\tpresigned: true,\n'
410 elif [ ! -z "$ARGS" ]; then
411 printf '\tcertificate: "%s",\n' "$ARGS"
412 else
413 printf '\tcertificate: "platform",\n'
414 fi
415 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
416 printf 'dex_import {\n'
417 printf '\tname: "%s",\n' "$PKGNAME"
418 printf '\towner: "%s",\n' "$VENDOR"
419 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
420 elif [ "$CLASS" = "ETC" ]; then
421 if [ "$EXTENSION" = "xml" ]; then
422 printf 'prebuilt_etc_xml {\n'
423 else
424 printf 'prebuilt_etc {\n'
425 fi
426 printf '\tname: "%s",\n' "$PKGNAME"
427 printf '\towner: "%s",\n' "$VENDOR"
428 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
429 elif [ "$CLASS" = "EXECUTABLES" ]; then
430 if [ "$EXTENSION" = "sh" ]; then
431 printf 'sh_binary {\n'
432 else
433 printf 'cc_prebuilt_binary {\n'
434 fi
435 printf '\tname: "%s",\n' "$PKGNAME"
436 printf '\towner: "%s",\n' "$VENDOR"
437 if [ "$ARGS" = "rootfs" ]; then
438 SRC="$SRC/rootfs"
439 if [ "$EXTRA" = "sbin" ]; then
440 SRC="$SRC/sbin"
441 printf '\tdist {\n'
442 printf '\t\tdest: "%s",\n' "root/sbin"
443 printf '\t},'
444 fi
445 else
446 SRC="$SRC/bin"
447 fi
448 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
449 unset EXTENSION
450 else
451 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
452 fi
453 if [ "$CLASS" = "APPS" ]; then
454 printf '\tdex_preopt: {\n'
455 printf '\t\tenabled: false,\n'
456 printf '\t},\n'
457 fi
458 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] || [ "$CLASS" = "ETC" ] ; then
459 if [ "$DIRNAME" != "." ]; then
460 printf '\tsub_dir: "%s",\n' "$DIRNAME"
461 fi
462 fi
463 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
464 printf '\tprefer: true,\n'
465 fi
466 if [ "$EXTRA" = "priv-app" ]; then
467 printf '\tprivileged: true,\n'
468 fi
469 if [ "$PARTITION" = "vendor" ]; then
470 printf '\tsoc_specific: true,\n'
471 elif [ "$PARTITION" = "product" ]; then
472 printf '\tproduct_specific: true,\n'
473 elif [ "$PARTITION" = "odm" ]; then
474 printf '\tdevice_specific: true,\n'
475 fi
476 printf '}\n\n'
477 done
478}
479
480#
481# write_makefile_packages:
482#
483# $1: The LOCAL_MODULE_CLASS for the given module list
484# $2: /odm, /product, or /vendor partition
485# $3: type-specific extra flags
486# $4: Name of the array holding the target list
487#
488# Internal function which writes out the BUILD_PREBUILT stanzas
489# for all modules in the list. This is called by write_product_packages
490# after the modules are categorized.
491#
492function write_makefile_packages() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700493
494 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500495 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700496 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700497
498 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700499 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700500 local FILELIST=("${!ARR_NAME}")
501
502 local FILE=
503 local ARGS=
504 local BASENAME=
505 local EXTENSION=
506 local PKGNAME=
507 local SRC=
508
509 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300510 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700511 ARGS=$(target_args "$P")
512
513 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100514 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700515 EXTENSION=${BASENAME##*.}
516 PKGNAME=${BASENAME%.*}
517
518 # Add to final package list
519 PACKAGE_LIST+=("$PKGNAME")
520
521 SRC="proprietary"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400522 if [ "$PARTITION" = "system" ]; then
523 SRC+="/system"
524 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700525 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500526 elif [ "$PARTITION" = "product" ]; then
527 SRC+="/product"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700528 elif [ "$PARTITION" = "odm" ]; then
529 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700530 fi
531
532 printf 'include $(CLEAR_VARS)\n'
533 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
534 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
535 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700536 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700537 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
538 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
539 #if [ "$VENDOR_PKG" = "true" ]; then
540 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
541 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
542 #else
543 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
544 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
545 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700546 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700547 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
548 else
549 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
550 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700551 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700552 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700553 fi
554 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200555 if [ "$EXTRA" = "priv-app" ]; then
556 SRC="$SRC/priv-app"
557 else
558 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700559 fi
560 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
561 local CERT=platform
562 if [ ! -z "$ARGS" ]; then
563 CERT="$ARGS"
564 fi
565 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
566 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
567 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200568 local CERT=platform
569 if [ ! -z "$ARGS" ]; then
570 CERT="$ARGS"
571 fi
572 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700573 elif [ "$CLASS" = "ETC" ]; then
574 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
575 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700576 if [ "$ARGS" = "rootfs" ]; then
577 SRC="$SRC/rootfs"
578 if [ "$EXTRA" = "sbin" ]; then
579 SRC="$SRC/sbin"
580 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
581 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
582 fi
583 else
584 SRC="$SRC/bin"
585 fi
586 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
587 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700588 else
Steve Kondika991cf12016-07-28 12:13:12 -0700589 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700590 fi
591 printf 'LOCAL_MODULE_TAGS := optional\n'
592 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400593 if [ "$CLASS" = "APPS" ]; then
594 printf 'LOCAL_DEX_PREOPT := false\n'
595 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700596 if [ ! -z "$EXTENSION" ]; then
597 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
598 fi
M1cha15f226c2017-01-04 09:00:11 +0100599 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
600 if [ "$DIRNAME" != "." ]; then
601 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
602 fi
603 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700604 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700605 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
606 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500607 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800608 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500609 elif [ "$PARTITION" = "product" ]; then
610 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700611 elif [ "$PARTITION" = "odm" ]; then
612 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700613 fi
614 printf 'include $(BUILD_PREBUILT)\n\n'
615 done
616}
617
618#
619# write_product_packages:
620#
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -0700621# This function will create prebuilt entries in the
622# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700623# product makefile for all files in the blob list which
624# start with a single dash (-) character.
625#
626function write_product_packages() {
627 PACKAGE_LIST=()
628
629 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
630
631 if [ "$COUNT" = "0" ]; then
632 return 0
633 fi
634
635 # Figure out what's 32-bit, what's 64-bit, and what's multilib
636 # I really should not be doing this in bash due to shitty array passing :(
637 local T_LIB32=( $(prefix_match "lib/") )
638 local T_LIB64=( $(prefix_match "lib64/") )
639 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
640 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700641 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700642
Steve Kondik03ce4002016-07-29 00:00:16 -0700643 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700644 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700645 fi
646 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700647 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700648 fi
649 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700650 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700651 fi
652
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400653 local T_S_LIB32=( $(prefix_match "system/lib/") )
654 local T_S_LIB64=( $(prefix_match "system/lib64/") )
655 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
656 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
657 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
658
659 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700660 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400661 fi
662 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700663 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400664 fi
665 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700666 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400667 fi
668
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700669 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
670 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
671 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
672 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700673 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700674
675 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700676 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700677 fi
678 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700679 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700680 fi
681 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700682 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500683 fi
684
685 local T_P_LIB32=( $(prefix_match "product/lib/") )
686 local T_P_LIB64=( $(prefix_match "product/lib64/") )
687 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
688 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
689 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
690
691 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700692 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500693 fi
694 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700695 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500696 fi
697 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700698 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700699 fi
700
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700701 local T_O_LIB32=( $(prefix_match "odm/lib/") )
702 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
703 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
704 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
705 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
706
707 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700708 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700709 fi
710 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700711 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700712 fi
713 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700714 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700715 fi
716
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700717 # Apps
718 local APPS=( $(prefix_match "app/") )
719 if [ "${#APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700720 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700721 fi
722 local PRIV_APPS=( $(prefix_match "priv-app/") )
723 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700724 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700725 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400726 local S_APPS=( $(prefix_match "system/app/") )
727 if [ "${#S_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700728 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400729 fi
730 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
731 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700732 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400733 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700734 local V_APPS=( $(prefix_match "vendor/app/") )
735 if [ "${#V_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700736 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700737 fi
738 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
739 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700740 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500741 fi
742 local P_APPS=( $(prefix_match "product/app/") )
743 if [ "${#P_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700744 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500745 fi
746 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
747 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700748 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700749 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700750 local O_APPS=( $(prefix_match "odm/app/") )
751 if [ "${#O_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700752 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700753 fi
754 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
755 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700756 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700757 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700758
759 # Framework
760 local FRAMEWORK=( $(prefix_match "framework/") )
761 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700762 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700763 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400764 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
765 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700766 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400767 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200768 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200769 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700770 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500771 fi
772 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
773 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700774 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oderc16f3272017-10-08 23:15:52 +0200775 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700776 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
777 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700778 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700779 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700780
781 # Etc
782 local ETC=( $(prefix_match "etc/") )
783 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700784 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700785 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400786 local S_ETC=( $(prefix_match "system/etc/") )
787 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700788 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400789 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700790 local V_ETC=( $(prefix_match "vendor/etc/") )
791 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700792 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500793 fi
794 local P_ETC=( $(prefix_match "product/etc/") )
795 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700796 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700797 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700798 local O_ETC=( $(prefix_match "odm/etc/") )
799 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700800 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700801 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700802
803 # Executables
804 local BIN=( $(prefix_match "bin/") )
805 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700806 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700807 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400808 local S_BIN=( $(prefix_match "system/bin/") )
809 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700810 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400811 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700812 local V_BIN=( $(prefix_match "vendor/bin/") )
813 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700814 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500815 fi
816 local P_BIN=( $(prefix_match "product/bin/") )
817 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700818 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700819 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700820 local O_BIN=( $(prefix_match "odm/bin/") )
821 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700822 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700823 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700824 local SBIN=( $(prefix_match "sbin/") )
825 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab103c2ac2019-09-20 10:30:38 -0700826 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700827 fi
828
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700829
830 # Actually write out the final PRODUCT_PACKAGES list
831 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
832
833 if [ "$PACKAGE_COUNT" -eq "0" ]; then
834 return 0
835 fi
836
837 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
838 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
839 local LINEEND=" \\"
840 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
841 LINEEND=""
842 fi
843 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
844 done
845}
846
847#
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -0700848# write_blueprint_header:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700849#
850# $1: file which will be written to
851#
852# writes out the copyright header with the current year.
853# note that this is not an append operation, and should
854# be executed first!
855#
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -0700856function write_blueprint_header() {
857 if [ -f $1 ]; then
858 rm $1
859 fi
860
861 YEAR=$(date +"%Y")
862
863 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
864
865 printf "/**\n" > $1
866 NUM_REGEX='^[0-9]+$'
867 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
868 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
869 else
870 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
871 fi
872
873 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
874 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
875 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
876 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
877 else
878 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
879 fi
880
881 cat << EOF >> $1
882 *
883 * Licensed under the Apache License, Version 2.0 (the "License");
884 * you may not use this file except in compliance with the License.
885 * You may obtain a copy of the License at
886 *
887 * http://www.apache.org/licenses/LICENSE-2.0
888 *
889 * Unless required by applicable law or agreed to in writing, software
890 * distributed under the License is distributed on an "AS IS" BASIS,
891 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
892 * See the License for the specific language governing permissions and
893 * limitations under the License.
894 *
895 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
896 */
897
898EOF
899}
900
901#
902# write_makefile_header:
903#
904# $1: file which will be written to
905#
906# writes out the copyright header with the current year.
907# note that this is not an append operation, and should
908# be executed first!
909#
910function write_makefile_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600911 if [ -f $1 ]; then
912 rm $1
913 fi
914
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700915 YEAR=$(date +"%Y")
916
917 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
918
Matt Mower8945f5e2017-01-07 14:08:17 -0600919 NUM_REGEX='^[0-9]+$'
920 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
921 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
922 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
923 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
924 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
925 fi
926 if [ $YEAR -eq 2017 ]; then
927 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
928 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
929 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
930 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
931 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
932 else
933 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
934 fi
935 else
936 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
937 fi
938
939 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700940#
941# Licensed under the Apache License, Version 2.0 (the "License");
942# you may not use this file except in compliance with the License.
943# You may obtain a copy of the License at
944#
945# http://www.apache.org/licenses/LICENSE-2.0
946#
947# Unless required by applicable law or agreed to in writing, software
948# distributed under the License is distributed on an "AS IS" BASIS,
949# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
950# See the License for the specific language governing permissions and
951# limitations under the License.
952
953# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
954
955EOF
956}
957
958#
959# write_headers:
960#
961# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400962# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700963#
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -0700964# Calls write_makefile_header for each of the makefiles and
965# write_blueprint_header for Android.bp and creates the initial
966# path declaration and device guard for the Android.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700967#
968function write_headers() {
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -0700969 write_makefile_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400970
971 GUARD="$2"
972 if [ -z "$GUARD" ]; then
973 GUARD="TARGET_DEVICE"
974 fi
975
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700976 cat << EOF >> "$ANDROIDMK"
977LOCAL_PATH := \$(call my-dir)
978
979EOF
980 if [ "$COMMON" -ne 1 ]; then
981 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400982ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700983
984EOF
985 else
986 if [ -z "$1" ]; then
987 echo "Argument with devices to be added to guard must be set!"
988 exit 1
989 fi
990 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400991ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700992
993EOF
994 fi
995
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -0700996 write_makefile_header "$BOARDMK"
997 write_makefile_header "$PRODUCTMK"
998 write_blueprint_header "$ANDROIDBP"
999
1000 cat << EOF >> "$ANDROIDBP"
1001soong_namespace {
1002}
1003
1004EOF
1005
1006 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1007 cat << EOF >> "$PRODUCTMK"
1008PRODUCT_SOONG_NAMESPACES += \\
1009 vendor/$VENDOR/$DEVICE
1010
1011EOF
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001012}
1013
1014#
1015# write_footers:
1016#
1017# Closes the inital guard and any other finalization tasks. Must
1018# be called as the final step.
1019#
1020function write_footers() {
1021 cat << EOF >> "$ANDROIDMK"
1022endif
1023EOF
1024}
1025
1026# Return success if adb is up and not in recovery
1027function _adb_connected {
1028 {
Steve Kondik7561d192016-09-01 21:40:27 -07001029 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001030 then
1031 return 0
1032 fi
1033 } 2>/dev/null
1034
1035 return 1
1036};
1037
1038#
Bruno Martins3b96ba52016-07-27 15:00:05 +01001039# parse_file_list:
1040#
1041# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001042# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +01001043#
1044# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001045#
1046function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +01001047 if [ -z "$1" ]; then
1048 echo "An input file is expected!"
1049 exit 1
1050 elif [ ! -f "$1" ]; then
1051 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001052 exit 1
1053 fi
1054
Vladimir Olteanc5034462019-01-17 03:04:16 +02001055 if [ -n "$2" ]; then
1056 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001057 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +02001058 # Match all lines starting with first line found to start* with '#'
1059 # comment and contain** $2, and ending with first line to be empty*.
1060 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1061 # **the $2 match is case-insensitive
1062 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001063 else
1064 LIST=$1
1065 fi
1066
1067
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001068 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001069 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001070 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001071 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001072 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001073 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001074
1075 while read -r line; do
1076 if [ -z "$line" ]; then continue; fi
1077
Steve Kondik48f8df82016-08-14 03:55:08 -07001078 # If the line has a pipe delimiter, a sha1 hash should follow.
1079 # This indicates the file should be pinned and not overwritten
1080 # when extracting files.
1081 local SPLIT=(${line//\|/ })
1082 local COUNT=${#SPLIT[@]}
1083 local SPEC=${SPLIT[0]}
1084 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001085 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -07001086 if [ "$COUNT" -gt "1" ]; then
1087 HASH=${SPLIT[1]}
1088 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001089 if [ "$COUNT" -gt "2" ]; then
1090 FIXUP_HASH=${SPLIT[2]}
1091 fi
Steve Kondik48f8df82016-08-14 03:55:08 -07001092
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001093 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -07001094 if [[ "$SPEC" =~ ^- ]]; then
1095 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1096 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001097 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001098 else
Steve Kondik48f8df82016-08-14 03:55:08 -07001099 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1100 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001101 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001102 fi
1103
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001104 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001105}
1106
1107#
1108# write_makefiles:
1109#
1110# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001111# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001112#
1113# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawab05784b42019-09-20 07:06:09 -07001114# the given file and appends to the Android.bp as well as
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001115# the product makefile.
1116#
1117function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001118 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001119 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001120 write_product_packages
1121}
1122
1123#
Louis Popia516c2f2016-07-25 15:51:13 +02001124# append_firmware_calls_to_makefiles:
1125#
1126# Appends to Android.mk the calls to all images present in radio folder
1127# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1128#
1129function append_firmware_calls_to_makefiles() {
1130 cat << EOF >> "$ANDROIDMK"
1131ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1132
1133RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1134\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1135 \$(call add-radio-file,radio/\$(f)))
1136\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1137
1138endif
1139
1140EOF
1141}
1142
1143#
Luca Stefani7f9fff22016-07-18 13:47:55 +02001144# get_file:
1145#
1146# $1: input file
1147# $2: target file/folder
1148# $3: source of the file (can be "adb" or a local folder)
1149#
1150# Silently extracts the input file to defined target
1151# Returns success if file can be pulled from the device or found locally
1152#
1153function get_file() {
1154 local SRC="$3"
1155
1156 if [ "$SRC" = "adb" ]; then
1157 # try to pull
1158 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1159
1160 return 1
1161 else
1162 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +03001163 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1164 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +02001165 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +02001166
1167 return 1
1168 fi
1169};
1170
1171#
1172# oat2dex:
1173#
1174# $1: extracted apk|jar (to check if deodex is required)
1175# $2: odexed apk|jar to deodex
1176# $3: source of the odexed apk|jar
1177#
1178# Convert apk|jar .odex in the corresposing classes.dex
1179#
1180function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001181 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001182 local OEM_TARGET="$2"
1183 local SRC="$3"
1184 local TARGET=
1185 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -05001186 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001187
1188 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins98599b62019-10-02 15:56:25 -06001189 export BAKSMALIJAR="$BLISS_ROOT"/prebuilts/tools-bliss/common/smali/baksmali.jar
1190 export SMALIJAR="$BLISS_ROOT"/prebuilts/tools-bliss/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +02001191 fi
1192
Joe Maples9be579f2018-01-05 14:51:33 -05001193 if [ -z "$VDEXEXTRACTOR" ]; then
Bruno Martins98599b62019-10-02 15:56:25 -06001194 export VDEXEXTRACTOR="$BLISS_ROOT"/prebuilts/tools-bliss/"${HOST,,}"-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -05001195 fi
1196
codeworkx1c29bf62018-09-23 12:36:57 +02001197 if [ -z "$CDEXCONVERTER" ]; then
Bruno Martins98599b62019-10-02 15:56:25 -06001198 export CDEXCONVERTER="$BLISS_ROOT"/prebuilts/tools-bliss/"${HOST,,}"-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +02001199 fi
1200
Luca Stefani7f9fff22016-07-18 13:47:55 +02001201 # Extract existing boot.oats to the temp folder
1202 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001203 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +02001204 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001205 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +03001206 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001207 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001208 else
1209 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001210 fi
1211 done
1212 fi
1213
1214 if [ -z "$ARCHES" ]; then
1215 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1216 fi
1217
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001218 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -07001219 return;
1220 fi
1221
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001222 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001223 return 0 # target apk|jar is already odexed, return
1224 fi
1225
1226 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001227 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001228
1229 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001230 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001231
1232 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001233 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -05001234 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001235 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1236 for CLASS in $CLASSES; do
1237 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1238 # Check if we have to deal with CompactDex
1239 if [[ "$CLASS" == *.cdex ]]; then
1240 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1241 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1242 else
1243 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1244 fi
1245 done
Joe Maples9be579f2018-01-05 14:51:33 -05001246 else
1247 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1248 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001249 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001250 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001251 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001252 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001253 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001254 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001255 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001256 # try to extract classes.dex from boot.vdex for frameworks jars
1257 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001258 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001259 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001260 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1261 for CLASS in $CLASSES; do
1262 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1263 # Check if we have to deal with CompactDex
1264 if [[ "$CLASS" == *.cdex ]]; then
1265 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1266 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1267 else
1268 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1269 fi
1270 done
Joe Maples9be579f2018-01-05 14:51:33 -05001271 else
1272 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1273 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1274 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001275 else
1276 continue
1277 fi
1278
Luca Stefani7f9fff22016-07-18 13:47:55 +02001279 done
1280
1281 rm -rf "$TMPDIR/dexout"
1282}
1283
1284#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001285# init_adb_connection:
1286#
1287# Starts adb server and waits for the device
1288#
1289function init_adb_connection() {
1290 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1291 if ! _adb_connected; then
1292 echo "No device is online. Waiting for one..."
1293 echo "Please connect USB and/or enable USB debugging"
1294 until _adb_connected; do
1295 sleep 1
1296 done
1297 echo "Device Found."
1298 fi
1299
1300 # Retrieve IP and PORT info if we're using a TCP connection
1301 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1302 | head -1 | awk '{print $1}')
1303 adb root &> /dev/null
1304 sleep 0.3
1305 if [ -n "$TCPIPPORT" ]; then
1306 # adb root just killed our connection
1307 # so reconnect...
1308 adb connect "$TCPIPPORT"
1309 fi
1310 adb wait-for-device &> /dev/null
1311 sleep 0.3
1312}
1313
1314#
Luca Stefani3a030122016-07-30 12:08:25 +02001315# fix_xml:
1316#
1317# $1: xml file to fix
1318#
1319function fix_xml() {
1320 local XML="$1"
1321 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1322
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001323 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1324 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001325
1326 mv "$TEMP_XML" "$XML"
1327}
1328
Vladimir Oltean4818c232019-01-17 03:07:34 +02001329function get_hash() {
1330 local FILE="$1"
1331
1332 if [ "$(uname)" == "Darwin" ]; then
1333 shasum "${FILE}" | awk '{print $1}'
1334 else
1335 sha1sum "${FILE}" | awk '{print $1}'
1336 fi
1337}
1338
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001339function print_spec() {
1340 local SPEC_PRODUCT_PACKAGE="$1"
1341 local SPEC_SRC_FILE="$2"
1342 local SPEC_DST_FILE="$3"
1343 local SPEC_ARGS="$4"
1344 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001345 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001346
1347 local PRODUCT_PACKAGE=""
1348 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1349 PRODUCT_PACKAGE="-"
1350 fi
1351 local SRC=""
1352 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1353 SRC="${SPEC_SRC_FILE}:"
1354 fi
1355 local DST=""
1356 if [ ! -z "${SPEC_DST_FILE}" ]; then
1357 DST="${SPEC_DST_FILE}"
1358 fi
1359 local ARGS=""
1360 if [ ! -z "${SPEC_ARGS}" ]; then
1361 ARGS=";${SPEC_ARGS}"
1362 fi
1363 local HASH=""
1364 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1365 HASH="|${SPEC_HASH}"
1366 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001367 local FIXUP_HASH=""
1368 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1369 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1370 fi
1371 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1372}
1373
1374# To be overridden by device-level extract-files.sh
1375# Parameters:
1376# $1: spec name of a blob. Can be used for filtering.
1377# If the spec is "src:dest", then $1 is "dest".
1378# If the spec is "src", then $1 is "src".
1379# $2: path to blob file. Can be used for fixups.
1380#
1381function blob_fixup() {
1382 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001383}
1384
Luca Stefani3a030122016-07-30 12:08:25 +02001385#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001386# extract:
1387#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001388# Positional parameters:
1389# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001390# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001391# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1392#
1393# Non-positional parameters (coming after $2):
1394# --section: preferred way of selecting the portion to parse and extract from
1395# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001396# --kang: if present, this option will activate the printing of hashes for the
1397# extracted blobs. Useful with --section for subsequent pinning of
1398# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001399#
1400function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001401 # Consume positional parameters
1402 local PROPRIETARY_FILES_TXT="$1"; shift
1403 local SRC="$1"; shift
1404 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001405 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001406
1407 # Consume optional, non-positional parameters
1408 while [ "$#" -gt 0 ]; do
1409 case "$1" in
1410 -s|--section)
1411 SECTION="$2"; shift
1412 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001413 -k|--kang)
1414 KANG=true
1415 DISABLE_PINNING=1
1416 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001417 *)
1418 # Backwards-compatibility with the old behavior, where $3, if
1419 # present, denoted an optional positional ${SECTION} argument.
1420 # Users of ${SECTION} are encouraged to migrate from setting it as
1421 # positional $3, to non-positional --section ${SECTION}, the
1422 # reason being that it doesn't scale to have more than 1 optional
1423 # positional argument.
1424 SECTION="$1"
1425 ;;
1426 esac
1427 shift
1428 done
1429
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001430 if [ -z "$OUTDIR" ]; then
1431 echo "Output dir not set!"
1432 exit 1
1433 fi
1434
Vladimir Olteanc5034462019-01-17 03:04:16 +02001435 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001436
1437 # Allow failing, so we can try $DEST and/or $FILE
1438 set +e
1439
1440 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001441 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001442 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001443 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001444 local COUNT=${#FILELIST[@]}
Jackeaglecf6f4de2019-09-24 04:07:22 -04001445 local OUTPUT_ROOT="$BLISS_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001446 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1447
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001448 if [ "$SRC" = "adb" ]; then
1449 init_adb_connection
1450 fi
1451
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001452 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001453 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001454
1455 # Check if we're working with the same zip that was passed last time.
1456 # If so, let's just use what's already extracted.
1457 MD5=`md5sum "$SRC"| awk '{print $1}'`
1458 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1459
1460 if [ "$MD5" != "$OLDMD5" ]; then
1461 rm -rf "$DUMPDIR"
1462 mkdir "$DUMPDIR"
1463 unzip "$SRC" -d "$DUMPDIR"
1464 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1465
1466 # Stop if an A/B OTA zip is detected. We cannot extract these.
1467 if [ -a "$DUMPDIR"/payload.bin ]; then
1468 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1469 exit 1
1470 # If OTA is block based, extract it.
1471 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1472 echo "Converting system.new.dat to system.img"
Jackeaglecf6f4de2019-09-24 04:07:22 -04001473 python "$BLISS_ROOT"/vendor/bliss/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001474 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1475 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1476 echo "Requesting sudo access to mount the system.img"
1477 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1478 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1479 sudo umount "$DUMPDIR"/tmp
1480 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1481 fi
1482 fi
1483
1484 SRC="$DUMPDIR"
1485 fi
1486
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001487 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001488 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001489 rm -rf "${OUTPUT_TMP:?}"
1490 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001491 if [ -d "$OUTPUT_ROOT" ]; then
1492 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1493 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001494 VENDOR_STATE=1
1495 fi
1496
Vladimir Olteanc5034462019-01-17 03:04:16 +02001497 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001498
1499 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001500
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001501 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001502 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001503 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001504 local OUTPUT_DIR=
1505 local TMP_DIR=
1506 local SRC_FILE=
1507 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001508 local IS_PRODUCT_PACKAGE=false
1509
1510 # Note: this relies on the fact that the ${FILELIST[@]} array
1511 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1512 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1513 IS_PRODUCT_PACKAGE=true
1514 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001515
Vladimir Olteand652a062018-06-24 20:42:01 +03001516 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001517 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1518 TMP_DIR="${OUTPUT_TMP}/rootfs"
1519 SRC_FILE="/${SPEC_SRC_FILE}"
1520 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001521 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001522 OUTPUT_DIR="${OUTPUT_ROOT}"
1523 TMP_DIR="${OUTPUT_TMP}"
1524 SRC_FILE="/system/${SPEC_SRC_FILE}"
1525 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001526 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001527
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001528 # Strip the file path in the vendor repo of "system", if present
1529 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001530 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001531 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001532
Gabriele Me6df25b2017-10-11 00:58:59 +02001533 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001534 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001535 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001536 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001537 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001538 if [ -f "${VENDOR_REPO_FILE}" ]; then
1539 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001540 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001541 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001542 fi
1543 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001544 local TMP_HASH=$(get_hash "${PINNED}")
1545 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001546 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001547 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1548 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001549 fi
1550 fi
1551 fi
1552 fi
1553
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001554 if [ "${KANG}" = false ]; then
1555 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1556 fi
1557
Gabriele Me6df25b2017-10-11 00:58:59 +02001558 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001559 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001560 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001561 FOUND=false
1562 # Try Lineage target first.
1563 # Also try to search for files stripped of
1564 # the "/system" prefix, if we're actually extracting
1565 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001566 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001567 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1568 FOUND=true
1569 break
1570 }
1571 done
1572
1573 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001574 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001575 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001576 fi
1577 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001578
Vladimir Oltean4818c232019-01-17 03:07:34 +02001579 # Blob fixup pipeline has 2 parts: one that is fixed and
1580 # one that is user-configurable
1581 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1582 # Deodex apk|jar if that's the case
1583 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1584 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1585 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001586 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1587 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001588 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001589 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001590 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1591 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001592 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001593 # Now run user-supplied fixup function
1594 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1595 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001596
Vladimir Olteand6747712018-06-24 20:46:42 +03001597 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001598 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001599 local TYPE="${DIR##*/}"
1600 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001601 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001602 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001603 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001604 fi
1605 fi
1606
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001607 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001608 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1609 fi
1610
1611 # Check and print whether the fixup pipeline actually did anything.
1612 # This isn't done right after the fixup pipeline because we want this print
1613 # to come after print_spec above, when in kang mode.
1614 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1615 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1616 # Now sanity-check the spec for this blob.
1617 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1618 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1619 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1620 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001621 fi
1622
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001623 done
1624
1625 # Don't allow failing
1626 set -e
1627}
Louis Popia516c2f2016-07-25 15:51:13 +02001628
1629#
1630# extract_firmware:
1631#
1632# $1: file containing the list of items to extract
1633# $2: path to extracted radio folder
1634#
1635function extract_firmware() {
1636 if [ -z "$OUTDIR" ]; then
1637 echo "Output dir not set!"
1638 exit 1
1639 fi
1640
1641 parse_file_list "$1"
1642
1643 # Don't allow failing
1644 set -e
1645
1646 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1647 local COUNT=${#FILELIST[@]}
1648 local SRC="$2"
Jackeaglecf6f4de2019-09-24 04:07:22 -04001649 local OUTPUT_DIR="$BLISS_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001650
1651 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1652 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1653 rm -rf "${OUTPUT_DIR:?}/"*
1654 VENDOR_RADIO_STATE=1
1655 fi
1656
1657 echo "Extracting $COUNT files in $1 from $SRC:"
1658
1659 for (( i=1; i<COUNT+1; i++ )); do
1660 local FILE="${FILELIST[$i-1]}"
1661 printf ' - %s \n' "/radio/$FILE"
1662
1663 if [ ! -d "$OUTPUT_DIR" ]; then
1664 mkdir -p "$OUTPUT_DIR"
1665 fi
1666 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1667 chmod 644 "$OUTPUT_DIR/$FILE"
1668 done
1669}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001670
1671function extract_img_data() {
1672 local image_file="$1"
1673 local out_dir="$2"
1674 local logFile="$TMPDIR/debugfs.log"
1675
1676 if [ ! -d "$out_dir" ]; then
1677 mkdir -p "$out_dir"
1678 fi
1679
1680 if [[ "$HOST_OS" == "Darwin" ]]; then
1681 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1682 echo "[-] Failed to extract data from '$image_file'"
1683 abort 1
1684 }
1685 else
1686 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1687 do
1688 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1689 echo "[-] Failed to extract data from '$image_file'"
1690 abort 1
1691 }
1692 done
1693 fi
1694
1695 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1696 if grep -Fq "$symlink_err" "$logFile"; then
1697 echo "[-] Symlinks have not been properly processed from $image_file"
1698 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1699 abort 1
1700 fi
1701}
1702
1703declare -ra VENDOR_SKIP_FILES=(
1704 "bin/toybox_vendor"
1705 "bin/toolbox"
1706 "bin/grep"
1707 "build.prop"
1708 "compatibility_matrix.xml"
1709 "default.prop"
1710 "etc/NOTICE.xml.gz"
1711 "etc/vintf/compatibility_matrix.xml"
1712 "etc/vintf/manifest.xml"
1713 "etc/wifi/wpa_supplicant.conf"
1714 "manifest.xml"
1715 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1716 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1717 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1718 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1719 "overlay/framework-res__auto_generated_rro.apk"
1720 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1721)
1722
1723function array_contains() {
1724 local element
1725 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1726 return 1
1727}
1728
1729function generate_prop_list_from_image() {
1730 local image_file="$1"
1731 local image_dir="$TMPDIR/image-temp"
1732 local output_list="$2"
1733 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1734 local -n skipped_vendor_files="$3"
1735
1736 extract_img_data "$image_file" "$image_dir"
1737
1738 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1739 do
1740 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1741 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1742 continue
1743 fi
1744 # Skip device defined skipped files since they will be re-generated at build time
1745 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1746 continue
1747 fi
1748 if suffix_match_file ".apk" "$FILE" ; then
1749 echo "-vendor/$FILE" >> "$output_list_tmp"
1750 else
1751 echo "vendor/$FILE" >> "$output_list_tmp"
1752 fi
1753 done
1754
1755 # Sort merged file with all lists
1756 sort -u "$output_list_tmp" > "$output_list"
1757
1758 # Clean-up
1759 rm -f "$output_list_tmp"
1760}