blob: 2d23b5dfa905c1da15ff1cfe7cadbb3e773552ed [file] [log] [blame]
Steve Kondik5bd66602016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_COPY_FILES_HASHES=()
20PRODUCT_PACKAGES_LIST=()
21PRODUCT_PACKAGES_HASHES=()
22PACKAGE_LIST=()
23VENDOR_STATE=-1
24VENDOR_RADIO_STATE=-1
25COMMON=-1
26ARCHES=
27FULLY_DEODEXED=-1
28
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040029TMPDIR=$(mktemp -d)
Steve Kondik5bd66602016-07-15 10:39:58 -070030
31#
32# cleanup
33#
34# kill our tmpfiles with fire on exit
35#
36function cleanup() {
37 rm -rf "${TMPDIR:?}"
38}
39
40trap cleanup EXIT INT TERM ERR
41
42#
43# setup_vendor
44#
45# $1: device name
46# $2: vendor name
47# $3: CM root directory
48# $4: is common device - optional, default to false
49# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050050# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070051#
52# Must be called before any other functions can be used. This
53# sets up the internal state for a new vendor configuration.
54#
55function setup_vendor() {
56 local DEVICE="$1"
57 if [ -z "$DEVICE" ]; then
58 echo "\$DEVICE must be set before including this script!"
59 exit 1
60 fi
61
62 export VENDOR="$2"
63 if [ -z "$VENDOR" ]; then
64 echo "\$VENDOR must be set before including this script!"
65 exit 1
66 fi
67
68 export CM_ROOT="$3"
69 if [ ! -d "$CM_ROOT" ]; then
70 echo "\$CM_ROOT must be set and valid before including this script!"
71 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
75 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
76 mkdir -p "$CM_ROOT/$OUTDIR"
77 fi
78
Jake Whatley9843b322017-01-25 21:49:16 -050079 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
Jake Whatleycb7cd072016-09-18 20:55:12 +020084 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/device-vendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070085 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
86 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
87
88 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
89 COMMON=1
90 else
91 COMMON=0
92 fi
93
Gabriele Mc44696d2017-05-01 18:22:04 +020094 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070095 VENDOR_STATE=1
96 VENDOR_RADIO_STATE=1
97 else
98 VENDOR_STATE=0
99 VENDOR_RADIO_STATE=0
100 fi
101}
102
103#
104# target_file:
105#
106# $1: colon delimited list
107#
108# Returns destination filename without args
109#
110function target_file() {
111 local LINE="$1"
112 local SPLIT=(${LINE//:/ })
113 local COUNT=${#SPLIT[@]}
114 if [ "$COUNT" -gt "1" ]; then
115 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
116 printf '%s\n' "${SPLIT[1]}"
117 return 0
118 fi
119 fi
120 printf '%s\n' "${SPLIT[0]}"
121}
122
123#
124# target_args:
125#
126# $1: colon delimited list
127#
128# Returns optional arguments (last value) for given target
129#
130function target_args() {
131 local LINE="$1"
132 local SPLIT=(${LINE//:/ })
133 local COUNT=${#SPLIT[@]}
134 if [ "$COUNT" -gt "1" ]; then
135 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
136 printf '%s\n' "${SPLIT[$COUNT-1]}"
137 fi
138 fi
139}
140
141#
142# prefix_match:
143#
144# $1: the prefix to match on
145#
146# Internal function which loops thru the packages list and returns a new
147# list containing the matched files with the prefix stripped away.
148#
149function prefix_match() {
150 local PREFIX="$1"
151 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
152 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
153 printf '%s\n' "${FILE#$PREFIX}"
154 fi
155 done
156}
157
158#
159# write_product_copy_files:
160#
161# Creates the PRODUCT_COPY_FILES section in the product makefile for all
162# items in the list which do not start with a dash (-).
163#
164function write_product_copy_files() {
165 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
166 local TARGET=
167 local FILE=
168 local LINEEND=
169
170 if [ "$COUNT" -eq "0" ]; then
171 return 0
172 fi
173
174 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
175 for (( i=1; i<COUNT+1; i++ )); do
176 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
177 LINEEND=" \\"
178 if [ "$i" -eq "$COUNT" ]; then
179 LINEEND=""
180 fi
181
182 TARGET=$(target_file "$FILE")
183 printf ' %s/proprietary/%s:system/%s%s\n' \
184 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
185 done
186 return 0
187}
188
189#
190# write_packages:
191#
192# $1: The LOCAL_MODULE_CLASS for the given module list
193# $2: "true" if this package is part of the vendor/ path
194# $3: type-specific extra flags
195# $4: Name of the array holding the target list
196#
197# Internal function which writes out the BUILD_PREBUILT stanzas
198# for all modules in the list. This is called by write_product_packages
199# after the modules are categorized.
200#
201function write_packages() {
202
203 local CLASS="$1"
204 local VENDOR_PKG="$2"
205 local EXTRA="$3"
206
207 # Yes, this is a horrible hack - we create a new array using indirection
208 local ARR_NAME="$4[@]"
209 local FILELIST=("${!ARR_NAME}")
210
211 local FILE=
212 local ARGS=
213 local BASENAME=
214 local EXTENSION=
215 local PKGNAME=
216 local SRC=
217
218 for P in "${FILELIST[@]}"; do
219 FILE=$(target_file "$P")
220 ARGS=$(target_args "$P")
221
222 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100223 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700224 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
M1cha3e8c5bf2017-01-04 09:00:11 +0100304 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
305 if [ "$DIRNAME" != "." ]; then
306 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
307 fi
308 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700309 if [ "$EXTRA" = "priv-app" ]; then
310 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
311 fi
312 if [ "$VENDOR_PKG" = "true" ]; then
313 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
314 fi
315 printf 'include $(BUILD_PREBUILT)\n\n'
316 done
317}
318
319#
320# write_product_packages:
321#
322# This function will create BUILD_PREBUILT entries in the
323# Android.mk and associated PRODUCT_PACKAGES list in the
324# product makefile for all files in the blob list which
325# start with a single dash (-) character.
326#
327function write_product_packages() {
328 PACKAGE_LIST=()
329
330 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
331
332 if [ "$COUNT" = "0" ]; then
333 return 0
334 fi
335
336 # Figure out what's 32-bit, what's 64-bit, and what's multilib
337 # I really should not be doing this in bash due to shitty array passing :(
338 local T_LIB32=( $(prefix_match "lib/") )
339 local T_LIB64=( $(prefix_match "lib64/") )
340 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
341 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
342 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
343
344 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
345 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
346 fi
347 if [ "${#LIB32[@]}" -gt "0" ]; then
348 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
349 fi
350 if [ "${#LIB64[@]}" -gt "0" ]; then
351 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
352 fi
353
354 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
355 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
356 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
357 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
358 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
359
360 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
361 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
362 fi
363 if [ "${#V_LIB32[@]}" -gt "0" ]; then
364 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
365 fi
366 if [ "${#V_LIB64[@]}" -gt "0" ]; then
367 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
368 fi
369
370 # Apps
371 local APPS=( $(prefix_match "app/") )
372 if [ "${#APPS[@]}" -gt "0" ]; then
373 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
374 fi
375 local PRIV_APPS=( $(prefix_match "priv-app/") )
376 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
377 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
378 fi
379 local V_APPS=( $(prefix_match "vendor/app/") )
380 if [ "${#V_APPS[@]}" -gt "0" ]; then
381 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
382 fi
383 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
384 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
385 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
386 fi
387
388 # Framework
389 local FRAMEWORK=( $(prefix_match "framework/") )
390 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
391 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
392 fi
393
394 # Etc
395 local ETC=( $(prefix_match "etc/") )
396 if [ "${#ETC[@]}" -gt "0" ]; then
397 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
398 fi
399 local V_ETC=( $(prefix_match "vendor/etc/") )
400 if [ "${#V_ETC[@]}" -gt "0" ]; then
401 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
402 fi
403
404 # Executables
405 local BIN=( $(prefix_match "bin/") )
406 if [ "${#BIN[@]}" -gt "0" ]; then
407 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
408 fi
409 local V_BIN=( $(prefix_match "vendor/bin/") )
410 if [ "${#V_BIN[@]}" -gt "0" ]; then
411 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
412 fi
413 local SBIN=( $(prefix_match "sbin/") )
414 if [ "${#SBIN[@]}" -gt "0" ]; then
415 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
416 fi
417
418
419 # Actually write out the final PRODUCT_PACKAGES list
420 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
421
422 if [ "$PACKAGE_COUNT" -eq "0" ]; then
423 return 0
424 fi
425
426 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
427 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
428 local LINEEND=" \\"
429 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
430 LINEEND=""
431 fi
432 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
433 done
434}
435
436#
437# write_header:
438#
439# $1: file which will be written to
440#
441# writes out the copyright header with the current year.
442# note that this is not an append operation, and should
443# be executed first!
444#
445function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500446 if [ -f $1 ]; then
447 rm $1
448 fi
449
Steve Kondik5bd66602016-07-15 10:39:58 -0700450 YEAR=$(date +"%Y")
451
452 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
453
Jake Whatley9843b322017-01-25 21:49:16 -0500454 NUM_REGEX='^[0-9]+$'
455 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
456 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
457 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
458 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
459 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
460 fi
461 if [ $YEAR -eq 2017 ]; then
462 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
463 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
464 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
465 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
466 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
467 else
468 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
469 fi
470 else
471 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
472 fi
473
474 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700475#
476# Licensed under the Apache License, Version 2.0 (the "License");
477# you may not use this file except in compliance with the License.
478# You may obtain a copy of the License at
479#
480# http://www.apache.org/licenses/LICENSE-2.0
481#
482# Unless required by applicable law or agreed to in writing, software
483# distributed under the License is distributed on an "AS IS" BASIS,
484# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
485# See the License for the specific language governing permissions and
486# limitations under the License.
487
488# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
489
490EOF
491}
492
493#
494# write_headers:
495#
496# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500497# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700498#
499# Calls write_header for each of the makefiles and creates
500# the initial path declaration and device guard for the
501# Android.mk
502#
503function write_headers() {
504 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500505
506 GUARD="$2"
507 if [ -z "$GUARD" ]; then
508 GUARD="TARGET_DEVICE"
509 fi
510
Steve Kondik5bd66602016-07-15 10:39:58 -0700511 cat << EOF >> "$ANDROIDMK"
512LOCAL_PATH := \$(call my-dir)
513
514EOF
515 if [ "$COMMON" -ne 1 ]; then
516 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500517ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700518
519EOF
520 else
521 if [ -z "$1" ]; then
522 echo "Argument with devices to be added to guard must be set!"
523 exit 1
524 fi
525 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500526ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700527
528EOF
529 fi
530
531 write_header "$BOARDMK"
532 write_header "$PRODUCTMK"
533}
534
535#
536# write_footers:
537#
538# Closes the inital guard and any other finalization tasks. Must
539# be called as the final step.
540#
541function write_footers() {
542 cat << EOF >> "$ANDROIDMK"
543endif
544EOF
545}
546
547# Return success if adb is up and not in recovery
548function _adb_connected {
549 {
Jake Whatley9843b322017-01-25 21:49:16 -0500550 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700551 then
552 return 0
553 fi
554 } 2>/dev/null
555
556 return 1
557};
558
559#
560# parse_file_list:
561#
562# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400563# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700564#
565# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
566#
567function parse_file_list() {
568 if [ -z "$1" ]; then
569 echo "An input file is expected!"
570 exit 1
571 elif [ ! -f "$1" ]; then
572 echo "Input file "$1" does not exist!"
573 exit 1
574 fi
575
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400576 if [ $# -eq 2 ]; then
577 LIST=$TMPDIR/files.txt
578 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
579 else
580 LIST=$1
581 fi
582
583
Steve Kondik5bd66602016-07-15 10:39:58 -0700584 PRODUCT_PACKAGES_LIST=()
585 PRODUCT_PACKAGES_HASHES=()
586 PRODUCT_COPY_FILES_LIST=()
587 PRODUCT_COPY_FILES_HASHES=()
588
589 while read -r line; do
590 if [ -z "$line" ]; then continue; fi
591
592 # If the line has a pipe delimiter, a sha1 hash should follow.
593 # This indicates the file should be pinned and not overwritten
594 # when extracting files.
595 local SPLIT=(${line//\|/ })
596 local COUNT=${#SPLIT[@]}
597 local SPEC=${SPLIT[0]}
598 local HASH="x"
599 if [ "$COUNT" -gt "1" ]; then
600 HASH=${SPLIT[1]}
601 fi
602
603 # if line starts with a dash, it needs to be packaged
604 if [[ "$SPEC" =~ ^- ]]; then
605 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
606 PRODUCT_PACKAGES_HASHES+=("$HASH")
607 else
608 PRODUCT_COPY_FILES_LIST+=("$SPEC")
609 PRODUCT_COPY_FILES_HASHES+=("$HASH")
610 fi
611
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400612 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700613}
614
615#
616# write_makefiles:
617#
618# $1: file containing the list of items to extract
619#
620# Calls write_product_copy_files and write_product_packages on
621# the given file and appends to the Android.mk as well as
622# the product makefile.
623#
624function write_makefiles() {
625 parse_file_list "$1"
626 write_product_copy_files
627 write_product_packages
628}
629
630#
631# append_firmware_calls_to_makefiles:
632#
633# Appends to Android.mk the calls to all images present in radio folder
634# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
635#
636function append_firmware_calls_to_makefiles() {
637 cat << EOF >> "$ANDROIDMK"
638ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
639
640RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
641\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
642 \$(call add-radio-file,radio/\$(f)))
643\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
644
645endif
646
647EOF
648}
649
650#
651# get_file:
652#
653# $1: input file
654# $2: target file/folder
655# $3: source of the file (can be "adb" or a local folder)
656#
657# Silently extracts the input file to defined target
658# Returns success if file can be pulled from the device or found locally
659#
660function get_file() {
661 local SRC="$3"
662
663 if [ "$SRC" = "adb" ]; then
664 # try to pull
665 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
666
667 return 1
668 else
669 # try to copy
Jake Whatley9843b322017-01-25 21:49:16 -0500670 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700671
672 return 1
673 fi
674};
675
676#
677# oat2dex:
678#
679# $1: extracted apk|jar (to check if deodex is required)
680# $2: odexed apk|jar to deodex
681# $3: source of the odexed apk|jar
682#
683# Convert apk|jar .odex in the corresposing classes.dex
684#
685function oat2dex() {
686 local CM_TARGET="$1"
687 local OEM_TARGET="$2"
688 local SRC="$3"
689 local TARGET=
Sam Mortimer26329022017-08-23 18:59:52 -0700690 local DEXBASE=
691 local ODEX=
692 local VDEX=
Steve Kondik5bd66602016-07-15 10:39:58 -0700693
694 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Marko Man6a182a42016-09-18 01:59:20 +0200695 export BAKSMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
696 export SMALIJAR="$CM_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -0700697 fi
698
699 # Extract existing boot.oats to the temp folder
700 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500701 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700702 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500703 mkdir -p "$TMPDIR/system/framework/$ARCH"
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -0400704 if [ -d "$SRC/framework" ] && [ "$SRC" != "adb" ]; then
705 ARCHDIR="framework/$ARCH/"
706 else
707 ARCHDIR="system/framework/$ARCH/"
708 fi
709 if get_file "$ARCHDIR" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700710 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500711 else
712 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700713 fi
714 done
715 fi
716
717 if [ -z "$ARCHES" ]; then
718 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
719 fi
720
721 if [ ! -f "$CM_TARGET" ]; then
722 return;
723 fi
724
725 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
726 return 0 # target apk|jar is already odexed, return
727 fi
728
729 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500730 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700731
Sam Mortimer26329022017-08-23 18:59:52 -0700732 local DEXBASE="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}")"
733 local ODEX="${DEXBASE}.odex"
734 local VDEX="${DEXBASE}.vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700735
Sam Mortimer26329022017-08-23 18:59:52 -0700736 if get_file "$ODEX" "$TMPDIR" "$SRC"; then
737 # If there was an odex, also look for a vdex.
738 get_file "$VDEX" "$TMPDIR" "$SRC"
739 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$ODEX")"
Steve Kondik5bd66602016-07-15 10:39:58 -0700740 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500741 # try to extract classes.dex from boot.oats for framework jars
742 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
743 if [ ! -f "$JAROAT" ]; then
744 JAROAT=$BOOTOAT;
745 fi
746 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
Steve Kondik5bd66602016-07-15 10:39:58 -0700747 else
748 continue
749 fi
750
Jake Whatley9843b322017-01-25 21:49:16 -0500751 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
Steve Kondik5bd66602016-07-15 10:39:58 -0700752 done
753
754 rm -rf "$TMPDIR/dexout"
755}
756
757#
758# init_adb_connection:
759#
760# Starts adb server and waits for the device
761#
762function init_adb_connection() {
763 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
764 if ! _adb_connected; then
765 echo "No device is online. Waiting for one..."
766 echo "Please connect USB and/or enable USB debugging"
767 until _adb_connected; do
768 sleep 1
769 done
770 echo "Device Found."
771 fi
772
773 # Retrieve IP and PORT info if we're using a TCP connection
774 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
775 | head -1 | awk '{print $1}')
776 adb root &> /dev/null
777 sleep 0.3
778 if [ -n "$TCPIPPORT" ]; then
779 # adb root just killed our connection
780 # so reconnect...
781 adb connect "$TCPIPPORT"
782 fi
783 adb wait-for-device &> /dev/null
784 sleep 0.3
785}
786
787#
788# fix_xml:
789#
790# $1: xml file to fix
791#
792function fix_xml() {
793 local XML="$1"
794 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
795
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200796 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
797 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700798
799 mv "$TEMP_XML" "$XML"
800}
801
802#
803# extract:
804#
805# $1: file containing the list of items to extract
Dan Pasanen0cc05012017-03-21 09:06:11 -0500806# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400807# $3: section in list file to extract - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700808#
809function extract() {
810 if [ -z "$OUTDIR" ]; then
811 echo "Output dir not set!"
812 exit 1
813 fi
814
Harry Youd972c4112017-08-05 09:18:56 +0100815 if [ -z "$3" ]; then
816 parse_file_list "$1"
817 else
818 parse_file_list "$1" "$3"
819 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700820
821 # Allow failing, so we can try $DEST and/or $FILE
822 set +e
823
824 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
825 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
826 local COUNT=${#FILELIST[@]}
827 local SRC="$2"
828 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
829 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
830
831 if [ "$SRC" = "adb" ]; then
832 init_adb_connection
833 fi
834
Dan Pasanen0cc05012017-03-21 09:06:11 -0500835 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
836 DUMPDIR="$CM_ROOT"/system_dump
837
838 # Check if we're working with the same zip that was passed last time.
839 # If so, let's just use what's already extracted.
840 MD5=`md5sum "$SRC"| awk '{print $1}'`
841 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
842
843 if [ "$MD5" != "$OLDMD5" ]; then
844 rm -rf "$DUMPDIR"
845 mkdir "$DUMPDIR"
846 unzip "$SRC" -d "$DUMPDIR"
847 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
848
849 # Stop if an A/B OTA zip is detected. We cannot extract these.
850 if [ -a "$DUMPDIR"/payload.bin ]; then
851 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
852 exit 1
853 # If OTA is block based, extract it.
854 elif [ -a "$DUMPDIR"/system.new.dat ]; then
855 echo "Converting system.new.dat to system.img"
856 python "$CM_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
857 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
858 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
859 echo "Requesting sudo access to mount the system.img"
860 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
861 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
862 sudo umount "$DUMPDIR"/tmp
863 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
864 fi
865 fi
866
867 SRC="$DUMPDIR"
868 fi
869
Steve Kondik5bd66602016-07-15 10:39:58 -0700870 if [ "$VENDOR_STATE" -eq "0" ]; then
871 echo "Cleaning output directory ($OUTPUT_ROOT).."
872 rm -rf "${OUTPUT_TMP:?}"
873 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -0500874 if [ -d "$OUTPUT_ROOT" ]; then
875 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
876 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700877 VENDOR_STATE=1
878 fi
879
880 echo "Extracting $COUNT files in $1 from $SRC:"
881
882 for (( i=1; i<COUNT+1; i++ )); do
883
884 local FROM=$(target_file "${FILELIST[$i-1]}")
885 local ARGS=$(target_args "${FILELIST[$i-1]}")
886 local SPLIT=(${FILELIST[$i-1]//:/ })
887 local FILE="${SPLIT[0]#-}"
888 local OUTPUT_DIR="$OUTPUT_ROOT"
889 local TMP_DIR="$OUTPUT_TMP"
890 local TARGET=
891
892 if [ "$ARGS" = "rootfs" ]; then
893 TARGET="$FROM"
894 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
895 TMP_DIR="$TMP_DIR/rootfs"
Rashed Abdel-Tawab7da4a1b2017-04-04 18:03:35 -0400896 elif [ -f "$SRC/$FILE" ] && [ "$SRC" != "adb" ]; then
897 TARGET="$FROM"
Steve Kondik5bd66602016-07-15 10:39:58 -0700898 else
899 TARGET="system/$FROM"
900 FILE="system/$FILE"
901 fi
902
903 if [ "$SRC" = "adb" ]; then
904 printf ' - %s .. ' "/$TARGET"
905 else
906 printf ' - %s \n' "/$TARGET"
907 fi
908
909 local DIR=$(dirname "$FROM")
910 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
911 mkdir -p "$OUTPUT_DIR/$DIR"
912 fi
913 local DEST="$OUTPUT_DIR/$FROM"
914
915 if [ "$SRC" = "adb" ]; then
916 # Try CM target first
917 adb pull "/$TARGET" "$DEST"
918 # if file does not exist try OEM target
919 if [ "$?" != "0" ]; then
920 adb pull "/$FILE" "$DEST"
921 fi
922 else
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -0500923 # Try CM target first
924 if [ -f "$SRC/$TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700925 cp "$SRC/$TARGET" "$DEST"
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -0500926 # if file does not exist try OEM target
927 elif [ -f "$SRC/$FILE" ]; then
928 cp "$SRC/$FILE" "$DEST"
Steve Kondik5bd66602016-07-15 10:39:58 -0700929 else
930 printf ' !! file not found in source\n'
931 fi
932 fi
933
934 if [ "$?" == "0" ]; then
935 # Deodex apk|jar if that's the case
936 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
937 oat2dex "$DEST" "$FILE" "$SRC"
938 if [ -f "$TMPDIR/classes.dex" ]; then
939 zip -gjq "$DEST" "$TMPDIR/classes.dex"
940 rm "$TMPDIR/classes.dex"
941 printf ' (updated %s from odex files)\n' "/$FILE"
942 fi
943 elif [[ "$DEST" =~ .xml$ ]]; then
944 fix_xml "$DEST"
945 fi
946 fi
947
948 # Check pinned files
949 local HASH="${HASHLIST[$i-1]}"
Jake Whatley9843b322017-01-25 21:49:16 -0500950 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700951 local KEEP=""
952 local TMP="$TMP_DIR/$FROM"
953 if [ -f "$TMP" ]; then
954 if [ ! -f "$DEST" ]; then
955 KEEP="1"
956 else
Jake Whatley9843b322017-01-25 21:49:16 -0500957 if [ "$(uname)" == "Darwin" ]; then
958 local DEST_HASH=$(shasum "$DEST" | awk '{print $1}' )
959 else
960 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
961 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700962 if [ "$DEST_HASH" != "$HASH" ]; then
963 KEEP="1"
964 fi
965 fi
966 if [ "$KEEP" = "1" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500967 if [ "$(uname)" == "Darwin" ]; then
968 local TMP_HASH=$(shasum "$TMP" | awk '{print $1}' )
969 else
970 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
971 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700972 if [ "$TMP_HASH" = "$HASH" ]; then
973 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
974 cp -p "$TMP" "$DEST"
975 fi
976 fi
977 fi
978 fi
979
980 if [ -f "$DEST" ]; then
981 local TYPE="${DIR##*/}"
982 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
983 chmod 755 "$DEST"
984 else
985 chmod 644 "$DEST"
986 fi
987 fi
988
989 done
990
991 # Don't allow failing
992 set -e
993}
994
995#
996# extract_firmware:
997#
998# $1: file containing the list of items to extract
999# $2: path to extracted radio folder
1000#
1001function extract_firmware() {
1002 if [ -z "$OUTDIR" ]; then
1003 echo "Output dir not set!"
1004 exit 1
1005 fi
1006
1007 parse_file_list "$1"
1008
1009 # Don't allow failing
1010 set -e
1011
1012 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1013 local COUNT=${#FILELIST[@]}
1014 local SRC="$2"
1015 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
1016
1017 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1018 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1019 rm -rf "${OUTPUT_DIR:?}/"*
1020 VENDOR_RADIO_STATE=1
1021 fi
1022
1023 echo "Extracting $COUNT files in $1 from $SRC:"
1024
1025 for (( i=1; i<COUNT+1; i++ )); do
1026 local FILE="${FILELIST[$i-1]}"
1027 printf ' - %s \n' "/radio/$FILE"
1028
1029 if [ ! -d "$OUTPUT_DIR" ]; then
1030 mkdir -p "$OUTPUT_DIR"
1031 fi
1032 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1033 chmod 644 "$OUTPUT_DIR/$FILE"
1034 done
1035}