blob: 6c28ed0c51f786a9765fa82d86ae74dcfd782a4e [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
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -070088 export ANDROIDBP="$LINEAGE_ROOT"/"$OUTDIR"/Android.bp
Luca Stefani5c60e4f2017-08-17 19:28:48 +020089 export ANDROIDMK="$LINEAGE_ROOT"/"$OUTDIR"/Android.mk
90 export BOARDMK="$LINEAGE_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -070091
92 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
93 COMMON=1
94 else
95 COMMON=0
96 fi
97
Gabriele Mb6effb32017-05-01 18:22:04 +020098 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -070099 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +0200100 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700101 else
102 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +0200103 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700104 fi
105}
106
Vladimir Oltean95643282018-06-24 20:22:41 +0300107# Helper functions for parsing a spec.
108# notes: an optional "|SHA1" that may appear in the format is stripped
109# early from the spec in the parse_file_list function, and
110# should not be present inside the input parameter passed
111# to these functions.
112
113#
114# input: spec in the form of "src[:dst][;args]"
115# output: "src"
116#
117function src_file() {
118 local SPEC="$1"
119 local SPLIT=(${SPEC//:/ })
120 local ARGS="$(target_args ${SPEC})"
121 # Regardless of there being a ":" delimiter or not in the spec,
122 # the source file is always either the first, or the only entry.
123 local SRC="${SPLIT[0]}"
124 # Remove target_args suffix, if present
125 echo "${SRC%;${ARGS}}"
126}
127
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700128#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300129# input: spec in the form of "src[:dst][;args]"
130# output: "dst" if present, "src" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700131#
132function target_file() {
dianlujitao33ee5962020-01-02 15:26:44 +0800133 local SPEC="${1%%;*}"
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300134 local SPLIT=(${SPEC//:/ })
135 local ARGS="$(target_args ${SPEC})"
136 local DST=
137 case ${#SPLIT[@]} in
138 1)
139 # The spec doesn't have a : delimiter
140 DST="${SPLIT[0]}"
141 ;;
142 *)
143 # The spec actually has a src:dst format
144 DST="${SPLIT[1]}"
145 ;;
146 esac
147 # Remove target_args suffix, if present
148 echo "${DST%;${ARGS}}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700149}
150
151#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300152# input: spec in the form of "src[:dst][;args]"
153# output: "args" if present, "" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700154#
155function target_args() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300156 local SPEC="$1"
157 local SPLIT=(${SPEC//;/ })
158 local ARGS=
159 case ${#SPLIT[@]} in
160 1)
161 # No ";" delimiter in the spec.
162 ;;
163 *)
164 # The "args" are whatever comes after the ";" character.
165 # Basically the spec stripped of whatever is to the left of ";".
166 ARGS="${SPEC#${SPLIT[0]};}"
167 ;;
168 esac
169 echo "${ARGS}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700170}
171
172#
173# prefix_match:
174#
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300175# input:
176# - $1: prefix
177# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
178# output:
179# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700180#
181function prefix_match() {
182 local PREFIX="$1"
Vladimir Olteana48b9fe2018-04-02 22:37:09 +0300183 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
184 local FILE=$(target_file "$LINE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700185 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300186 local ARGS=$(target_args "$LINE")
187 if [ -z "${ARGS}" ]; then
188 echo "${FILE#$PREFIX}"
189 else
190 echo "${FILE#$PREFIX};${ARGS}"
191 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700192 fi
193 done
194}
195
196#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400197# prefix_match_file:
198#
199# $1: the prefix to match on
200# $2: the file to match the prefix for
201#
202# Internal function which returns true if a filename contains the
203# specified prefix.
204#
205function prefix_match_file() {
206 local PREFIX="$1"
207 local FILE="$2"
208 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
209 return 0
210 else
211 return 1
212 fi
213}
214
215#
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -0700216# suffix_match_file:
217#
218# $1: the suffix to match on
219# $2: the file to match the suffix for
220#
221# Internal function which returns true if a filename contains the
222# specified suffix.
223#
224function suffix_match_file() {
225 local SUFFIX="$1"
226 local FILE="$2"
227 if [[ "$FILE" = *"$SUFFIX" ]]; then
228 return 0
229 else
230 return 1
231 fi
232}
233
234#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400235# truncate_file
236#
237# $1: the filename to truncate
238# $2: the argument to output the truncated filename to
239#
240# Internal function which truncates a filename by removing the first dir
241# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
242#
243function truncate_file() {
244 local FILE="$1"
245 RETURN_FILE="$2"
246 local FIND="${FILE%%/*}"
247 local LOCATION="${#FIND}+1"
248 echo ${FILE:$LOCATION}
249}
250
251#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700252# write_product_copy_files:
253#
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400254# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400255#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700256# Creates the PRODUCT_COPY_FILES section in the product makefile for all
257# items in the list which do not start with a dash (-).
258#
259function write_product_copy_files() {
260 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
261 local TARGET=
262 local FILE=
263 local LINEEND=
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400264 local TREBLE_COMPAT=$1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700265
266 if [ "$COUNT" -eq "0" ]; then
267 return 0
268 fi
269
270 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
271 for (( i=1; i<COUNT+1; i++ )); do
272 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
273 LINEEND=" \\"
274 if [ "$i" -eq "$COUNT" ]; then
275 LINEEND=""
276 fi
277
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300278 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400279 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400280 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400281 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400282 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400283 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400284 local OUTTARGET=$(truncate_file $TARGET)
285 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
286 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani0409f232020-09-09 15:53:58 +0200287 elif prefix_match_file "system_ext/" $TARGET ; then
288 local OUTTARGET=$(truncate_file $TARGET)
289 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
290 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
291 elif prefix_match_file "system/system_ext/" $TARGET ; then
292 local OUTTARGET=$(truncate_file $TARGET)
293 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
294 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400295 elif prefix_match_file "odm/" $TARGET ; then
296 local OUTTARGET=$(truncate_file $TARGET)
297 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
298 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400299 elif prefix_match_file "vendor/odm/" $TARGET ; then
300 local OUTTARGET=$(truncate_file $TARGET)
301 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
302 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
303 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
304 local OUTTARGET=$(truncate_file $TARGET)
305 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
306 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
307 elif prefix_match_file "vendor/" $TARGET ; then
308 local OUTTARGET=$(truncate_file $TARGET)
309 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
310 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
311 elif prefix_match_file "system/vendor/" $TARGET ; then
312 local OUTTARGET=$(truncate_file $TARGET)
313 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
314 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400315 elif prefix_match_file "system/" $TARGET ; then
316 local OUTTARGET=$(truncate_file $TARGET)
317 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
318 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400319 else
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400320 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400321 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
322 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700323 done
324 return 0
325}
326
327#
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700328# write_blueprint_packages:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700329#
330# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani0409f232020-09-09 15:53:58 +0200331# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700332# $3: type-specific extra flags
333# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700334#
335# Internal function which writes out the BUILD_PREBUILT stanzas
336# for all modules in the list. This is called by write_product_packages
337# after the modules are categorized.
338#
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700339function write_blueprint_packages() {
340
341 local CLASS="$1"
342 local PARTITION="$2"
343 local EXTRA="$3"
344
345 # Yes, this is a horrible hack - we create a new array using indirection
346 local ARR_NAME="$4[@]"
347 local FILELIST=("${!ARR_NAME}")
348
349 local FILE=
350 local ARGS=
351 local BASENAME=
352 local EXTENSION=
353 local PKGNAME=
354 local SRC=
355
356 for P in "${FILELIST[@]}"; do
357 FILE=$(target_file "$P")
358 ARGS=$(target_args "$P")
359
360 BASENAME=$(basename "$FILE")
361 DIRNAME=$(dirname "$FILE")
362 EXTENSION=${BASENAME##*.}
363 PKGNAME=${BASENAME%.*}
364
365 # Add to final package list
366 PACKAGE_LIST+=("$PKGNAME")
367
368 SRC="proprietary"
369 if [ "$PARTITION" = "system" ]; then
370 SRC+="/system"
371 elif [ "$PARTITION" = "vendor" ]; then
372 SRC+="/vendor"
373 elif [ "$PARTITION" = "product" ]; then
374 SRC+="/product"
Luca Stefani0409f232020-09-09 15:53:58 +0200375 elif [ "$PARTITION" = "system_ext" ]; then
376 SRC+="/system_ext"
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700377 elif [ "$PARTITION" = "odm" ]; then
378 SRC+="/odm"
379 fi
380
381 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
382 printf 'cc_prebuilt_library_shared {\n'
383 printf '\tname: "%s",\n' "$PKGNAME"
384 printf '\towner: "%s",\n' "$VENDOR"
385 printf '\tstrip: {\n'
386 printf '\t\tnone: true,\n'
387 printf '\t},\n'
388 printf '\ttarget: {\n'
389 if [ "$EXTRA" = "both" ]; then
390 printf '\t\tandroid_arm: {\n'
391 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
392 printf '\t\t},\n'
393 printf '\t\tandroid_arm64: {\n'
394 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
395 printf '\t\t},\n'
396 elif [ "$EXTRA" = "64" ]; then
397 printf '\t\tandroid_arm64: {\n'
398 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
399 printf '\t\t},\n'
400 else
401 printf '\t\tandroid_arm: {\n'
402 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
403 printf '\t\t},\n'
404 fi
405 printf '\t},\n'
406 if [ "$EXTRA" != "none" ]; then
407 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
408 fi
409 elif [ "$CLASS" = "APPS" ]; then
410 printf 'android_app_import {\n'
411 printf '\tname: "%s",\n' "$PKGNAME"
412 printf '\towner: "%s",\n' "$VENDOR"
413 if [ "$EXTRA" = "priv-app" ]; then
414 SRC="$SRC/priv-app"
415 else
416 SRC="$SRC/app"
417 fi
418 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
419 if [ "$ARGS" = "PRESIGNED" ]; then
420 printf '\tpresigned: true,\n'
421 elif [ ! -z "$ARGS" ]; then
422 printf '\tcertificate: "%s",\n' "$ARGS"
423 else
424 printf '\tcertificate: "platform",\n'
425 fi
426 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
427 printf 'dex_import {\n'
428 printf '\tname: "%s",\n' "$PKGNAME"
429 printf '\towner: "%s",\n' "$VENDOR"
430 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
431 elif [ "$CLASS" = "ETC" ]; then
432 if [ "$EXTENSION" = "xml" ]; then
433 printf 'prebuilt_etc_xml {\n'
434 else
435 printf 'prebuilt_etc {\n'
436 fi
437 printf '\tname: "%s",\n' "$PKGNAME"
438 printf '\towner: "%s",\n' "$VENDOR"
439 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
440 elif [ "$CLASS" = "EXECUTABLES" ]; then
441 if [ "$EXTENSION" = "sh" ]; then
442 printf 'sh_binary {\n'
443 else
444 printf 'cc_prebuilt_binary {\n'
445 fi
446 printf '\tname: "%s",\n' "$PKGNAME"
447 printf '\towner: "%s",\n' "$VENDOR"
448 if [ "$ARGS" = "rootfs" ]; then
449 SRC="$SRC/rootfs"
450 if [ "$EXTRA" = "sbin" ]; then
451 SRC="$SRC/sbin"
452 printf '\tdist {\n'
453 printf '\t\tdest: "%s",\n' "root/sbin"
454 printf '\t},'
455 fi
456 else
457 SRC="$SRC/bin"
458 fi
459 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
460 unset EXTENSION
461 else
462 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
463 fi
464 if [ "$CLASS" = "APPS" ]; then
465 printf '\tdex_preopt: {\n'
466 printf '\t\tenabled: false,\n'
467 printf '\t},\n'
468 fi
Andreas Schneiderb1d3f652020-05-25 17:03:17 +0200469 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700470 if [ "$DIRNAME" != "." ]; then
Andreas Schneidera7e400c2020-05-23 15:58:43 +0200471 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700472 fi
473 fi
Andreas Schneiderb1d3f652020-05-25 17:03:17 +0200474 if [ "$CLASS" = "ETC" ] ; then
475 if [ "$DIRNAME" != "." ]; then
476 printf '\tsub_dir: "%s",\n' "$DIRNAME"
477 fi
478 fi
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700479 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
480 printf '\tprefer: true,\n'
481 fi
482 if [ "$EXTRA" = "priv-app" ]; then
483 printf '\tprivileged: true,\n'
484 fi
485 if [ "$PARTITION" = "vendor" ]; then
486 printf '\tsoc_specific: true,\n'
487 elif [ "$PARTITION" = "product" ]; then
488 printf '\tproduct_specific: true,\n'
Luca Stefani0409f232020-09-09 15:53:58 +0200489 elif [ "$PARTITION" = "system_ext" ]; then
490 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700491 elif [ "$PARTITION" = "odm" ]; then
492 printf '\tdevice_specific: true,\n'
493 fi
494 printf '}\n\n'
495 done
496}
497
498#
499# write_makefile_packages:
500#
501# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani0409f232020-09-09 15:53:58 +0200502# $2: /odm, /product, /system_ext, or /vendor partition
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700503# $3: type-specific extra flags
504# $4: Name of the array holding the target list
505#
506# Internal function which writes out the BUILD_PREBUILT stanzas
507# for all modules in the list. This is called by write_product_packages
508# after the modules are categorized.
509#
510function write_makefile_packages() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700511
512 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500513 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700514 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700515
516 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700517 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700518 local FILELIST=("${!ARR_NAME}")
519
520 local FILE=
521 local ARGS=
522 local BASENAME=
523 local EXTENSION=
524 local PKGNAME=
525 local SRC=
526
527 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300528 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700529 ARGS=$(target_args "$P")
530
531 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100532 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700533 EXTENSION=${BASENAME##*.}
Mohd Farazd1d72352019-10-08 16:13:50 +0530534 EXTENSION="."$EXTENSION
535 if [ "$EXTENSION" = ".jar" ]; then
536 EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
537 elif [ "$EXTENSION" = ".apk" ]; then
538 EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
539 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700540 PKGNAME=${BASENAME%.*}
541
542 # Add to final package list
543 PACKAGE_LIST+=("$PKGNAME")
544
545 SRC="proprietary"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400546 if [ "$PARTITION" = "system" ]; then
547 SRC+="/system"
548 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700549 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500550 elif [ "$PARTITION" = "product" ]; then
551 SRC+="/product"
Luca Stefani0409f232020-09-09 15:53:58 +0200552 elif [ "$PARTITION" = "system_ext" ]; then
553 SRC+="/system_ext"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700554 elif [ "$PARTITION" = "odm" ]; then
555 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700556 fi
557
558 printf 'include $(CLEAR_VARS)\n'
559 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
560 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
561 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700562 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700563 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
564 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
565 #if [ "$VENDOR_PKG" = "true" ]; then
566 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
567 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
568 #else
569 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
570 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
571 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700572 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700573 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
574 else
575 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
576 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700577 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700578 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700579 fi
580 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200581 if [ "$EXTRA" = "priv-app" ]; then
582 SRC="$SRC/priv-app"
583 else
584 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700585 fi
586 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
587 local CERT=platform
588 if [ ! -z "$ARGS" ]; then
589 CERT="$ARGS"
590 fi
591 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
592 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
593 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200594 local CERT=platform
595 if [ ! -z "$ARGS" ]; then
596 CERT="$ARGS"
597 fi
598 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700599 elif [ "$CLASS" = "ETC" ]; then
600 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
601 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700602 if [ "$ARGS" = "rootfs" ]; then
603 SRC="$SRC/rootfs"
604 if [ "$EXTRA" = "sbin" ]; then
605 SRC="$SRC/sbin"
606 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
607 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
608 fi
609 else
610 SRC="$SRC/bin"
611 fi
612 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
613 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700614 else
Steve Kondika991cf12016-07-28 12:13:12 -0700615 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700616 fi
617 printf 'LOCAL_MODULE_TAGS := optional\n'
618 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400619 if [ "$CLASS" = "APPS" ]; then
620 printf 'LOCAL_DEX_PREOPT := false\n'
621 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700622 if [ ! -z "$EXTENSION" ]; then
Mohd Farazd1d72352019-10-08 16:13:50 +0530623 printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
Steve Kondika991cf12016-07-28 12:13:12 -0700624 fi
M1cha15f226c2017-01-04 09:00:11 +0100625 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
626 if [ "$DIRNAME" != "." ]; then
627 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
628 fi
629 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700630 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700631 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
632 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500633 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800634 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500635 elif [ "$PARTITION" = "product" ]; then
636 printf 'LOCAL_PRODUCT_MODULE := true\n'
Luca Stefani0409f232020-09-09 15:53:58 +0200637 elif [ "$PARTITION" = "system_ext" ]; then
638 printf 'LOCAL_SYSTEM_EXT_MODULE := true\n'
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700639 elif [ "$PARTITION" = "odm" ]; then
640 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700641 fi
642 printf 'include $(BUILD_PREBUILT)\n\n'
643 done
644}
645
646#
647# write_product_packages:
648#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700649# This function will create prebuilt entries in the
650# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700651# product makefile for all files in the blob list which
652# start with a single dash (-) character.
653#
654function write_product_packages() {
655 PACKAGE_LIST=()
656
657 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
658
659 if [ "$COUNT" = "0" ]; then
660 return 0
661 fi
662
663 # Figure out what's 32-bit, what's 64-bit, and what's multilib
664 # I really should not be doing this in bash due to shitty array passing :(
665 local T_LIB32=( $(prefix_match "lib/") )
666 local T_LIB64=( $(prefix_match "lib64/") )
667 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
668 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700669 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700670
Steve Kondik03ce4002016-07-29 00:00:16 -0700671 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700672 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700673 fi
674 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700675 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700676 fi
677 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700678 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700679 fi
680
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400681 local T_S_LIB32=( $(prefix_match "system/lib/") )
682 local T_S_LIB64=( $(prefix_match "system/lib64/") )
683 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
684 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
685 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
686
687 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700688 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400689 fi
690 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700691 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400692 fi
693 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700694 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400695 fi
696
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700697 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
698 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
699 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
700 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700701 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700702
703 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700704 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700705 fi
706 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700707 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700708 fi
709 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700710 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500711 fi
712
713 local T_P_LIB32=( $(prefix_match "product/lib/") )
714 local T_P_LIB64=( $(prefix_match "product/lib64/") )
715 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
716 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
717 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
718
719 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700720 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500721 fi
722 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700723 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500724 fi
725 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700726 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700727 fi
728
Luca Stefani0409f232020-09-09 15:53:58 +0200729 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
730 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
731 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
732 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
733 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
734
735 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
736 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
737 fi
738 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
739 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
740 fi
741 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
742 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
743 fi
744
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700745 local T_O_LIB32=( $(prefix_match "odm/lib/") )
746 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
747 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
748 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
749 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
750
751 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700752 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700753 fi
754 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700755 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700756 fi
757 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700758 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700759 fi
760
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700761 # Apps
762 local APPS=( $(prefix_match "app/") )
763 if [ "${#APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700764 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700765 fi
766 local PRIV_APPS=( $(prefix_match "priv-app/") )
767 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700768 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700769 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400770 local S_APPS=( $(prefix_match "system/app/") )
771 if [ "${#S_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700772 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400773 fi
774 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
775 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700776 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400777 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700778 local V_APPS=( $(prefix_match "vendor/app/") )
779 if [ "${#V_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700780 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700781 fi
782 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
783 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700784 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500785 fi
786 local P_APPS=( $(prefix_match "product/app/") )
787 if [ "${#P_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700788 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500789 fi
790 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
791 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700792 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700793 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200794 local SE_APPS=( $(prefix_match "system_ext/app/") )
795 if [ "${#SE_APPS[@]}" -gt "0" ]; then
796 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
797 fi
798 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
799 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
800 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
801 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700802 local O_APPS=( $(prefix_match "odm/app/") )
803 if [ "${#O_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700804 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700805 fi
806 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
807 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700808 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700809 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700810
811 # Framework
812 local FRAMEWORK=( $(prefix_match "framework/") )
813 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700814 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700815 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400816 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
817 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700818 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400819 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200820 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200821 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700822 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500823 fi
824 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
825 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700826 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oderc16f3272017-10-08 23:15:52 +0200827 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200828 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich411066c2020-09-16 17:58:53 -0700829 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani0409f232020-09-09 15:53:58 +0200830 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
831 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700832 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
833 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700834 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700835 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700836
837 # Etc
838 local ETC=( $(prefix_match "etc/") )
839 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700840 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700841 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400842 local S_ETC=( $(prefix_match "system/etc/") )
843 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700844 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400845 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700846 local V_ETC=( $(prefix_match "vendor/etc/") )
847 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700848 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500849 fi
850 local P_ETC=( $(prefix_match "product/etc/") )
851 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700852 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700853 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200854 local SE_ETC=( $(prefix_match "system_ext/etc/") )
855 if [ "${#SE_ETC[@]}" -gt "0" ]; then
856 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
857 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700858 local O_ETC=( $(prefix_match "odm/etc/") )
859 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700860 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700861 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700862
863 # Executables
864 local BIN=( $(prefix_match "bin/") )
865 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700866 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700867 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400868 local S_BIN=( $(prefix_match "system/bin/") )
869 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700870 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400871 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700872 local V_BIN=( $(prefix_match "vendor/bin/") )
873 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700874 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500875 fi
876 local P_BIN=( $(prefix_match "product/bin/") )
877 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700878 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700879 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200880 local SE_BIN=( $(prefix_match "system_ext/bin/") )
881 if [ "${#SE_BIN[@]}" -gt "0" ]; then
882 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
883 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700884 local O_BIN=( $(prefix_match "odm/bin/") )
885 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700886 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700887 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700888 local SBIN=( $(prefix_match "sbin/") )
889 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700890 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700891 fi
892
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700893
894 # Actually write out the final PRODUCT_PACKAGES list
895 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
896
897 if [ "$PACKAGE_COUNT" -eq "0" ]; then
898 return 0
899 fi
900
901 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
902 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
903 local LINEEND=" \\"
904 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
905 LINEEND=""
906 fi
907 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
908 done
909}
910
911#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700912# write_blueprint_header:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700913#
914# $1: file which will be written to
915#
916# writes out the copyright header with the current year.
917# note that this is not an append operation, and should
918# be executed first!
919#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700920function write_blueprint_header() {
921 if [ -f $1 ]; then
922 rm $1
923 fi
924
925 YEAR=$(date +"%Y")
926
927 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
928
929 printf "/**\n" > $1
930 NUM_REGEX='^[0-9]+$'
931 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
932 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
933 else
934 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
935 fi
936
937 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
938 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
939 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
940 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
941 else
942 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
943 fi
944
945 cat << EOF >> $1
946 *
947 * Licensed under the Apache License, Version 2.0 (the "License");
948 * you may not use this file except in compliance with the License.
949 * You may obtain a copy of the License at
950 *
951 * http://www.apache.org/licenses/LICENSE-2.0
952 *
953 * Unless required by applicable law or agreed to in writing, software
954 * distributed under the License is distributed on an "AS IS" BASIS,
955 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
956 * See the License for the specific language governing permissions and
957 * limitations under the License.
958 *
959 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
960 */
961
962EOF
963}
964
965#
966# write_makefile_header:
967#
968# $1: file which will be written to
969#
970# writes out the copyright header with the current year.
971# note that this is not an append operation, and should
972# be executed first!
973#
974function write_makefile_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600975 if [ -f $1 ]; then
976 rm $1
977 fi
978
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700979 YEAR=$(date +"%Y")
980
981 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
982
Matt Mower8945f5e2017-01-07 14:08:17 -0600983 NUM_REGEX='^[0-9]+$'
984 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
985 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
986 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
987 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
988 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
989 fi
990 if [ $YEAR -eq 2017 ]; then
991 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
992 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
993 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
994 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
995 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
996 else
997 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
998 fi
999 else
1000 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
1001 fi
1002
1003 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001004#
1005# Licensed under the Apache License, Version 2.0 (the "License");
1006# you may not use this file except in compliance with the License.
1007# You may obtain a copy of the License at
1008#
1009# http://www.apache.org/licenses/LICENSE-2.0
1010#
1011# Unless required by applicable law or agreed to in writing, software
1012# distributed under the License is distributed on an "AS IS" BASIS,
1013# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1014# See the License for the specific language governing permissions and
1015# limitations under the License.
1016
1017# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1018
1019EOF
1020}
1021
1022#
1023# write_headers:
1024#
1025# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001026# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001027#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001028# Calls write_makefile_header for each of the makefiles and
1029# write_blueprint_header for Android.bp and creates the initial
1030# path declaration and device guard for the Android.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001031#
1032function write_headers() {
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001033 write_makefile_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001034
1035 GUARD="$2"
1036 if [ -z "$GUARD" ]; then
1037 GUARD="TARGET_DEVICE"
1038 fi
1039
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001040 cat << EOF >> "$ANDROIDMK"
1041LOCAL_PATH := \$(call my-dir)
1042
1043EOF
1044 if [ "$COMMON" -ne 1 ]; then
1045 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001046ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001047
1048EOF
1049 else
1050 if [ -z "$1" ]; then
1051 echo "Argument with devices to be added to guard must be set!"
1052 exit 1
1053 fi
1054 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001055ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001056
1057EOF
1058 fi
1059
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001060 write_makefile_header "$BOARDMK"
1061 write_makefile_header "$PRODUCTMK"
1062 write_blueprint_header "$ANDROIDBP"
1063
1064 cat << EOF >> "$ANDROIDBP"
1065soong_namespace {
1066}
1067
1068EOF
1069
1070 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1071 cat << EOF >> "$PRODUCTMK"
1072PRODUCT_SOONG_NAMESPACES += \\
1073 vendor/$VENDOR/$DEVICE
1074
1075EOF
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001076}
1077
1078#
1079# write_footers:
1080#
1081# Closes the inital guard and any other finalization tasks. Must
1082# be called as the final step.
1083#
1084function write_footers() {
1085 cat << EOF >> "$ANDROIDMK"
1086endif
1087EOF
1088}
1089
1090# Return success if adb is up and not in recovery
1091function _adb_connected {
1092 {
Steve Kondik7561d192016-09-01 21:40:27 -07001093 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001094 then
1095 return 0
1096 fi
1097 } 2>/dev/null
1098
1099 return 1
1100};
1101
1102#
Bruno Martins3b96ba52016-07-27 15:00:05 +01001103# parse_file_list:
1104#
1105# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001106# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +01001107#
1108# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001109#
1110function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +01001111 if [ -z "$1" ]; then
1112 echo "An input file is expected!"
1113 exit 1
1114 elif [ ! -f "$1" ]; then
1115 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001116 exit 1
1117 fi
1118
Vladimir Olteanc5034462019-01-17 03:04:16 +02001119 if [ -n "$2" ]; then
1120 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001121 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +02001122 # Match all lines starting with first line found to start* with '#'
1123 # comment and contain** $2, and ending with first line to be empty*.
1124 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1125 # **the $2 match is case-insensitive
1126 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001127 else
1128 LIST=$1
1129 fi
1130
1131
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001132 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001133 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001134 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001135 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001136 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001137 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001138
1139 while read -r line; do
1140 if [ -z "$line" ]; then continue; fi
1141
Steve Kondik48f8df82016-08-14 03:55:08 -07001142 # If the line has a pipe delimiter, a sha1 hash should follow.
1143 # This indicates the file should be pinned and not overwritten
1144 # when extracting files.
1145 local SPLIT=(${line//\|/ })
1146 local COUNT=${#SPLIT[@]}
1147 local SPEC=${SPLIT[0]}
1148 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001149 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -07001150 if [ "$COUNT" -gt "1" ]; then
1151 HASH=${SPLIT[1]}
1152 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001153 if [ "$COUNT" -gt "2" ]; then
1154 FIXUP_HASH=${SPLIT[2]}
1155 fi
Steve Kondik48f8df82016-08-14 03:55:08 -07001156
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001157 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -07001158 if [[ "$SPEC" =~ ^- ]]; then
1159 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1160 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001161 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001162 else
Steve Kondik48f8df82016-08-14 03:55:08 -07001163 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1164 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001165 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001166 fi
1167
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001168 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001169}
1170
1171#
1172# write_makefiles:
1173#
1174# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001175# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001176#
1177# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001178# the given file and appends to the Android.bp as well as
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001179# the product makefile.
1180#
1181function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001182 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001183 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001184 write_product_packages
1185}
1186
1187#
Louis Popia516c2f2016-07-25 15:51:13 +02001188# append_firmware_calls_to_makefiles:
1189#
1190# Appends to Android.mk the calls to all images present in radio folder
1191# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1192#
1193function append_firmware_calls_to_makefiles() {
1194 cat << EOF >> "$ANDROIDMK"
1195ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1196
1197RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1198\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1199 \$(call add-radio-file,radio/\$(f)))
1200\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1201
1202endif
1203
1204EOF
1205}
1206
1207#
Luca Stefani7f9fff22016-07-18 13:47:55 +02001208# get_file:
1209#
1210# $1: input file
1211# $2: target file/folder
1212# $3: source of the file (can be "adb" or a local folder)
1213#
1214# Silently extracts the input file to defined target
1215# Returns success if file can be pulled from the device or found locally
1216#
1217function get_file() {
1218 local SRC="$3"
1219
1220 if [ "$SRC" = "adb" ]; then
1221 # try to pull
1222 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1223
1224 return 1
1225 else
1226 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +03001227 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1228 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +02001229 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +02001230
1231 return 1
1232 fi
1233};
1234
1235#
1236# oat2dex:
1237#
1238# $1: extracted apk|jar (to check if deodex is required)
1239# $2: odexed apk|jar to deodex
1240# $3: source of the odexed apk|jar
1241#
1242# Convert apk|jar .odex in the corresposing classes.dex
1243#
1244function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001245 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001246 local OEM_TARGET="$2"
1247 local SRC="$3"
1248 local TARGET=
1249 local OAT=
XiNGRZ4a2b65f2019-12-24 10:37:13 +08001250 local HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001251
1252 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +01001253 export BAKSMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/baksmali.jar
1254 export SMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +02001255 fi
1256
Joe Maples9be579f2018-01-05 14:51:33 -05001257 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wangae82c342020-03-10 09:40:47 +02001258 export VDEXEXTRACTOR="$LINEAGE_ROOT"/prebuilts/tools-lineage/${HOST}-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -05001259 fi
1260
codeworkx1c29bf62018-09-23 12:36:57 +02001261 if [ -z "$CDEXCONVERTER" ]; then
Han Wangae82c342020-03-10 09:40:47 +02001262 export CDEXCONVERTER="$LINEAGE_ROOT"/prebuilts/tools-lineage/${HOST}-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +02001263 fi
1264
Luca Stefani7f9fff22016-07-18 13:47:55 +02001265 # Extract existing boot.oats to the temp folder
1266 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001267 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +02001268 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001269 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +03001270 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001271 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001272 else
1273 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001274 fi
1275 done
1276 fi
1277
1278 if [ -z "$ARCHES" ]; then
1279 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1280 fi
1281
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001282 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -07001283 return;
1284 fi
1285
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001286 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001287 return 0 # target apk|jar is already odexed, return
1288 fi
1289
1290 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001291 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001292
1293 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001294 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001295
1296 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001297 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -05001298 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001299 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1300 for CLASS in $CLASSES; do
1301 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1302 # Check if we have to deal with CompactDex
1303 if [[ "$CLASS" == *.cdex ]]; then
1304 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1305 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1306 else
1307 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1308 fi
1309 done
Joe Maples9be579f2018-01-05 14:51:33 -05001310 else
1311 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1312 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001313 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001314 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001315 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001316 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001317 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001318 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001319 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001320 # try to extract classes.dex from boot.vdex for frameworks jars
1321 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001322 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001323 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001324 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1325 for CLASS in $CLASSES; do
1326 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1327 # Check if we have to deal with CompactDex
1328 if [[ "$CLASS" == *.cdex ]]; then
1329 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1330 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1331 else
1332 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1333 fi
1334 done
Joe Maples9be579f2018-01-05 14:51:33 -05001335 else
1336 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1337 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1338 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001339 else
1340 continue
1341 fi
1342
Luca Stefani7f9fff22016-07-18 13:47:55 +02001343 done
1344
1345 rm -rf "$TMPDIR/dexout"
1346}
1347
1348#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001349# init_adb_connection:
1350#
1351# Starts adb server and waits for the device
1352#
1353function init_adb_connection() {
1354 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1355 if ! _adb_connected; then
1356 echo "No device is online. Waiting for one..."
1357 echo "Please connect USB and/or enable USB debugging"
1358 until _adb_connected; do
1359 sleep 1
1360 done
1361 echo "Device Found."
1362 fi
1363
1364 # Retrieve IP and PORT info if we're using a TCP connection
1365 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1366 | head -1 | awk '{print $1}')
1367 adb root &> /dev/null
1368 sleep 0.3
1369 if [ -n "$TCPIPPORT" ]; then
1370 # adb root just killed our connection
1371 # so reconnect...
1372 adb connect "$TCPIPPORT"
1373 fi
1374 adb wait-for-device &> /dev/null
1375 sleep 0.3
1376}
1377
1378#
Luca Stefani3a030122016-07-30 12:08:25 +02001379# fix_xml:
1380#
1381# $1: xml file to fix
1382#
1383function fix_xml() {
1384 local XML="$1"
1385 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1386
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001387 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1388 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001389
1390 mv "$TEMP_XML" "$XML"
1391}
1392
Vladimir Oltean4818c232019-01-17 03:07:34 +02001393function get_hash() {
1394 local FILE="$1"
1395
1396 if [ "$(uname)" == "Darwin" ]; then
1397 shasum "${FILE}" | awk '{print $1}'
1398 else
1399 sha1sum "${FILE}" | awk '{print $1}'
1400 fi
1401}
1402
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001403function print_spec() {
1404 local SPEC_PRODUCT_PACKAGE="$1"
1405 local SPEC_SRC_FILE="$2"
1406 local SPEC_DST_FILE="$3"
1407 local SPEC_ARGS="$4"
1408 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001409 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001410
1411 local PRODUCT_PACKAGE=""
1412 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1413 PRODUCT_PACKAGE="-"
1414 fi
1415 local SRC=""
1416 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1417 SRC="${SPEC_SRC_FILE}:"
1418 fi
1419 local DST=""
1420 if [ ! -z "${SPEC_DST_FILE}" ]; then
1421 DST="${SPEC_DST_FILE}"
1422 fi
1423 local ARGS=""
1424 if [ ! -z "${SPEC_ARGS}" ]; then
1425 ARGS=";${SPEC_ARGS}"
1426 fi
1427 local HASH=""
1428 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1429 HASH="|${SPEC_HASH}"
1430 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001431 local FIXUP_HASH=""
1432 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1433 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1434 fi
1435 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1436}
1437
1438# To be overridden by device-level extract-files.sh
1439# Parameters:
1440# $1: spec name of a blob. Can be used for filtering.
1441# If the spec is "src:dest", then $1 is "dest".
1442# If the spec is "src", then $1 is "src".
1443# $2: path to blob file. Can be used for fixups.
1444#
1445function blob_fixup() {
1446 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001447}
1448
Luca Stefani3a030122016-07-30 12:08:25 +02001449#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001450# extract:
1451#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001452# Positional parameters:
1453# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001454# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001455# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1456#
1457# Non-positional parameters (coming after $2):
1458# --section: preferred way of selecting the portion to parse and extract from
1459# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001460# --kang: if present, this option will activate the printing of hashes for the
1461# extracted blobs. Useful with --section for subsequent pinning of
1462# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001463#
1464function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001465 # Consume positional parameters
1466 local PROPRIETARY_FILES_TXT="$1"; shift
1467 local SRC="$1"; shift
1468 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001469 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001470
1471 # Consume optional, non-positional parameters
1472 while [ "$#" -gt 0 ]; do
1473 case "$1" in
1474 -s|--section)
1475 SECTION="$2"; shift
1476 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001477 -k|--kang)
1478 KANG=true
1479 DISABLE_PINNING=1
1480 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001481 *)
1482 # Backwards-compatibility with the old behavior, where $3, if
1483 # present, denoted an optional positional ${SECTION} argument.
1484 # Users of ${SECTION} are encouraged to migrate from setting it as
1485 # positional $3, to non-positional --section ${SECTION}, the
1486 # reason being that it doesn't scale to have more than 1 optional
1487 # positional argument.
1488 SECTION="$1"
1489 ;;
1490 esac
1491 shift
1492 done
1493
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001494 if [ -z "$OUTDIR" ]; then
1495 echo "Output dir not set!"
1496 exit 1
1497 fi
1498
Vladimir Olteanc5034462019-01-17 03:04:16 +02001499 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001500
1501 # Allow failing, so we can try $DEST and/or $FILE
1502 set +e
1503
1504 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001505 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001506 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001507 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001508 local COUNT=${#FILELIST[@]}
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001509 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001510 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1511
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001512 if [ "$SRC" = "adb" ]; then
1513 init_adb_connection
1514 fi
1515
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001516 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001517 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001518
1519 # Check if we're working with the same zip that was passed last time.
1520 # If so, let's just use what's already extracted.
1521 MD5=`md5sum "$SRC"| awk '{print $1}'`
1522 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1523
1524 if [ "$MD5" != "$OLDMD5" ]; then
1525 rm -rf "$DUMPDIR"
1526 mkdir "$DUMPDIR"
1527 unzip "$SRC" -d "$DUMPDIR"
1528 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1529
1530 # Stop if an A/B OTA zip is detected. We cannot extract these.
1531 if [ -a "$DUMPDIR"/payload.bin ]; then
1532 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1533 exit 1
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001534 fi
dianlujitaofc486342020-04-21 23:03:20 +08001535
Luca Stefani0409f232020-09-09 15:53:58 +02001536 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitaofc486342020-04-21 23:03:20 +08001537 do
1538 # If OTA is block based, extract it.
dianlujitaoee95f242020-04-21 23:01:13 +08001539 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1540 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1541 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1542 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1543 fi
dianlujitaofc486342020-04-21 23:03:20 +08001544 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1545 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1546 python "$LINEAGE_ROOT"/vendor/lineage/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1547 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1548 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1549 echo "Requesting sudo access to mount the "$PARTITION".img"
1550 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1551 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1552 sudo umount "$DUMPDIR"/tmp
1553 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1554 fi
1555 done
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001556 fi
1557
1558 SRC="$DUMPDIR"
1559 fi
1560
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001561 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001562 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001563 rm -rf "${OUTPUT_TMP:?}"
1564 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001565 if [ -d "$OUTPUT_ROOT" ]; then
1566 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1567 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001568 VENDOR_STATE=1
1569 fi
1570
Vladimir Olteanc5034462019-01-17 03:04:16 +02001571 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001572
1573 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001574
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001575 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001576 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001577 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001578 local OUTPUT_DIR=
1579 local TMP_DIR=
1580 local SRC_FILE=
1581 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001582 local IS_PRODUCT_PACKAGE=false
1583
1584 # Note: this relies on the fact that the ${FILELIST[@]} array
1585 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1586 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1587 IS_PRODUCT_PACKAGE=true
1588 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001589
Vladimir Olteand652a062018-06-24 20:42:01 +03001590 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001591 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1592 TMP_DIR="${OUTPUT_TMP}/rootfs"
1593 SRC_FILE="/${SPEC_SRC_FILE}"
1594 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001595 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001596 OUTPUT_DIR="${OUTPUT_ROOT}"
1597 TMP_DIR="${OUTPUT_TMP}"
1598 SRC_FILE="/system/${SPEC_SRC_FILE}"
1599 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001600 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001601
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001602 # Strip the file path in the vendor repo of "system", if present
Vladimir Olteanc5034462019-01-17 03:04:16 +02001603 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitaodb1caf42020-04-06 12:43:16 +08001604 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001605 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001606
Gabriele Me6df25b2017-10-11 00:58:59 +02001607 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001608 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001609 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001610 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001611 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001612 if [ -f "${VENDOR_REPO_FILE}" ]; then
1613 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001614 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001615 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001616 fi
1617 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001618 local TMP_HASH=$(get_hash "${PINNED}")
1619 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001620 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001621 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1622 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001623 fi
1624 fi
1625 fi
1626 fi
1627
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001628 if [ "${KANG}" = false ]; then
1629 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1630 fi
1631
Gabriele Me6df25b2017-10-11 00:58:59 +02001632 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001633 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001634 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001635 FOUND=false
1636 # Try Lineage target first.
1637 # Also try to search for files stripped of
1638 # the "/system" prefix, if we're actually extracting
1639 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001640 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001641 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1642 FOUND=true
1643 break
1644 }
1645 done
1646
1647 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001648 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001649 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001650 fi
1651 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001652
Vladimir Oltean4818c232019-01-17 03:07:34 +02001653 # Blob fixup pipeline has 2 parts: one that is fixed and
1654 # one that is user-configurable
1655 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1656 # Deodex apk|jar if that's the case
1657 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1658 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1659 if [ -f "$TMPDIR/classes.dex" ]; then
dianlujitao0b501d52020-04-06 12:45:36 +08001660 touch -t 200901010000 "$TMPDIR/classes"*
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001661 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1662 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001663 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001664 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001665 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1666 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001667 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001668 # Now run user-supplied fixup function
1669 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1670 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001671
Vladimir Olteand6747712018-06-24 20:46:42 +03001672 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001673 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001674 local TYPE="${DIR##*/}"
1675 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001676 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001677 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001678 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001679 fi
1680 fi
1681
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001682 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001683 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1684 fi
1685
1686 # Check and print whether the fixup pipeline actually did anything.
1687 # This isn't done right after the fixup pipeline because we want this print
1688 # to come after print_spec above, when in kang mode.
1689 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1690 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1691 # Now sanity-check the spec for this blob.
1692 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1693 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1694 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1695 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001696 fi
1697
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001698 done
1699
1700 # Don't allow failing
1701 set -e
1702}
Louis Popia516c2f2016-07-25 15:51:13 +02001703
1704#
1705# extract_firmware:
1706#
1707# $1: file containing the list of items to extract
1708# $2: path to extracted radio folder
1709#
1710function extract_firmware() {
1711 if [ -z "$OUTDIR" ]; then
1712 echo "Output dir not set!"
1713 exit 1
1714 fi
1715
1716 parse_file_list "$1"
1717
1718 # Don't allow failing
1719 set -e
1720
1721 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1722 local COUNT=${#FILELIST[@]}
1723 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001724 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001725
1726 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1727 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1728 rm -rf "${OUTPUT_DIR:?}/"*
1729 VENDOR_RADIO_STATE=1
1730 fi
1731
1732 echo "Extracting $COUNT files in $1 from $SRC:"
1733
1734 for (( i=1; i<COUNT+1; i++ )); do
1735 local FILE="${FILELIST[$i-1]}"
1736 printf ' - %s \n' "/radio/$FILE"
1737
1738 if [ ! -d "$OUTPUT_DIR" ]; then
1739 mkdir -p "$OUTPUT_DIR"
1740 fi
1741 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1742 chmod 644 "$OUTPUT_DIR/$FILE"
1743 done
1744}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001745
1746function extract_img_data() {
1747 local image_file="$1"
1748 local out_dir="$2"
1749 local logFile="$TMPDIR/debugfs.log"
1750
1751 if [ ! -d "$out_dir" ]; then
1752 mkdir -p "$out_dir"
1753 fi
1754
1755 if [[ "$HOST_OS" == "Darwin" ]]; then
1756 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1757 echo "[-] Failed to extract data from '$image_file'"
1758 abort 1
1759 }
1760 else
1761 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1762 do
1763 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1764 echo "[-] Failed to extract data from '$image_file'"
1765 abort 1
1766 }
1767 done
1768 fi
1769
1770 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1771 if grep -Fq "$symlink_err" "$logFile"; then
1772 echo "[-] Symlinks have not been properly processed from $image_file"
1773 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1774 abort 1
1775 fi
1776}
1777
1778declare -ra VENDOR_SKIP_FILES=(
1779 "bin/toybox_vendor"
1780 "bin/toolbox"
1781 "bin/grep"
1782 "build.prop"
1783 "compatibility_matrix.xml"
1784 "default.prop"
1785 "etc/NOTICE.xml.gz"
1786 "etc/vintf/compatibility_matrix.xml"
1787 "etc/vintf/manifest.xml"
1788 "etc/wifi/wpa_supplicant.conf"
1789 "manifest.xml"
1790 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1791 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1792 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1793 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1794 "overlay/framework-res__auto_generated_rro.apk"
1795 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1796)
1797
1798function array_contains() {
1799 local element
1800 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1801 return 1
1802}
1803
1804function generate_prop_list_from_image() {
1805 local image_file="$1"
1806 local image_dir="$TMPDIR/image-temp"
1807 local output_list="$2"
1808 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1809 local -n skipped_vendor_files="$3"
1810
1811 extract_img_data "$image_file" "$image_dir"
1812
1813 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1814 do
1815 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1816 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1817 continue
1818 fi
1819 # Skip device defined skipped files since they will be re-generated at build time
1820 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1821 continue
1822 fi
1823 if suffix_match_file ".apk" "$FILE" ; then
1824 echo "-vendor/$FILE" >> "$output_list_tmp"
1825 else
1826 echo "vendor/$FILE" >> "$output_list_tmp"
1827 fi
1828 done
1829
1830 # Sort merged file with all lists
1831 sort -u "$output_list_tmp" > "$output_list"
1832
1833 # Clean-up
1834 rm -f "$output_list_tmp"
1835}