blob: e7eff829da481597d76796fc45dba9adf5243a09 [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
29TMPDIR="/tmp/extractfiles.$$"
30mkdir "$TMPDIR"
31
32#
33# cleanup
34#
35# kill our tmpfiles with fire on exit
36#
37function cleanup() {
38 rm -rf "${TMPDIR:?}"
39}
40
41trap cleanup EXIT INT TERM ERR
42
43#
44# setup_vendor
45#
46# $1: device name
47# $2: vendor name
48# $3: CM root directory
49# $4: is common device - optional, default to false
50# $5: cleanup - optional, default to true
51#
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 Whatleycb7cd072016-09-18 20:55:12 +020079 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/device-vendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070080 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
81 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
82
83 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
84 COMMON=1
85 else
86 COMMON=0
87 fi
88
89 if [ "$5" == "true" ] || [ "$5" == "1" ]; then
90 VENDOR_STATE=1
91 VENDOR_RADIO_STATE=1
92 else
93 VENDOR_STATE=0
94 VENDOR_RADIO_STATE=0
95 fi
96}
97
98#
99# target_file:
100#
101# $1: colon delimited list
102#
103# Returns destination filename without args
104#
105function target_file() {
106 local LINE="$1"
107 local SPLIT=(${LINE//:/ })
108 local COUNT=${#SPLIT[@]}
109 if [ "$COUNT" -gt "1" ]; then
110 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
111 printf '%s\n' "${SPLIT[1]}"
112 return 0
113 fi
114 fi
115 printf '%s\n' "${SPLIT[0]}"
116}
117
118#
119# target_args:
120#
121# $1: colon delimited list
122#
123# Returns optional arguments (last value) for given target
124#
125function target_args() {
126 local LINE="$1"
127 local SPLIT=(${LINE//:/ })
128 local COUNT=${#SPLIT[@]}
129 if [ "$COUNT" -gt "1" ]; then
130 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
131 printf '%s\n' "${SPLIT[$COUNT-1]}"
132 fi
133 fi
134}
135
136#
137# prefix_match:
138#
139# $1: the prefix to match on
140#
141# Internal function which loops thru the packages list and returns a new
142# list containing the matched files with the prefix stripped away.
143#
144function prefix_match() {
145 local PREFIX="$1"
146 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
147 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
148 printf '%s\n' "${FILE#$PREFIX}"
149 fi
150 done
151}
152
153#
154# write_product_copy_files:
155#
156# Creates the PRODUCT_COPY_FILES section in the product makefile for all
157# items in the list which do not start with a dash (-).
158#
159function write_product_copy_files() {
160 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
161 local TARGET=
162 local FILE=
163 local LINEEND=
164
165 if [ "$COUNT" -eq "0" ]; then
166 return 0
167 fi
168
169 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
170 for (( i=1; i<COUNT+1; i++ )); do
171 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
172 LINEEND=" \\"
173 if [ "$i" -eq "$COUNT" ]; then
174 LINEEND=""
175 fi
176
177 TARGET=$(target_file "$FILE")
178 printf ' %s/proprietary/%s:system/%s%s\n' \
179 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
180 done
181 return 0
182}
183
184#
185# write_packages:
186#
187# $1: The LOCAL_MODULE_CLASS for the given module list
188# $2: "true" if this package is part of the vendor/ path
189# $3: type-specific extra flags
190# $4: Name of the array holding the target list
191#
192# Internal function which writes out the BUILD_PREBUILT stanzas
193# for all modules in the list. This is called by write_product_packages
194# after the modules are categorized.
195#
196function write_packages() {
197
198 local CLASS="$1"
199 local VENDOR_PKG="$2"
200 local EXTRA="$3"
201
202 # Yes, this is a horrible hack - we create a new array using indirection
203 local ARR_NAME="$4[@]"
204 local FILELIST=("${!ARR_NAME}")
205
206 local FILE=
207 local ARGS=
208 local BASENAME=
209 local EXTENSION=
210 local PKGNAME=
211 local SRC=
212
213 for P in "${FILELIST[@]}"; do
214 FILE=$(target_file "$P")
215 ARGS=$(target_args "$P")
216
217 BASENAME=$(basename "$FILE")
218 EXTENSION=${BASENAME##*.}
219 PKGNAME=${BASENAME%.*}
220
221 # Add to final package list
222 PACKAGE_LIST+=("$PKGNAME")
223
224 SRC="proprietary"
225 if [ "$VENDOR_PKG" = "true" ]; then
226 SRC+="/vendor"
227 fi
228
229 printf 'include $(CLEAR_VARS)\n'
230 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
231 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
232 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
233 if [ "$EXTRA" = "both" ]; then
234 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
235 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
236 #if [ "$VENDOR_PKG" = "true" ]; then
237 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
238 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
239 #else
240 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
241 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
242 #fi
243 elif [ "$EXTRA" = "64" ]; then
244 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
245 else
246 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
247 fi
248 if [ "$EXTRA" != "none" ]; then
249 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
250 fi
251 elif [ "$CLASS" = "APPS" ]; then
252 if [ "$EXTRA" = "priv-app" ]; then
253 SRC="$SRC/priv-app"
254 else
255 SRC="$SRC/app"
256 fi
257 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
258 local CERT=platform
259 if [ ! -z "$ARGS" ]; then
260 CERT="$ARGS"
261 fi
262 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
263 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
264 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200265 local CERT=platform
266 if [ ! -z "$ARGS" ]; then
267 CERT="$ARGS"
268 fi
269 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700270 elif [ "$CLASS" = "ETC" ]; then
271 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
272 elif [ "$CLASS" = "EXECUTABLES" ]; then
273 if [ "$ARGS" = "rootfs" ]; then
274 SRC="$SRC/rootfs"
275 if [ "$EXTRA" = "sbin" ]; then
276 SRC="$SRC/sbin"
277 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
278 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
279 fi
280 else
281 SRC="$SRC/bin"
282 fi
283 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
284 unset EXTENSION
285 else
286 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
287 fi
288 printf 'LOCAL_MODULE_TAGS := optional\n'
289 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400290 if [ "$CLASS" = "APPS" ]; then
291 printf 'LOCAL_DEX_PREOPT := false\n'
292 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700293 if [ ! -z "$EXTENSION" ]; then
294 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
295 fi
296 if [ "$EXTRA" = "priv-app" ]; then
297 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
298 fi
299 if [ "$VENDOR_PKG" = "true" ]; then
300 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
301 fi
302 printf 'include $(BUILD_PREBUILT)\n\n'
303 done
304}
305
306#
307# write_product_packages:
308#
309# This function will create BUILD_PREBUILT entries in the
310# Android.mk and associated PRODUCT_PACKAGES list in the
311# product makefile for all files in the blob list which
312# start with a single dash (-) character.
313#
314function write_product_packages() {
315 PACKAGE_LIST=()
316
317 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
318
319 if [ "$COUNT" = "0" ]; then
320 return 0
321 fi
322
323 # Figure out what's 32-bit, what's 64-bit, and what's multilib
324 # I really should not be doing this in bash due to shitty array passing :(
325 local T_LIB32=( $(prefix_match "lib/") )
326 local T_LIB64=( $(prefix_match "lib64/") )
327 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
328 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
329 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
330
331 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
332 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
333 fi
334 if [ "${#LIB32[@]}" -gt "0" ]; then
335 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
336 fi
337 if [ "${#LIB64[@]}" -gt "0" ]; then
338 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
339 fi
340
341 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
342 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
343 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
344 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
345 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
346
347 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
348 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
349 fi
350 if [ "${#V_LIB32[@]}" -gt "0" ]; then
351 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
352 fi
353 if [ "${#V_LIB64[@]}" -gt "0" ]; then
354 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
355 fi
356
357 # Apps
358 local APPS=( $(prefix_match "app/") )
359 if [ "${#APPS[@]}" -gt "0" ]; then
360 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
361 fi
362 local PRIV_APPS=( $(prefix_match "priv-app/") )
363 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
364 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
365 fi
366 local V_APPS=( $(prefix_match "vendor/app/") )
367 if [ "${#V_APPS[@]}" -gt "0" ]; then
368 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
369 fi
370 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
371 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
372 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
373 fi
374
375 # Framework
376 local FRAMEWORK=( $(prefix_match "framework/") )
377 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
378 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
379 fi
380
381 # Etc
382 local ETC=( $(prefix_match "etc/") )
383 if [ "${#ETC[@]}" -gt "0" ]; then
384 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
385 fi
386 local V_ETC=( $(prefix_match "vendor/etc/") )
387 if [ "${#V_ETC[@]}" -gt "0" ]; then
388 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
389 fi
390
391 # Executables
392 local BIN=( $(prefix_match "bin/") )
393 if [ "${#BIN[@]}" -gt "0" ]; then
394 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
395 fi
396 local V_BIN=( $(prefix_match "vendor/bin/") )
397 if [ "${#V_BIN[@]}" -gt "0" ]; then
398 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
399 fi
400 local SBIN=( $(prefix_match "sbin/") )
401 if [ "${#SBIN[@]}" -gt "0" ]; then
402 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
403 fi
404
405
406 # Actually write out the final PRODUCT_PACKAGES list
407 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
408
409 if [ "$PACKAGE_COUNT" -eq "0" ]; then
410 return 0
411 fi
412
413 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
414 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
415 local LINEEND=" \\"
416 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
417 LINEEND=""
418 fi
419 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
420 done
421}
422
423#
424# write_header:
425#
426# $1: file which will be written to
427#
428# writes out the copyright header with the current year.
429# note that this is not an append operation, and should
430# be executed first!
431#
432function write_header() {
433 YEAR=$(date +"%Y")
434
435 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
436
437 cat << EOF > $1
438# Copyright (C) $YEAR The CyanogenMod Project
439#
440# Licensed under the Apache License, Version 2.0 (the "License");
441# you may not use this file except in compliance with the License.
442# You may obtain a copy of the License at
443#
444# http://www.apache.org/licenses/LICENSE-2.0
445#
446# Unless required by applicable law or agreed to in writing, software
447# distributed under the License is distributed on an "AS IS" BASIS,
448# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
449# See the License for the specific language governing permissions and
450# limitations under the License.
451
452# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
453
454EOF
455}
456
457#
458# write_headers:
459#
460# $1: devices falling under common to be added to guard - optional
461#
462# Calls write_header for each of the makefiles and creates
463# the initial path declaration and device guard for the
464# Android.mk
465#
466function write_headers() {
467 write_header "$ANDROIDMK"
468 cat << EOF >> "$ANDROIDMK"
469LOCAL_PATH := \$(call my-dir)
470
471EOF
472 if [ "$COMMON" -ne 1 ]; then
473 cat << EOF >> "$ANDROIDMK"
474ifeq (\$(TARGET_DEVICE),$DEVICE)
475
476EOF
477 else
478 if [ -z "$1" ]; then
479 echo "Argument with devices to be added to guard must be set!"
480 exit 1
481 fi
482 cat << EOF >> "$ANDROIDMK"
483ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
484
485EOF
486 fi
487
488 write_header "$BOARDMK"
489 write_header "$PRODUCTMK"
490}
491
492#
493# write_footers:
494#
495# Closes the inital guard and any other finalization tasks. Must
496# be called as the final step.
497#
498function write_footers() {
499 cat << EOF >> "$ANDROIDMK"
500endif
501EOF
502}
503
504# Return success if adb is up and not in recovery
505function _adb_connected {
506 {
507 if [[ "$(adb get-state)" == device &&
508 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
509 then
510 return 0
511 fi
512 } 2>/dev/null
513
514 return 1
515};
516
517#
518# parse_file_list:
519#
520# $1: input file
521#
522# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
523#
524function parse_file_list() {
525 if [ -z "$1" ]; then
526 echo "An input file is expected!"
527 exit 1
528 elif [ ! -f "$1" ]; then
529 echo "Input file "$1" does not exist!"
530 exit 1
531 fi
532
533 PRODUCT_PACKAGES_LIST=()
534 PRODUCT_PACKAGES_HASHES=()
535 PRODUCT_COPY_FILES_LIST=()
536 PRODUCT_COPY_FILES_HASHES=()
537
538 while read -r line; do
539 if [ -z "$line" ]; then continue; fi
540
541 # If the line has a pipe delimiter, a sha1 hash should follow.
542 # This indicates the file should be pinned and not overwritten
543 # when extracting files.
544 local SPLIT=(${line//\|/ })
545 local COUNT=${#SPLIT[@]}
546 local SPEC=${SPLIT[0]}
547 local HASH="x"
548 if [ "$COUNT" -gt "1" ]; then
549 HASH=${SPLIT[1]}
550 fi
551
552 # if line starts with a dash, it needs to be packaged
553 if [[ "$SPEC" =~ ^- ]]; then
554 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
555 PRODUCT_PACKAGES_HASHES+=("$HASH")
556 else
557 PRODUCT_COPY_FILES_LIST+=("$SPEC")
558 PRODUCT_COPY_FILES_HASHES+=("$HASH")
559 fi
560
561 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
562}
563
564#
565# write_makefiles:
566#
567# $1: file containing the list of items to extract
568#
569# Calls write_product_copy_files and write_product_packages on
570# the given file and appends to the Android.mk as well as
571# the product makefile.
572#
573function write_makefiles() {
574 parse_file_list "$1"
575 write_product_copy_files
576 write_product_packages
577}
578
579#
580# append_firmware_calls_to_makefiles:
581#
582# Appends to Android.mk the calls to all images present in radio folder
583# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
584#
585function append_firmware_calls_to_makefiles() {
586 cat << EOF >> "$ANDROIDMK"
587ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
588
589RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
590\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
591 \$(call add-radio-file,radio/\$(f)))
592\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
593
594endif
595
596EOF
597}
598
599#
600# get_file:
601#
602# $1: input file
603# $2: target file/folder
604# $3: source of the file (can be "adb" or a local folder)
605#
606# Silently extracts the input file to defined target
607# Returns success if file can be pulled from the device or found locally
608#
609function get_file() {
610 local SRC="$3"
611
612 if [ "$SRC" = "adb" ]; then
613 # try to pull
614 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
615
616 return 1
617 else
618 # try to copy
619 cp "$SRC/$1" "$2" 2>/dev/null && return 0
620
621 return 1
622 fi
623};
624
625#
626# oat2dex:
627#
628# $1: extracted apk|jar (to check if deodex is required)
629# $2: odexed apk|jar to deodex
630# $3: source of the odexed apk|jar
631#
632# Convert apk|jar .odex in the corresposing classes.dex
633#
634function oat2dex() {
635 local CM_TARGET="$1"
636 local OEM_TARGET="$2"
637 local SRC="$3"
638 local TARGET=
639 local OAT=
640
641 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Marko Man6a182a42016-09-18 01:59:20 +0200642 export BAKSMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
643 export SMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -0700644 fi
645
646 # Extract existing boot.oats to the temp folder
647 if [ -z "$ARCHES" ]; then
648 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
649 for ARCH in "arm64" "arm" "x86_64" "x86"; do
650 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
651 ARCHES+="$ARCH "
652 fi
653 done
654 fi
655
656 if [ -z "$ARCHES" ]; then
657 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
658 fi
659
660 if [ ! -f "$CM_TARGET" ]; then
661 return;
662 fi
663
664 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
665 return 0 # target apk|jar is already odexed, return
666 fi
667
668 for ARCH in $ARCHES; do
669 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
670
671 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
672
673 if get_file "$OAT" "$TMPDIR" "$SRC"; then
674 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
675 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
676 # try to extract classes.dex from boot.oat for framework jars
677 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
678 else
679 continue
680 fi
681
682 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
683 done
684
685 rm -rf "$TMPDIR/dexout"
686}
687
688#
689# init_adb_connection:
690#
691# Starts adb server and waits for the device
692#
693function init_adb_connection() {
694 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
695 if ! _adb_connected; then
696 echo "No device is online. Waiting for one..."
697 echo "Please connect USB and/or enable USB debugging"
698 until _adb_connected; do
699 sleep 1
700 done
701 echo "Device Found."
702 fi
703
704 # Retrieve IP and PORT info if we're using a TCP connection
705 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
706 | head -1 | awk '{print $1}')
707 adb root &> /dev/null
708 sleep 0.3
709 if [ -n "$TCPIPPORT" ]; then
710 # adb root just killed our connection
711 # so reconnect...
712 adb connect "$TCPIPPORT"
713 fi
714 adb wait-for-device &> /dev/null
715 sleep 0.3
716}
717
718#
719# fix_xml:
720#
721# $1: xml file to fix
722#
723function fix_xml() {
724 local XML="$1"
725 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
726
727 grep '^<?xml version' "$XML" > "$TEMP_XML"
728 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
729
730 mv "$TEMP_XML" "$XML"
731}
732
733#
734# extract:
735#
736# $1: file containing the list of items to extract
737# $2: path to extracted system folder, or "adb" to extract from device
738#
739function extract() {
740 if [ -z "$OUTDIR" ]; then
741 echo "Output dir not set!"
742 exit 1
743 fi
744
745 parse_file_list "$1"
746
747 # Allow failing, so we can try $DEST and/or $FILE
748 set +e
749
750 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
751 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
752 local COUNT=${#FILELIST[@]}
753 local SRC="$2"
754 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
755 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
756
757 if [ "$SRC" = "adb" ]; then
758 init_adb_connection
759 fi
760
761 if [ "$VENDOR_STATE" -eq "0" ]; then
762 echo "Cleaning output directory ($OUTPUT_ROOT).."
763 rm -rf "${OUTPUT_TMP:?}"
764 mkdir -p "${OUTPUT_TMP:?}"
765 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
766 VENDOR_STATE=1
767 fi
768
769 echo "Extracting $COUNT files in $1 from $SRC:"
770
771 for (( i=1; i<COUNT+1; i++ )); do
772
773 local FROM=$(target_file "${FILELIST[$i-1]}")
774 local ARGS=$(target_args "${FILELIST[$i-1]}")
775 local SPLIT=(${FILELIST[$i-1]//:/ })
776 local FILE="${SPLIT[0]#-}"
777 local OUTPUT_DIR="$OUTPUT_ROOT"
778 local TMP_DIR="$OUTPUT_TMP"
779 local TARGET=
780
781 if [ "$ARGS" = "rootfs" ]; then
782 TARGET="$FROM"
783 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
784 TMP_DIR="$TMP_DIR/rootfs"
785 else
786 TARGET="system/$FROM"
787 FILE="system/$FILE"
788 fi
789
790 if [ "$SRC" = "adb" ]; then
791 printf ' - %s .. ' "/$TARGET"
792 else
793 printf ' - %s \n' "/$TARGET"
794 fi
795
796 local DIR=$(dirname "$FROM")
797 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
798 mkdir -p "$OUTPUT_DIR/$DIR"
799 fi
800 local DEST="$OUTPUT_DIR/$FROM"
801
802 if [ "$SRC" = "adb" ]; then
803 # Try CM target first
804 adb pull "/$TARGET" "$DEST"
805 # if file does not exist try OEM target
806 if [ "$?" != "0" ]; then
807 adb pull "/$FILE" "$DEST"
808 fi
809 else
810 # Try OEM target first
811 if [ -f "$SRC/$FILE" ]; then
812 cp "$SRC/$FILE" "$DEST"
813 # if file does not exist try CM target
814 elif [ -f "$SRC/$TARGET" ]; then
815 cp "$SRC/$TARGET" "$DEST"
816 else
817 printf ' !! file not found in source\n'
818 fi
819 fi
820
821 if [ "$?" == "0" ]; then
822 # Deodex apk|jar if that's the case
823 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
824 oat2dex "$DEST" "$FILE" "$SRC"
825 if [ -f "$TMPDIR/classes.dex" ]; then
826 zip -gjq "$DEST" "$TMPDIR/classes.dex"
827 rm "$TMPDIR/classes.dex"
828 printf ' (updated %s from odex files)\n' "/$FILE"
829 fi
830 elif [[ "$DEST" =~ .xml$ ]]; then
831 fix_xml "$DEST"
832 fi
833 fi
834
835 # Check pinned files
836 local HASH="${HASHLIST[$i-1]}"
837 if [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
838 local KEEP=""
839 local TMP="$TMP_DIR/$FROM"
840 if [ -f "$TMP" ]; then
841 if [ ! -f "$DEST" ]; then
842 KEEP="1"
843 else
844 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
845 if [ "$DEST_HASH" != "$HASH" ]; then
846 KEEP="1"
847 fi
848 fi
849 if [ "$KEEP" = "1" ]; then
850 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
851 if [ "$TMP_HASH" = "$HASH" ]; then
852 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
853 cp -p "$TMP" "$DEST"
854 fi
855 fi
856 fi
857 fi
858
859 if [ -f "$DEST" ]; then
860 local TYPE="${DIR##*/}"
861 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
862 chmod 755 "$DEST"
863 else
864 chmod 644 "$DEST"
865 fi
866 fi
867
868 done
869
870 # Don't allow failing
871 set -e
872}
873
874#
875# extract_firmware:
876#
877# $1: file containing the list of items to extract
878# $2: path to extracted radio folder
879#
880function extract_firmware() {
881 if [ -z "$OUTDIR" ]; then
882 echo "Output dir not set!"
883 exit 1
884 fi
885
886 parse_file_list "$1"
887
888 # Don't allow failing
889 set -e
890
891 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
892 local COUNT=${#FILELIST[@]}
893 local SRC="$2"
894 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
895
896 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
897 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
898 rm -rf "${OUTPUT_DIR:?}/"*
899 VENDOR_RADIO_STATE=1
900 fi
901
902 echo "Extracting $COUNT files in $1 from $SRC:"
903
904 for (( i=1; i<COUNT+1; i++ )); do
905 local FILE="${FILELIST[$i-1]}"
906 printf ' - %s \n' "/radio/$FILE"
907
908 if [ ! -d "$OUTPUT_DIR" ]; then
909 mkdir -p "$OUTPUT_DIR"
910 fi
911 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
912 chmod 644 "$OUTPUT_DIR/$FILE"
913 done
914}