blob: 5b5a7325ecf1e3f7f50d53dac619fc65bd5fb006 [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() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300133 local SPEC="$1"
134 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"
287 elif prefix_match_file "odm/" $TARGET ; then
288 local OUTTARGET=$(truncate_file $TARGET)
289 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
290 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8aae50d2019-10-05 00:09:41 -0400291 elif prefix_match_file "vendor/odm/" $TARGET ; then
292 local OUTTARGET=$(truncate_file $TARGET)
293 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
294 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
295 elif prefix_match_file "system/vendor/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"
299 elif prefix_match_file "vendor/" $TARGET ; then
300 local OUTTARGET=$(truncate_file $TARGET)
301 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
302 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
303 elif prefix_match_file "system/vendor/" $TARGET ; then
304 local OUTTARGET=$(truncate_file $TARGET)
305 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
306 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400307 elif prefix_match_file "system/" $TARGET ; then
308 local OUTTARGET=$(truncate_file $TARGET)
309 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
310 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400311 else
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400312 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400313 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
314 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700315 done
316 return 0
317}
318
319#
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700320# write_blueprint_packages:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700321#
322# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400323# $2: /system, /odm, /product, or /vendor partition
Steve Kondika991cf12016-07-28 12:13:12 -0700324# $3: type-specific extra flags
325# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700326#
327# Internal function which writes out the BUILD_PREBUILT stanzas
328# for all modules in the list. This is called by write_product_packages
329# after the modules are categorized.
330#
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700331function write_blueprint_packages() {
332
333 local CLASS="$1"
334 local PARTITION="$2"
335 local EXTRA="$3"
336
337 # Yes, this is a horrible hack - we create a new array using indirection
338 local ARR_NAME="$4[@]"
339 local FILELIST=("${!ARR_NAME}")
340
341 local FILE=
342 local ARGS=
343 local BASENAME=
344 local EXTENSION=
345 local PKGNAME=
346 local SRC=
347
348 for P in "${FILELIST[@]}"; do
349 FILE=$(target_file "$P")
350 ARGS=$(target_args "$P")
351
352 BASENAME=$(basename "$FILE")
353 DIRNAME=$(dirname "$FILE")
354 EXTENSION=${BASENAME##*.}
355 PKGNAME=${BASENAME%.*}
356
357 # Add to final package list
358 PACKAGE_LIST+=("$PKGNAME")
359
360 SRC="proprietary"
361 if [ "$PARTITION" = "system" ]; then
362 SRC+="/system"
363 elif [ "$PARTITION" = "vendor" ]; then
364 SRC+="/vendor"
365 elif [ "$PARTITION" = "product" ]; then
366 SRC+="/product"
367 elif [ "$PARTITION" = "odm" ]; then
368 SRC+="/odm"
369 fi
370
371 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
372 printf 'cc_prebuilt_library_shared {\n'
373 printf '\tname: "%s",\n' "$PKGNAME"
374 printf '\towner: "%s",\n' "$VENDOR"
375 printf '\tstrip: {\n'
376 printf '\t\tnone: true,\n'
377 printf '\t},\n'
378 printf '\ttarget: {\n'
379 if [ "$EXTRA" = "both" ]; then
380 printf '\t\tandroid_arm: {\n'
381 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
382 printf '\t\t},\n'
383 printf '\t\tandroid_arm64: {\n'
384 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
385 printf '\t\t},\n'
386 elif [ "$EXTRA" = "64" ]; then
387 printf '\t\tandroid_arm64: {\n'
388 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
389 printf '\t\t},\n'
390 else
391 printf '\t\tandroid_arm: {\n'
392 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
393 printf '\t\t},\n'
394 fi
395 printf '\t},\n'
396 if [ "$EXTRA" != "none" ]; then
397 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
398 fi
399 elif [ "$CLASS" = "APPS" ]; then
400 printf 'android_app_import {\n'
401 printf '\tname: "%s",\n' "$PKGNAME"
402 printf '\towner: "%s",\n' "$VENDOR"
403 if [ "$EXTRA" = "priv-app" ]; then
404 SRC="$SRC/priv-app"
405 else
406 SRC="$SRC/app"
407 fi
408 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
409 if [ "$ARGS" = "PRESIGNED" ]; then
410 printf '\tpresigned: true,\n'
411 elif [ ! -z "$ARGS" ]; then
412 printf '\tcertificate: "%s",\n' "$ARGS"
413 else
414 printf '\tcertificate: "platform",\n'
415 fi
416 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
417 printf 'dex_import {\n'
418 printf '\tname: "%s",\n' "$PKGNAME"
419 printf '\towner: "%s",\n' "$VENDOR"
420 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
421 elif [ "$CLASS" = "ETC" ]; then
422 if [ "$EXTENSION" = "xml" ]; then
423 printf 'prebuilt_etc_xml {\n'
424 else
425 printf 'prebuilt_etc {\n'
426 fi
427 printf '\tname: "%s",\n' "$PKGNAME"
428 printf '\towner: "%s",\n' "$VENDOR"
429 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
430 elif [ "$CLASS" = "EXECUTABLES" ]; then
431 if [ "$EXTENSION" = "sh" ]; then
432 printf 'sh_binary {\n'
433 else
434 printf 'cc_prebuilt_binary {\n'
435 fi
436 printf '\tname: "%s",\n' "$PKGNAME"
437 printf '\towner: "%s",\n' "$VENDOR"
438 if [ "$ARGS" = "rootfs" ]; then
439 SRC="$SRC/rootfs"
440 if [ "$EXTRA" = "sbin" ]; then
441 SRC="$SRC/sbin"
442 printf '\tdist {\n'
443 printf '\t\tdest: "%s",\n' "root/sbin"
444 printf '\t},'
445 fi
446 else
447 SRC="$SRC/bin"
448 fi
449 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
450 unset EXTENSION
451 else
452 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
453 fi
454 if [ "$CLASS" = "APPS" ]; then
455 printf '\tdex_preopt: {\n'
456 printf '\t\tenabled: false,\n'
457 printf '\t},\n'
458 fi
459 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] || [ "$CLASS" = "ETC" ] ; then
460 if [ "$DIRNAME" != "." ]; then
461 printf '\tsub_dir: "%s",\n' "$DIRNAME"
462 fi
463 fi
464 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
465 printf '\tprefer: true,\n'
466 fi
467 if [ "$EXTRA" = "priv-app" ]; then
468 printf '\tprivileged: true,\n'
469 fi
470 if [ "$PARTITION" = "vendor" ]; then
471 printf '\tsoc_specific: true,\n'
472 elif [ "$PARTITION" = "product" ]; then
473 printf '\tproduct_specific: true,\n'
474 elif [ "$PARTITION" = "odm" ]; then
475 printf '\tdevice_specific: true,\n'
476 fi
477 printf '}\n\n'
478 done
479}
480
481#
482# write_makefile_packages:
483#
484# $1: The LOCAL_MODULE_CLASS for the given module list
485# $2: /odm, /product, or /vendor partition
486# $3: type-specific extra flags
487# $4: Name of the array holding the target list
488#
489# Internal function which writes out the BUILD_PREBUILT stanzas
490# for all modules in the list. This is called by write_product_packages
491# after the modules are categorized.
492#
493function write_makefile_packages() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700494
495 local CLASS="$1"
razorlovesb5c2c962019-07-29 02:21:34 -0500496 local PARTITION="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700497 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700498
499 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700500 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700501 local FILELIST=("${!ARR_NAME}")
502
503 local FILE=
504 local ARGS=
505 local BASENAME=
506 local EXTENSION=
507 local PKGNAME=
508 local SRC=
509
510 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300511 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700512 ARGS=$(target_args "$P")
513
514 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100515 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700516 EXTENSION=${BASENAME##*.}
517 PKGNAME=${BASENAME%.*}
518
519 # Add to final package list
520 PACKAGE_LIST+=("$PKGNAME")
521
522 SRC="proprietary"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400523 if [ "$PARTITION" = "system" ]; then
524 SRC+="/system"
525 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700526 SRC+="/vendor"
razorlovesb5c2c962019-07-29 02:21:34 -0500527 elif [ "$PARTITION" = "product" ]; then
528 SRC+="/product"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700529 elif [ "$PARTITION" = "odm" ]; then
530 SRC+="/odm"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700531 fi
532
533 printf 'include $(CLEAR_VARS)\n'
534 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
535 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
536 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700537 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700538 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
539 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
540 #if [ "$VENDOR_PKG" = "true" ]; then
541 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
542 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
543 #else
544 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
545 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
546 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700547 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700548 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
549 else
550 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
551 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700552 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700553 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700554 fi
555 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200556 if [ "$EXTRA" = "priv-app" ]; then
557 SRC="$SRC/priv-app"
558 else
559 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700560 fi
561 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
562 local CERT=platform
563 if [ ! -z "$ARGS" ]; then
564 CERT="$ARGS"
565 fi
566 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
567 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
568 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200569 local CERT=platform
570 if [ ! -z "$ARGS" ]; then
571 CERT="$ARGS"
572 fi
573 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700574 elif [ "$CLASS" = "ETC" ]; then
575 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
576 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700577 if [ "$ARGS" = "rootfs" ]; then
578 SRC="$SRC/rootfs"
579 if [ "$EXTRA" = "sbin" ]; then
580 SRC="$SRC/sbin"
581 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
582 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
583 fi
584 else
585 SRC="$SRC/bin"
586 fi
587 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
588 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700589 else
Steve Kondika991cf12016-07-28 12:13:12 -0700590 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700591 fi
592 printf 'LOCAL_MODULE_TAGS := optional\n'
593 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400594 if [ "$CLASS" = "APPS" ]; then
595 printf 'LOCAL_DEX_PREOPT := false\n'
596 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700597 if [ ! -z "$EXTENSION" ]; then
598 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
599 fi
M1cha15f226c2017-01-04 09:00:11 +0100600 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
601 if [ "$DIRNAME" != "." ]; then
602 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
603 fi
604 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700605 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700606 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
607 fi
razorlovesb5c2c962019-07-29 02:21:34 -0500608 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800609 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesb5c2c962019-07-29 02:21:34 -0500610 elif [ "$PARTITION" = "product" ]; then
611 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700612 elif [ "$PARTITION" = "odm" ]; then
613 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700614 fi
615 printf 'include $(BUILD_PREBUILT)\n\n'
616 done
617}
618
619#
620# write_product_packages:
621#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700622# This function will create prebuilt entries in the
623# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700624# product makefile for all files in the blob list which
625# start with a single dash (-) character.
626#
627function write_product_packages() {
628 PACKAGE_LIST=()
629
630 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
631
632 if [ "$COUNT" = "0" ]; then
633 return 0
634 fi
635
636 # Figure out what's 32-bit, what's 64-bit, and what's multilib
637 # I really should not be doing this in bash due to shitty array passing :(
638 local T_LIB32=( $(prefix_match "lib/") )
639 local T_LIB64=( $(prefix_match "lib64/") )
640 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
641 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700642 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700643
Steve Kondik03ce4002016-07-29 00:00:16 -0700644 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700645 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700646 fi
647 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700648 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700649 fi
650 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700651 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik03ce4002016-07-29 00:00:16 -0700652 fi
653
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400654 local T_S_LIB32=( $(prefix_match "system/lib/") )
655 local T_S_LIB64=( $(prefix_match "system/lib64/") )
656 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
657 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
658 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
659
660 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700661 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400662 fi
663 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700664 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400665 fi
666 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700667 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400668 fi
669
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700670 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
671 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
672 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
673 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700674 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700675
676 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700677 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700678 fi
679 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700680 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700681 fi
682 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700683 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500684 fi
685
686 local T_P_LIB32=( $(prefix_match "product/lib/") )
687 local T_P_LIB64=( $(prefix_match "product/lib64/") )
688 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
689 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
690 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
691
692 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700693 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500694 fi
695 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700696 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500697 fi
698 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700699 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700700 fi
701
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700702 local T_O_LIB32=( $(prefix_match "odm/lib/") )
703 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
704 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
705 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
706 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
707
708 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700709 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700710 fi
711 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700712 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700713 fi
714 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700715 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700716 fi
717
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700718 # Apps
719 local APPS=( $(prefix_match "app/") )
720 if [ "${#APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700721 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700722 fi
723 local PRIV_APPS=( $(prefix_match "priv-app/") )
724 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700725 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700726 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400727 local S_APPS=( $(prefix_match "system/app/") )
728 if [ "${#S_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700729 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400730 fi
731 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
732 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700733 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400734 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700735 local V_APPS=( $(prefix_match "vendor/app/") )
736 if [ "${#V_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700737 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700738 fi
739 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
740 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700741 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500742 fi
743 local P_APPS=( $(prefix_match "product/app/") )
744 if [ "${#P_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700745 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500746 fi
747 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
748 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700749 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700750 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700751 local O_APPS=( $(prefix_match "odm/app/") )
752 if [ "${#O_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700753 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700754 fi
755 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
756 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700757 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700758 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700759
760 # Framework
761 local FRAMEWORK=( $(prefix_match "framework/") )
762 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700763 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700764 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400765 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
766 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700767 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400768 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200769 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200770 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700771 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500772 fi
773 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
774 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700775 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oderc16f3272017-10-08 23:15:52 +0200776 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700777 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
778 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700779 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700780 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700781
782 # Etc
783 local ETC=( $(prefix_match "etc/") )
784 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700785 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700786 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400787 local S_ETC=( $(prefix_match "system/etc/") )
788 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700789 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400790 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700791 local V_ETC=( $(prefix_match "vendor/etc/") )
792 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700793 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500794 fi
795 local P_ETC=( $(prefix_match "product/etc/") )
796 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700797 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700798 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700799 local O_ETC=( $(prefix_match "odm/etc/") )
800 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700801 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700802 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700803
804 # Executables
805 local BIN=( $(prefix_match "bin/") )
806 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700807 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700808 fi
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400809 local S_BIN=( $(prefix_match "system/bin/") )
810 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700811 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab8de76b22019-09-28 23:37:36 -0400812 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700813 local V_BIN=( $(prefix_match "vendor/bin/") )
814 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700815 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesb5c2c962019-07-29 02:21:34 -0500816 fi
817 local P_BIN=( $(prefix_match "product/bin/") )
818 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700819 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700820 fi
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700821 local O_BIN=( $(prefix_match "odm/bin/") )
822 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700823 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawab08e3a272019-09-20 07:32:39 -0700824 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700825 local SBIN=( $(prefix_match "sbin/") )
826 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawab34b5cdc2019-09-20 10:30:38 -0700827 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondika991cf12016-07-28 12:13:12 -0700828 fi
829
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700830
831 # Actually write out the final PRODUCT_PACKAGES list
832 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
833
834 if [ "$PACKAGE_COUNT" -eq "0" ]; then
835 return 0
836 fi
837
838 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
839 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
840 local LINEEND=" \\"
841 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
842 LINEEND=""
843 fi
844 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
845 done
846}
847
848#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700849# write_blueprint_header:
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700850#
851# $1: file which will be written to
852#
853# writes out the copyright header with the current year.
854# note that this is not an append operation, and should
855# be executed first!
856#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700857function write_blueprint_header() {
858 if [ -f $1 ]; then
859 rm $1
860 fi
861
862 YEAR=$(date +"%Y")
863
864 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
865
866 printf "/**\n" > $1
867 NUM_REGEX='^[0-9]+$'
868 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
869 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
870 else
871 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
872 fi
873
874 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
875 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
876 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
877 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
878 else
879 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
880 fi
881
882 cat << EOF >> $1
883 *
884 * Licensed under the Apache License, Version 2.0 (the "License");
885 * you may not use this file except in compliance with the License.
886 * You may obtain a copy of the License at
887 *
888 * http://www.apache.org/licenses/LICENSE-2.0
889 *
890 * Unless required by applicable law or agreed to in writing, software
891 * distributed under the License is distributed on an "AS IS" BASIS,
892 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
893 * See the License for the specific language governing permissions and
894 * limitations under the License.
895 *
896 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
897 */
898
899EOF
900}
901
902#
903# write_makefile_header:
904#
905# $1: file which will be written to
906#
907# writes out the copyright header with the current year.
908# note that this is not an append operation, and should
909# be executed first!
910#
911function write_makefile_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600912 if [ -f $1 ]; then
913 rm $1
914 fi
915
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700916 YEAR=$(date +"%Y")
917
918 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
919
Matt Mower8945f5e2017-01-07 14:08:17 -0600920 NUM_REGEX='^[0-9]+$'
921 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
922 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
923 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
924 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
925 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
926 fi
927 if [ $YEAR -eq 2017 ]; then
928 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
929 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
930 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
931 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
932 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
933 else
934 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
935 fi
936 else
937 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
938 fi
939
940 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700941#
942# Licensed under the Apache License, Version 2.0 (the "License");
943# you may not use this file except in compliance with the License.
944# You may obtain a copy of the License at
945#
946# http://www.apache.org/licenses/LICENSE-2.0
947#
948# Unless required by applicable law or agreed to in writing, software
949# distributed under the License is distributed on an "AS IS" BASIS,
950# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
951# See the License for the specific language governing permissions and
952# limitations under the License.
953
954# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
955
956EOF
957}
958
959#
960# write_headers:
961#
962# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400963# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700964#
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700965# Calls write_makefile_header for each of the makefiles and
966# write_blueprint_header for Android.bp and creates the initial
967# path declaration and device guard for the Android.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700968#
969function write_headers() {
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700970 write_makefile_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400971
972 GUARD="$2"
973 if [ -z "$GUARD" ]; then
974 GUARD="TARGET_DEVICE"
975 fi
976
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700977 cat << EOF >> "$ANDROIDMK"
978LOCAL_PATH := \$(call my-dir)
979
980EOF
981 if [ "$COMMON" -ne 1 ]; then
982 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400983ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700984
985EOF
986 else
987 if [ -z "$1" ]; then
988 echo "Argument with devices to be added to guard must be set!"
989 exit 1
990 fi
991 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400992ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700993
994EOF
995 fi
996
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -0700997 write_makefile_header "$BOARDMK"
998 write_makefile_header "$PRODUCTMK"
999 write_blueprint_header "$ANDROIDBP"
1000
1001 cat << EOF >> "$ANDROIDBP"
1002soong_namespace {
1003}
1004
1005EOF
1006
1007 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1008 cat << EOF >> "$PRODUCTMK"
1009PRODUCT_SOONG_NAMESPACES += \\
1010 vendor/$VENDOR/$DEVICE
1011
1012EOF
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001013}
1014
1015#
1016# write_footers:
1017#
1018# Closes the inital guard and any other finalization tasks. Must
1019# be called as the final step.
1020#
1021function write_footers() {
1022 cat << EOF >> "$ANDROIDMK"
1023endif
1024EOF
1025}
1026
1027# Return success if adb is up and not in recovery
1028function _adb_connected {
1029 {
Steve Kondik7561d192016-09-01 21:40:27 -07001030 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001031 then
1032 return 0
1033 fi
1034 } 2>/dev/null
1035
1036 return 1
1037};
1038
1039#
Bruno Martins3b96ba52016-07-27 15:00:05 +01001040# parse_file_list:
1041#
1042# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001043# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +01001044#
1045# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001046#
1047function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +01001048 if [ -z "$1" ]; then
1049 echo "An input file is expected!"
1050 exit 1
1051 elif [ ! -f "$1" ]; then
1052 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001053 exit 1
1054 fi
1055
Vladimir Olteanc5034462019-01-17 03:04:16 +02001056 if [ -n "$2" ]; then
1057 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001058 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +02001059 # Match all lines starting with first line found to start* with '#'
1060 # comment and contain** $2, and ending with first line to be empty*.
1061 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1062 # **the $2 match is case-insensitive
1063 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001064 else
1065 LIST=$1
1066 fi
1067
1068
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001069 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001070 PRODUCT_PACKAGES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001071 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001072 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -07001073 PRODUCT_COPY_FILES_HASHES=()
Vladimir Oltean4818c232019-01-17 03:07:34 +02001074 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001075
1076 while read -r line; do
1077 if [ -z "$line" ]; then continue; fi
1078
Steve Kondik48f8df82016-08-14 03:55:08 -07001079 # If the line has a pipe delimiter, a sha1 hash should follow.
1080 # This indicates the file should be pinned and not overwritten
1081 # when extracting files.
1082 local SPLIT=(${line//\|/ })
1083 local COUNT=${#SPLIT[@]}
1084 local SPEC=${SPLIT[0]}
1085 local HASH="x"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001086 local FIXUP_HASH="x"
Steve Kondik48f8df82016-08-14 03:55:08 -07001087 if [ "$COUNT" -gt "1" ]; then
1088 HASH=${SPLIT[1]}
1089 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001090 if [ "$COUNT" -gt "2" ]; then
1091 FIXUP_HASH=${SPLIT[2]}
1092 fi
Steve Kondik48f8df82016-08-14 03:55:08 -07001093
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001094 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -07001095 if [[ "$SPEC" =~ ^- ]]; then
1096 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1097 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001098 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001099 else
Steve Kondik48f8df82016-08-14 03:55:08 -07001100 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1101 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Oltean4818c232019-01-17 03:07:34 +02001102 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001103 fi
1104
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -04001105 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001106}
1107
1108#
1109# write_makefiles:
1110#
1111# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001112# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001113#
1114# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawab42752d42019-09-20 07:06:09 -07001115# the given file and appends to the Android.bp as well as
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001116# the product makefile.
1117#
1118function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001119 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -04001120 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001121 write_product_packages
1122}
1123
1124#
Louis Popia516c2f2016-07-25 15:51:13 +02001125# append_firmware_calls_to_makefiles:
1126#
1127# Appends to Android.mk the calls to all images present in radio folder
1128# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1129#
1130function append_firmware_calls_to_makefiles() {
1131 cat << EOF >> "$ANDROIDMK"
1132ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1133
1134RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1135\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1136 \$(call add-radio-file,radio/\$(f)))
1137\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1138
1139endif
1140
1141EOF
1142}
1143
1144#
Luca Stefani7f9fff22016-07-18 13:47:55 +02001145# get_file:
1146#
1147# $1: input file
1148# $2: target file/folder
1149# $3: source of the file (can be "adb" or a local folder)
1150#
1151# Silently extracts the input file to defined target
1152# Returns success if file can be pulled from the device or found locally
1153#
1154function get_file() {
1155 local SRC="$3"
1156
1157 if [ "$SRC" = "adb" ]; then
1158 # try to pull
1159 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1160
1161 return 1
1162 else
1163 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +03001164 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1165 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +02001166 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +02001167
1168 return 1
1169 fi
1170};
1171
1172#
1173# oat2dex:
1174#
1175# $1: extracted apk|jar (to check if deodex is required)
1176# $2: odexed apk|jar to deodex
1177# $3: source of the odexed apk|jar
1178#
1179# Convert apk|jar .odex in the corresposing classes.dex
1180#
1181function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001182 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001183 local OEM_TARGET="$2"
1184 local SRC="$3"
1185 local TARGET=
1186 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -05001187 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001188
1189 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +01001190 export BAKSMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/baksmali.jar
1191 export SMALIJAR="$LINEAGE_ROOT"/prebuilts/tools-lineage/common/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +02001192 fi
1193
Joe Maples9be579f2018-01-05 14:51:33 -05001194 if [ -z "$VDEXEXTRACTOR" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +01001195 export VDEXEXTRACTOR="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/vdexExtractor
Joe Maples9be579f2018-01-05 14:51:33 -05001196 fi
1197
codeworkx1c29bf62018-09-23 12:36:57 +02001198 if [ -z "$CDEXCONVERTER" ]; then
Bruno Martins8194b8e2019-09-23 11:51:33 +01001199 export CDEXCONVERTER="$LINEAGE_ROOT"/prebuilts/tools-lineage/"${HOST,,}"-x86/bin/compact_dex_converter
codeworkx1c29bf62018-09-23 12:36:57 +02001200 fi
1201
Luca Stefani7f9fff22016-07-18 13:47:55 +02001202 # Extract existing boot.oats to the temp folder
1203 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001204 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +02001205 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001206 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +03001207 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001208 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001209 else
1210 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001211 fi
1212 done
1213 fi
1214
1215 if [ -z "$ARCHES" ]; then
1216 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1217 fi
1218
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001219 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -07001220 return;
1221 fi
1222
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001223 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +02001224 return 0 # target apk|jar is already odexed, return
1225 fi
1226
1227 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -07001228 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001229
1230 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001231 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001232
1233 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001234 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -05001235 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001236 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1237 for CLASS in $CLASSES; do
1238 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1239 # Check if we have to deal with CompactDex
1240 if [[ "$CLASS" == *.cdex ]]; then
1241 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1242 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1243 else
1244 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1245 fi
1246 done
Joe Maples9be579f2018-01-05 14:51:33 -05001247 else
1248 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1249 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -04001250 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001251 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +01001252 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +02001253 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +01001254 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +02001255 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +01001256 fi
Joe Maples9be579f2018-01-05 14:51:33 -05001257 # try to extract classes.dex from boot.vdex for frameworks jars
1258 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +02001259 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +01001260 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001261 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1262 for CLASS in $CLASSES; do
1263 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1264 # Check if we have to deal with CompactDex
1265 if [[ "$CLASS" == *.cdex ]]; then
1266 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1267 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1268 else
1269 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1270 fi
1271 done
Joe Maples9be579f2018-01-05 14:51:33 -05001272 else
1273 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1274 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
1275 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +02001276 else
1277 continue
1278 fi
1279
Luca Stefani7f9fff22016-07-18 13:47:55 +02001280 done
1281
1282 rm -rf "$TMPDIR/dexout"
1283}
1284
1285#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001286# init_adb_connection:
1287#
1288# Starts adb server and waits for the device
1289#
1290function init_adb_connection() {
1291 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1292 if ! _adb_connected; then
1293 echo "No device is online. Waiting for one..."
1294 echo "Please connect USB and/or enable USB debugging"
1295 until _adb_connected; do
1296 sleep 1
1297 done
1298 echo "Device Found."
1299 fi
1300
1301 # Retrieve IP and PORT info if we're using a TCP connection
1302 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1303 | head -1 | awk '{print $1}')
1304 adb root &> /dev/null
1305 sleep 0.3
1306 if [ -n "$TCPIPPORT" ]; then
1307 # adb root just killed our connection
1308 # so reconnect...
1309 adb connect "$TCPIPPORT"
1310 fi
1311 adb wait-for-device &> /dev/null
1312 sleep 0.3
1313}
1314
1315#
Luca Stefani3a030122016-07-30 12:08:25 +02001316# fix_xml:
1317#
1318# $1: xml file to fix
1319#
1320function fix_xml() {
1321 local XML="$1"
1322 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1323
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +02001324 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1325 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +02001326
1327 mv "$TEMP_XML" "$XML"
1328}
1329
Vladimir Oltean4818c232019-01-17 03:07:34 +02001330function get_hash() {
1331 local FILE="$1"
1332
1333 if [ "$(uname)" == "Darwin" ]; then
1334 shasum "${FILE}" | awk '{print $1}'
1335 else
1336 sha1sum "${FILE}" | awk '{print $1}'
1337 fi
1338}
1339
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001340function print_spec() {
1341 local SPEC_PRODUCT_PACKAGE="$1"
1342 local SPEC_SRC_FILE="$2"
1343 local SPEC_DST_FILE="$3"
1344 local SPEC_ARGS="$4"
1345 local SPEC_HASH="$5"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001346 local SPEC_FIXUP_HASH="$6"
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001347
1348 local PRODUCT_PACKAGE=""
1349 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1350 PRODUCT_PACKAGE="-"
1351 fi
1352 local SRC=""
1353 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1354 SRC="${SPEC_SRC_FILE}:"
1355 fi
1356 local DST=""
1357 if [ ! -z "${SPEC_DST_FILE}" ]; then
1358 DST="${SPEC_DST_FILE}"
1359 fi
1360 local ARGS=""
1361 if [ ! -z "${SPEC_ARGS}" ]; then
1362 ARGS=";${SPEC_ARGS}"
1363 fi
1364 local HASH=""
1365 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1366 HASH="|${SPEC_HASH}"
1367 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001368 local FIXUP_HASH=""
1369 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1370 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1371 fi
1372 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1373}
1374
1375# To be overridden by device-level extract-files.sh
1376# Parameters:
1377# $1: spec name of a blob. Can be used for filtering.
1378# If the spec is "src:dest", then $1 is "dest".
1379# If the spec is "src", then $1 is "src".
1380# $2: path to blob file. Can be used for fixups.
1381#
1382function blob_fixup() {
1383 :
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001384}
1385
Luca Stefani3a030122016-07-30 12:08:25 +02001386#
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001387# extract:
1388#
Vladimir Olteanc5034462019-01-17 03:04:16 +02001389# Positional parameters:
1390# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001391# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +02001392# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1393#
1394# Non-positional parameters (coming after $2):
1395# --section: preferred way of selecting the portion to parse and extract from
1396# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001397# --kang: if present, this option will activate the printing of hashes for the
1398# extracted blobs. Useful with --section for subsequent pinning of
1399# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001400#
1401function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +02001402 # Consume positional parameters
1403 local PROPRIETARY_FILES_TXT="$1"; shift
1404 local SRC="$1"; shift
1405 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001406 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +02001407
1408 # Consume optional, non-positional parameters
1409 while [ "$#" -gt 0 ]; do
1410 case "$1" in
1411 -s|--section)
1412 SECTION="$2"; shift
1413 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001414 -k|--kang)
1415 KANG=true
1416 DISABLE_PINNING=1
1417 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +02001418 *)
1419 # Backwards-compatibility with the old behavior, where $3, if
1420 # present, denoted an optional positional ${SECTION} argument.
1421 # Users of ${SECTION} are encouraged to migrate from setting it as
1422 # positional $3, to non-positional --section ${SECTION}, the
1423 # reason being that it doesn't scale to have more than 1 optional
1424 # positional argument.
1425 SECTION="$1"
1426 ;;
1427 esac
1428 shift
1429 done
1430
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001431 if [ -z "$OUTDIR" ]; then
1432 echo "Output dir not set!"
1433 exit 1
1434 fi
1435
Vladimir Olteanc5034462019-01-17 03:04:16 +02001436 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001437
1438 # Allow failing, so we can try $DEST and/or $FILE
1439 set +e
1440
1441 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001442 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Oltean4818c232019-01-17 03:07:34 +02001443 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001444 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001445 local COUNT=${#FILELIST[@]}
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001446 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001447 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1448
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001449 if [ "$SRC" = "adb" ]; then
1450 init_adb_connection
1451 fi
1452
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001453 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001454 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001455
1456 # Check if we're working with the same zip that was passed last time.
1457 # If so, let's just use what's already extracted.
1458 MD5=`md5sum "$SRC"| awk '{print $1}'`
1459 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1460
1461 if [ "$MD5" != "$OLDMD5" ]; then
1462 rm -rf "$DUMPDIR"
1463 mkdir "$DUMPDIR"
1464 unzip "$SRC" -d "$DUMPDIR"
1465 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1466
1467 # Stop if an A/B OTA zip is detected. We cannot extract these.
1468 if [ -a "$DUMPDIR"/payload.bin ]; then
1469 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1470 exit 1
1471 # If OTA is block based, extract it.
1472 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1473 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001474 python "$LINEAGE_ROOT"/vendor/lineage/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001475 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1476 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1477 echo "Requesting sudo access to mount the system.img"
1478 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1479 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1480 sudo umount "$DUMPDIR"/tmp
1481 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1482 fi
1483 fi
1484
1485 SRC="$DUMPDIR"
1486 fi
1487
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001488 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001489 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001490 rm -rf "${OUTPUT_TMP:?}"
1491 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001492 if [ -d "$OUTPUT_ROOT" ]; then
1493 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1494 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001495 VENDOR_STATE=1
1496 fi
1497
Vladimir Olteanc5034462019-01-17 03:04:16 +02001498 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001499
1500 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001501
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001502 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001503 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001504 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001505 local OUTPUT_DIR=
1506 local TMP_DIR=
1507 local SRC_FILE=
1508 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001509 local IS_PRODUCT_PACKAGE=false
1510
1511 # Note: this relies on the fact that the ${FILELIST[@]} array
1512 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1513 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1514 IS_PRODUCT_PACKAGE=true
1515 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001516
Vladimir Olteand652a062018-06-24 20:42:01 +03001517 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001518 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1519 TMP_DIR="${OUTPUT_TMP}/rootfs"
1520 SRC_FILE="/${SPEC_SRC_FILE}"
1521 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001522 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001523 OUTPUT_DIR="${OUTPUT_ROOT}"
1524 TMP_DIR="${OUTPUT_TMP}"
1525 SRC_FILE="/system/${SPEC_SRC_FILE}"
1526 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001527 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001528
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001529 # Strip the file path in the vendor repo of "system", if present
1530 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001531 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001532 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001533
Gabriele Me6df25b2017-10-11 00:58:59 +02001534 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001535 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Oltean4818c232019-01-17 03:07:34 +02001536 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001537 local KEEP=""
Vladimir Oltean4818c232019-01-17 03:07:34 +02001538 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001539 if [ -f "${VENDOR_REPO_FILE}" ]; then
1540 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001541 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001542 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001543 fi
1544 if [ -f "$PINNED" ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001545 local TMP_HASH=$(get_hash "${PINNED}")
1546 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele Me6df25b2017-10-11 00:58:59 +02001547 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001548 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1549 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001550 fi
1551 fi
1552 fi
1553 fi
1554
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001555 if [ "${KANG}" = false ]; then
1556 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1557 fi
1558
Gabriele Me6df25b2017-10-11 00:58:59 +02001559 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001560 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001561 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001562 FOUND=false
1563 # Try Lineage target first.
1564 # Also try to search for files stripped of
1565 # the "/system" prefix, if we're actually extracting
1566 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001567 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001568 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1569 FOUND=true
1570 break
1571 }
1572 done
1573
1574 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001575 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001576 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001577 fi
1578 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001579
Vladimir Oltean4818c232019-01-17 03:07:34 +02001580 # Blob fixup pipeline has 2 parts: one that is fixed and
1581 # one that is user-configurable
1582 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1583 # Deodex apk|jar if that's the case
1584 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1585 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1586 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawab19c36cd2018-03-15 12:55:22 -07001587 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1588 rm "$TMPDIR/classes"*
Vladimir Oltean4818c232019-01-17 03:07:34 +02001589 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001590 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001591 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1592 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001593 fi
Vladimir Oltean4818c232019-01-17 03:07:34 +02001594 # Now run user-supplied fixup function
1595 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1596 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Luca Stefani7f9fff22016-07-18 13:47:55 +02001597
Vladimir Olteand6747712018-06-24 20:46:42 +03001598 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001599 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001600 local TYPE="${DIR##*/}"
1601 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001602 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001603 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001604 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001605 fi
1606 fi
1607
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001608 if [ "${KANG}" = true ]; then
Vladimir Oltean4818c232019-01-17 03:07:34 +02001609 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1610 fi
1611
1612 # Check and print whether the fixup pipeline actually did anything.
1613 # This isn't done right after the fixup pipeline because we want this print
1614 # to come after print_spec above, when in kang mode.
1615 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1616 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1617 # Now sanity-check the spec for this blob.
1618 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1619 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1620 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1621 fi
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001622 fi
1623
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001624 done
1625
1626 # Don't allow failing
1627 set -e
1628}
Louis Popia516c2f2016-07-25 15:51:13 +02001629
1630#
1631# extract_firmware:
1632#
1633# $1: file containing the list of items to extract
1634# $2: path to extracted radio folder
1635#
1636function extract_firmware() {
1637 if [ -z "$OUTDIR" ]; then
1638 echo "Output dir not set!"
1639 exit 1
1640 fi
1641
1642 parse_file_list "$1"
1643
1644 # Don't allow failing
1645 set -e
1646
1647 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1648 local COUNT=${#FILELIST[@]}
1649 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001650 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001651
1652 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1653 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1654 rm -rf "${OUTPUT_DIR:?}/"*
1655 VENDOR_RADIO_STATE=1
1656 fi
1657
1658 echo "Extracting $COUNT files in $1 from $SRC:"
1659
1660 for (( i=1; i<COUNT+1; i++ )); do
1661 local FILE="${FILELIST[$i-1]}"
1662 printf ' - %s \n' "/radio/$FILE"
1663
1664 if [ ! -d "$OUTPUT_DIR" ]; then
1665 mkdir -p "$OUTPUT_DIR"
1666 fi
1667 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1668 chmod 644 "$OUTPUT_DIR/$FILE"
1669 done
1670}
Rashed Abdel-Tawab1c29c372019-03-29 20:07:25 -07001671
1672function extract_img_data() {
1673 local image_file="$1"
1674 local out_dir="$2"
1675 local logFile="$TMPDIR/debugfs.log"
1676
1677 if [ ! -d "$out_dir" ]; then
1678 mkdir -p "$out_dir"
1679 fi
1680
1681 if [[ "$HOST_OS" == "Darwin" ]]; then
1682 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1683 echo "[-] Failed to extract data from '$image_file'"
1684 abort 1
1685 }
1686 else
1687 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1688 do
1689 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1690 echo "[-] Failed to extract data from '$image_file'"
1691 abort 1
1692 }
1693 done
1694 fi
1695
1696 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1697 if grep -Fq "$symlink_err" "$logFile"; then
1698 echo "[-] Symlinks have not been properly processed from $image_file"
1699 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1700 abort 1
1701 fi
1702}
1703
1704declare -ra VENDOR_SKIP_FILES=(
1705 "bin/toybox_vendor"
1706 "bin/toolbox"
1707 "bin/grep"
1708 "build.prop"
1709 "compatibility_matrix.xml"
1710 "default.prop"
1711 "etc/NOTICE.xml.gz"
1712 "etc/vintf/compatibility_matrix.xml"
1713 "etc/vintf/manifest.xml"
1714 "etc/wifi/wpa_supplicant.conf"
1715 "manifest.xml"
1716 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1717 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1718 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1719 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1720 "overlay/framework-res__auto_generated_rro.apk"
1721 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1722)
1723
1724function array_contains() {
1725 local element
1726 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1727 return 1
1728}
1729
1730function generate_prop_list_from_image() {
1731 local image_file="$1"
1732 local image_dir="$TMPDIR/image-temp"
1733 local output_list="$2"
1734 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1735 local -n skipped_vendor_files="$3"
1736
1737 extract_img_data "$image_file" "$image_dir"
1738
1739 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1740 do
1741 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1742 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1743 continue
1744 fi
1745 # Skip device defined skipped files since they will be re-generated at build time
1746 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1747 continue
1748 fi
1749 if suffix_match_file ".apk" "$FILE" ; then
1750 echo "-vendor/$FILE" >> "$output_list_tmp"
1751 else
1752 echo "vendor/$FILE" >> "$output_list_tmp"
1753 fi
1754 done
1755
1756 # Sort merged file with all lists
1757 sort -u "$output_list_tmp" > "$output_list"
1758
1759 # Clean-up
1760 rm -f "$output_list_tmp"
1761}