blob: f6a5d9d76222afaef68f520c06555db69c53d414 [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() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300132 local SPEC="$1"
133 local SPLIT=(${SPEC//:/ })
134 local ARGS="$(target_args ${SPEC})"
135 local DST=
136 case ${#SPLIT[@]} in
137 1)
138 # The spec doesn't have a : delimiter
139 DST="${SPLIT[0]}"
140 ;;
141 *)
142 # The spec actually has a src:dst format
143 DST="${SPLIT[1]}"
144 ;;
145 esac
146 # Remove target_args suffix, if present
147 echo "${DST%;${ARGS}}"
Steve 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
398 elif [ "$CLASS" = "APPS" ]; then
399 printf 'android_app_import {\n'
400 printf '\tname: "%s",\n' "$PKGNAME"
401 printf '\towner: "%s",\n' "$VENDOR"
402 if [ "$EXTRA" = "priv-app" ]; then
403 SRC="$SRC/priv-app"
404 else
405 SRC="$SRC/app"
406 fi
407 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
408 if [ "$ARGS" = "PRESIGNED" ]; then
409 printf '\tpresigned: true,\n'
410 elif [ ! -z "$ARGS" ]; then
411 printf '\tcertificate: "%s",\n' "$ARGS"
412 else
413 printf '\tcertificate: "platform",\n'
414 fi
415 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
416 printf 'dex_import {\n'
417 printf '\tname: "%s",\n' "$PKGNAME"
418 printf '\towner: "%s",\n' "$VENDOR"
419 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
420 elif [ "$CLASS" = "ETC" ]; then
421 if [ "$EXTENSION" = "xml" ]; then
422 printf 'prebuilt_etc_xml {\n'
423 else
424 printf 'prebuilt_etc {\n'
425 fi
426 printf '\tname: "%s",\n' "$PKGNAME"
427 printf '\towner: "%s",\n' "$VENDOR"
428 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
429 elif [ "$CLASS" = "EXECUTABLES" ]; then
430 if [ "$EXTENSION" = "sh" ]; then
431 printf 'sh_binary {\n'
432 else
433 printf 'cc_prebuilt_binary {\n'
434 fi
435 printf '\tname: "%s",\n' "$PKGNAME"
436 printf '\towner: "%s",\n' "$VENDOR"
437 if [ "$ARGS" = "rootfs" ]; then
438 SRC="$SRC/rootfs"
439 if [ "$EXTRA" = "sbin" ]; then
440 SRC="$SRC/sbin"
441 printf '\tdist {\n'
442 printf '\t\tdest: "%s",\n' "root/sbin"
443 printf '\t},'
444 fi
445 else
446 SRC="$SRC/bin"
447 fi
448 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
449 unset EXTENSION
450 else
451 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
452 fi
453 if [ "$CLASS" = "APPS" ]; then
454 printf '\tdex_preopt: {\n'
455 printf '\t\tenabled: false,\n'
456 printf '\t},\n'
457 fi
458 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] || [ "$CLASS" = "ETC" ] ; then
459 if [ "$DIRNAME" != "." ]; then
460 printf '\tsub_dir: "%s",\n' "$DIRNAME"
461 fi
462 fi
463 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
464 printf '\tprefer: true,\n'
465 fi
466 if [ "$EXTRA" = "priv-app" ]; then
467 printf '\tprivileged: true,\n'
468 fi
469 if [ "$PARTITION" = "vendor" ]; then
470 printf '\tsoc_specific: true,\n'
471 elif [ "$PARTITION" = "product" ]; then
472 printf '\tproduct_specific: true,\n'
473 elif [ "$PARTITION" = "odm" ]; then
474 printf '\tdevice_specific: true,\n'
475 fi
476 printf '}\n\n'
477 done
478}
479
480#
481# write_makefile_packages:
482#
483# $1: The LOCAL_MODULE_CLASS for the given module list
484# $2: /odm, /product, or /vendor partition
485# $3: type-specific extra flags
486# $4: Name of the array holding the target list
487#
488# Internal function which writes out the BUILD_PREBUILT stanzas
489# for all modules in the list. This is called by write_product_packages
490# after the modules are categorized.
491#
492function write_makefile_packages() {
Steve Kondik5bd66602016-07-15 10:39:58 -0700493
494 local CLASS="$1"
razorlovesa0d296b2019-07-29 02:21:34 -0500495 local PARTITION="$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700496 local EXTRA="$3"
497
498 # Yes, this is a horrible hack - we create a new array using indirection
499 local ARR_NAME="$4[@]"
500 local FILELIST=("${!ARR_NAME}")
501
502 local FILE=
503 local ARGS=
504 local BASENAME=
505 local EXTENSION=
506 local PKGNAME=
507 local SRC=
508
509 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300510 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700511 ARGS=$(target_args "$P")
512
513 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100514 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700515 EXTENSION=${BASENAME##*.}
Mohd Farazcb0f4872019-10-08 16:13:50 +0530516 EXTENSION="."$EXTENSION
517 if [ "$EXTENSION" = ".jar" ]; then
518 EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
519 elif [ "$EXTENSION" = ".apk" ]; then
520 EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
521 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700522 PKGNAME=${BASENAME%.*}
523
524 # Add to final package list
525 PACKAGE_LIST+=("$PKGNAME")
526
527 SRC="proprietary"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400528 if [ "$PARTITION" = "system" ]; then
529 SRC+="/system"
530 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700531 SRC+="/vendor"
razorlovesa0d296b2019-07-29 02:21:34 -0500532 elif [ "$PARTITION" = "product" ]; then
533 SRC+="/product"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700534 elif [ "$PARTITION" = "odm" ]; then
535 SRC+="/odm"
Steve Kondik5bd66602016-07-15 10:39:58 -0700536 fi
537
538 printf 'include $(CLEAR_VARS)\n'
539 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
540 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
541 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
542 if [ "$EXTRA" = "both" ]; then
543 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
544 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
545 #if [ "$VENDOR_PKG" = "true" ]; then
546 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
547 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
548 #else
549 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
550 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
551 #fi
552 elif [ "$EXTRA" = "64" ]; then
553 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
554 else
555 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
556 fi
557 if [ "$EXTRA" != "none" ]; then
558 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
559 fi
560 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200561 if [ "$EXTRA" = "priv-app" ]; then
562 SRC="$SRC/priv-app"
563 else
564 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700565 fi
566 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
567 local CERT=platform
568 if [ ! -z "$ARGS" ]; then
569 CERT="$ARGS"
570 fi
571 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
572 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
573 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200574 local CERT=platform
575 if [ ! -z "$ARGS" ]; then
576 CERT="$ARGS"
577 fi
578 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700579 elif [ "$CLASS" = "ETC" ]; then
580 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
581 elif [ "$CLASS" = "EXECUTABLES" ]; then
582 if [ "$ARGS" = "rootfs" ]; then
583 SRC="$SRC/rootfs"
584 if [ "$EXTRA" = "sbin" ]; then
585 SRC="$SRC/sbin"
586 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
587 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
588 fi
589 else
590 SRC="$SRC/bin"
591 fi
592 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
593 unset EXTENSION
594 else
595 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
596 fi
597 printf 'LOCAL_MODULE_TAGS := optional\n'
598 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400599 if [ "$CLASS" = "APPS" ]; then
600 printf 'LOCAL_DEX_PREOPT := false\n'
601 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700602 if [ ! -z "$EXTENSION" ]; then
Mohd Farazcb0f4872019-10-08 16:13:50 +0530603 printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
Steve Kondik5bd66602016-07-15 10:39:58 -0700604 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100605 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
606 if [ "$DIRNAME" != "." ]; then
607 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
608 fi
609 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700610 if [ "$EXTRA" = "priv-app" ]; then
611 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
612 fi
razorlovesa0d296b2019-07-29 02:21:34 -0500613 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800614 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesa0d296b2019-07-29 02:21:34 -0500615 elif [ "$PARTITION" = "product" ]; then
616 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700617 elif [ "$PARTITION" = "odm" ]; then
618 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700619 fi
620 printf 'include $(BUILD_PREBUILT)\n\n'
621 done
622}
623
624#
625# write_product_packages:
626#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700627# This function will create prebuilt entries in the
628# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700629# product makefile for all files in the blob list which
630# start with a single dash (-) character.
631#
632function write_product_packages() {
633 PACKAGE_LIST=()
634
635 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
636
637 if [ "$COUNT" = "0" ]; then
638 return 0
639 fi
640
641 # Figure out what's 32-bit, what's 64-bit, and what's multilib
642 # I really should not be doing this in bash due to shitty array passing :(
643 local T_LIB32=( $(prefix_match "lib/") )
644 local T_LIB64=( $(prefix_match "lib64/") )
645 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
646 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
647 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
648
649 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700650 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700651 fi
652 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700653 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700654 fi
655 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700656 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700657 fi
658
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400659 local T_S_LIB32=( $(prefix_match "system/lib/") )
660 local T_S_LIB64=( $(prefix_match "system/lib64/") )
661 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
662 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
663 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
664
665 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700666 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400667 fi
668 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700669 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400670 fi
671 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700672 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400673 fi
674
Steve Kondik5bd66602016-07-15 10:39:58 -0700675 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
676 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
677 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
678 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
679 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
680
681 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700682 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700683 fi
684 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700685 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700686 fi
687 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700688 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500689 fi
690
691 local T_P_LIB32=( $(prefix_match "product/lib/") )
692 local T_P_LIB64=( $(prefix_match "product/lib64/") )
693 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
694 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
695 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
696
697 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700698 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500699 fi
700 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700701 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500702 fi
703 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700704 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700705 fi
706
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700707 local T_O_LIB32=( $(prefix_match "odm/lib/") )
708 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
709 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
710 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
711 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
712
713 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700714 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700715 fi
716 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700717 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700718 fi
719 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700720 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700721 fi
722
Steve Kondik5bd66602016-07-15 10:39:58 -0700723 # Apps
724 local APPS=( $(prefix_match "app/") )
725 if [ "${#APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200726 write_makefile_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700727 fi
728 local PRIV_APPS=( $(prefix_match "priv-app/") )
729 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200730 write_makefile_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700731 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400732 local S_APPS=( $(prefix_match "system/app/") )
733 if [ "${#S_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200734 write_makefile_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400735 fi
736 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
737 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200738 write_makefile_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400739 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700740 local V_APPS=( $(prefix_match "vendor/app/") )
741 if [ "${#V_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200742 write_makefile_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700743 fi
744 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
745 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200746 write_makefile_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
razorlovesa0d296b2019-07-29 02:21:34 -0500747 fi
748 local P_APPS=( $(prefix_match "product/app/") )
749 if [ "${#P_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200750 write_makefile_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
razorlovesa0d296b2019-07-29 02:21:34 -0500751 fi
752 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
753 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200754 write_makefile_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700755 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700756 local O_APPS=( $(prefix_match "odm/app/") )
757 if [ "${#O_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200758 write_makefile_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700759 fi
760 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
761 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
Marko Man6c5f9d02019-10-20 17:09:08 +0200762 write_makefile_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700763 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700764
765 # Framework
766 local FRAMEWORK=( $(prefix_match "framework/") )
767 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700768 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Marko Man6c5f9d02019-10-20 17:09:08 +0200769 write_makefile_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700770 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400771 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
772 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700773 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Marko Man6c5f9d02019-10-20 17:09:08 +0200774 write_makefile_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400775 fi
Christian Oder974b5902017-10-08 23:15:52 +0200776 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200777 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700778 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
Marko Man6c5f9d02019-10-20 17:09:08 +0200779 write_makefile_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
razorlovesa0d296b2019-07-29 02:21:34 -0500780 fi
781 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
782 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700783 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Marko Man6c5f9d02019-10-20 17:09:08 +0200784 write_makefile_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
Christian Oder974b5902017-10-08 23:15:52 +0200785 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700786 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
787 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700788 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Marko Man6c5f9d02019-10-20 17:09:08 +0200789 write_makefile_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700790 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700791
792 # Etc
793 local ETC=( $(prefix_match "etc/") )
794 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700795 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700796 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400797 local S_ETC=( $(prefix_match "system/etc/") )
798 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700799 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400800 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700801 local V_ETC=( $(prefix_match "vendor/etc/") )
802 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700803 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500804 fi
805 local P_ETC=( $(prefix_match "product/etc/") )
806 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700807 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700808 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700809 local O_ETC=( $(prefix_match "odm/etc/") )
810 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700811 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700812 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700813
814 # Executables
815 local BIN=( $(prefix_match "bin/") )
816 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700817 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700818 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400819 local S_BIN=( $(prefix_match "system/bin/") )
820 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700821 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400822 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700823 local V_BIN=( $(prefix_match "vendor/bin/") )
824 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700825 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500826 fi
827 local P_BIN=( $(prefix_match "product/bin/") )
828 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700829 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700830 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700831 local O_BIN=( $(prefix_match "odm/bin/") )
832 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700833 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700834 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700835 local SBIN=( $(prefix_match "sbin/") )
836 if [ "${#SBIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700837 write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700838 fi
839
840
841 # Actually write out the final PRODUCT_PACKAGES list
842 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
843
844 if [ "$PACKAGE_COUNT" -eq "0" ]; then
845 return 0
846 fi
847
848 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
849 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
850 local LINEEND=" \\"
851 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
852 LINEEND=""
853 fi
854 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
855 done
856}
857
858#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700859# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700860#
861# $1: file which will be written to
862#
863# writes out the copyright header with the current year.
864# note that this is not an append operation, and should
865# be executed first!
866#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700867function write_blueprint_header() {
868 if [ -f $1 ]; then
869 rm $1
870 fi
871
872 YEAR=$(date +"%Y")
873
874 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
875
876 printf "/**\n" > $1
877 NUM_REGEX='^[0-9]+$'
878 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
879 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
880 else
881 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
882 fi
883
884 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
885 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
886 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
887 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
888 else
889 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
890 fi
891
892 cat << EOF >> $1
893 *
894 * Licensed under the Apache License, Version 2.0 (the "License");
895 * you may not use this file except in compliance with the License.
896 * You may obtain a copy of the License at
897 *
898 * http://www.apache.org/licenses/LICENSE-2.0
899 *
900 * Unless required by applicable law or agreed to in writing, software
901 * distributed under the License is distributed on an "AS IS" BASIS,
902 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
903 * See the License for the specific language governing permissions and
904 * limitations under the License.
905 *
906 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
907 */
908
909EOF
910}
911
912#
913# write_makefile_header:
914#
915# $1: file which will be written to
916#
917# writes out the copyright header with the current year.
918# note that this is not an append operation, and should
919# be executed first!
920#
921function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500922 if [ -f $1 ]; then
923 rm $1
924 fi
925
Steve Kondik5bd66602016-07-15 10:39:58 -0700926 YEAR=$(date +"%Y")
927
928 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
929
Jake Whatley9843b322017-01-25 21:49:16 -0500930 NUM_REGEX='^[0-9]+$'
931 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
932 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
933 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
934 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
935 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
936 fi
937 if [ $YEAR -eq 2017 ]; then
938 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
939 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
940 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
941 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
942 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
943 else
944 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
945 fi
946 else
947 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
948 fi
949
950 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700951#
952# Licensed under the Apache License, Version 2.0 (the "License");
953# you may not use this file except in compliance with the License.
954# You may obtain a copy of the License at
955#
956# http://www.apache.org/licenses/LICENSE-2.0
957#
958# Unless required by applicable law or agreed to in writing, software
959# distributed under the License is distributed on an "AS IS" BASIS,
960# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
961# See the License for the specific language governing permissions and
962# limitations under the License.
963
964# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
965
966EOF
967}
968
969#
970# write_headers:
971#
972# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500973# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700974#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700975# Calls write_makefile_header for each of the makefiles and
976# write_blueprint_header for Android.bp and creates the initial
977# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -0700978#
979function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700980 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500981
982 GUARD="$2"
983 if [ -z "$GUARD" ]; then
984 GUARD="TARGET_DEVICE"
985 fi
986
Steve Kondik5bd66602016-07-15 10:39:58 -0700987 cat << EOF >> "$ANDROIDMK"
988LOCAL_PATH := \$(call my-dir)
989
990EOF
991 if [ "$COMMON" -ne 1 ]; then
992 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500993ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700994
995EOF
996 else
997 if [ -z "$1" ]; then
998 echo "Argument with devices to be added to guard must be set!"
999 exit 1
1000 fi
1001 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001002ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001003
1004EOF
1005 fi
1006
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001007 write_makefile_header "$BOARDMK"
1008 write_makefile_header "$PRODUCTMK"
1009 write_blueprint_header "$ANDROIDBP"
1010
1011 cat << EOF >> "$ANDROIDBP"
1012soong_namespace {
1013}
1014
1015EOF
1016
1017 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1018 cat << EOF >> "$PRODUCTMK"
1019PRODUCT_SOONG_NAMESPACES += \\
1020 vendor/$VENDOR/$DEVICE
1021
1022EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001023}
1024
1025#
1026# write_footers:
1027#
1028# Closes the inital guard and any other finalization tasks. Must
1029# be called as the final step.
1030#
1031function write_footers() {
1032 cat << EOF >> "$ANDROIDMK"
1033endif
1034EOF
1035}
1036
1037# Return success if adb is up and not in recovery
1038function _adb_connected {
1039 {
Jake Whatley9843b322017-01-25 21:49:16 -05001040 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001041 then
1042 return 0
1043 fi
1044 } 2>/dev/null
1045
1046 return 1
1047};
1048
1049#
1050# parse_file_list:
1051#
1052# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001053# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001054#
1055# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1056#
1057function parse_file_list() {
1058 if [ -z "$1" ]; then
1059 echo "An input file is expected!"
1060 exit 1
1061 elif [ ! -f "$1" ]; then
1062 echo "Input file "$1" does not exist!"
1063 exit 1
1064 fi
1065
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001066 if [ -n "$2" ]; then
1067 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001068 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001069 # Match all lines starting with first line found to start* with '#'
1070 # comment and contain** $2, and ending with first line to be empty*.
1071 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1072 # **the $2 match is case-insensitive
1073 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001074 else
1075 LIST=$1
1076 fi
1077
1078
Steve Kondik5bd66602016-07-15 10:39:58 -07001079 PRODUCT_PACKAGES_LIST=()
1080 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001081 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001082 PRODUCT_COPY_FILES_LIST=()
1083 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001084 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001085
1086 while read -r line; do
1087 if [ -z "$line" ]; then continue; fi
1088
1089 # If the line has a pipe delimiter, a sha1 hash should follow.
1090 # This indicates the file should be pinned and not overwritten
1091 # when extracting files.
1092 local SPLIT=(${line//\|/ })
1093 local COUNT=${#SPLIT[@]}
1094 local SPEC=${SPLIT[0]}
1095 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001096 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001097 if [ "$COUNT" -gt "1" ]; then
1098 HASH=${SPLIT[1]}
1099 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001100 if [ "$COUNT" -gt "2" ]; then
1101 FIXUP_HASH=${SPLIT[2]}
1102 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001103
1104 # if line starts with a dash, it needs to be packaged
1105 if [[ "$SPEC" =~ ^- ]]; then
1106 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1107 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001108 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001109 else
1110 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1111 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001112 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001113 fi
1114
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001115 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001116}
1117
1118#
1119# write_makefiles:
1120#
1121# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001122# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001123#
1124# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001125# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -07001126# the product makefile.
1127#
1128function write_makefiles() {
1129 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001130 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001131 write_product_packages
1132}
1133
1134#
1135# append_firmware_calls_to_makefiles:
1136#
1137# Appends to Android.mk the calls to all images present in radio folder
1138# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1139#
1140function append_firmware_calls_to_makefiles() {
1141 cat << EOF >> "$ANDROIDMK"
1142ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1143
1144RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1145\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1146 \$(call add-radio-file,radio/\$(f)))
1147\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1148
1149endif
1150
1151EOF
1152}
1153
1154#
1155# get_file:
1156#
1157# $1: input file
1158# $2: target file/folder
1159# $3: source of the file (can be "adb" or a local folder)
1160#
1161# Silently extracts the input file to defined target
1162# Returns success if file can be pulled from the device or found locally
1163#
1164function get_file() {
1165 local SRC="$3"
1166
1167 if [ "$SRC" = "adb" ]; then
1168 # try to pull
1169 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1170
1171 return 1
1172 else
1173 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001174 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1175 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001176 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001177
1178 return 1
1179 fi
1180};
1181
1182#
1183# oat2dex:
1184#
1185# $1: extracted apk|jar (to check if deodex is required)
1186# $2: odexed apk|jar to deodex
1187# $3: source of the odexed apk|jar
1188#
1189# Convert apk|jar .odex in the corresposing classes.dex
1190#
1191function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001192 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001193 local OEM_TARGET="$2"
1194 local SRC="$3"
1195 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001196 local OAT=
1197 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -07001198
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001199 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1200 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1201 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001202 fi
1203
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001204 if [ -z "$VDEXEXTRACTOR" ]; then
theimpulson9a911af2019-08-14 03:25:12 +00001205 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001206 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001207
codeworkx85eda752018-09-23 12:36:57 +02001208 if [ -z "$CDEXCONVERTER" ]; then
theimpulson9a911af2019-08-14 03:25:12 +00001209 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001210 fi
1211
Steve Kondik5bd66602016-07-15 10:39:58 -07001212 # Extract existing boot.oats to the temp folder
1213 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001214 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001215 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001216 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001217 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001218 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001219 else
1220 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001221 fi
1222 done
1223 fi
1224
1225 if [ -z "$ARCHES" ]; then
1226 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1227 fi
1228
theimpulson9a911af2019-08-14 03:25:12 +00001229 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001230 return;
1231 fi
1232
theimpulson9a911af2019-08-14 03:25:12 +00001233 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001234 return 0 # target apk|jar is already odexed, return
1235 fi
1236
1237 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001238 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001239
Joe Maplesfb3941c2018-01-05 14:51:33 -05001240 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1241 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001242
Joe Maplesfb3941c2018-01-05 14:51:33 -05001243 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1244 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1245 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001246 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1247 for CLASS in $CLASSES; do
1248 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1249 # Check if we have to deal with CompactDex
1250 if [[ "$CLASS" == *.cdex ]]; then
1251 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1252 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1253 else
1254 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1255 fi
1256 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001257 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001258 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1259 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001260 fi
theimpulson9a911af2019-08-14 03:25:12 +00001261 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001262 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001263 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001264 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001265 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001266 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001267 # try to extract classes.dex from boot.vdex for frameworks jars
1268 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001269 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001270 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001271 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1272 for CLASS in $CLASSES; do
1273 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1274 # Check if we have to deal with CompactDex
1275 if [[ "$CLASS" == *.cdex ]]; then
1276 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1277 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1278 else
1279 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1280 fi
1281 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001282 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001283 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1284 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001285 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001286 else
1287 continue
1288 fi
1289
Steve Kondik5bd66602016-07-15 10:39:58 -07001290 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001291
1292 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001293}
1294
1295#
1296# init_adb_connection:
1297#
1298# Starts adb server and waits for the device
1299#
1300function init_adb_connection() {
1301 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1302 if ! _adb_connected; then
1303 echo "No device is online. Waiting for one..."
1304 echo "Please connect USB and/or enable USB debugging"
1305 until _adb_connected; do
1306 sleep 1
1307 done
1308 echo "Device Found."
1309 fi
1310
1311 # Retrieve IP and PORT info if we're using a TCP connection
1312 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1313 | head -1 | awk '{print $1}')
1314 adb root &> /dev/null
1315 sleep 0.3
1316 if [ -n "$TCPIPPORT" ]; then
1317 # adb root just killed our connection
1318 # so reconnect...
1319 adb connect "$TCPIPPORT"
1320 fi
1321 adb wait-for-device &> /dev/null
1322 sleep 0.3
1323}
1324
1325#
1326# fix_xml:
1327#
1328# $1: xml file to fix
1329#
1330function fix_xml() {
1331 local XML="$1"
1332 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1333
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001334 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1335 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001336
1337 mv "$TEMP_XML" "$XML"
1338}
1339
Vladimir Olteande985fe2019-01-17 03:07:34 +02001340function get_hash() {
1341 local FILE="$1"
1342
1343 if [ "$(uname)" == "Darwin" ]; then
1344 shasum "${FILE}" | awk '{print $1}'
1345 else
1346 sha1sum "${FILE}" | awk '{print $1}'
1347 fi
1348}
1349
Vladimir Olteana7d20492019-01-17 03:05:52 +02001350function print_spec() {
1351 local SPEC_PRODUCT_PACKAGE="$1"
1352 local SPEC_SRC_FILE="$2"
1353 local SPEC_DST_FILE="$3"
1354 local SPEC_ARGS="$4"
1355 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001356 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001357
1358 local PRODUCT_PACKAGE=""
1359 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1360 PRODUCT_PACKAGE="-"
1361 fi
1362 local SRC=""
1363 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1364 SRC="${SPEC_SRC_FILE}:"
1365 fi
1366 local DST=""
1367 if [ ! -z "${SPEC_DST_FILE}" ]; then
1368 DST="${SPEC_DST_FILE}"
1369 fi
1370 local ARGS=""
1371 if [ ! -z "${SPEC_ARGS}" ]; then
1372 ARGS=";${SPEC_ARGS}"
1373 fi
1374 local HASH=""
1375 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1376 HASH="|${SPEC_HASH}"
1377 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001378 local FIXUP_HASH=""
1379 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1380 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1381 fi
1382 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1383}
1384
1385# To be overridden by device-level extract-files.sh
1386# Parameters:
1387# $1: spec name of a blob. Can be used for filtering.
1388# If the spec is "src:dest", then $1 is "dest".
1389# If the spec is "src", then $1 is "src".
1390# $2: path to blob file. Can be used for fixups.
1391#
1392function blob_fixup() {
1393 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001394}
1395
Steve Kondik5bd66602016-07-15 10:39:58 -07001396#
1397# extract:
1398#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001399# Positional parameters:
1400# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001401# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001402# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1403#
1404# Non-positional parameters (coming after $2):
1405# --section: preferred way of selecting the portion to parse and extract from
1406# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001407# --kang: if present, this option will activate the printing of hashes for the
1408# extracted blobs. Useful with --section for subsequent pinning of
1409# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001410#
1411function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001412 # Consume positional parameters
1413 local PROPRIETARY_FILES_TXT="$1"; shift
1414 local SRC="$1"; shift
1415 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001416 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001417
1418 # Consume optional, non-positional parameters
1419 while [ "$#" -gt 0 ]; do
1420 case "$1" in
1421 -s|--section)
1422 SECTION="$2"; shift
1423 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001424 -k|--kang)
1425 KANG=true
1426 DISABLE_PINNING=1
1427 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001428 *)
1429 # Backwards-compatibility with the old behavior, where $3, if
1430 # present, denoted an optional positional ${SECTION} argument.
1431 # Users of ${SECTION} are encouraged to migrate from setting it as
1432 # positional $3, to non-positional --section ${SECTION}, the
1433 # reason being that it doesn't scale to have more than 1 optional
1434 # positional argument.
1435 SECTION="$1"
1436 ;;
1437 esac
1438 shift
1439 done
1440
Steve Kondik5bd66602016-07-15 10:39:58 -07001441 if [ -z "$OUTDIR" ]; then
1442 echo "Output dir not set!"
1443 exit 1
1444 fi
1445
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001446 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001447
1448 # Allow failing, so we can try $DEST and/or $FILE
1449 set +e
1450
1451 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1452 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001453 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001454 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001455 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001456 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001457 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1458
1459 if [ "$SRC" = "adb" ]; then
1460 init_adb_connection
1461 fi
1462
Dan Pasanen0cc05012017-03-21 09:06:11 -05001463 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001464 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001465
1466 # Check if we're working with the same zip that was passed last time.
1467 # If so, let's just use what's already extracted.
1468 MD5=`md5sum "$SRC"| awk '{print $1}'`
1469 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1470
1471 if [ "$MD5" != "$OLDMD5" ]; then
1472 rm -rf "$DUMPDIR"
1473 mkdir "$DUMPDIR"
1474 unzip "$SRC" -d "$DUMPDIR"
1475 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1476
1477 # Stop if an A/B OTA zip is detected. We cannot extract these.
1478 if [ -a "$DUMPDIR"/payload.bin ]; then
1479 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1480 exit 1
1481 # If OTA is block based, extract it.
1482 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1483 echo "Converting system.new.dat to system.img"
theimpulson9a911af2019-08-14 03:25:12 +00001484 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001485 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1486 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1487 echo "Requesting sudo access to mount the system.img"
1488 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1489 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1490 sudo umount "$DUMPDIR"/tmp
1491 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1492 fi
1493 fi
1494
1495 SRC="$DUMPDIR"
1496 fi
1497
Steve Kondik5bd66602016-07-15 10:39:58 -07001498 if [ "$VENDOR_STATE" -eq "0" ]; then
1499 echo "Cleaning output directory ($OUTPUT_ROOT).."
1500 rm -rf "${OUTPUT_TMP:?}"
1501 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001502 if [ -d "$OUTPUT_ROOT" ]; then
1503 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1504 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001505 VENDOR_STATE=1
1506 fi
1507
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001508 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001509
1510 for (( i=1; i<COUNT+1; i++ )); do
1511
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001512 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001513 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001514 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001515 local OUTPUT_DIR=
1516 local TMP_DIR=
1517 local SRC_FILE=
1518 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001519 local IS_PRODUCT_PACKAGE=false
1520
1521 # Note: this relies on the fact that the ${FILELIST[@]} array
1522 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1523 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1524 IS_PRODUCT_PACKAGE=true
1525 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001526
Vladimir Olteand6391332018-06-24 20:42:01 +03001527 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001528 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1529 TMP_DIR="${OUTPUT_TMP}/rootfs"
1530 SRC_FILE="/${SPEC_SRC_FILE}"
1531 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001532 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001533 OUTPUT_DIR="${OUTPUT_ROOT}"
1534 TMP_DIR="${OUTPUT_TMP}"
1535 SRC_FILE="/system/${SPEC_SRC_FILE}"
1536 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001537 fi
1538
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001539 # Strip the file path in the vendor repo of "system", if present
1540 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001541 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001542 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001543
Gabriele M58270a32017-11-13 23:15:29 +01001544 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001545 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001546 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001547 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001548 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001549 if [ -f "${VENDOR_REPO_FILE}" ]; then
1550 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001551 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001552 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001553 fi
1554 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001555 local TMP_HASH=$(get_hash "${PINNED}")
1556 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001557 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001558 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1559 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001560 fi
1561 fi
1562 fi
1563 fi
1564
Vladimir Olteana7d20492019-01-17 03:05:52 +02001565 if [ "${KANG}" = false ]; then
1566 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1567 fi
1568
Gabriele M58270a32017-11-13 23:15:29 +01001569 if [ "$KEEP" = "1" ]; then
Vladimir Olteana7d20492019-01-17 03:05:52 +02001570 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001571 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001572 FOUND=false
1573 # Try Lineage target first.
1574 # Also try to search for files stripped of
1575 # the "/system" prefix, if we're actually extracting
1576 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001577 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001578 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1579 FOUND=true
1580 break
1581 }
1582 done
1583
1584 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001585 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001586 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001587 fi
1588 fi
1589
Vladimir Olteande985fe2019-01-17 03:07:34 +02001590 # Blob fixup pipeline has 2 parts: one that is fixed and
1591 # one that is user-configurable
1592 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1593 # Deodex apk|jar if that's the case
1594 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1595 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1596 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001597 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1598 rm "$TMPDIR/classes"*
Vladimir Olteande985fe2019-01-17 03:07:34 +02001599 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001600 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001601 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1602 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001603 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001604 # Now run user-supplied fixup function
1605 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1606 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001607
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001608 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001609 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001610 local TYPE="${DIR##*/}"
1611 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001612 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001613 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001614 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001615 fi
1616 fi
1617
Vladimir Olteana7d20492019-01-17 03:05:52 +02001618 if [ "${KANG}" = true ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001619 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1620 fi
1621
1622 # Check and print whether the fixup pipeline actually did anything.
1623 # This isn't done right after the fixup pipeline because we want this print
1624 # to come after print_spec above, when in kang mode.
1625 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1626 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1627 # Now sanity-check the spec for this blob.
1628 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1629 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1630 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1631 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001632 fi
1633
Steve Kondik5bd66602016-07-15 10:39:58 -07001634 done
1635
1636 # Don't allow failing
1637 set -e
1638}
1639
1640#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001641# extract2:
1642#
1643# Positional parameters:
1644# $1: file containing the list of items to extract (aka proprietary-files.txt)
1645#
1646# Non-positional parameters (coming after $2):
1647# --section: selects the portion to parse and extracts from proprietary-files.txt
1648# --kang: if present, this option will activate the printing of hashes for the
1649# extracted blobs. Useful with --section for subsequent pinning of
1650# blobs taken from other origins.
1651#
1652function extract2() {
1653 # Consume positional parameters
1654 local PROPRIETARY_FILES_TXT="$1"; shift
1655 local SECTION=""
1656 local KANG=false
1657
1658 # Consume optional, non-positional parameters
1659 while [ "$#" -gt 0 ]; do
1660 case "$1" in
1661 --adb)
1662 ADB=true
1663 ;;
1664 --system)
1665 SYSTEM_SRC="$2"; shift
1666 ;;
1667 --vendor)
1668 VENDOR_SRC="$2"; shift
1669 ;;
1670 --odm)
1671 ODM_SRC="$2"; shift
1672 ;;
1673 --product)
1674 PRODUCT_SRC="$2"; shift
1675 ;;
1676 -s|--section)
1677 SECTION="$2"; shift
1678 ;;
1679 -k|--kang)
1680 KANG=true
1681 DISABLE_PINNING=1
1682 ;;
1683 esac
1684 shift
1685 done
1686
1687 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1688 echo "No sources set! You must select --adb or pass paths to partition dumps."
1689 exit 1
1690 fi
1691
1692 if [ -z "$OUTDIR" ]; then
1693 echo "Output dir not set!"
1694 exit 1
1695 fi
1696
1697 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1698
1699 # Allow failing, so we can try $DEST and/or $FILE
1700 set +e
1701
1702 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1703 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1704 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1705 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1706 local COUNT=${#FILELIST[@]}
1707 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1708 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1709
1710 if [ "$ADB" = true ]; then
1711 init_adb_connection
1712 fi
1713
1714 if [ "$VENDOR_STATE" -eq "0" ]; then
1715 echo "Cleaning output directory ($OUTPUT_ROOT).."
1716 rm -rf "${OUTPUT_TMP:?}"
1717 mkdir -p "${OUTPUT_TMP:?}"
1718 if [ -d "$OUTPUT_ROOT" ]; then
1719 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1720 fi
1721 VENDOR_STATE=1
1722 fi
1723
1724 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1725
1726 for (( i=1; i<COUNT+1; i++ )); do
1727
1728 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1729 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1730 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1731 local OUTPUT_DIR=
1732 local TMP_DIR=
1733 local SRC_FILE=
1734 local DST_FILE=
1735 local IS_PRODUCT_PACKAGE=false
1736
1737 # Note: this relies on the fact that the ${FILELIST[@]} array
1738 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1739 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1740 IS_PRODUCT_PACKAGE=true
1741 fi
1742
1743 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1744 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1745 TMP_DIR="${OUTPUT_TMP}/rootfs"
1746 else
1747 OUTPUT_DIR="${OUTPUT_ROOT}"
1748 TMP_DIR="${OUTPUT_TMP}"
1749 fi
1750 SRC_FILE="${SPEC_SRC_FILE}"
1751 DST_FILE="${SPEC_DST_FILE}"
1752
1753 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1754 local BLOB_DISPLAY_NAME="${DST_FILE}"
1755 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1756
1757 # Check pinned files
1758 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1759 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1760 local KEEP=""
1761 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1762 if [ -f "${VENDOR_REPO_FILE}" ]; then
1763 local PINNED="${VENDOR_REPO_FILE}"
1764 else
1765 local PINNED="${TMP_DIR}${DST_FILE}"
1766 fi
1767 if [ -f "$PINNED" ]; then
1768 local TMP_HASH=$(get_hash "${PINNED}")
1769 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1770 KEEP="1"
1771 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1772 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1773 fi
1774 fi
1775 fi
1776 fi
1777
1778 if [ "${KANG}" = false ]; then
1779 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1780 fi
1781
1782 if [ "$KEEP" = "1" ]; then
1783 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1784 else
1785 FOUND=false
1786 PARTITION_SOURCE_DIR=
1787 # Try Lineage target first.
1788 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1789 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1790 if [ "$PARTITION" = "system" ]; then
1791 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1792 elif [ "$PARTITION" = "vendor" ]; then
1793 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1794 elif [ "$PARTITION" = "product" ]; then
1795 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1796 elif [ "$PARTITION" = "odm" ]; then
1797 PARTITION_SOURCE_DIR="$ODM_SRC"
1798 fi
1799 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1800 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1801 FOUND=true
1802 break
1803 }
1804 # Search with the full system/ prefix if the file was not found on the system partition
1805 # because we may be searching in a mounted system-as-root system.img
1806 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1807 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1808 FOUND=true
1809 break
1810 }
1811 fi
1812 done
1813
1814 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1815 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1816 fi
1817
1818 if [ "${FOUND}" = false ]; then
1819 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1820 continue
1821 fi
1822 fi
1823
1824 # Blob fixup pipeline has 2 parts: one that is fixed and
1825 # one that is user-configurable
1826 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1827 # Deodex apk|jar if that's the case
1828 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1829 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1830 if [ -f "$TMPDIR/classes.dex" ]; then
1831 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1832 rm "$TMPDIR/classes"*
1833 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1834 fi
1835 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1836 fix_xml "${VENDOR_REPO_FILE}"
1837 fi
1838 # Now run user-supplied fixup function
1839 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1840 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1841
1842 if [ -f "${VENDOR_REPO_FILE}" ]; then
1843 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1844 local TYPE="${DIR##*/}"
1845 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1846 chmod 755 "${VENDOR_REPO_FILE}"
1847 else
1848 chmod 644 "${VENDOR_REPO_FILE}"
1849 fi
1850 fi
1851
1852 if [ "${KANG}" = true ]; then
1853 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1854 fi
1855
1856 # Check and print whether the fixup pipeline actually did anything.
1857 # This isn't done right after the fixup pipeline because we want this print
1858 # to come after print_spec above, when in kang mode.
1859 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1860 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1861 # Now sanity-check the spec for this blob.
1862 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1863 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1864 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1865 fi
1866 fi
1867
1868 done
1869
1870 # Don't allow failing
1871 set -e
1872}
1873
1874#
Steve Kondik5bd66602016-07-15 10:39:58 -07001875# extract_firmware:
1876#
1877# $1: file containing the list of items to extract
1878# $2: path to extracted radio folder
1879#
1880function extract_firmware() {
1881 if [ -z "$OUTDIR" ]; then
1882 echo "Output dir not set!"
1883 exit 1
1884 fi
1885
1886 parse_file_list "$1"
1887
1888 # Don't allow failing
1889 set -e
1890
1891 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1892 local COUNT=${#FILELIST[@]}
1893 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001894 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001895
1896 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1897 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1898 rm -rf "${OUTPUT_DIR:?}/"*
1899 VENDOR_RADIO_STATE=1
1900 fi
1901
1902 echo "Extracting $COUNT files in $1 from $SRC:"
1903
1904 for (( i=1; i<COUNT+1; i++ )); do
1905 local FILE="${FILELIST[$i-1]}"
1906 printf ' - %s \n' "/radio/$FILE"
1907
1908 if [ ! -d "$OUTPUT_DIR" ]; then
1909 mkdir -p "$OUTPUT_DIR"
1910 fi
1911 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1912 chmod 644 "$OUTPUT_DIR/$FILE"
1913 done
1914}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001915
1916function extract_img_data() {
1917 local image_file="$1"
1918 local out_dir="$2"
1919 local logFile="$TMPDIR/debugfs.log"
1920
1921 if [ ! -d "$out_dir" ]; then
1922 mkdir -p "$out_dir"
1923 fi
1924
1925 if [[ "$HOST_OS" == "Darwin" ]]; then
1926 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1927 echo "[-] Failed to extract data from '$image_file'"
1928 abort 1
1929 }
1930 else
1931 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1932 do
1933 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1934 echo "[-] Failed to extract data from '$image_file'"
1935 abort 1
1936 }
1937 done
1938 fi
1939
1940 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1941 if grep -Fq "$symlink_err" "$logFile"; then
1942 echo "[-] Symlinks have not been properly processed from $image_file"
1943 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1944 abort 1
1945 fi
1946}
1947
1948declare -ra VENDOR_SKIP_FILES=(
1949 "bin/toybox_vendor"
1950 "bin/toolbox"
1951 "bin/grep"
1952 "build.prop"
1953 "compatibility_matrix.xml"
1954 "default.prop"
1955 "etc/NOTICE.xml.gz"
1956 "etc/vintf/compatibility_matrix.xml"
1957 "etc/vintf/manifest.xml"
1958 "etc/wifi/wpa_supplicant.conf"
1959 "manifest.xml"
1960 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1961 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1962 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1963 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1964 "overlay/framework-res__auto_generated_rro.apk"
1965 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1966)
1967
1968function array_contains() {
1969 local element
1970 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1971 return 1
1972}
1973
1974function generate_prop_list_from_image() {
1975 local image_file="$1"
1976 local image_dir="$TMPDIR/image-temp"
1977 local output_list="$2"
1978 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1979 local -n skipped_vendor_files="$3"
1980
1981 extract_img_data "$image_file" "$image_dir"
1982
1983 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1984 do
1985 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1986 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1987 continue
1988 fi
1989 # Skip device defined skipped files since they will be re-generated at build time
1990 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1991 continue
1992 fi
1993 if suffix_match_file ".apk" "$FILE" ; then
1994 echo "-vendor/$FILE" >> "$output_list_tmp"
1995 else
1996 echo "vendor/$FILE" >> "$output_list_tmp"
1997 fi
1998 done
1999
2000 # Sort merged file with all lists
2001 sort -u "$output_list_tmp" > "$output_list"
2002
2003 # Clean-up
2004 rm -f "$output_list_tmp"
2005}