blob: 2d37a8775a7e70814710841391219dca9dab1747 [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
dianlujitao37e088a2020-09-12 00:15:13 +08004# 2017-2020 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)
Sebastiano Barezzi136c9bb2020-12-23 16:35:43 +010033HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070034
35#
Steve Kondik48f8df82016-08-14 03:55:08 -070036# cleanup
37#
38# kill our tmpfiles with fire on exit
39#
40function cleanup() {
41 rm -rf "${TMPDIR:?}"
42}
43
Gabriele M6c3c2c02017-10-11 12:55:51 +020044trap cleanup 0
Steve Kondik48f8df82016-08-14 03:55:08 -070045
46#
Steve Kondik4e2aaab2016-07-15 10:39:58 -070047# setup_vendor
48#
49# $1: device name
50# $2: vendor name
Jackeagled6811aa2019-09-24 08:26:40 +020051# $3: Bliss root directory
Steve Kondik4e2aaab2016-07-15 10:39:58 -070052# $4: is common device - optional, default to false
53# $5: cleanup - optional, default to true
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040054# $6: custom vendor makefile name - optional, default to false
Steve Kondik4e2aaab2016-07-15 10:39:58 -070055#
56# Must be called before any other functions can be used. This
57# sets up the internal state for a new vendor configuration.
58#
59function setup_vendor() {
60 local DEVICE="$1"
61 if [ -z "$DEVICE" ]; then
62 echo "\$DEVICE must be set before including this script!"
63 exit 1
64 fi
65
66 export VENDOR="$2"
67 if [ -z "$VENDOR" ]; then
68 echo "\$VENDOR must be set before including this script!"
69 exit 1
70 fi
71
Jackeagled6811aa2019-09-24 08:26:40 +020072 export BLISS_ROOT="$3"
73 if [ ! -d "$BLISS_ROOT" ]; then
74 echo "\$BLISS_ROOT must be set and valid before including this script!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070075 exit 1
76 fi
77
78 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
Jackeagled6811aa2019-09-24 08:26:40 +020079 if [ ! -d "$BLISS_ROOT/$OUTDIR" ]; then
80 mkdir -p "$BLISS_ROOT/$OUTDIR"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070081 fi
82
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040083 VNDNAME="$6"
84 if [ -z "$VNDNAME" ]; then
85 VNDNAME="$DEVICE"
86 fi
87
Jackeagled6811aa2019-09-24 08:26:40 +020088 export PRODUCTMK="$BLISS_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
89 export ANDROIDBP="$BLISS_ROOT"/"$OUTDIR"/Android.bp
90 export ANDROIDMK="$BLISS_ROOT"/"$OUTDIR"/Android.mk
91 export BOARDMK="$BLISS_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -070092
93 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
94 COMMON=1
95 else
96 COMMON=0
97 fi
98
Gabriele Mb6effb32017-05-01 18:22:04 +020099 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700100 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +0200101 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700102 else
103 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +0200104 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700105 fi
Sebastiano Barezzi136c9bb2020-12-23 16:35:43 +0100106
107 if [ -z "$PATCHELF" ]; then
108 export PATCHELF="$LINEAGE_ROOT"/prebuilts/tools-lineage/${HOST}-x86/bin/patchelf
109 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700110}
111
Vladimir Oltean95643282018-06-24 20:22:41 +0300112# Helper functions for parsing a spec.
113# notes: an optional "|SHA1" that may appear in the format is stripped
114# early from the spec in the parse_file_list function, and
115# should not be present inside the input parameter passed
116# to these functions.
117
118#
119# input: spec in the form of "src[:dst][;args]"
120# output: "src"
121#
122function src_file() {
123 local SPEC="$1"
124 local SPLIT=(${SPEC//:/ })
125 local ARGS="$(target_args ${SPEC})"
126 # Regardless of there being a ":" delimiter or not in the spec,
127 # the source file is always either the first, or the only entry.
128 local SRC="${SPLIT[0]}"
129 # Remove target_args suffix, if present
130 echo "${SRC%;${ARGS}}"
131}
132
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700133#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300134# input: spec in the form of "src[:dst][;args]"
135# output: "dst" if present, "src" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700136#
137function target_file() {
dianlujitao33ee5962020-01-02 15:26:44 +0800138 local SPEC="${1%%;*}"
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300139 local SPLIT=(${SPEC//:/ })
140 local ARGS="$(target_args ${SPEC})"
141 local DST=
142 case ${#SPLIT[@]} in
143 1)
144 # The spec doesn't have a : delimiter
145 DST="${SPLIT[0]}"
146 ;;
147 *)
148 # The spec actually has a src:dst format
149 DST="${SPLIT[1]}"
150 ;;
151 esac
152 # Remove target_args suffix, if present
153 echo "${DST%;${ARGS}}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700154}
155
156#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300157# input: spec in the form of "src[:dst][;args]"
158# output: "args" if present, "" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700159#
160function target_args() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300161 local SPEC="$1"
162 local SPLIT=(${SPEC//;/ })
163 local ARGS=
164 case ${#SPLIT[@]} in
165 1)
166 # No ";" delimiter in the spec.
167 ;;
168 *)
169 # The "args" are whatever comes after the ";" character.
170 # Basically the spec stripped of whatever is to the left of ";".
171 ARGS="${SPEC#${SPLIT[0]};}"
172 ;;
173 esac
174 echo "${ARGS}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700175}
176
177#
178# prefix_match:
179#
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300180# input:
181# - $1: prefix
182# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
183# output:
184# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700185#
186function prefix_match() {
187 local PREFIX="$1"
Vladimir Olteana48b9fe2018-04-02 22:37:09 +0300188 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
189 local FILE=$(target_file "$LINE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700190 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300191 local ARGS=$(target_args "$LINE")
192 if [ -z "${ARGS}" ]; then
193 echo "${FILE#$PREFIX}"
194 else
195 echo "${FILE#$PREFIX};${ARGS}"
196 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700197 fi
198 done
199}
200
201#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400202# prefix_match_file:
203#
204# $1: the prefix to match on
205# $2: the file to match the prefix for
206#
207# Internal function which returns true if a filename contains the
208# specified prefix.
209#
210function prefix_match_file() {
211 local PREFIX="$1"
212 local FILE="$2"
213 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
214 return 0
215 else
216 return 1
217 fi
218}
219
220#
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -0700221# suffix_match_file:
222#
223# $1: the suffix to match on
224# $2: the file to match the suffix for
225#
226# Internal function which returns true if a filename contains the
227# specified suffix.
228#
229function suffix_match_file() {
230 local SUFFIX="$1"
231 local FILE="$2"
232 if [[ "$FILE" = *"$SUFFIX" ]]; then
233 return 0
234 else
235 return 1
236 fi
237}
238
239#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400240# truncate_file
241#
242# $1: the filename to truncate
243# $2: the argument to output the truncated filename to
244#
245# Internal function which truncates a filename by removing the first dir
246# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
247#
248function truncate_file() {
249 local FILE="$1"
250 RETURN_FILE="$2"
251 local FIND="${FILE%%/*}"
252 local LOCATION="${#FIND}+1"
253 echo ${FILE:$LOCATION}
254}
255
256#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700257# write_product_copy_files:
258#
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400259# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400260#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700261# Creates the PRODUCT_COPY_FILES section in the product makefile for all
262# items in the list which do not start with a dash (-).
263#
264function write_product_copy_files() {
265 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
266 local TARGET=
267 local FILE=
268 local LINEEND=
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400269 local TREBLE_COMPAT=$1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700270
271 if [ "$COUNT" -eq "0" ]; then
272 return 0
273 fi
274
275 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
276 for (( i=1; i<COUNT+1; i++ )); do
277 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
278 LINEEND=" \\"
279 if [ "$i" -eq "$COUNT" ]; then
280 LINEEND=""
281 fi
282
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300283 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400284 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400285 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400286 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400287 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400288 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400289 local OUTTARGET=$(truncate_file $TARGET)
290 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
291 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani0409f232020-09-09 15:53:58 +0200292 elif prefix_match_file "system_ext/" $TARGET ; then
293 local OUTTARGET=$(truncate_file $TARGET)
294 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
295 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
296 elif prefix_match_file "system/system_ext/" $TARGET ; then
297 local OUTTARGET=$(truncate_file $TARGET)
298 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
299 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400300 elif prefix_match_file "odm/" $TARGET ; then
301 local OUTTARGET=$(truncate_file $TARGET)
302 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
303 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400304 elif prefix_match_file "vendor/odm/" $TARGET ; then
305 local OUTTARGET=$(truncate_file $TARGET)
306 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
307 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
308 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
309 local OUTTARGET=$(truncate_file $TARGET)
310 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
311 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
312 elif prefix_match_file "vendor/" $TARGET ; then
313 local OUTTARGET=$(truncate_file $TARGET)
314 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
315 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
316 elif prefix_match_file "system/vendor/" $TARGET ; then
317 local OUTTARGET=$(truncate_file $TARGET)
318 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
319 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400320 elif prefix_match_file "system/" $TARGET ; then
321 local OUTTARGET=$(truncate_file $TARGET)
322 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
323 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400324 else
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400325 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400326 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
327 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700328 done
329 return 0
330}
331
332#
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700333# write_blueprint_packages:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700334#
335# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani0409f232020-09-09 15:53:58 +0200336# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700337# $3: type-specific extra flags
338# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700339#
340# Internal function which writes out the BUILD_PREBUILT stanzas
341# for all modules in the list. This is called by write_product_packages
342# after the modules are categorized.
343#
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700344function write_blueprint_packages() {
345
346 local CLASS="$1"
347 local PARTITION="$2"
348 local EXTRA="$3"
349
350 # Yes, this is a horrible hack - we create a new array using indirection
351 local ARR_NAME="$4[@]"
352 local FILELIST=("${!ARR_NAME}")
353
354 local FILE=
355 local ARGS=
356 local BASENAME=
357 local EXTENSION=
358 local PKGNAME=
359 local SRC=
360
361 for P in "${FILELIST[@]}"; do
362 FILE=$(target_file "$P")
363 ARGS=$(target_args "$P")
364
365 BASENAME=$(basename "$FILE")
366 DIRNAME=$(dirname "$FILE")
367 EXTENSION=${BASENAME##*.}
368 PKGNAME=${BASENAME%.*}
369
370 # Add to final package list
371 PACKAGE_LIST+=("$PKGNAME")
372
373 SRC="proprietary"
374 if [ "$PARTITION" = "system" ]; then
375 SRC+="/system"
376 elif [ "$PARTITION" = "vendor" ]; then
377 SRC+="/vendor"
378 elif [ "$PARTITION" = "product" ]; then
379 SRC+="/product"
Luca Stefani0409f232020-09-09 15:53:58 +0200380 elif [ "$PARTITION" = "system_ext" ]; then
381 SRC+="/system_ext"
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700382 elif [ "$PARTITION" = "odm" ]; then
383 SRC+="/odm"
384 fi
385
386 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
387 printf 'cc_prebuilt_library_shared {\n'
388 printf '\tname: "%s",\n' "$PKGNAME"
389 printf '\towner: "%s",\n' "$VENDOR"
390 printf '\tstrip: {\n'
391 printf '\t\tnone: true,\n'
392 printf '\t},\n'
393 printf '\ttarget: {\n'
394 if [ "$EXTRA" = "both" ]; then
395 printf '\t\tandroid_arm: {\n'
396 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
397 printf '\t\t},\n'
398 printf '\t\tandroid_arm64: {\n'
399 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
400 printf '\t\t},\n'
401 elif [ "$EXTRA" = "64" ]; then
402 printf '\t\tandroid_arm64: {\n'
403 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
404 printf '\t\t},\n'
405 else
406 printf '\t\tandroid_arm: {\n'
407 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
408 printf '\t\t},\n'
409 fi
410 printf '\t},\n'
411 if [ "$EXTRA" != "none" ]; then
412 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
413 fi
dianlujitao37e088a2020-09-12 00:15:13 +0800414 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700415 elif [ "$CLASS" = "APPS" ]; then
416 printf 'android_app_import {\n'
417 printf '\tname: "%s",\n' "$PKGNAME"
418 printf '\towner: "%s",\n' "$VENDOR"
419 if [ "$EXTRA" = "priv-app" ]; then
420 SRC="$SRC/priv-app"
421 else
422 SRC="$SRC/app"
423 fi
424 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
425 if [ "$ARGS" = "PRESIGNED" ]; then
426 printf '\tpresigned: true,\n'
427 elif [ ! -z "$ARGS" ]; then
428 printf '\tcertificate: "%s",\n' "$ARGS"
429 else
430 printf '\tcertificate: "platform",\n'
431 fi
432 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
433 printf 'dex_import {\n'
434 printf '\tname: "%s",\n' "$PKGNAME"
435 printf '\towner: "%s",\n' "$VENDOR"
436 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
437 elif [ "$CLASS" = "ETC" ]; then
438 if [ "$EXTENSION" = "xml" ]; then
439 printf 'prebuilt_etc_xml {\n'
440 else
441 printf 'prebuilt_etc {\n'
442 fi
443 printf '\tname: "%s",\n' "$PKGNAME"
444 printf '\towner: "%s",\n' "$VENDOR"
445 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337c74d4b32020-10-06 19:29:02 +0200446 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700447 elif [ "$CLASS" = "EXECUTABLES" ]; then
448 if [ "$EXTENSION" = "sh" ]; then
449 printf 'sh_binary {\n'
450 else
451 printf 'cc_prebuilt_binary {\n'
452 fi
453 printf '\tname: "%s",\n' "$PKGNAME"
454 printf '\towner: "%s",\n' "$VENDOR"
455 if [ "$ARGS" = "rootfs" ]; then
456 SRC="$SRC/rootfs"
457 if [ "$EXTRA" = "sbin" ]; then
458 SRC="$SRC/sbin"
459 printf '\tdist {\n'
460 printf '\t\tdest: "%s",\n' "root/sbin"
461 printf '\t},'
462 fi
463 else
464 SRC="$SRC/bin"
465 fi
466 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
467 unset EXTENSION
468 else
469 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
470 fi
471 if [ "$CLASS" = "APPS" ]; then
472 printf '\tdex_preopt: {\n'
473 printf '\t\tenabled: false,\n'
474 printf '\t},\n'
475 fi
Andreas Schneiderb1d3f652020-05-25 17:03:17 +0200476 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700477 if [ "$DIRNAME" != "." ]; then
Andreas Schneidera7e400c2020-05-23 15:58:43 +0200478 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700479 fi
480 fi
Andreas Schneiderb1d3f652020-05-25 17:03:17 +0200481 if [ "$CLASS" = "ETC" ] ; then
482 if [ "$DIRNAME" != "." ]; then
483 printf '\tsub_dir: "%s",\n' "$DIRNAME"
484 fi
485 fi
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700486 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
487 printf '\tprefer: true,\n'
488 fi
489 if [ "$EXTRA" = "priv-app" ]; then
490 printf '\tprivileged: true,\n'
491 fi
492 if [ "$PARTITION" = "vendor" ]; then
493 printf '\tsoc_specific: true,\n'
494 elif [ "$PARTITION" = "product" ]; then
495 printf '\tproduct_specific: true,\n'
Luca Stefani0409f232020-09-09 15:53:58 +0200496 elif [ "$PARTITION" = "system_ext" ]; then
497 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700498 elif [ "$PARTITION" = "odm" ]; then
499 printf '\tdevice_specific: true,\n'
500 fi
501 printf '}\n\n'
502 done
503}
504
505#
506# write_makefile_packages:
507#
508# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani0409f232020-09-09 15:53:58 +0200509# $2: /odm, /product, /system_ext, or /vendor partition
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700510# $3: type-specific extra flags
511# $4: Name of the array holding the target list
512#
513# Internal function which writes out the BUILD_PREBUILT stanzas
514# for all modules in the list. This is called by write_product_packages
515# after the modules are categorized.
516#
517function write_makefile_packages() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700518
519 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500520 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700521 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700522
523 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700524 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700525 local FILELIST=("${!ARR_NAME}")
526
527 local FILE=
528 local ARGS=
529 local BASENAME=
530 local EXTENSION=
531 local PKGNAME=
532 local SRC=
533
534 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300535 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700536 ARGS=$(target_args "$P")
537
538 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100539 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700540 EXTENSION=${BASENAME##*.}
Mohd Farazd1d72352019-10-08 16:13:50 +0530541 EXTENSION="."$EXTENSION
542 if [ "$EXTENSION" = ".jar" ]; then
543 EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
544 elif [ "$EXTENSION" = ".apk" ]; then
545 EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
546 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700547 PKGNAME=${BASENAME%.*}
548
549 # Add to final package list
550 PACKAGE_LIST+=("$PKGNAME")
551
552 SRC="proprietary"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400553 if [ "$PARTITION" = "system" ]; then
554 SRC+="/system"
555 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700556 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500557 elif [ "$PARTITION" = "product" ]; then
558 SRC+="/product"
Luca Stefani0409f232020-09-09 15:53:58 +0200559 elif [ "$PARTITION" = "system_ext" ]; then
560 SRC+="/system_ext"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700561 elif [ "$PARTITION" = "odm" ]; then
562 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700563 fi
564
565 printf 'include $(CLEAR_VARS)\n'
566 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
567 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
568 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700569 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700570 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
571 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
572 #if [ "$VENDOR_PKG" = "true" ]; then
573 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
574 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
575 #else
576 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
577 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
578 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700579 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700580 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
581 else
582 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
583 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700584 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700585 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700586 fi
587 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200588 if [ "$EXTRA" = "priv-app" ]; then
589 SRC="$SRC/priv-app"
590 else
591 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700592 fi
593 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
594 local CERT=platform
595 if [ ! -z "$ARGS" ]; then
596 CERT="$ARGS"
597 fi
598 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
599 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
600 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200601 local CERT=platform
602 if [ ! -z "$ARGS" ]; then
603 CERT="$ARGS"
604 fi
605 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700606 elif [ "$CLASS" = "ETC" ]; then
607 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
608 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700609 if [ "$ARGS" = "rootfs" ]; then
610 SRC="$SRC/rootfs"
611 if [ "$EXTRA" = "sbin" ]; then
612 SRC="$SRC/sbin"
613 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
614 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
615 fi
616 else
617 SRC="$SRC/bin"
618 fi
619 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
620 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700621 else
Steve Kondika991cf12016-07-28 12:13:12 -0700622 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700623 fi
624 printf 'LOCAL_MODULE_TAGS := optional\n'
625 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400626 if [ "$CLASS" = "APPS" ]; then
627 printf 'LOCAL_DEX_PREOPT := false\n'
628 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700629 if [ ! -z "$EXTENSION" ]; then
Mohd Farazd1d72352019-10-08 16:13:50 +0530630 printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
Steve Kondika991cf12016-07-28 12:13:12 -0700631 fi
M1cha15f226c2017-01-04 09:00:11 +0100632 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
633 if [ "$DIRNAME" != "." ]; then
634 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
635 fi
636 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700637 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700638 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
639 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500640 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800641 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500642 elif [ "$PARTITION" = "product" ]; then
643 printf 'LOCAL_PRODUCT_MODULE := true\n'
Luca Stefani0409f232020-09-09 15:53:58 +0200644 elif [ "$PARTITION" = "system_ext" ]; then
645 printf 'LOCAL_SYSTEM_EXT_MODULE := true\n'
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700646 elif [ "$PARTITION" = "odm" ]; then
647 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700648 fi
649 printf 'include $(BUILD_PREBUILT)\n\n'
650 done
651}
652
653#
654# write_product_packages:
655#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700656# This function will create prebuilt entries in the
657# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700658# product makefile for all files in the blob list which
659# start with a single dash (-) character.
660#
661function write_product_packages() {
662 PACKAGE_LIST=()
663
664 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
665
666 if [ "$COUNT" = "0" ]; then
667 return 0
668 fi
669
670 # Figure out what's 32-bit, what's 64-bit, and what's multilib
671 # I really should not be doing this in bash due to shitty array passing :(
672 local T_LIB32=( $(prefix_match "lib/") )
673 local T_LIB64=( $(prefix_match "lib64/") )
674 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
675 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700676 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700677
Steve Kondik03ce4002016-07-29 00:00:16 -0700678 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700679 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700680 fi
681 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700682 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700683 fi
684 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700685 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700686 fi
687
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400688 local T_S_LIB32=( $(prefix_match "system/lib/") )
689 local T_S_LIB64=( $(prefix_match "system/lib64/") )
690 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
691 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
692 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
693
694 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700695 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400696 fi
697 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700698 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400699 fi
700 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700701 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400702 fi
703
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700704 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
705 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
706 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
707 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700708 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700709
710 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700711 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700712 fi
713 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700714 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700715 fi
716 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700717 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500718 fi
719
720 local T_P_LIB32=( $(prefix_match "product/lib/") )
721 local T_P_LIB64=( $(prefix_match "product/lib64/") )
722 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
723 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
724 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
725
726 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700727 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500728 fi
729 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700730 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500731 fi
732 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700733 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700734 fi
735
Luca Stefani0409f232020-09-09 15:53:58 +0200736 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
737 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
738 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
739 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
740 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
741
742 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
743 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
744 fi
745 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
746 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
747 fi
748 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
749 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
750 fi
751
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700752 local T_O_LIB32=( $(prefix_match "odm/lib/") )
753 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
754 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
755 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
756 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
757
758 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700759 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700760 fi
761 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700762 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700763 fi
764 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700765 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700766 fi
767
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700768 # Apps
769 local APPS=( $(prefix_match "app/") )
770 if [ "${#APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700771 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700772 fi
773 local PRIV_APPS=( $(prefix_match "priv-app/") )
774 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700775 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700776 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400777 local S_APPS=( $(prefix_match "system/app/") )
778 if [ "${#S_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700779 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400780 fi
781 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
782 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700783 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400784 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700785 local V_APPS=( $(prefix_match "vendor/app/") )
786 if [ "${#V_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700787 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700788 fi
789 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
790 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700791 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500792 fi
793 local P_APPS=( $(prefix_match "product/app/") )
794 if [ "${#P_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700795 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500796 fi
797 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
798 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700799 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700800 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200801 local SE_APPS=( $(prefix_match "system_ext/app/") )
802 if [ "${#SE_APPS[@]}" -gt "0" ]; then
803 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
804 fi
805 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
806 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
807 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
808 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700809 local O_APPS=( $(prefix_match "odm/app/") )
810 if [ "${#O_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700811 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700812 fi
813 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
814 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700815 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700816 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700817
818 # Framework
819 local FRAMEWORK=( $(prefix_match "framework/") )
820 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700821 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700822 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400823 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
824 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700825 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400826 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200827 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200828 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700829 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500830 fi
831 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
832 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700833 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oderc16f3272017-10-08 23:15:52 +0200834 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200835 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich411066c2020-09-16 17:58:53 -0700836 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani0409f232020-09-09 15:53:58 +0200837 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
838 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700839 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
840 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700841 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700842 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700843
844 # Etc
845 local ETC=( $(prefix_match "etc/") )
846 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700847 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700848 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400849 local S_ETC=( $(prefix_match "system/etc/") )
850 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700851 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400852 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700853 local V_ETC=( $(prefix_match "vendor/etc/") )
854 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700855 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500856 fi
857 local P_ETC=( $(prefix_match "product/etc/") )
858 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700859 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700860 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200861 local SE_ETC=( $(prefix_match "system_ext/etc/") )
862 if [ "${#SE_ETC[@]}" -gt "0" ]; then
863 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
864 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700865 local O_ETC=( $(prefix_match "odm/etc/") )
866 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700867 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700868 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700869
870 # Executables
871 local BIN=( $(prefix_match "bin/") )
872 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700873 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700874 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400875 local S_BIN=( $(prefix_match "system/bin/") )
876 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700877 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400878 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700879 local V_BIN=( $(prefix_match "vendor/bin/") )
880 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700881 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500882 fi
883 local P_BIN=( $(prefix_match "product/bin/") )
884 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700885 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700886 fi
Luca Stefani0409f232020-09-09 15:53:58 +0200887 local SE_BIN=( $(prefix_match "system_ext/bin/") )
888 if [ "${#SE_BIN[@]}" -gt "0" ]; then
889 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
890 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700891 local O_BIN=( $(prefix_match "odm/bin/") )
892 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700893 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700894 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700895 local SBIN=( $(prefix_match "sbin/") )
896 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700897 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700898 fi
899
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700900
901 # Actually write out the final PRODUCT_PACKAGES list
902 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
903
904 if [ "$PACKAGE_COUNT" -eq "0" ]; then
905 return 0
906 fi
907
908 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
909 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
910 local LINEEND=" \\"
911 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
912 LINEEND=""
913 fi
914 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
915 done
916}
917
918#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700919# write_blueprint_header:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700920#
921# $1: file which will be written to
922#
923# writes out the copyright header with the current year.
924# note that this is not an append operation, and should
925# be executed first!
926#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700927function write_blueprint_header() {
928 if [ -f $1 ]; then
929 rm $1
930 fi
931
932 YEAR=$(date +"%Y")
933
934 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
935
936 printf "/**\n" > $1
937 NUM_REGEX='^[0-9]+$'
938 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
939 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
940 else
941 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
942 fi
943
944 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
945 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
946 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
947 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
948 else
949 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
950 fi
951
952 cat << EOF >> $1
953 *
954 * Licensed under the Apache License, Version 2.0 (the "License");
955 * you may not use this file except in compliance with the License.
956 * You may obtain a copy of the License at
957 *
958 * http://www.apache.org/licenses/LICENSE-2.0
959 *
960 * Unless required by applicable law or agreed to in writing, software
961 * distributed under the License is distributed on an "AS IS" BASIS,
962 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
963 * See the License for the specific language governing permissions and
964 * limitations under the License.
965 *
966 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
967 */
968
969EOF
970}
971
972#
973# write_makefile_header:
974#
975# $1: file which will be written to
976#
977# writes out the copyright header with the current year.
978# note that this is not an append operation, and should
979# be executed first!
980#
981function write_makefile_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600982 if [ -f $1 ]; then
983 rm $1
984 fi
985
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700986 YEAR=$(date +"%Y")
987
988 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
989
Matt Mower8945f5e2017-01-07 14:08:17 -0600990 NUM_REGEX='^[0-9]+$'
991 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
992 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
993 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
994 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
995 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
996 fi
997 if [ $YEAR -eq 2017 ]; then
998 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
999 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
1000 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
1001 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
1002 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
1003 else
1004 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
1005 fi
1006 else
1007 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
1008 fi
1009
1010 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001011#
1012# Licensed under the Apache License, Version 2.0 (the "License");
1013# you may not use this file except in compliance with the License.
1014# You may obtain a copy of the License at
1015#
1016# http://www.apache.org/licenses/LICENSE-2.0
1017#
1018# Unless required by applicable law or agreed to in writing, software
1019# distributed under the License is distributed on an "AS IS" BASIS,
1020# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1021# See the License for the specific language governing permissions and
1022# limitations under the License.
1023
1024# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1025
1026EOF
1027}
1028
1029#
1030# write_headers:
1031#
1032# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001033# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001034#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001035# Calls write_makefile_header for each of the makefiles and
1036# write_blueprint_header for Android.bp and creates the initial
1037# path declaration and device guard for the Android.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001038#
1039function write_headers() {
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001040 write_makefile_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001041
1042 GUARD="$2"
1043 if [ -z "$GUARD" ]; then
1044 GUARD="TARGET_DEVICE"
1045 fi
1046
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001047 cat << EOF >> "$ANDROIDMK"
1048LOCAL_PATH := \$(call my-dir)
1049
1050EOF
1051 if [ "$COMMON" -ne 1 ]; then
1052 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001053ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001054
1055EOF
1056 else
1057 if [ -z "$1" ]; then
1058 echo "Argument with devices to be added to guard must be set!"
1059 exit 1
1060 fi
1061 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -04001062ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001063
1064EOF
1065 fi
1066
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001067 write_makefile_header "$BOARDMK"
1068 write_makefile_header "$PRODUCTMK"
1069 write_blueprint_header "$ANDROIDBP"
1070
1071 cat << EOF >> "$ANDROIDBP"
1072soong_namespace {
1073}
1074
1075EOF
1076
1077 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1078 cat << EOF >> "$PRODUCTMK"
1079PRODUCT_SOONG_NAMESPACES += \\
1080 vendor/$VENDOR/$DEVICE
1081
1082EOF
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001083}
1084
1085#
1086# write_footers:
1087#
1088# Closes the inital guard and any other finalization tasks. Must
1089# be called as the final step.
1090#
1091function write_footers() {
1092 cat << EOF >> "$ANDROIDMK"
1093endif
1094EOF
1095}
1096
1097# Return success if adb is up and not in recovery
1098function _adb_connected {
1099 {
Steve Kondik7561d192016-09-01 21:40:27 -07001100 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001101 then
1102 return 0
1103 fi
1104 } 2>/dev/null
1105
1106 return 1
1107};
1108
1109#
Bruno Martins3b96ba52016-07-27 15:00:05 +01001110# parse_file_list:
1111#
1112# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001113# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +01001114#
1115# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001116#
1117function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +01001118 if [ -z "$1" ]; then
1119 echo "An input file is expected!"
1120 exit 1
1121 elif [ ! -f "$1" ]; then
1122 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001123 exit 1
1124 fi
1125
Vladimir Olteanc5034462019-01-17 03:04:16 +02001126 if [ -n "$2" ]; then
1127 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001128 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +02001129 # Match all lines starting with first line found to start* with '#'
1130 # comment and contain** $2, and ending with first line to be empty*.
1131 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1132 # **the $2 match is case-insensitive
1133 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001134 else
1135 LIST=$1
1136 fi
1137
1138
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001139 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001140 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001141 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001142 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001143 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001144 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001145
1146 while read -r line; do
1147 if [ -z "$line" ]; then continue; fi
1148
Steve Kondik48f8df82016-08-14 03:55:08 -07001149 # If the line has a pipe delimiter, a sha1 hash should follow.
1150 # This indicates the file should be pinned and not overwritten
1151 # when extracting files.
1152 local SPLIT=(${line//\|/ })
1153 local COUNT=${#SPLIT[@]}
1154 local SPEC=${SPLIT[0]}
1155 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001156 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -07001157 if [ "$COUNT" -gt "1" ]; then
1158 HASH=${SPLIT[1]}
1159 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001160 if [ "$COUNT" -gt "2" ]; then
1161 FIXUP_HASH=${SPLIT[2]}
1162 fi
Steve Kondik48f8df82016-08-14 03:55:08 -07001163
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001164 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -07001165 if [[ "$SPEC" =~ ^- ]]; then
1166 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1167 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001168 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001169 else
Steve Kondik48f8df82016-08-14 03:55:08 -07001170 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1171 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001172 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001173 fi
1174
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001175 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001176}
1177
1178#
1179# write_makefiles:
1180#
1181# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001182# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001183#
1184# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001185# the given file and appends to the Android.bp as well as
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001186# the product makefile.
1187#
1188function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001189 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001190 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001191 write_product_packages
1192}
1193
1194#
Louis Popia516c2f2016-07-25 15:51:13 +02001195# append_firmware_calls_to_makefiles:
1196#
1197# Appends to Android.mk the calls to all images present in radio folder
1198# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1199#
1200function append_firmware_calls_to_makefiles() {
1201 cat << EOF >> "$ANDROIDMK"
1202ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1203
1204RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1205\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1206 \$(call add-radio-file,radio/\$(f)))
1207\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1208
1209endif
1210
1211EOF
1212}
1213
1214#
Luca Stefani7f9fff22016-07-18 13:47:55 +02001215# get_file:
1216#
1217# $1: input file
1218# $2: target file/folder
1219# $3: source of the file (can be "adb" or a local folder)
1220#
1221# Silently extracts the input file to defined target
1222# Returns success if file can be pulled from the device or found locally
1223#
1224function get_file() {
1225 local SRC="$3"
1226
1227 if [ "$SRC" = "adb" ]; then
1228 # try to pull
1229 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1230
1231 return 1
1232 else
1233 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +03001234 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1235 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +02001236 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +02001237
1238 return 1
1239 fi
1240};
1241
1242#
1243# oat2dex:
1244#
1245# $1: extracted apk|jar (to check if deodex is required)
1246# $2: odexed apk|jar to deodex
1247# $3: source of the odexed apk|jar
1248#
1249# Convert apk|jar .odex in the corresposing classes.dex
1250#
1251function oat2dex() {
Jackeagled6811aa2019-09-24 08:26:40 +02001252 local BLISS_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001253 local OEM_TARGET="$2"
1254 local SRC="$3"
1255 local TARGET=
1256 local OAT=
1257
1258 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Jackeagled6811aa2019-09-24 08:26:40 +02001259 export BAKSMALIJAR="$BLISS_ROOT"/prebuilts/tools-lineage/common/smali/baksmali.jar
1260 export SMALIJAR="$BLISS_ROOT"/prebuilts/tools-lineage/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +02001261 fi
1262
Joe Maples9be579f2018-01-05 14:51:33 -05001263 if [ -z "$VDEXEXTRACTOR" ]; then
Jackeagled6811aa2019-09-24 08:26:40 +02001264 export VDEXEXTRACTOR="$BLISS_ROOT"/prebuilts/tools-lineage/${HOST}-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -05001265 fi
1266
codeworkx1c29bf62018-09-23 12:36:57 +02001267 if [ -z "$CDEXCONVERTER" ]; then
Jackeagled6811aa2019-09-24 08:26:40 +02001268 export CDEXCONVERTER="$BLISS_ROOT"/prebuilts/tools-lineage/${HOST}-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +02001269 fi
1270
Luca Stefani7f9fff22016-07-18 13:47:55 +02001271 # Extract existing boot.oats to the temp folder
1272 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001273 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +02001274 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001275 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +03001276 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001277 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001278 else
1279 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001280 fi
1281 done
1282 fi
1283
1284 if [ -z "$ARCHES" ]; then
1285 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1286 fi
1287
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001288 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -07001289 return;
1290 fi
1291
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001292 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001293 return 0 # target apk|jar is already odexed, return
1294 fi
1295
1296 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001297 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001298
1299 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001300 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001301
1302 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001303 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -05001304 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001305 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1306 for CLASS in $CLASSES; do
1307 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1308 # Check if we have to deal with CompactDex
1309 if [[ "$CLASS" == *.cdex ]]; then
1310 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1311 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1312 else
1313 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1314 fi
1315 done
Joe Maples9be579f2018-01-05 14:51:33 -05001316 else
1317 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1318 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001319 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001320 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001321 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001322 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001323 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001324 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001325 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001326 # try to extract classes.dex from boot.vdex for frameworks jars
1327 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001328 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001329 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001330 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1331 for CLASS in $CLASSES; do
1332 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1333 # Check if we have to deal with CompactDex
1334 if [[ "$CLASS" == *.cdex ]]; then
1335 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1336 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1337 else
1338 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1339 fi
1340 done
Joe Maples9be579f2018-01-05 14:51:33 -05001341 else
1342 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1343 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1344 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001345 else
1346 continue
1347 fi
1348
Luca Stefani7f9fff22016-07-18 13:47:55 +02001349 done
1350
1351 rm -rf "$TMPDIR/dexout"
1352}
1353
1354#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001355# init_adb_connection:
1356#
1357# Starts adb server and waits for the device
1358#
1359function init_adb_connection() {
1360 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1361 if ! _adb_connected; then
1362 echo "No device is online. Waiting for one..."
1363 echo "Please connect USB and/or enable USB debugging"
1364 until _adb_connected; do
1365 sleep 1
1366 done
1367 echo "Device Found."
1368 fi
1369
1370 # Retrieve IP and PORT info if we're using a TCP connection
1371 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1372 | head -1 | awk '{print $1}')
1373 adb root &> /dev/null
1374 sleep 0.3
1375 if [ -n "$TCPIPPORT" ]; then
1376 # adb root just killed our connection
1377 # so reconnect...
1378 adb connect "$TCPIPPORT"
1379 fi
1380 adb wait-for-device &> /dev/null
1381 sleep 0.3
1382}
1383
1384#
Luca Stefani3a030122016-07-30 12:08:25 +02001385# fix_xml:
1386#
1387# $1: xml file to fix
1388#
1389function fix_xml() {
1390 local XML="$1"
1391 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1392
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001393 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1394 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001395
1396 mv "$TEMP_XML" "$XML"
1397}
1398
Vladimir Oltean4818c232019-01-17 03:07:34 +02001399function get_hash() {
1400 local FILE="$1"
1401
1402 if [ "$(uname)" == "Darwin" ]; then
1403 shasum "${FILE}" | awk '{print $1}'
1404 else
1405 sha1sum "${FILE}" | awk '{print $1}'
1406 fi
1407}
1408
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001409function print_spec() {
1410 local SPEC_PRODUCT_PACKAGE="$1"
1411 local SPEC_SRC_FILE="$2"
1412 local SPEC_DST_FILE="$3"
1413 local SPEC_ARGS="$4"
1414 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001415 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001416
1417 local PRODUCT_PACKAGE=""
1418 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1419 PRODUCT_PACKAGE="-"
1420 fi
1421 local SRC=""
1422 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1423 SRC="${SPEC_SRC_FILE}:"
1424 fi
1425 local DST=""
1426 if [ ! -z "${SPEC_DST_FILE}" ]; then
1427 DST="${SPEC_DST_FILE}"
1428 fi
1429 local ARGS=""
1430 if [ ! -z "${SPEC_ARGS}" ]; then
1431 ARGS=";${SPEC_ARGS}"
1432 fi
1433 local HASH=""
1434 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1435 HASH="|${SPEC_HASH}"
1436 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001437 local FIXUP_HASH=""
1438 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1439 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1440 fi
1441 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1442}
1443
1444# To be overridden by device-level extract-files.sh
1445# Parameters:
1446# $1: spec name of a blob. Can be used for filtering.
1447# If the spec is "src:dest", then $1 is "dest".
1448# If the spec is "src", then $1 is "src".
1449# $2: path to blob file. Can be used for fixups.
1450#
1451function blob_fixup() {
1452 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001453}
1454
Luca Stefani3a030122016-07-30 12:08:25 +02001455#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001456# extract:
1457#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001458# Positional parameters:
1459# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001460# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001461# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1462#
1463# Non-positional parameters (coming after $2):
1464# --section: preferred way of selecting the portion to parse and extract from
1465# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001466# --kang: if present, this option will activate the printing of hashes for the
1467# extracted blobs. Useful with --section for subsequent pinning of
1468# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001469#
1470function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001471 # Consume positional parameters
1472 local PROPRIETARY_FILES_TXT="$1"; shift
1473 local SRC="$1"; shift
1474 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001475 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001476
1477 # Consume optional, non-positional parameters
1478 while [ "$#" -gt 0 ]; do
1479 case "$1" in
1480 -s|--section)
1481 SECTION="$2"; shift
1482 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001483 -k|--kang)
1484 KANG=true
1485 DISABLE_PINNING=1
1486 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001487 *)
1488 # Backwards-compatibility with the old behavior, where $3, if
1489 # present, denoted an optional positional ${SECTION} argument.
1490 # Users of ${SECTION} are encouraged to migrate from setting it as
1491 # positional $3, to non-positional --section ${SECTION}, the
1492 # reason being that it doesn't scale to have more than 1 optional
1493 # positional argument.
1494 SECTION="$1"
1495 ;;
1496 esac
1497 shift
1498 done
1499
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001500 if [ -z "$OUTDIR" ]; then
1501 echo "Output dir not set!"
1502 exit 1
1503 fi
1504
Vladimir Olteanc5034462019-01-17 03:04:16 +02001505 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001506
1507 # Allow failing, so we can try $DEST and/or $FILE
1508 set +e
1509
1510 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001511 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001512 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001513 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001514 local COUNT=${#FILELIST[@]}
Jackeagled6811aa2019-09-24 08:26:40 +02001515 local OUTPUT_ROOT="$BLISS_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001516 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1517
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001518 if [ "$SRC" = "adb" ]; then
1519 init_adb_connection
1520 fi
1521
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001522 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001523 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001524
1525 # Check if we're working with the same zip that was passed last time.
1526 # If so, let's just use what's already extracted.
1527 MD5=`md5sum "$SRC"| awk '{print $1}'`
1528 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1529
1530 if [ "$MD5" != "$OLDMD5" ]; then
1531 rm -rf "$DUMPDIR"
1532 mkdir "$DUMPDIR"
1533 unzip "$SRC" -d "$DUMPDIR"
1534 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1535
1536 # Stop if an A/B OTA zip is detected. We cannot extract these.
1537 if [ -a "$DUMPDIR"/payload.bin ]; then
1538 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1539 exit 1
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001540 fi
dianlujitaofc486342020-04-21 23:03:20 +08001541
Luca Stefani0409f232020-09-09 15:53:58 +02001542 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitaofc486342020-04-21 23:03:20 +08001543 do
1544 # If OTA is block based, extract it.
dianlujitaoee95f242020-04-21 23:01:13 +08001545 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1546 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1547 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1548 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1549 fi
dianlujitaofc486342020-04-21 23:03:20 +08001550 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1551 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
Jackeagled6811aa2019-09-24 08:26:40 +02001552 python "$BLISS_ROOT"/vendor/lineage/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
dianlujitaofc486342020-04-21 23:03:20 +08001553 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1554 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1555 echo "Requesting sudo access to mount the "$PARTITION".img"
1556 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1557 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1558 sudo umount "$DUMPDIR"/tmp
1559 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1560 fi
1561 done
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001562 fi
1563
1564 SRC="$DUMPDIR"
1565 fi
1566
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001567 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001568 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001569 rm -rf "${OUTPUT_TMP:?}"
1570 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001571 if [ -d "$OUTPUT_ROOT" ]; then
1572 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1573 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001574 VENDOR_STATE=1
1575 fi
1576
Vladimir Olteanc5034462019-01-17 03:04:16 +02001577 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001578
1579 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001580
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001581 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001582 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001583 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001584 local OUTPUT_DIR=
1585 local TMP_DIR=
1586 local SRC_FILE=
1587 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001588 local IS_PRODUCT_PACKAGE=false
1589
1590 # Note: this relies on the fact that the ${FILELIST[@]} array
1591 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1592 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1593 IS_PRODUCT_PACKAGE=true
1594 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001595
Vladimir Olteand652a062018-06-24 20:42:01 +03001596 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001597 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1598 TMP_DIR="${OUTPUT_TMP}/rootfs"
1599 SRC_FILE="/${SPEC_SRC_FILE}"
1600 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001601 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001602 OUTPUT_DIR="${OUTPUT_ROOT}"
1603 TMP_DIR="${OUTPUT_TMP}"
1604 SRC_FILE="/system/${SPEC_SRC_FILE}"
1605 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001606 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001607
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001608 # Strip the file path in the vendor repo of "system", if present
Vladimir Olteanc5034462019-01-17 03:04:16 +02001609 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitaodb1caf42020-04-06 12:43:16 +08001610 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001611 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001612
Gabriele Me6df25b2017-10-11 00:58:59 +02001613 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001614 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001615 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001616 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001617 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001618 if [ -f "${VENDOR_REPO_FILE}" ]; then
1619 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001620 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001621 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001622 fi
1623 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001624 local TMP_HASH=$(get_hash "${PINNED}")
1625 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001626 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001627 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1628 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001629 fi
1630 fi
1631 fi
1632 fi
1633
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001634 if [ "${KANG}" = false ]; then
1635 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1636 fi
1637
Gabriele Me6df25b2017-10-11 00:58:59 +02001638 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001639 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001640 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001641 FOUND=false
1642 # Try Lineage target first.
1643 # Also try to search for files stripped of
1644 # the "/system" prefix, if we're actually extracting
1645 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001646 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001647 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1648 FOUND=true
1649 break
1650 }
1651 done
1652
1653 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001654 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001655 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001656 fi
1657 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001658
Vladimir Oltean4818c232019-01-17 03:07:34 +02001659 # Blob fixup pipeline has 2 parts: one that is fixed and
1660 # one that is user-configurable
1661 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1662 # Deodex apk|jar if that's the case
1663 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1664 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1665 if [ -f "$TMPDIR/classes.dex" ]; then
dianlujitao0b501d52020-04-06 12:45:36 +08001666 touch -t 200901010000 "$TMPDIR/classes"*
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001667 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1668 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001669 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001670 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001671 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1672 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001673 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001674 # Now run user-supplied fixup function
1675 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1676 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001677
Vladimir Olteand6747712018-06-24 20:46:42 +03001678 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001679 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001680 local TYPE="${DIR##*/}"
1681 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001682 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001683 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001684 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001685 fi
1686 fi
1687
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001688 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001689 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1690 fi
1691
1692 # Check and print whether the fixup pipeline actually did anything.
1693 # This isn't done right after the fixup pipeline because we want this print
1694 # to come after print_spec above, when in kang mode.
1695 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1696 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1697 # Now sanity-check the spec for this blob.
1698 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1699 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1700 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1701 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001702 fi
1703
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001704 done
1705
1706 # Don't allow failing
1707 set -e
1708}
Louis Popia516c2f2016-07-25 15:51:13 +02001709
1710#
1711# extract_firmware:
1712#
1713# $1: file containing the list of items to extract
1714# $2: path to extracted radio folder
1715#
1716function extract_firmware() {
1717 if [ -z "$OUTDIR" ]; then
1718 echo "Output dir not set!"
1719 exit 1
1720 fi
1721
1722 parse_file_list "$1"
1723
1724 # Don't allow failing
1725 set -e
1726
1727 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1728 local COUNT=${#FILELIST[@]}
1729 local SRC="$2"
Jackeagled6811aa2019-09-24 08:26:40 +02001730 local OUTPUT_DIR="$BLISS_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001731
1732 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1733 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1734 rm -rf "${OUTPUT_DIR:?}/"*
1735 VENDOR_RADIO_STATE=1
1736 fi
1737
1738 echo "Extracting $COUNT files in $1 from $SRC:"
1739
1740 for (( i=1; i<COUNT+1; i++ )); do
1741 local FILE="${FILELIST[$i-1]}"
1742 printf ' - %s \n' "/radio/$FILE"
1743
1744 if [ ! -d "$OUTPUT_DIR" ]; then
1745 mkdir -p "$OUTPUT_DIR"
1746 fi
1747 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1748 chmod 644 "$OUTPUT_DIR/$FILE"
1749 done
1750}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001751
1752function extract_img_data() {
1753 local image_file="$1"
1754 local out_dir="$2"
1755 local logFile="$TMPDIR/debugfs.log"
1756
1757 if [ ! -d "$out_dir" ]; then
1758 mkdir -p "$out_dir"
1759 fi
1760
1761 if [[ "$HOST_OS" == "Darwin" ]]; then
1762 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1763 echo "[-] Failed to extract data from '$image_file'"
1764 abort 1
1765 }
1766 else
1767 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1768 do
1769 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1770 echo "[-] Failed to extract data from '$image_file'"
1771 abort 1
1772 }
1773 done
1774 fi
1775
1776 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1777 if grep -Fq "$symlink_err" "$logFile"; then
1778 echo "[-] Symlinks have not been properly processed from $image_file"
1779 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1780 abort 1
1781 fi
1782}
1783
1784declare -ra VENDOR_SKIP_FILES=(
1785 "bin/toybox_vendor"
1786 "bin/toolbox"
1787 "bin/grep"
1788 "build.prop"
1789 "compatibility_matrix.xml"
1790 "default.prop"
1791 "etc/NOTICE.xml.gz"
1792 "etc/vintf/compatibility_matrix.xml"
1793 "etc/vintf/manifest.xml"
1794 "etc/wifi/wpa_supplicant.conf"
1795 "manifest.xml"
1796 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1797 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1798 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1799 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1800 "overlay/framework-res__auto_generated_rro.apk"
1801 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1802)
1803
1804function array_contains() {
1805 local element
1806 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1807 return 1
1808}
1809
1810function generate_prop_list_from_image() {
1811 local image_file="$1"
1812 local image_dir="$TMPDIR/image-temp"
1813 local output_list="$2"
1814 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1815 local -n skipped_vendor_files="$3"
1816
1817 extract_img_data "$image_file" "$image_dir"
1818
1819 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1820 do
1821 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1822 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1823 continue
1824 fi
1825 # Skip device defined skipped files since they will be re-generated at build time
1826 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1827 continue
1828 fi
1829 if suffix_match_file ".apk" "$FILE" ; then
1830 echo "-vendor/$FILE" >> "$output_list_tmp"
1831 else
1832 echo "vendor/$FILE" >> "$output_list_tmp"
1833 fi
1834 done
1835
1836 # Sort merged file with all lists
1837 sort -u "$output_list_tmp" > "$output_list"
1838
1839 # Clean-up
1840 rm -f "$output_list_tmp"
1841}