blob: b04ce10f4e041300a8f9bd1a700ba48b840ea28b [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
87 export ANDROIDMK="$BLISS_ROOT"/"$OUTDIR"/Android.mk
88 export BOARDMK="$BLISS_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -070089
90 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
91 COMMON=1
92 else
93 COMMON=0
94 fi
95
Gabriele Mb6effb32017-05-01 18:22:04 +020096 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -070097 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +020098 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070099 else
100 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +0200101 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700102 fi
103}
104
Vladimir Oltean95643282018-06-24 20:22:41 +0300105# Helper functions for parsing a spec.
106# notes: an optional "|SHA1" that may appear in the format is stripped
107# early from the spec in the parse_file_list function, and
108# should not be present inside the input parameter passed
109# to these functions.
110
111#
112# input: spec in the form of "src[:dst][;args]"
113# output: "src"
114#
115function src_file() {
116 local SPEC="$1"
117 local SPLIT=(${SPEC//:/ })
118 local ARGS="$(target_args ${SPEC})"
119 # Regardless of there being a ":" delimiter or not in the spec,
120 # the source file is always either the first, or the only entry.
121 local SRC="${SPLIT[0]}"
122 # Remove target_args suffix, if present
123 echo "${SRC%;${ARGS}}"
124}
125
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700126#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300127# input: spec in the form of "src[:dst][;args]"
128# output: "dst" if present, "src" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700129#
130function target_file() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300131 local SPEC="$1"
132 local SPLIT=(${SPEC//:/ })
133 local ARGS="$(target_args ${SPEC})"
134 local DST=
135 case ${#SPLIT[@]} in
136 1)
137 # The spec doesn't have a : delimiter
138 DST="${SPLIT[0]}"
139 ;;
140 *)
141 # The spec actually has a src:dst format
142 DST="${SPLIT[1]}"
143 ;;
144 esac
145 # Remove target_args suffix, if present
146 echo "${DST%;${ARGS}}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700147}
148
149#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300150# input: spec in the form of "src[:dst][;args]"
151# output: "args" if present, "" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700152#
153function target_args() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300154 local SPEC="$1"
155 local SPLIT=(${SPEC//;/ })
156 local ARGS=
157 case ${#SPLIT[@]} in
158 1)
159 # No ";" delimiter in the spec.
160 ;;
161 *)
162 # The "args" are whatever comes after the ";" character.
163 # Basically the spec stripped of whatever is to the left of ";".
164 ARGS="${SPEC#${SPLIT[0]};}"
165 ;;
166 esac
167 echo "${ARGS}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700168}
169
170#
171# prefix_match:
172#
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300173# input:
174# - $1: prefix
175# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
176# output:
177# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700178#
179function prefix_match() {
180 local PREFIX="$1"
Vladimir Olteana48b9fe2018-04-02 22:37:09 +0300181 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
182 local FILE=$(target_file "$LINE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700183 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300184 local ARGS=$(target_args "$LINE")
185 if [ -z "${ARGS}" ]; then
186 echo "${FILE#$PREFIX}"
187 else
188 echo "${FILE#$PREFIX};${ARGS}"
189 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700190 fi
191 done
192}
193
194#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400195# prefix_match_file:
196#
197# $1: the prefix to match on
198# $2: the file to match the prefix for
199#
200# Internal function which returns true if a filename contains the
201# specified prefix.
202#
203function prefix_match_file() {
204 local PREFIX="$1"
205 local FILE="$2"
206 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
207 return 0
208 else
209 return 1
210 fi
211}
212
213#
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -0700214# suffix_match_file:
215#
216# $1: the suffix to match on
217# $2: the file to match the suffix for
218#
219# Internal function which returns true if a filename contains the
220# specified suffix.
221#
222function suffix_match_file() {
223 local SUFFIX="$1"
224 local FILE="$2"
225 if [[ "$FILE" = *"$SUFFIX" ]]; then
226 return 0
227 else
228 return 1
229 fi
230}
231
232#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400233# truncate_file
234#
235# $1: the filename to truncate
236# $2: the argument to output the truncated filename to
237#
238# Internal function which truncates a filename by removing the first dir
239# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
240#
241function truncate_file() {
242 local FILE="$1"
243 RETURN_FILE="$2"
244 local FIND="${FILE%%/*}"
245 local LOCATION="${#FIND}+1"
246 echo ${FILE:$LOCATION}
247}
248
249#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700250# write_product_copy_files:
251#
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400252# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400253#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700254# Creates the PRODUCT_COPY_FILES section in the product makefile for all
255# items in the list which do not start with a dash (-).
256#
257function write_product_copy_files() {
258 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
259 local TARGET=
260 local FILE=
261 local LINEEND=
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400262 local TREBLE_COMPAT=$1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700263
264 if [ "$COUNT" -eq "0" ]; then
265 return 0
266 fi
267
268 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
269 for (( i=1; i<COUNT+1; i++ )); do
270 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
271 LINEEND=" \\"
272 if [ "$i" -eq "$COUNT" ]; then
273 LINEEND=""
274 fi
275
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300276 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400277 if prefix_match_file "vendor/" $TARGET ; then
278 local OUTTARGET=$(truncate_file $TARGET)
279 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
280 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
281 elif prefix_match_file "product/" $TARGET ; then
282 local OUTTARGET=$(truncate_file $TARGET)
283 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
284 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
285 elif prefix_match_file "odm/" $TARGET ; then
286 local OUTTARGET=$(truncate_file $TARGET)
287 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
288 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
289 elif prefix_match_file "system/" $TARGET ; then
290 local OUTTARGET=$(truncate_file $TARGET)
291 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
292 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400293 else
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400294 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400295 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
296 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700297 done
298 return 0
299}
300
301#
302# write_packages:
303#
304# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400305# $2: /system, /odm, /product, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700306# $3: type-specific extra flags
307# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700308#
309# Internal function which writes out the BUILD_PREBUILT stanzas
310# for all modules in the list. This is called by write_product_packages
311# after the modules are categorized.
312#
313function write_packages() {
314
315 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500316 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700317 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700318
319 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700320 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700321 local FILELIST=("${!ARR_NAME}")
322
323 local FILE=
324 local ARGS=
325 local BASENAME=
326 local EXTENSION=
327 local PKGNAME=
328 local SRC=
329
330 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300331 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700332 ARGS=$(target_args "$P")
333
334 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100335 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700336 EXTENSION=${BASENAME##*.}
337 PKGNAME=${BASENAME%.*}
338
339 # Add to final package list
340 PACKAGE_LIST+=("$PKGNAME")
341
342 SRC="proprietary"
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400343 if [ "$PARTITION" = "system" ]; then
344 SRC+="/system"
345 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700346 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500347 elif [ "$PARTITION" = "product" ]; then
348 SRC+="/product"
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700349 elif [ "$PARTITION" = "odm" ]; then
350 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700351 fi
352
353 printf 'include $(CLEAR_VARS)\n'
354 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
355 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
356 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700357 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700358 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
359 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
360 #if [ "$VENDOR_PKG" = "true" ]; then
361 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
362 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
363 #else
364 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
365 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
366 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700367 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700368 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
369 else
370 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
371 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700372 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700373 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700374 fi
375 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200376 if [ "$EXTRA" = "priv-app" ]; then
377 SRC="$SRC/priv-app"
378 else
379 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700380 fi
381 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
382 local CERT=platform
383 if [ ! -z "$ARGS" ]; then
384 CERT="$ARGS"
385 fi
386 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
387 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
388 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200389 local CERT=platform
390 if [ ! -z "$ARGS" ]; then
391 CERT="$ARGS"
392 fi
393 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700394 elif [ "$CLASS" = "ETC" ]; then
395 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
396 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700397 if [ "$ARGS" = "rootfs" ]; then
398 SRC="$SRC/rootfs"
399 if [ "$EXTRA" = "sbin" ]; then
400 SRC="$SRC/sbin"
401 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
402 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
403 fi
404 else
405 SRC="$SRC/bin"
406 fi
407 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
408 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700409 else
Steve Kondika991cf12016-07-28 12:13:12 -0700410 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700411 fi
412 printf 'LOCAL_MODULE_TAGS := optional\n'
413 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400414 if [ "$CLASS" = "APPS" ]; then
415 printf 'LOCAL_DEX_PREOPT := false\n'
416 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700417 if [ ! -z "$EXTENSION" ]; then
418 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
419 fi
M1cha15f226c2017-01-04 09:00:11 +0100420 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
421 if [ "$DIRNAME" != "." ]; then
422 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
423 fi
424 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700425 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700426 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
427 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500428 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800429 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500430 elif [ "$PARTITION" = "product" ]; then
431 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700432 elif [ "$PARTITION" = "odm" ]; then
433 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700434 fi
435 printf 'include $(BUILD_PREBUILT)\n\n'
436 done
437}
438
439#
440# write_product_packages:
441#
442# This function will create BUILD_PREBUILT entries in the
443# Android.mk and associated PRODUCT_PACKAGES list in the
444# product makefile for all files in the blob list which
445# start with a single dash (-) character.
446#
447function write_product_packages() {
448 PACKAGE_LIST=()
449
450 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
451
452 if [ "$COUNT" = "0" ]; then
453 return 0
454 fi
455
456 # Figure out what's 32-bit, what's 64-bit, and what's multilib
457 # I really should not be doing this in bash due to shitty array passing :(
458 local T_LIB32=( $(prefix_match "lib/") )
459 local T_LIB64=( $(prefix_match "lib64/") )
460 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
461 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700462 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700463
Steve Kondik03ce4002016-07-29 00:00:16 -0700464 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500465 write_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700466 fi
467 if [ "${#LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500468 write_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700469 fi
470 if [ "${#LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500471 write_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700472 fi
473
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400474 local T_S_LIB32=( $(prefix_match "system/lib/") )
475 local T_S_LIB64=( $(prefix_match "system/lib64/") )
476 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
477 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
478 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
479
480 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
481 write_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDMK"
482 fi
483 if [ "${#S_LIB32[@]}" -gt "0" ]; then
484 write_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDMK"
485 fi
486 if [ "${#S_LIB64[@]}" -gt "0" ]; then
487 write_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDMK"
488 fi
489
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700490 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
491 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
492 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
493 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700494 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700495
496 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500497 write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700498 fi
499 if [ "${#V_LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500500 write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700501 fi
502 if [ "${#V_LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500503 write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
504 fi
505
506 local T_P_LIB32=( $(prefix_match "product/lib/") )
507 local T_P_LIB64=( $(prefix_match "product/lib64/") )
508 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
509 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
510 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
511
512 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
513 write_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDMK"
514 fi
515 if [ "${#P_LIB32[@]}" -gt "0" ]; then
516 write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
517 fi
518 if [ "${#P_LIB64[@]}" -gt "0" ]; then
519 write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700520 fi
521
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700522 local T_O_LIB32=( $(prefix_match "odm/lib/") )
523 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
524 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
525 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
526 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
527
528 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
529 write_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDMK"
530 fi
531 if [ "${#O_LIB32[@]}" -gt "0" ]; then
532 write_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDMK"
533 fi
534 if [ "${#O_LIB64[@]}" -gt "0" ]; then
535 write_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDMK"
536 fi
537
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700538 # Apps
539 local APPS=( $(prefix_match "app/") )
540 if [ "${#APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500541 write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700542 fi
543 local PRIV_APPS=( $(prefix_match "priv-app/") )
544 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500545 write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700546 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400547 local S_APPS=( $(prefix_match "system/app/") )
548 if [ "${#S_APPS[@]}" -gt "0" ]; then
549 write_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDMK"
550 fi
551 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
552 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
553 write_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDMK"
554 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700555 local V_APPS=( $(prefix_match "vendor/app/") )
556 if [ "${#V_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500557 write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700558 fi
559 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
560 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500561 write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
562 fi
563 local P_APPS=( $(prefix_match "product/app/") )
564 if [ "${#P_APPS[@]}" -gt "0" ]; then
565 write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
566 fi
567 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
568 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
569 write_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700570 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700571 local O_APPS=( $(prefix_match "odm/app/") )
572 if [ "${#O_APPS[@]}" -gt "0" ]; then
573 write_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
574 fi
575 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
576 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
577 write_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
578 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700579
580 # Framework
581 local FRAMEWORK=( $(prefix_match "framework/") )
582 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500583 write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700584 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400585 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
586 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
587 write_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDMK"
588 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200589 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200590 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500591 write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
592 fi
593 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
594 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
595 write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
Christian Oderc16f3272017-10-08 23:15:52 +0200596 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700597 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
598 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
599 write_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
600 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700601
602 # Etc
603 local ETC=( $(prefix_match "etc/") )
604 if [ "${#ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500605 write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700606 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400607 local S_ETC=( $(prefix_match "system/etc/") )
608 if [ "${#ETC[@]}" -gt "0" ]; then
609 write_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDMK"
610 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700611 local V_ETC=( $(prefix_match "vendor/etc/") )
612 if [ "${#V_ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500613 write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
614 fi
615 local P_ETC=( $(prefix_match "product/etc/") )
616 if [ "${#P_ETC[@]}" -gt "0" ]; then
617 write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700618 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700619 local O_ETC=( $(prefix_match "odm/etc/") )
620 if [ "${#O_ETC[@]}" -gt "0" ]; then
621 write_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDMK"
622 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700623
624 # Executables
625 local BIN=( $(prefix_match "bin/") )
626 if [ "${#BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500627 write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700628 fi
Rashed Abdel-Tawab1370f192019-09-28 23:37:36 -0400629 local S_BIN=( $(prefix_match "system/bin/") )
630 if [ "${#BIN[@]}" -gt "0" ]; then
631 write_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDMK"
632 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700633 local V_BIN=( $(prefix_match "vendor/bin/") )
634 if [ "${#V_BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500635 write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
636 fi
637 local P_BIN=( $(prefix_match "product/bin/") )
638 if [ "${#P_BIN[@]}" -gt "0" ]; then
639 write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700640 fi
Rashed Abdel-Tawab8c3c95f2019-09-20 07:32:39 -0700641 local O_BIN=( $(prefix_match "odm/bin/") )
642 if [ "${#O_BIN[@]}" -gt "0" ]; then
643 write_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDMK"
644 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700645 local SBIN=( $(prefix_match "sbin/") )
646 if [ "${#SBIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500647 write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700648 fi
649
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700650
651 # Actually write out the final PRODUCT_PACKAGES list
652 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
653
654 if [ "$PACKAGE_COUNT" -eq "0" ]; then
655 return 0
656 fi
657
658 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
659 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
660 local LINEEND=" \\"
661 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
662 LINEEND=""
663 fi
664 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
665 done
666}
667
668#
669# write_header:
670#
671# $1: file which will be written to
672#
673# writes out the copyright header with the current year.
674# note that this is not an append operation, and should
675# be executed first!
676#
677function write_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600678 if [ -f $1 ]; then
679 rm $1
680 fi
681
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700682 YEAR=$(date +"%Y")
683
684 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
685
Matt Mower8945f5e2017-01-07 14:08:17 -0600686 NUM_REGEX='^[0-9]+$'
687 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
688 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
689 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
690 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
691 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
692 fi
693 if [ $YEAR -eq 2017 ]; then
694 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
695 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
696 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
697 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
698 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
699 else
700 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
701 fi
702 else
703 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
704 fi
705
706 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700707#
708# Licensed under the Apache License, Version 2.0 (the "License");
709# you may not use this file except in compliance with the License.
710# You may obtain a copy of the License at
711#
712# http://www.apache.org/licenses/LICENSE-2.0
713#
714# Unless required by applicable law or agreed to in writing, software
715# distributed under the License is distributed on an "AS IS" BASIS,
716# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
717# See the License for the specific language governing permissions and
718# limitations under the License.
719
720# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
721
722EOF
723}
724
725#
726# write_headers:
727#
728# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400729# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700730#
731# Calls write_header for each of the makefiles and creates
732# the initial path declaration and device guard for the
733# Android.mk
734#
735function write_headers() {
736 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400737
738 GUARD="$2"
739 if [ -z "$GUARD" ]; then
740 GUARD="TARGET_DEVICE"
741 fi
742
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700743 cat << EOF >> "$ANDROIDMK"
744LOCAL_PATH := \$(call my-dir)
745
746EOF
747 if [ "$COMMON" -ne 1 ]; then
748 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400749ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700750
751EOF
752 else
753 if [ -z "$1" ]; then
754 echo "Argument with devices to be added to guard must be set!"
755 exit 1
756 fi
757 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400758ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700759
760EOF
761 fi
762
763 write_header "$BOARDMK"
764 write_header "$PRODUCTMK"
765}
766
767#
768# write_footers:
769#
770# Closes the inital guard and any other finalization tasks. Must
771# be called as the final step.
772#
773function write_footers() {
774 cat << EOF >> "$ANDROIDMK"
775endif
776EOF
777}
778
779# Return success if adb is up and not in recovery
780function _adb_connected {
781 {
Steve Kondik7561d192016-09-01 21:40:27 -0700782 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700783 then
784 return 0
785 fi
786 } 2>/dev/null
787
788 return 1
789};
790
791#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100792# parse_file_list:
793#
794# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400795# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +0100796#
797# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700798#
799function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100800 if [ -z "$1" ]; then
801 echo "An input file is expected!"
802 exit 1
803 elif [ ! -f "$1" ]; then
804 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700805 exit 1
806 fi
807
Vladimir Olteanc5034462019-01-17 03:04:16 +0200808 if [ -n "$2" ]; then
809 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400810 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +0200811 # Match all lines starting with first line found to start* with '#'
812 # comment and contain** $2, and ending with first line to be empty*.
813 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
814 # **the $2 match is case-insensitive
815 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400816 else
817 LIST=$1
818 fi
819
820
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700821 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700822 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200823 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700824 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700825 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200826 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700827
828 while read -r line; do
829 if [ -z "$line" ]; then continue; fi
830
Steve Kondik48f8df82016-08-14 03:55:08 -0700831 # If the line has a pipe delimiter, a sha1 hash should follow.
832 # This indicates the file should be pinned and not overwritten
833 # when extracting files.
834 local SPLIT=(${line//\|/ })
835 local COUNT=${#SPLIT[@]}
836 local SPEC=${SPLIT[0]}
837 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +0200838 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -0700839 if [ "$COUNT" -gt "1" ]; then
840 HASH=${SPLIT[1]}
841 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +0200842 if [ "$COUNT" -gt "2" ]; then
843 FIXUP_HASH=${SPLIT[2]}
844 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700845
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700846 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700847 if [[ "$SPEC" =~ ^- ]]; then
848 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
849 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200850 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700851 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700852 PRODUCT_COPY_FILES_LIST+=("$SPEC")
853 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200854 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700855 fi
856
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400857 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700858}
859
860#
861# write_makefiles:
862#
863# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400864# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700865#
866# Calls write_product_copy_files and write_product_packages on
867# the given file and appends to the Android.mk as well as
868# the product makefile.
869#
870function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700871 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400872 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700873 write_product_packages
874}
875
876#
Louis Popia516c2f2016-07-25 15:51:13 +0200877# append_firmware_calls_to_makefiles:
878#
879# Appends to Android.mk the calls to all images present in radio folder
880# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
881#
882function append_firmware_calls_to_makefiles() {
883 cat << EOF >> "$ANDROIDMK"
884ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
885
886RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
887\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
888 \$(call add-radio-file,radio/\$(f)))
889\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
890
891endif
892
893EOF
894}
895
896#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200897# get_file:
898#
899# $1: input file
900# $2: target file/folder
901# $3: source of the file (can be "adb" or a local folder)
902#
903# Silently extracts the input file to defined target
904# Returns success if file can be pulled from the device or found locally
905#
906function get_file() {
907 local SRC="$3"
908
909 if [ "$SRC" = "adb" ]; then
910 # try to pull
911 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
912
913 return 1
914 else
915 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +0300916 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
917 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +0200918 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200919
920 return 1
921 fi
922};
923
924#
925# oat2dex:
926#
927# $1: extracted apk|jar (to check if deodex is required)
928# $2: odexed apk|jar to deodex
929# $3: source of the odexed apk|jar
930#
931# Convert apk|jar .odex in the corresposing classes.dex
932#
933function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200934 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200935 local OEM_TARGET="$2"
936 local SRC="$3"
937 local TARGET=
938 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -0500939 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200940
941 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins98599b62019-10-02 15:56:25 -0600942 export BAKSMALIJAR="$BLISS_ROOT"/prebuilts/tools-bliss/common/smali/baksmali.jar
943 export SMALIJAR="$BLISS_ROOT"/prebuilts/tools-bliss/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200944 fi
945
Joe Maples9be579f2018-01-05 14:51:33 -0500946 if [ -z "$VDEXEXTRACTOR" ]; then
Bruno Martins98599b62019-10-02 15:56:25 -0600947 export VDEXEXTRACTOR="$BLISS_ROOT"/prebuilts/tools-bliss/"${HOST,,}"-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -0500948 fi
949
codeworkx1c29bf62018-09-23 12:36:57 +0200950 if [ -z "$CDEXCONVERTER" ]; then
Bruno Martins98599b62019-10-02 15:56:25 -0600951 export CDEXCONVERTER="$BLISS_ROOT"/prebuilts/tools-bliss/"${HOST,,}"-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +0200952 fi
953
Luca Stefani7f9fff22016-07-18 13:47:55 +0200954 # Extract existing boot.oats to the temp folder
955 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700956 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200957 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700958 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +0300959 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200960 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700961 else
962 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200963 fi
964 done
965 fi
966
967 if [ -z "$ARCHES" ]; then
968 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
969 fi
970
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200971 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700972 return;
973 fi
974
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200975 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200976 return 0 # target apk|jar is already odexed, return
977 fi
978
979 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700980 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200981
982 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400983 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200984
985 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400986 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -0500987 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -0700988 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
989 for CLASS in $CLASSES; do
990 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
991 # Check if we have to deal with CompactDex
992 if [[ "$CLASS" == *.cdex ]]; then
993 "$CDEXCONVERTER" "$CLASS" &>/dev/null
994 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
995 else
996 mv "$CLASS" "$TMPDIR/$NEWCLASS"
997 fi
998 done
Joe Maples9be579f2018-01-05 14:51:33 -0500999 else
1000 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1001 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001002 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001003 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001004 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001005 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001006 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001007 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001008 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001009 # try to extract classes.dex from boot.vdex for frameworks jars
1010 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001011 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001012 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001013 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1014 for CLASS in $CLASSES; do
1015 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1016 # Check if we have to deal with CompactDex
1017 if [[ "$CLASS" == *.cdex ]]; then
1018 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1019 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1020 else
1021 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1022 fi
1023 done
Joe Maples9be579f2018-01-05 14:51:33 -05001024 else
1025 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1026 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1027 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001028 else
1029 continue
1030 fi
1031
Luca Stefani7f9fff22016-07-18 13:47:55 +02001032 done
1033
1034 rm -rf "$TMPDIR/dexout"
1035}
1036
1037#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001038# init_adb_connection:
1039#
1040# Starts adb server and waits for the device
1041#
1042function init_adb_connection() {
1043 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1044 if ! _adb_connected; then
1045 echo "No device is online. Waiting for one..."
1046 echo "Please connect USB and/or enable USB debugging"
1047 until _adb_connected; do
1048 sleep 1
1049 done
1050 echo "Device Found."
1051 fi
1052
1053 # Retrieve IP and PORT info if we're using a TCP connection
1054 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1055 | head -1 | awk '{print $1}')
1056 adb root &> /dev/null
1057 sleep 0.3
1058 if [ -n "$TCPIPPORT" ]; then
1059 # adb root just killed our connection
1060 # so reconnect...
1061 adb connect "$TCPIPPORT"
1062 fi
1063 adb wait-for-device &> /dev/null
1064 sleep 0.3
1065}
1066
1067#
Luca Stefani3a030122016-07-30 12:08:25 +02001068# fix_xml:
1069#
1070# $1: xml file to fix
1071#
1072function fix_xml() {
1073 local XML="$1"
1074 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1075
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001076 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1077 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001078
1079 mv "$TEMP_XML" "$XML"
1080}
1081
Vladimir Oltean4818c232019-01-17 03:07:34 +02001082function get_hash() {
1083 local FILE="$1"
1084
1085 if [ "$(uname)" == "Darwin" ]; then
1086 shasum "${FILE}" | awk '{print $1}'
1087 else
1088 sha1sum "${FILE}" | awk '{print $1}'
1089 fi
1090}
1091
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001092function print_spec() {
1093 local SPEC_PRODUCT_PACKAGE="$1"
1094 local SPEC_SRC_FILE="$2"
1095 local SPEC_DST_FILE="$3"
1096 local SPEC_ARGS="$4"
1097 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001098 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001099
1100 local PRODUCT_PACKAGE=""
1101 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1102 PRODUCT_PACKAGE="-"
1103 fi
1104 local SRC=""
1105 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1106 SRC="${SPEC_SRC_FILE}:"
1107 fi
1108 local DST=""
1109 if [ ! -z "${SPEC_DST_FILE}" ]; then
1110 DST="${SPEC_DST_FILE}"
1111 fi
1112 local ARGS=""
1113 if [ ! -z "${SPEC_ARGS}" ]; then
1114 ARGS=";${SPEC_ARGS}"
1115 fi
1116 local HASH=""
1117 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1118 HASH="|${SPEC_HASH}"
1119 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001120 local FIXUP_HASH=""
1121 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1122 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1123 fi
1124 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1125}
1126
1127# To be overridden by device-level extract-files.sh
1128# Parameters:
1129# $1: spec name of a blob. Can be used for filtering.
1130# If the spec is "src:dest", then $1 is "dest".
1131# If the spec is "src", then $1 is "src".
1132# $2: path to blob file. Can be used for fixups.
1133#
1134function blob_fixup() {
1135 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001136}
1137
Luca Stefani3a030122016-07-30 12:08:25 +02001138#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001139# extract:
1140#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001141# Positional parameters:
1142# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001143# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001144# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1145#
1146# Non-positional parameters (coming after $2):
1147# --section: preferred way of selecting the portion to parse and extract from
1148# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001149# --kang: if present, this option will activate the printing of hashes for the
1150# extracted blobs. Useful with --section for subsequent pinning of
1151# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001152#
1153function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001154 # Consume positional parameters
1155 local PROPRIETARY_FILES_TXT="$1"; shift
1156 local SRC="$1"; shift
1157 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001158 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001159
1160 # Consume optional, non-positional parameters
1161 while [ "$#" -gt 0 ]; do
1162 case "$1" in
1163 -s|--section)
1164 SECTION="$2"; shift
1165 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001166 -k|--kang)
1167 KANG=true
1168 DISABLE_PINNING=1
1169 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001170 *)
1171 # Backwards-compatibility with the old behavior, where $3, if
1172 # present, denoted an optional positional ${SECTION} argument.
1173 # Users of ${SECTION} are encouraged to migrate from setting it as
1174 # positional $3, to non-positional --section ${SECTION}, the
1175 # reason being that it doesn't scale to have more than 1 optional
1176 # positional argument.
1177 SECTION="$1"
1178 ;;
1179 esac
1180 shift
1181 done
1182
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001183 if [ -z "$OUTDIR" ]; then
1184 echo "Output dir not set!"
1185 exit 1
1186 fi
1187
Vladimir Olteanc5034462019-01-17 03:04:16 +02001188 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001189
1190 # Allow failing, so we can try $DEST and/or $FILE
1191 set +e
1192
1193 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001194 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001195 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001196 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001197 local COUNT=${#FILELIST[@]}
Jackeaglecf6f4de2019-09-24 04:07:22 -04001198 local OUTPUT_ROOT="$BLISS_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001199 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1200
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001201 if [ "$SRC" = "adb" ]; then
1202 init_adb_connection
1203 fi
1204
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001205 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001206 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001207
1208 # Check if we're working with the same zip that was passed last time.
1209 # If so, let's just use what's already extracted.
1210 MD5=`md5sum "$SRC"| awk '{print $1}'`
1211 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1212
1213 if [ "$MD5" != "$OLDMD5" ]; then
1214 rm -rf "$DUMPDIR"
1215 mkdir "$DUMPDIR"
1216 unzip "$SRC" -d "$DUMPDIR"
1217 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1218
1219 # Stop if an A/B OTA zip is detected. We cannot extract these.
1220 if [ -a "$DUMPDIR"/payload.bin ]; then
1221 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1222 exit 1
1223 # If OTA is block based, extract it.
1224 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1225 echo "Converting system.new.dat to system.img"
Jackeaglecf6f4de2019-09-24 04:07:22 -04001226 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 -05001227 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1228 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1229 echo "Requesting sudo access to mount the system.img"
1230 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1231 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1232 sudo umount "$DUMPDIR"/tmp
1233 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1234 fi
1235 fi
1236
1237 SRC="$DUMPDIR"
1238 fi
1239
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001240 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001241 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001242 rm -rf "${OUTPUT_TMP:?}"
1243 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001244 if [ -d "$OUTPUT_ROOT" ]; then
1245 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1246 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001247 VENDOR_STATE=1
1248 fi
1249
Vladimir Olteanc5034462019-01-17 03:04:16 +02001250 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001251
1252 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001253
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001254 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001255 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001256 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001257 local OUTPUT_DIR=
1258 local TMP_DIR=
1259 local SRC_FILE=
1260 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001261 local IS_PRODUCT_PACKAGE=false
1262
1263 # Note: this relies on the fact that the ${FILELIST[@]} array
1264 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1265 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1266 IS_PRODUCT_PACKAGE=true
1267 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001268
Vladimir Olteand652a062018-06-24 20:42:01 +03001269 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001270 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1271 TMP_DIR="${OUTPUT_TMP}/rootfs"
1272 SRC_FILE="/${SPEC_SRC_FILE}"
1273 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001274 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001275 OUTPUT_DIR="${OUTPUT_ROOT}"
1276 TMP_DIR="${OUTPUT_TMP}"
1277 SRC_FILE="/system/${SPEC_SRC_FILE}"
1278 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001279 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001280
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001281 # Strip the file path in the vendor repo of "system", if present
1282 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001283 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001284 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001285
Gabriele Me6df25b2017-10-11 00:58:59 +02001286 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001287 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001288 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001289 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001290 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001291 if [ -f "${VENDOR_REPO_FILE}" ]; then
1292 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001293 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001294 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001295 fi
1296 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001297 local TMP_HASH=$(get_hash "${PINNED}")
1298 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001299 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001300 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1301 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001302 fi
1303 fi
1304 fi
1305 fi
1306
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001307 if [ "${KANG}" = false ]; then
1308 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1309 fi
1310
Gabriele Me6df25b2017-10-11 00:58:59 +02001311 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001312 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001313 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001314 FOUND=false
1315 # Try Lineage target first.
1316 # Also try to search for files stripped of
1317 # the "/system" prefix, if we're actually extracting
1318 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001319 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001320 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1321 FOUND=true
1322 break
1323 }
1324 done
1325
1326 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001327 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001328 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001329 fi
1330 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001331
Vladimir Oltean4818c232019-01-17 03:07:34 +02001332 # Blob fixup pipeline has 2 parts: one that is fixed and
1333 # one that is user-configurable
1334 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1335 # Deodex apk|jar if that's the case
1336 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1337 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1338 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001339 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1340 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001341 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001342 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001343 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1344 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001345 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001346 # Now run user-supplied fixup function
1347 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1348 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001349
Vladimir Olteand6747712018-06-24 20:46:42 +03001350 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001351 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001352 local TYPE="${DIR##*/}"
1353 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001354 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001355 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001356 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001357 fi
1358 fi
1359
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001360 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001361 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1362 fi
1363
1364 # Check and print whether the fixup pipeline actually did anything.
1365 # This isn't done right after the fixup pipeline because we want this print
1366 # to come after print_spec above, when in kang mode.
1367 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1368 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1369 # Now sanity-check the spec for this blob.
1370 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1371 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1372 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1373 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001374 fi
1375
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001376 done
1377
1378 # Don't allow failing
1379 set -e
1380}
Louis Popia516c2f2016-07-25 15:51:13 +02001381
1382#
1383# extract_firmware:
1384#
1385# $1: file containing the list of items to extract
1386# $2: path to extracted radio folder
1387#
1388function extract_firmware() {
1389 if [ -z "$OUTDIR" ]; then
1390 echo "Output dir not set!"
1391 exit 1
1392 fi
1393
1394 parse_file_list "$1"
1395
1396 # Don't allow failing
1397 set -e
1398
1399 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1400 local COUNT=${#FILELIST[@]}
1401 local SRC="$2"
Jackeaglecf6f4de2019-09-24 04:07:22 -04001402 local OUTPUT_DIR="$BLISS_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001403
1404 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1405 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1406 rm -rf "${OUTPUT_DIR:?}/"*
1407 VENDOR_RADIO_STATE=1
1408 fi
1409
1410 echo "Extracting $COUNT files in $1 from $SRC:"
1411
1412 for (( i=1; i<COUNT+1; i++ )); do
1413 local FILE="${FILELIST[$i-1]}"
1414 printf ' - %s \n' "/radio/$FILE"
1415
1416 if [ ! -d "$OUTPUT_DIR" ]; then
1417 mkdir -p "$OUTPUT_DIR"
1418 fi
1419 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1420 chmod 644 "$OUTPUT_DIR/$FILE"
1421 done
1422}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001423
1424function extract_img_data() {
1425 local image_file="$1"
1426 local out_dir="$2"
1427 local logFile="$TMPDIR/debugfs.log"
1428
1429 if [ ! -d "$out_dir" ]; then
1430 mkdir -p "$out_dir"
1431 fi
1432
1433 if [[ "$HOST_OS" == "Darwin" ]]; then
1434 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1435 echo "[-] Failed to extract data from '$image_file'"
1436 abort 1
1437 }
1438 else
1439 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1440 do
1441 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1442 echo "[-] Failed to extract data from '$image_file'"
1443 abort 1
1444 }
1445 done
1446 fi
1447
1448 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1449 if grep -Fq "$symlink_err" "$logFile"; then
1450 echo "[-] Symlinks have not been properly processed from $image_file"
1451 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1452 abort 1
1453 fi
1454}
1455
1456declare -ra VENDOR_SKIP_FILES=(
1457 "bin/toybox_vendor"
1458 "bin/toolbox"
1459 "bin/grep"
1460 "build.prop"
1461 "compatibility_matrix.xml"
1462 "default.prop"
1463 "etc/NOTICE.xml.gz"
1464 "etc/vintf/compatibility_matrix.xml"
1465 "etc/vintf/manifest.xml"
1466 "etc/wifi/wpa_supplicant.conf"
1467 "manifest.xml"
1468 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1469 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1470 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1471 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1472 "overlay/framework-res__auto_generated_rro.apk"
1473 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1474)
1475
1476function array_contains() {
1477 local element
1478 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1479 return 1
1480}
1481
1482function generate_prop_list_from_image() {
1483 local image_file="$1"
1484 local image_dir="$TMPDIR/image-temp"
1485 local output_list="$2"
1486 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1487 local -n skipped_vendor_files="$3"
1488
1489 extract_img_data "$image_file" "$image_dir"
1490
1491 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1492 do
1493 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1494 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1495 continue
1496 fi
1497 # Skip device defined skipped files since they will be re-generated at build time
1498 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1499 continue
1500 fi
1501 if suffix_match_file ".apk" "$FILE" ; then
1502 echo "-vendor/$FILE" >> "$output_list_tmp"
1503 else
1504 echo "vendor/$FILE" >> "$output_list_tmp"
1505 fi
1506 done
1507
1508 # Sort merged file with all lists
1509 sort -u "$output_list_tmp" > "$output_list"
1510
1511 # Clean-up
1512 rm -f "$output_list_tmp"
1513}