blob: a780a66353214fd39e2d6ce9bcc8a7cf67a7f601 [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-Tawab0ca76432017-10-07 14:18:39 -0400253# $1: make treble compatible makefile - optional, default to false
254#
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-Tawab0ca76432017-10-07 14:18:39 -0400278 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
279 if prefix_match_file "vendor/" $TARGET ; then
280 local OUTTARGET=$(truncate_file $TARGET)
281 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
282 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
razorlovesb5c2c962019-07-29 02:21:34 -0500283 elif prefix_match_file "product/" $TARGET ; then
284 local OUTTARGET=$(truncate_file $TARGET)
285 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
286 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700287 elif prefix_match_file "odm/" $TARGET ; then
288 local OUTTARGET=$(truncate_file $TARGET)
289 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
290 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400291 else
292 printf ' %s/proprietary/%s:system/%s%s\n' \
293 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
294 fi
295 else
296 printf ' %s/proprietary/%s:system/%s%s\n' \
297 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
298 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700299 done
300 return 0
301}
302
303#
304# write_packages:
305#
306# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700307# $2: /odm, /product, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700308# $3: type-specific extra flags
309# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700310#
311# Internal function which writes out the BUILD_PREBUILT stanzas
312# for all modules in the list. This is called by write_product_packages
313# after the modules are categorized.
314#
315function write_packages() {
316
317 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500318 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700319 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700320
321 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700322 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700323 local FILELIST=("${!ARR_NAME}")
324
325 local FILE=
326 local ARGS=
327 local BASENAME=
328 local EXTENSION=
329 local PKGNAME=
330 local SRC=
331
332 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300333 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700334 ARGS=$(target_args "$P")
335
336 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100337 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700338 EXTENSION=${BASENAME##*.}
339 PKGNAME=${BASENAME%.*}
340
341 # Add to final package list
342 PACKAGE_LIST+=("$PKGNAME")
343
344 SRC="proprietary"
razorlovesb5c2c962019-07-29 02:21:34 -0500345 if [ "$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-Tawab08e3a272019-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-Tawab08e3a272019-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
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700474 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
475 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
476 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
477 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700478 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700479
480 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500481 write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700482 fi
483 if [ "${#V_LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500484 write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700485 fi
486 if [ "${#V_LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500487 write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
488 fi
489
490 local T_P_LIB32=( $(prefix_match "product/lib/") )
491 local T_P_LIB64=( $(prefix_match "product/lib64/") )
492 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
493 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
494 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
495
496 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
497 write_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDMK"
498 fi
499 if [ "${#P_LIB32[@]}" -gt "0" ]; then
500 write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
501 fi
502 if [ "${#P_LIB64[@]}" -gt "0" ]; then
503 write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700504 fi
505
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700506 local T_O_LIB32=( $(prefix_match "odm/lib/") )
507 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
508 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
509 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
510 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
511
512 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
513 write_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDMK"
514 fi
515 if [ "${#O_LIB32[@]}" -gt "0" ]; then
516 write_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDMK"
517 fi
518 if [ "${#O_LIB64[@]}" -gt "0" ]; then
519 write_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDMK"
520 fi
521
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700522 # Apps
523 local APPS=( $(prefix_match "app/") )
524 if [ "${#APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500525 write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700526 fi
527 local PRIV_APPS=( $(prefix_match "priv-app/") )
528 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500529 write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700530 fi
531 local V_APPS=( $(prefix_match "vendor/app/") )
532 if [ "${#V_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500533 write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700534 fi
535 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
536 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500537 write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
538 fi
539 local P_APPS=( $(prefix_match "product/app/") )
540 if [ "${#P_APPS[@]}" -gt "0" ]; then
541 write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
542 fi
543 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
544 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
545 write_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700546 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700547 local O_APPS=( $(prefix_match "odm/app/") )
548 if [ "${#O_APPS[@]}" -gt "0" ]; then
549 write_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
550 fi
551 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
552 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
553 write_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
554 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700555
556 # Framework
557 local FRAMEWORK=( $(prefix_match "framework/") )
558 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500559 write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700560 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200561 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200562 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500563 write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
564 fi
565 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
566 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
567 write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
Christian Oderc16f3272017-10-08 23:15:52 +0200568 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700569 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
570 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
571 write_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
572 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700573
574 # Etc
575 local ETC=( $(prefix_match "etc/") )
576 if [ "${#ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500577 write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700578 fi
579 local V_ETC=( $(prefix_match "vendor/etc/") )
580 if [ "${#V_ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500581 write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
582 fi
583 local P_ETC=( $(prefix_match "product/etc/") )
584 if [ "${#P_ETC[@]}" -gt "0" ]; then
585 write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700586 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700587 local O_ETC=( $(prefix_match "odm/etc/") )
588 if [ "${#O_ETC[@]}" -gt "0" ]; then
589 write_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDMK"
590 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700591
592 # Executables
593 local BIN=( $(prefix_match "bin/") )
594 if [ "${#BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500595 write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700596 fi
597 local V_BIN=( $(prefix_match "vendor/bin/") )
598 if [ "${#V_BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500599 write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
600 fi
601 local P_BIN=( $(prefix_match "product/bin/") )
602 if [ "${#P_BIN[@]}" -gt "0" ]; then
603 write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700604 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700605 local O_BIN=( $(prefix_match "odm/bin/") )
606 if [ "${#O_BIN[@]}" -gt "0" ]; then
607 write_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDMK"
608 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700609 local SBIN=( $(prefix_match "sbin/") )
610 if [ "${#SBIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500611 write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700612 fi
613
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700614
615 # Actually write out the final PRODUCT_PACKAGES list
616 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
617
618 if [ "$PACKAGE_COUNT" -eq "0" ]; then
619 return 0
620 fi
621
622 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
623 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
624 local LINEEND=" \\"
625 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
626 LINEEND=""
627 fi
628 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
629 done
630}
631
632#
633# write_header:
634#
635# $1: file which will be written to
636#
637# writes out the copyright header with the current year.
638# note that this is not an append operation, and should
639# be executed first!
640#
641function write_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600642 if [ -f $1 ]; then
643 rm $1
644 fi
645
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700646 YEAR=$(date +"%Y")
647
648 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
649
Matt Mower8945f5e2017-01-07 14:08:17 -0600650 NUM_REGEX='^[0-9]+$'
651 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
652 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
653 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
654 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
655 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
656 fi
657 if [ $YEAR -eq 2017 ]; then
658 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
659 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
660 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
661 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
662 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
663 else
664 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
665 fi
666 else
667 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
668 fi
669
670 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700671#
672# Licensed under the Apache License, Version 2.0 (the "License");
673# you may not use this file except in compliance with the License.
674# You may obtain a copy of the License at
675#
676# http://www.apache.org/licenses/LICENSE-2.0
677#
678# Unless required by applicable law or agreed to in writing, software
679# distributed under the License is distributed on an "AS IS" BASIS,
680# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
681# See the License for the specific language governing permissions and
682# limitations under the License.
683
684# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
685
686EOF
687}
688
689#
690# write_headers:
691#
692# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400693# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700694#
695# Calls write_header for each of the makefiles and creates
696# the initial path declaration and device guard for the
697# Android.mk
698#
699function write_headers() {
700 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400701
702 GUARD="$2"
703 if [ -z "$GUARD" ]; then
704 GUARD="TARGET_DEVICE"
705 fi
706
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700707 cat << EOF >> "$ANDROIDMK"
708LOCAL_PATH := \$(call my-dir)
709
710EOF
711 if [ "$COMMON" -ne 1 ]; then
712 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400713ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700714
715EOF
716 else
717 if [ -z "$1" ]; then
718 echo "Argument with devices to be added to guard must be set!"
719 exit 1
720 fi
721 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400722ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700723
724EOF
725 fi
726
727 write_header "$BOARDMK"
728 write_header "$PRODUCTMK"
729}
730
731#
732# write_footers:
733#
734# Closes the inital guard and any other finalization tasks. Must
735# be called as the final step.
736#
737function write_footers() {
738 cat << EOF >> "$ANDROIDMK"
739endif
740EOF
741}
742
743# Return success if adb is up and not in recovery
744function _adb_connected {
745 {
Steve Kondik7561d192016-09-01 21:40:27 -0700746 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700747 then
748 return 0
749 fi
750 } 2>/dev/null
751
752 return 1
753};
754
755#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100756# parse_file_list:
757#
758# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400759# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +0100760#
761# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700762#
763function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100764 if [ -z "$1" ]; then
765 echo "An input file is expected!"
766 exit 1
767 elif [ ! -f "$1" ]; then
768 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700769 exit 1
770 fi
771
Vladimir Olteanc5034462019-01-17 03:04:16 +0200772 if [ -n "$2" ]; then
773 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400774 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +0200775 # Match all lines starting with first line found to start* with '#'
776 # comment and contain** $2, and ending with first line to be empty*.
777 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
778 # **the $2 match is case-insensitive
779 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400780 else
781 LIST=$1
782 fi
783
784
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700785 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700786 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200787 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700788 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700789 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200790 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700791
792 while read -r line; do
793 if [ -z "$line" ]; then continue; fi
794
Steve Kondik48f8df82016-08-14 03:55:08 -0700795 # If the line has a pipe delimiter, a sha1 hash should follow.
796 # This indicates the file should be pinned and not overwritten
797 # when extracting files.
798 local SPLIT=(${line//\|/ })
799 local COUNT=${#SPLIT[@]}
800 local SPEC=${SPLIT[0]}
801 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +0200802 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -0700803 if [ "$COUNT" -gt "1" ]; then
804 HASH=${SPLIT[1]}
805 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +0200806 if [ "$COUNT" -gt "2" ]; then
807 FIXUP_HASH=${SPLIT[2]}
808 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700809
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700810 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700811 if [[ "$SPEC" =~ ^- ]]; then
812 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
813 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200814 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700815 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700816 PRODUCT_COPY_FILES_LIST+=("$SPEC")
817 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200818 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700819 fi
820
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400821 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700822}
823
824#
825# write_makefiles:
826#
827# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400828# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700829#
830# Calls write_product_copy_files and write_product_packages on
831# the given file and appends to the Android.mk as well as
832# the product makefile.
833#
834function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700835 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400836 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700837 write_product_packages
838}
839
840#
Louis Popia516c2f2016-07-25 15:51:13 +0200841# append_firmware_calls_to_makefiles:
842#
843# Appends to Android.mk the calls to all images present in radio folder
844# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
845#
846function append_firmware_calls_to_makefiles() {
847 cat << EOF >> "$ANDROIDMK"
848ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
849
850RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
851\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
852 \$(call add-radio-file,radio/\$(f)))
853\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
854
855endif
856
857EOF
858}
859
860#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200861# get_file:
862#
863# $1: input file
864# $2: target file/folder
865# $3: source of the file (can be "adb" or a local folder)
866#
867# Silently extracts the input file to defined target
868# Returns success if file can be pulled from the device or found locally
869#
870function get_file() {
871 local SRC="$3"
872
873 if [ "$SRC" = "adb" ]; then
874 # try to pull
875 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
876
877 return 1
878 else
879 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +0300880 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
881 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +0200882 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200883
884 return 1
885 fi
886};
887
888#
889# oat2dex:
890#
891# $1: extracted apk|jar (to check if deodex is required)
892# $2: odexed apk|jar to deodex
893# $3: source of the odexed apk|jar
894#
895# Convert apk|jar .odex in the corresposing classes.dex
896#
897function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200898 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200899 local OEM_TARGET="$2"
900 local SRC="$3"
901 local TARGET=
902 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -0500903 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200904
905 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100906 export BAKSMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/baksmali.jar
907 export SMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200908 fi
909
Joe Maples9be579f2018-01-05 14:51:33 -0500910 if [ -z "$VDEXEXTRACTOR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100911 export VDEXEXTRACTOR="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -0500912 fi
913
codeworkx1c29bf62018-09-23 12:36:57 +0200914 if [ -z "$CDEXCONVERTER" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100915 export CDEXCONVERTER="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +0200916 fi
917
Luca Stefani7f9fff22016-07-18 13:47:55 +0200918 # Extract existing boot.oats to the temp folder
919 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700920 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200921 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700922 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +0300923 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200924 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700925 else
926 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200927 fi
928 done
929 fi
930
931 if [ -z "$ARCHES" ]; then
932 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
933 fi
934
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200935 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700936 return;
937 fi
938
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200939 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200940 return 0 # target apk|jar is already odexed, return
941 fi
942
943 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700944 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200945
946 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400947 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200948
949 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400950 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -0500951 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -0700952 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
953 for CLASS in $CLASSES; do
954 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
955 # Check if we have to deal with CompactDex
956 if [[ "$CLASS" == *.cdex ]]; then
957 "$CDEXCONVERTER" "$CLASS" &>/dev/null
958 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
959 else
960 mv "$CLASS" "$TMPDIR/$NEWCLASS"
961 fi
962 done
Joe Maples9be579f2018-01-05 14:51:33 -0500963 else
964 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
965 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400966 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200967 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +0100968 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +0200969 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +0100970 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +0200971 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +0100972 fi
Joe Maples9be579f2018-01-05 14:51:33 -0500973 # try to extract classes.dex from boot.vdex for frameworks jars
974 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +0200975 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +0100976 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -0700977 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
978 for CLASS in $CLASSES; do
979 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
980 # Check if we have to deal with CompactDex
981 if [[ "$CLASS" == *.cdex ]]; then
982 "$CDEXCONVERTER" "$CLASS" &>/dev/null
983 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
984 else
985 mv "$CLASS" "$TMPDIR/$NEWCLASS"
986 fi
987 done
Joe Maples9be579f2018-01-05 14:51:33 -0500988 else
989 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
990 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
991 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +0200992 else
993 continue
994 fi
995
Luca Stefani7f9fff22016-07-18 13:47:55 +0200996 done
997
998 rm -rf "$TMPDIR/dexout"
999}
1000
1001#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001002# init_adb_connection:
1003#
1004# Starts adb server and waits for the device
1005#
1006function init_adb_connection() {
1007 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1008 if ! _adb_connected; then
1009 echo "No device is online. Waiting for one..."
1010 echo "Please connect USB and/or enable USB debugging"
1011 until _adb_connected; do
1012 sleep 1
1013 done
1014 echo "Device Found."
1015 fi
1016
1017 # Retrieve IP and PORT info if we're using a TCP connection
1018 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1019 | head -1 | awk '{print $1}')
1020 adb root &> /dev/null
1021 sleep 0.3
1022 if [ -n "$TCPIPPORT" ]; then
1023 # adb root just killed our connection
1024 # so reconnect...
1025 adb connect "$TCPIPPORT"
1026 fi
1027 adb wait-for-device &> /dev/null
1028 sleep 0.3
1029}
1030
1031#
Luca Stefani3a030122016-07-30 12:08:25 +02001032# fix_xml:
1033#
1034# $1: xml file to fix
1035#
1036function fix_xml() {
1037 local XML="$1"
1038 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1039
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001040 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1041 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001042
1043 mv "$TEMP_XML" "$XML"
1044}
1045
Vladimir Oltean4818c232019-01-17 03:07:34 +02001046function get_hash() {
1047 local FILE="$1"
1048
1049 if [ "$(uname)" == "Darwin" ]; then
1050 shasum "${FILE}" | awk '{print $1}'
1051 else
1052 sha1sum "${FILE}" | awk '{print $1}'
1053 fi
1054}
1055
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001056function print_spec() {
1057 local SPEC_PRODUCT_PACKAGE="$1"
1058 local SPEC_SRC_FILE="$2"
1059 local SPEC_DST_FILE="$3"
1060 local SPEC_ARGS="$4"
1061 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001062 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001063
1064 local PRODUCT_PACKAGE=""
1065 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1066 PRODUCT_PACKAGE="-"
1067 fi
1068 local SRC=""
1069 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1070 SRC="${SPEC_SRC_FILE}:"
1071 fi
1072 local DST=""
1073 if [ ! -z "${SPEC_DST_FILE}" ]; then
1074 DST="${SPEC_DST_FILE}"
1075 fi
1076 local ARGS=""
1077 if [ ! -z "${SPEC_ARGS}" ]; then
1078 ARGS=";${SPEC_ARGS}"
1079 fi
1080 local HASH=""
1081 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1082 HASH="|${SPEC_HASH}"
1083 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001084 local FIXUP_HASH=""
1085 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1086 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1087 fi
1088 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1089}
1090
1091# To be overridden by device-level extract-files.sh
1092# Parameters:
1093# $1: spec name of a blob. Can be used for filtering.
1094# If the spec is "src:dest", then $1 is "dest".
1095# If the spec is "src", then $1 is "src".
1096# $2: path to blob file. Can be used for fixups.
1097#
1098function blob_fixup() {
1099 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001100}
1101
Luca Stefani3a030122016-07-30 12:08:25 +02001102#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001103# extract:
1104#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001105# Positional parameters:
1106# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001107# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001108# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1109#
1110# Non-positional parameters (coming after $2):
1111# --section: preferred way of selecting the portion to parse and extract from
1112# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001113# --kang: if present, this option will activate the printing of hashes for the
1114# extracted blobs. Useful with --section for subsequent pinning of
1115# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001116#
1117function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001118 # Consume positional parameters
1119 local PROPRIETARY_FILES_TXT="$1"; shift
1120 local SRC="$1"; shift
1121 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001122 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001123
1124 # Consume optional, non-positional parameters
1125 while [ "$#" -gt 0 ]; do
1126 case "$1" in
1127 -s|--section)
1128 SECTION="$2"; shift
1129 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001130 -k|--kang)
1131 KANG=true
1132 DISABLE_PINNING=1
1133 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001134 *)
1135 # Backwards-compatibility with the old behavior, where $3, if
1136 # present, denoted an optional positional ${SECTION} argument.
1137 # Users of ${SECTION} are encouraged to migrate from setting it as
1138 # positional $3, to non-positional --section ${SECTION}, the
1139 # reason being that it doesn't scale to have more than 1 optional
1140 # positional argument.
1141 SECTION="$1"
1142 ;;
1143 esac
1144 shift
1145 done
1146
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001147 if [ -z "$OUTDIR" ]; then
1148 echo "Output dir not set!"
1149 exit 1
1150 fi
1151
Vladimir Olteanc5034462019-01-17 03:04:16 +02001152 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001153
1154 # Allow failing, so we can try $DEST and/or $FILE
1155 set +e
1156
1157 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001158 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001159 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001160 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001161 local COUNT=${#FILELIST[@]}
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001162 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001163 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1164
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001165 if [ "$SRC" = "adb" ]; then
1166 init_adb_connection
1167 fi
1168
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001169 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001170 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001171
1172 # Check if we're working with the same zip that was passed last time.
1173 # If so, let's just use what's already extracted.
1174 MD5=`md5sum "$SRC"| awk '{print $1}'`
1175 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1176
1177 if [ "$MD5" != "$OLDMD5" ]; then
1178 rm -rf "$DUMPDIR"
1179 mkdir "$DUMPDIR"
1180 unzip "$SRC" -d "$DUMPDIR"
1181 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1182
1183 # Stop if an A/B OTA zip is detected. We cannot extract these.
1184 if [ -a "$DUMPDIR"/payload.bin ]; then
1185 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1186 exit 1
1187 # If OTA is block based, extract it.
1188 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1189 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001190 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 -05001191 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1192 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1193 echo "Requesting sudo access to mount the system.img"
1194 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1195 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1196 sudo umount "$DUMPDIR"/tmp
1197 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1198 fi
1199 fi
1200
1201 SRC="$DUMPDIR"
1202 fi
1203
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001204 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001205 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001206 rm -rf "${OUTPUT_TMP:?}"
1207 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001208 if [ -d "$OUTPUT_ROOT" ]; then
1209 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1210 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001211 VENDOR_STATE=1
1212 fi
1213
Vladimir Olteanc5034462019-01-17 03:04:16 +02001214 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001215
1216 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001217
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001218 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001219 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001220 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001221 local OUTPUT_DIR=
1222 local TMP_DIR=
1223 local SRC_FILE=
1224 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001225 local IS_PRODUCT_PACKAGE=false
1226
1227 # Note: this relies on the fact that the ${FILELIST[@]} array
1228 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1229 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1230 IS_PRODUCT_PACKAGE=true
1231 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001232
Vladimir Olteand652a062018-06-24 20:42:01 +03001233 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001234 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1235 TMP_DIR="${OUTPUT_TMP}/rootfs"
1236 SRC_FILE="/${SPEC_SRC_FILE}"
1237 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001238 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001239 OUTPUT_DIR="${OUTPUT_ROOT}"
1240 TMP_DIR="${OUTPUT_TMP}"
1241 SRC_FILE="/system/${SPEC_SRC_FILE}"
1242 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001243 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001244
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001245 # Strip the file path in the vendor repo of "system", if present
1246 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001247 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001248 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001249
Gabriele Me6df25b2017-10-11 00:58:59 +02001250 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001251 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001252 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001253 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001254 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001255 if [ -f "${VENDOR_REPO_FILE}" ]; then
1256 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001257 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001258 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001259 fi
1260 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001261 local TMP_HASH=$(get_hash "${PINNED}")
1262 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001263 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001264 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1265 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001266 fi
1267 fi
1268 fi
1269 fi
1270
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001271 if [ "${KANG}" = false ]; then
1272 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1273 fi
1274
Gabriele Me6df25b2017-10-11 00:58:59 +02001275 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001276 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001277 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001278 FOUND=false
1279 # Try Lineage target first.
1280 # Also try to search for files stripped of
1281 # the "/system" prefix, if we're actually extracting
1282 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001283 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001284 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1285 FOUND=true
1286 break
1287 }
1288 done
1289
1290 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001291 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001292 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001293 fi
1294 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001295
Vladimir Oltean4818c232019-01-17 03:07:34 +02001296 # Blob fixup pipeline has 2 parts: one that is fixed and
1297 # one that is user-configurable
1298 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1299 # Deodex apk|jar if that's the case
1300 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1301 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1302 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001303 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1304 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001305 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001306 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001307 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1308 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001309 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001310 # Now run user-supplied fixup function
1311 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1312 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001313
Vladimir Olteand6747712018-06-24 20:46:42 +03001314 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001315 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001316 local TYPE="${DIR##*/}"
1317 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001318 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001319 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001320 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001321 fi
1322 fi
1323
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001324 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001325 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1326 fi
1327
1328 # Check and print whether the fixup pipeline actually did anything.
1329 # This isn't done right after the fixup pipeline because we want this print
1330 # to come after print_spec above, when in kang mode.
1331 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1332 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1333 # Now sanity-check the spec for this blob.
1334 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1335 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1336 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1337 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001338 fi
1339
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001340 done
1341
1342 # Don't allow failing
1343 set -e
1344}
Louis Popia516c2f2016-07-25 15:51:13 +02001345
1346#
1347# extract_firmware:
1348#
1349# $1: file containing the list of items to extract
1350# $2: path to extracted radio folder
1351#
1352function extract_firmware() {
1353 if [ -z "$OUTDIR" ]; then
1354 echo "Output dir not set!"
1355 exit 1
1356 fi
1357
1358 parse_file_list "$1"
1359
1360 # Don't allow failing
1361 set -e
1362
1363 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1364 local COUNT=${#FILELIST[@]}
1365 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001366 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001367
1368 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1369 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1370 rm -rf "${OUTPUT_DIR:?}/"*
1371 VENDOR_RADIO_STATE=1
1372 fi
1373
1374 echo "Extracting $COUNT files in $1 from $SRC:"
1375
1376 for (( i=1; i<COUNT+1; i++ )); do
1377 local FILE="${FILELIST[$i-1]}"
1378 printf ' - %s \n' "/radio/$FILE"
1379
1380 if [ ! -d "$OUTPUT_DIR" ]; then
1381 mkdir -p "$OUTPUT_DIR"
1382 fi
1383 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1384 chmod 644 "$OUTPUT_DIR/$FILE"
1385 done
1386}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001387
1388function extract_img_data() {
1389 local image_file="$1"
1390 local out_dir="$2"
1391 local logFile="$TMPDIR/debugfs.log"
1392
1393 if [ ! -d "$out_dir" ]; then
1394 mkdir -p "$out_dir"
1395 fi
1396
1397 if [[ "$HOST_OS" == "Darwin" ]]; then
1398 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1399 echo "[-] Failed to extract data from '$image_file'"
1400 abort 1
1401 }
1402 else
1403 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1404 do
1405 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1406 echo "[-] Failed to extract data from '$image_file'"
1407 abort 1
1408 }
1409 done
1410 fi
1411
1412 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1413 if grep -Fq "$symlink_err" "$logFile"; then
1414 echo "[-] Symlinks have not been properly processed from $image_file"
1415 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1416 abort 1
1417 fi
1418}
1419
1420declare -ra VENDOR_SKIP_FILES=(
1421 "bin/toybox_vendor"
1422 "bin/toolbox"
1423 "bin/grep"
1424 "build.prop"
1425 "compatibility_matrix.xml"
1426 "default.prop"
1427 "etc/NOTICE.xml.gz"
1428 "etc/vintf/compatibility_matrix.xml"
1429 "etc/vintf/manifest.xml"
1430 "etc/wifi/wpa_supplicant.conf"
1431 "manifest.xml"
1432 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1433 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1434 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1435 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1436 "overlay/framework-res__auto_generated_rro.apk"
1437 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1438)
1439
1440function array_contains() {
1441 local element
1442 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1443 return 1
1444}
1445
1446function generate_prop_list_from_image() {
1447 local image_file="$1"
1448 local image_dir="$TMPDIR/image-temp"
1449 local output_list="$2"
1450 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1451 local -n skipped_vendor_files="$3"
1452
1453 extract_img_data "$image_file" "$image_dir"
1454
1455 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1456 do
1457 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1458 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1459 continue
1460 fi
1461 # Skip device defined skipped files since they will be re-generated at build time
1462 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1463 continue
1464 fi
1465 if suffix_match_file ".apk" "$FILE" ; then
1466 echo "-vendor/$FILE" >> "$output_list_tmp"
1467 else
1468 echo "vendor/$FILE" >> "$output_list_tmp"
1469 fi
1470 done
1471
1472 # Sort merged file with all lists
1473 sort -u "$output_list_tmp" > "$output_list"
1474
1475 # Clean-up
1476 rm -f "$output_list_tmp"
1477}