blob: 634307eee5b2cd93a5f8564f40348bbfd76ec0d8 [file] [log] [blame]
Steve Kondik5bd66602016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020020PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070021PRODUCT_PACKAGES_LIST=()
22PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020023PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070024PACKAGE_LIST=()
25VENDOR_STATE=-1
26VENDOR_RADIO_STATE=-1
27COMMON=-1
28ARCHES=
29FULLY_DEODEXED=-1
30
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040031TMPDIR=$(mktemp -d)
Steve Kondik5bd66602016-07-15 10:39:58 -070032
33#
34# cleanup
35#
36# kill our tmpfiles with fire on exit
37#
38function cleanup() {
39 rm -rf "${TMPDIR:?}"
40}
41
Gabriele Mb8e54572017-10-11 12:55:51 +020042trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070043
44#
45# setup_vendor
46#
47# $1: device name
48# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000049# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070050# $4: is common device - optional, default to false
51# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050052# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070053#
54# Must be called before any other functions can be used. This
55# sets up the internal state for a new vendor configuration.
56#
57function setup_vendor() {
58 local DEVICE="$1"
59 if [ -z "$DEVICE" ]; then
60 echo "\$DEVICE must be set before including this script!"
61 exit 1
62 fi
63
64 export VENDOR="$2"
65 if [ -z "$VENDOR" ]; then
66 echo "\$VENDOR must be set before including this script!"
67 exit 1
68 fi
69
theimpulson9a911af2019-08-14 03:25:12 +000070 export OMNI_ROOT="$3"
71 if [ ! -d "$OMNI_ROOT" ]; then
72 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070073 exit 1
74 fi
75
76 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000077 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
78 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070079 fi
80
Jake Whatley9843b322017-01-25 21:49:16 -050081 VNDNAME="$6"
82 if [ -z "$VNDNAME" ]; then
83 VNDNAME="$DEVICE"
84 fi
85
theimpulsonbb72ab82019-08-14 06:03:32 +000086 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -070087 export ANDROIDBP="$OMNI_ROOT"/"$OUTDIR"/Android.bp
theimpulson9a911af2019-08-14 03:25:12 +000088 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
89 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070090
91 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
92 COMMON=1
93 else
94 COMMON=0
95 fi
96
Gabriele Mc44696d2017-05-01 18:22:04 +020097 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070098 VENDOR_STATE=1
99 VENDOR_RADIO_STATE=1
100 else
101 VENDOR_STATE=0
102 VENDOR_RADIO_STATE=0
103 fi
104}
105
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300106# Helper functions for parsing a spec.
107# notes: an optional "|SHA1" that may appear in the format is stripped
108# early from the spec in the parse_file_list function, and
109# should not be present inside the input parameter passed
110# to these functions.
111
112#
113# input: spec in the form of "src[:dst][;args]"
114# output: "src"
115#
116function src_file() {
117 local SPEC="$1"
118 local SPLIT=(${SPEC//:/ })
119 local ARGS="$(target_args ${SPEC})"
120 # Regardless of there being a ":" delimiter or not in the spec,
121 # the source file is always either the first, or the only entry.
122 local SRC="${SPLIT[0]}"
123 # Remove target_args suffix, if present
124 echo "${SRC%;${ARGS}}"
125}
126
Steve Kondik5bd66602016-07-15 10:39:58 -0700127#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300128# input: spec in the form of "src[:dst][;args]"
129# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700130#
131function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800132 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300133 local SPLIT=(${SPEC//:/ })
134 local ARGS="$(target_args ${SPEC})"
135 local DST=
136 case ${#SPLIT[@]} in
137 1)
138 # The spec doesn't have a : delimiter
139 DST="${SPLIT[0]}"
140 ;;
141 *)
142 # The spec actually has a src:dst format
143 DST="${SPLIT[1]}"
144 ;;
145 esac
146 # Remove target_args suffix, if present
147 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700148}
149
150#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300151# input: spec in the form of "src[:dst][;args]"
152# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700153#
154function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300155 local SPEC="$1"
156 local SPLIT=(${SPEC//;/ })
157 local ARGS=
158 case ${#SPLIT[@]} in
159 1)
160 # No ";" delimiter in the spec.
161 ;;
162 *)
163 # The "args" are whatever comes after the ";" character.
164 # Basically the spec stripped of whatever is to the left of ";".
165 ARGS="${SPEC#${SPLIT[0]};}"
166 ;;
167 esac
168 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700169}
170
171#
172# prefix_match:
173#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300174# input:
175# - $1: prefix
176# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
177# output:
178# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700179#
180function prefix_match() {
181 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300182 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
183 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700184 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300185 local ARGS=$(target_args "$LINE")
186 if [ -z "${ARGS}" ]; then
187 echo "${FILE#$PREFIX}"
188 else
189 echo "${FILE#$PREFIX};${ARGS}"
190 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700191 fi
192 done
193}
194
195#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400196# prefix_match_file:
197#
198# $1: the prefix to match on
199# $2: the file to match the prefix for
200#
201# Internal function which returns true if a filename contains the
202# specified prefix.
203#
204function prefix_match_file() {
205 local PREFIX="$1"
206 local FILE="$2"
207 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
208 return 0
209 else
210 return 1
211 fi
212}
213
214#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700215# suffix_match_file:
216#
217# $1: the suffix to match on
218# $2: the file to match the suffix for
219#
220# Internal function which returns true if a filename contains the
221# specified suffix.
222#
223function suffix_match_file() {
224 local SUFFIX="$1"
225 local FILE="$2"
226 if [[ "$FILE" = *"$SUFFIX" ]]; then
227 return 0
228 else
229 return 1
230 fi
231}
232
233#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400234# truncate_file
235#
236# $1: the filename to truncate
237# $2: the argument to output the truncated filename to
238#
239# Internal function which truncates a filename by removing the first dir
240# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
241#
242function truncate_file() {
243 local FILE="$1"
244 RETURN_FILE="$2"
245 local FIND="${FILE%%/*}"
246 local LOCATION="${#FIND}+1"
247 echo ${FILE:$LOCATION}
248}
249
250#
Steve Kondik5bd66602016-07-15 10:39:58 -0700251# write_product_copy_files:
252#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400253# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400254#
Steve Kondik5bd66602016-07-15 10:39:58 -0700255# Creates the PRODUCT_COPY_FILES section in the product makefile for all
256# items in the list which do not start with a dash (-).
257#
258function write_product_copy_files() {
259 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
260 local TARGET=
261 local FILE=
262 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400263 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700264
265 if [ "$COUNT" -eq "0" ]; then
266 return 0
267 fi
268
269 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
270 for (( i=1; i<COUNT+1; i++ )); do
271 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
272 LINEEND=" \\"
273 if [ "$i" -eq "$COUNT" ]; then
274 LINEEND=""
275 fi
276
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300277 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400278 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400279 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400280 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400281 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400282 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400283 local OUTTARGET=$(truncate_file $TARGET)
284 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
285 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
286 elif prefix_match_file "odm/" $TARGET ; then
287 local OUTTARGET=$(truncate_file $TARGET)
288 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
289 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400290 elif prefix_match_file "vendor/odm/" $TARGET ; then
291 local OUTTARGET=$(truncate_file $TARGET)
292 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
293 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
294 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
295 local OUTTARGET=$(truncate_file $TARGET)
296 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
297 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
298 elif prefix_match_file "vendor/" $TARGET ; then
299 local OUTTARGET=$(truncate_file $TARGET)
300 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
301 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
302 elif prefix_match_file "system/vendor/" $TARGET ; then
303 local OUTTARGET=$(truncate_file $TARGET)
304 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
305 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400306 elif prefix_match_file "system/" $TARGET ; then
307 local OUTTARGET=$(truncate_file $TARGET)
308 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
309 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400310 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400311 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400312 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
313 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700314 done
315 return 0
316}
317
318#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700319# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700320#
321# $1: The LOCAL_MODULE_CLASS for the given module list
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400322# $2: /system, /odm, /product, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700323# $3: type-specific extra flags
324# $4: Name of the array holding the target list
325#
326# Internal function which writes out the BUILD_PREBUILT stanzas
327# for all modules in the list. This is called by write_product_packages
328# after the modules are categorized.
329#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700330function write_blueprint_packages() {
331
332 local CLASS="$1"
333 local PARTITION="$2"
334 local EXTRA="$3"
335
336 # Yes, this is a horrible hack - we create a new array using indirection
337 local ARR_NAME="$4[@]"
338 local FILELIST=("${!ARR_NAME}")
339
340 local FILE=
341 local ARGS=
342 local BASENAME=
343 local EXTENSION=
344 local PKGNAME=
345 local SRC=
346
347 for P in "${FILELIST[@]}"; do
348 FILE=$(target_file "$P")
349 ARGS=$(target_args "$P")
350
351 BASENAME=$(basename "$FILE")
352 DIRNAME=$(dirname "$FILE")
353 EXTENSION=${BASENAME##*.}
354 PKGNAME=${BASENAME%.*}
355
356 # Add to final package list
357 PACKAGE_LIST+=("$PKGNAME")
358
359 SRC="proprietary"
360 if [ "$PARTITION" = "system" ]; then
361 SRC+="/system"
362 elif [ "$PARTITION" = "vendor" ]; then
363 SRC+="/vendor"
364 elif [ "$PARTITION" = "product" ]; then
365 SRC+="/product"
366 elif [ "$PARTITION" = "odm" ]; then
367 SRC+="/odm"
368 fi
369
370 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
371 printf 'cc_prebuilt_library_shared {\n'
372 printf '\tname: "%s",\n' "$PKGNAME"
373 printf '\towner: "%s",\n' "$VENDOR"
374 printf '\tstrip: {\n'
375 printf '\t\tnone: true,\n'
376 printf '\t},\n'
377 printf '\ttarget: {\n'
378 if [ "$EXTRA" = "both" ]; then
379 printf '\t\tandroid_arm: {\n'
380 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
381 printf '\t\t},\n'
382 printf '\t\tandroid_arm64: {\n'
383 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
384 printf '\t\t},\n'
385 elif [ "$EXTRA" = "64" ]; then
386 printf '\t\tandroid_arm64: {\n'
387 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
388 printf '\t\t},\n'
389 else
390 printf '\t\tandroid_arm: {\n'
391 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
392 printf '\t\t},\n'
393 fi
394 printf '\t},\n'
395 if [ "$EXTRA" != "none" ]; then
396 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
397 fi
dianlujitao848101c2020-09-12 00:15:13 +0800398 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700399 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
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200459 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700460 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200461 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700462 fi
463 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200464 if [ "$CLASS" = "ETC" ] ; then
465 if [ "$DIRNAME" != "." ]; then
466 printf '\tsub_dir: "%s",\n' "$DIRNAME"
467 fi
468 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700469 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
470 printf '\tprefer: true,\n'
471 fi
472 if [ "$EXTRA" = "priv-app" ]; then
473 printf '\tprivileged: true,\n'
474 fi
475 if [ "$PARTITION" = "vendor" ]; then
476 printf '\tsoc_specific: true,\n'
477 elif [ "$PARTITION" = "product" ]; then
478 printf '\tproduct_specific: true,\n'
479 elif [ "$PARTITION" = "odm" ]; then
480 printf '\tdevice_specific: true,\n'
481 fi
482 printf '}\n\n'
483 done
484}
485
486#
487# write_makefile_packages:
488#
489# $1: The LOCAL_MODULE_CLASS for the given module list
490# $2: /odm, /product, or /vendor partition
491# $3: type-specific extra flags
492# $4: Name of the array holding the target list
493#
494# Internal function which writes out the BUILD_PREBUILT stanzas
495# for all modules in the list. This is called by write_product_packages
496# after the modules are categorized.
497#
498function write_makefile_packages() {
Steve Kondik5bd66602016-07-15 10:39:58 -0700499
500 local CLASS="$1"
razorlovesa0d296b2019-07-29 02:21:34 -0500501 local PARTITION="$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700502 local EXTRA="$3"
503
504 # Yes, this is a horrible hack - we create a new array using indirection
505 local ARR_NAME="$4[@]"
506 local FILELIST=("${!ARR_NAME}")
507
508 local FILE=
509 local ARGS=
510 local BASENAME=
511 local EXTENSION=
512 local PKGNAME=
513 local SRC=
514
515 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300516 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700517 ARGS=$(target_args "$P")
518
519 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100520 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700521 EXTENSION=${BASENAME##*.}
Mohd Farazcb0f4872019-10-08 16:13:50 +0530522 EXTENSION="."$EXTENSION
523 if [ "$EXTENSION" = ".jar" ]; then
524 EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
525 elif [ "$EXTENSION" = ".apk" ]; then
526 EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
527 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700528 PKGNAME=${BASENAME%.*}
529
530 # Add to final package list
531 PACKAGE_LIST+=("$PKGNAME")
532
533 SRC="proprietary"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400534 if [ "$PARTITION" = "system" ]; then
535 SRC+="/system"
536 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700537 SRC+="/vendor"
razorlovesa0d296b2019-07-29 02:21:34 -0500538 elif [ "$PARTITION" = "product" ]; then
539 SRC+="/product"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700540 elif [ "$PARTITION" = "odm" ]; then
541 SRC+="/odm"
Steve Kondik5bd66602016-07-15 10:39:58 -0700542 fi
543
544 printf 'include $(CLEAR_VARS)\n'
545 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
546 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
547 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
548 if [ "$EXTRA" = "both" ]; then
549 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
550 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
551 #if [ "$VENDOR_PKG" = "true" ]; then
552 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
553 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
554 #else
555 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
556 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
557 #fi
558 elif [ "$EXTRA" = "64" ]; then
559 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
560 else
561 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
562 fi
563 if [ "$EXTRA" != "none" ]; then
564 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
565 fi
566 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200567 if [ "$EXTRA" = "priv-app" ]; then
568 SRC="$SRC/priv-app"
569 else
570 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700571 fi
572 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
573 local CERT=platform
574 if [ ! -z "$ARGS" ]; then
575 CERT="$ARGS"
576 fi
577 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
578 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
579 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200580 local CERT=platform
581 if [ ! -z "$ARGS" ]; then
582 CERT="$ARGS"
583 fi
584 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700585 elif [ "$CLASS" = "ETC" ]; then
586 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
587 elif [ "$CLASS" = "EXECUTABLES" ]; then
588 if [ "$ARGS" = "rootfs" ]; then
589 SRC="$SRC/rootfs"
590 if [ "$EXTRA" = "sbin" ]; then
591 SRC="$SRC/sbin"
592 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
593 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
594 fi
595 else
596 SRC="$SRC/bin"
597 fi
598 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
599 unset EXTENSION
600 else
601 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
602 fi
603 printf 'LOCAL_MODULE_TAGS := optional\n'
604 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400605 if [ "$CLASS" = "APPS" ]; then
606 printf 'LOCAL_DEX_PREOPT := false\n'
607 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700608 if [ ! -z "$EXTENSION" ]; then
Mohd Farazcb0f4872019-10-08 16:13:50 +0530609 printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
Steve Kondik5bd66602016-07-15 10:39:58 -0700610 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100611 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
612 if [ "$DIRNAME" != "." ]; then
613 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
614 fi
615 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700616 if [ "$EXTRA" = "priv-app" ]; then
617 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
618 fi
razorlovesa0d296b2019-07-29 02:21:34 -0500619 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800620 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesa0d296b2019-07-29 02:21:34 -0500621 elif [ "$PARTITION" = "product" ]; then
622 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700623 elif [ "$PARTITION" = "odm" ]; then
624 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700625 fi
626 printf 'include $(BUILD_PREBUILT)\n\n'
627 done
628}
629
630#
631# write_product_packages:
632#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700633# This function will create prebuilt entries in the
634# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700635# product makefile for all files in the blob list which
636# start with a single dash (-) character.
637#
638function write_product_packages() {
639 PACKAGE_LIST=()
640
641 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
642
643 if [ "$COUNT" = "0" ]; then
644 return 0
645 fi
646
647 # Figure out what's 32-bit, what's 64-bit, and what's multilib
648 # I really should not be doing this in bash due to shitty array passing :(
649 local T_LIB32=( $(prefix_match "lib/") )
650 local T_LIB64=( $(prefix_match "lib64/") )
651 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
652 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
653 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
654
655 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700656 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700657 fi
658 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700659 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700660 fi
661 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700662 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700663 fi
664
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400665 local T_S_LIB32=( $(prefix_match "system/lib/") )
666 local T_S_LIB64=( $(prefix_match "system/lib64/") )
667 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
668 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
669 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
670
671 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700672 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400673 fi
674 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700675 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400676 fi
677 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700678 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400679 fi
680
Steve Kondik5bd66602016-07-15 10:39:58 -0700681 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
682 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
683 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
684 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
685 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
686
687 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700688 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700689 fi
690 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700691 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700692 fi
693 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700694 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500695 fi
696
697 local T_P_LIB32=( $(prefix_match "product/lib/") )
698 local T_P_LIB64=( $(prefix_match "product/lib64/") )
699 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
700 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
701 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
702
703 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700704 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500705 fi
706 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700707 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500708 fi
709 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700710 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700711 fi
712
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700713 local T_O_LIB32=( $(prefix_match "odm/lib/") )
714 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
715 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
716 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
717 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
718
719 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700720 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700721 fi
722 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700723 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700724 fi
725 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700726 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700727 fi
728
Steve Kondik5bd66602016-07-15 10:39:58 -0700729 # Apps
730 local APPS=( $(prefix_match "app/") )
731 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100732 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700733 fi
734 local PRIV_APPS=( $(prefix_match "priv-app/") )
735 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100736 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700737 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400738 local S_APPS=( $(prefix_match "system/app/") )
739 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100740 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400741 fi
742 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
743 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100744 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400745 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700746 local V_APPS=( $(prefix_match "vendor/app/") )
747 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100748 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700749 fi
750 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
751 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100752 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500753 fi
754 local P_APPS=( $(prefix_match "product/app/") )
755 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100756 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500757 fi
758 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
759 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100760 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700761 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700762 local O_APPS=( $(prefix_match "odm/app/") )
763 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100764 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700765 fi
766 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
767 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100768 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700769 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700770
771 # Framework
772 local FRAMEWORK=( $(prefix_match "framework/") )
773 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700774 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700775 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400776 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
777 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700778 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400779 fi
Christian Oder974b5902017-10-08 23:15:52 +0200780 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200781 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700782 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500783 fi
784 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
785 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700786 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200787 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700788 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
789 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700790 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700791 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700792
793 # Etc
794 local ETC=( $(prefix_match "etc/") )
795 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700796 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700797 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400798 local S_ETC=( $(prefix_match "system/etc/") )
799 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700800 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400801 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700802 local V_ETC=( $(prefix_match "vendor/etc/") )
803 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700804 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500805 fi
806 local P_ETC=( $(prefix_match "product/etc/") )
807 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700808 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700809 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700810 local O_ETC=( $(prefix_match "odm/etc/") )
811 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700812 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700813 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700814
815 # Executables
816 local BIN=( $(prefix_match "bin/") )
817 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700818 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700819 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400820 local S_BIN=( $(prefix_match "system/bin/") )
821 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700822 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400823 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700824 local V_BIN=( $(prefix_match "vendor/bin/") )
825 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700826 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500827 fi
828 local P_BIN=( $(prefix_match "product/bin/") )
829 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700830 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700831 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700832 local O_BIN=( $(prefix_match "odm/bin/") )
833 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700834 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700835 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700836 local SBIN=( $(prefix_match "sbin/") )
837 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700838 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700839 fi
840
841
842 # Actually write out the final PRODUCT_PACKAGES list
843 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
844
845 if [ "$PACKAGE_COUNT" -eq "0" ]; then
846 return 0
847 fi
848
849 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
850 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
851 local LINEEND=" \\"
852 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
853 LINEEND=""
854 fi
855 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
856 done
857}
858
859#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700860# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700861#
862# $1: file which will be written to
863#
864# writes out the copyright header with the current year.
865# note that this is not an append operation, and should
866# be executed first!
867#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700868function write_blueprint_header() {
869 if [ -f $1 ]; then
870 rm $1
871 fi
872
873 YEAR=$(date +"%Y")
874
875 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
876
877 printf "/**\n" > $1
878 NUM_REGEX='^[0-9]+$'
879 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
880 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
881 else
882 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
883 fi
884
885 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
886 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
887 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
888 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
889 else
890 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
891 fi
892
893 cat << EOF >> $1
894 *
895 * Licensed under the Apache License, Version 2.0 (the "License");
896 * you may not use this file except in compliance with the License.
897 * You may obtain a copy of the License at
898 *
899 * http://www.apache.org/licenses/LICENSE-2.0
900 *
901 * Unless required by applicable law or agreed to in writing, software
902 * distributed under the License is distributed on an "AS IS" BASIS,
903 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
904 * See the License for the specific language governing permissions and
905 * limitations under the License.
906 *
907 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
908 */
909
910EOF
911}
912
913#
914# write_makefile_header:
915#
916# $1: file which will be written to
917#
918# writes out the copyright header with the current year.
919# note that this is not an append operation, and should
920# be executed first!
921#
922function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500923 if [ -f $1 ]; then
924 rm $1
925 fi
926
Steve Kondik5bd66602016-07-15 10:39:58 -0700927 YEAR=$(date +"%Y")
928
929 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
930
Jake Whatley9843b322017-01-25 21:49:16 -0500931 NUM_REGEX='^[0-9]+$'
932 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
933 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
934 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
935 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
936 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
937 fi
938 if [ $YEAR -eq 2017 ]; then
939 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
940 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
941 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
942 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
943 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
944 else
945 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
946 fi
947 else
948 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
949 fi
950
951 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700952#
953# Licensed under the Apache License, Version 2.0 (the "License");
954# you may not use this file except in compliance with the License.
955# You may obtain a copy of the License at
956#
957# http://www.apache.org/licenses/LICENSE-2.0
958#
959# Unless required by applicable law or agreed to in writing, software
960# distributed under the License is distributed on an "AS IS" BASIS,
961# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
962# See the License for the specific language governing permissions and
963# limitations under the License.
964
965# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
966
967EOF
968}
969
970#
971# write_headers:
972#
973# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500974# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700975#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700976# Calls write_makefile_header for each of the makefiles and
977# write_blueprint_header for Android.bp and creates the initial
978# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -0700979#
980function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700981 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500982
983 GUARD="$2"
984 if [ -z "$GUARD" ]; then
985 GUARD="TARGET_DEVICE"
986 fi
987
Steve Kondik5bd66602016-07-15 10:39:58 -0700988 cat << EOF >> "$ANDROIDMK"
989LOCAL_PATH := \$(call my-dir)
990
991EOF
992 if [ "$COMMON" -ne 1 ]; then
993 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500994ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700995
996EOF
997 else
998 if [ -z "$1" ]; then
999 echo "Argument with devices to be added to guard must be set!"
1000 exit 1
1001 fi
1002 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001003ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001004
1005EOF
1006 fi
1007
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001008 write_makefile_header "$BOARDMK"
1009 write_makefile_header "$PRODUCTMK"
1010 write_blueprint_header "$ANDROIDBP"
1011
1012 cat << EOF >> "$ANDROIDBP"
1013soong_namespace {
1014}
1015
1016EOF
1017
1018 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1019 cat << EOF >> "$PRODUCTMK"
1020PRODUCT_SOONG_NAMESPACES += \\
1021 vendor/$VENDOR/$DEVICE
1022
1023EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001024}
1025
1026#
1027# write_footers:
1028#
1029# Closes the inital guard and any other finalization tasks. Must
1030# be called as the final step.
1031#
1032function write_footers() {
1033 cat << EOF >> "$ANDROIDMK"
1034endif
1035EOF
1036}
1037
1038# Return success if adb is up and not in recovery
1039function _adb_connected {
1040 {
Jake Whatley9843b322017-01-25 21:49:16 -05001041 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001042 then
1043 return 0
1044 fi
1045 } 2>/dev/null
1046
1047 return 1
1048};
1049
1050#
1051# parse_file_list:
1052#
1053# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001054# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001055#
1056# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1057#
1058function parse_file_list() {
1059 if [ -z "$1" ]; then
1060 echo "An input file is expected!"
1061 exit 1
1062 elif [ ! -f "$1" ]; then
1063 echo "Input file "$1" does not exist!"
1064 exit 1
1065 fi
1066
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001067 if [ -n "$2" ]; then
1068 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001069 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001070 # Match all lines starting with first line found to start* with '#'
1071 # comment and contain** $2, and ending with first line to be empty*.
1072 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1073 # **the $2 match is case-insensitive
1074 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001075 else
1076 LIST=$1
1077 fi
1078
1079
Steve Kondik5bd66602016-07-15 10:39:58 -07001080 PRODUCT_PACKAGES_LIST=()
1081 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001082 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001083 PRODUCT_COPY_FILES_LIST=()
1084 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001085 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001086
1087 while read -r line; do
1088 if [ -z "$line" ]; then continue; fi
1089
1090 # If the line has a pipe delimiter, a sha1 hash should follow.
1091 # This indicates the file should be pinned and not overwritten
1092 # when extracting files.
1093 local SPLIT=(${line//\|/ })
1094 local COUNT=${#SPLIT[@]}
1095 local SPEC=${SPLIT[0]}
1096 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001097 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001098 if [ "$COUNT" -gt "1" ]; then
1099 HASH=${SPLIT[1]}
1100 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001101 if [ "$COUNT" -gt "2" ]; then
1102 FIXUP_HASH=${SPLIT[2]}
1103 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001104
1105 # if line starts with a dash, it needs to be packaged
1106 if [[ "$SPEC" =~ ^- ]]; then
1107 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1108 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001109 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001110 else
1111 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1112 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001113 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001114 fi
1115
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001116 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001117}
1118
1119#
1120# write_makefiles:
1121#
1122# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001123# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001124#
1125# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001126# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -07001127# the product makefile.
1128#
1129function write_makefiles() {
1130 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001131 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001132 write_product_packages
1133}
1134
1135#
1136# append_firmware_calls_to_makefiles:
1137#
1138# Appends to Android.mk the calls to all images present in radio folder
1139# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1140#
1141function append_firmware_calls_to_makefiles() {
1142 cat << EOF >> "$ANDROIDMK"
1143ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1144
1145RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1146\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1147 \$(call add-radio-file,radio/\$(f)))
1148\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1149
1150endif
1151
1152EOF
1153}
1154
1155#
1156# get_file:
1157#
1158# $1: input file
1159# $2: target file/folder
1160# $3: source of the file (can be "adb" or a local folder)
1161#
1162# Silently extracts the input file to defined target
1163# Returns success if file can be pulled from the device or found locally
1164#
1165function get_file() {
1166 local SRC="$3"
1167
1168 if [ "$SRC" = "adb" ]; then
1169 # try to pull
1170 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1171
1172 return 1
1173 else
1174 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001175 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1176 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001177 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001178
1179 return 1
1180 fi
1181};
1182
1183#
1184# oat2dex:
1185#
1186# $1: extracted apk|jar (to check if deodex is required)
1187# $2: odexed apk|jar to deodex
1188# $3: source of the odexed apk|jar
1189#
1190# Convert apk|jar .odex in the corresposing classes.dex
1191#
1192function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001193 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001194 local OEM_TARGET="$2"
1195 local SRC="$3"
1196 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001197 local OAT=
XiNGRZd7793bc2019-12-24 10:37:13 +08001198 local HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik5bd66602016-07-15 10:39:58 -07001199
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001200 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1201 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1202 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001203 fi
1204
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001205 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001206 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001207 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001208
codeworkx85eda752018-09-23 12:36:57 +02001209 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001210 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001211 fi
1212
Steve Kondik5bd66602016-07-15 10:39:58 -07001213 # Extract existing boot.oats to the temp folder
1214 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001215 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001216 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001217 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001218 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001219 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001220 else
1221 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001222 fi
1223 done
1224 fi
1225
1226 if [ -z "$ARCHES" ]; then
1227 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1228 fi
1229
theimpulson9a911af2019-08-14 03:25:12 +00001230 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001231 return;
1232 fi
1233
theimpulson9a911af2019-08-14 03:25:12 +00001234 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001235 return 0 # target apk|jar is already odexed, return
1236 fi
1237
1238 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001239 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001240
Joe Maplesfb3941c2018-01-05 14:51:33 -05001241 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1242 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001243
Joe Maplesfb3941c2018-01-05 14:51:33 -05001244 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1245 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1246 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001247 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1248 for CLASS in $CLASSES; do
1249 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1250 # Check if we have to deal with CompactDex
1251 if [[ "$CLASS" == *.cdex ]]; then
1252 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1253 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1254 else
1255 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1256 fi
1257 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001258 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001259 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1260 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001261 fi
theimpulson9a911af2019-08-14 03:25:12 +00001262 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001263 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001264 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001265 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001266 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001267 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001268 # try to extract classes.dex from boot.vdex for frameworks jars
1269 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001270 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001271 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001272 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1273 for CLASS in $CLASSES; do
1274 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1275 # Check if we have to deal with CompactDex
1276 if [[ "$CLASS" == *.cdex ]]; then
1277 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1278 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1279 else
1280 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1281 fi
1282 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001283 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001284 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1285 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001286 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001287 else
1288 continue
1289 fi
1290
Steve Kondik5bd66602016-07-15 10:39:58 -07001291 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001292
1293 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001294}
1295
1296#
1297# init_adb_connection:
1298#
1299# Starts adb server and waits for the device
1300#
1301function init_adb_connection() {
1302 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1303 if ! _adb_connected; then
1304 echo "No device is online. Waiting for one..."
1305 echo "Please connect USB and/or enable USB debugging"
1306 until _adb_connected; do
1307 sleep 1
1308 done
1309 echo "Device Found."
1310 fi
1311
1312 # Retrieve IP and PORT info if we're using a TCP connection
1313 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1314 | head -1 | awk '{print $1}')
1315 adb root &> /dev/null
1316 sleep 0.3
1317 if [ -n "$TCPIPPORT" ]; then
1318 # adb root just killed our connection
1319 # so reconnect...
1320 adb connect "$TCPIPPORT"
1321 fi
1322 adb wait-for-device &> /dev/null
1323 sleep 0.3
1324}
1325
1326#
1327# fix_xml:
1328#
1329# $1: xml file to fix
1330#
1331function fix_xml() {
1332 local XML="$1"
1333 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1334
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001335 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1336 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001337
1338 mv "$TEMP_XML" "$XML"
1339}
1340
Vladimir Olteande985fe2019-01-17 03:07:34 +02001341function get_hash() {
1342 local FILE="$1"
1343
1344 if [ "$(uname)" == "Darwin" ]; then
1345 shasum "${FILE}" | awk '{print $1}'
1346 else
1347 sha1sum "${FILE}" | awk '{print $1}'
1348 fi
1349}
1350
Vladimir Olteana7d20492019-01-17 03:05:52 +02001351function print_spec() {
1352 local SPEC_PRODUCT_PACKAGE="$1"
1353 local SPEC_SRC_FILE="$2"
1354 local SPEC_DST_FILE="$3"
1355 local SPEC_ARGS="$4"
1356 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001357 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001358
1359 local PRODUCT_PACKAGE=""
1360 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1361 PRODUCT_PACKAGE="-"
1362 fi
1363 local SRC=""
1364 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1365 SRC="${SPEC_SRC_FILE}:"
1366 fi
1367 local DST=""
1368 if [ ! -z "${SPEC_DST_FILE}" ]; then
1369 DST="${SPEC_DST_FILE}"
1370 fi
1371 local ARGS=""
1372 if [ ! -z "${SPEC_ARGS}" ]; then
1373 ARGS=";${SPEC_ARGS}"
1374 fi
1375 local HASH=""
1376 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1377 HASH="|${SPEC_HASH}"
1378 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001379 local FIXUP_HASH=""
1380 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1381 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1382 fi
1383 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1384}
1385
1386# To be overridden by device-level extract-files.sh
1387# Parameters:
1388# $1: spec name of a blob. Can be used for filtering.
1389# If the spec is "src:dest", then $1 is "dest".
1390# If the spec is "src", then $1 is "src".
1391# $2: path to blob file. Can be used for fixups.
1392#
1393function blob_fixup() {
1394 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001395}
1396
Steve Kondik5bd66602016-07-15 10:39:58 -07001397#
1398# extract:
1399#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001400# Positional parameters:
1401# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001402# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001403# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1404#
1405# Non-positional parameters (coming after $2):
1406# --section: preferred way of selecting the portion to parse and extract from
1407# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001408# --kang: if present, this option will activate the printing of hashes for the
1409# extracted blobs. Useful with --section for subsequent pinning of
1410# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001411#
1412function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001413 # Consume positional parameters
1414 local PROPRIETARY_FILES_TXT="$1"; shift
1415 local SRC="$1"; shift
1416 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001417 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001418
1419 # Consume optional, non-positional parameters
1420 while [ "$#" -gt 0 ]; do
1421 case "$1" in
1422 -s|--section)
1423 SECTION="$2"; shift
1424 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001425 -k|--kang)
1426 KANG=true
1427 DISABLE_PINNING=1
1428 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001429 *)
1430 # Backwards-compatibility with the old behavior, where $3, if
1431 # present, denoted an optional positional ${SECTION} argument.
1432 # Users of ${SECTION} are encouraged to migrate from setting it as
1433 # positional $3, to non-positional --section ${SECTION}, the
1434 # reason being that it doesn't scale to have more than 1 optional
1435 # positional argument.
1436 SECTION="$1"
1437 ;;
1438 esac
1439 shift
1440 done
1441
Steve Kondik5bd66602016-07-15 10:39:58 -07001442 if [ -z "$OUTDIR" ]; then
1443 echo "Output dir not set!"
1444 exit 1
1445 fi
1446
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001447 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001448
1449 # Allow failing, so we can try $DEST and/or $FILE
1450 set +e
1451
1452 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1453 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001454 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001455 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001456 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001457 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001458 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1459
1460 if [ "$SRC" = "adb" ]; then
1461 init_adb_connection
1462 fi
1463
Dan Pasanen0cc05012017-03-21 09:06:11 -05001464 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001465 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001466
1467 # Check if we're working with the same zip that was passed last time.
1468 # If so, let's just use what's already extracted.
1469 MD5=`md5sum "$SRC"| awk '{print $1}'`
1470 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1471
1472 if [ "$MD5" != "$OLDMD5" ]; then
1473 rm -rf "$DUMPDIR"
1474 mkdir "$DUMPDIR"
1475 unzip "$SRC" -d "$DUMPDIR"
1476 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1477
1478 # Stop if an A/B OTA zip is detected. We cannot extract these.
1479 if [ -a "$DUMPDIR"/payload.bin ]; then
1480 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1481 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001482 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001483
1484 for PARTITION in "system" "odm" "product" "vendor"
1485 do
1486 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001487 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1488 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1489 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1490 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1491 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001492 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1493 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1494 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1495 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1496 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
1497 echo "Requesting sudo access to mount the "$PARTITION".img"
1498 sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
1499 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
1500 sudo umount "$DUMPDIR"/tmp
1501 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
1502 fi
1503 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001504 fi
1505
1506 SRC="$DUMPDIR"
1507 fi
1508
Steve Kondik5bd66602016-07-15 10:39:58 -07001509 if [ "$VENDOR_STATE" -eq "0" ]; then
1510 echo "Cleaning output directory ($OUTPUT_ROOT).."
1511 rm -rf "${OUTPUT_TMP:?}"
1512 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001513 if [ -d "$OUTPUT_ROOT" ]; then
1514 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1515 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001516 VENDOR_STATE=1
1517 fi
1518
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001519 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001520
1521 for (( i=1; i<COUNT+1; i++ )); do
1522
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001523 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001524 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001525 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001526 local OUTPUT_DIR=
1527 local TMP_DIR=
1528 local SRC_FILE=
1529 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001530 local IS_PRODUCT_PACKAGE=false
1531
1532 # Note: this relies on the fact that the ${FILELIST[@]} array
1533 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1534 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1535 IS_PRODUCT_PACKAGE=true
1536 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001537
Vladimir Olteand6391332018-06-24 20:42:01 +03001538 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001539 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1540 TMP_DIR="${OUTPUT_TMP}/rootfs"
1541 SRC_FILE="/${SPEC_SRC_FILE}"
1542 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001543 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001544 OUTPUT_DIR="${OUTPUT_ROOT}"
1545 TMP_DIR="${OUTPUT_TMP}"
1546 SRC_FILE="/system/${SPEC_SRC_FILE}"
1547 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001548 fi
1549
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001550 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001551 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001552 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001553 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001554
Gabriele M58270a32017-11-13 23:15:29 +01001555 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001556 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001557 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001558 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001559 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001560 if [ -f "${VENDOR_REPO_FILE}" ]; then
1561 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001562 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001563 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001564 fi
1565 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001566 local TMP_HASH=$(get_hash "${PINNED}")
1567 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001568 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001569 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1570 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001571 fi
1572 fi
1573 fi
1574 fi
1575
Vladimir Olteana7d20492019-01-17 03:05:52 +02001576 if [ "${KANG}" = false ]; then
1577 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1578 fi
1579
Gabriele M58270a32017-11-13 23:15:29 +01001580 if [ "$KEEP" = "1" ]; then
Vladimir Olteana7d20492019-01-17 03:05:52 +02001581 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001582 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001583 FOUND=false
1584 # Try Lineage target first.
1585 # Also try to search for files stripped of
1586 # the "/system" prefix, if we're actually extracting
1587 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001588 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001589 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1590 FOUND=true
1591 break
1592 }
1593 done
1594
1595 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001596 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001597 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001598 fi
1599 fi
1600
Vladimir Olteande985fe2019-01-17 03:07:34 +02001601 # Blob fixup pipeline has 2 parts: one that is fixed and
1602 # one that is user-configurable
1603 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1604 # Deodex apk|jar if that's the case
1605 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1606 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1607 if [ -f "$TMPDIR/classes.dex" ]; then
dianlujitaoded7c1e2020-04-06 12:45:36 +08001608 touch -t 200901010000 "$TMPDIR/classes"*
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001609 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1610 rm "$TMPDIR/classes"*
Vladimir Olteande985fe2019-01-17 03:07:34 +02001611 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001612 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001613 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1614 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001615 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001616 # Now run user-supplied fixup function
1617 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1618 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001619
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001620 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001621 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001622 local TYPE="${DIR##*/}"
1623 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001624 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001625 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001626 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001627 fi
1628 fi
1629
Vladimir Olteana7d20492019-01-17 03:05:52 +02001630 if [ "${KANG}" = true ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001631 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1632 fi
1633
1634 # Check and print whether the fixup pipeline actually did anything.
1635 # This isn't done right after the fixup pipeline because we want this print
1636 # to come after print_spec above, when in kang mode.
1637 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1638 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1639 # Now sanity-check the spec for this blob.
1640 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1641 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1642 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1643 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001644 fi
1645
Steve Kondik5bd66602016-07-15 10:39:58 -07001646 done
1647
1648 # Don't allow failing
1649 set -e
1650}
1651
1652#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001653# extract2:
1654#
1655# Positional parameters:
1656# $1: file containing the list of items to extract (aka proprietary-files.txt)
1657#
1658# Non-positional parameters (coming after $2):
1659# --section: selects the portion to parse and extracts from proprietary-files.txt
1660# --kang: if present, this option will activate the printing of hashes for the
1661# extracted blobs. Useful with --section for subsequent pinning of
1662# blobs taken from other origins.
1663#
1664function extract2() {
1665 # Consume positional parameters
1666 local PROPRIETARY_FILES_TXT="$1"; shift
1667 local SECTION=""
1668 local KANG=false
1669
1670 # Consume optional, non-positional parameters
1671 while [ "$#" -gt 0 ]; do
1672 case "$1" in
1673 --adb)
1674 ADB=true
1675 ;;
1676 --system)
1677 SYSTEM_SRC="$2"; shift
1678 ;;
1679 --vendor)
1680 VENDOR_SRC="$2"; shift
1681 ;;
1682 --odm)
1683 ODM_SRC="$2"; shift
1684 ;;
1685 --product)
1686 PRODUCT_SRC="$2"; shift
1687 ;;
1688 -s|--section)
1689 SECTION="$2"; shift
1690 ;;
1691 -k|--kang)
1692 KANG=true
1693 DISABLE_PINNING=1
1694 ;;
1695 esac
1696 shift
1697 done
1698
1699 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1700 echo "No sources set! You must select --adb or pass paths to partition dumps."
1701 exit 1
1702 fi
1703
1704 if [ -z "$OUTDIR" ]; then
1705 echo "Output dir not set!"
1706 exit 1
1707 fi
1708
1709 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1710
1711 # Allow failing, so we can try $DEST and/or $FILE
1712 set +e
1713
1714 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1715 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1716 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1717 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1718 local COUNT=${#FILELIST[@]}
1719 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1720 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1721
1722 if [ "$ADB" = true ]; then
1723 init_adb_connection
1724 fi
1725
1726 if [ "$VENDOR_STATE" -eq "0" ]; then
1727 echo "Cleaning output directory ($OUTPUT_ROOT).."
1728 rm -rf "${OUTPUT_TMP:?}"
1729 mkdir -p "${OUTPUT_TMP:?}"
1730 if [ -d "$OUTPUT_ROOT" ]; then
1731 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1732 fi
1733 VENDOR_STATE=1
1734 fi
1735
1736 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1737
1738 for (( i=1; i<COUNT+1; i++ )); do
1739
1740 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1741 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1742 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1743 local OUTPUT_DIR=
1744 local TMP_DIR=
1745 local SRC_FILE=
1746 local DST_FILE=
1747 local IS_PRODUCT_PACKAGE=false
1748
1749 # Note: this relies on the fact that the ${FILELIST[@]} array
1750 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1751 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1752 IS_PRODUCT_PACKAGE=true
1753 fi
1754
1755 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1756 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1757 TMP_DIR="${OUTPUT_TMP}/rootfs"
1758 else
1759 OUTPUT_DIR="${OUTPUT_ROOT}"
1760 TMP_DIR="${OUTPUT_TMP}"
1761 fi
1762 SRC_FILE="${SPEC_SRC_FILE}"
1763 DST_FILE="${SPEC_DST_FILE}"
1764
1765 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1766 local BLOB_DISPLAY_NAME="${DST_FILE}"
1767 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1768
1769 # Check pinned files
1770 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1771 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1772 local KEEP=""
1773 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1774 if [ -f "${VENDOR_REPO_FILE}" ]; then
1775 local PINNED="${VENDOR_REPO_FILE}"
1776 else
1777 local PINNED="${TMP_DIR}${DST_FILE}"
1778 fi
1779 if [ -f "$PINNED" ]; then
1780 local TMP_HASH=$(get_hash "${PINNED}")
1781 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1782 KEEP="1"
1783 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1784 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1785 fi
1786 fi
1787 fi
1788 fi
1789
1790 if [ "${KANG}" = false ]; then
1791 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1792 fi
1793
1794 if [ "$KEEP" = "1" ]; then
1795 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1796 else
1797 FOUND=false
1798 PARTITION_SOURCE_DIR=
1799 # Try Lineage target first.
1800 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1801 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1802 if [ "$PARTITION" = "system" ]; then
1803 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1804 elif [ "$PARTITION" = "vendor" ]; then
1805 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1806 elif [ "$PARTITION" = "product" ]; then
1807 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1808 elif [ "$PARTITION" = "odm" ]; then
1809 PARTITION_SOURCE_DIR="$ODM_SRC"
1810 fi
1811 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1812 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1813 FOUND=true
1814 break
1815 }
1816 # Search with the full system/ prefix if the file was not found on the system partition
1817 # because we may be searching in a mounted system-as-root system.img
1818 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1819 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1820 FOUND=true
1821 break
1822 }
1823 fi
1824 done
1825
1826 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1827 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1828 fi
1829
1830 if [ "${FOUND}" = false ]; then
1831 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1832 continue
1833 fi
1834 fi
1835
1836 # Blob fixup pipeline has 2 parts: one that is fixed and
1837 # one that is user-configurable
1838 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1839 # Deodex apk|jar if that's the case
1840 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1841 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1842 if [ -f "$TMPDIR/classes.dex" ]; then
1843 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1844 rm "$TMPDIR/classes"*
1845 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1846 fi
1847 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1848 fix_xml "${VENDOR_REPO_FILE}"
1849 fi
1850 # Now run user-supplied fixup function
1851 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1852 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1853
1854 if [ -f "${VENDOR_REPO_FILE}" ]; then
1855 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1856 local TYPE="${DIR##*/}"
1857 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1858 chmod 755 "${VENDOR_REPO_FILE}"
1859 else
1860 chmod 644 "${VENDOR_REPO_FILE}"
1861 fi
1862 fi
1863
1864 if [ "${KANG}" = true ]; then
1865 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1866 fi
1867
1868 # Check and print whether the fixup pipeline actually did anything.
1869 # This isn't done right after the fixup pipeline because we want this print
1870 # to come after print_spec above, when in kang mode.
1871 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1872 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1873 # Now sanity-check the spec for this blob.
1874 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1875 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1876 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1877 fi
1878 fi
1879
1880 done
1881
1882 # Don't allow failing
1883 set -e
1884}
1885
1886#
Steve Kondik5bd66602016-07-15 10:39:58 -07001887# extract_firmware:
1888#
1889# $1: file containing the list of items to extract
1890# $2: path to extracted radio folder
1891#
1892function extract_firmware() {
1893 if [ -z "$OUTDIR" ]; then
1894 echo "Output dir not set!"
1895 exit 1
1896 fi
1897
1898 parse_file_list "$1"
1899
1900 # Don't allow failing
1901 set -e
1902
1903 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1904 local COUNT=${#FILELIST[@]}
1905 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001906 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001907
1908 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1909 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1910 rm -rf "${OUTPUT_DIR:?}/"*
1911 VENDOR_RADIO_STATE=1
1912 fi
1913
1914 echo "Extracting $COUNT files in $1 from $SRC:"
1915
1916 for (( i=1; i<COUNT+1; i++ )); do
1917 local FILE="${FILELIST[$i-1]}"
1918 printf ' - %s \n' "/radio/$FILE"
1919
1920 if [ ! -d "$OUTPUT_DIR" ]; then
1921 mkdir -p "$OUTPUT_DIR"
1922 fi
1923 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1924 chmod 644 "$OUTPUT_DIR/$FILE"
1925 done
1926}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001927
1928function extract_img_data() {
1929 local image_file="$1"
1930 local out_dir="$2"
1931 local logFile="$TMPDIR/debugfs.log"
1932
1933 if [ ! -d "$out_dir" ]; then
1934 mkdir -p "$out_dir"
1935 fi
1936
1937 if [[ "$HOST_OS" == "Darwin" ]]; then
1938 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1939 echo "[-] Failed to extract data from '$image_file'"
1940 abort 1
1941 }
1942 else
1943 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1944 do
1945 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1946 echo "[-] Failed to extract data from '$image_file'"
1947 abort 1
1948 }
1949 done
1950 fi
1951
1952 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1953 if grep -Fq "$symlink_err" "$logFile"; then
1954 echo "[-] Symlinks have not been properly processed from $image_file"
1955 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1956 abort 1
1957 fi
1958}
1959
1960declare -ra VENDOR_SKIP_FILES=(
1961 "bin/toybox_vendor"
1962 "bin/toolbox"
1963 "bin/grep"
1964 "build.prop"
1965 "compatibility_matrix.xml"
1966 "default.prop"
1967 "etc/NOTICE.xml.gz"
1968 "etc/vintf/compatibility_matrix.xml"
1969 "etc/vintf/manifest.xml"
1970 "etc/wifi/wpa_supplicant.conf"
1971 "manifest.xml"
1972 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1973 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1974 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1975 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1976 "overlay/framework-res__auto_generated_rro.apk"
1977 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1978)
1979
1980function array_contains() {
1981 local element
1982 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1983 return 1
1984}
1985
1986function generate_prop_list_from_image() {
1987 local image_file="$1"
1988 local image_dir="$TMPDIR/image-temp"
1989 local output_list="$2"
1990 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1991 local -n skipped_vendor_files="$3"
1992
1993 extract_img_data "$image_file" "$image_dir"
1994
1995 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1996 do
1997 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1998 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1999 continue
2000 fi
2001 # Skip device defined skipped files since they will be re-generated at build time
2002 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
2003 continue
2004 fi
2005 if suffix_match_file ".apk" "$FILE" ; then
2006 echo "-vendor/$FILE" >> "$output_list_tmp"
2007 else
2008 echo "vendor/$FILE" >> "$output_list_tmp"
2009 fi
2010 done
2011
2012 # Sort merged file with all lists
2013 sort -u "$output_list_tmp" > "$output_list"
2014
2015 # Clean-up
2016 rm -f "$output_list_tmp"
2017}