blob: 655bf5c70df6e35fca6d69eb1119de0b96aa35cd [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=()
20PRODUCT_PACKAGES_LIST=()
21PRODUCT_PACKAGES_HASHES=()
22PACKAGE_LIST=()
23VENDOR_STATE=-1
24VENDOR_RADIO_STATE=-1
25COMMON=-1
26ARCHES=
27FULLY_DEODEXED=-1
28
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040029TMPDIR=$(mktemp -d)
Steve Kondik5bd66602016-07-15 10:39:58 -070030
31#
32# cleanup
33#
34# kill our tmpfiles with fire on exit
35#
36function cleanup() {
37 rm -rf "${TMPDIR:?}"
38}
39
40trap cleanup EXIT INT TERM ERR
41
42#
43# setup_vendor
44#
45# $1: device name
46# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000047# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070048# $4: is common device - optional, default to false
49# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050050# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070051#
52# Must be called before any other functions can be used. This
53# sets up the internal state for a new vendor configuration.
54#
55function setup_vendor() {
56 local DEVICE="$1"
57 if [ -z "$DEVICE" ]; then
58 echo "\$DEVICE must be set before including this script!"
59 exit 1
60 fi
61
62 export VENDOR="$2"
63 if [ -z "$VENDOR" ]; then
64 echo "\$VENDOR must be set before including this script!"
65 exit 1
66 fi
67
theimpulson9a911af2019-08-14 03:25:12 +000068 export OMNI_ROOT="$3"
69 if [ ! -d "$OMNI_ROOT" ]; then
70 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070071 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000075 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
76 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070077 fi
78
Jake Whatley9843b322017-01-25 21:49:16 -050079 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
theimpulson9a911af2019-08-14 03:25:12 +000084 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/device-vendor.mk
85 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
86 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070087
88 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
89 COMMON=1
90 else
91 COMMON=0
92 fi
93
Gabriele Mc44696d2017-05-01 18:22:04 +020094 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070095 VENDOR_STATE=1
96 VENDOR_RADIO_STATE=1
97 else
98 VENDOR_STATE=0
99 VENDOR_RADIO_STATE=0
100 fi
101}
102
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300103# Helper functions for parsing a spec.
104# notes: an optional "|SHA1" that may appear in the format is stripped
105# early from the spec in the parse_file_list function, and
106# should not be present inside the input parameter passed
107# to these functions.
108
109#
110# input: spec in the form of "src[:dst][;args]"
111# output: "src"
112#
113function src_file() {
114 local SPEC="$1"
115 local SPLIT=(${SPEC//:/ })
116 local ARGS="$(target_args ${SPEC})"
117 # Regardless of there being a ":" delimiter or not in the spec,
118 # the source file is always either the first, or the only entry.
119 local SRC="${SPLIT[0]}"
120 # Remove target_args suffix, if present
121 echo "${SRC%;${ARGS}}"
122}
123
Steve Kondik5bd66602016-07-15 10:39:58 -0700124#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300125# input: spec in the form of "src[:dst][;args]"
126# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700127#
128function target_file() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300129 local SPEC="$1"
130 local SPLIT=(${SPEC//:/ })
131 local ARGS="$(target_args ${SPEC})"
132 local DST=
133 case ${#SPLIT[@]} in
134 1)
135 # The spec doesn't have a : delimiter
136 DST="${SPLIT[0]}"
137 ;;
138 *)
139 # The spec actually has a src:dst format
140 DST="${SPLIT[1]}"
141 ;;
142 esac
143 # Remove target_args suffix, if present
144 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700145}
146
147#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300148# input: spec in the form of "src[:dst][;args]"
149# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700150#
151function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300152 local SPEC="$1"
153 local SPLIT=(${SPEC//;/ })
154 local ARGS=
155 case ${#SPLIT[@]} in
156 1)
157 # No ";" delimiter in the spec.
158 ;;
159 *)
160 # The "args" are whatever comes after the ";" character.
161 # Basically the spec stripped of whatever is to the left of ";".
162 ARGS="${SPEC#${SPLIT[0]};}"
163 ;;
164 esac
165 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700166}
167
168#
169# prefix_match:
170#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300171# input:
172# - $1: prefix
173# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
174# output:
175# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700176#
177function prefix_match() {
178 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300179 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
180 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700181 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300182 local ARGS=$(target_args "$LINE")
183 if [ -z "${ARGS}" ]; then
184 echo "${FILE#$PREFIX}"
185 else
186 echo "${FILE#$PREFIX};${ARGS}"
187 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700188 fi
189 done
190}
191
192#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400193# prefix_match_file:
194#
195# $1: the prefix to match on
196# $2: the file to match the prefix for
197#
198# Internal function which returns true if a filename contains the
199# specified prefix.
200#
201function prefix_match_file() {
202 local PREFIX="$1"
203 local FILE="$2"
204 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
205 return 0
206 else
207 return 1
208 fi
209}
210
211#
212# truncate_file
213#
214# $1: the filename to truncate
215# $2: the argument to output the truncated filename to
216#
217# Internal function which truncates a filename by removing the first dir
218# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
219#
220function truncate_file() {
221 local FILE="$1"
222 RETURN_FILE="$2"
223 local FIND="${FILE%%/*}"
224 local LOCATION="${#FIND}+1"
225 echo ${FILE:$LOCATION}
226}
227
228#
Steve Kondik5bd66602016-07-15 10:39:58 -0700229# write_product_copy_files:
230#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400231# $1: make treble compatible makefile - optional, default to false
232#
Steve Kondik5bd66602016-07-15 10:39:58 -0700233# Creates the PRODUCT_COPY_FILES section in the product makefile for all
234# items in the list which do not start with a dash (-).
235#
236function write_product_copy_files() {
237 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
238 local TARGET=
239 local FILE=
240 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400241 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700242
243 if [ "$COUNT" -eq "0" ]; then
244 return 0
245 fi
246
247 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
248 for (( i=1; i<COUNT+1; i++ )); do
249 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
250 LINEEND=" \\"
251 if [ "$i" -eq "$COUNT" ]; then
252 LINEEND=""
253 fi
254
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300255 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400256 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
257 if prefix_match_file "vendor/" $TARGET ; then
258 local OUTTARGET=$(truncate_file $TARGET)
259 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
260 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
261 else
262 printf ' %s/proprietary/%s:system/%s%s\n' \
263 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
264 fi
265 else
266 printf ' %s/proprietary/%s:system/%s%s\n' \
267 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
268 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700269 done
270 return 0
271}
272
273#
274# write_packages:
275#
276# $1: The LOCAL_MODULE_CLASS for the given module list
277# $2: "true" if this package is part of the vendor/ path
278# $3: type-specific extra flags
279# $4: Name of the array holding the target list
280#
281# Internal function which writes out the BUILD_PREBUILT stanzas
282# for all modules in the list. This is called by write_product_packages
283# after the modules are categorized.
284#
285function write_packages() {
286
287 local CLASS="$1"
288 local VENDOR_PKG="$2"
289 local EXTRA="$3"
290
291 # Yes, this is a horrible hack - we create a new array using indirection
292 local ARR_NAME="$4[@]"
293 local FILELIST=("${!ARR_NAME}")
294
295 local FILE=
296 local ARGS=
297 local BASENAME=
298 local EXTENSION=
299 local PKGNAME=
300 local SRC=
301
302 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300303 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700304 ARGS=$(target_args "$P")
305
306 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100307 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700308 EXTENSION=${BASENAME##*.}
309 PKGNAME=${BASENAME%.*}
310
311 # Add to final package list
312 PACKAGE_LIST+=("$PKGNAME")
313
314 SRC="proprietary"
315 if [ "$VENDOR_PKG" = "true" ]; then
316 SRC+="/vendor"
317 fi
318
319 printf 'include $(CLEAR_VARS)\n'
320 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
321 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
322 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
323 if [ "$EXTRA" = "both" ]; then
324 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
325 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
326 #if [ "$VENDOR_PKG" = "true" ]; then
327 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
328 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
329 #else
330 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
331 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
332 #fi
333 elif [ "$EXTRA" = "64" ]; then
334 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
335 else
336 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
337 fi
338 if [ "$EXTRA" != "none" ]; then
339 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
340 fi
341 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200342 if [ "$EXTRA" = "priv-app" ]; then
343 SRC="$SRC/priv-app"
344 else
345 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700346 fi
347 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
348 local CERT=platform
349 if [ ! -z "$ARGS" ]; then
350 CERT="$ARGS"
351 fi
352 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
353 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
354 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200355 local CERT=platform
356 if [ ! -z "$ARGS" ]; then
357 CERT="$ARGS"
358 fi
359 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700360 elif [ "$CLASS" = "ETC" ]; then
361 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
362 elif [ "$CLASS" = "EXECUTABLES" ]; then
363 if [ "$ARGS" = "rootfs" ]; then
364 SRC="$SRC/rootfs"
365 if [ "$EXTRA" = "sbin" ]; then
366 SRC="$SRC/sbin"
367 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
368 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
369 fi
370 else
371 SRC="$SRC/bin"
372 fi
373 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
374 unset EXTENSION
375 else
376 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
377 fi
378 printf 'LOCAL_MODULE_TAGS := optional\n'
379 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400380 if [ "$CLASS" = "APPS" ]; then
381 printf 'LOCAL_DEX_PREOPT := false\n'
382 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700383 if [ ! -z "$EXTENSION" ]; then
384 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
385 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100386 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
387 if [ "$DIRNAME" != "." ]; then
388 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
389 fi
390 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700391 if [ "$EXTRA" = "priv-app" ]; then
392 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
393 fi
394 if [ "$VENDOR_PKG" = "true" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800395 printf 'LOCAL_VENDOR_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700396 fi
397 printf 'include $(BUILD_PREBUILT)\n\n'
398 done
399}
400
401#
402# write_product_packages:
403#
404# This function will create BUILD_PREBUILT entries in the
405# Android.mk and associated PRODUCT_PACKAGES list in the
406# product makefile for all files in the blob list which
407# start with a single dash (-) character.
408#
409function write_product_packages() {
410 PACKAGE_LIST=()
411
412 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
413
414 if [ "$COUNT" = "0" ]; then
415 return 0
416 fi
417
418 # Figure out what's 32-bit, what's 64-bit, and what's multilib
419 # I really should not be doing this in bash due to shitty array passing :(
420 local T_LIB32=( $(prefix_match "lib/") )
421 local T_LIB64=( $(prefix_match "lib64/") )
422 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
423 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
424 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
425
426 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
427 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
428 fi
429 if [ "${#LIB32[@]}" -gt "0" ]; then
430 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
431 fi
432 if [ "${#LIB64[@]}" -gt "0" ]; then
433 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
434 fi
435
436 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
437 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
438 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
439 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
440 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
441
442 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
443 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
444 fi
445 if [ "${#V_LIB32[@]}" -gt "0" ]; then
446 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
447 fi
448 if [ "${#V_LIB64[@]}" -gt "0" ]; then
449 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
450 fi
451
452 # Apps
453 local APPS=( $(prefix_match "app/") )
454 if [ "${#APPS[@]}" -gt "0" ]; then
455 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
456 fi
457 local PRIV_APPS=( $(prefix_match "priv-app/") )
458 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
459 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
460 fi
461 local V_APPS=( $(prefix_match "vendor/app/") )
462 if [ "${#V_APPS[@]}" -gt "0" ]; then
463 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
464 fi
465 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
466 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
467 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
468 fi
469
470 # Framework
471 local FRAMEWORK=( $(prefix_match "framework/") )
472 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
473 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
474 fi
Christian Oder974b5902017-10-08 23:15:52 +0200475 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200476 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Christian Oder974b5902017-10-08 23:15:52 +0200477 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
478 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700479
480 # Etc
481 local ETC=( $(prefix_match "etc/") )
482 if [ "${#ETC[@]}" -gt "0" ]; then
483 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
484 fi
485 local V_ETC=( $(prefix_match "vendor/etc/") )
486 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabcc98bc32017-10-08 17:33:42 -0400487 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700488 fi
489
490 # Executables
491 local BIN=( $(prefix_match "bin/") )
492 if [ "${#BIN[@]}" -gt "0" ]; then
493 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
494 fi
495 local V_BIN=( $(prefix_match "vendor/bin/") )
496 if [ "${#V_BIN[@]}" -gt "0" ]; then
497 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
498 fi
499 local SBIN=( $(prefix_match "sbin/") )
500 if [ "${#SBIN[@]}" -gt "0" ]; then
501 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
502 fi
503
504
505 # Actually write out the final PRODUCT_PACKAGES list
506 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
507
508 if [ "$PACKAGE_COUNT" -eq "0" ]; then
509 return 0
510 fi
511
512 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
513 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
514 local LINEEND=" \\"
515 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
516 LINEEND=""
517 fi
518 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
519 done
520}
521
522#
523# write_header:
524#
525# $1: file which will be written to
526#
527# writes out the copyright header with the current year.
528# note that this is not an append operation, and should
529# be executed first!
530#
531function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500532 if [ -f $1 ]; then
533 rm $1
534 fi
535
Steve Kondik5bd66602016-07-15 10:39:58 -0700536 YEAR=$(date +"%Y")
537
538 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
539
Jake Whatley9843b322017-01-25 21:49:16 -0500540 NUM_REGEX='^[0-9]+$'
541 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
542 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
543 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
544 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
545 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
546 fi
547 if [ $YEAR -eq 2017 ]; then
548 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
549 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
550 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
551 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
552 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
553 else
554 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
555 fi
556 else
557 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
558 fi
559
560 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700561#
562# Licensed under the Apache License, Version 2.0 (the "License");
563# you may not use this file except in compliance with the License.
564# You may obtain a copy of the License at
565#
566# http://www.apache.org/licenses/LICENSE-2.0
567#
568# Unless required by applicable law or agreed to in writing, software
569# distributed under the License is distributed on an "AS IS" BASIS,
570# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
571# See the License for the specific language governing permissions and
572# limitations under the License.
573
574# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
575
576EOF
577}
578
579#
580# write_headers:
581#
582# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500583# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700584#
585# Calls write_header for each of the makefiles and creates
586# the initial path declaration and device guard for the
587# Android.mk
588#
589function write_headers() {
590 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500591
592 GUARD="$2"
593 if [ -z "$GUARD" ]; then
594 GUARD="TARGET_DEVICE"
595 fi
596
Steve Kondik5bd66602016-07-15 10:39:58 -0700597 cat << EOF >> "$ANDROIDMK"
598LOCAL_PATH := \$(call my-dir)
599
600EOF
601 if [ "$COMMON" -ne 1 ]; then
602 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500603ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700604
605EOF
606 else
607 if [ -z "$1" ]; then
608 echo "Argument with devices to be added to guard must be set!"
609 exit 1
610 fi
611 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500612ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700613
614EOF
615 fi
616
617 write_header "$BOARDMK"
618 write_header "$PRODUCTMK"
619}
620
621#
622# write_footers:
623#
624# Closes the inital guard and any other finalization tasks. Must
625# be called as the final step.
626#
627function write_footers() {
628 cat << EOF >> "$ANDROIDMK"
629endif
630EOF
631}
632
633# Return success if adb is up and not in recovery
634function _adb_connected {
635 {
Jake Whatley9843b322017-01-25 21:49:16 -0500636 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700637 then
638 return 0
639 fi
640 } 2>/dev/null
641
642 return 1
643};
644
645#
646# parse_file_list:
647#
648# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400649# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700650#
651# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
652#
653function parse_file_list() {
654 if [ -z "$1" ]; then
655 echo "An input file is expected!"
656 exit 1
657 elif [ ! -f "$1" ]; then
658 echo "Input file "$1" does not exist!"
659 exit 1
660 fi
661
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400662 if [ $# -eq 2 ]; then
663 LIST=$TMPDIR/files.txt
664 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
665 else
666 LIST=$1
667 fi
668
669
Steve Kondik5bd66602016-07-15 10:39:58 -0700670 PRODUCT_PACKAGES_LIST=()
671 PRODUCT_PACKAGES_HASHES=()
672 PRODUCT_COPY_FILES_LIST=()
673 PRODUCT_COPY_FILES_HASHES=()
674
675 while read -r line; do
676 if [ -z "$line" ]; then continue; fi
677
678 # If the line has a pipe delimiter, a sha1 hash should follow.
679 # This indicates the file should be pinned and not overwritten
680 # when extracting files.
681 local SPLIT=(${line//\|/ })
682 local COUNT=${#SPLIT[@]}
683 local SPEC=${SPLIT[0]}
684 local HASH="x"
685 if [ "$COUNT" -gt "1" ]; then
686 HASH=${SPLIT[1]}
687 fi
688
689 # if line starts with a dash, it needs to be packaged
690 if [[ "$SPEC" =~ ^- ]]; then
691 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
692 PRODUCT_PACKAGES_HASHES+=("$HASH")
693 else
694 PRODUCT_COPY_FILES_LIST+=("$SPEC")
695 PRODUCT_COPY_FILES_HASHES+=("$HASH")
696 fi
697
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400698 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700699}
700
701#
702# write_makefiles:
703#
704# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400705# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700706#
707# Calls write_product_copy_files and write_product_packages on
708# the given file and appends to the Android.mk as well as
709# the product makefile.
710#
711function write_makefiles() {
712 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400713 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700714 write_product_packages
715}
716
717#
718# append_firmware_calls_to_makefiles:
719#
720# Appends to Android.mk the calls to all images present in radio folder
721# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
722#
723function append_firmware_calls_to_makefiles() {
724 cat << EOF >> "$ANDROIDMK"
725ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
726
727RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
728\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
729 \$(call add-radio-file,radio/\$(f)))
730\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
731
732endif
733
734EOF
735}
736
737#
738# get_file:
739#
740# $1: input file
741# $2: target file/folder
742# $3: source of the file (can be "adb" or a local folder)
743#
744# Silently extracts the input file to defined target
745# Returns success if file can be pulled from the device or found locally
746#
747function get_file() {
748 local SRC="$3"
749
750 if [ "$SRC" = "adb" ]; then
751 # try to pull
752 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
753
754 return 1
755 else
756 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +0300757 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
758 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700759
760 return 1
761 fi
762};
763
764#
765# oat2dex:
766#
767# $1: extracted apk|jar (to check if deodex is required)
768# $2: odexed apk|jar to deodex
769# $3: source of the odexed apk|jar
770#
771# Convert apk|jar .odex in the corresposing classes.dex
772#
773function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +0000774 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -0700775 local OEM_TARGET="$2"
776 local SRC="$3"
777 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -0500778 local OAT=
779 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -0700780
Joe Maplesfb3941c2018-01-05 14:51:33 -0500781 if [ -z "$ANDROID_HOST_OUT" ]; then
782 echo "ERROR: ANDROID_HOST_OUT not found!"
783 echo "ERROR: Please lunch a device before running this script."
784 exit 1
Steve Kondik5bd66602016-07-15 10:39:58 -0700785 fi
786
Joe Maplesfb3941c2018-01-05 14:51:33 -0500787 if [ -z "$OATDUMP" ] || [ -z "$VDEXEXTRACTOR" ]; then
788 if [ ! -f "$ANDROID_HOST_OUT/bin/oatdump" ]; then
789 echo "ERROR: oatdump utility not found!"
790 echo "ERROR: Please run 'make oatdump'"
791 echo "ERROR: from the top of the android tree before running this script."
792 exit 1
793 else
794 export OATDUMP="$ANDROID_HOST_OUT/bin/oatdump"
795 fi
theimpulson9a911af2019-08-14 03:25:12 +0000796 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/vdexExtractor
Joe Maplesfb3941c2018-01-05 14:51:33 -0500797 fi
798
codeworkx85eda752018-09-23 12:36:57 +0200799 if [ -z "$CDEXCONVERTER" ]; then
theimpulson9a911af2019-08-14 03:25:12 +0000800 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +0200801 fi
802
Steve Kondik5bd66602016-07-15 10:39:58 -0700803 # Extract existing boot.oats to the temp folder
804 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500805 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700806 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500807 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +0300808 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700809 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500810 else
811 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700812 fi
813 done
814 fi
815
816 if [ -z "$ARCHES" ]; then
817 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
818 fi
819
theimpulson9a911af2019-08-14 03:25:12 +0000820 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700821 return;
822 fi
823
theimpulson9a911af2019-08-14 03:25:12 +0000824 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700825 return 0 # target apk|jar is already odexed, return
826 fi
827
828 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500829 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700830
Joe Maplesfb3941c2018-01-05 14:51:33 -0500831 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
832 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700833
Joe Maplesfb3941c2018-01-05 14:51:33 -0500834
835 if get_file "$OAT" "$TMPDIR" "$SRC"; then
836 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
837 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
codeworkx85eda752018-09-23 12:36:57 +0200838 # Check if we have to deal with CompactDex
839 if [ -f "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" ]; then
840 "$CDEXCONVERTER" "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" &> /dev/null
841 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
842 else
TheStrix6bd412c2018-10-03 19:06:49 +0530843 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.dex" "$TMPDIR/classes.dex"
codeworkx85eda752018-09-23 12:36:57 +0200844 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500845 else
846 "$OATDUMP" --oat-file="$TMPDIR/$(basename "$OAT")" --export-dex-to="$TMPDIR" > /dev/null
847 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
848 fi
theimpulson9a911af2019-08-14 03:25:12 +0000849 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500850 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +0200851 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -0500852 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +0200853 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -0500854 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500855 # try to extract classes.dex from boot.vdex for frameworks jars
856 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +0200857 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
858 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")"
859 # Check if we have to deal with CompactDex
860 if [ -f "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" ]; then
861 "$CDEXCONVERTER" "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" &> /dev/null
862 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
863 else
864 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.dex" "$TMPDIR/classes.dex"
865 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500866 else
867 "$OATDUMP" --oat-file="$JAROAT" --export-dex-to="$TMPDIR" > /dev/null
868 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
869 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700870 else
871 continue
872 fi
873
Steve Kondik5bd66602016-07-15 10:39:58 -0700874 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700875}
876
877#
878# init_adb_connection:
879#
880# Starts adb server and waits for the device
881#
882function init_adb_connection() {
883 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
884 if ! _adb_connected; then
885 echo "No device is online. Waiting for one..."
886 echo "Please connect USB and/or enable USB debugging"
887 until _adb_connected; do
888 sleep 1
889 done
890 echo "Device Found."
891 fi
892
893 # Retrieve IP and PORT info if we're using a TCP connection
894 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
895 | head -1 | awk '{print $1}')
896 adb root &> /dev/null
897 sleep 0.3
898 if [ -n "$TCPIPPORT" ]; then
899 # adb root just killed our connection
900 # so reconnect...
901 adb connect "$TCPIPPORT"
902 fi
903 adb wait-for-device &> /dev/null
904 sleep 0.3
905}
906
907#
908# fix_xml:
909#
910# $1: xml file to fix
911#
912function fix_xml() {
913 local XML="$1"
914 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
915
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200916 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
917 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700918
919 mv "$TEMP_XML" "$XML"
920}
921
922#
923# extract:
924#
925# $1: file containing the list of items to extract
Dan Pasanen0cc05012017-03-21 09:06:11 -0500926# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400927# $3: section in list file to extract - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700928#
929function extract() {
930 if [ -z "$OUTDIR" ]; then
931 echo "Output dir not set!"
932 exit 1
933 fi
934
Harry Youd972c4112017-08-05 09:18:56 +0100935 if [ -z "$3" ]; then
936 parse_file_list "$1"
937 else
938 parse_file_list "$1" "$3"
939 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700940
941 # Allow failing, so we can try $DEST and/or $FILE
942 set +e
943
944 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
945 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
946 local COUNT=${#FILELIST[@]}
947 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +0000948 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -0700949 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
950
951 if [ "$SRC" = "adb" ]; then
952 init_adb_connection
953 fi
954
Dan Pasanen0cc05012017-03-21 09:06:11 -0500955 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
theimpulson9a911af2019-08-14 03:25:12 +0000956 DUMPDIR="$OMNI_ROOT"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -0500957
958 # Check if we're working with the same zip that was passed last time.
959 # If so, let's just use what's already extracted.
960 MD5=`md5sum "$SRC"| awk '{print $1}'`
961 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
962
963 if [ "$MD5" != "$OLDMD5" ]; then
964 rm -rf "$DUMPDIR"
965 mkdir "$DUMPDIR"
966 unzip "$SRC" -d "$DUMPDIR"
967 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
968
969 # Stop if an A/B OTA zip is detected. We cannot extract these.
970 if [ -a "$DUMPDIR"/payload.bin ]; then
971 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
972 exit 1
973 # If OTA is block based, extract it.
974 elif [ -a "$DUMPDIR"/system.new.dat ]; then
975 echo "Converting system.new.dat to system.img"
theimpulson9a911af2019-08-14 03:25:12 +0000976 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 -0500977 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
978 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
979 echo "Requesting sudo access to mount the system.img"
980 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
981 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
982 sudo umount "$DUMPDIR"/tmp
983 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
984 fi
985 fi
986
987 SRC="$DUMPDIR"
988 fi
989
Steve Kondik5bd66602016-07-15 10:39:58 -0700990 if [ "$VENDOR_STATE" -eq "0" ]; then
991 echo "Cleaning output directory ($OUTPUT_ROOT).."
992 rm -rf "${OUTPUT_TMP:?}"
993 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -0500994 if [ -d "$OUTPUT_ROOT" ]; then
995 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
996 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700997 VENDOR_STATE=1
998 fi
999
1000 echo "Extracting $COUNT files in $1 from $SRC:"
1001
1002 for (( i=1; i<COUNT+1; i++ )); do
1003
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001004 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001005 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001006 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001007 local OUTPUT_DIR=
1008 local TMP_DIR=
1009 local SRC_FILE=
1010 local DST_FILE=
Steve Kondik5bd66602016-07-15 10:39:58 -07001011
Vladimir Olteand6391332018-06-24 20:42:01 +03001012 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001013 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1014 TMP_DIR="${OUTPUT_TMP}/rootfs"
1015 SRC_FILE="/${SPEC_SRC_FILE}"
1016 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001017 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001018 OUTPUT_DIR="${OUTPUT_ROOT}"
1019 TMP_DIR="${OUTPUT_TMP}"
1020 SRC_FILE="/system/${SPEC_SRC_FILE}"
1021 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001022 fi
1023
1024 if [ "$SRC" = "adb" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001025 printf ' - %s .. ' "${DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001026 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001027 printf ' - %s \n' "${DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001028 fi
1029
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001030 # Strip the file path in the vendor repo of "system", if present
1031 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
1032 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001033
Gabriele M58270a32017-11-13 23:15:29 +01001034 # Check pinned files
1035 local HASH="${HASHLIST[$i-1]}"
1036 local KEEP=""
1037 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001038 if [ -f "${VENDOR_REPO_FILE}" ]; then
1039 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001040 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001041 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001042 fi
1043 if [ -f "$PINNED" ]; then
1044 if [ "$(uname)" == "Darwin" ]; then
1045 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1046 else
1047 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1048 fi
1049 if [ "$TMP_HASH" = "$HASH" ]; then
1050 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001051 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1052 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001053 fi
1054 fi
1055 fi
1056 fi
1057
1058 if [ "$KEEP" = "1" ]; then
1059 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001060 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001061 FOUND=false
1062 # Try Lineage target first.
1063 # Also try to search for files stripped of
1064 # the "/system" prefix, if we're actually extracting
1065 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001066 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001067 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1068 FOUND=true
1069 break
1070 }
1071 done
1072
1073 if [ "${FOUND}" = false ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001074 printf ' !! file not found in source\n'
1075 fi
1076 fi
1077
1078 if [ "$?" == "0" ]; then
1079 # Deodex apk|jar if that's the case
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001080 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001081 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Steve Kondik5bd66602016-07-15 10:39:58 -07001082 if [ -f "$TMPDIR/classes.dex" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001083 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes.dex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001084 rm "$TMPDIR/classes.dex"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001085 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001086 fi
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001087 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1088 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001089 fi
1090 fi
1091
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001092 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001093 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001094 local TYPE="${DIR##*/}"
1095 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001096 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001097 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001098 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001099 fi
1100 fi
1101
1102 done
1103
1104 # Don't allow failing
1105 set -e
1106}
1107
1108#
1109# extract_firmware:
1110#
1111# $1: file containing the list of items to extract
1112# $2: path to extracted radio folder
1113#
1114function extract_firmware() {
1115 if [ -z "$OUTDIR" ]; then
1116 echo "Output dir not set!"
1117 exit 1
1118 fi
1119
1120 parse_file_list "$1"
1121
1122 # Don't allow failing
1123 set -e
1124
1125 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1126 local COUNT=${#FILELIST[@]}
1127 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001128 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001129
1130 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1131 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1132 rm -rf "${OUTPUT_DIR:?}/"*
1133 VENDOR_RADIO_STATE=1
1134 fi
1135
1136 echo "Extracting $COUNT files in $1 from $SRC:"
1137
1138 for (( i=1; i<COUNT+1; i++ )); do
1139 local FILE="${FILELIST[$i-1]}"
1140 printf ' - %s \n' "/radio/$FILE"
1141
1142 if [ ! -d "$OUTPUT_DIR" ]; then
1143 mkdir -p "$OUTPUT_DIR"
1144 fi
1145 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1146 chmod 644 "$OUTPUT_DIR/$FILE"
1147 done
1148}