blob: c9550ac9a46c93ed6f82d7dcc02ce5aa814f00c6 [file] [log] [blame]
PIPIPIG23366617d7dcb2019-09-29 12:47:28 -04001#!/bin/bash
Steve Kondik4e2aaab2016-07-15 10:39:58 -07002#
3# Copyright (C) 2016 The CyanogenMod Project
Bruno Martins8194b8e2019-09-23 11:51:33 +01004# Copyright (C) 2017-2019 The LineageOS Project
Steve Kondik4e2aaab2016-07-15 10:39:58 -07005#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -070020PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +020021PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070022PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -070023PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +020024PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070025PACKAGE_LIST=()
26VENDOR_STATE=-1
Louis Popia516c2f2016-07-25 15:51:13 +020027VENDOR_RADIO_STATE=-1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070028COMMON=-1
Luca Stefani7f9fff22016-07-18 13:47:55 +020029ARCHES=
30FULLY_DEODEXED=-1
31
Rashed Abdel-Tawab11186d62017-08-05 23:11:35 -040032TMPDIR=$(mktemp -d)
Steve Kondik4e2aaab2016-07-15 10:39:58 -070033
34#
Steve Kondik48f8df82016-08-14 03:55:08 -070035# cleanup
36#
37# kill our tmpfiles with fire on exit
38#
39function cleanup() {
40 rm -rf "${TMPDIR:?}"
41}
42
Gabriele M6c3c2c02017-10-11 12:55:51 +020043trap cleanup 0
Steve Kondik48f8df82016-08-14 03:55:08 -070044
45#
Steve Kondik4e2aaab2016-07-15 10:39:58 -070046# setup_vendor
47#
48# $1: device name
49# $2: vendor name
Luca Stefani5c60e4f2017-08-17 19:28:48 +020050# $3: Lineage root directory
Steve Kondik4e2aaab2016-07-15 10:39:58 -070051# $4: is common device - optional, default to false
52# $5: cleanup - optional, default to true
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040053# $6: custom vendor makefile name - optional, default to false
Steve Kondik4e2aaab2016-07-15 10:39:58 -070054#
55# Must be called before any other functions can be used. This
56# sets up the internal state for a new vendor configuration.
57#
58function setup_vendor() {
59 local DEVICE="$1"
60 if [ -z "$DEVICE" ]; then
61 echo "\$DEVICE must be set before including this script!"
62 exit 1
63 fi
64
65 export VENDOR="$2"
66 if [ -z "$VENDOR" ]; then
67 echo "\$VENDOR must be set before including this script!"
68 exit 1
69 fi
70
Luca Stefani5c60e4f2017-08-17 19:28:48 +020071 export LINEAGE_ROOT="$3"
72 if [ ! -d "$LINEAGE_ROOT" ]; then
73 echo "\$LINEAGE_ROOT must be set and valid before including this script!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070074 exit 1
75 fi
76
77 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
Luca Stefani5c60e4f2017-08-17 19:28:48 +020078 if [ ! -d "$LINEAGE_ROOT/$OUTDIR" ]; then
79 mkdir -p "$LINEAGE_ROOT/$OUTDIR"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070080 fi
81
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040082 VNDNAME="$6"
83 if [ -z "$VNDNAME" ]; then
84 VNDNAME="$DEVICE"
85 fi
86
Luca Stefani5c60e4f2017-08-17 19:28:48 +020087 export PRODUCTMK="$LINEAGE_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
88 export ANDROIDMK="$LINEAGE_ROOT"/"$OUTDIR"/Android.mk
89 export BOARDMK="$LINEAGE_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-Tawab8de76b22019-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-Tawab8de76b22019-09-28 23:37:36 -0400278 if prefix_match_file "vendor/" $TARGET ; then
279 local OUTTARGET=$(truncate_file $TARGET)
280 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
281 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
282 elif prefix_match_file "product/" $TARGET ; then
283 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"
290 elif prefix_match_file "system/" $TARGET ; then
291 local OUTTARGET=$(truncate_file $TARGET)
292 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
293 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400294 else
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400295 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400296 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
297 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700298 done
299 return 0
300}
301
302#
303# write_packages:
304#
305# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400306# $2: /system, /odm, /product, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700307# $3: type-specific extra flags
308# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700309#
310# Internal function which writes out the BUILD_PREBUILT stanzas
311# for all modules in the list. This is called by write_product_packages
312# after the modules are categorized.
313#
314function write_packages() {
315
316 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500317 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700318 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700319
320 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700321 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700322 local FILELIST=("${!ARR_NAME}")
323
324 local FILE=
325 local ARGS=
326 local BASENAME=
327 local EXTENSION=
328 local PKGNAME=
329 local SRC=
330
331 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300332 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700333 ARGS=$(target_args "$P")
334
335 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100336 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700337 EXTENSION=${BASENAME##*.}
338 PKGNAME=${BASENAME%.*}
339
340 # Add to final package list
341 PACKAGE_LIST+=("$PKGNAME")
342
343 SRC="proprietary"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400344 if [ "$PARTITION" = "system" ]; then
345 SRC+="/system"
346 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700347 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500348 elif [ "$PARTITION" = "product" ]; then
349 SRC+="/product"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700350 elif [ "$PARTITION" = "odm" ]; then
351 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700352 fi
353
354 printf 'include $(CLEAR_VARS)\n'
355 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
356 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
357 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700358 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700359 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
360 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
361 #if [ "$VENDOR_PKG" = "true" ]; then
362 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
363 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
364 #else
365 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
366 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
367 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700368 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700369 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
370 else
371 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
372 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700373 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700374 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700375 fi
376 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200377 if [ "$EXTRA" = "priv-app" ]; then
378 SRC="$SRC/priv-app"
379 else
380 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700381 fi
382 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
383 local CERT=platform
384 if [ ! -z "$ARGS" ]; then
385 CERT="$ARGS"
386 fi
387 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
388 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
389 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200390 local CERT=platform
391 if [ ! -z "$ARGS" ]; then
392 CERT="$ARGS"
393 fi
394 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700395 elif [ "$CLASS" = "ETC" ]; then
396 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
397 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700398 if [ "$ARGS" = "rootfs" ]; then
399 SRC="$SRC/rootfs"
400 if [ "$EXTRA" = "sbin" ]; then
401 SRC="$SRC/sbin"
402 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
403 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
404 fi
405 else
406 SRC="$SRC/bin"
407 fi
408 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
409 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700410 else
Steve Kondika991cf12016-07-28 12:13:12 -0700411 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700412 fi
413 printf 'LOCAL_MODULE_TAGS := optional\n'
414 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400415 if [ "$CLASS" = "APPS" ]; then
416 printf 'LOCAL_DEX_PREOPT := false\n'
417 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700418 if [ ! -z "$EXTENSION" ]; then
419 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
420 fi
M1cha15f226c2017-01-04 09:00:11 +0100421 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
422 if [ "$DIRNAME" != "." ]; then
423 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
424 fi
425 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700426 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700427 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
428 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500429 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800430 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500431 elif [ "$PARTITION" = "product" ]; then
432 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700433 elif [ "$PARTITION" = "odm" ]; then
434 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700435 fi
436 printf 'include $(BUILD_PREBUILT)\n\n'
437 done
438}
439
440#
441# write_product_packages:
442#
443# This function will create BUILD_PREBUILT entries in the
444# Android.mk and associated PRODUCT_PACKAGES list in the
445# product makefile for all files in the blob list which
446# start with a single dash (-) character.
447#
448function write_product_packages() {
449 PACKAGE_LIST=()
450
451 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
452
453 if [ "$COUNT" = "0" ]; then
454 return 0
455 fi
456
457 # Figure out what's 32-bit, what's 64-bit, and what's multilib
458 # I really should not be doing this in bash due to shitty array passing :(
459 local T_LIB32=( $(prefix_match "lib/") )
460 local T_LIB64=( $(prefix_match "lib64/") )
461 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
462 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700463 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700464
Steve Kondik03ce4002016-07-29 00:00:16 -0700465 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500466 write_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700467 fi
468 if [ "${#LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500469 write_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700470 fi
471 if [ "${#LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500472 write_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700473 fi
474
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400475 local T_S_LIB32=( $(prefix_match "system/lib/") )
476 local T_S_LIB64=( $(prefix_match "system/lib64/") )
477 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
478 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
479 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
480
481 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
482 write_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDMK"
483 fi
484 if [ "${#S_LIB32[@]}" -gt "0" ]; then
485 write_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDMK"
486 fi
487 if [ "${#S_LIB64[@]}" -gt "0" ]; then
488 write_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDMK"
489 fi
490
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700491 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
492 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
493 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
494 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700495 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700496
497 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500498 write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700499 fi
500 if [ "${#V_LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500501 write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700502 fi
503 if [ "${#V_LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500504 write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
505 fi
506
507 local T_P_LIB32=( $(prefix_match "product/lib/") )
508 local T_P_LIB64=( $(prefix_match "product/lib64/") )
509 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
510 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
511 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
512
513 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
514 write_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDMK"
515 fi
516 if [ "${#P_LIB32[@]}" -gt "0" ]; then
517 write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
518 fi
519 if [ "${#P_LIB64[@]}" -gt "0" ]; then
520 write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700521 fi
522
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700523 local T_O_LIB32=( $(prefix_match "odm/lib/") )
524 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
525 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
526 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
527 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
528
529 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
530 write_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDMK"
531 fi
532 if [ "${#O_LIB32[@]}" -gt "0" ]; then
533 write_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDMK"
534 fi
535 if [ "${#O_LIB64[@]}" -gt "0" ]; then
536 write_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDMK"
537 fi
538
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700539 # Apps
540 local APPS=( $(prefix_match "app/") )
541 if [ "${#APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500542 write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700543 fi
544 local PRIV_APPS=( $(prefix_match "priv-app/") )
545 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500546 write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700547 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400548 local S_APPS=( $(prefix_match "system/app/") )
549 if [ "${#S_APPS[@]}" -gt "0" ]; then
550 write_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDMK"
551 fi
552 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
553 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
554 write_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDMK"
555 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700556 local V_APPS=( $(prefix_match "vendor/app/") )
557 if [ "${#V_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500558 write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700559 fi
560 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
561 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500562 write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
563 fi
564 local P_APPS=( $(prefix_match "product/app/") )
565 if [ "${#P_APPS[@]}" -gt "0" ]; then
566 write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
567 fi
568 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
569 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
570 write_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700571 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700572 local O_APPS=( $(prefix_match "odm/app/") )
573 if [ "${#O_APPS[@]}" -gt "0" ]; then
574 write_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
575 fi
576 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
577 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
578 write_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
579 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700580
581 # Framework
582 local FRAMEWORK=( $(prefix_match "framework/") )
583 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500584 write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700585 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400586 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
587 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
588 write_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDMK"
589 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200590 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200591 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500592 write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
593 fi
594 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
595 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
596 write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
Christian Oderc16f3272017-10-08 23:15:52 +0200597 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700598 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
599 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
600 write_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
601 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700602
603 # Etc
604 local ETC=( $(prefix_match "etc/") )
605 if [ "${#ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500606 write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700607 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400608 local S_ETC=( $(prefix_match "system/etc/") )
609 if [ "${#ETC[@]}" -gt "0" ]; then
610 write_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDMK"
611 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700612 local V_ETC=( $(prefix_match "vendor/etc/") )
613 if [ "${#V_ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500614 write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
615 fi
616 local P_ETC=( $(prefix_match "product/etc/") )
617 if [ "${#P_ETC[@]}" -gt "0" ]; then
618 write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700619 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700620 local O_ETC=( $(prefix_match "odm/etc/") )
621 if [ "${#O_ETC[@]}" -gt "0" ]; then
622 write_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDMK"
623 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700624
625 # Executables
626 local BIN=( $(prefix_match "bin/") )
627 if [ "${#BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500628 write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700629 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400630 local S_BIN=( $(prefix_match "system/bin/") )
631 if [ "${#BIN[@]}" -gt "0" ]; then
632 write_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDMK"
633 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700634 local V_BIN=( $(prefix_match "vendor/bin/") )
635 if [ "${#V_BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500636 write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
637 fi
638 local P_BIN=( $(prefix_match "product/bin/") )
639 if [ "${#P_BIN[@]}" -gt "0" ]; then
640 write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700641 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700642 local O_BIN=( $(prefix_match "odm/bin/") )
643 if [ "${#O_BIN[@]}" -gt "0" ]; then
644 write_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDMK"
645 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700646 local SBIN=( $(prefix_match "sbin/") )
647 if [ "${#SBIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500648 write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700649 fi
650
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700651
652 # Actually write out the final PRODUCT_PACKAGES list
653 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
654
655 if [ "$PACKAGE_COUNT" -eq "0" ]; then
656 return 0
657 fi
658
659 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
660 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
661 local LINEEND=" \\"
662 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
663 LINEEND=""
664 fi
665 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
666 done
667}
668
669#
670# write_header:
671#
672# $1: file which will be written to
673#
674# writes out the copyright header with the current year.
675# note that this is not an append operation, and should
676# be executed first!
677#
678function write_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600679 if [ -f $1 ]; then
680 rm $1
681 fi
682
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700683 YEAR=$(date +"%Y")
684
685 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
686
Matt Mower8945f5e2017-01-07 14:08:17 -0600687 NUM_REGEX='^[0-9]+$'
688 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
689 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
690 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
691 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
692 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
693 fi
694 if [ $YEAR -eq 2017 ]; then
695 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
696 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
697 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
698 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
699 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
700 else
701 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
702 fi
703 else
704 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
705 fi
706
707 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700708#
709# Licensed under the Apache License, Version 2.0 (the "License");
710# you may not use this file except in compliance with the License.
711# You may obtain a copy of the License at
712#
713# http://www.apache.org/licenses/LICENSE-2.0
714#
715# Unless required by applicable law or agreed to in writing, software
716# distributed under the License is distributed on an "AS IS" BASIS,
717# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
718# See the License for the specific language governing permissions and
719# limitations under the License.
720
721# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
722
723EOF
724}
725
726#
727# write_headers:
728#
729# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400730# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700731#
732# Calls write_header for each of the makefiles and creates
733# the initial path declaration and device guard for the
734# Android.mk
735#
736function write_headers() {
737 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400738
739 GUARD="$2"
740 if [ -z "$GUARD" ]; then
741 GUARD="TARGET_DEVICE"
742 fi
743
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700744 cat << EOF >> "$ANDROIDMK"
745LOCAL_PATH := \$(call my-dir)
746
747EOF
748 if [ "$COMMON" -ne 1 ]; then
749 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400750ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700751
752EOF
753 else
754 if [ -z "$1" ]; then
755 echo "Argument with devices to be added to guard must be set!"
756 exit 1
757 fi
758 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400759ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700760
761EOF
762 fi
763
764 write_header "$BOARDMK"
765 write_header "$PRODUCTMK"
766}
767
768#
769# write_footers:
770#
771# Closes the inital guard and any other finalization tasks. Must
772# be called as the final step.
773#
774function write_footers() {
775 cat << EOF >> "$ANDROIDMK"
776endif
777EOF
778}
779
780# Return success if adb is up and not in recovery
781function _adb_connected {
782 {
Steve Kondik7561d192016-09-01 21:40:27 -0700783 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700784 then
785 return 0
786 fi
787 } 2>/dev/null
788
789 return 1
790};
791
792#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100793# parse_file_list:
794#
795# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400796# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +0100797#
798# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700799#
800function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100801 if [ -z "$1" ]; then
802 echo "An input file is expected!"
803 exit 1
804 elif [ ! -f "$1" ]; then
805 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700806 exit 1
807 fi
808
Vladimir Olteanc5034462019-01-17 03:04:16 +0200809 if [ -n "$2" ]; then
810 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400811 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +0200812 # Match all lines starting with first line found to start* with '#'
813 # comment and contain** $2, and ending with first line to be empty*.
814 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
815 # **the $2 match is case-insensitive
816 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400817 else
818 LIST=$1
819 fi
820
821
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700822 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700823 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200824 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700825 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700826 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200827 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700828
829 while read -r line; do
830 if [ -z "$line" ]; then continue; fi
831
Steve Kondik48f8df82016-08-14 03:55:08 -0700832 # If the line has a pipe delimiter, a sha1 hash should follow.
833 # This indicates the file should be pinned and not overwritten
834 # when extracting files.
835 local SPLIT=(${line//\|/ })
836 local COUNT=${#SPLIT[@]}
837 local SPEC=${SPLIT[0]}
838 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +0200839 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -0700840 if [ "$COUNT" -gt "1" ]; then
841 HASH=${SPLIT[1]}
842 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +0200843 if [ "$COUNT" -gt "2" ]; then
844 FIXUP_HASH=${SPLIT[2]}
845 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700846
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700847 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700848 if [[ "$SPEC" =~ ^- ]]; then
849 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
850 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200851 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700852 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700853 PRODUCT_COPY_FILES_LIST+=("$SPEC")
854 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200855 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700856 fi
857
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400858 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700859}
860
861#
862# write_makefiles:
863#
864# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400865# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700866#
867# Calls write_product_copy_files and write_product_packages on
868# the given file and appends to the Android.mk as well as
869# the product makefile.
870#
871function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700872 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400873 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700874 write_product_packages
875}
876
877#
Louis Popia516c2f2016-07-25 15:51:13 +0200878# append_firmware_calls_to_makefiles:
879#
880# Appends to Android.mk the calls to all images present in radio folder
881# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
882#
883function append_firmware_calls_to_makefiles() {
884 cat << EOF >> "$ANDROIDMK"
885ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
886
887RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
888\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
889 \$(call add-radio-file,radio/\$(f)))
890\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
891
892endif
893
894EOF
895}
896
897#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200898# get_file:
899#
900# $1: input file
901# $2: target file/folder
902# $3: source of the file (can be "adb" or a local folder)
903#
904# Silently extracts the input file to defined target
905# Returns success if file can be pulled from the device or found locally
906#
907function get_file() {
908 local SRC="$3"
909
910 if [ "$SRC" = "adb" ]; then
911 # try to pull
912 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
913
914 return 1
915 else
916 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +0300917 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
918 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +0200919 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200920
921 return 1
922 fi
923};
924
925#
926# oat2dex:
927#
928# $1: extracted apk|jar (to check if deodex is required)
929# $2: odexed apk|jar to deodex
930# $3: source of the odexed apk|jar
931#
932# Convert apk|jar .odex in the corresposing classes.dex
933#
934function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200935 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200936 local OEM_TARGET="$2"
937 local SRC="$3"
938 local TARGET=
939 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -0500940 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200941
942 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100943 export BAKSMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/baksmali.jar
944 export SMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200945 fi
946
Joe Maples9be579f2018-01-05 14:51:33 -0500947 if [ -z "$VDEXEXTRACTOR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100948 export VDEXEXTRACTOR="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -0500949 fi
950
codeworkx1c29bf62018-09-23 12:36:57 +0200951 if [ -z "$CDEXCONVERTER" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100952 export CDEXCONVERTER="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +0200953 fi
954
Luca Stefani7f9fff22016-07-18 13:47:55 +0200955 # Extract existing boot.oats to the temp folder
956 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700957 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200958 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700959 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +0300960 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200961 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700962 else
963 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200964 fi
965 done
966 fi
967
968 if [ -z "$ARCHES" ]; then
969 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
970 fi
971
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200972 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700973 return;
974 fi
975
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200976 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200977 return 0 # target apk|jar is already odexed, return
978 fi
979
980 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700981 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200982
983 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400984 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200985
986 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400987 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -0500988 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -0700989 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
990 for CLASS in $CLASSES; do
991 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
992 # Check if we have to deal with CompactDex
993 if [[ "$CLASS" == *.cdex ]]; then
994 "$CDEXCONVERTER" "$CLASS" &>/dev/null
995 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
996 else
997 mv "$CLASS" "$TMPDIR/$NEWCLASS"
998 fi
999 done
Joe Maples9be579f2018-01-05 14:51:33 -05001000 else
1001 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1002 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001003 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001004 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001005 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001006 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001007 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001008 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001009 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001010 # try to extract classes.dex from boot.vdex for frameworks jars
1011 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001012 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001013 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001014 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1015 for CLASS in $CLASSES; do
1016 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1017 # Check if we have to deal with CompactDex
1018 if [[ "$CLASS" == *.cdex ]]; then
1019 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1020 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1021 else
1022 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1023 fi
1024 done
Joe Maples9be579f2018-01-05 14:51:33 -05001025 else
1026 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1027 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1028 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001029 else
1030 continue
1031 fi
1032
Luca Stefani7f9fff22016-07-18 13:47:55 +02001033 done
1034
1035 rm -rf "$TMPDIR/dexout"
1036}
1037
1038#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001039# init_adb_connection:
1040#
1041# Starts adb server and waits for the device
1042#
1043function init_adb_connection() {
1044 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1045 if ! _adb_connected; then
1046 echo "No device is online. Waiting for one..."
1047 echo "Please connect USB and/or enable USB debugging"
1048 until _adb_connected; do
1049 sleep 1
1050 done
1051 echo "Device Found."
1052 fi
1053
1054 # Retrieve IP and PORT info if we're using a TCP connection
1055 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1056 | head -1 | awk '{print $1}')
1057 adb root &> /dev/null
1058 sleep 0.3
1059 if [ -n "$TCPIPPORT" ]; then
1060 # adb root just killed our connection
1061 # so reconnect...
1062 adb connect "$TCPIPPORT"
1063 fi
1064 adb wait-for-device &> /dev/null
1065 sleep 0.3
1066}
1067
1068#
Luca Stefani3a030122016-07-30 12:08:25 +02001069# fix_xml:
1070#
1071# $1: xml file to fix
1072#
1073function fix_xml() {
1074 local XML="$1"
1075 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1076
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001077 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1078 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001079
1080 mv "$TEMP_XML" "$XML"
1081}
1082
Vladimir Oltean4818c232019-01-17 03:07:34 +02001083function get_hash() {
1084 local FILE="$1"
1085
1086 if [ "$(uname)" == "Darwin" ]; then
1087 shasum "${FILE}" | awk '{print $1}'
1088 else
1089 sha1sum "${FILE}" | awk '{print $1}'
1090 fi
1091}
1092
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001093function print_spec() {
1094 local SPEC_PRODUCT_PACKAGE="$1"
1095 local SPEC_SRC_FILE="$2"
1096 local SPEC_DST_FILE="$3"
1097 local SPEC_ARGS="$4"
1098 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001099 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001100
1101 local PRODUCT_PACKAGE=""
1102 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1103 PRODUCT_PACKAGE="-"
1104 fi
1105 local SRC=""
1106 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1107 SRC="${SPEC_SRC_FILE}:"
1108 fi
1109 local DST=""
1110 if [ ! -z "${SPEC_DST_FILE}" ]; then
1111 DST="${SPEC_DST_FILE}"
1112 fi
1113 local ARGS=""
1114 if [ ! -z "${SPEC_ARGS}" ]; then
1115 ARGS=";${SPEC_ARGS}"
1116 fi
1117 local HASH=""
1118 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1119 HASH="|${SPEC_HASH}"
1120 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001121 local FIXUP_HASH=""
1122 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1123 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1124 fi
1125 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1126}
1127
1128# To be overridden by device-level extract-files.sh
1129# Parameters:
1130# $1: spec name of a blob. Can be used for filtering.
1131# If the spec is "src:dest", then $1 is "dest".
1132# If the spec is "src", then $1 is "src".
1133# $2: path to blob file. Can be used for fixups.
1134#
1135function blob_fixup() {
1136 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001137}
1138
Luca Stefani3a030122016-07-30 12:08:25 +02001139#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001140# extract:
1141#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001142# Positional parameters:
1143# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001144# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001145# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1146#
1147# Non-positional parameters (coming after $2):
1148# --section: preferred way of selecting the portion to parse and extract from
1149# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001150# --kang: if present, this option will activate the printing of hashes for the
1151# extracted blobs. Useful with --section for subsequent pinning of
1152# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001153#
1154function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001155 # Consume positional parameters
1156 local PROPRIETARY_FILES_TXT="$1"; shift
1157 local SRC="$1"; shift
1158 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001159 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001160
1161 # Consume optional, non-positional parameters
1162 while [ "$#" -gt 0 ]; do
1163 case "$1" in
1164 -s|--section)
1165 SECTION="$2"; shift
1166 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001167 -k|--kang)
1168 KANG=true
1169 DISABLE_PINNING=1
1170 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001171 *)
1172 # Backwards-compatibility with the old behavior, where $3, if
1173 # present, denoted an optional positional ${SECTION} argument.
1174 # Users of ${SECTION} are encouraged to migrate from setting it as
1175 # positional $3, to non-positional --section ${SECTION}, the
1176 # reason being that it doesn't scale to have more than 1 optional
1177 # positional argument.
1178 SECTION="$1"
1179 ;;
1180 esac
1181 shift
1182 done
1183
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001184 if [ -z "$OUTDIR" ]; then
1185 echo "Output dir not set!"
1186 exit 1
1187 fi
1188
Vladimir Olteanc5034462019-01-17 03:04:16 +02001189 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001190
1191 # Allow failing, so we can try $DEST and/or $FILE
1192 set +e
1193
1194 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001195 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001196 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001197 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001198 local COUNT=${#FILELIST[@]}
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001199 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001200 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1201
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001202 if [ "$SRC" = "adb" ]; then
1203 init_adb_connection
1204 fi
1205
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001206 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001207 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001208
1209 # Check if we're working with the same zip that was passed last time.
1210 # If so, let's just use what's already extracted.
1211 MD5=`md5sum "$SRC"| awk '{print $1}'`
1212 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1213
1214 if [ "$MD5" != "$OLDMD5" ]; then
1215 rm -rf "$DUMPDIR"
1216 mkdir "$DUMPDIR"
1217 unzip "$SRC" -d "$DUMPDIR"
1218 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1219
1220 # Stop if an A/B OTA zip is detected. We cannot extract these.
1221 if [ -a "$DUMPDIR"/payload.bin ]; then
1222 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1223 exit 1
1224 # If OTA is block based, extract it.
1225 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1226 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001227 python "$LINEAGE_ROOT"/vendor/lineage/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001228 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1229 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1230 echo "Requesting sudo access to mount the system.img"
1231 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1232 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1233 sudo umount "$DUMPDIR"/tmp
1234 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1235 fi
1236 fi
1237
1238 SRC="$DUMPDIR"
1239 fi
1240
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001241 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001242 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001243 rm -rf "${OUTPUT_TMP:?}"
1244 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001245 if [ -d "$OUTPUT_ROOT" ]; then
1246 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1247 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001248 VENDOR_STATE=1
1249 fi
1250
Vladimir Olteanc5034462019-01-17 03:04:16 +02001251 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001252
1253 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001254
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001255 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001256 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001257 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001258 local OUTPUT_DIR=
1259 local TMP_DIR=
1260 local SRC_FILE=
1261 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001262 local IS_PRODUCT_PACKAGE=false
1263
1264 # Note: this relies on the fact that the ${FILELIST[@]} array
1265 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1266 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1267 IS_PRODUCT_PACKAGE=true
1268 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001269
Vladimir Olteand652a062018-06-24 20:42:01 +03001270 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001271 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1272 TMP_DIR="${OUTPUT_TMP}/rootfs"
1273 SRC_FILE="/${SPEC_SRC_FILE}"
1274 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001275 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001276 OUTPUT_DIR="${OUTPUT_ROOT}"
1277 TMP_DIR="${OUTPUT_TMP}"
1278 SRC_FILE="/system/${SPEC_SRC_FILE}"
1279 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001280 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001281
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001282 # Strip the file path in the vendor repo of "system", if present
1283 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001284 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001285 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001286
Gabriele Me6df25b2017-10-11 00:58:59 +02001287 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001288 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001289 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001290 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001291 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001292 if [ -f "${VENDOR_REPO_FILE}" ]; then
1293 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001294 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001295 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001296 fi
1297 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001298 local TMP_HASH=$(get_hash "${PINNED}")
1299 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001300 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001301 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1302 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001303 fi
1304 fi
1305 fi
1306 fi
1307
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001308 if [ "${KANG}" = false ]; then
1309 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1310 fi
1311
Gabriele Me6df25b2017-10-11 00:58:59 +02001312 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001313 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001314 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001315 FOUND=false
1316 # Try Lineage target first.
1317 # Also try to search for files stripped of
1318 # the "/system" prefix, if we're actually extracting
1319 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001320 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001321 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1322 FOUND=true
1323 break
1324 }
1325 done
1326
1327 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001328 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001329 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001330 fi
1331 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001332
Vladimir Oltean4818c232019-01-17 03:07:34 +02001333 # Blob fixup pipeline has 2 parts: one that is fixed and
1334 # one that is user-configurable
1335 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1336 # Deodex apk|jar if that's the case
1337 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1338 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1339 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001340 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1341 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001342 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001343 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001344 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1345 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001346 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001347 # Now run user-supplied fixup function
1348 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1349 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001350
Vladimir Olteand6747712018-06-24 20:46:42 +03001351 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001352 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001353 local TYPE="${DIR##*/}"
1354 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001355 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001356 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001357 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001358 fi
1359 fi
1360
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001361 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001362 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1363 fi
1364
1365 # Check and print whether the fixup pipeline actually did anything.
1366 # This isn't done right after the fixup pipeline because we want this print
1367 # to come after print_spec above, when in kang mode.
1368 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1369 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1370 # Now sanity-check the spec for this blob.
1371 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1372 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1373 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1374 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001375 fi
1376
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001377 done
1378
1379 # Don't allow failing
1380 set -e
1381}
Louis Popia516c2f2016-07-25 15:51:13 +02001382
1383#
1384# extract_firmware:
1385#
1386# $1: file containing the list of items to extract
1387# $2: path to extracted radio folder
1388#
1389function extract_firmware() {
1390 if [ -z "$OUTDIR" ]; then
1391 echo "Output dir not set!"
1392 exit 1
1393 fi
1394
1395 parse_file_list "$1"
1396
1397 # Don't allow failing
1398 set -e
1399
1400 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1401 local COUNT=${#FILELIST[@]}
1402 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001403 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001404
1405 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1406 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1407 rm -rf "${OUTPUT_DIR:?}/"*
1408 VENDOR_RADIO_STATE=1
1409 fi
1410
1411 echo "Extracting $COUNT files in $1 from $SRC:"
1412
1413 for (( i=1; i<COUNT+1; i++ )); do
1414 local FILE="${FILELIST[$i-1]}"
1415 printf ' - %s \n' "/radio/$FILE"
1416
1417 if [ ! -d "$OUTPUT_DIR" ]; then
1418 mkdir -p "$OUTPUT_DIR"
1419 fi
1420 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1421 chmod 644 "$OUTPUT_DIR/$FILE"
1422 done
1423}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001424
1425function extract_img_data() {
1426 local image_file="$1"
1427 local out_dir="$2"
1428 local logFile="$TMPDIR/debugfs.log"
1429
1430 if [ ! -d "$out_dir" ]; then
1431 mkdir -p "$out_dir"
1432 fi
1433
1434 if [[ "$HOST_OS" == "Darwin" ]]; then
1435 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1436 echo "[-] Failed to extract data from '$image_file'"
1437 abort 1
1438 }
1439 else
1440 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1441 do
1442 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1443 echo "[-] Failed to extract data from '$image_file'"
1444 abort 1
1445 }
1446 done
1447 fi
1448
1449 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1450 if grep -Fq "$symlink_err" "$logFile"; then
1451 echo "[-] Symlinks have not been properly processed from $image_file"
1452 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1453 abort 1
1454 fi
1455}
1456
1457declare -ra VENDOR_SKIP_FILES=(
1458 "bin/toybox_vendor"
1459 "bin/toolbox"
1460 "bin/grep"
1461 "build.prop"
1462 "compatibility_matrix.xml"
1463 "default.prop"
1464 "etc/NOTICE.xml.gz"
1465 "etc/vintf/compatibility_matrix.xml"
1466 "etc/vintf/manifest.xml"
1467 "etc/wifi/wpa_supplicant.conf"
1468 "manifest.xml"
1469 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1470 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1471 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1472 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1473 "overlay/framework-res__auto_generated_rro.apk"
1474 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1475)
1476
1477function array_contains() {
1478 local element
1479 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1480 return 1
1481}
1482
1483function generate_prop_list_from_image() {
1484 local image_file="$1"
1485 local image_dir="$TMPDIR/image-temp"
1486 local output_list="$2"
1487 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1488 local -n skipped_vendor_files="$3"
1489
1490 extract_img_data "$image_file" "$image_dir"
1491
1492 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1493 do
1494 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1495 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1496 continue
1497 fi
1498 # Skip device defined skipped files since they will be re-generated at build time
1499 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1500 continue
1501 fi
1502 if suffix_match_file ".apk" "$FILE" ; then
1503 echo "-vendor/$FILE" >> "$output_list_tmp"
1504 else
1505 echo "vendor/$FILE" >> "$output_list_tmp"
1506 fi
1507 done
1508
1509 # Sort merged file with all lists
1510 sort -u "$output_list_tmp" > "$output_list"
1511
1512 # Clean-up
1513 rm -f "$output_list_tmp"
1514}