blob: a3f1364efcbabdb7b86f3d7cd1f4102c808dd805 [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-Tawab8aae50d2019-10-05 00:09:41 -0400278 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400279 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400280 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400281 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400282 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400283 local OUTTARGET=$(truncate_file $TARGET)
284 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
285 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
286 elif prefix_match_file "odm/" $TARGET ; then
287 local OUTTARGET=$(truncate_file $TARGET)
288 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
289 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400290 elif prefix_match_file "vendor/odm/" $TARGET ; then
291 local OUTTARGET=$(truncate_file $TARGET)
292 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
293 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
294 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
295 local OUTTARGET=$(truncate_file $TARGET)
296 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
297 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
298 elif prefix_match_file "vendor/" $TARGET ; then
299 local OUTTARGET=$(truncate_file $TARGET)
300 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
301 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
302 elif prefix_match_file "system/vendor/" $TARGET ; then
303 local OUTTARGET=$(truncate_file $TARGET)
304 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
305 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400306 elif prefix_match_file "system/" $TARGET ; then
307 local OUTTARGET=$(truncate_file $TARGET)
308 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
309 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400310 else
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400311 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400312 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
313 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700314 done
315 return 0
316}
317
318#
319# write_packages:
320#
321# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400322# $2: /system, /odm, /product, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700323# $3: type-specific extra flags
324# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700325#
326# Internal function which writes out the BUILD_PREBUILT stanzas
327# for all modules in the list. This is called by write_product_packages
328# after the modules are categorized.
329#
330function write_packages() {
331
332 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500333 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700334 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700335
336 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700337 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700338 local FILELIST=("${!ARR_NAME}")
339
340 local FILE=
341 local ARGS=
342 local BASENAME=
343 local EXTENSION=
344 local PKGNAME=
345 local SRC=
346
347 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300348 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700349 ARGS=$(target_args "$P")
350
351 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100352 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700353 EXTENSION=${BASENAME##*.}
354 PKGNAME=${BASENAME%.*}
355
356 # Add to final package list
357 PACKAGE_LIST+=("$PKGNAME")
358
359 SRC="proprietary"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400360 if [ "$PARTITION" = "system" ]; then
361 SRC+="/system"
362 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700363 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500364 elif [ "$PARTITION" = "product" ]; then
365 SRC+="/product"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700366 elif [ "$PARTITION" = "odm" ]; then
367 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700368 fi
369
370 printf 'include $(CLEAR_VARS)\n'
371 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
372 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
373 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700374 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700375 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
376 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
377 #if [ "$VENDOR_PKG" = "true" ]; then
378 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
379 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
380 #else
381 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
382 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
383 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700384 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700385 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
386 else
387 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
388 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700389 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700390 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700391 fi
392 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200393 if [ "$EXTRA" = "priv-app" ]; then
394 SRC="$SRC/priv-app"
395 else
396 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700397 fi
398 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
399 local CERT=platform
400 if [ ! -z "$ARGS" ]; then
401 CERT="$ARGS"
402 fi
403 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
404 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
405 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200406 local CERT=platform
407 if [ ! -z "$ARGS" ]; then
408 CERT="$ARGS"
409 fi
410 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700411 elif [ "$CLASS" = "ETC" ]; then
412 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
413 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700414 if [ "$ARGS" = "rootfs" ]; then
415 SRC="$SRC/rootfs"
416 if [ "$EXTRA" = "sbin" ]; then
417 SRC="$SRC/sbin"
418 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
419 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
420 fi
421 else
422 SRC="$SRC/bin"
423 fi
424 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
425 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700426 else
Steve Kondika991cf12016-07-28 12:13:12 -0700427 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700428 fi
429 printf 'LOCAL_MODULE_TAGS := optional\n'
430 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400431 if [ "$CLASS" = "APPS" ]; then
432 printf 'LOCAL_DEX_PREOPT := false\n'
433 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700434 if [ ! -z "$EXTENSION" ]; then
435 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
436 fi
M1cha15f226c2017-01-04 09:00:11 +0100437 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
438 if [ "$DIRNAME" != "." ]; then
439 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
440 fi
441 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700442 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700443 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
444 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500445 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800446 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500447 elif [ "$PARTITION" = "product" ]; then
448 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700449 elif [ "$PARTITION" = "odm" ]; then
450 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700451 fi
452 printf 'include $(BUILD_PREBUILT)\n\n'
453 done
454}
455
456#
457# write_product_packages:
458#
459# This function will create BUILD_PREBUILT entries in the
460# Android.mk and associated PRODUCT_PACKAGES list in the
461# product makefile for all files in the blob list which
462# start with a single dash (-) character.
463#
464function write_product_packages() {
465 PACKAGE_LIST=()
466
467 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
468
469 if [ "$COUNT" = "0" ]; then
470 return 0
471 fi
472
473 # Figure out what's 32-bit, what's 64-bit, and what's multilib
474 # I really should not be doing this in bash due to shitty array passing :(
475 local T_LIB32=( $(prefix_match "lib/") )
476 local T_LIB64=( $(prefix_match "lib64/") )
477 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
478 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700479 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700480
Steve Kondik03ce4002016-07-29 00:00:16 -0700481 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500482 write_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700483 fi
484 if [ "${#LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500485 write_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700486 fi
487 if [ "${#LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500488 write_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDMK"
Steve Kondik03ce4002016-07-29 00:00:16 -0700489 fi
490
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400491 local T_S_LIB32=( $(prefix_match "system/lib/") )
492 local T_S_LIB64=( $(prefix_match "system/lib64/") )
493 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
494 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
495 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
496
497 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
498 write_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDMK"
499 fi
500 if [ "${#S_LIB32[@]}" -gt "0" ]; then
501 write_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDMK"
502 fi
503 if [ "${#S_LIB64[@]}" -gt "0" ]; then
504 write_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDMK"
505 fi
506
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700507 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
508 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
509 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
510 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700511 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700512
513 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500514 write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700515 fi
516 if [ "${#V_LIB32[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500517 write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700518 fi
519 if [ "${#V_LIB64[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500520 write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
521 fi
522
523 local T_P_LIB32=( $(prefix_match "product/lib/") )
524 local T_P_LIB64=( $(prefix_match "product/lib64/") )
525 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
526 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
527 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
528
529 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
530 write_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDMK"
531 fi
532 if [ "${#P_LIB32[@]}" -gt "0" ]; then
533 write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
534 fi
535 if [ "${#P_LIB64[@]}" -gt "0" ]; then
536 write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700537 fi
538
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700539 local T_O_LIB32=( $(prefix_match "odm/lib/") )
540 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
541 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
542 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
543 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
544
545 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
546 write_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDMK"
547 fi
548 if [ "${#O_LIB32[@]}" -gt "0" ]; then
549 write_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDMK"
550 fi
551 if [ "${#O_LIB64[@]}" -gt "0" ]; then
552 write_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDMK"
553 fi
554
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700555 # Apps
556 local APPS=( $(prefix_match "app/") )
557 if [ "${#APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500558 write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700559 fi
560 local PRIV_APPS=( $(prefix_match "priv-app/") )
561 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500562 write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700563 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400564 local S_APPS=( $(prefix_match "system/app/") )
565 if [ "${#S_APPS[@]}" -gt "0" ]; then
566 write_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDMK"
567 fi
568 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
569 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
570 write_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDMK"
571 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700572 local V_APPS=( $(prefix_match "vendor/app/") )
573 if [ "${#V_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500574 write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700575 fi
576 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
577 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500578 write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
579 fi
580 local P_APPS=( $(prefix_match "product/app/") )
581 if [ "${#P_APPS[@]}" -gt "0" ]; then
582 write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
583 fi
584 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
585 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
586 write_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700587 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700588 local O_APPS=( $(prefix_match "odm/app/") )
589 if [ "${#O_APPS[@]}" -gt "0" ]; then
590 write_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
591 fi
592 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
593 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
594 write_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
595 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700596
597 # Framework
598 local FRAMEWORK=( $(prefix_match "framework/") )
599 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500600 write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700601 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400602 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
603 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
604 write_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDMK"
605 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200606 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200607 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500608 write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
609 fi
610 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
611 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
612 write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
Christian Oderc16f3272017-10-08 23:15:52 +0200613 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700614 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
615 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
616 write_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
617 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700618
619 # Etc
620 local ETC=( $(prefix_match "etc/") )
621 if [ "${#ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500622 write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700623 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400624 local S_ETC=( $(prefix_match "system/etc/") )
625 if [ "${#ETC[@]}" -gt "0" ]; then
626 write_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDMK"
627 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700628 local V_ETC=( $(prefix_match "vendor/etc/") )
629 if [ "${#V_ETC[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500630 write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
631 fi
632 local P_ETC=( $(prefix_match "product/etc/") )
633 if [ "${#P_ETC[@]}" -gt "0" ]; then
634 write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700635 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700636 local O_ETC=( $(prefix_match "odm/etc/") )
637 if [ "${#O_ETC[@]}" -gt "0" ]; then
638 write_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDMK"
639 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700640
641 # Executables
642 local BIN=( $(prefix_match "bin/") )
643 if [ "${#BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500644 write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700645 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400646 local S_BIN=( $(prefix_match "system/bin/") )
647 if [ "${#BIN[@]}" -gt "0" ]; then
648 write_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDMK"
649 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700650 local V_BIN=( $(prefix_match "vendor/bin/") )
651 if [ "${#V_BIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500652 write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
653 fi
654 local P_BIN=( $(prefix_match "product/bin/") )
655 if [ "${#P_BIN[@]}" -gt "0" ]; then
656 write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700657 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700658 local O_BIN=( $(prefix_match "odm/bin/") )
659 if [ "${#O_BIN[@]}" -gt "0" ]; then
660 write_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDMK"
661 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700662 local SBIN=( $(prefix_match "sbin/") )
663 if [ "${#SBIN[@]}" -gt "0" ]; then
razorlovesb5c2c962019-07-29 02:21:34 -0500664 write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700665 fi
666
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700667
668 # Actually write out the final PRODUCT_PACKAGES list
669 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
670
671 if [ "$PACKAGE_COUNT" -eq "0" ]; then
672 return 0
673 fi
674
675 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
676 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
677 local LINEEND=" \\"
678 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
679 LINEEND=""
680 fi
681 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
682 done
683}
684
685#
686# write_header:
687#
688# $1: file which will be written to
689#
690# writes out the copyright header with the current year.
691# note that this is not an append operation, and should
692# be executed first!
693#
694function write_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600695 if [ -f $1 ]; then
696 rm $1
697 fi
698
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700699 YEAR=$(date +"%Y")
700
701 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
702
Matt Mower8945f5e2017-01-07 14:08:17 -0600703 NUM_REGEX='^[0-9]+$'
704 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
705 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
706 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
707 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
708 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
709 fi
710 if [ $YEAR -eq 2017 ]; then
711 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
712 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
713 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
714 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
715 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
716 else
717 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
718 fi
719 else
720 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
721 fi
722
723 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700724#
725# Licensed under the Apache License, Version 2.0 (the "License");
726# you may not use this file except in compliance with the License.
727# You may obtain a copy of the License at
728#
729# http://www.apache.org/licenses/LICENSE-2.0
730#
731# Unless required by applicable law or agreed to in writing, software
732# distributed under the License is distributed on an "AS IS" BASIS,
733# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
734# See the License for the specific language governing permissions and
735# limitations under the License.
736
737# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
738
739EOF
740}
741
742#
743# write_headers:
744#
745# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400746# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700747#
748# Calls write_header for each of the makefiles and creates
749# the initial path declaration and device guard for the
750# Android.mk
751#
752function write_headers() {
753 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400754
755 GUARD="$2"
756 if [ -z "$GUARD" ]; then
757 GUARD="TARGET_DEVICE"
758 fi
759
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700760 cat << EOF >> "$ANDROIDMK"
761LOCAL_PATH := \$(call my-dir)
762
763EOF
764 if [ "$COMMON" -ne 1 ]; then
765 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400766ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700767
768EOF
769 else
770 if [ -z "$1" ]; then
771 echo "Argument with devices to be added to guard must be set!"
772 exit 1
773 fi
774 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400775ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700776
777EOF
778 fi
779
780 write_header "$BOARDMK"
781 write_header "$PRODUCTMK"
782}
783
784#
785# write_footers:
786#
787# Closes the inital guard and any other finalization tasks. Must
788# be called as the final step.
789#
790function write_footers() {
791 cat << EOF >> "$ANDROIDMK"
792endif
793EOF
794}
795
796# Return success if adb is up and not in recovery
797function _adb_connected {
798 {
Steve Kondik7561d192016-09-01 21:40:27 -0700799 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700800 then
801 return 0
802 fi
803 } 2>/dev/null
804
805 return 1
806};
807
808#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100809# parse_file_list:
810#
811# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400812# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +0100813#
814# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700815#
816function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100817 if [ -z "$1" ]; then
818 echo "An input file is expected!"
819 exit 1
820 elif [ ! -f "$1" ]; then
821 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700822 exit 1
823 fi
824
Vladimir Olteanc5034462019-01-17 03:04:16 +0200825 if [ -n "$2" ]; then
826 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400827 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +0200828 # Match all lines starting with first line found to start* with '#'
829 # comment and contain** $2, and ending with first line to be empty*.
830 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
831 # **the $2 match is case-insensitive
832 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400833 else
834 LIST=$1
835 fi
836
837
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700838 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700839 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200840 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700841 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700842 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +0200843 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700844
845 while read -r line; do
846 if [ -z "$line" ]; then continue; fi
847
Steve Kondik48f8df82016-08-14 03:55:08 -0700848 # If the line has a pipe delimiter, a sha1 hash should follow.
849 # This indicates the file should be pinned and not overwritten
850 # when extracting files.
851 local SPLIT=(${line//\|/ })
852 local COUNT=${#SPLIT[@]}
853 local SPEC=${SPLIT[0]}
854 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +0200855 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -0700856 if [ "$COUNT" -gt "1" ]; then
857 HASH=${SPLIT[1]}
858 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +0200859 if [ "$COUNT" -gt "2" ]; then
860 FIXUP_HASH=${SPLIT[2]}
861 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700862
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700863 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700864 if [[ "$SPEC" =~ ^- ]]; then
865 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
866 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200867 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700868 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700869 PRODUCT_COPY_FILES_LIST+=("$SPEC")
870 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +0200871 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700872 fi
873
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400874 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700875}
876
877#
878# write_makefiles:
879#
880# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400881# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700882#
883# Calls write_product_copy_files and write_product_packages on
884# the given file and appends to the Android.mk as well as
885# the product makefile.
886#
887function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700888 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400889 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700890 write_product_packages
891}
892
893#
Louis Popia516c2f2016-07-25 15:51:13 +0200894# append_firmware_calls_to_makefiles:
895#
896# Appends to Android.mk the calls to all images present in radio folder
897# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
898#
899function append_firmware_calls_to_makefiles() {
900 cat << EOF >> "$ANDROIDMK"
901ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
902
903RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
904\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
905 \$(call add-radio-file,radio/\$(f)))
906\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
907
908endif
909
910EOF
911}
912
913#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200914# get_file:
915#
916# $1: input file
917# $2: target file/folder
918# $3: source of the file (can be "adb" or a local folder)
919#
920# Silently extracts the input file to defined target
921# Returns success if file can be pulled from the device or found locally
922#
923function get_file() {
924 local SRC="$3"
925
926 if [ "$SRC" = "adb" ]; then
927 # try to pull
928 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
929
930 return 1
931 else
932 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +0300933 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
934 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +0200935 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200936
937 return 1
938 fi
939};
940
941#
942# oat2dex:
943#
944# $1: extracted apk|jar (to check if deodex is required)
945# $2: odexed apk|jar to deodex
946# $3: source of the odexed apk|jar
947#
948# Convert apk|jar .odex in the corresposing classes.dex
949#
950function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200951 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200952 local OEM_TARGET="$2"
953 local SRC="$3"
954 local TARGET=
955 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -0500956 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200957
958 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100959 export BAKSMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/baksmali.jar
960 export SMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200961 fi
962
Joe Maples9be579f2018-01-05 14:51:33 -0500963 if [ -z "$VDEXEXTRACTOR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100964 export VDEXEXTRACTOR="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -0500965 fi
966
codeworkx1c29bf62018-09-23 12:36:57 +0200967 if [ -z "$CDEXCONVERTER" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +0100968 export CDEXCONVERTER="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +0200969 fi
970
Luca Stefani7f9fff22016-07-18 13:47:55 +0200971 # Extract existing boot.oats to the temp folder
972 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700973 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200974 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700975 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +0300976 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200977 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700978 else
979 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200980 fi
981 done
982 fi
983
984 if [ -z "$ARCHES" ]; then
985 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
986 fi
987
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200988 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700989 return;
990 fi
991
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200992 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200993 return 0 # target apk|jar is already odexed, return
994 fi
995
996 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700997 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200998
999 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001000 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001001
1002 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001003 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -05001004 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001005 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1006 for CLASS in $CLASSES; do
1007 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1008 # Check if we have to deal with CompactDex
1009 if [[ "$CLASS" == *.cdex ]]; then
1010 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1011 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1012 else
1013 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1014 fi
1015 done
Joe Maples9be579f2018-01-05 14:51:33 -05001016 else
1017 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1018 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001019 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001020 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001021 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001022 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001023 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001024 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001025 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001026 # try to extract classes.dex from boot.vdex for frameworks jars
1027 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001028 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001029 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001030 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1031 for CLASS in $CLASSES; do
1032 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1033 # Check if we have to deal with CompactDex
1034 if [[ "$CLASS" == *.cdex ]]; then
1035 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1036 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1037 else
1038 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1039 fi
1040 done
Joe Maples9be579f2018-01-05 14:51:33 -05001041 else
1042 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1043 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1044 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001045 else
1046 continue
1047 fi
1048
Luca Stefani7f9fff22016-07-18 13:47:55 +02001049 done
1050
1051 rm -rf "$TMPDIR/dexout"
1052}
1053
1054#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001055# init_adb_connection:
1056#
1057# Starts adb server and waits for the device
1058#
1059function init_adb_connection() {
1060 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1061 if ! _adb_connected; then
1062 echo "No device is online. Waiting for one..."
1063 echo "Please connect USB and/or enable USB debugging"
1064 until _adb_connected; do
1065 sleep 1
1066 done
1067 echo "Device Found."
1068 fi
1069
1070 # Retrieve IP and PORT info if we're using a TCP connection
1071 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1072 | head -1 | awk '{print $1}')
1073 adb root &> /dev/null
1074 sleep 0.3
1075 if [ -n "$TCPIPPORT" ]; then
1076 # adb root just killed our connection
1077 # so reconnect...
1078 adb connect "$TCPIPPORT"
1079 fi
1080 adb wait-for-device &> /dev/null
1081 sleep 0.3
1082}
1083
1084#
Luca Stefani3a030122016-07-30 12:08:25 +02001085# fix_xml:
1086#
1087# $1: xml file to fix
1088#
1089function fix_xml() {
1090 local XML="$1"
1091 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1092
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001093 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1094 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001095
1096 mv "$TEMP_XML" "$XML"
1097}
1098
Vladimir Oltean4818c232019-01-17 03:07:34 +02001099function get_hash() {
1100 local FILE="$1"
1101
1102 if [ "$(uname)" == "Darwin" ]; then
1103 shasum "${FILE}" | awk '{print $1}'
1104 else
1105 sha1sum "${FILE}" | awk '{print $1}'
1106 fi
1107}
1108
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001109function print_spec() {
1110 local SPEC_PRODUCT_PACKAGE="$1"
1111 local SPEC_SRC_FILE="$2"
1112 local SPEC_DST_FILE="$3"
1113 local SPEC_ARGS="$4"
1114 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001115 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001116
1117 local PRODUCT_PACKAGE=""
1118 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1119 PRODUCT_PACKAGE="-"
1120 fi
1121 local SRC=""
1122 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1123 SRC="${SPEC_SRC_FILE}:"
1124 fi
1125 local DST=""
1126 if [ ! -z "${SPEC_DST_FILE}" ]; then
1127 DST="${SPEC_DST_FILE}"
1128 fi
1129 local ARGS=""
1130 if [ ! -z "${SPEC_ARGS}" ]; then
1131 ARGS=";${SPEC_ARGS}"
1132 fi
1133 local HASH=""
1134 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1135 HASH="|${SPEC_HASH}"
1136 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001137 local FIXUP_HASH=""
1138 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1139 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1140 fi
1141 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1142}
1143
1144# To be overridden by device-level extract-files.sh
1145# Parameters:
1146# $1: spec name of a blob. Can be used for filtering.
1147# If the spec is "src:dest", then $1 is "dest".
1148# If the spec is "src", then $1 is "src".
1149# $2: path to blob file. Can be used for fixups.
1150#
1151function blob_fixup() {
1152 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001153}
1154
Luca Stefani3a030122016-07-30 12:08:25 +02001155#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001156# extract:
1157#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001158# Positional parameters:
1159# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001160# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001161# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1162#
1163# Non-positional parameters (coming after $2):
1164# --section: preferred way of selecting the portion to parse and extract from
1165# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001166# --kang: if present, this option will activate the printing of hashes for the
1167# extracted blobs. Useful with --section for subsequent pinning of
1168# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001169#
1170function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001171 # Consume positional parameters
1172 local PROPRIETARY_FILES_TXT="$1"; shift
1173 local SRC="$1"; shift
1174 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001175 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001176
1177 # Consume optional, non-positional parameters
1178 while [ "$#" -gt 0 ]; do
1179 case "$1" in
1180 -s|--section)
1181 SECTION="$2"; shift
1182 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001183 -k|--kang)
1184 KANG=true
1185 DISABLE_PINNING=1
1186 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001187 *)
1188 # Backwards-compatibility with the old behavior, where $3, if
1189 # present, denoted an optional positional ${SECTION} argument.
1190 # Users of ${SECTION} are encouraged to migrate from setting it as
1191 # positional $3, to non-positional --section ${SECTION}, the
1192 # reason being that it doesn't scale to have more than 1 optional
1193 # positional argument.
1194 SECTION="$1"
1195 ;;
1196 esac
1197 shift
1198 done
1199
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001200 if [ -z "$OUTDIR" ]; then
1201 echo "Output dir not set!"
1202 exit 1
1203 fi
1204
Vladimir Olteanc5034462019-01-17 03:04:16 +02001205 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001206
1207 # Allow failing, so we can try $DEST and/or $FILE
1208 set +e
1209
1210 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001211 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001212 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001213 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001214 local COUNT=${#FILELIST[@]}
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001215 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001216 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1217
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001218 if [ "$SRC" = "adb" ]; then
1219 init_adb_connection
1220 fi
1221
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001222 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001223 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001224
1225 # Check if we're working with the same zip that was passed last time.
1226 # If so, let's just use what's already extracted.
1227 MD5=`md5sum "$SRC"| awk '{print $1}'`
1228 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1229
1230 if [ "$MD5" != "$OLDMD5" ]; then
1231 rm -rf "$DUMPDIR"
1232 mkdir "$DUMPDIR"
1233 unzip "$SRC" -d "$DUMPDIR"
1234 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1235
1236 # Stop if an A/B OTA zip is detected. We cannot extract these.
1237 if [ -a "$DUMPDIR"/payload.bin ]; then
1238 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1239 exit 1
1240 # If OTA is block based, extract it.
1241 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1242 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001243 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 -05001244 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1245 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1246 echo "Requesting sudo access to mount the system.img"
1247 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1248 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1249 sudo umount "$DUMPDIR"/tmp
1250 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1251 fi
1252 fi
1253
1254 SRC="$DUMPDIR"
1255 fi
1256
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001257 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001258 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001259 rm -rf "${OUTPUT_TMP:?}"
1260 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001261 if [ -d "$OUTPUT_ROOT" ]; then
1262 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1263 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001264 VENDOR_STATE=1
1265 fi
1266
Vladimir Olteanc5034462019-01-17 03:04:16 +02001267 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001268
1269 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001270
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001271 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001272 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001273 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001274 local OUTPUT_DIR=
1275 local TMP_DIR=
1276 local SRC_FILE=
1277 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001278 local IS_PRODUCT_PACKAGE=false
1279
1280 # Note: this relies on the fact that the ${FILELIST[@]} array
1281 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1282 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1283 IS_PRODUCT_PACKAGE=true
1284 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001285
Vladimir Olteand652a062018-06-24 20:42:01 +03001286 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001287 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1288 TMP_DIR="${OUTPUT_TMP}/rootfs"
1289 SRC_FILE="/${SPEC_SRC_FILE}"
1290 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001291 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001292 OUTPUT_DIR="${OUTPUT_ROOT}"
1293 TMP_DIR="${OUTPUT_TMP}"
1294 SRC_FILE="/system/${SPEC_SRC_FILE}"
1295 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001296 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001297
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001298 # Strip the file path in the vendor repo of "system", if present
1299 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001300 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001301 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001302
Gabriele Me6df25b2017-10-11 00:58:59 +02001303 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001304 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001305 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001306 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001307 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001308 if [ -f "${VENDOR_REPO_FILE}" ]; then
1309 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001310 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001311 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001312 fi
1313 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001314 local TMP_HASH=$(get_hash "${PINNED}")
1315 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001316 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001317 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1318 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001319 fi
1320 fi
1321 fi
1322 fi
1323
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001324 if [ "${KANG}" = false ]; then
1325 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1326 fi
1327
Gabriele Me6df25b2017-10-11 00:58:59 +02001328 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001329 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001330 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001331 FOUND=false
1332 # Try Lineage target first.
1333 # Also try to search for files stripped of
1334 # the "/system" prefix, if we're actually extracting
1335 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001336 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001337 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1338 FOUND=true
1339 break
1340 }
1341 done
1342
1343 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001344 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001345 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001346 fi
1347 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001348
Vladimir Oltean4818c232019-01-17 03:07:34 +02001349 # Blob fixup pipeline has 2 parts: one that is fixed and
1350 # one that is user-configurable
1351 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1352 # Deodex apk|jar if that's the case
1353 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1354 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1355 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001356 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1357 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001358 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001359 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001360 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1361 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001362 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001363 # Now run user-supplied fixup function
1364 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1365 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001366
Vladimir Olteand6747712018-06-24 20:46:42 +03001367 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001368 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001369 local TYPE="${DIR##*/}"
1370 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001371 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001372 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001373 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001374 fi
1375 fi
1376
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001377 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001378 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1379 fi
1380
1381 # Check and print whether the fixup pipeline actually did anything.
1382 # This isn't done right after the fixup pipeline because we want this print
1383 # to come after print_spec above, when in kang mode.
1384 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1385 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1386 # Now sanity-check the spec for this blob.
1387 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1388 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1389 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1390 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001391 fi
1392
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001393 done
1394
1395 # Don't allow failing
1396 set -e
1397}
Louis Popia516c2f2016-07-25 15:51:13 +02001398
1399#
1400# extract_firmware:
1401#
1402# $1: file containing the list of items to extract
1403# $2: path to extracted radio folder
1404#
1405function extract_firmware() {
1406 if [ -z "$OUTDIR" ]; then
1407 echo "Output dir not set!"
1408 exit 1
1409 fi
1410
1411 parse_file_list "$1"
1412
1413 # Don't allow failing
1414 set -e
1415
1416 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1417 local COUNT=${#FILELIST[@]}
1418 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001419 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001420
1421 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1422 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1423 rm -rf "${OUTPUT_DIR:?}/"*
1424 VENDOR_RADIO_STATE=1
1425 fi
1426
1427 echo "Extracting $COUNT files in $1 from $SRC:"
1428
1429 for (( i=1; i<COUNT+1; i++ )); do
1430 local FILE="${FILELIST[$i-1]}"
1431 printf ' - %s \n' "/radio/$FILE"
1432
1433 if [ ! -d "$OUTPUT_DIR" ]; then
1434 mkdir -p "$OUTPUT_DIR"
1435 fi
1436 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1437 chmod 644 "$OUTPUT_DIR/$FILE"
1438 done
1439}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001440
1441function extract_img_data() {
1442 local image_file="$1"
1443 local out_dir="$2"
1444 local logFile="$TMPDIR/debugfs.log"
1445
1446 if [ ! -d "$out_dir" ]; then
1447 mkdir -p "$out_dir"
1448 fi
1449
1450 if [[ "$HOST_OS" == "Darwin" ]]; then
1451 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1452 echo "[-] Failed to extract data from '$image_file'"
1453 abort 1
1454 }
1455 else
1456 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1457 do
1458 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1459 echo "[-] Failed to extract data from '$image_file'"
1460 abort 1
1461 }
1462 done
1463 fi
1464
1465 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1466 if grep -Fq "$symlink_err" "$logFile"; then
1467 echo "[-] Symlinks have not been properly processed from $image_file"
1468 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1469 abort 1
1470 fi
1471}
1472
1473declare -ra VENDOR_SKIP_FILES=(
1474 "bin/toybox_vendor"
1475 "bin/toolbox"
1476 "bin/grep"
1477 "build.prop"
1478 "compatibility_matrix.xml"
1479 "default.prop"
1480 "etc/NOTICE.xml.gz"
1481 "etc/vintf/compatibility_matrix.xml"
1482 "etc/vintf/manifest.xml"
1483 "etc/wifi/wpa_supplicant.conf"
1484 "manifest.xml"
1485 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1486 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1487 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1488 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1489 "overlay/framework-res__auto_generated_rro.apk"
1490 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1491)
1492
1493function array_contains() {
1494 local element
1495 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1496 return 1
1497}
1498
1499function generate_prop_list_from_image() {
1500 local image_file="$1"
1501 local image_dir="$TMPDIR/image-temp"
1502 local output_list="$2"
1503 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1504 local -n skipped_vendor_files="$3"
1505
1506 extract_img_data "$image_file" "$image_dir"
1507
1508 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1509 do
1510 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1511 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1512 continue
1513 fi
1514 # Skip device defined skipped files since they will be re-generated at build time
1515 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1516 continue
1517 fi
1518 if suffix_match_file ".apk" "$FILE" ; then
1519 echo "-vendor/$FILE" >> "$output_list_tmp"
1520 else
1521 echo "vendor/$FILE" >> "$output_list_tmp"
1522 fi
1523 done
1524
1525 # Sort merged file with all lists
1526 sort -u "$output_list_tmp" > "$output_list"
1527
1528 # Clean-up
1529 rm -f "$output_list_tmp"
1530}