blob: bc65079c8315422bac246f2611499ed9fb2dcdd6 [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
47# $3: CM root directory
48# $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
68 export CM_ROOT="$3"
69 if [ ! -d "$CM_ROOT" ]; then
70 echo "\$CM_ROOT must be set and valid before including this script!"
71 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
75 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
76 mkdir -p "$CM_ROOT/$OUTDIR"
77 fi
78
Jake Whatley9843b322017-01-25 21:49:16 -050079 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
Jake Whatleycb7cd072016-09-18 20:55:12 +020084 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/device-vendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070085 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
86 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
87
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
103#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300104# input: spec in the form of "src[:dst][;args]"
105# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700106#
107function target_file() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300108 local SPEC="$1"
109 local SPLIT=(${SPEC//:/ })
110 local ARGS="$(target_args ${SPEC})"
111 local DST=
112 case ${#SPLIT[@]} in
113 1)
114 # The spec doesn't have a : delimiter
115 DST="${SPLIT[0]}"
116 ;;
117 *)
118 # The spec actually has a src:dst format
119 DST="${SPLIT[1]}"
120 ;;
121 esac
122 # Remove target_args suffix, if present
123 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700124}
125
126#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300127# input: spec in the form of "src[:dst][;args]"
128# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700129#
130function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300131 local SPEC="$1"
132 local SPLIT=(${SPEC//;/ })
133 local ARGS=
134 case ${#SPLIT[@]} in
135 1)
136 # No ";" delimiter in the spec.
137 ;;
138 *)
139 # The "args" are whatever comes after the ";" character.
140 # Basically the spec stripped of whatever is to the left of ";".
141 ARGS="${SPEC#${SPLIT[0]};}"
142 ;;
143 esac
144 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700145}
146
147#
148# prefix_match:
149#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300150# input:
151# - $1: prefix
152# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
153# output:
154# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700155#
156function prefix_match() {
157 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300158 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
159 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700160 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300161 local ARGS=$(target_args "$LINE")
162 if [ -z "${ARGS}" ]; then
163 echo "${FILE#$PREFIX}"
164 else
165 echo "${FILE#$PREFIX};${ARGS}"
166 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700167 fi
168 done
169}
170
171#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400172# prefix_match_file:
173#
174# $1: the prefix to match on
175# $2: the file to match the prefix for
176#
177# Internal function which returns true if a filename contains the
178# specified prefix.
179#
180function prefix_match_file() {
181 local PREFIX="$1"
182 local FILE="$2"
183 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
184 return 0
185 else
186 return 1
187 fi
188}
189
190#
191# truncate_file
192#
193# $1: the filename to truncate
194# $2: the argument to output the truncated filename to
195#
196# Internal function which truncates a filename by removing the first dir
197# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
198#
199function truncate_file() {
200 local FILE="$1"
201 RETURN_FILE="$2"
202 local FIND="${FILE%%/*}"
203 local LOCATION="${#FIND}+1"
204 echo ${FILE:$LOCATION}
205}
206
207#
Steve Kondik5bd66602016-07-15 10:39:58 -0700208# write_product_copy_files:
209#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400210# $1: make treble compatible makefile - optional, default to false
211#
Steve Kondik5bd66602016-07-15 10:39:58 -0700212# Creates the PRODUCT_COPY_FILES section in the product makefile for all
213# items in the list which do not start with a dash (-).
214#
215function write_product_copy_files() {
216 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
217 local TARGET=
218 local FILE=
219 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400220 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700221
222 if [ "$COUNT" -eq "0" ]; then
223 return 0
224 fi
225
226 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
227 for (( i=1; i<COUNT+1; i++ )); do
228 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
229 LINEEND=" \\"
230 if [ "$i" -eq "$COUNT" ]; then
231 LINEEND=""
232 fi
233
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300234 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400235 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
236 if prefix_match_file "vendor/" $TARGET ; then
237 local OUTTARGET=$(truncate_file $TARGET)
238 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
239 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
240 else
241 printf ' %s/proprietary/%s:system/%s%s\n' \
242 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
243 fi
244 else
245 printf ' %s/proprietary/%s:system/%s%s\n' \
246 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
247 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700248 done
249 return 0
250}
251
252#
253# write_packages:
254#
255# $1: The LOCAL_MODULE_CLASS for the given module list
256# $2: "true" if this package is part of the vendor/ path
257# $3: type-specific extra flags
258# $4: Name of the array holding the target list
259#
260# Internal function which writes out the BUILD_PREBUILT stanzas
261# for all modules in the list. This is called by write_product_packages
262# after the modules are categorized.
263#
264function write_packages() {
265
266 local CLASS="$1"
267 local VENDOR_PKG="$2"
268 local EXTRA="$3"
269
270 # Yes, this is a horrible hack - we create a new array using indirection
271 local ARR_NAME="$4[@]"
272 local FILELIST=("${!ARR_NAME}")
273
274 local FILE=
275 local ARGS=
276 local BASENAME=
277 local EXTENSION=
278 local PKGNAME=
279 local SRC=
280
281 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300282 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700283 ARGS=$(target_args "$P")
284
285 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100286 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700287 EXTENSION=${BASENAME##*.}
288 PKGNAME=${BASENAME%.*}
289
290 # Add to final package list
291 PACKAGE_LIST+=("$PKGNAME")
292
293 SRC="proprietary"
294 if [ "$VENDOR_PKG" = "true" ]; then
295 SRC+="/vendor"
296 fi
297
298 printf 'include $(CLEAR_VARS)\n'
299 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
300 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
301 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
302 if [ "$EXTRA" = "both" ]; then
303 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
304 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
305 #if [ "$VENDOR_PKG" = "true" ]; then
306 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
307 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
308 #else
309 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
310 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
311 #fi
312 elif [ "$EXTRA" = "64" ]; then
313 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
314 else
315 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
316 fi
317 if [ "$EXTRA" != "none" ]; then
318 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
319 fi
320 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200321 if [ "$EXTRA" = "priv-app" ]; then
322 SRC="$SRC/priv-app"
323 else
324 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700325 fi
326 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
327 local CERT=platform
328 if [ ! -z "$ARGS" ]; then
329 CERT="$ARGS"
330 fi
331 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
332 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
333 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200334 local CERT=platform
335 if [ ! -z "$ARGS" ]; then
336 CERT="$ARGS"
337 fi
338 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700339 elif [ "$CLASS" = "ETC" ]; then
340 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
341 elif [ "$CLASS" = "EXECUTABLES" ]; then
342 if [ "$ARGS" = "rootfs" ]; then
343 SRC="$SRC/rootfs"
344 if [ "$EXTRA" = "sbin" ]; then
345 SRC="$SRC/sbin"
346 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
347 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
348 fi
349 else
350 SRC="$SRC/bin"
351 fi
352 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
353 unset EXTENSION
354 else
355 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
356 fi
357 printf 'LOCAL_MODULE_TAGS := optional\n'
358 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400359 if [ "$CLASS" = "APPS" ]; then
360 printf 'LOCAL_DEX_PREOPT := false\n'
361 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700362 if [ ! -z "$EXTENSION" ]; then
363 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
364 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100365 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
366 if [ "$DIRNAME" != "." ]; then
367 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
368 fi
369 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700370 if [ "$EXTRA" = "priv-app" ]; then
371 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
372 fi
373 if [ "$VENDOR_PKG" = "true" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800374 printf 'LOCAL_VENDOR_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700375 fi
376 printf 'include $(BUILD_PREBUILT)\n\n'
377 done
378}
379
380#
381# write_product_packages:
382#
383# This function will create BUILD_PREBUILT entries in the
384# Android.mk and associated PRODUCT_PACKAGES list in the
385# product makefile for all files in the blob list which
386# start with a single dash (-) character.
387#
388function write_product_packages() {
389 PACKAGE_LIST=()
390
391 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
392
393 if [ "$COUNT" = "0" ]; then
394 return 0
395 fi
396
397 # Figure out what's 32-bit, what's 64-bit, and what's multilib
398 # I really should not be doing this in bash due to shitty array passing :(
399 local T_LIB32=( $(prefix_match "lib/") )
400 local T_LIB64=( $(prefix_match "lib64/") )
401 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
402 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
403 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
404
405 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
406 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
407 fi
408 if [ "${#LIB32[@]}" -gt "0" ]; then
409 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
410 fi
411 if [ "${#LIB64[@]}" -gt "0" ]; then
412 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
413 fi
414
415 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
416 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
417 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
418 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
419 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
420
421 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
422 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
423 fi
424 if [ "${#V_LIB32[@]}" -gt "0" ]; then
425 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
426 fi
427 if [ "${#V_LIB64[@]}" -gt "0" ]; then
428 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
429 fi
430
431 # Apps
432 local APPS=( $(prefix_match "app/") )
433 if [ "${#APPS[@]}" -gt "0" ]; then
434 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
435 fi
436 local PRIV_APPS=( $(prefix_match "priv-app/") )
437 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
438 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
439 fi
440 local V_APPS=( $(prefix_match "vendor/app/") )
441 if [ "${#V_APPS[@]}" -gt "0" ]; then
442 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
443 fi
444 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
445 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
446 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
447 fi
448
449 # Framework
450 local FRAMEWORK=( $(prefix_match "framework/") )
451 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
452 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
453 fi
Christian Oder974b5902017-10-08 23:15:52 +0200454 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200455 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Christian Oder974b5902017-10-08 23:15:52 +0200456 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
457 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700458
459 # Etc
460 local ETC=( $(prefix_match "etc/") )
461 if [ "${#ETC[@]}" -gt "0" ]; then
462 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
463 fi
464 local V_ETC=( $(prefix_match "vendor/etc/") )
465 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabcc98bc32017-10-08 17:33:42 -0400466 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700467 fi
468
469 # Executables
470 local BIN=( $(prefix_match "bin/") )
471 if [ "${#BIN[@]}" -gt "0" ]; then
472 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
473 fi
474 local V_BIN=( $(prefix_match "vendor/bin/") )
475 if [ "${#V_BIN[@]}" -gt "0" ]; then
476 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
477 fi
478 local SBIN=( $(prefix_match "sbin/") )
479 if [ "${#SBIN[@]}" -gt "0" ]; then
480 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
481 fi
482
483
484 # Actually write out the final PRODUCT_PACKAGES list
485 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
486
487 if [ "$PACKAGE_COUNT" -eq "0" ]; then
488 return 0
489 fi
490
491 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
492 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
493 local LINEEND=" \\"
494 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
495 LINEEND=""
496 fi
497 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
498 done
499}
500
501#
502# write_header:
503#
504# $1: file which will be written to
505#
506# writes out the copyright header with the current year.
507# note that this is not an append operation, and should
508# be executed first!
509#
510function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500511 if [ -f $1 ]; then
512 rm $1
513 fi
514
Steve Kondik5bd66602016-07-15 10:39:58 -0700515 YEAR=$(date +"%Y")
516
517 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
518
Jake Whatley9843b322017-01-25 21:49:16 -0500519 NUM_REGEX='^[0-9]+$'
520 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
521 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
522 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
523 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
524 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
525 fi
526 if [ $YEAR -eq 2017 ]; then
527 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
528 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
529 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
530 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
531 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
532 else
533 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
534 fi
535 else
536 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
537 fi
538
539 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700540#
541# Licensed under the Apache License, Version 2.0 (the "License");
542# you may not use this file except in compliance with the License.
543# You may obtain a copy of the License at
544#
545# http://www.apache.org/licenses/LICENSE-2.0
546#
547# Unless required by applicable law or agreed to in writing, software
548# distributed under the License is distributed on an "AS IS" BASIS,
549# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
550# See the License for the specific language governing permissions and
551# limitations under the License.
552
553# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
554
555EOF
556}
557
558#
559# write_headers:
560#
561# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500562# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700563#
564# Calls write_header for each of the makefiles and creates
565# the initial path declaration and device guard for the
566# Android.mk
567#
568function write_headers() {
569 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500570
571 GUARD="$2"
572 if [ -z "$GUARD" ]; then
573 GUARD="TARGET_DEVICE"
574 fi
575
Steve Kondik5bd66602016-07-15 10:39:58 -0700576 cat << EOF >> "$ANDROIDMK"
577LOCAL_PATH := \$(call my-dir)
578
579EOF
580 if [ "$COMMON" -ne 1 ]; then
581 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500582ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700583
584EOF
585 else
586 if [ -z "$1" ]; then
587 echo "Argument with devices to be added to guard must be set!"
588 exit 1
589 fi
590 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500591ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700592
593EOF
594 fi
595
596 write_header "$BOARDMK"
597 write_header "$PRODUCTMK"
598}
599
600#
601# write_footers:
602#
603# Closes the inital guard and any other finalization tasks. Must
604# be called as the final step.
605#
606function write_footers() {
607 cat << EOF >> "$ANDROIDMK"
608endif
609EOF
610}
611
612# Return success if adb is up and not in recovery
613function _adb_connected {
614 {
Jake Whatley9843b322017-01-25 21:49:16 -0500615 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700616 then
617 return 0
618 fi
619 } 2>/dev/null
620
621 return 1
622};
623
624#
625# parse_file_list:
626#
627# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400628# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700629#
630# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
631#
632function parse_file_list() {
633 if [ -z "$1" ]; then
634 echo "An input file is expected!"
635 exit 1
636 elif [ ! -f "$1" ]; then
637 echo "Input file "$1" does not exist!"
638 exit 1
639 fi
640
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400641 if [ $# -eq 2 ]; then
642 LIST=$TMPDIR/files.txt
643 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
644 else
645 LIST=$1
646 fi
647
648
Steve Kondik5bd66602016-07-15 10:39:58 -0700649 PRODUCT_PACKAGES_LIST=()
650 PRODUCT_PACKAGES_HASHES=()
651 PRODUCT_COPY_FILES_LIST=()
652 PRODUCT_COPY_FILES_HASHES=()
653
654 while read -r line; do
655 if [ -z "$line" ]; then continue; fi
656
657 # If the line has a pipe delimiter, a sha1 hash should follow.
658 # This indicates the file should be pinned and not overwritten
659 # when extracting files.
660 local SPLIT=(${line//\|/ })
661 local COUNT=${#SPLIT[@]}
662 local SPEC=${SPLIT[0]}
663 local HASH="x"
664 if [ "$COUNT" -gt "1" ]; then
665 HASH=${SPLIT[1]}
666 fi
667
668 # if line starts with a dash, it needs to be packaged
669 if [[ "$SPEC" =~ ^- ]]; then
670 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
671 PRODUCT_PACKAGES_HASHES+=("$HASH")
672 else
673 PRODUCT_COPY_FILES_LIST+=("$SPEC")
674 PRODUCT_COPY_FILES_HASHES+=("$HASH")
675 fi
676
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400677 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700678}
679
680#
681# write_makefiles:
682#
683# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400684# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700685#
686# Calls write_product_copy_files and write_product_packages on
687# the given file and appends to the Android.mk as well as
688# the product makefile.
689#
690function write_makefiles() {
691 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400692 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700693 write_product_packages
694}
695
696#
697# append_firmware_calls_to_makefiles:
698#
699# Appends to Android.mk the calls to all images present in radio folder
700# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
701#
702function append_firmware_calls_to_makefiles() {
703 cat << EOF >> "$ANDROIDMK"
704ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
705
706RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
707\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
708 \$(call add-radio-file,radio/\$(f)))
709\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
710
711endif
712
713EOF
714}
715
716#
717# get_file:
718#
719# $1: input file
720# $2: target file/folder
721# $3: source of the file (can be "adb" or a local folder)
722#
723# Silently extracts the input file to defined target
724# Returns success if file can be pulled from the device or found locally
725#
726function get_file() {
727 local SRC="$3"
728
729 if [ "$SRC" = "adb" ]; then
730 # try to pull
731 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
732
733 return 1
734 else
735 # try to copy
Jake Whatley9843b322017-01-25 21:49:16 -0500736 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700737
738 return 1
739 fi
740};
741
742#
743# oat2dex:
744#
745# $1: extracted apk|jar (to check if deodex is required)
746# $2: odexed apk|jar to deodex
747# $3: source of the odexed apk|jar
748#
749# Convert apk|jar .odex in the corresposing classes.dex
750#
751function oat2dex() {
752 local CM_TARGET="$1"
753 local OEM_TARGET="$2"
754 local SRC="$3"
755 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -0500756 local OAT=
757 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -0700758
Joe Maplesfb3941c2018-01-05 14:51:33 -0500759 if [ -z "$ANDROID_HOST_OUT" ]; then
760 echo "ERROR: ANDROID_HOST_OUT not found!"
761 echo "ERROR: Please lunch a device before running this script."
762 exit 1
Steve Kondik5bd66602016-07-15 10:39:58 -0700763 fi
764
Joe Maplesfb3941c2018-01-05 14:51:33 -0500765 if [ -z "$OATDUMP" ] || [ -z "$VDEXEXTRACTOR" ]; then
766 if [ ! -f "$ANDROID_HOST_OUT/bin/oatdump" ]; then
767 echo "ERROR: oatdump utility not found!"
768 echo "ERROR: Please run 'make oatdump'"
769 echo "ERROR: from the top of the android tree before running this script."
770 exit 1
771 else
772 export OATDUMP="$ANDROID_HOST_OUT/bin/oatdump"
773 fi
774 export VDEXEXTRACTOR="$CM_ROOT"/vendor/omni/build/tools/"$HOST"/vdexExtractor
775 fi
776
Steve Kondik5bd66602016-07-15 10:39:58 -0700777 # Extract existing boot.oats to the temp folder
778 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500779 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700780 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500781 mkdir -p "$TMPDIR/system/framework/$ARCH"
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -0400782 if [ -d "$SRC/framework" ] && [ "$SRC" != "adb" ]; then
783 ARCHDIR="framework/$ARCH/"
784 else
785 ARCHDIR="system/framework/$ARCH/"
786 fi
787 if get_file "$ARCHDIR" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700788 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500789 else
790 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700791 fi
792 done
793 fi
794
795 if [ -z "$ARCHES" ]; then
796 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
797 fi
798
799 if [ ! -f "$CM_TARGET" ]; then
800 return;
801 fi
802
803 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
804 return 0 # target apk|jar is already odexed, return
805 fi
806
807 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500808 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700809
Joe Maplesfb3941c2018-01-05 14:51:33 -0500810 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
811 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700812
Joe Maplesfb3941c2018-01-05 14:51:33 -0500813
814 if get_file "$OAT" "$TMPDIR" "$SRC"; then
815 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
816 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
817 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}").apk_classes.dex" "$TMPDIR/classes.dex"
818 else
819 "$OATDUMP" --oat-file="$TMPDIR/$(basename "$OAT")" --export-dex-to="$TMPDIR" > /dev/null
820 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
821 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700822 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500823 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Joe Maplesfb3941c2018-01-05 14:51:33 -0500824 JARVDEX="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -0500825 if [ ! -f "$JAROAT" ]; then
826 JAROAT=$BOOTOAT;
827 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500828
829 # try to extract classes.dex from boot.vdex for frameworks jars
830 # fallback to boot.oat if vdex is not available
831 if [ -f "$JARVDEX" ]; then
832 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$JARVDEX" > /dev/null
833 mv "$TMPDIR/boot-$(basename "${OEM_TARGET%.*}").apk_classes.dex" "$TMPDIR/classes.dex"
834 else
835 "$OATDUMP" --oat-file="$JAROAT" --export-dex-to="$TMPDIR" > /dev/null
836 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
837 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700838 else
839 continue
840 fi
841
Steve Kondik5bd66602016-07-15 10:39:58 -0700842 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700843}
844
845#
846# init_adb_connection:
847#
848# Starts adb server and waits for the device
849#
850function init_adb_connection() {
851 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
852 if ! _adb_connected; then
853 echo "No device is online. Waiting for one..."
854 echo "Please connect USB and/or enable USB debugging"
855 until _adb_connected; do
856 sleep 1
857 done
858 echo "Device Found."
859 fi
860
861 # Retrieve IP and PORT info if we're using a TCP connection
862 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
863 | head -1 | awk '{print $1}')
864 adb root &> /dev/null
865 sleep 0.3
866 if [ -n "$TCPIPPORT" ]; then
867 # adb root just killed our connection
868 # so reconnect...
869 adb connect "$TCPIPPORT"
870 fi
871 adb wait-for-device &> /dev/null
872 sleep 0.3
873}
874
875#
876# fix_xml:
877#
878# $1: xml file to fix
879#
880function fix_xml() {
881 local XML="$1"
882 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
883
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200884 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
885 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700886
887 mv "$TEMP_XML" "$XML"
888}
889
890#
891# extract:
892#
893# $1: file containing the list of items to extract
Dan Pasanen0cc05012017-03-21 09:06:11 -0500894# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400895# $3: section in list file to extract - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700896#
897function extract() {
898 if [ -z "$OUTDIR" ]; then
899 echo "Output dir not set!"
900 exit 1
901 fi
902
Harry Youd972c4112017-08-05 09:18:56 +0100903 if [ -z "$3" ]; then
904 parse_file_list "$1"
905 else
906 parse_file_list "$1" "$3"
907 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700908
909 # Allow failing, so we can try $DEST and/or $FILE
910 set +e
911
912 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
913 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
914 local COUNT=${#FILELIST[@]}
915 local SRC="$2"
916 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
917 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
918
919 if [ "$SRC" = "adb" ]; then
920 init_adb_connection
921 fi
922
Dan Pasanen0cc05012017-03-21 09:06:11 -0500923 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
924 DUMPDIR="$CM_ROOT"/system_dump
925
926 # Check if we're working with the same zip that was passed last time.
927 # If so, let's just use what's already extracted.
928 MD5=`md5sum "$SRC"| awk '{print $1}'`
929 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
930
931 if [ "$MD5" != "$OLDMD5" ]; then
932 rm -rf "$DUMPDIR"
933 mkdir "$DUMPDIR"
934 unzip "$SRC" -d "$DUMPDIR"
935 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
936
937 # Stop if an A/B OTA zip is detected. We cannot extract these.
938 if [ -a "$DUMPDIR"/payload.bin ]; then
939 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
940 exit 1
941 # If OTA is block based, extract it.
942 elif [ -a "$DUMPDIR"/system.new.dat ]; then
943 echo "Converting system.new.dat to system.img"
944 python "$CM_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
945 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
946 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
947 echo "Requesting sudo access to mount the system.img"
948 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
949 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
950 sudo umount "$DUMPDIR"/tmp
951 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
952 fi
953 fi
954
955 SRC="$DUMPDIR"
956 fi
957
Steve Kondik5bd66602016-07-15 10:39:58 -0700958 if [ "$VENDOR_STATE" -eq "0" ]; then
959 echo "Cleaning output directory ($OUTPUT_ROOT).."
960 rm -rf "${OUTPUT_TMP:?}"
961 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -0500962 if [ -d "$OUTPUT_ROOT" ]; then
963 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
964 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700965 VENDOR_STATE=1
966 fi
967
968 echo "Extracting $COUNT files in $1 from $SRC:"
969
970 for (( i=1; i<COUNT+1; i++ )); do
971
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300972 local FROM=$(target_file "${FILELIST[$i-1]}")
Steve Kondik5bd66602016-07-15 10:39:58 -0700973 local ARGS=$(target_args "${FILELIST[$i-1]}")
974 local SPLIT=(${FILELIST[$i-1]//:/ })
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200975 local FILE=$(echo "${SPLIT[0]#-}" | sed 's/\;.*//')
Steve Kondik5bd66602016-07-15 10:39:58 -0700976 local OUTPUT_DIR="$OUTPUT_ROOT"
977 local TMP_DIR="$OUTPUT_TMP"
978 local TARGET=
979
980 if [ "$ARGS" = "rootfs" ]; then
981 TARGET="$FROM"
982 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
983 TMP_DIR="$TMP_DIR/rootfs"
984 else
985 TARGET="system/$FROM"
986 FILE="system/$FILE"
987 fi
988
989 if [ "$SRC" = "adb" ]; then
990 printf ' - %s .. ' "/$TARGET"
991 else
992 printf ' - %s \n' "/$TARGET"
993 fi
994
995 local DIR=$(dirname "$FROM")
996 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
997 mkdir -p "$OUTPUT_DIR/$DIR"
998 fi
999 local DEST="$OUTPUT_DIR/$FROM"
1000
Gabriele M58270a32017-11-13 23:15:29 +01001001 # Check pinned files
1002 local HASH="${HASHLIST[$i-1]}"
1003 local KEEP=""
1004 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
1005 if [ -f "$DEST" ]; then
1006 local PINNED="$DEST"
1007 else
1008 local PINNED="$TMP_DIR/$FROM"
1009 fi
1010 if [ -f "$PINNED" ]; then
1011 if [ "$(uname)" == "Darwin" ]; then
1012 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1013 else
1014 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1015 fi
1016 if [ "$TMP_HASH" = "$HASH" ]; then
1017 KEEP="1"
1018 if [ ! -f "$DEST" ]; then
1019 cp -p "$PINNED" "$DEST"
1020 fi
1021 fi
1022 fi
1023 fi
1024
1025 if [ "$KEEP" = "1" ]; then
1026 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
1027 elif [ "$SRC" = "adb" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001028 # Try CM target first
1029 adb pull "/$TARGET" "$DEST"
1030 # if file does not exist try OEM target
1031 if [ "$?" != "0" ]; then
1032 adb pull "/$FILE" "$DEST"
1033 fi
1034 else
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -05001035 # Try CM target first
1036 if [ -f "$SRC/$TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001037 cp "$SRC/$TARGET" "$DEST"
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -05001038 # if file does not exist try OEM target
1039 elif [ -f "$SRC/$FILE" ]; then
1040 cp "$SRC/$FILE" "$DEST"
Steve Kondik5bd66602016-07-15 10:39:58 -07001041 else
1042 printf ' !! file not found in source\n'
1043 fi
1044 fi
1045
1046 if [ "$?" == "0" ]; then
1047 # Deodex apk|jar if that's the case
1048 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
1049 oat2dex "$DEST" "$FILE" "$SRC"
1050 if [ -f "$TMPDIR/classes.dex" ]; then
1051 zip -gjq "$DEST" "$TMPDIR/classes.dex"
1052 rm "$TMPDIR/classes.dex"
1053 printf ' (updated %s from odex files)\n' "/$FILE"
1054 fi
1055 elif [[ "$DEST" =~ .xml$ ]]; then
1056 fix_xml "$DEST"
1057 fi
1058 fi
1059
1060 # Check pinned files
1061 local HASH="${HASHLIST[$i-1]}"
Jake Whatley9843b322017-01-25 21:49:16 -05001062 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001063 local KEEP=""
1064 local TMP="$TMP_DIR/$FROM"
1065 if [ -f "$TMP" ]; then
1066 if [ ! -f "$DEST" ]; then
1067 KEEP="1"
1068 else
Jake Whatley9843b322017-01-25 21:49:16 -05001069 if [ "$(uname)" == "Darwin" ]; then
1070 local DEST_HASH=$(shasum "$DEST" | awk '{print $1}' )
1071 else
1072 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
1073 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001074 if [ "$DEST_HASH" != "$HASH" ]; then
1075 KEEP="1"
1076 fi
1077 fi
1078 if [ "$KEEP" = "1" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001079 if [ "$(uname)" == "Darwin" ]; then
1080 local TMP_HASH=$(shasum "$TMP" | awk '{print $1}' )
1081 else
1082 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
1083 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001084 if [ "$TMP_HASH" = "$HASH" ]; then
1085 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
1086 cp -p "$TMP" "$DEST"
1087 fi
1088 fi
1089 fi
1090 fi
1091
1092 if [ -f "$DEST" ]; then
1093 local TYPE="${DIR##*/}"
1094 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1095 chmod 755 "$DEST"
1096 else
1097 chmod 644 "$DEST"
1098 fi
1099 fi
1100
1101 done
1102
1103 # Don't allow failing
1104 set -e
1105}
1106
1107#
1108# extract_firmware:
1109#
1110# $1: file containing the list of items to extract
1111# $2: path to extracted radio folder
1112#
1113function extract_firmware() {
1114 if [ -z "$OUTDIR" ]; then
1115 echo "Output dir not set!"
1116 exit 1
1117 fi
1118
1119 parse_file_list "$1"
1120
1121 # Don't allow failing
1122 set -e
1123
1124 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1125 local COUNT=${#FILELIST[@]}
1126 local SRC="$2"
1127 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
1128
1129 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1130 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1131 rm -rf "${OUTPUT_DIR:?}/"*
1132 VENDOR_RADIO_STATE=1
1133 fi
1134
1135 echo "Extracting $COUNT files in $1 from $SRC:"
1136
1137 for (( i=1; i<COUNT+1; i++ )); do
1138 local FILE="${FILELIST[$i-1]}"
1139 printf ' - %s \n' "/radio/$FILE"
1140
1141 if [ ! -d "$OUTPUT_DIR" ]; then
1142 mkdir -p "$OUTPUT_DIR"
1143 fi
1144 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1145 chmod 644 "$OUTPUT_DIR/$FILE"
1146 done
1147}