blob: 2295ee24b152862e9303f19043fdfe550389d47f [file] [log] [blame]
Joe Maples2cafab12018-08-08 09:39:44 -04001#!/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=$(mktemp -d)
30
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
50# $6: custom vendor makefile name - optional, default to false
51#
52# Must be called before any other functions can be used. This
53# sets up the internal state for a new vendor configuration.
54#
55function setup_vendor() {
56 local DEVICE="$1"
57 if [ -z "$DEVICE" ]; then
58 echo "\$DEVICE must be set before including this script!"
59 exit 1
60 fi
61
62 export VENDOR="$2"
63 if [ -z "$VENDOR" ]; then
64 echo "\$VENDOR must be set before including this script!"
65 exit 1
66 fi
67
68 export CM_ROOT="$3"
69 if [ ! -d "$CM_ROOT" ]; then
70 echo "\$CM_ROOT must be set and valid before including this script!"
71 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
75 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
76 mkdir -p "$CM_ROOT/$OUTDIR"
77 fi
78
79 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
84 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
85 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
94 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
95 VENDOR_STATE=1
96 VENDOR_RADIO_STATE=1
97 else
98 VENDOR_STATE=0
99 VENDOR_RADIO_STATE=0
100 fi
101}
102
103# Helper functions for parsing a spec.
104# notes: an optional "|SHA1" that may appear in the format is stripped
105# early from the spec in the parse_file_list function, and
106# should not be present inside the input parameter passed
107# to these functions.
108
109#
110# input: spec in the form of "src[:dst][;args]"
111# output: "src"
112#
113function src_file() {
114 local SPEC="$1"
115 local SPLIT=(${SPEC//:/ })
116 local ARGS="$(target_args ${SPEC})"
117 # Regardless of there being a ":" delimiter or not in the spec,
118 # the source file is always either the first, or the only entry.
119 local SRC="${SPLIT[0]}"
120 # Remove target_args suffix, if present
121 echo "${SRC%;${ARGS}}"
122}
123
124#
125# input: spec in the form of "src[:dst][;args]"
126# output: "dst" if present, "src" otherwise.
127#
128function target_file() {
129 local SPEC="$1"
130 local SPLIT=(${SPEC//:/ })
131 local ARGS="$(target_args ${SPEC})"
132 local DST=
133 case ${#SPLIT[@]} in
134 1)
135 # The spec doesn't have a : delimiter
136 DST="${SPLIT[0]}"
137 ;;
138 *)
139 # The spec actually has a src:dst format
140 DST="${SPLIT[1]}"
141 ;;
142 esac
143 # Remove target_args suffix, if present
144 echo "${DST%;${ARGS}}"
145}
146
147#
148# input: spec in the form of "src[:dst][;args]"
149# output: "args" if present, "" otherwise.
150#
151function target_args() {
152 local SPEC="$1"
153 local SPLIT=(${SPEC//;/ })
154 local ARGS=
155 case ${#SPLIT[@]} in
156 1)
157 # No ";" delimiter in the spec.
158 ;;
159 *)
160 # The "args" are whatever comes after the ";" character.
161 # Basically the spec stripped of whatever is to the left of ";".
162 ARGS="${SPEC#${SPLIT[0]};}"
163 ;;
164 esac
165 echo "${ARGS}"
166}
167
168#
169# prefix_match:
170#
171# input:
172# - $1: prefix
173# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
174# output:
175# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
176#
177function prefix_match() {
178 local PREFIX="$1"
179 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
180 local FILE=$(target_file "$LINE")
181 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
182 local ARGS=$(target_args "$LINE")
183 if [ -z "${ARGS}" ]; then
184 echo "${FILE#$PREFIX}"
185 else
186 echo "${FILE#$PREFIX};${ARGS}"
187 fi
188 fi
189 done
190}
191
192#
193# prefix_match_file:
194#
195# $1: the prefix to match on
196# $2: the file to match the prefix for
197#
198# Internal function which returns true if a filename contains the
199# specified prefix.
200#
201function prefix_match_file() {
202 local PREFIX="$1"
203 local FILE="$2"
204 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
205 return 0
206 else
207 return 1
208 fi
209}
210
211#
212# truncate_file
213#
214# $1: the filename to truncate
215# $2: the argument to output the truncated filename to
216#
217# Internal function which truncates a filename by removing the first dir
218# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
219#
220function truncate_file() {
221 local FILE="$1"
222 RETURN_FILE="$2"
223 local FIND="${FILE%%/*}"
224 local LOCATION="${#FIND}+1"
225 echo ${FILE:$LOCATION}
226}
227
228#
229# write_product_copy_files:
230#
231# $1: make treble compatible makefile - optional, default to false
232#
233# Creates the PRODUCT_COPY_FILES section in the product makefile for all
234# items in the list which do not start with a dash (-).
235#
236function write_product_copy_files() {
237 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
238 local TARGET=
239 local FILE=
240 local LINEEND=
241 local TREBLE_COMPAT=$1
242
243 if [ "$COUNT" -eq "0" ]; then
244 return 0
245 fi
246
247 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
248 for (( i=1; i<COUNT+1; i++ )); do
249 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
250 LINEEND=" \\"
251 if [ "$i" -eq "$COUNT" ]; then
252 LINEEND=""
253 fi
254
255 TARGET=$(target_file "$FILE")
256 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
257 if prefix_match_file "vendor/" $TARGET ; then
258 local OUTTARGET=$(truncate_file $TARGET)
259 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
260 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
261 else
262 printf ' %s/proprietary/%s:system/%s%s\n' \
263 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
264 fi
265 else
266 printf ' %s/proprietary/%s:system/%s%s\n' \
267 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
268 fi
269 done
270 return 0
271}
272
273#
274# write_packages:
275#
276# $1: The LOCAL_MODULE_CLASS for the given module list
277# $2: "true" if this package is part of the vendor/ path
278# $3: type-specific extra flags
279# $4: Name of the array holding the target list
280#
281# Internal function which writes out the BUILD_PREBUILT stanzas
282# for all modules in the list. This is called by write_product_packages
283# after the modules are categorized.
284#
285function write_packages() {
286
287 local CLASS="$1"
288 local VENDOR_PKG="$2"
289 local EXTRA="$3"
290
291 # Yes, this is a horrible hack - we create a new array using indirection
292 local ARR_NAME="$4[@]"
293 local FILELIST=("${!ARR_NAME}")
294
295 local FILE=
296 local ARGS=
297 local BASENAME=
298 local EXTENSION=
299 local PKGNAME=
300 local SRC=
301
302 for P in "${FILELIST[@]}"; do
303 FILE=$(target_file "$P")
304 ARGS=$(target_args "$P")
305
306 BASENAME=$(basename "$FILE")
307 DIRNAME=$(dirname "$FILE")
308 EXTENSION=${BASENAME##*.}
309 PKGNAME=${BASENAME%.*}
310
311 # Add to final package list
312 PACKAGE_LIST+=("$PKGNAME")
313
314 SRC="proprietary"
315 if [ "$VENDOR_PKG" = "true" ]; then
316 SRC+="/vendor"
317 fi
318
319 printf 'include $(CLEAR_VARS)\n'
320 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
321 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
322 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
323 if [ "$EXTRA" = "both" ]; then
324 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
325 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
326 #if [ "$VENDOR_PKG" = "true" ]; then
327 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
328 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
329 #else
330 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
331 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
332 #fi
333 elif [ "$EXTRA" = "64" ]; then
334 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
335 else
336 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
337 fi
338 if [ "$EXTRA" != "none" ]; then
339 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
340 fi
341 elif [ "$CLASS" = "APPS" ]; then
342 if [ "$EXTRA" = "priv-app" ]; then
343 SRC="$SRC/priv-app"
344 else
345 SRC="$SRC/app"
346 fi
347 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
348 local CERT=platform
349 if [ ! -z "$ARGS" ]; then
350 CERT="$ARGS"
351 fi
352 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
353 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
354 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
355 local CERT=platform
356 if [ ! -z "$ARGS" ]; then
357 CERT="$ARGS"
358 fi
359 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
360 elif [ "$CLASS" = "ETC" ]; then
361 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
362 elif [ "$CLASS" = "EXECUTABLES" ]; then
363 if [ "$ARGS" = "rootfs" ]; then
364 SRC="$SRC/rootfs"
365 if [ "$EXTRA" = "sbin" ]; then
366 SRC="$SRC/sbin"
367 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
368 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
369 fi
370 else
371 SRC="$SRC/bin"
372 fi
373 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
374 unset EXTENSION
375 else
376 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
377 fi
378 printf 'LOCAL_MODULE_TAGS := optional\n'
379 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Christian Oder3f770372018-08-10 23:13:11 +0200380 if [ "$CLASS" = "APPS" ] || [ "$CLASS" = "JAVA_LIBRARIES" ]; then
Joe Maples2cafab12018-08-08 09:39:44 -0400381 printf 'LOCAL_DEX_PREOPT := false\n'
382 fi
383 if [ ! -z "$EXTENSION" ]; then
384 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
385 fi
386 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
387 if [ "$DIRNAME" != "." ]; then
388 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
389 fi
390 fi
391 if [ "$EXTRA" = "priv-app" ]; then
392 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
393 fi
394 if [ "$VENDOR_PKG" = "true" ]; then
395 printf 'LOCAL_VENDOR_MODULE := true\n'
396 fi
397 printf 'include $(BUILD_PREBUILT)\n\n'
398 done
399}
400
401#
402# write_product_packages:
403#
404# This function will create BUILD_PREBUILT entries in the
405# Android.mk and associated PRODUCT_PACKAGES list in the
406# product makefile for all files in the blob list which
407# start with a single dash (-) character.
408#
409function write_product_packages() {
410 PACKAGE_LIST=()
411
412 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
413
414 if [ "$COUNT" = "0" ]; then
415 return 0
416 fi
417
418 # Figure out what's 32-bit, what's 64-bit, and what's multilib
419 # I really should not be doing this in bash due to shitty array passing :(
420 local T_LIB32=( $(prefix_match "lib/") )
421 local T_LIB64=( $(prefix_match "lib64/") )
422 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
423 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
424 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
425
426 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
427 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
428 fi
429 if [ "${#LIB32[@]}" -gt "0" ]; then
430 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
431 fi
432 if [ "${#LIB64[@]}" -gt "0" ]; then
433 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
434 fi
435
436 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
437 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
438 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
439 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
440 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
441
442 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
443 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
444 fi
445 if [ "${#V_LIB32[@]}" -gt "0" ]; then
446 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
447 fi
448 if [ "${#V_LIB64[@]}" -gt "0" ]; then
449 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
450 fi
451
452 # Apps
453 local APPS=( $(prefix_match "app/") )
454 if [ "${#APPS[@]}" -gt "0" ]; then
455 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
456 fi
457 local PRIV_APPS=( $(prefix_match "priv-app/") )
458 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
459 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
460 fi
461 local V_APPS=( $(prefix_match "vendor/app/") )
462 if [ "${#V_APPS[@]}" -gt "0" ]; then
463 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
464 fi
465 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
466 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
467 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
468 fi
469
470 # Framework
471 local FRAMEWORK=( $(prefix_match "framework/") )
472 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
473 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
474 fi
475 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
476 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
477 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
478 fi
479
480 # Etc
481 local ETC=( $(prefix_match "etc/") )
482 if [ "${#ETC[@]}" -gt "0" ]; then
483 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
484 fi
485 local V_ETC=( $(prefix_match "vendor/etc/") )
486 if [ "${#V_ETC[@]}" -gt "0" ]; then
487 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
488 fi
489
490 # Executables
491 local BIN=( $(prefix_match "bin/") )
492 if [ "${#BIN[@]}" -gt "0" ]; then
493 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
494 fi
495 local V_BIN=( $(prefix_match "vendor/bin/") )
496 if [ "${#V_BIN[@]}" -gt "0" ]; then
497 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
498 fi
499 local SBIN=( $(prefix_match "sbin/") )
500 if [ "${#SBIN[@]}" -gt "0" ]; then
501 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
502 fi
503
504
505 # Actually write out the final PRODUCT_PACKAGES list
506 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
507
508 if [ "$PACKAGE_COUNT" -eq "0" ]; then
509 return 0
510 fi
511
512 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
513 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
514 local LINEEND=" \\"
515 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
516 LINEEND=""
517 fi
518 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
519 done
520}
521
522#
523# write_header:
524#
525# $1: file which will be written to
526#
527# writes out the copyright header with the current year.
528# note that this is not an append operation, and should
529# be executed first!
530#
531function write_header() {
532 if [ -f $1 ]; then
533 rm $1
534 fi
535
536 YEAR=$(date +"%Y")
537
538 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
539
540 NUM_REGEX='^[0-9]+$'
541 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
542 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
543 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
544 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
545 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
546 fi
547 if [ $YEAR -eq 2017 ]; then
548 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
549 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
550 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
551 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
552 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
553 else
554 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
555 fi
556 else
557 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
558 fi
559
560 cat << EOF >> $1
561#
562# Licensed under the Apache License, Version 2.0 (the "License");
563# you may not use this file except in compliance with the License.
564# You may obtain a copy of the License at
565#
566# http://www.apache.org/licenses/LICENSE-2.0
567#
568# Unless required by applicable law or agreed to in writing, software
569# distributed under the License is distributed on an "AS IS" BASIS,
570# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
571# See the License for the specific language governing permissions and
572# limitations under the License.
573
574# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
575
576EOF
577}
578
579#
580# write_headers:
581#
582# $1: devices falling under common to be added to guard - optional
583# $2: custom guard - optional
584#
585# Calls write_header for each of the makefiles and creates
586# the initial path declaration and device guard for the
587# Android.mk
588#
589function write_headers() {
590 write_header "$ANDROIDMK"
591
592 GUARD="$2"
593 if [ -z "$GUARD" ]; then
594 GUARD="TARGET_DEVICE"
595 fi
596
597 cat << EOF >> "$ANDROIDMK"
598LOCAL_PATH := \$(call my-dir)
599
600EOF
601 if [ "$COMMON" -ne 1 ]; then
602 cat << EOF >> "$ANDROIDMK"
603ifeq (\$($GUARD),$DEVICE)
604
605EOF
606 else
607 if [ -z "$1" ]; then
608 echo "Argument with devices to be added to guard must be set!"
609 exit 1
610 fi
611 cat << EOF >> "$ANDROIDMK"
612ifneq (\$(filter $1,\$($GUARD)),)
613
614EOF
615 fi
616
617 write_header "$BOARDMK"
618 write_header "$PRODUCTMK"
619}
620
621#
622# write_footers:
623#
624# Closes the inital guard and any other finalization tasks. Must
625# be called as the final step.
626#
627function write_footers() {
628 cat << EOF >> "$ANDROIDMK"
629endif
630EOF
631}
632
633# Return success if adb is up and not in recovery
634function _adb_connected {
635 {
636 if [[ "$(adb get-state)" == device ]]
637 then
638 return 0
639 fi
640 } 2>/dev/null
641
642 return 1
643};
644
645#
646# parse_file_list:
647#
648# $1: input file
649# $2: blob section in file - optional
650#
651# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
652#
653function parse_file_list() {
654 if [ -z "$1" ]; then
655 echo "An input file is expected!"
656 exit 1
657 elif [ ! -f "$1" ]; then
658 echo "Input file "$1" does not exist!"
659 exit 1
660 fi
661
662 if [ $# -eq 2 ]; then
663 LIST=$TMPDIR/files.txt
664 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
665 else
666 LIST=$1
667 fi
668
669
670 PRODUCT_PACKAGES_LIST=()
671 PRODUCT_PACKAGES_HASHES=()
672 PRODUCT_COPY_FILES_LIST=()
673 PRODUCT_COPY_FILES_HASHES=()
674
675 while read -r line; do
676 if [ -z "$line" ]; then continue; fi
677
678 # If the line has a pipe delimiter, a sha1 hash should follow.
679 # This indicates the file should be pinned and not overwritten
680 # when extracting files.
681 local SPLIT=(${line//\|/ })
682 local COUNT=${#SPLIT[@]}
683 local SPEC=${SPLIT[0]}
684 local HASH="x"
685 if [ "$COUNT" -gt "1" ]; then
686 HASH=${SPLIT[1]}
687 fi
688
689 # if line starts with a dash, it needs to be packaged
690 if [[ "$SPEC" =~ ^- ]]; then
691 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
692 PRODUCT_PACKAGES_HASHES+=("$HASH")
693 else
694 PRODUCT_COPY_FILES_LIST+=("$SPEC")
695 PRODUCT_COPY_FILES_HASHES+=("$HASH")
696 fi
697
698 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
699}
700
701#
702# write_makefiles:
703#
704# $1: file containing the list of items to extract
705# $2: make treble compatible makefile - optional
706#
707# Calls write_product_copy_files and write_product_packages on
708# the given file and appends to the Android.mk as well as
709# the product makefile.
710#
711function write_makefiles() {
712 parse_file_list "$1"
713 write_product_copy_files "$2"
714 write_product_packages
715}
716
717#
718# append_firmware_calls_to_makefiles:
719#
720# Appends to Android.mk the calls to all images present in radio folder
721# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
722#
723function append_firmware_calls_to_makefiles() {
724 cat << EOF >> "$ANDROIDMK"
725ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
726
727RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
728\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
729 \$(call add-radio-file,radio/\$(f)))
730\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
731
732endif
733
734EOF
735}
736
737#
738# get_file:
739#
740# $1: input file
741# $2: target file/folder
742# $3: source of the file (can be "adb" or a local folder)
743#
744# Silently extracts the input file to defined target
745# Returns success if file can be pulled from the device or found locally
746#
747function get_file() {
748 local SRC="$3"
749
750 if [ "$SRC" = "adb" ]; then
751 # try to pull
752 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
753
754 return 1
755 else
756 # try to copy
757 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
758 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
759
760 return 1
761 fi
762};
763
764#
765# oat2dex:
766#
767# $1: extracted apk|jar (to check if deodex is required)
768# $2: odexed apk|jar to deodex
769# $3: source of the odexed apk|jar
770#
771# Convert apk|jar .odex in the corresposing classes.dex
772#
773function oat2dex() {
774 local CM_TARGET="$1"
775 local OEM_TARGET="$2"
776 local SRC="$3"
777 local TARGET=
778 local OAT=
779
780 if [ -z "$ANDROID_HOST_OUT" ]; then
781 echo "ERROR: ANDROID_HOST_OUT not found!"
782 echo "ERROR: Please lunch a device before running this script."
783 exit 1
784 fi
785
786 if [ -z "$OATDUMP" ]; then
787 if [ ! -f "$ANDROID_HOST_OUT/bin/oatdump" ]; then
788 echo "ERROR: oatdump utility not found!"
789 echo "ERROR: Please run 'make oatdump'"
790 echo "ERROR: from the top of the android tree before running this script."
791 exit 1
792 else
793 export OATDUMP="$ANDROID_HOST_OUT/bin/oatdump"
794 fi
795 fi
796
797 # Extract existing boot.oats to the temp folder
798 if [ -z "$ARCHES" ]; then
799 echo "Checking if system is odexed and locating boot.oats..."
800 for ARCH in "arm64" "arm" "x86_64" "x86"; do
801 mkdir -p "$TMPDIR/system/framework/$ARCH"
802 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
803 ARCHES+="$ARCH "
804 else
805 rmdir "$TMPDIR/system/framework/$ARCH"
806 fi
807 done
808 fi
809
810 if [ -z "$ARCHES" ]; then
811 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
812 fi
813
814 if [ ! -f "$CM_TARGET" ]; then
815 return;
816 fi
817
818 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
819 return 0 # target apk|jar is already odexed, return
820 fi
821
822 for ARCH in $ARCHES; do
823 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
824
825 local BASEPATH="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}")"
826 local OAT="$BASEPATH.odex"
827 local VDEX="$BASEPATH.vdex"
828
829 if get_file "$OAT" "$TMPDIR" "$SRC"; then
830 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
831 if !("$OATDUMP" --oat-file="$TMPDIR/$(basename "$VDEX")" --export-dex-to="$TMPDIR" &> /dev/null); then
832 echo "Warning: vdex extraction failed, falling back to odex"
833 "$OATDUMP" --oat-file="$TMPDIR/$(basename "$OAT")" --export-dex-to="$TMPDIR" > /dev/null
834 fi
835 else
836 "$OATDUMP" --oat-file="$TMPDIR/$(basename "$OAT")" --export-dex-to="$TMPDIR" > /dev/null
837 fi
838 mv "$TMPDIR/$(basename "$BASEPATH").${OEM_TARGET##*.}_export.dex" "$TMPDIR/classes.dex"
839 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
840 BASEPATH="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*})"
841 JAROAT="$BASEPATH.oat"
842 JARVDEX="$BASEPATH.vdex"
843 if [ ! -f "$JAROAT" ]; then
844 JAROAT=$BOOTOAT;
845 fi
846
847 # try to extract classes.dex from boot.vdex for frameworks jars
848 # fallback to boot.oat if vdex is not available or fails
849 if [ -f "$JARVDEX" ]; then
850 if !("$OATDUMP" --oat-file="$JARVDEX" --export-dex-to="$TMPDIR" &> /dev/null); then
851 echo "Warning: vdex extraction failed, falling back to odex"
852 "$OATDUMP" --oat-file="$JAROAT" --export-dex-to="$TMPDIR" > /dev/null
853 fi
854 else
855 "$OATDUMP" --oat-file="$JAROAT" --export-dex-to="$TMPDIR" > /dev/null
856 fi
857 mv "$TMPDIR/$(basename "$BASEPATH").jar_export.dex" "$TMPDIR/classes.dex"
858 else
859 continue
860 fi
861
862 done
863}
864
865#
866# init_adb_connection:
867#
868# Starts adb server and waits for the device
869#
870function init_adb_connection() {
871 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
872 if ! _adb_connected; then
873 echo "No device is online. Waiting for one..."
874 echo "Please connect USB and/or enable USB debugging"
875 until _adb_connected; do
876 sleep 1
877 done
878 echo "Device Found."
879 fi
880
881 # Retrieve IP and PORT info if we're using a TCP connection
882 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
883 | head -1 | awk '{print $1}')
884 adb root &> /dev/null
885 sleep 0.3
886 if [ -n "$TCPIPPORT" ]; then
887 # adb root just killed our connection
888 # so reconnect...
889 adb connect "$TCPIPPORT"
890 fi
891 adb wait-for-device &> /dev/null
892 sleep 0.3
893}
894
895#
896# fix_xml:
897#
898# $1: xml file to fix
899#
900function fix_xml() {
901 local XML="$1"
902 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
903
904 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
905 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
906
907 mv "$TEMP_XML" "$XML"
908}
909
910#
911# extract:
912#
913# $1: file containing the list of items to extract
914# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
915# $3: section in list file to extract - optional
916#
917function extract() {
918 if [ -z "$OUTDIR" ]; then
919 echo "Output dir not set!"
920 exit 1
921 fi
922
923 if [ -z "$3" ]; then
924 parse_file_list "$1"
925 else
926 parse_file_list "$1" "$3"
927 fi
928
929 # Allow failing, so we can try $DEST and/or $FILE
930 set +e
931
932 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
933 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
934 local COUNT=${#FILELIST[@]}
935 local SRC="$2"
936 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
937 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
938
939 if [ "$SRC" = "adb" ]; then
940 init_adb_connection
941 fi
942
943 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
944 DUMPDIR="$CM_ROOT"/system_dump
945
946 # Check if we're working with the same zip that was passed last time.
947 # If so, let's just use what's already extracted.
948 MD5=`md5sum "$SRC"| awk '{print $1}'`
949 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
950
951 if [ "$MD5" != "$OLDMD5" ]; then
952 rm -rf "$DUMPDIR"
953 mkdir "$DUMPDIR"
954 unzip "$SRC" -d "$DUMPDIR"
955 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
956
957 # Stop if an A/B OTA zip is detected. We cannot extract these.
958 if [ -a "$DUMPDIR"/payload.bin ]; then
959 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
960 exit 1
961 # If OTA is block based, extract it.
962 elif [ -a "$DUMPDIR"/system.new.dat ]; then
963 echo "Converting system.new.dat to system.img"
964 python "$CM_ROOT"/vendor/cm/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
965 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
966 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
967 echo "Requesting sudo access to mount the system.img"
968 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
969 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
970 sudo umount "$DUMPDIR"/tmp
971 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
972 fi
973 fi
974
975 SRC="$DUMPDIR"
976 fi
977
978 if [ "$VENDOR_STATE" -eq "0" ]; then
979 echo "Cleaning output directory ($OUTPUT_ROOT).."
980 rm -rf "${OUTPUT_TMP:?}"
981 mkdir -p "${OUTPUT_TMP:?}"
982 if [ -d "$OUTPUT_ROOT" ]; then
983 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
984 fi
985 VENDOR_STATE=1
986 fi
987
988 echo "Extracting $COUNT files in $1 from $SRC:"
989
990 for (( i=1; i<COUNT+1; i++ )); do
991
992 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
993 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
994 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
995 local OUTPUT_DIR=
996 local TMP_DIR=
997 local SRC_FILE=
998 local DST_FILE=
999
1000 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1001 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1002 TMP_DIR="${OUTPUT_TMP}/rootfs"
1003 SRC_FILE="/${SPEC_SRC_FILE}"
1004 DST_FILE="/${SPEC_DST_FILE}"
1005 else
1006 OUTPUT_DIR="${OUTPUT_ROOT}"
1007 TMP_DIR="${OUTPUT_TMP}"
1008 SRC_FILE="/system/${SPEC_SRC_FILE}"
1009 DST_FILE="/system/${SPEC_DST_FILE}"
1010 fi
1011
1012 if [ "$SRC" = "adb" ]; then
1013 printf ' - %s .. \n' "${DST_FILE}"
1014 else
1015 printf ' - %s \n' "${DST_FILE}"
1016 fi
1017
1018 # Strip the file path in the vendor repo of "system", if present
1019 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
1020 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1021
1022 # Check pinned files
1023 local HASH="${HASHLIST[$i-1]}"
1024 local KEEP=""
1025 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
1026 if [ -f "${VENDOR_REPO_FILE}" ]; then
1027 local PINNED="${VENDOR_REPO_FILE}"
1028 else
1029 local PINNED="${TMP_DIR}${DST_FILE#/system}"
1030 fi
1031 if [ -f "$PINNED" ]; then
1032 if [ "$(uname)" == "Darwin" ]; then
1033 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1034 else
1035 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1036 fi
1037 if [ "$TMP_HASH" = "$HASH" ]; then
1038 KEEP="1"
1039 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1040 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1041 fi
1042 fi
1043 fi
1044 fi
1045
1046 if [ "$KEEP" = "1" ]; then
1047 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
1048 else
1049 FOUND=false
1050 # Try Lineage target first.
1051 # Also try to search for files stripped of
1052 # the "/system" prefix, if we're actually extracting
1053 # from a system image.
1054 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1055 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1056 FOUND=true
1057 break
1058 }
1059 done
1060
1061 if [ "${FOUND}" = false ]; then
1062 printf ' !! file not found in source\n'
1063 fi
1064 fi
1065
1066 if [ "$?" == "0" ]; then
1067 # Deodex apk|jar if that's the case
1068 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1069 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1070 if [ -f "$TMPDIR/classes.dex" ]; then
1071 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes.dex"
1072 rm "$TMPDIR/classes.dex"
1073 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1074 fi
1075 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1076 fix_xml "${VENDOR_REPO_FILE}"
1077 fi
1078 fi
1079
1080 if [ -f "${VENDOR_REPO_FILE}" ]; then
1081 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1082 local TYPE="${DIR##*/}"
1083 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1084 chmod 755 "${VENDOR_REPO_FILE}"
1085 else
1086 chmod 644 "${VENDOR_REPO_FILE}"
1087 fi
1088 fi
1089
1090 done
1091
1092 # Don't allow failing
1093 set -e
1094}
1095
1096#
1097# extract_firmware:
1098#
1099# $1: file containing the list of items to extract
1100# $2: path to extracted radio folder
1101#
1102function extract_firmware() {
1103 if [ -z "$OUTDIR" ]; then
1104 echo "Output dir not set!"
1105 exit 1
1106 fi
1107
1108 parse_file_list "$1"
1109
1110 # Don't allow failing
1111 set -e
1112
1113 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1114 local COUNT=${#FILELIST[@]}
1115 local SRC="$2"
1116 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
1117
1118 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1119 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1120 rm -rf "${OUTPUT_DIR:?}/"*
1121 VENDOR_RADIO_STATE=1
1122 fi
1123
1124 echo "Extracting $COUNT files in $1 from $SRC:"
1125
1126 for (( i=1; i<COUNT+1; i++ )); do
1127 local FILE="${FILELIST[$i-1]}"
1128 printf ' - %s \n' "/radio/$FILE"
1129
1130 if [ ! -d "$OUTPUT_DIR" ]; then
1131 mkdir -p "$OUTPUT_DIR"
1132 fi
1133 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1134 chmod 644 "$OUTPUT_DIR/$FILE"
1135 done
1136}