blob: 87cb1248c953e5b7c3b60f1e94c3c9be01540df8 [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#
319# write_packages:
320#
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#
330function write_packages() {
331
332 local CLASS="$1"
razorlovesa0d296b2019-07-29 02:21:34 -0500333 local PARTITION="$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700334 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
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300348 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700349 ARGS=$(target_args "$P")
350
351 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100352 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700353 EXTENSION=${BASENAME##*.}
354 PKGNAME=${BASENAME%.*}
355
356 # Add to final package list
357 PACKAGE_LIST+=("$PKGNAME")
358
359 SRC="proprietary"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400360 if [ "$PARTITION" = "system" ]; then
361 SRC+="/system"
362 elif [ "$PARTITION" = "vendor" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700363 SRC+="/vendor"
razorlovesa0d296b2019-07-29 02:21:34 -0500364 elif [ "$PARTITION" = "product" ]; then
365 SRC+="/product"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700366 elif [ "$PARTITION" = "odm" ]; then
367 SRC+="/odm"
Steve Kondik5bd66602016-07-15 10:39:58 -0700368 fi
369
370 printf 'include $(CLEAR_VARS)\n'
371 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
372 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
373 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
374 if [ "$EXTRA" = "both" ]; then
375 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
376 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
377 #if [ "$VENDOR_PKG" = "true" ]; then
378 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
379 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
380 #else
381 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
382 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
383 #fi
384 elif [ "$EXTRA" = "64" ]; then
385 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
386 else
387 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
388 fi
389 if [ "$EXTRA" != "none" ]; then
390 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
391 fi
392 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200393 if [ "$EXTRA" = "priv-app" ]; then
394 SRC="$SRC/priv-app"
395 else
396 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700397 fi
398 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
399 local CERT=platform
400 if [ ! -z "$ARGS" ]; then
401 CERT="$ARGS"
402 fi
403 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
404 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
405 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200406 local CERT=platform
407 if [ ! -z "$ARGS" ]; then
408 CERT="$ARGS"
409 fi
410 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700411 elif [ "$CLASS" = "ETC" ]; then
412 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
413 elif [ "$CLASS" = "EXECUTABLES" ]; then
414 if [ "$ARGS" = "rootfs" ]; then
415 SRC="$SRC/rootfs"
416 if [ "$EXTRA" = "sbin" ]; then
417 SRC="$SRC/sbin"
418 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
419 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
420 fi
421 else
422 SRC="$SRC/bin"
423 fi
424 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
425 unset EXTENSION
426 else
427 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
428 fi
429 printf 'LOCAL_MODULE_TAGS := optional\n'
430 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400431 if [ "$CLASS" = "APPS" ]; then
432 printf 'LOCAL_DEX_PREOPT := false\n'
433 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700434 if [ ! -z "$EXTENSION" ]; then
435 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
436 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100437 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
438 if [ "$DIRNAME" != "." ]; then
439 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
440 fi
441 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700442 if [ "$EXTRA" = "priv-app" ]; then
443 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
444 fi
razorlovesa0d296b2019-07-29 02:21:34 -0500445 if [ "$PARTITION" = "vendor" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800446 printf 'LOCAL_VENDOR_MODULE := true\n'
razorlovesa0d296b2019-07-29 02:21:34 -0500447 elif [ "$PARTITION" = "product" ]; then
448 printf 'LOCAL_PRODUCT_MODULE := true\n'
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700449 elif [ "$PARTITION" = "odm" ]; then
450 printf 'LOCAL_ODM_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700451 fi
452 printf 'include $(BUILD_PREBUILT)\n\n'
453 done
454}
455
456#
457# write_product_packages:
458#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700459# This function will create prebuilt entries in the
460# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700461# product makefile for all files in the blob list which
462# start with a single dash (-) character.
463#
464function write_product_packages() {
465 PACKAGE_LIST=()
466
467 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
468
469 if [ "$COUNT" = "0" ]; then
470 return 0
471 fi
472
473 # Figure out what's 32-bit, what's 64-bit, and what's multilib
474 # I really should not be doing this in bash due to shitty array passing :(
475 local T_LIB32=( $(prefix_match "lib/") )
476 local T_LIB64=( $(prefix_match "lib64/") )
477 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
478 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
479 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
480
481 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500482 write_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700483 fi
484 if [ "${#LIB32[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500485 write_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700486 fi
487 if [ "${#LIB64[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500488 write_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700489 fi
490
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400491 local T_S_LIB32=( $(prefix_match "system/lib/") )
492 local T_S_LIB64=( $(prefix_match "system/lib64/") )
493 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
494 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
495 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
496
497 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
498 write_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDMK"
499 fi
500 if [ "${#S_LIB32[@]}" -gt "0" ]; then
501 write_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDMK"
502 fi
503 if [ "${#S_LIB64[@]}" -gt "0" ]; then
504 write_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDMK"
505 fi
506
Steve Kondik5bd66602016-07-15 10:39:58 -0700507 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
508 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
509 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
510 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
511 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
512
513 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500514 write_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700515 fi
516 if [ "${#V_LIB32[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500517 write_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700518 fi
519 if [ "${#V_LIB64[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500520 write_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDMK"
521 fi
522
523 local T_P_LIB32=( $(prefix_match "product/lib/") )
524 local T_P_LIB64=( $(prefix_match "product/lib64/") )
525 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
526 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
527 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
528
529 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
530 write_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDMK"
531 fi
532 if [ "${#P_LIB32[@]}" -gt "0" ]; then
533 write_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDMK"
534 fi
535 if [ "${#P_LIB64[@]}" -gt "0" ]; then
536 write_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700537 fi
538
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700539 local T_O_LIB32=( $(prefix_match "odm/lib/") )
540 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
541 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
542 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
543 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
544
545 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
546 write_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDMK"
547 fi
548 if [ "${#O_LIB32[@]}" -gt "0" ]; then
549 write_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDMK"
550 fi
551 if [ "${#O_LIB64[@]}" -gt "0" ]; then
552 write_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDMK"
553 fi
554
Steve Kondik5bd66602016-07-15 10:39:58 -0700555 # Apps
556 local APPS=( $(prefix_match "app/") )
557 if [ "${#APPS[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500558 write_packages "APPS" "" "" "APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700559 fi
560 local PRIV_APPS=( $(prefix_match "priv-app/") )
561 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500562 write_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700563 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400564 local S_APPS=( $(prefix_match "system/app/") )
565 if [ "${#S_APPS[@]}" -gt "0" ]; then
566 write_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDMK"
567 fi
568 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
569 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
570 write_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDMK"
571 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700572 local V_APPS=( $(prefix_match "vendor/app/") )
573 if [ "${#V_APPS[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500574 write_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700575 fi
576 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
577 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500578 write_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
579 fi
580 local P_APPS=( $(prefix_match "product/app/") )
581 if [ "${#P_APPS[@]}" -gt "0" ]; then
582 write_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDMK"
583 fi
584 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
585 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
586 write_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700587 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700588 local O_APPS=( $(prefix_match "odm/app/") )
589 if [ "${#O_APPS[@]}" -gt "0" ]; then
590 write_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDMK"
591 fi
592 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
593 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
594 write_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDMK"
595 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700596
597 # Framework
598 local FRAMEWORK=( $(prefix_match "framework/") )
599 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500600 write_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700601 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400602 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
603 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
604 write_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDMK"
605 fi
Christian Oder974b5902017-10-08 23:15:52 +0200606 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200607 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500608 write_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDMK"
609 fi
610 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
611 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
612 write_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDMK"
Christian Oder974b5902017-10-08 23:15:52 +0200613 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700614 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
615 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
616 write_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDMK"
617 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700618
619 # Etc
620 local ETC=( $(prefix_match "etc/") )
621 if [ "${#ETC[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500622 write_packages "ETC" "" "" "ETC" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700623 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400624 local S_ETC=( $(prefix_match "system/etc/") )
625 if [ "${#ETC[@]}" -gt "0" ]; then
626 write_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDMK"
627 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700628 local V_ETC=( $(prefix_match "vendor/etc/") )
629 if [ "${#V_ETC[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500630 write_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDMK"
631 fi
632 local P_ETC=( $(prefix_match "product/etc/") )
633 if [ "${#P_ETC[@]}" -gt "0" ]; then
634 write_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700635 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700636 local O_ETC=( $(prefix_match "odm/etc/") )
637 if [ "${#O_ETC[@]}" -gt "0" ]; then
638 write_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDMK"
639 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700640
641 # Executables
642 local BIN=( $(prefix_match "bin/") )
643 if [ "${#BIN[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500644 write_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700645 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400646 local S_BIN=( $(prefix_match "system/bin/") )
647 if [ "${#BIN[@]}" -gt "0" ]; then
648 write_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDMK"
649 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700650 local V_BIN=( $(prefix_match "vendor/bin/") )
651 if [ "${#V_BIN[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500652 write_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDMK"
653 fi
654 local P_BIN=( $(prefix_match "product/bin/") )
655 if [ "${#P_BIN[@]}" -gt "0" ]; then
656 write_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700657 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700658 local O_BIN=( $(prefix_match "odm/bin/") )
659 if [ "${#O_BIN[@]}" -gt "0" ]; then
660 write_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDMK"
661 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700662 local SBIN=( $(prefix_match "sbin/") )
663 if [ "${#SBIN[@]}" -gt "0" ]; then
razorlovesa0d296b2019-07-29 02:21:34 -0500664 write_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700665 fi
666
667
668 # Actually write out the final PRODUCT_PACKAGES list
669 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
670
671 if [ "$PACKAGE_COUNT" -eq "0" ]; then
672 return 0
673 fi
674
675 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
676 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
677 local LINEEND=" \\"
678 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
679 LINEEND=""
680 fi
681 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
682 done
683}
684
685#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700686# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700687#
688# $1: file which will be written to
689#
690# writes out the copyright header with the current year.
691# note that this is not an append operation, and should
692# be executed first!
693#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700694function write_blueprint_header() {
695 if [ -f $1 ]; then
696 rm $1
697 fi
698
699 YEAR=$(date +"%Y")
700
701 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
702
703 printf "/**\n" > $1
704 NUM_REGEX='^[0-9]+$'
705 if [[ ! $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] || [ $INITIAL_COPYRIGHT_YEAR -lt 2019 ]; then
706 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=2019
707 else
708 BLUEPRINT_INITIAL_COPYRIGHT_YEAR=$INITIAL_COPYRIGHT_YEAR
709 fi
710
711 if [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
712 printf " * Copyright (C) $YEAR The LineageOS Project\n" >> $1
713 elif [ $BLUEPRINT_INITIAL_COPYRIGHT_YEAR -le 2019 ]; then
714 printf " * Copyright (C) 2019-$YEAR The LineageOS Project\n" >> $1
715 else
716 printf " * Copyright (C) $BLUEPRINT_INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
717 fi
718
719 cat << EOF >> $1
720 *
721 * Licensed under the Apache License, Version 2.0 (the "License");
722 * you may not use this file except in compliance with the License.
723 * You may obtain a copy of the License at
724 *
725 * http://www.apache.org/licenses/LICENSE-2.0
726 *
727 * Unless required by applicable law or agreed to in writing, software
728 * distributed under the License is distributed on an "AS IS" BASIS,
729 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
730 * See the License for the specific language governing permissions and
731 * limitations under the License.
732 *
733 * This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
734 */
735
736EOF
737}
738
739#
740# write_makefile_header:
741#
742# $1: file which will be written to
743#
744# writes out the copyright header with the current year.
745# note that this is not an append operation, and should
746# be executed first!
747#
748function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500749 if [ -f $1 ]; then
750 rm $1
751 fi
752
Steve Kondik5bd66602016-07-15 10:39:58 -0700753 YEAR=$(date +"%Y")
754
755 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
756
Jake Whatley9843b322017-01-25 21:49:16 -0500757 NUM_REGEX='^[0-9]+$'
758 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
759 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
760 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
761 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
762 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
763 fi
764 if [ $YEAR -eq 2017 ]; then
765 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
766 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
767 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
768 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
769 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
770 else
771 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
772 fi
773 else
774 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
775 fi
776
777 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700778#
779# Licensed under the Apache License, Version 2.0 (the "License");
780# you may not use this file except in compliance with the License.
781# You may obtain a copy of the License at
782#
783# http://www.apache.org/licenses/LICENSE-2.0
784#
785# Unless required by applicable law or agreed to in writing, software
786# distributed under the License is distributed on an "AS IS" BASIS,
787# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
788# See the License for the specific language governing permissions and
789# limitations under the License.
790
791# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
792
793EOF
794}
795
796#
797# write_headers:
798#
799# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500800# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700801#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700802# Calls write_makefile_header for each of the makefiles and
803# write_blueprint_header for Android.bp and creates the initial
804# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -0700805#
806function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700807 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500808
809 GUARD="$2"
810 if [ -z "$GUARD" ]; then
811 GUARD="TARGET_DEVICE"
812 fi
813
Steve Kondik5bd66602016-07-15 10:39:58 -0700814 cat << EOF >> "$ANDROIDMK"
815LOCAL_PATH := \$(call my-dir)
816
817EOF
818 if [ "$COMMON" -ne 1 ]; then
819 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500820ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700821
822EOF
823 else
824 if [ -z "$1" ]; then
825 echo "Argument with devices to be added to guard must be set!"
826 exit 1
827 fi
828 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500829ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700830
831EOF
832 fi
833
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700834 write_makefile_header "$BOARDMK"
835 write_makefile_header "$PRODUCTMK"
836 write_blueprint_header "$ANDROIDBP"
837
838 cat << EOF >> "$ANDROIDBP"
839soong_namespace {
840}
841
842EOF
843
844 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
845 cat << EOF >> "$PRODUCTMK"
846PRODUCT_SOONG_NAMESPACES += \\
847 vendor/$VENDOR/$DEVICE
848
849EOF
Steve Kondik5bd66602016-07-15 10:39:58 -0700850}
851
852#
853# write_footers:
854#
855# Closes the inital guard and any other finalization tasks. Must
856# be called as the final step.
857#
858function write_footers() {
859 cat << EOF >> "$ANDROIDMK"
860endif
861EOF
862}
863
864# Return success if adb is up and not in recovery
865function _adb_connected {
866 {
Jake Whatley9843b322017-01-25 21:49:16 -0500867 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700868 then
869 return 0
870 fi
871 } 2>/dev/null
872
873 return 1
874};
875
876#
877# parse_file_list:
878#
879# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400880# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700881#
882# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
883#
884function parse_file_list() {
885 if [ -z "$1" ]; then
886 echo "An input file is expected!"
887 exit 1
888 elif [ ! -f "$1" ]; then
889 echo "Input file "$1" does not exist!"
890 exit 1
891 fi
892
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200893 if [ -n "$2" ]; then
894 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400895 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +0200896 # Match all lines starting with first line found to start* with '#'
897 # comment and contain** $2, and ending with first line to be empty*.
898 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
899 # **the $2 match is case-insensitive
900 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400901 else
902 LIST=$1
903 fi
904
905
Steve Kondik5bd66602016-07-15 10:39:58 -0700906 PRODUCT_PACKAGES_LIST=()
907 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +0200908 PRODUCT_PACKAGES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -0700909 PRODUCT_COPY_FILES_LIST=()
910 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +0200911 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -0700912
913 while read -r line; do
914 if [ -z "$line" ]; then continue; fi
915
916 # If the line has a pipe delimiter, a sha1 hash should follow.
917 # This indicates the file should be pinned and not overwritten
918 # when extracting files.
919 local SPLIT=(${line//\|/ })
920 local COUNT=${#SPLIT[@]}
921 local SPEC=${SPLIT[0]}
922 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +0200923 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -0700924 if [ "$COUNT" -gt "1" ]; then
925 HASH=${SPLIT[1]}
926 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +0200927 if [ "$COUNT" -gt "2" ]; then
928 FIXUP_HASH=${SPLIT[2]}
929 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700930
931 # if line starts with a dash, it needs to be packaged
932 if [[ "$SPEC" =~ ^- ]]; then
933 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
934 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +0200935 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -0700936 else
937 PRODUCT_COPY_FILES_LIST+=("$SPEC")
938 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +0200939 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -0700940 fi
941
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400942 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700943}
944
945#
946# write_makefiles:
947#
948# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400949# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700950#
951# Calls write_product_copy_files and write_product_packages on
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700952# the given file and appends to the Android.bp as well as
Steve Kondik5bd66602016-07-15 10:39:58 -0700953# the product makefile.
954#
955function write_makefiles() {
956 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400957 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700958 write_product_packages
959}
960
961#
962# append_firmware_calls_to_makefiles:
963#
964# Appends to Android.mk the calls to all images present in radio folder
965# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
966#
967function append_firmware_calls_to_makefiles() {
968 cat << EOF >> "$ANDROIDMK"
969ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
970
971RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
972\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
973 \$(call add-radio-file,radio/\$(f)))
974\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
975
976endif
977
978EOF
979}
980
981#
982# get_file:
983#
984# $1: input file
985# $2: target file/folder
986# $3: source of the file (can be "adb" or a local folder)
987#
988# Silently extracts the input file to defined target
989# Returns success if file can be pulled from the device or found locally
990#
991function get_file() {
992 local SRC="$3"
993
994 if [ "$SRC" = "adb" ]; then
995 # try to pull
996 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
997
998 return 1
999 else
1000 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001001 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1002 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001003 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001004
1005 return 1
1006 fi
1007};
1008
1009#
1010# oat2dex:
1011#
1012# $1: extracted apk|jar (to check if deodex is required)
1013# $2: odexed apk|jar to deodex
1014# $3: source of the odexed apk|jar
1015#
1016# Convert apk|jar .odex in the corresposing classes.dex
1017#
1018function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001019 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001020 local OEM_TARGET="$2"
1021 local SRC="$3"
1022 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001023 local OAT=
1024 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -07001025
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001026 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1027 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1028 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001029 fi
1030
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001031 if [ -z "$VDEXEXTRACTOR" ]; then
theimpulson9a911af2019-08-14 03:25:12 +00001032 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001033 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001034
codeworkx85eda752018-09-23 12:36:57 +02001035 if [ -z "$CDEXCONVERTER" ]; then
theimpulson9a911af2019-08-14 03:25:12 +00001036 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001037 fi
1038
Steve Kondik5bd66602016-07-15 10:39:58 -07001039 # Extract existing boot.oats to the temp folder
1040 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001041 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001042 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001043 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001044 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001045 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001046 else
1047 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001048 fi
1049 done
1050 fi
1051
1052 if [ -z "$ARCHES" ]; then
1053 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1054 fi
1055
theimpulson9a911af2019-08-14 03:25:12 +00001056 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001057 return;
1058 fi
1059
theimpulson9a911af2019-08-14 03:25:12 +00001060 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001061 return 0 # target apk|jar is already odexed, return
1062 fi
1063
1064 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001065 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001066
Joe Maplesfb3941c2018-01-05 14:51:33 -05001067 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1068 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001069
Joe Maplesfb3941c2018-01-05 14:51:33 -05001070 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1071 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1072 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001073 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1074 for CLASS in $CLASSES; do
1075 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1076 # Check if we have to deal with CompactDex
1077 if [[ "$CLASS" == *.cdex ]]; then
1078 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1079 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1080 else
1081 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1082 fi
1083 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001084 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001085 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1086 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001087 fi
theimpulson9a911af2019-08-14 03:25:12 +00001088 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001089 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001090 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001091 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001092 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001093 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001094 # try to extract classes.dex from boot.vdex for frameworks jars
1095 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001096 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001097 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001098 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1099 for CLASS in $CLASSES; do
1100 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1101 # Check if we have to deal with CompactDex
1102 if [[ "$CLASS" == *.cdex ]]; then
1103 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1104 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1105 else
1106 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1107 fi
1108 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001109 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001110 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1111 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001112 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001113 else
1114 continue
1115 fi
1116
Steve Kondik5bd66602016-07-15 10:39:58 -07001117 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001118
1119 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001120}
1121
1122#
1123# init_adb_connection:
1124#
1125# Starts adb server and waits for the device
1126#
1127function init_adb_connection() {
1128 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1129 if ! _adb_connected; then
1130 echo "No device is online. Waiting for one..."
1131 echo "Please connect USB and/or enable USB debugging"
1132 until _adb_connected; do
1133 sleep 1
1134 done
1135 echo "Device Found."
1136 fi
1137
1138 # Retrieve IP and PORT info if we're using a TCP connection
1139 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
1140 | head -1 | awk '{print $1}')
1141 adb root &> /dev/null
1142 sleep 0.3
1143 if [ -n "$TCPIPPORT" ]; then
1144 # adb root just killed our connection
1145 # so reconnect...
1146 adb connect "$TCPIPPORT"
1147 fi
1148 adb wait-for-device &> /dev/null
1149 sleep 0.3
1150}
1151
1152#
1153# fix_xml:
1154#
1155# $1: xml file to fix
1156#
1157function fix_xml() {
1158 local XML="$1"
1159 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1160
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001161 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1162 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001163
1164 mv "$TEMP_XML" "$XML"
1165}
1166
Vladimir Olteande985fe2019-01-17 03:07:34 +02001167function get_hash() {
1168 local FILE="$1"
1169
1170 if [ "$(uname)" == "Darwin" ]; then
1171 shasum "${FILE}" | awk '{print $1}'
1172 else
1173 sha1sum "${FILE}" | awk '{print $1}'
1174 fi
1175}
1176
Vladimir Olteana7d20492019-01-17 03:05:52 +02001177function print_spec() {
1178 local SPEC_PRODUCT_PACKAGE="$1"
1179 local SPEC_SRC_FILE="$2"
1180 local SPEC_DST_FILE="$3"
1181 local SPEC_ARGS="$4"
1182 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001183 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001184
1185 local PRODUCT_PACKAGE=""
1186 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1187 PRODUCT_PACKAGE="-"
1188 fi
1189 local SRC=""
1190 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1191 SRC="${SPEC_SRC_FILE}:"
1192 fi
1193 local DST=""
1194 if [ ! -z "${SPEC_DST_FILE}" ]; then
1195 DST="${SPEC_DST_FILE}"
1196 fi
1197 local ARGS=""
1198 if [ ! -z "${SPEC_ARGS}" ]; then
1199 ARGS=";${SPEC_ARGS}"
1200 fi
1201 local HASH=""
1202 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1203 HASH="|${SPEC_HASH}"
1204 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001205 local FIXUP_HASH=""
1206 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1207 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1208 fi
1209 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1210}
1211
1212# To be overridden by device-level extract-files.sh
1213# Parameters:
1214# $1: spec name of a blob. Can be used for filtering.
1215# If the spec is "src:dest", then $1 is "dest".
1216# If the spec is "src", then $1 is "src".
1217# $2: path to blob file. Can be used for fixups.
1218#
1219function blob_fixup() {
1220 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001221}
1222
Steve Kondik5bd66602016-07-15 10:39:58 -07001223#
1224# extract:
1225#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001226# Positional parameters:
1227# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001228# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001229# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1230#
1231# Non-positional parameters (coming after $2):
1232# --section: preferred way of selecting the portion to parse and extract from
1233# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001234# --kang: if present, this option will activate the printing of hashes for the
1235# extracted blobs. Useful with --section for subsequent pinning of
1236# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001237#
1238function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001239 # Consume positional parameters
1240 local PROPRIETARY_FILES_TXT="$1"; shift
1241 local SRC="$1"; shift
1242 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001243 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001244
1245 # Consume optional, non-positional parameters
1246 while [ "$#" -gt 0 ]; do
1247 case "$1" in
1248 -s|--section)
1249 SECTION="$2"; shift
1250 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001251 -k|--kang)
1252 KANG=true
1253 DISABLE_PINNING=1
1254 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001255 *)
1256 # Backwards-compatibility with the old behavior, where $3, if
1257 # present, denoted an optional positional ${SECTION} argument.
1258 # Users of ${SECTION} are encouraged to migrate from setting it as
1259 # positional $3, to non-positional --section ${SECTION}, the
1260 # reason being that it doesn't scale to have more than 1 optional
1261 # positional argument.
1262 SECTION="$1"
1263 ;;
1264 esac
1265 shift
1266 done
1267
Steve Kondik5bd66602016-07-15 10:39:58 -07001268 if [ -z "$OUTDIR" ]; then
1269 echo "Output dir not set!"
1270 exit 1
1271 fi
1272
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001273 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001274
1275 # Allow failing, so we can try $DEST and/or $FILE
1276 set +e
1277
1278 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1279 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001280 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001281 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001282 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001283 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001284 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1285
1286 if [ "$SRC" = "adb" ]; then
1287 init_adb_connection
1288 fi
1289
Dan Pasanen0cc05012017-03-21 09:06:11 -05001290 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001291 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001292
1293 # Check if we're working with the same zip that was passed last time.
1294 # If so, let's just use what's already extracted.
1295 MD5=`md5sum "$SRC"| awk '{print $1}'`
1296 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1297
1298 if [ "$MD5" != "$OLDMD5" ]; then
1299 rm -rf "$DUMPDIR"
1300 mkdir "$DUMPDIR"
1301 unzip "$SRC" -d "$DUMPDIR"
1302 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1303
1304 # Stop if an A/B OTA zip is detected. We cannot extract these.
1305 if [ -a "$DUMPDIR"/payload.bin ]; then
1306 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1307 exit 1
1308 # If OTA is block based, extract it.
1309 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1310 echo "Converting system.new.dat to system.img"
theimpulson9a911af2019-08-14 03:25:12 +00001311 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 -05001312 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1313 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1314 echo "Requesting sudo access to mount the system.img"
1315 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1316 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1317 sudo umount "$DUMPDIR"/tmp
1318 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1319 fi
1320 fi
1321
1322 SRC="$DUMPDIR"
1323 fi
1324
Steve Kondik5bd66602016-07-15 10:39:58 -07001325 if [ "$VENDOR_STATE" -eq "0" ]; then
1326 echo "Cleaning output directory ($OUTPUT_ROOT).."
1327 rm -rf "${OUTPUT_TMP:?}"
1328 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001329 if [ -d "$OUTPUT_ROOT" ]; then
1330 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1331 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001332 VENDOR_STATE=1
1333 fi
1334
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001335 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001336
1337 for (( i=1; i<COUNT+1; i++ )); do
1338
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001339 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001340 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001341 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001342 local OUTPUT_DIR=
1343 local TMP_DIR=
1344 local SRC_FILE=
1345 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001346 local IS_PRODUCT_PACKAGE=false
1347
1348 # Note: this relies on the fact that the ${FILELIST[@]} array
1349 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1350 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1351 IS_PRODUCT_PACKAGE=true
1352 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001353
Vladimir Olteand6391332018-06-24 20:42:01 +03001354 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001355 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1356 TMP_DIR="${OUTPUT_TMP}/rootfs"
1357 SRC_FILE="/${SPEC_SRC_FILE}"
1358 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001359 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001360 OUTPUT_DIR="${OUTPUT_ROOT}"
1361 TMP_DIR="${OUTPUT_TMP}"
1362 SRC_FILE="/system/${SPEC_SRC_FILE}"
1363 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001364 fi
1365
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001366 # Strip the file path in the vendor repo of "system", if present
1367 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001368 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001369 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001370
Gabriele M58270a32017-11-13 23:15:29 +01001371 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001372 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001373 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001374 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001375 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001376 if [ -f "${VENDOR_REPO_FILE}" ]; then
1377 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001378 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001379 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001380 fi
1381 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001382 local TMP_HASH=$(get_hash "${PINNED}")
1383 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001384 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001385 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1386 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001387 fi
1388 fi
1389 fi
1390 fi
1391
Vladimir Olteana7d20492019-01-17 03:05:52 +02001392 if [ "${KANG}" = false ]; then
1393 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1394 fi
1395
Gabriele M58270a32017-11-13 23:15:29 +01001396 if [ "$KEEP" = "1" ]; then
Vladimir Olteana7d20492019-01-17 03:05:52 +02001397 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001398 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001399 FOUND=false
1400 # Try Lineage target first.
1401 # Also try to search for files stripped of
1402 # the "/system" prefix, if we're actually extracting
1403 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001404 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001405 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1406 FOUND=true
1407 break
1408 }
1409 done
1410
1411 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001412 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001413 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001414 fi
1415 fi
1416
Vladimir Olteande985fe2019-01-17 03:07:34 +02001417 # Blob fixup pipeline has 2 parts: one that is fixed and
1418 # one that is user-configurable
1419 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1420 # Deodex apk|jar if that's the case
1421 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1422 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1423 if [ -f "$TMPDIR/classes.dex" ]; then
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001424 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1425 rm "$TMPDIR/classes"*
Vladimir Olteande985fe2019-01-17 03:07:34 +02001426 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001427 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001428 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1429 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001430 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001431 # Now run user-supplied fixup function
1432 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1433 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001434
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001435 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001436 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001437 local TYPE="${DIR##*/}"
1438 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001439 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001440 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001441 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001442 fi
1443 fi
1444
Vladimir Olteana7d20492019-01-17 03:05:52 +02001445 if [ "${KANG}" = true ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001446 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1447 fi
1448
1449 # Check and print whether the fixup pipeline actually did anything.
1450 # This isn't done right after the fixup pipeline because we want this print
1451 # to come after print_spec above, when in kang mode.
1452 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1453 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1454 # Now sanity-check the spec for this blob.
1455 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1456 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1457 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1458 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001459 fi
1460
Steve Kondik5bd66602016-07-15 10:39:58 -07001461 done
1462
1463 # Don't allow failing
1464 set -e
1465}
1466
1467#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001468# extract2:
1469#
1470# Positional parameters:
1471# $1: file containing the list of items to extract (aka proprietary-files.txt)
1472#
1473# Non-positional parameters (coming after $2):
1474# --section: selects the portion to parse and extracts from proprietary-files.txt
1475# --kang: if present, this option will activate the printing of hashes for the
1476# extracted blobs. Useful with --section for subsequent pinning of
1477# blobs taken from other origins.
1478#
1479function extract2() {
1480 # Consume positional parameters
1481 local PROPRIETARY_FILES_TXT="$1"; shift
1482 local SECTION=""
1483 local KANG=false
1484
1485 # Consume optional, non-positional parameters
1486 while [ "$#" -gt 0 ]; do
1487 case "$1" in
1488 --adb)
1489 ADB=true
1490 ;;
1491 --system)
1492 SYSTEM_SRC="$2"; shift
1493 ;;
1494 --vendor)
1495 VENDOR_SRC="$2"; shift
1496 ;;
1497 --odm)
1498 ODM_SRC="$2"; shift
1499 ;;
1500 --product)
1501 PRODUCT_SRC="$2"; shift
1502 ;;
1503 -s|--section)
1504 SECTION="$2"; shift
1505 ;;
1506 -k|--kang)
1507 KANG=true
1508 DISABLE_PINNING=1
1509 ;;
1510 esac
1511 shift
1512 done
1513
1514 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1515 echo "No sources set! You must select --adb or pass paths to partition dumps."
1516 exit 1
1517 fi
1518
1519 if [ -z "$OUTDIR" ]; then
1520 echo "Output dir not set!"
1521 exit 1
1522 fi
1523
1524 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1525
1526 # Allow failing, so we can try $DEST and/or $FILE
1527 set +e
1528
1529 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1530 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1531 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1532 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1533 local COUNT=${#FILELIST[@]}
1534 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1535 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1536
1537 if [ "$ADB" = true ]; then
1538 init_adb_connection
1539 fi
1540
1541 if [ "$VENDOR_STATE" -eq "0" ]; then
1542 echo "Cleaning output directory ($OUTPUT_ROOT).."
1543 rm -rf "${OUTPUT_TMP:?}"
1544 mkdir -p "${OUTPUT_TMP:?}"
1545 if [ -d "$OUTPUT_ROOT" ]; then
1546 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1547 fi
1548 VENDOR_STATE=1
1549 fi
1550
1551 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1552
1553 for (( i=1; i<COUNT+1; i++ )); do
1554
1555 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1556 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1557 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1558 local OUTPUT_DIR=
1559 local TMP_DIR=
1560 local SRC_FILE=
1561 local DST_FILE=
1562 local IS_PRODUCT_PACKAGE=false
1563
1564 # Note: this relies on the fact that the ${FILELIST[@]} array
1565 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1566 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1567 IS_PRODUCT_PACKAGE=true
1568 fi
1569
1570 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1571 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1572 TMP_DIR="${OUTPUT_TMP}/rootfs"
1573 else
1574 OUTPUT_DIR="${OUTPUT_ROOT}"
1575 TMP_DIR="${OUTPUT_TMP}"
1576 fi
1577 SRC_FILE="${SPEC_SRC_FILE}"
1578 DST_FILE="${SPEC_DST_FILE}"
1579
1580 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1581 local BLOB_DISPLAY_NAME="${DST_FILE}"
1582 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1583
1584 # Check pinned files
1585 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1586 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1587 local KEEP=""
1588 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1589 if [ -f "${VENDOR_REPO_FILE}" ]; then
1590 local PINNED="${VENDOR_REPO_FILE}"
1591 else
1592 local PINNED="${TMP_DIR}${DST_FILE}"
1593 fi
1594 if [ -f "$PINNED" ]; then
1595 local TMP_HASH=$(get_hash "${PINNED}")
1596 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1597 KEEP="1"
1598 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1599 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1600 fi
1601 fi
1602 fi
1603 fi
1604
1605 if [ "${KANG}" = false ]; then
1606 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1607 fi
1608
1609 if [ "$KEEP" = "1" ]; then
1610 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1611 else
1612 FOUND=false
1613 PARTITION_SOURCE_DIR=
1614 # Try Lineage target first.
1615 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1616 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1617 if [ "$PARTITION" = "system" ]; then
1618 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1619 elif [ "$PARTITION" = "vendor" ]; then
1620 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1621 elif [ "$PARTITION" = "product" ]; then
1622 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1623 elif [ "$PARTITION" = "odm" ]; then
1624 PARTITION_SOURCE_DIR="$ODM_SRC"
1625 fi
1626 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1627 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1628 FOUND=true
1629 break
1630 }
1631 # Search with the full system/ prefix if the file was not found on the system partition
1632 # because we may be searching in a mounted system-as-root system.img
1633 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1634 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1635 FOUND=true
1636 break
1637 }
1638 fi
1639 done
1640
1641 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1642 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1643 fi
1644
1645 if [ "${FOUND}" = false ]; then
1646 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1647 continue
1648 fi
1649 fi
1650
1651 # Blob fixup pipeline has 2 parts: one that is fixed and
1652 # one that is user-configurable
1653 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1654 # Deodex apk|jar if that's the case
1655 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1656 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
1657 if [ -f "$TMPDIR/classes.dex" ]; then
1658 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1659 rm "$TMPDIR/classes"*
1660 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1661 fi
1662 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1663 fix_xml "${VENDOR_REPO_FILE}"
1664 fi
1665 # Now run user-supplied fixup function
1666 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1667 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1668
1669 if [ -f "${VENDOR_REPO_FILE}" ]; then
1670 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1671 local TYPE="${DIR##*/}"
1672 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1673 chmod 755 "${VENDOR_REPO_FILE}"
1674 else
1675 chmod 644 "${VENDOR_REPO_FILE}"
1676 fi
1677 fi
1678
1679 if [ "${KANG}" = true ]; then
1680 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1681 fi
1682
1683 # Check and print whether the fixup pipeline actually did anything.
1684 # This isn't done right after the fixup pipeline because we want this print
1685 # to come after print_spec above, when in kang mode.
1686 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1687 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1688 # Now sanity-check the spec for this blob.
1689 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1690 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
1691 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
1692 fi
1693 fi
1694
1695 done
1696
1697 # Don't allow failing
1698 set -e
1699}
1700
1701#
Steve Kondik5bd66602016-07-15 10:39:58 -07001702# extract_firmware:
1703#
1704# $1: file containing the list of items to extract
1705# $2: path to extracted radio folder
1706#
1707function extract_firmware() {
1708 if [ -z "$OUTDIR" ]; then
1709 echo "Output dir not set!"
1710 exit 1
1711 fi
1712
1713 parse_file_list "$1"
1714
1715 # Don't allow failing
1716 set -e
1717
1718 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1719 local COUNT=${#FILELIST[@]}
1720 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001721 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001722
1723 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1724 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1725 rm -rf "${OUTPUT_DIR:?}/"*
1726 VENDOR_RADIO_STATE=1
1727 fi
1728
1729 echo "Extracting $COUNT files in $1 from $SRC:"
1730
1731 for (( i=1; i<COUNT+1; i++ )); do
1732 local FILE="${FILELIST[$i-1]}"
1733 printf ' - %s \n' "/radio/$FILE"
1734
1735 if [ ! -d "$OUTPUT_DIR" ]; then
1736 mkdir -p "$OUTPUT_DIR"
1737 fi
1738 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1739 chmod 644 "$OUTPUT_DIR/$FILE"
1740 done
1741}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001742
1743function extract_img_data() {
1744 local image_file="$1"
1745 local out_dir="$2"
1746 local logFile="$TMPDIR/debugfs.log"
1747
1748 if [ ! -d "$out_dir" ]; then
1749 mkdir -p "$out_dir"
1750 fi
1751
1752 if [[ "$HOST_OS" == "Darwin" ]]; then
1753 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1754 echo "[-] Failed to extract data from '$image_file'"
1755 abort 1
1756 }
1757 else
1758 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1759 do
1760 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1761 echo "[-] Failed to extract data from '$image_file'"
1762 abort 1
1763 }
1764 done
1765 fi
1766
1767 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1768 if grep -Fq "$symlink_err" "$logFile"; then
1769 echo "[-] Symlinks have not been properly processed from $image_file"
1770 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1771 abort 1
1772 fi
1773}
1774
1775declare -ra VENDOR_SKIP_FILES=(
1776 "bin/toybox_vendor"
1777 "bin/toolbox"
1778 "bin/grep"
1779 "build.prop"
1780 "compatibility_matrix.xml"
1781 "default.prop"
1782 "etc/NOTICE.xml.gz"
1783 "etc/vintf/compatibility_matrix.xml"
1784 "etc/vintf/manifest.xml"
1785 "etc/wifi/wpa_supplicant.conf"
1786 "manifest.xml"
1787 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1788 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1789 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1790 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1791 "overlay/framework-res__auto_generated_rro.apk"
1792 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1793)
1794
1795function array_contains() {
1796 local element
1797 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1798 return 1
1799}
1800
1801function generate_prop_list_from_image() {
1802 local image_file="$1"
1803 local image_dir="$TMPDIR/image-temp"
1804 local output_list="$2"
1805 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1806 local -n skipped_vendor_files="$3"
1807
1808 extract_img_data "$image_file" "$image_dir"
1809
1810 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1811 do
1812 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1813 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1814 continue
1815 fi
1816 # Skip device defined skipped files since they will be re-generated at build time
1817 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1818 continue
1819 fi
1820 if suffix_match_file ".apk" "$FILE" ; then
1821 echo "-vendor/$FILE" >> "$output_list_tmp"
1822 else
1823 echo "vendor/$FILE" >> "$output_list_tmp"
1824 fi
1825 done
1826
1827 # Sort merged file with all lists
1828 sort -u "$output_list_tmp" > "$output_list"
1829
1830 # Clean-up
1831 rm -f "$output_list_tmp"
1832}