blob: fdcc4dcea4071ab2050c13ad8ca3361db5d4b79b [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"
290 if [ ! -z "$EXTENSION" ]; then
291 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
292 fi
293 if [ "$EXTRA" = "priv-app" ]; then
294 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
295 fi
296 if [ "$VENDOR_PKG" = "true" ]; then
297 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
298 fi
299 printf 'include $(BUILD_PREBUILT)\n\n'
300 done
301}
302
303#
304# write_product_packages:
305#
306# This function will create BUILD_PREBUILT entries in the
307# Android.mk and associated PRODUCT_PACKAGES list in the
308# product makefile for all files in the blob list which
309# start with a single dash (-) character.
310#
311function write_product_packages() {
312 PACKAGE_LIST=()
313
314 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
315
316 if [ "$COUNT" = "0" ]; then
317 return 0
318 fi
319
320 # Figure out what's 32-bit, what's 64-bit, and what's multilib
321 # I really should not be doing this in bash due to shitty array passing :(
322 local T_LIB32=( $(prefix_match "lib/") )
323 local T_LIB64=( $(prefix_match "lib64/") )
324 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
325 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
326 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
327
328 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
329 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
330 fi
331 if [ "${#LIB32[@]}" -gt "0" ]; then
332 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
333 fi
334 if [ "${#LIB64[@]}" -gt "0" ]; then
335 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
336 fi
337
338 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
339 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
340 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
341 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
342 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
343
344 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
345 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
346 fi
347 if [ "${#V_LIB32[@]}" -gt "0" ]; then
348 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
349 fi
350 if [ "${#V_LIB64[@]}" -gt "0" ]; then
351 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
352 fi
353
354 # Apps
355 local APPS=( $(prefix_match "app/") )
356 if [ "${#APPS[@]}" -gt "0" ]; then
357 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
358 fi
359 local PRIV_APPS=( $(prefix_match "priv-app/") )
360 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
361 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
362 fi
363 local V_APPS=( $(prefix_match "vendor/app/") )
364 if [ "${#V_APPS[@]}" -gt "0" ]; then
365 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
366 fi
367 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
368 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
369 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
370 fi
371
372 # Framework
373 local FRAMEWORK=( $(prefix_match "framework/") )
374 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
375 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
376 fi
377
378 # Etc
379 local ETC=( $(prefix_match "etc/") )
380 if [ "${#ETC[@]}" -gt "0" ]; then
381 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
382 fi
383 local V_ETC=( $(prefix_match "vendor/etc/") )
384 if [ "${#V_ETC[@]}" -gt "0" ]; then
385 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
386 fi
387
388 # Executables
389 local BIN=( $(prefix_match "bin/") )
390 if [ "${#BIN[@]}" -gt "0" ]; then
391 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
392 fi
393 local V_BIN=( $(prefix_match "vendor/bin/") )
394 if [ "${#V_BIN[@]}" -gt "0" ]; then
395 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
396 fi
397 local SBIN=( $(prefix_match "sbin/") )
398 if [ "${#SBIN[@]}" -gt "0" ]; then
399 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
400 fi
401
402
403 # Actually write out the final PRODUCT_PACKAGES list
404 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
405
406 if [ "$PACKAGE_COUNT" -eq "0" ]; then
407 return 0
408 fi
409
410 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
411 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
412 local LINEEND=" \\"
413 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
414 LINEEND=""
415 fi
416 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
417 done
418}
419
420#
421# write_header:
422#
423# $1: file which will be written to
424#
425# writes out the copyright header with the current year.
426# note that this is not an append operation, and should
427# be executed first!
428#
429function write_header() {
430 YEAR=$(date +"%Y")
431
432 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
433
434 cat << EOF > $1
435# Copyright (C) $YEAR The CyanogenMod Project
436#
437# Licensed under the Apache License, Version 2.0 (the "License");
438# you may not use this file except in compliance with the License.
439# You may obtain a copy of the License at
440#
441# http://www.apache.org/licenses/LICENSE-2.0
442#
443# Unless required by applicable law or agreed to in writing, software
444# distributed under the License is distributed on an "AS IS" BASIS,
445# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
446# See the License for the specific language governing permissions and
447# limitations under the License.
448
449# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
450
451EOF
452}
453
454#
455# write_headers:
456#
457# $1: devices falling under common to be added to guard - optional
458#
459# Calls write_header for each of the makefiles and creates
460# the initial path declaration and device guard for the
461# Android.mk
462#
463function write_headers() {
464 write_header "$ANDROIDMK"
465 cat << EOF >> "$ANDROIDMK"
466LOCAL_PATH := \$(call my-dir)
467
468EOF
469 if [ "$COMMON" -ne 1 ]; then
470 cat << EOF >> "$ANDROIDMK"
471ifeq (\$(TARGET_DEVICE),$DEVICE)
472
473EOF
474 else
475 if [ -z "$1" ]; then
476 echo "Argument with devices to be added to guard must be set!"
477 exit 1
478 fi
479 cat << EOF >> "$ANDROIDMK"
480ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
481
482EOF
483 fi
484
485 write_header "$BOARDMK"
486 write_header "$PRODUCTMK"
487}
488
489#
490# write_footers:
491#
492# Closes the inital guard and any other finalization tasks. Must
493# be called as the final step.
494#
495function write_footers() {
496 cat << EOF >> "$ANDROIDMK"
497endif
498EOF
499}
500
501# Return success if adb is up and not in recovery
502function _adb_connected {
503 {
504 if [[ "$(adb get-state)" == device &&
505 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
506 then
507 return 0
508 fi
509 } 2>/dev/null
510
511 return 1
512};
513
514#
515# parse_file_list:
516#
517# $1: input file
518#
519# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
520#
521function parse_file_list() {
522 if [ -z "$1" ]; then
523 echo "An input file is expected!"
524 exit 1
525 elif [ ! -f "$1" ]; then
526 echo "Input file "$1" does not exist!"
527 exit 1
528 fi
529
530 PRODUCT_PACKAGES_LIST=()
531 PRODUCT_PACKAGES_HASHES=()
532 PRODUCT_COPY_FILES_LIST=()
533 PRODUCT_COPY_FILES_HASHES=()
534
535 while read -r line; do
536 if [ -z "$line" ]; then continue; fi
537
538 # If the line has a pipe delimiter, a sha1 hash should follow.
539 # This indicates the file should be pinned and not overwritten
540 # when extracting files.
541 local SPLIT=(${line//\|/ })
542 local COUNT=${#SPLIT[@]}
543 local SPEC=${SPLIT[0]}
544 local HASH="x"
545 if [ "$COUNT" -gt "1" ]; then
546 HASH=${SPLIT[1]}
547 fi
548
549 # if line starts with a dash, it needs to be packaged
550 if [[ "$SPEC" =~ ^- ]]; then
551 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
552 PRODUCT_PACKAGES_HASHES+=("$HASH")
553 else
554 PRODUCT_COPY_FILES_LIST+=("$SPEC")
555 PRODUCT_COPY_FILES_HASHES+=("$HASH")
556 fi
557
558 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
559}
560
561#
562# write_makefiles:
563#
564# $1: file containing the list of items to extract
565#
566# Calls write_product_copy_files and write_product_packages on
567# the given file and appends to the Android.mk as well as
568# the product makefile.
569#
570function write_makefiles() {
571 parse_file_list "$1"
572 write_product_copy_files
573 write_product_packages
574}
575
576#
577# append_firmware_calls_to_makefiles:
578#
579# Appends to Android.mk the calls to all images present in radio folder
580# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
581#
582function append_firmware_calls_to_makefiles() {
583 cat << EOF >> "$ANDROIDMK"
584ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
585
586RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
587\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
588 \$(call add-radio-file,radio/\$(f)))
589\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
590
591endif
592
593EOF
594}
595
596#
597# get_file:
598#
599# $1: input file
600# $2: target file/folder
601# $3: source of the file (can be "adb" or a local folder)
602#
603# Silently extracts the input file to defined target
604# Returns success if file can be pulled from the device or found locally
605#
606function get_file() {
607 local SRC="$3"
608
609 if [ "$SRC" = "adb" ]; then
610 # try to pull
611 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
612
613 return 1
614 else
615 # try to copy
616 cp "$SRC/$1" "$2" 2>/dev/null && return 0
617
618 return 1
619 fi
620};
621
622#
623# oat2dex:
624#
625# $1: extracted apk|jar (to check if deodex is required)
626# $2: odexed apk|jar to deodex
627# $3: source of the odexed apk|jar
628#
629# Convert apk|jar .odex in the corresposing classes.dex
630#
631function oat2dex() {
632 local CM_TARGET="$1"
633 local OEM_TARGET="$2"
634 local SRC="$3"
635 local TARGET=
636 local OAT=
637
638 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Marko Man6a182a42016-09-18 01:59:20 +0200639 export BAKSMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
640 export SMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -0700641 fi
642
643 # Extract existing boot.oats to the temp folder
644 if [ -z "$ARCHES" ]; then
645 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
646 for ARCH in "arm64" "arm" "x86_64" "x86"; do
647 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
648 ARCHES+="$ARCH "
649 fi
650 done
651 fi
652
653 if [ -z "$ARCHES" ]; then
654 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
655 fi
656
657 if [ ! -f "$CM_TARGET" ]; then
658 return;
659 fi
660
661 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
662 return 0 # target apk|jar is already odexed, return
663 fi
664
665 for ARCH in $ARCHES; do
666 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
667
668 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
669
670 if get_file "$OAT" "$TMPDIR" "$SRC"; then
671 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
672 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
673 # try to extract classes.dex from boot.oat for framework jars
674 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
675 else
676 continue
677 fi
678
679 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
680 done
681
682 rm -rf "$TMPDIR/dexout"
683}
684
685#
686# init_adb_connection:
687#
688# Starts adb server and waits for the device
689#
690function init_adb_connection() {
691 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
692 if ! _adb_connected; then
693 echo "No device is online. Waiting for one..."
694 echo "Please connect USB and/or enable USB debugging"
695 until _adb_connected; do
696 sleep 1
697 done
698 echo "Device Found."
699 fi
700
701 # Retrieve IP and PORT info if we're using a TCP connection
702 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
703 | head -1 | awk '{print $1}')
704 adb root &> /dev/null
705 sleep 0.3
706 if [ -n "$TCPIPPORT" ]; then
707 # adb root just killed our connection
708 # so reconnect...
709 adb connect "$TCPIPPORT"
710 fi
711 adb wait-for-device &> /dev/null
712 sleep 0.3
713}
714
715#
716# fix_xml:
717#
718# $1: xml file to fix
719#
720function fix_xml() {
721 local XML="$1"
722 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
723
724 grep '^<?xml version' "$XML" > "$TEMP_XML"
725 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
726
727 mv "$TEMP_XML" "$XML"
728}
729
730#
731# extract:
732#
733# $1: file containing the list of items to extract
734# $2: path to extracted system folder, or "adb" to extract from device
735#
736function extract() {
737 if [ -z "$OUTDIR" ]; then
738 echo "Output dir not set!"
739 exit 1
740 fi
741
742 parse_file_list "$1"
743
744 # Allow failing, so we can try $DEST and/or $FILE
745 set +e
746
747 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
748 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
749 local COUNT=${#FILELIST[@]}
750 local SRC="$2"
751 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
752 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
753
754 if [ "$SRC" = "adb" ]; then
755 init_adb_connection
756 fi
757
758 if [ "$VENDOR_STATE" -eq "0" ]; then
759 echo "Cleaning output directory ($OUTPUT_ROOT).."
760 rm -rf "${OUTPUT_TMP:?}"
761 mkdir -p "${OUTPUT_TMP:?}"
762 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
763 VENDOR_STATE=1
764 fi
765
766 echo "Extracting $COUNT files in $1 from $SRC:"
767
768 for (( i=1; i<COUNT+1; i++ )); do
769
770 local FROM=$(target_file "${FILELIST[$i-1]}")
771 local ARGS=$(target_args "${FILELIST[$i-1]}")
772 local SPLIT=(${FILELIST[$i-1]//:/ })
773 local FILE="${SPLIT[0]#-}"
774 local OUTPUT_DIR="$OUTPUT_ROOT"
775 local TMP_DIR="$OUTPUT_TMP"
776 local TARGET=
777
778 if [ "$ARGS" = "rootfs" ]; then
779 TARGET="$FROM"
780 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
781 TMP_DIR="$TMP_DIR/rootfs"
782 else
783 TARGET="system/$FROM"
784 FILE="system/$FILE"
785 fi
786
787 if [ "$SRC" = "adb" ]; then
788 printf ' - %s .. ' "/$TARGET"
789 else
790 printf ' - %s \n' "/$TARGET"
791 fi
792
793 local DIR=$(dirname "$FROM")
794 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
795 mkdir -p "$OUTPUT_DIR/$DIR"
796 fi
797 local DEST="$OUTPUT_DIR/$FROM"
798
799 if [ "$SRC" = "adb" ]; then
800 # Try CM target first
801 adb pull "/$TARGET" "$DEST"
802 # if file does not exist try OEM target
803 if [ "$?" != "0" ]; then
804 adb pull "/$FILE" "$DEST"
805 fi
806 else
807 # Try OEM target first
808 if [ -f "$SRC/$FILE" ]; then
809 cp "$SRC/$FILE" "$DEST"
810 # if file does not exist try CM target
811 elif [ -f "$SRC/$TARGET" ]; then
812 cp "$SRC/$TARGET" "$DEST"
813 else
814 printf ' !! file not found in source\n'
815 fi
816 fi
817
818 if [ "$?" == "0" ]; then
819 # Deodex apk|jar if that's the case
820 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
821 oat2dex "$DEST" "$FILE" "$SRC"
822 if [ -f "$TMPDIR/classes.dex" ]; then
823 zip -gjq "$DEST" "$TMPDIR/classes.dex"
824 rm "$TMPDIR/classes.dex"
825 printf ' (updated %s from odex files)\n' "/$FILE"
826 fi
827 elif [[ "$DEST" =~ .xml$ ]]; then
828 fix_xml "$DEST"
829 fi
830 fi
831
832 # Check pinned files
833 local HASH="${HASHLIST[$i-1]}"
834 if [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
835 local KEEP=""
836 local TMP="$TMP_DIR/$FROM"
837 if [ -f "$TMP" ]; then
838 if [ ! -f "$DEST" ]; then
839 KEEP="1"
840 else
841 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
842 if [ "$DEST_HASH" != "$HASH" ]; then
843 KEEP="1"
844 fi
845 fi
846 if [ "$KEEP" = "1" ]; then
847 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
848 if [ "$TMP_HASH" = "$HASH" ]; then
849 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
850 cp -p "$TMP" "$DEST"
851 fi
852 fi
853 fi
854 fi
855
856 if [ -f "$DEST" ]; then
857 local TYPE="${DIR##*/}"
858 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
859 chmod 755 "$DEST"
860 else
861 chmod 644 "$DEST"
862 fi
863 fi
864
865 done
866
867 # Don't allow failing
868 set -e
869}
870
871#
872# extract_firmware:
873#
874# $1: file containing the list of items to extract
875# $2: path to extracted radio folder
876#
877function extract_firmware() {
878 if [ -z "$OUTDIR" ]; then
879 echo "Output dir not set!"
880 exit 1
881 fi
882
883 parse_file_list "$1"
884
885 # Don't allow failing
886 set -e
887
888 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
889 local COUNT=${#FILELIST[@]}
890 local SRC="$2"
891 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
892
893 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
894 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
895 rm -rf "${OUTPUT_DIR:?}/"*
896 VENDOR_RADIO_STATE=1
897 fi
898
899 echo "Extracting $COUNT files in $1 from $SRC:"
900
901 for (( i=1; i<COUNT+1; i++ )); do
902 local FILE="${FILELIST[$i-1]}"
903 printf ' - %s \n' "/radio/$FILE"
904
905 if [ ! -d "$OUTPUT_DIR" ]; then
906 mkdir -p "$OUTPUT_DIR"
907 fi
908 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
909 chmod 644 "$OUTPUT_DIR/$FILE"
910 done
911}