blob: baca1cc7d4e38a3df392d86e46c4f4f367e72dd8 [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")
M1cha3e8c5bf2017-01-04 09:00:11 +0100224 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700225 EXTENSION=${BASENAME##*.}
226 PKGNAME=${BASENAME%.*}
227
228 # Add to final package list
229 PACKAGE_LIST+=("$PKGNAME")
230
231 SRC="proprietary"
232 if [ "$VENDOR_PKG" = "true" ]; then
233 SRC+="/vendor"
234 fi
235
236 printf 'include $(CLEAR_VARS)\n'
237 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
238 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
239 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
240 if [ "$EXTRA" = "both" ]; then
241 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
242 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
243 #if [ "$VENDOR_PKG" = "true" ]; then
244 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
245 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
246 #else
247 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
248 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
249 #fi
250 elif [ "$EXTRA" = "64" ]; then
251 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
252 else
253 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
254 fi
255 if [ "$EXTRA" != "none" ]; then
256 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
257 fi
258 elif [ "$CLASS" = "APPS" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500259 if [ -z "$ARGS" ]; then
260 if [ "$EXTRA" = "priv-app" ]; then
261 SRC="$SRC/priv-app"
262 else
263 SRC="$SRC/app"
264 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700265 fi
266 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
267 local CERT=platform
268 if [ ! -z "$ARGS" ]; then
269 CERT="$ARGS"
270 fi
271 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
272 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
273 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200274 local CERT=platform
275 if [ ! -z "$ARGS" ]; then
276 CERT="$ARGS"
277 fi
278 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700279 elif [ "$CLASS" = "ETC" ]; then
280 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
281 elif [ "$CLASS" = "EXECUTABLES" ]; then
282 if [ "$ARGS" = "rootfs" ]; then
283 SRC="$SRC/rootfs"
284 if [ "$EXTRA" = "sbin" ]; then
285 SRC="$SRC/sbin"
286 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
287 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
288 fi
289 else
290 SRC="$SRC/bin"
291 fi
292 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
293 unset EXTENSION
294 else
295 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
296 fi
297 printf 'LOCAL_MODULE_TAGS := optional\n'
298 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400299 if [ "$CLASS" = "APPS" ]; then
300 printf 'LOCAL_DEX_PREOPT := false\n'
301 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700302 if [ ! -z "$EXTENSION" ]; then
303 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
304 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100305 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
306 if [ "$DIRNAME" != "." ]; then
307 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
308 fi
309 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700310 if [ "$EXTRA" = "priv-app" ]; then
311 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
312 fi
313 if [ "$VENDOR_PKG" = "true" ]; then
314 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
315 fi
316 printf 'include $(BUILD_PREBUILT)\n\n'
317 done
318}
319
320#
321# write_product_packages:
322#
323# This function will create BUILD_PREBUILT entries in the
324# Android.mk and associated PRODUCT_PACKAGES list in the
325# product makefile for all files in the blob list which
326# start with a single dash (-) character.
327#
328function write_product_packages() {
329 PACKAGE_LIST=()
330
331 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
332
333 if [ "$COUNT" = "0" ]; then
334 return 0
335 fi
336
337 # Figure out what's 32-bit, what's 64-bit, and what's multilib
338 # I really should not be doing this in bash due to shitty array passing :(
339 local T_LIB32=( $(prefix_match "lib/") )
340 local T_LIB64=( $(prefix_match "lib64/") )
341 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
342 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
343 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
344
345 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
346 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
347 fi
348 if [ "${#LIB32[@]}" -gt "0" ]; then
349 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
350 fi
351 if [ "${#LIB64[@]}" -gt "0" ]; then
352 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
353 fi
354
355 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
356 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
357 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
358 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
359 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
360
361 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
362 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
363 fi
364 if [ "${#V_LIB32[@]}" -gt "0" ]; then
365 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
366 fi
367 if [ "${#V_LIB64[@]}" -gt "0" ]; then
368 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
369 fi
370
371 # Apps
372 local APPS=( $(prefix_match "app/") )
373 if [ "${#APPS[@]}" -gt "0" ]; then
374 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
375 fi
376 local PRIV_APPS=( $(prefix_match "priv-app/") )
377 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
378 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
379 fi
380 local V_APPS=( $(prefix_match "vendor/app/") )
381 if [ "${#V_APPS[@]}" -gt "0" ]; then
382 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
383 fi
384 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
385 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
386 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
387 fi
388
389 # Framework
390 local FRAMEWORK=( $(prefix_match "framework/") )
391 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
392 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
393 fi
394
395 # Etc
396 local ETC=( $(prefix_match "etc/") )
397 if [ "${#ETC[@]}" -gt "0" ]; then
398 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
399 fi
400 local V_ETC=( $(prefix_match "vendor/etc/") )
401 if [ "${#V_ETC[@]}" -gt "0" ]; then
402 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
403 fi
404
405 # Executables
406 local BIN=( $(prefix_match "bin/") )
407 if [ "${#BIN[@]}" -gt "0" ]; then
408 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
409 fi
410 local V_BIN=( $(prefix_match "vendor/bin/") )
411 if [ "${#V_BIN[@]}" -gt "0" ]; then
412 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
413 fi
414 local SBIN=( $(prefix_match "sbin/") )
415 if [ "${#SBIN[@]}" -gt "0" ]; then
416 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
417 fi
418
419
420 # Actually write out the final PRODUCT_PACKAGES list
421 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
422
423 if [ "$PACKAGE_COUNT" -eq "0" ]; then
424 return 0
425 fi
426
427 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
428 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
429 local LINEEND=" \\"
430 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
431 LINEEND=""
432 fi
433 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
434 done
435}
436
437#
438# write_header:
439#
440# $1: file which will be written to
441#
442# writes out the copyright header with the current year.
443# note that this is not an append operation, and should
444# be executed first!
445#
446function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500447 if [ -f $1 ]; then
448 rm $1
449 fi
450
Steve Kondik5bd66602016-07-15 10:39:58 -0700451 YEAR=$(date +"%Y")
452
453 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
454
Jake Whatley9843b322017-01-25 21:49:16 -0500455 NUM_REGEX='^[0-9]+$'
456 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
457 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
458 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
459 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
460 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
461 fi
462 if [ $YEAR -eq 2017 ]; then
463 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
464 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
465 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
466 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
467 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
468 else
469 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
470 fi
471 else
472 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
473 fi
474
475 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700476#
477# Licensed under the Apache License, Version 2.0 (the "License");
478# you may not use this file except in compliance with the License.
479# You may obtain a copy of the License at
480#
481# http://www.apache.org/licenses/LICENSE-2.0
482#
483# Unless required by applicable law or agreed to in writing, software
484# distributed under the License is distributed on an "AS IS" BASIS,
485# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
486# See the License for the specific language governing permissions and
487# limitations under the License.
488
489# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
490
491EOF
492}
493
494#
495# write_headers:
496#
497# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500498# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700499#
500# Calls write_header for each of the makefiles and creates
501# the initial path declaration and device guard for the
502# Android.mk
503#
504function write_headers() {
505 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500506
507 GUARD="$2"
508 if [ -z "$GUARD" ]; then
509 GUARD="TARGET_DEVICE"
510 fi
511
Steve Kondik5bd66602016-07-15 10:39:58 -0700512 cat << EOF >> "$ANDROIDMK"
513LOCAL_PATH := \$(call my-dir)
514
515EOF
516 if [ "$COMMON" -ne 1 ]; then
517 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500518ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700519
520EOF
521 else
522 if [ -z "$1" ]; then
523 echo "Argument with devices to be added to guard must be set!"
524 exit 1
525 fi
526 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500527ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700528
529EOF
530 fi
531
532 write_header "$BOARDMK"
533 write_header "$PRODUCTMK"
534}
535
536#
537# write_footers:
538#
539# Closes the inital guard and any other finalization tasks. Must
540# be called as the final step.
541#
542function write_footers() {
543 cat << EOF >> "$ANDROIDMK"
544endif
545EOF
546}
547
548# Return success if adb is up and not in recovery
549function _adb_connected {
550 {
Jake Whatley9843b322017-01-25 21:49:16 -0500551 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700552 then
553 return 0
554 fi
555 } 2>/dev/null
556
557 return 1
558};
559
560#
561# parse_file_list:
562#
563# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400564# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700565#
566# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
567#
568function parse_file_list() {
569 if [ -z "$1" ]; then
570 echo "An input file is expected!"
571 exit 1
572 elif [ ! -f "$1" ]; then
573 echo "Input file "$1" does not exist!"
574 exit 1
575 fi
576
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400577 if [ $# -eq 2 ]; then
578 LIST=$TMPDIR/files.txt
579 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
580 else
581 LIST=$1
582 fi
583
584
Steve Kondik5bd66602016-07-15 10:39:58 -0700585 PRODUCT_PACKAGES_LIST=()
586 PRODUCT_PACKAGES_HASHES=()
587 PRODUCT_COPY_FILES_LIST=()
588 PRODUCT_COPY_FILES_HASHES=()
589
590 while read -r line; do
591 if [ -z "$line" ]; then continue; fi
592
593 # If the line has a pipe delimiter, a sha1 hash should follow.
594 # This indicates the file should be pinned and not overwritten
595 # when extracting files.
596 local SPLIT=(${line//\|/ })
597 local COUNT=${#SPLIT[@]}
598 local SPEC=${SPLIT[0]}
599 local HASH="x"
600 if [ "$COUNT" -gt "1" ]; then
601 HASH=${SPLIT[1]}
602 fi
603
604 # if line starts with a dash, it needs to be packaged
605 if [[ "$SPEC" =~ ^- ]]; then
606 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
607 PRODUCT_PACKAGES_HASHES+=("$HASH")
608 else
609 PRODUCT_COPY_FILES_LIST+=("$SPEC")
610 PRODUCT_COPY_FILES_HASHES+=("$HASH")
611 fi
612
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400613 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700614}
615
616#
617# write_makefiles:
618#
619# $1: file containing the list of items to extract
620#
621# Calls write_product_copy_files and write_product_packages on
622# the given file and appends to the Android.mk as well as
623# the product makefile.
624#
625function write_makefiles() {
626 parse_file_list "$1"
627 write_product_copy_files
628 write_product_packages
629}
630
631#
632# append_firmware_calls_to_makefiles:
633#
634# Appends to Android.mk the calls to all images present in radio folder
635# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
636#
637function append_firmware_calls_to_makefiles() {
638 cat << EOF >> "$ANDROIDMK"
639ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
640
641RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
642\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
643 \$(call add-radio-file,radio/\$(f)))
644\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
645
646endif
647
648EOF
649}
650
651#
652# get_file:
653#
654# $1: input file
655# $2: target file/folder
656# $3: source of the file (can be "adb" or a local folder)
657#
658# Silently extracts the input file to defined target
659# Returns success if file can be pulled from the device or found locally
660#
661function get_file() {
662 local SRC="$3"
663
664 if [ "$SRC" = "adb" ]; then
665 # try to pull
666 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
667
668 return 1
669 else
670 # try to copy
Jake Whatley9843b322017-01-25 21:49:16 -0500671 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700672
673 return 1
674 fi
675};
676
677#
678# oat2dex:
679#
680# $1: extracted apk|jar (to check if deodex is required)
681# $2: odexed apk|jar to deodex
682# $3: source of the odexed apk|jar
683#
684# Convert apk|jar .odex in the corresposing classes.dex
685#
686function oat2dex() {
687 local CM_TARGET="$1"
688 local OEM_TARGET="$2"
689 local SRC="$3"
690 local TARGET=
Sam Mortimer26329022017-08-23 18:59:52 -0700691 local DEXBASE=
692 local ODEX=
693 local VDEX=
Steve Kondik5bd66602016-07-15 10:39:58 -0700694
695 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Marko Man6a182a42016-09-18 01:59:20 +0200696 export BAKSMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
697 export SMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -0700698 fi
699
700 # Extract existing boot.oats to the temp folder
701 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500702 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700703 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500704 mkdir -p "$TMPDIR/system/framework/$ARCH"
705 if get_file "system/framework/$ARCH/" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700706 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500707 else
708 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700709 fi
710 done
711 fi
712
713 if [ -z "$ARCHES" ]; then
714 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
715 fi
716
717 if [ ! -f "$CM_TARGET" ]; then
718 return;
719 fi
720
721 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
722 return 0 # target apk|jar is already odexed, return
723 fi
724
725 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500726 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700727
Sam Mortimer26329022017-08-23 18:59:52 -0700728 local DEXBASE="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}")"
729 local ODEX="${DEXBASE}.odex"
730 local VDEX="${DEXBASE}.vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700731
Sam Mortimer26329022017-08-23 18:59:52 -0700732 if get_file "$ODEX" "$TMPDIR" "$SRC"; then
733 # If there was an odex, also look for a vdex.
734 get_file "$VDEX" "$TMPDIR" "$SRC"
735 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$ODEX")"
Steve Kondik5bd66602016-07-15 10:39:58 -0700736 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500737 # try to extract classes.dex from boot.oats for framework jars
738 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
739 if [ ! -f "$JAROAT" ]; then
740 JAROAT=$BOOTOAT;
741 fi
742 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
Steve Kondik5bd66602016-07-15 10:39:58 -0700743 else
744 continue
745 fi
746
Jake Whatley9843b322017-01-25 21:49:16 -0500747 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
Steve Kondik5bd66602016-07-15 10:39:58 -0700748 done
749
750 rm -rf "$TMPDIR/dexout"
751}
752
753#
754# init_adb_connection:
755#
756# Starts adb server and waits for the device
757#
758function init_adb_connection() {
759 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
760 if ! _adb_connected; then
761 echo "No device is online. Waiting for one..."
762 echo "Please connect USB and/or enable USB debugging"
763 until _adb_connected; do
764 sleep 1
765 done
766 echo "Device Found."
767 fi
768
769 # Retrieve IP and PORT info if we're using a TCP connection
770 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
771 | head -1 | awk '{print $1}')
772 adb root &> /dev/null
773 sleep 0.3
774 if [ -n "$TCPIPPORT" ]; then
775 # adb root just killed our connection
776 # so reconnect...
777 adb connect "$TCPIPPORT"
778 fi
779 adb wait-for-device &> /dev/null
780 sleep 0.3
781}
782
783#
784# fix_xml:
785#
786# $1: xml file to fix
787#
788function fix_xml() {
789 local XML="$1"
790 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
791
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200792 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
793 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700794
795 mv "$TEMP_XML" "$XML"
796}
797
798#
799# extract:
800#
801# $1: file containing the list of items to extract
Dan Pasanen0cc05012017-03-21 09:06:11 -0500802# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400803# $3: section in list file to extract - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700804#
805function extract() {
806 if [ -z "$OUTDIR" ]; then
807 echo "Output dir not set!"
808 exit 1
809 fi
810
Harry Youd972c4112017-08-05 09:18:56 +0100811 if [ -z "$3" ]; then
812 parse_file_list "$1"
813 else
814 parse_file_list "$1" "$3"
815 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700816
817 # Allow failing, so we can try $DEST and/or $FILE
818 set +e
819
820 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
821 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
822 local COUNT=${#FILELIST[@]}
823 local SRC="$2"
824 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
825 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
826
827 if [ "$SRC" = "adb" ]; then
828 init_adb_connection
829 fi
830
Dan Pasanen0cc05012017-03-21 09:06:11 -0500831 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
832 DUMPDIR="$CM_ROOT"/system_dump
833
834 # Check if we're working with the same zip that was passed last time.
835 # If so, let's just use what's already extracted.
836 MD5=`md5sum "$SRC"| awk '{print $1}'`
837 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
838
839 if [ "$MD5" != "$OLDMD5" ]; then
840 rm -rf "$DUMPDIR"
841 mkdir "$DUMPDIR"
842 unzip "$SRC" -d "$DUMPDIR"
843 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
844
845 # Stop if an A/B OTA zip is detected. We cannot extract these.
846 if [ -a "$DUMPDIR"/payload.bin ]; then
847 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
848 exit 1
849 # If OTA is block based, extract it.
850 elif [ -a "$DUMPDIR"/system.new.dat ]; then
851 echo "Converting system.new.dat to system.img"
852 python "$CM_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
853 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
854 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
855 echo "Requesting sudo access to mount the system.img"
856 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
857 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
858 sudo umount "$DUMPDIR"/tmp
859 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
860 fi
861 fi
862
863 SRC="$DUMPDIR"
864 fi
865
Steve Kondik5bd66602016-07-15 10:39:58 -0700866 if [ "$VENDOR_STATE" -eq "0" ]; then
867 echo "Cleaning output directory ($OUTPUT_ROOT).."
868 rm -rf "${OUTPUT_TMP:?}"
869 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -0500870 if [ -d "$OUTPUT_ROOT" ]; then
871 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
872 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700873 VENDOR_STATE=1
874 fi
875
876 echo "Extracting $COUNT files in $1 from $SRC:"
877
878 for (( i=1; i<COUNT+1; i++ )); do
879
880 local FROM=$(target_file "${FILELIST[$i-1]}")
881 local ARGS=$(target_args "${FILELIST[$i-1]}")
882 local SPLIT=(${FILELIST[$i-1]//:/ })
883 local FILE="${SPLIT[0]#-}"
884 local OUTPUT_DIR="$OUTPUT_ROOT"
885 local TMP_DIR="$OUTPUT_TMP"
886 local TARGET=
887
888 if [ "$ARGS" = "rootfs" ]; then
889 TARGET="$FROM"
890 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
891 TMP_DIR="$TMP_DIR/rootfs"
Rashed Abdel-Tawab7da4a1b2017-04-04 18:03:35 -0400892 elif [ -f "$SRC/$FILE" ] && [ "$SRC" != "adb" ]; then
893 TARGET="$FROM"
Steve Kondik5bd66602016-07-15 10:39:58 -0700894 else
895 TARGET="system/$FROM"
896 FILE="system/$FILE"
897 fi
898
899 if [ "$SRC" = "adb" ]; then
900 printf ' - %s .. ' "/$TARGET"
901 else
902 printf ' - %s \n' "/$TARGET"
903 fi
904
905 local DIR=$(dirname "$FROM")
906 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
907 mkdir -p "$OUTPUT_DIR/$DIR"
908 fi
909 local DEST="$OUTPUT_DIR/$FROM"
910
911 if [ "$SRC" = "adb" ]; then
912 # Try CM target first
913 adb pull "/$TARGET" "$DEST"
914 # if file does not exist try OEM target
915 if [ "$?" != "0" ]; then
916 adb pull "/$FILE" "$DEST"
917 fi
918 else
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -0500919 # Try CM target first
920 if [ -f "$SRC/$TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700921 cp "$SRC/$TARGET" "$DEST"
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -0500922 # if file does not exist try OEM target
923 elif [ -f "$SRC/$FILE" ]; then
924 cp "$SRC/$FILE" "$DEST"
Steve Kondik5bd66602016-07-15 10:39:58 -0700925 else
926 printf ' !! file not found in source\n'
927 fi
928 fi
929
930 if [ "$?" == "0" ]; then
931 # Deodex apk|jar if that's the case
932 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
933 oat2dex "$DEST" "$FILE" "$SRC"
934 if [ -f "$TMPDIR/classes.dex" ]; then
935 zip -gjq "$DEST" "$TMPDIR/classes.dex"
936 rm "$TMPDIR/classes.dex"
937 printf ' (updated %s from odex files)\n' "/$FILE"
938 fi
939 elif [[ "$DEST" =~ .xml$ ]]; then
940 fix_xml "$DEST"
941 fi
942 fi
943
944 # Check pinned files
945 local HASH="${HASHLIST[$i-1]}"
Jake Whatley9843b322017-01-25 21:49:16 -0500946 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700947 local KEEP=""
948 local TMP="$TMP_DIR/$FROM"
949 if [ -f "$TMP" ]; then
950 if [ ! -f "$DEST" ]; then
951 KEEP="1"
952 else
Jake Whatley9843b322017-01-25 21:49:16 -0500953 if [ "$(uname)" == "Darwin" ]; then
954 local DEST_HASH=$(shasum "$DEST" | awk '{print $1}' )
955 else
956 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
957 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700958 if [ "$DEST_HASH" != "$HASH" ]; then
959 KEEP="1"
960 fi
961 fi
962 if [ "$KEEP" = "1" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500963 if [ "$(uname)" == "Darwin" ]; then
964 local TMP_HASH=$(shasum "$TMP" | awk '{print $1}' )
965 else
966 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
967 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700968 if [ "$TMP_HASH" = "$HASH" ]; then
969 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
970 cp -p "$TMP" "$DEST"
971 fi
972 fi
973 fi
974 fi
975
976 if [ -f "$DEST" ]; then
977 local TYPE="${DIR##*/}"
978 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
979 chmod 755 "$DEST"
980 else
981 chmod 644 "$DEST"
982 fi
983 fi
984
985 done
986
987 # Don't allow failing
988 set -e
989}
990
991#
992# extract_firmware:
993#
994# $1: file containing the list of items to extract
995# $2: path to extracted radio folder
996#
997function extract_firmware() {
998 if [ -z "$OUTDIR" ]; then
999 echo "Output dir not set!"
1000 exit 1
1001 fi
1002
1003 parse_file_list "$1"
1004
1005 # Don't allow failing
1006 set -e
1007
1008 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1009 local COUNT=${#FILELIST[@]}
1010 local SRC="$2"
1011 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
1012
1013 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1014 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1015 rm -rf "${OUTPUT_DIR:?}/"*
1016 VENDOR_RADIO_STATE=1
1017 fi
1018
1019 echo "Extracting $COUNT files in $1 from $SRC:"
1020
1021 for (( i=1; i<COUNT+1; i++ )); do
1022 local FILE="${FILELIST[$i-1]}"
1023 printf ' - %s \n' "/radio/$FILE"
1024
1025 if [ ! -d "$OUTPUT_DIR" ]; then
1026 mkdir -p "$OUTPUT_DIR"
1027 fi
1028 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1029 chmod 644 "$OUTPUT_DIR/$FILE"
1030 done
1031}