blob: 4bed018ab26a42e3ad6d706599931d60c930c1c6 [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
Jake Whatley9843b322017-01-25 21:49:16 -050051# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070052#
53# Must be called before any other functions can be used. This
54# sets up the internal state for a new vendor configuration.
55#
56function setup_vendor() {
57 local DEVICE="$1"
58 if [ -z "$DEVICE" ]; then
59 echo "\$DEVICE must be set before including this script!"
60 exit 1
61 fi
62
63 export VENDOR="$2"
64 if [ -z "$VENDOR" ]; then
65 echo "\$VENDOR must be set before including this script!"
66 exit 1
67 fi
68
69 export CM_ROOT="$3"
70 if [ ! -d "$CM_ROOT" ]; then
71 echo "\$CM_ROOT must be set and valid before including this script!"
72 exit 1
73 fi
74
75 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
76 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
77 mkdir -p "$CM_ROOT/$OUTDIR"
78 fi
79
Jake Whatley9843b322017-01-25 21:49:16 -050080 VNDNAME="$6"
81 if [ -z "$VNDNAME" ]; then
82 VNDNAME="$DEVICE"
83 fi
84
Jake Whatleycb7cd072016-09-18 20:55:12 +020085 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/device-vendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070086 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
87 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
88
89 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
90 COMMON=1
91 else
92 COMMON=0
93 fi
94
Gabriele Mc44696d2017-05-01 18:22:04 +020095 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070096 VENDOR_STATE=1
97 VENDOR_RADIO_STATE=1
98 else
99 VENDOR_STATE=0
100 VENDOR_RADIO_STATE=0
101 fi
102}
103
104#
105# target_file:
106#
107# $1: colon delimited list
108#
109# Returns destination filename without args
110#
111function target_file() {
112 local LINE="$1"
113 local SPLIT=(${LINE//:/ })
114 local COUNT=${#SPLIT[@]}
115 if [ "$COUNT" -gt "1" ]; then
116 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
117 printf '%s\n' "${SPLIT[1]}"
118 return 0
119 fi
120 fi
121 printf '%s\n' "${SPLIT[0]}"
122}
123
124#
125# target_args:
126#
127# $1: colon delimited list
128#
129# Returns optional arguments (last value) for given target
130#
131function target_args() {
132 local LINE="$1"
133 local SPLIT=(${LINE//:/ })
134 local COUNT=${#SPLIT[@]}
135 if [ "$COUNT" -gt "1" ]; then
136 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
137 printf '%s\n' "${SPLIT[$COUNT-1]}"
138 fi
139 fi
140}
141
142#
143# prefix_match:
144#
145# $1: the prefix to match on
146#
147# Internal function which loops thru the packages list and returns a new
148# list containing the matched files with the prefix stripped away.
149#
150function prefix_match() {
151 local PREFIX="$1"
152 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
153 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
154 printf '%s\n' "${FILE#$PREFIX}"
155 fi
156 done
157}
158
159#
160# write_product_copy_files:
161#
162# Creates the PRODUCT_COPY_FILES section in the product makefile for all
163# items in the list which do not start with a dash (-).
164#
165function write_product_copy_files() {
166 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
167 local TARGET=
168 local FILE=
169 local LINEEND=
170
171 if [ "$COUNT" -eq "0" ]; then
172 return 0
173 fi
174
175 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
176 for (( i=1; i<COUNT+1; i++ )); do
177 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
178 LINEEND=" \\"
179 if [ "$i" -eq "$COUNT" ]; then
180 LINEEND=""
181 fi
182
183 TARGET=$(target_file "$FILE")
184 printf ' %s/proprietary/%s:system/%s%s\n' \
185 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
186 done
187 return 0
188}
189
190#
191# write_packages:
192#
193# $1: The LOCAL_MODULE_CLASS for the given module list
194# $2: "true" if this package is part of the vendor/ path
195# $3: type-specific extra flags
196# $4: Name of the array holding the target list
197#
198# Internal function which writes out the BUILD_PREBUILT stanzas
199# for all modules in the list. This is called by write_product_packages
200# after the modules are categorized.
201#
202function write_packages() {
203
204 local CLASS="$1"
205 local VENDOR_PKG="$2"
206 local EXTRA="$3"
207
208 # Yes, this is a horrible hack - we create a new array using indirection
209 local ARR_NAME="$4[@]"
210 local FILELIST=("${!ARR_NAME}")
211
212 local FILE=
213 local ARGS=
214 local BASENAME=
215 local EXTENSION=
216 local PKGNAME=
217 local SRC=
218
219 for P in "${FILELIST[@]}"; do
220 FILE=$(target_file "$P")
221 ARGS=$(target_args "$P")
222
223 BASENAME=$(basename "$FILE")
224 EXTENSION=${BASENAME##*.}
225 PKGNAME=${BASENAME%.*}
226
227 # Add to final package list
228 PACKAGE_LIST+=("$PKGNAME")
229
230 SRC="proprietary"
231 if [ "$VENDOR_PKG" = "true" ]; then
232 SRC+="/vendor"
233 fi
234
235 printf 'include $(CLEAR_VARS)\n'
236 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
237 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
238 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
239 if [ "$EXTRA" = "both" ]; then
240 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
241 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
242 #if [ "$VENDOR_PKG" = "true" ]; then
243 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
244 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
245 #else
246 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
247 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
248 #fi
249 elif [ "$EXTRA" = "64" ]; then
250 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
251 else
252 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
253 fi
254 if [ "$EXTRA" != "none" ]; then
255 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
256 fi
257 elif [ "$CLASS" = "APPS" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500258 if [ -z "$ARGS" ]; then
259 if [ "$EXTRA" = "priv-app" ]; then
260 SRC="$SRC/priv-app"
261 else
262 SRC="$SRC/app"
263 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700264 fi
265 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
266 local CERT=platform
267 if [ ! -z "$ARGS" ]; then
268 CERT="$ARGS"
269 fi
270 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
271 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
272 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200273 local CERT=platform
274 if [ ! -z "$ARGS" ]; then
275 CERT="$ARGS"
276 fi
277 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700278 elif [ "$CLASS" = "ETC" ]; then
279 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
280 elif [ "$CLASS" = "EXECUTABLES" ]; then
281 if [ "$ARGS" = "rootfs" ]; then
282 SRC="$SRC/rootfs"
283 if [ "$EXTRA" = "sbin" ]; then
284 SRC="$SRC/sbin"
285 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
286 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
287 fi
288 else
289 SRC="$SRC/bin"
290 fi
291 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
292 unset EXTENSION
293 else
294 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
295 fi
296 printf 'LOCAL_MODULE_TAGS := optional\n'
297 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400298 if [ "$CLASS" = "APPS" ]; then
299 printf 'LOCAL_DEX_PREOPT := false\n'
300 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700301 if [ ! -z "$EXTENSION" ]; then
302 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
303 fi
304 if [ "$EXTRA" = "priv-app" ]; then
305 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
306 fi
307 if [ "$VENDOR_PKG" = "true" ]; then
308 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
309 fi
310 printf 'include $(BUILD_PREBUILT)\n\n'
311 done
312}
313
314#
315# write_product_packages:
316#
317# This function will create BUILD_PREBUILT entries in the
318# Android.mk and associated PRODUCT_PACKAGES list in the
319# product makefile for all files in the blob list which
320# start with a single dash (-) character.
321#
322function write_product_packages() {
323 PACKAGE_LIST=()
324
325 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
326
327 if [ "$COUNT" = "0" ]; then
328 return 0
329 fi
330
331 # Figure out what's 32-bit, what's 64-bit, and what's multilib
332 # I really should not be doing this in bash due to shitty array passing :(
333 local T_LIB32=( $(prefix_match "lib/") )
334 local T_LIB64=( $(prefix_match "lib64/") )
335 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
336 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
337 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
338
339 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
340 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
341 fi
342 if [ "${#LIB32[@]}" -gt "0" ]; then
343 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
344 fi
345 if [ "${#LIB64[@]}" -gt "0" ]; then
346 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
347 fi
348
349 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
350 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
351 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
352 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
353 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
354
355 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
356 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
357 fi
358 if [ "${#V_LIB32[@]}" -gt "0" ]; then
359 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
360 fi
361 if [ "${#V_LIB64[@]}" -gt "0" ]; then
362 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
363 fi
364
365 # Apps
366 local APPS=( $(prefix_match "app/") )
367 if [ "${#APPS[@]}" -gt "0" ]; then
368 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
369 fi
370 local PRIV_APPS=( $(prefix_match "priv-app/") )
371 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
372 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
373 fi
374 local V_APPS=( $(prefix_match "vendor/app/") )
375 if [ "${#V_APPS[@]}" -gt "0" ]; then
376 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
377 fi
378 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
379 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
380 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
381 fi
382
383 # Framework
384 local FRAMEWORK=( $(prefix_match "framework/") )
385 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
386 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
387 fi
388
389 # Etc
390 local ETC=( $(prefix_match "etc/") )
391 if [ "${#ETC[@]}" -gt "0" ]; then
392 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
393 fi
394 local V_ETC=( $(prefix_match "vendor/etc/") )
395 if [ "${#V_ETC[@]}" -gt "0" ]; then
396 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
397 fi
398
399 # Executables
400 local BIN=( $(prefix_match "bin/") )
401 if [ "${#BIN[@]}" -gt "0" ]; then
402 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
403 fi
404 local V_BIN=( $(prefix_match "vendor/bin/") )
405 if [ "${#V_BIN[@]}" -gt "0" ]; then
406 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
407 fi
408 local SBIN=( $(prefix_match "sbin/") )
409 if [ "${#SBIN[@]}" -gt "0" ]; then
410 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
411 fi
412
413
414 # Actually write out the final PRODUCT_PACKAGES list
415 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
416
417 if [ "$PACKAGE_COUNT" -eq "0" ]; then
418 return 0
419 fi
420
421 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
422 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
423 local LINEEND=" \\"
424 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
425 LINEEND=""
426 fi
427 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
428 done
429}
430
431#
432# write_header:
433#
434# $1: file which will be written to
435#
436# writes out the copyright header with the current year.
437# note that this is not an append operation, and should
438# be executed first!
439#
440function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500441 if [ -f $1 ]; then
442 rm $1
443 fi
444
Steve Kondik5bd66602016-07-15 10:39:58 -0700445 YEAR=$(date +"%Y")
446
447 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
448
Jake Whatley9843b322017-01-25 21:49:16 -0500449 NUM_REGEX='^[0-9]+$'
450 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
451 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
452 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
453 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
454 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
455 fi
456 if [ $YEAR -eq 2017 ]; then
457 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
458 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
459 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
460 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
461 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
462 else
463 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
464 fi
465 else
466 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
467 fi
468
469 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700470#
471# Licensed under the Apache License, Version 2.0 (the "License");
472# you may not use this file except in compliance with the License.
473# You may obtain a copy of the License at
474#
475# http://www.apache.org/licenses/LICENSE-2.0
476#
477# Unless required by applicable law or agreed to in writing, software
478# distributed under the License is distributed on an "AS IS" BASIS,
479# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
480# See the License for the specific language governing permissions and
481# limitations under the License.
482
483# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
484
485EOF
486}
487
488#
489# write_headers:
490#
491# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500492# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700493#
494# Calls write_header for each of the makefiles and creates
495# the initial path declaration and device guard for the
496# Android.mk
497#
498function write_headers() {
499 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500500
501 GUARD="$2"
502 if [ -z "$GUARD" ]; then
503 GUARD="TARGET_DEVICE"
504 fi
505
Steve Kondik5bd66602016-07-15 10:39:58 -0700506 cat << EOF >> "$ANDROIDMK"
507LOCAL_PATH := \$(call my-dir)
508
509EOF
510 if [ "$COMMON" -ne 1 ]; then
511 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500512ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700513
514EOF
515 else
516 if [ -z "$1" ]; then
517 echo "Argument with devices to be added to guard must be set!"
518 exit 1
519 fi
520 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500521ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700522
523EOF
524 fi
525
526 write_header "$BOARDMK"
527 write_header "$PRODUCTMK"
528}
529
530#
531# write_footers:
532#
533# Closes the inital guard and any other finalization tasks. Must
534# be called as the final step.
535#
536function write_footers() {
537 cat << EOF >> "$ANDROIDMK"
538endif
539EOF
540}
541
542# Return success if adb is up and not in recovery
543function _adb_connected {
544 {
Jake Whatley9843b322017-01-25 21:49:16 -0500545 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700546 then
547 return 0
548 fi
549 } 2>/dev/null
550
551 return 1
552};
553
554#
555# parse_file_list:
556#
557# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400558# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700559#
560# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
561#
562function parse_file_list() {
563 if [ -z "$1" ]; then
564 echo "An input file is expected!"
565 exit 1
566 elif [ ! -f "$1" ]; then
567 echo "Input file "$1" does not exist!"
568 exit 1
569 fi
570
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400571 if [ $# -eq 2 ]; then
572 LIST=$TMPDIR/files.txt
573 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
574 else
575 LIST=$1
576 fi
577
578
Steve Kondik5bd66602016-07-15 10:39:58 -0700579 PRODUCT_PACKAGES_LIST=()
580 PRODUCT_PACKAGES_HASHES=()
581 PRODUCT_COPY_FILES_LIST=()
582 PRODUCT_COPY_FILES_HASHES=()
583
584 while read -r line; do
585 if [ -z "$line" ]; then continue; fi
586
587 # If the line has a pipe delimiter, a sha1 hash should follow.
588 # This indicates the file should be pinned and not overwritten
589 # when extracting files.
590 local SPLIT=(${line//\|/ })
591 local COUNT=${#SPLIT[@]}
592 local SPEC=${SPLIT[0]}
593 local HASH="x"
594 if [ "$COUNT" -gt "1" ]; then
595 HASH=${SPLIT[1]}
596 fi
597
598 # if line starts with a dash, it needs to be packaged
599 if [[ "$SPEC" =~ ^- ]]; then
600 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
601 PRODUCT_PACKAGES_HASHES+=("$HASH")
602 else
603 PRODUCT_COPY_FILES_LIST+=("$SPEC")
604 PRODUCT_COPY_FILES_HASHES+=("$HASH")
605 fi
606
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400607 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700608}
609
610#
611# write_makefiles:
612#
613# $1: file containing the list of items to extract
614#
615# Calls write_product_copy_files and write_product_packages on
616# the given file and appends to the Android.mk as well as
617# the product makefile.
618#
619function write_makefiles() {
620 parse_file_list "$1"
621 write_product_copy_files
622 write_product_packages
623}
624
625#
626# append_firmware_calls_to_makefiles:
627#
628# Appends to Android.mk the calls to all images present in radio folder
629# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
630#
631function append_firmware_calls_to_makefiles() {
632 cat << EOF >> "$ANDROIDMK"
633ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
634
635RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
636\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
637 \$(call add-radio-file,radio/\$(f)))
638\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
639
640endif
641
642EOF
643}
644
645#
646# get_file:
647#
648# $1: input file
649# $2: target file/folder
650# $3: source of the file (can be "adb" or a local folder)
651#
652# Silently extracts the input file to defined target
653# Returns success if file can be pulled from the device or found locally
654#
655function get_file() {
656 local SRC="$3"
657
658 if [ "$SRC" = "adb" ]; then
659 # try to pull
660 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
661
662 return 1
663 else
664 # try to copy
Jake Whatley9843b322017-01-25 21:49:16 -0500665 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700666
667 return 1
668 fi
669};
670
671#
672# oat2dex:
673#
674# $1: extracted apk|jar (to check if deodex is required)
675# $2: odexed apk|jar to deodex
676# $3: source of the odexed apk|jar
677#
678# Convert apk|jar .odex in the corresposing classes.dex
679#
680function oat2dex() {
681 local CM_TARGET="$1"
682 local OEM_TARGET="$2"
683 local SRC="$3"
684 local TARGET=
Sam Mortimer26329022017-08-23 18:59:52 -0700685 local DEXBASE=
686 local ODEX=
687 local VDEX=
Steve Kondik5bd66602016-07-15 10:39:58 -0700688
689 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Marko Man6a182a42016-09-18 01:59:20 +0200690 export BAKSMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
691 export SMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -0700692 fi
693
694 # Extract existing boot.oats to the temp folder
695 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500696 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700697 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500698 mkdir -p "$TMPDIR/system/framework/$ARCH"
699 if get_file "system/framework/$ARCH/" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700700 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500701 else
702 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700703 fi
704 done
705 fi
706
707 if [ -z "$ARCHES" ]; then
708 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
709 fi
710
711 if [ ! -f "$CM_TARGET" ]; then
712 return;
713 fi
714
715 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
716 return 0 # target apk|jar is already odexed, return
717 fi
718
719 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500720 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700721
Sam Mortimer26329022017-08-23 18:59:52 -0700722 local DEXBASE="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}")"
723 local ODEX="${DEXBASE}.odex"
724 local VDEX="${DEXBASE}.vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700725
Sam Mortimer26329022017-08-23 18:59:52 -0700726 if get_file "$ODEX" "$TMPDIR" "$SRC"; then
727 # If there was an odex, also look for a vdex.
728 get_file "$VDEX" "$TMPDIR" "$SRC"
729 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$ODEX")"
Steve Kondik5bd66602016-07-15 10:39:58 -0700730 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500731 # try to extract classes.dex from boot.oats for framework jars
732 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
733 if [ ! -f "$JAROAT" ]; then
734 JAROAT=$BOOTOAT;
735 fi
736 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
Steve Kondik5bd66602016-07-15 10:39:58 -0700737 else
738 continue
739 fi
740
Jake Whatley9843b322017-01-25 21:49:16 -0500741 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
Steve Kondik5bd66602016-07-15 10:39:58 -0700742 done
743
744 rm -rf "$TMPDIR/dexout"
745}
746
747#
748# init_adb_connection:
749#
750# Starts adb server and waits for the device
751#
752function init_adb_connection() {
753 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
754 if ! _adb_connected; then
755 echo "No device is online. Waiting for one..."
756 echo "Please connect USB and/or enable USB debugging"
757 until _adb_connected; do
758 sleep 1
759 done
760 echo "Device Found."
761 fi
762
763 # Retrieve IP and PORT info if we're using a TCP connection
764 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
765 | head -1 | awk '{print $1}')
766 adb root &> /dev/null
767 sleep 0.3
768 if [ -n "$TCPIPPORT" ]; then
769 # adb root just killed our connection
770 # so reconnect...
771 adb connect "$TCPIPPORT"
772 fi
773 adb wait-for-device &> /dev/null
774 sleep 0.3
775}
776
777#
778# fix_xml:
779#
780# $1: xml file to fix
781#
782function fix_xml() {
783 local XML="$1"
784 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
785
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200786 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
787 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700788
789 mv "$TEMP_XML" "$XML"
790}
791
792#
793# extract:
794#
795# $1: file containing the list of items to extract
Dan Pasanen0cc05012017-03-21 09:06:11 -0500796# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400797# $3: section in list file to extract - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700798#
799function extract() {
800 if [ -z "$OUTDIR" ]; then
801 echo "Output dir not set!"
802 exit 1
803 fi
804
Harry Youd972c4112017-08-05 09:18:56 +0100805 if [ -z "$3" ]; then
806 parse_file_list "$1"
807 else
808 parse_file_list "$1" "$3"
809 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700810
811 # Allow failing, so we can try $DEST and/or $FILE
812 set +e
813
814 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
815 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
816 local COUNT=${#FILELIST[@]}
817 local SRC="$2"
818 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
819 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
820
821 if [ "$SRC" = "adb" ]; then
822 init_adb_connection
823 fi
824
Dan Pasanen0cc05012017-03-21 09:06:11 -0500825 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
826 DUMPDIR="$CM_ROOT"/system_dump
827
828 # Check if we're working with the same zip that was passed last time.
829 # If so, let's just use what's already extracted.
830 MD5=`md5sum "$SRC"| awk '{print $1}'`
831 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
832
833 if [ "$MD5" != "$OLDMD5" ]; then
834 rm -rf "$DUMPDIR"
835 mkdir "$DUMPDIR"
836 unzip "$SRC" -d "$DUMPDIR"
837 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
838
839 # Stop if an A/B OTA zip is detected. We cannot extract these.
840 if [ -a "$DUMPDIR"/payload.bin ]; then
841 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
842 exit 1
843 # If OTA is block based, extract it.
844 elif [ -a "$DUMPDIR"/system.new.dat ]; then
845 echo "Converting system.new.dat to system.img"
846 python "$CM_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
847 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
848 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
849 echo "Requesting sudo access to mount the system.img"
850 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
851 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
852 sudo umount "$DUMPDIR"/tmp
853 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
854 fi
855 fi
856
857 SRC="$DUMPDIR"
858 fi
859
Steve Kondik5bd66602016-07-15 10:39:58 -0700860 if [ "$VENDOR_STATE" -eq "0" ]; then
861 echo "Cleaning output directory ($OUTPUT_ROOT).."
862 rm -rf "${OUTPUT_TMP:?}"
863 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -0500864 if [ -d "$OUTPUT_ROOT" ]; then
865 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
866 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700867 VENDOR_STATE=1
868 fi
869
870 echo "Extracting $COUNT files in $1 from $SRC:"
871
872 for (( i=1; i<COUNT+1; i++ )); do
873
874 local FROM=$(target_file "${FILELIST[$i-1]}")
875 local ARGS=$(target_args "${FILELIST[$i-1]}")
876 local SPLIT=(${FILELIST[$i-1]//:/ })
877 local FILE="${SPLIT[0]#-}"
878 local OUTPUT_DIR="$OUTPUT_ROOT"
879 local TMP_DIR="$OUTPUT_TMP"
880 local TARGET=
881
882 if [ "$ARGS" = "rootfs" ]; then
883 TARGET="$FROM"
884 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
885 TMP_DIR="$TMP_DIR/rootfs"
Rashed Abdel-Tawab7da4a1b2017-04-04 18:03:35 -0400886 elif [ -f "$SRC/$FILE" ] && [ "$SRC" != "adb" ]; then
887 TARGET="$FROM"
Steve Kondik5bd66602016-07-15 10:39:58 -0700888 else
889 TARGET="system/$FROM"
890 FILE="system/$FILE"
891 fi
892
893 if [ "$SRC" = "adb" ]; then
894 printf ' - %s .. ' "/$TARGET"
895 else
896 printf ' - %s \n' "/$TARGET"
897 fi
898
899 local DIR=$(dirname "$FROM")
900 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
901 mkdir -p "$OUTPUT_DIR/$DIR"
902 fi
903 local DEST="$OUTPUT_DIR/$FROM"
904
905 if [ "$SRC" = "adb" ]; then
906 # Try CM target first
907 adb pull "/$TARGET" "$DEST"
908 # if file does not exist try OEM target
909 if [ "$?" != "0" ]; then
910 adb pull "/$FILE" "$DEST"
911 fi
912 else
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -0500913 # Try CM target first
914 if [ -f "$SRC/$TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700915 cp "$SRC/$TARGET" "$DEST"
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -0500916 # if file does not exist try OEM target
917 elif [ -f "$SRC/$FILE" ]; then
918 cp "$SRC/$FILE" "$DEST"
Steve Kondik5bd66602016-07-15 10:39:58 -0700919 else
920 printf ' !! file not found in source\n'
921 fi
922 fi
923
924 if [ "$?" == "0" ]; then
925 # Deodex apk|jar if that's the case
926 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
927 oat2dex "$DEST" "$FILE" "$SRC"
928 if [ -f "$TMPDIR/classes.dex" ]; then
929 zip -gjq "$DEST" "$TMPDIR/classes.dex"
930 rm "$TMPDIR/classes.dex"
931 printf ' (updated %s from odex files)\n' "/$FILE"
932 fi
933 elif [[ "$DEST" =~ .xml$ ]]; then
934 fix_xml "$DEST"
935 fi
936 fi
937
938 # Check pinned files
939 local HASH="${HASHLIST[$i-1]}"
Jake Whatley9843b322017-01-25 21:49:16 -0500940 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700941 local KEEP=""
942 local TMP="$TMP_DIR/$FROM"
943 if [ -f "$TMP" ]; then
944 if [ ! -f "$DEST" ]; then
945 KEEP="1"
946 else
Jake Whatley9843b322017-01-25 21:49:16 -0500947 if [ "$(uname)" == "Darwin" ]; then
948 local DEST_HASH=$(shasum "$DEST" | awk '{print $1}' )
949 else
950 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
951 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700952 if [ "$DEST_HASH" != "$HASH" ]; then
953 KEEP="1"
954 fi
955 fi
956 if [ "$KEEP" = "1" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500957 if [ "$(uname)" == "Darwin" ]; then
958 local TMP_HASH=$(shasum "$TMP" | awk '{print $1}' )
959 else
960 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
961 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700962 if [ "$TMP_HASH" = "$HASH" ]; then
963 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
964 cp -p "$TMP" "$DEST"
965 fi
966 fi
967 fi
968 fi
969
970 if [ -f "$DEST" ]; then
971 local TYPE="${DIR##*/}"
972 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
973 chmod 755 "$DEST"
974 else
975 chmod 644 "$DEST"
976 fi
977 fi
978
979 done
980
981 # Don't allow failing
982 set -e
983}
984
985#
986# extract_firmware:
987#
988# $1: file containing the list of items to extract
989# $2: path to extracted radio folder
990#
991function extract_firmware() {
992 if [ -z "$OUTDIR" ]; then
993 echo "Output dir not set!"
994 exit 1
995 fi
996
997 parse_file_list "$1"
998
999 # Don't allow failing
1000 set -e
1001
1002 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1003 local COUNT=${#FILELIST[@]}
1004 local SRC="$2"
1005 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
1006
1007 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1008 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1009 rm -rf "${OUTPUT_DIR:?}/"*
1010 VENDOR_RADIO_STATE=1
1011 fi
1012
1013 echo "Extracting $COUNT files in $1 from $SRC:"
1014
1015 for (( i=1; i<COUNT+1; i++ )); do
1016 local FILE="${FILELIST[$i-1]}"
1017 printf ' - %s \n' "/radio/$FILE"
1018
1019 if [ ! -d "$OUTPUT_DIR" ]; then
1020 mkdir -p "$OUTPUT_DIR"
1021 fi
1022 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1023 chmod 644 "$OUTPUT_DIR/$FILE"
1024 done
1025}